function engagements_display_main

6.x engagements.module engagements_display_main()

displays the main Engagements tab, which shows the history of past engagements.

File

modules/engagements/engagements.module, line 3356
This is the primary module file for the engagements module.

Code

function engagements_display_main() 
 {
  global $user, $current_student_id;

  $faculty_id = 0;
  if ($user->is_faculty) {
    $faculty_id = $user->cwid;
  }

  fp_set_title('');

  fp_add_css(fp_get_module_path("engagements") . "/css/style.css");
  fp_add_js(fp_get_module_path("engagements") . "/js/engagements.js");
  fp_add_js(fp_get_module_path("advise") . "/js/advise.js");

  $types = content_get_types();

  $icons = array(
    'phone' => 'fa-phone',
    'email' => 'fa-envelope',
    'txt_msg' => 'fa-comment',
    'note' => 'fa-file-text',
    'in_person' => 'fa-user',
    'social_media' => 'fa-cloud',
    'other' => 'fa-asterisk',
    'video_chat' => 'fa-video-camera',
    'reminder' => 'fa-thumb-tack',
  );


  $rtn = "";
  $rtn .= "<div id='engagements-list-page'>";

  $rtn .= "<div class='add-engagements-buttons'>";
  if (user_has_permission('can_send_email_engagements')) {
    $rtn .= "<a href='javascript:engagementsNewEngagementDialog(\"email\",\"New Email\",\"$current_student_id\",\"$faculty_id\");' class='button'><i class='fa fa-envelope-o'></i>&nbsp; New Email</a>";
  }
  if (user_has_permission('can_send_txt_engagements')) {
    $rtn .= "<a href='javascript:engagementsNewEngagementDialog(\"txt_msg\",\"New Text Message\",\"$current_student_id\",\"$faculty_id\");' class='button'><i class='fa fa-comment-o'></i>&nbsp; New TXT</a>";
  }
  if (user_has_permission('can_log_engagements')) {
    $rtn .= "<a href='javascript:engagementsNewEngagementDialog(\"\",\"Log New Engagement\",\"$current_student_id\",\"$faculty_id\");' class='button'><i class='fa fa-plus'></i>&nbsp; Log New Engagement</a>";
  }

  $rtn .= "</div> <!-- add-engagements-buttons -->
           <div class='clear'></div>
           ";


  $rtn .= fp_render_section_title("Recent Engagements");

  $rtn .= "<div class='engagements-list'>";



  $res = pager_query("SELECT DISTINCT(a.cid) FROM content__engagement a, content n
                   WHERE field__student_id = ?
                   AND a.vid = n.vid
                   AND a.cid = n.cid
                   AND n.delete_flag = 0
                   AND n.published = 1                                    
                   ORDER BY a.vid DESC, field__activity_datetime DESC", array($current_student_id), 5, 0, "SELECT COUNT(DISTINCT(a.cid)) FROM content__engagement a, content n
                                                                                                         WHERE field__student_id = ?
                                                                                                         AND a.vid = n.vid
                                                                                                         AND a.cid = n.cid
                                                                                                         AND n.delete_flag = 0
                                                                                                         AND n.published = 1");

  while ($cur = db_fetch_object($res)) {
    $cid = $cur->cid;
    $content = content_load($cid);

    // is this "faculty" visibility?  If so, do we have access to view?
    if ($content->field__visibility ['value'] == 'faculty' && !user_has_permission('can_view_faculty_engagements')) {
      continue;
    }

    $visibility_icon = "";

    if ($content->field__visibility ['value'] == 'faculty') {
      $visibility_icon = "<i class='fa fa-lock' title='Visibile to Faculty/Staff only'></i>";
    }


    $icon = $icons [$content->field__engagement_type ['value']];

    $type = $content->field__engagement_type ['value'];
    $direction = $content->field__direction ['value'];
    $msg = $content->field__engagement_msg ['display_value'];
    $manual_entry = $content->field__manual_entry ['value'];

    $external_msg_id = @trim($content->field__external_msg_id ['value']);


    $pre_description = "";

    $sms_extra_details = $sms_delivery_details = "";

    if ($type == 'txt_msg' && $external_msg_id) {

      $phones = engagements_get_from_phones();
      $details = engagements_get_sms_from_history($external_msg_id);
      if ($details) {
        $desc = @$phones ['lines'][$details ['to_number']]['description'];
        if (!$desc) {
          $desc = @$phones ['lines'][$details ['from_number']]['description'];
        }

        $sms_extra_details = "<span class='sms-extra-details'>";
        $sms_extra_details .= t("(%desc)", array('%desc' => $desc));
        $sms_extra_details .= "</span>";

        if (isset($details ['delivery_status']) && $details ['delivery_status'] == 'undelivered') {

          $moreinfo = "";

          $moreinfo .= "<p>" . t("We're sorry, but this message was not delivered.
                                  Usually this happens when the recipient's carrier (ex: AT&T, Verizon, etc) marks the message as 'spam' and refuses to deliver
                                  it to the user. <strong>Unfortunately, this is something which is outside the control of @fp.</strong> 
                                  <br><br>
                                  This may also be due to the user's phone being invalid, disconnected, or incapable of receiving SMS text messages.
                                  <br><br>
                                  Your IT/@fp administrator may be able to log into your
                                  SMS vendor account for more information.
                                  <br><br>
                                  Note: the recipient <strong><u>did not receive this message</u></strong>.  Please try again via email or another
                                  communication route.", array("@fp" => variable_get("system_name", "FlightPath"))) . "</p>";

          $moreinfo = base64_encode($moreinfo);
          $sms_delivery_details .= "<div class='sms-undelivered-error'><i class='fa fa-warning'></i> " . t("Undelivered by phone carrier network - %msg", array("%msg" => @$details ['err_friendly_message'])) . "
                                    &nbsp; <a href='javascript:fp_alert(\"$moreinfo\",\"base64\");' title='" . t("More information") . "'><i class='fa fa-question-circle'></i></a></div>";
        }

      }
    }



    if ($manual_entry == 'Y') {
      // This was a manually logged engagement.
      $edit_link = "";
      // Is the user allowed to EDIT this manual engagement?
      if (content_user_access("edit", $content->cid)) {
        $edit_link = "<a href='javascript:fpOpenLargeIframeDialog(\"" . fp_url("content/$cid/edit", "window_mode=popup&content_tabs=false") . "\",\"Edit Alert\");' title='Edit' class='action-link'><i class='fa fa-pencil'></i></a>";
      }

      $pre_description .= t("Logged:") . " $edit_link &nbsp; &nbsp;";
    }



    ///////////
    // This section we can instead query the content_files table for this cid.
    if ($type == 'txt_msg') {
      $ttemp = array('', '');
      if (strstr($msg, "~~MEDIA~~") && strstr($msg, "~~END_MEDIA~~")) {
        $temp = explode("~~MEDIA~~", $msg);
        $msg = $temp [0]; // get rid of the media part of the msg.
        $ttemp = explode("~~END_MEDIA~~", $temp [1]);
      }

      $res2 = db_query("SELECT * FROM content_files WHERE cid = ? ORDER BY cid", array($cid));
      while ($cur2 = db_fetch_array($res2)) {
        $fid = $cur2 ['fid'];
        $file = content_get_uploaded_file($fid);
        $img_tag = "<img src='{$file ['url']}' style='max-width: 200px; height: auto;'>";
        $msg .= "<div class='media-attached'><a href='{$file ['url']}' target='_BLANK'>
                      $img_tag
                    </a></div>";
      }
      $msg .= $ttemp [1]; // in case there was anything AFTER ~~END_MEDIA~~
    }



    /*        
    // If the $msg contains ~~MEDIA~~ and ~~END_MEDIA~~ then we can assume there are filenames after it, which we assume to be images that got
    // text messaged.  Add them.
    
      $files = explode(",", $temp[1]);
      foreach ($files as $filename) {
        $ttemp = explode("~~END_MEDIA~~", $filename);
        $filename = $ttemp[0];
        $path = base_path() . '/custom/files/content_uploads/' . $filename;

        $img_tag = "<img src='$path' style='max-width: 200px; height: auto;'>";

        $ext_temp = explode(".", $filename);
        $ext = strtolower(@$ext_temp[count($ext_temp) - 1]);

        $image_exts = array("gif", "tiff", "jpg", "jpeg", "png", "bmp");
        if (!in_array($ext, $image_exts)) {
          $mime_icon = content_get_fontawesome_icon_for_mimetype("", $ext);
          $img_tag = "<div class='atached-file'><i class='fa $mime_icon'></i>" . " " . t("View %ext attachment in new window", array("%ext" => $ext)) . "</div>";
        }

        $msg .= "<div class='media-attached'><a href='$path' target='_BLANK'>
                    $img_tag
                  </a></div>" . $ttemp[1]; // add on the </p> at the end if its there.
      }
    } // contains ~~MEDIA~~
    */


    ////////////



    $header_desc_extra = "";
    if ($type == 'email' && $direction == 'sent' && $manual_entry != 'Y') {
      //$header_desc_extra = "&nbsp; &nbsp; &nbsp; &nbsp; Opens: 1";
      $num_opens = intval(db_result(db_query("SELECT opens FROM engagements_tracking WHERE cid = ?", array($content->cid))));
      $header_desc_extra = "<span class='header-desc-extra'>Opens: $num_opens</span>";
    }


    $fid_csv = @trim($content->field__attachment ['value']);
    if ($fid_csv) {
      // were there any attachments?
      $attachment_icon = "";
      if ($fid_csv) {
        $attachment_icon = "<i class='fa fa-paperclip'></i> ";
        $attachments = array();
        $attachment_csv = trim(@$content->field__attachment ['value']);
        // TODO:  An option here would be to just query the content_files table for this cid.


        if ($attachment_csv) {
          $temp = explode(",", $attachment_csv);
          $msg .= "<label>" . t("Attached File(s):") . "</label><div class='attached-files'>";
          foreach ($temp as $fid) {
            if ($fid == "") {
              continue;
            }
            $file = content_get_uploaded_file($fid);
            if ($file) {
              $mime_icon = content_get_fontawesome_icon_for_mimetype($file ['mimetype'], $file ['ext']);
              $msg .= "<div class='attached-file'><a href='{$file ['url']}'><i class='fa $mime_icon'></i> {$file ['original_filename']}</a></div>";
            }
          }
          $msg .= "</div>";
        }


      }

      // We need a random id to assign to this email body.
      $bodyid = "bid_" . md5(microtime() . mt_rand(9, 99999) . mt_rand(9, 9999));


      // If the msg body is too long, let it be expandable.
      $more_link = $less_link = "";
      if (strlen($msg) > 500) {
        $more_link = "<a href='javascript:engagementsExpandBody(\"$bodyid\");' class='more-body-link' id='more_$bodyid'>More</a>";
        $less_link = "<a href='javascript:engagementsShrinkBody(\"$bodyid\");' class='less-body-link' id='less_$bodyid' style='display:none;'>Less</a>";
      }

      $msg = "<div class='email-subject'>{$attachment_icon}Subject: $content->title</div><div class='email-body' id='$bodyid'>$msg</div>$more_link$less_link";
    }



    $author_user_id = intval($content->user_id);
    $author_account = fp_load_user($author_user_id);
    $author_name = $author_account->f_name . " " . $author_account->l_name;

    $act_feed_extra_class = "";
    if ($content->field__from_sms_phone ['value']) {
      $act_feed_extra_class .= " activity-feed-from-sms-" . engagements_convert_to_valid_phone_number(fp_get_machine_readable($content->field__from_sms_phone ['value']));
    }

    if ($content->field__to_sms_phone ['value']) {
      $act_feed_extra_class .= " activity-feed-to-sms-" . engagements_convert_to_valid_phone_number(fp_get_machine_readable($content->field__to_sms_phone ['value']));
    }


    $rtn .= "<div class='activity-feed-teaser activity-feed-teaser-$direction activity-feed-teaser-type-$type $act_feed_extra_class'>
                <div class='activity-header-content'>
                  <span class='header-icon'><i class='fa $icon'></i></span>
                  <span class='header-description'>
                      <span class='pre-description'>$pre_description</span>
                      {$content->field__engagement_type ['display_value']}                      
                  </span>$sms_extra_details 
                  <span class='header-phone-outcome'>: {$content->field__direction ['display_value']} $header_desc_extra</span>
                  $sms_delivery_details
                  <span class='header-edit'></span>
                  <span class='header-date-time'>" . $content->field__activity_datetime ['display_value'] . "</span>                  
                  <span class='header-author'>$author_name</span>
                  <span class='header-visibility'>$visibility_icon</span>
                </div>
                <div class='activity-contents'>
                  <div class='activity-comment'>
                    $msg                    
                  </div>
                </div>
             </div>
             <div class='clear'></div>";

    // mark this content as "read" since it appeared on the screen.
    content_set_last_access($content->cid);

  } // while cur



  // Display the pager that was generated by the pager_query above!  
  $rtn .= theme_pager(array(t('« newest'), t('‹ newer'), '', t('older ›'), t('oldest »')));

  $rtn .= "</div>"; // engagements-list         



  $rtn .= "</div>"; // engagements-list-page

  // Let's set our breadcrumbs
  $db = get_global_database_handler();
  $crumbs = array();
  $crumbs [] = array(
    'text' => 'Students',
    'path' => 'student-search',
  );
  $crumbs [] = array(
    'text' => $db->get_student_name($current_student_id) . " ({$current_student_id})",
    'path' => 'student-profile',
    'query' => "current_student_id={$current_student_id}",
  );
  fp_set_breadcrumbs($crumbs);



  watchdog("engagements", "view $current_student_id");


  return $rtn;
}