function calendar_confirm_cancel_appointment_form_submit

6.x calendar.module calendar_confirm_cancel_appointment_form_submit(&$form, &$form_state)

File

modules/calendar/calendar.module, line 517

Code

function calendar_confirm_cancel_appointment_form_submit(&$form, &$form_state) {
  global $user;

  if ($user->is_student) {
    $user_name = fp_get_student_name($user->cwid, FALSE);
  }
  else {
    $user_name = fp_get_faculty_name($user->cwid);
  }

  if (isset($form ['#redirect'])) {
    fp_add_message(t("The appointment was cancelled."));
    $form ['#redirect']['query'] .= "&cancel=yes";
  }
  else {
    // No redirect was specified, so let's just return.
    fp_add_message(t("The appointment was cancelled."));
  }



  $cid = $form_state ['values']['cid'];
  $content = content_load($cid);

  $reason_msg = trim(filter_markup($form_state ['values']['msg'], 'plain'));

  $utc_appointment_datetime = strtotime($content->field__appointment_datetime ['value']);

  $student_cwid = $content->field__student_id ['value'];
  $faculty_cwid = $content->field__faculty_id ['value'];


  // If the appointment had meeting data (eg, Zoom), make sure we send a request to delete the Zoom meeting as well.
  if (isset($content->field__video_data) && isset($content->field__video_data ['value']) && $content->field__video_data ['value'] != "") {
    $data = json_decode($content->field__video_data ['value']);
    $video_provider = $data->video_provider;
    if ($video_provider == "zoom") {
      $id = $data->id;
      $video_account_user_id = $data->video_account_owner_user_id;
      if (module_enabled("zoomapi")) {
        zoomapi_cancel_zoom_meeting($id, $video_account_user_id);
      }
    }
  }



  // First, send a notification to the student.
  $student_user_id = db_get_user_id_from_cwid($student_cwid, 'student');
  $student_account = fp_load_user($student_user_id);
  $student_tz = fp_get_user_timezone($student_account);

  $apt_date = format_date(convert_time($utc_appointment_datetime, 'UTC', $student_tz), 'short');
  $msg = "APPOINTMENT CANCELED: $user_name has canceled appointment: $apt_date";
  if ($reason_msg) {
    $msg .= "\n\n<br><br>--- Reason given: " . $reason_msg;
  }

  notify_send_notification_to_user($student_user_id, $msg, $cid, 'appointment');



  // Now, send message to faculty member
  $faculty_user_id = db_get_user_id_from_cwid($faculty_cwid, 'faculty');
  $faculty_account = fp_load_user($faculty_user_id);
  $faculty_tz = fp_get_user_timezone($faculty_account);

  $apt_date = format_date(convert_time($utc_appointment_datetime, 'UTC', $faculty_tz), 'short');
  $msg = "APPOINTMENT CANCELED: $user_name has canceled appointment: $apt_date";
  if ($reason_msg) {
    $msg .= "\n\n<br><br>--- Reason given: " . $reason_msg;
  }


  notify_send_notification_to_user($faculty_user_id, $msg, $cid, 'appointment');


  // Okay, now we set the content to be "unpublished" and save.
  $content->published = 0;
  content_save($content);

  watchdog("calendar", "appointment_cancel cid:$cid", array());


}