function calendar_appointment_content_form_validate

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

Custom validate handler for when we save an appointment form. We want to confirm the student is valid, and also store ONLY the cwid.

1 string reference to 'calendar_appointment_content_form_validate'
calendar_form_alter in modules/calendar/calendar.module
Implements hook_form_alter

File

modules/calendar/calendar.module, line 3340

Code

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

  $values = $form_state ['values'];

  $student_id = $line = $form_state ['values']['student_id'];
  // Might look like this:  first last (CWID).  We only want the CWID.
  if (strstr($line, "(")) {
    $temp = explode("(", $line);
    $student_id = $temp [1];
    $student_id = trim(str_replace(")", "", $student_id));
  }

  // $student_id should now contain only the CWID of the student.  Let's make sure it's a real person, by making sure their name is entered.
  $db = get_global_database_handler();
  $name = trim($db->get_student_name($student_id, FALSE));
  if (!$name) {
    form_error('student_id', t("Sorry, but the Student (by CWID or Name) you entered is not valid.  Please try again."));
  }

  // If we're good to go, it means we can add the new (stripped down) student_id to the form_state for saving.
  $form_state ['values']['student_id'] = $student_id;



  // Since we have passed validation, we can see if we should try to create a new Zoom meeting.

  $video_info = $location = "";
  $acid = 0;
  $location = "In-Person";

  $faculty_user_id = intval($user->id);
  $faculty_user = $user;
  $faculty_cwid = $faculty_user->cwid;
  $faculty_name = $faculty_user->f_name . " " . $faculty_user->l_name;
  $faculty_tz = fp_get_user_timezone($user);

  $student_name = $db->get_student_name($student_id);
  $schedule_time_ts = strtotime($values ['appointment_datetime']);
  $utc_schedule_time_ts = convert_time($schedule_time_ts, $faculty_tz, 'UTC'); // convert to utc time from our current timezone.
  $event_duration_minutes = intval($values ['appointment_duration_minutes']);

  $event_title = "Meeting between $student_name and $faculty_name";

  // Only do this for NEW appointments.
  if ($values ['cid'] === 'new' && module_enabled('zoomapi') && isset($values ['enable_zoom']) && $values ['enable_zoom'] === TRUE) {

    // We want to create a new Zoom meeting!
    $event_title = t("Zoom meeting between @stu and @fac", array("@stu" => $student_name, "@fac" => $faculty_name));
    $month = date('n', $schedule_time_ts);
    $year = date('Y', $schedule_time_ts);
    $day = date('j', $schedule_time_ts);


    //gmt_date_time must look like yyyy-MM-ddTHH:mm:ssZ.  For example: 2020-03-31T22:02:00Z.  The Z tells it that we are using GMT/UTC time.
    $gmt_date_time = date("Y-m-d\TH:i:s\Z", $utc_schedule_time_ts);

    $response = zoomapi_create_zoom_meeting($faculty_user_id, $gmt_date_time, $event_duration_minutes, "Video meeting - $faculty_name / $student_name");

    if (!$response) {
      form_error("", t("Sorry, but there was an issue creating the Zoom meeting for this appointment.  Your appointment has not been saved.
                        Please try another method, or contact the IT administrator to let them know of this issue."));
      return;
    }

    $decoded = json_decode($response);

    $meeting_id = $decoded->id;
    $join_url = $decoded->join_url;
    $location = $join_url;
    $dial_in = $decoded->settings->global_dial_in_numbers [0]->number;

    if (function_exists("engagements_convert_to_pretty_phone_number")) {
      $dial_in = engagements_convert_to_pretty_phone_number($dial_in, TRUE);
    }

    $video_info .= "<p><b>" . t("Zoom Meeting:") . "</b> <a href='$join_url'>" . $join_url . "</a> \n<br> " . t("Be sure to download the Zoom app on your phone, tablet, or computer.") . " \n<br> " . t("To dial in instead of using video chat, you may dial:<br>\n @dial (ID: @id)", array("@dial" => $dial_in, "@id" => $meeting_id)) . ".</p>\n";

    $decoded->video_info = $video_info; // add to our json object for ease of use later.
    $decoded->video_provider = "zoom"; // declare that this is a "zoom" meeting.
    $decoded->video_account_owner_user_id = $user->id;
    $video_data = json_encode($decoded); // re-encode as a simpl string, so we can add to the database.
    $form_state ['values']['video_data'] = $video_data;

  } // enable_zoom 





  // Send notifications


  // Get ics invitation string for email attachments
  $start_dt = date("Ymd\THis\Z", $utc_schedule_time_ts);
  $end_dt = date("Ymd\THis\Z", $utc_schedule_time_ts + ($event_duration_minutes * 60));
  $ics = calendar_get_ics_invitation_string($event_title, $location, $video_info, $start_dt, $end_dt);

  $attachment = array(
    'invite.ics' => $ics,
  );

  // First, send a notification to the student.
  $student_user_id = db_get_user_id_from_cwid($student_id, 'student');
  $student_tz = fp_get_user_timezone($student_user_id);
  $fstudent_tz = friendly_timezone($student_tz);
  $nmsg = "You have been scheduled for an appointment with $faculty_name:\n";

  $msg = "";
  $msg .= "<p><b>" . t("Scheduled:") . "</b> $event_title</p>\n";
  $msg .= "<p><b>" . t("Date and Time:") . "</b> " . format_date(convert_time($utc_schedule_time_ts, 'UTC', $student_tz), 'pretty') . " ($fstudent_tz)</p>\n";
  $msg .= "<p><b>" . t("Duration:") . "</b> " . $event_duration_minutes . " min</p>\n";
  $msg .= $video_info;


  notify_send_notification_to_user($student_user_id, $nmsg . $msg, $acid, 'appointment', '', $attachment);

  // Now, send a notification to the faculty member.    
  $ffaculty_tz = friendly_timezone($faculty_tz);
  $nmsg = "You have scheduled an appointment with $student_name:\n";
  $msg = "";
  $msg .= "<p><b>" . t("Scheduled:") . "</b> $event_title</p>\n";
  $msg .= "<p><b>" . t("Date and Time:") . "</b> " . format_date(convert_time($utc_schedule_time_ts, 'UTC', $faculty_tz), 'pretty') . " ($ffaculty_tz)</p>\n";
  $msg .= "<p><b>" . t("Duration:") . "</b> " . $event_duration_minutes . " min</p>\n";
  $msg .= $video_info;


  notify_send_notification_to_user($user->id, $nmsg . $msg, $acid, 'appointment', '', $attachment);


}