function calendar_find_and_remind_notify_upcoming_appointments

7.x calendar.module calendar_find_and_remind_notify_upcoming_appointments()
6.x calendar.module calendar_find_and_remind_notify_upcoming_appointments()

This function will find appointments approaching within X number of minutes, and send out notifications to all involved.

We will keep track of ones we've already sent by looking at the notification_history table.

1 call to calendar_find_and_remind_notify_upcoming_appointments()
calendar_cron in modules/calendar/calendar.module
Implements hook_cron

File

modules/calendar/calendar.module, line 1034

Code

function calendar_find_and_remind_notify_upcoming_appointments() {

  $start_date = date('Y-m-d H:i:s', time());
  $end_date = date("Y-m-d H:i:s", strtotime("NOW + 30 MINUTES"));

  $res = db_query("SELECT DISTINCT(a.cid) FROM content__appointment a, content n
                   WHERE (field__appointment_datetime BETWEEN ? AND ?)
                   AND a.vid = n.vid
                   AND a.cid = n.cid
                   AND n.delete_flag = 0
                   AND published = 1                                                                     
                   ORDER BY field__appointment_datetime ASC, a.vid DESC", array($start_date, $end_date));
  while ($cur = db_fetch_object($res)) {
    $cid = $cur->cid;

    $hid = db_result(db_query("SELECT hid FROM notification_history WHERE cid = ? AND notification_type='remind' LIMIT 1", array($cid)));
    if ($hid) {
      // We have already reminded the user(s) about this appointment.  We can skip.
      continue;
    }

    // If we are here, then we need to remind the users about the appointment!
    $content = content_load($cid);
    $utc_appointment_datetime = strtotime($content->field__appointment_datetime ['value']);

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

    $student_name = fp_get_student_name($student_cwid);
    $faculty_name = fp_get_faculty_name($faculty_cwid);

    // 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 REMINDER: You have an appointment soon with $faculty_name: $apt_date";

    $video_info = "";
    // If there is meeting data (eg, Zoom, include URLs and phone numbers here)
    if (strlen($content->field__video_data ['value']) > 0) {
      $decoded = json_decode($content->field__video_data ['value']);
      if ($decoded) {
        $video_info_test = @$decoded->video_info;
        if ($video_info_test) {
          $video_info = $video_info_test;
        }
      }

      $msg .= "\n" . $video_info;

    }

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



    // 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 REMINDER: You have an appointment soon with $student_name: $apt_date";

    // If there is meeting data (eg, Zoom, include URLs and phone numbers here)
    if ($video_info) {
      $msg .= "\n" . $video_info;
    }

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

    // No need to insert into the notification_history table, since these notification functions would have already done it.

  } // while  




}