You are here

function spaces_casetracker_case_stats in Spaces 5.2

Same name and namespace in other branches
  1. 5 spaces_casetracker/spaces_casetracker.module \spaces_casetracker_case_stats()

Casetracker: produce case stats for a user.

1 call to spaces_casetracker_case_stats()
spaces_casetracker_block in spaces_casetracker/spaces_casetracker.module
Implementation of hook_block()

File

spaces_casetracker/spaces_casetracker.module, line 314

Code

function spaces_casetracker_case_stats($uid, $gid = NULL) {
  $status_closed = 7;
  $status_open = 6;
  $week = time() - 7 * 24 * 3600;
  $args = array();

  // ASSIGNED
  if ($gid) {
    $args[] = $gid;
    $q = "SELECT COUNT(c.nid) AS count FROM {casetracker_case} c INNER JOIN {og_ancestry} og ON c.nid = og.nid WHERE og.group_nid = %d AND c.case_status_id = %d AND c.assign_to = %d";
  }
  else {
    $q = "SELECT COUNT(nid) AS count FROM {casetracker_case} WHERE case_status_id = %d AND assign_to = %d";
  }
  $stats['assigned']['total_open'] = db_result(db_query($q, array_merge($args, array(
    $status_open,
    $uid,
  ))));
  $stats['assigned']['total_closed'] = db_result(db_query($q, array_merge($args, array(
    $status_closed,
    $uid,
  ))));
  if ($gid) {
    $q = "SELECT COUNT(c.nid) AS count FROM {casetracker_case} c INNER JOIN {node} n ON c.nid = n.nid INNER JOIN {og_ancestry} og ON c.nid = og.nid WHERE og.group_nid = %d AND c.case_status_id = %d AND c.assign_to = %d AND n.created > %d";
  }
  else {
    $q = "SELECT COUNT(c.nid) AS count FROM {casetracker_case} c INNER JOIN {node} n ON c.nid = n.nid WHERE c.case_status_id = %d AND c.assign_to = %d AND n.created > %d";
  }
  $stats['assigned']['week_open'] = db_result(db_query($q, array_merge($args, array(
    $status_open,
    $uid,
    $week,
  ))));
  $stats['assigned']['week_closed'] = db_result(db_query($q, array_merge($args, array(
    $status_closed,
    $uid,
    $week,
  ))));

  // CREATED
  if ($gid) {
    $q = "SELECT COUNT(c.nid) AS count FROM {casetracker_case} c INNER JOIN {node} n ON c.nid = n.nid INNER JOIN {og_ancestry} og ON c.nid = og.nid WHERE og.group_nid = %d AND c.case_status_id = %d AND n.uid = %d";
  }
  else {
    $q = "SELECT COUNT(c.nid) AS count FROM {casetracker_case} c INNER JOIN {node} n ON c.nid = n.nid WHERE c.case_status_id = %d AND n.uid = %d";
  }
  $stats['created']['total_open'] = db_result(db_query($q, array_merge($args, array(
    $status_open,
    $uid,
  ))));
  $stats['created']['total_closed'] = db_result(db_query($q, array_merge($args, array(
    $status_closed,
    $uid,
  ))));
  $q .= " AND n.created > %d";
  $stats['created']['week_open'] = db_result(db_query($q, array_merge($args, array(
    $status_open,
    $uid,
    $week,
  ))));
  $stats['created']['week_closed'] = db_result(db_query($q, array_merge($args, array(
    $status_closed,
    $uid,
    $week,
  ))));
  if ($gid) {
    $q = "SELECT COUNT(c.case_type_id) AS count, cs.case_state_name AS name FROM {casetracker_case} c INNER JOIN {node} n ON c.nid = n.nid INNER JOIN {casetracker_case_states} cs ON c.case_type_id = cs.csid INNER JOIN {og_ancestry} og ON c.nid = og.nid WHERE og.group_nid = %d AND n.uid = %d GROUP BY c.case_type_id";
  }
  else {
    $q = "SELECT COUNT(c.case_type_id) AS count, cs.case_state_name AS name FROM {casetracker_case} c INNER JOIN {node} n ON c.nid = n.nid INNER JOIN {casetracker_case_states} cs ON c.case_type_id = cs.csid WHERE n.uid = %d GROUP BY c.case_type_id";
  }
  $result = db_query($q, array_merge($args, array(
    $uid,
  )));
  while ($type = db_fetch_array($result)) {
    $stats['created']['breakdown'][$type['name']] = $type['count'];
  }
  return $stats;
}