function calendar_schedule_appointment_confirm_form_submit

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

We passed validation, it's time to actually submit now!

File

modules/calendar/calendar.module, line 2080

Code

function calendar_schedule_appointment_confirm_form_submit($form, &$form_state) {

  $schedule_date = $form_state ['values']['schedule_date'];
  $schedule_begin_hm = $form_state ['values']['schedule_begin_hm'];

  $schedule_time_ts = strtotime($schedule_date . " " . $schedule_begin_hm);

  $utc_schedule_time_ts = $form_state ['values']['utc_schedule_time_ts'];

  $formatted_date_for_student = $form_state ['values']['formatted_date_for_student'];


  $fstudent_tz = friendly_timezone($form_state ['values']['student_tz']);
  $faculty_tz = $form_state ['values']['faculty_tz'];
  $ffaculty_tz = friendly_timezone($faculty_tz);


  $faculty_user_id = intval($form_state ['values']['faculty_user_id']);
  $faculty_user = fp_load_user($faculty_user_id);
  $faculty_cwid = $faculty_user->cwid;

  $faculty_name = fp_get_faculty_name($faculty_cwid);

  $event_type_cid = intval($form_state ['values']['event_type_cid']);
  $event_type_content = content_load($event_type_cid);
  $event_additional_msg = trim(@$event_type_content->field__additional_email_msg ['display_value']);


  $event_duration_minutes = intval($event_type_content->field__event_duration_minutes ['value']);

  $student_name = trim(filter_markup($form_state ['values']['name'], 'plain'));
  $student_cwid = trim(filter_markup($form_state ['values']['cwid'], 'plain'));
  $student_email = trim(filter_markup($form_state ['values']['email'], 'plain'));
  $student_phone = trim(filter_markup($form_state ['values']['phone'], 'plain'));
  $student_comments = trim(filter_markup($form_state ['values']['comments'], 'plain'));

  $video_info = $video_data = $join_url = $dial_in = $meeting_id = "";
  $location = t("In-Person");
  $event_title = "Meeting between $student_name and $faculty_name";


  if (isset($form_state ['values']['video_data'])) {
    $video_data = $form_state ['values']['video_data'];
    $decoded = json_decode($video_data);
    if ($form_state ['values']['video_type'] == 'zoom') {

      $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);
      }

      $event_title = t("Zoom meeting between @stu and @fac", array("@stu" => $student_name, "@fac" => $faculty_name));


      $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 = $form_state ['values']['video_account_owner_user_id'];
      $video_data = json_encode($decoded); // re-encode as a simpl string, so we can add to the database.                     
    }
  }


  // NOTE: the "\n" at the end is important, since when we txt these the tags get stripped.
  $msg = "";

  if ($event_additional_msg) {
    $msg .= "<p>" . $event_additional_msg . "</p>\n-----\n";
  }

  $msg .= "<p><b>" . t("Scheduled:") . "</b> $event_type_content->title " . t("between") . " $student_name " . t("and") . " $faculty_name</p>\n";
  $msg .= "<p><b>" . t("Date and Time:") . "</b> " . format_date(convert_time($utc_schedule_time_ts), 'pretty') . " ($fstudent_tz)</p>\n";
  $msg .= "<p><b>" . t("Duration:") . "</b> " . $event_duration_minutes . " min</p>\n";
  $msg .= $video_info;

  if ($student_comments) {
    $msg .= "<p><b>" . t("Student comments:") . "</b> $student_comments</p>\n";
  }

  // We will be creating a new appointment content node programmatically, then calling content_save() on it.

  // We will then redirect to a confirmation page.
  // This is a NEW content obj!
  $content = new stdClass();
  $content->type = 'appointment';
  $content->cid = "new";
  $content->published = 1;

  $content->title = "appointment content saved on " . format_date(convert_time(time()));

  $content->field__appointment_datetime ['value'] = date('Y-m-d H:i:s', $utc_schedule_time_ts);
  $content->field__appointment_duration_minutes ['value'] = $event_duration_minutes;
  $content->field__faculty_id ['value'] = $faculty_cwid;
  $content->field__student_id ['value'] = $student_cwid;
  $content->field__appointment_type ['value'] = 'advising';
  $content->field__appointment_msg ['value'] = $msg;
  $content->field__video_data ['value'] = $video_data;

  content_save($content);

  $acid = $content->cid; // save for later.



  // save as an "activity record" for this student.
  $acontent = new stdClass();
  $acontent->type = 'activity_record';
  $acontent->cid = "new";
  $acontent->published = 1;
  $acontent->delete_flag = 0;
  $acontent->title = t('Student has scheduled an appointment with @fn.', array("@fn" => $faculty_name));
  $acontent->field__student_id ['value'] = $student_cwid;
  $content->field__faculty_id ['value'] = $faculty_cwid;
  $acontent->field__activity_type ['value'] = 'calendar';
  content_save($acontent);



  // 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,
  );




  // Send notifications

  // First, send a notification to the student.
  $student_user_id = db_get_user_id_from_cwid($student_cwid, 'student');
  $nmsg = "You have scheduled an appointment with $faculty_name:\n";

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

  // Now, send a notification to the faculty member.
  $nmsg = "$student_name has scheduled an appointment with you:\n";
  $msg = "";

  if ($event_additional_msg) {
    $msg .= "<p>" . $event_additional_msg . "</p>\n-----\n";
  }

  $msg .= "<p><b>" . t("Scheduled:") . "</b> $event_type_content->title between $student_name and $faculty_name</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($faculty_user_id, $nmsg . $msg, $acid, 'appointment', '', $attachment);


  watchdog("calendar", "schedule_appointment student_id:$student_cwid, faculty_id:$faculty_cwid, appointment details: " . ppm($content, TRUE), array());


  fp_goto("schedule-appointment-completed/$content->cid");


}