function alerts_get_alert_count_by_type

6.x alerts.module alerts_get_alert_count_by_type($account = NULL)

Implements hook_get_count_for_alert_type

Set and Return back "unread" (not in content_last_access)

File

modules/alerts/alerts.module, line 152
module file for Alerts

Code

function alerts_get_alert_count_by_type($account = NULL) {
  global $user;
  if ($account === NULL) {
    $account = $user;
  }

  if ($account->id == 0) {
    return FALSE;
  }

  $rtn = array();
  $types = array('alert');

  // We need to know this user's list of advisees.
  $advisees = advise_get_advisees($account->cwid);
  $advisee_line = "";
  if ($advisees && count($advisees) > 0) {

    $advisees_list = "'" . join("','", $advisees) . "'";

    $advisee_line = " AND b.field__student_id IN (" . $advisees_list . ") ";
  }
  else {
    $advisee_line = " AND b.field__student_id IN ('') "; // Meaning, no advisees.  We do this so as not to break the queries and return back ALL alerts.
  }

  // If you have no advisees, that's OK, because you might have alerts which are targetting you.  
  $total_count = $read_count = 0;
  foreach ($types as $type) {

    $total_count = intval(db_result(db_query("SELECT COUNT(*) as mycount 
                      FROM content__$type b,                                            
                           content n
                     WHERE n.type = ?
                     AND n.published = 1
                     AND n.delete_flag = 0
                     AND b.vid = n.vid
                     AND b.cid = n.cid
                     AND b.field__exclude_advisor != 1
                     $advisee_line ", array($type))));


    // Also search for alerts which are targeting the account user.
    $total_count += intval(db_result(db_query("SELECT COUNT(*) as mycount 
                      FROM content__$type b,                                            
                           content n
                     WHERE n.type = ?
                     AND n.published = 1
                     AND n.delete_flag = 0
                     AND b.vid = n.vid
                     AND b.cid = n.cid
                     AND b.field__target_faculty_id = ?
                      ", array($type, $account->cwid))));


    ////////////////////////
    // Get Read count for alerts



    $read_count = intval(db_result(db_query("SELECT COUNT(*) as mycount 
                      FROM content_last_access a,
                           content__$type b,                                            
                           content n
                     WHERE n.type = ?
                     AND n.published = 1
                     AND n.delete_flag = 0
                     AND n.cid = a.cid                     
                     AND b.vid = n.vid
                     AND n.cid = b.cid
                     AND b.field__exclude_advisor != 1
                     $advisee_line
                     AND a.user_id = ?", array($type, $account->id))));


    // Also search for read alerts which are targeting the account user.
    $read_count += intval(db_result(db_query("SELECT COUNT(*) as mycount 
                      FROM content_last_access a,
                           content__$type b,                                            
                           content n
                     WHERE n.type = ?
                     AND n.published = 1
                     AND n.delete_flag = 0
                     AND n.cid = a.cid                     
                     AND b.vid = n.vid
                     AND n.cid = b.cid
                     AND b.field__target_faculty_id = ?                     
                     AND a.user_id = ?", array($type, $account->cwid, $account->id))));



    $rtn [$type]['total'] = $total_count;
    $rtn [$type]['read'] = $read_count;
    $rtn [$type]['unread'] = $total_count - $read_count;


    // TODO:  A setting such that "read" is actually "closed" and "unread" means "open".    


  } // foreach types 

  return $rtn;

}