function calendar_get_appointments_for_faculty

6.x calendar.module calendar_get_appointments_for_faculty($faculty_id, $start_date, $end_date, $bool_include_external_cals = FALSE)

Return back a list of appointment content nodes for this faculty member, which fall between the specified datetimes.

dates expected to look like: Y-m-d

1 call to calendar_get_appointments_for_faculty()
calendar_get_available_faculty_schedule in modules/calendar/calendar.module
Returns back an array of time slots available for this faculty member and event_type

File

modules/calendar/calendar.module, line 1200

Code

function calendar_get_appointments_for_faculty($faculty_id, $start_date, $end_date, $bool_include_external_cals = FALSE) {

  $rtn = array();

  $start_date .= " 00:00:01";
  $end_date .= " 23:59:59";



  $res = db_query("SELECT DISTINCT(a.cid) FROM content__appointment a, content n
                   WHERE field__faculty_id = ?
                   AND (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 a.vid DESC", array($faculty_id, $start_date, $end_date));
  while ($cur = db_fetch_object($res)) {
    $cid = $cur->cid;
    $content = content_load($cid);

    //$ap_datetime_ts = convert_time(strtotime($content->field__appointment_datetime['value']));
    $ap_datetime_ts = strtotime($content->field__appointment_datetime ['value']);

    $year = date('Y', $ap_datetime_ts);
    $month = date('n', $ap_datetime_ts);
    $day = date('j', $ap_datetime_ts);

    $hm = date('g:ia', $ap_datetime_ts);

    $rtn [$year][$month][$day][$ap_datetime_ts] = array(
      'start_ts' => $ap_datetime_ts,
      'hm' => $hm,
      'end_ts' => $ap_datetime_ts + (intval($content->field__appointment_duration_minutes ['value']) * 60),
      'origin' => 'fp',
      'tz' => 'UTC',
    );


  } // while

  $faculty_user_id = db_get_user_id_from_cwid($faculty_id, 'faculty');
  $faculty_tz = fp_get_user_timezone($faculty_user_id);

  if ($bool_include_external_cals) {
    // Meaning, we should also look up the external ics feeds we may have.

    $x = fp_get_module_path('calendar', TRUE, FALSE) . '/lib/ics-parser/vendor/autoload.php';
    require_once ($x);


    // Find external feeds (if any exist) for this user.
    $ics_feeds = array();

    $res = db_query("SELECT * FROM content__schedule_unavailable_time a, content n
                     WHERE field__faculty_id = ?
                     AND field__ics_url != ''
                     AND a.vid = n.vid
                     AND a.cid = n.cid
                     AND n.delete_flag = 0
                     AND published = 1
                     ORDER BY a.vid DESC", array($faculty_id));
    while ($cur = db_fetch_array($res)) {
      $ics_url = trim($cur ['field__ics_url']);
      if ($ics_url) {
        $ics_feeds [] = array(
          'url' => $ics_url,
          'tz' => $faculty_tz,
          'cid' => $cur ['cid'],
        );
      }
    }


    // DEV
    //$ics_feeds[] = array(
    //  'url' => "https://calendar.google.com/calendar/ical/example_url_here/public/basic.ics",
    //  'tz' => 'America/Chicago',
    //);      


    foreach ($ics_feeds as $details) {
      $ics = $details ['url'];
      $tz = @$details ['tz'];
      $cid = $details ['cid'];
      if (!$tz) {
        $tz = $faculty_tz;
      }


      $ical = NULL;
      $ical_str = "";

      try {

        // grab from cache or re-download the ics file?
        // See if more than 5 number of minutes have passed since we last pulled this file,
        // and regenerate it if so. Otherwise, use a database cache.  Maybe serialized into the user_attributes table?        
        $test = user_get_attribute($faculty_user_id, 'cache_ics_feed__' . $cid . '_reload_after_ts', 0);

        if (time() < $test) {
          // It has been cached previously, and the cache is still valid.  Let's see if we can get what's there.           
          $ical_str = user_get_attribute($faculty_user_id, 'cache_ics_feed__' . $cid, "");
          watchdog('calendar_ical', "Loading cached ics $ics ($cid)", array(), WATCHDOG_DEBUG);
        }

        if (!$ical_str || $ical_str == "") {
          watchdog('calendar_ical', "Downloading ics $ics ($cid) from web and caching", array(), WATCHDOG_DEBUG);
          $ical_str = fp_url_get_contents($ics);
          // Save to user_attributes as a cache for later retrieval
          user_set_attribute($faculty_user_id, 'cache_ics_feed__' . $cid, $ical_str);
          user_set_attribute($faculty_user_id, 'cache_ics_feed__' . $cid . '_reload_after_ts', time() + (5 * 60));

        }

        if ($ical_str == "") {
          watchdog('calendar_ical', "Error trying to read ical: $ics (cid: $cid). String is empty", array(), WATCHDOG_ERROR);
          fpm("error trying to read ical");
          return;
        }

        $ical = new ICal\ICal();
        $ical->initString($ical_str);
        if ($ical == NULL) {
          watchdog('calendar_ical', "Error trying to load ical: $ics string (cid: $cid) String: $ical_str", array(), WATCHDOG_ERROR);
          fpm("error trying to load ical_str");
          return;
        }


        $events = $ical->eventsFromRange($start_date, $end_date);

        foreach ($events as $event) {

          // if this is a "free" time, then the "transp" property will == "TRANSPARENT".  If it's "busy" it will be "OPAQUE".
          // We want to ignore anything marked as "free".
          if (@$event->transp === "TRANSPARENT") {
            continue;
          }


          $utc_start = $event->dtstart_array [2]; // the UTC timestamp 
          $utc_end = $event->dtend_array [2];

          // Convert into the OWNER's timezone!
          $converted_start = convert_time($utc_start, 'UTC', $tz);
          $converted_end = convert_time($utc_end, 'UTC', $tz);

          $ap_datetime_ts = $converted_start;

          $summary = $event->summary; // title. basically.          
          $location = $event->location; // zoom link or whatever.  Goes with body.
          $description = $event->description; // the body.


          // In order to catch events which span more than one day, let's add an entry
          // for each day covered between our start and end times.
          $last_date = '';
          for ($t = $converted_start; $t <= $converted_end; $t = $t + 21600) {
            $test_ts = $t;
            if (date('Y-m-d', $test_ts) == $last_date) {
              continue; // same date already, skip it.
            }


            $year = date('Y', $test_ts);
            $month = date('n', $test_ts);
            $day = date('j', $test_ts);

            $last_date = date('Y-m-d', $test_ts);

            $rtn [$year][$month][$day][$ap_datetime_ts] = array(
              'start_ts' => $converted_start,
              'end_ts' => $converted_end,
              'converted_to_tz' => $tz,
              'origin' => $ics,
              'summary' => $summary,
              'location' => $location,
              'description' => filter_markup($description, 'basic'),
            );



          } // for t


        } // foreach events


      }
      catch (Exception $e) {
        watchdog('calendar_ical', "Couldn't load calendar ical $ics.  Exception: <pre>" . print_r($e, TRUE) . "</pre>");
        fp_add_message($e->getMessage());
      }





    }

  } // bool_include_external_cals






  return $rtn;

}