You are here

function merci_total_usage in MERCI (Manage Equipment Reservations, Checkout and Inventory) 7.2

Same name and namespace in other branches
  1. 6.2 includes/database.inc \merci_total_usage()

Pass type as user/project and uid/nid returns total hours

File

includes/database.inc, line 221
MERCI - Managed Equipment Reservation Checkout and Inventory

Code

function merci_total_usage($id, $type = 'user', $date = NULL) {

  // Determine CCK table and columns the date data is stored in.
  $field = field_info_field('field_merci_date');
  $table = key($field['storage']['details']['sql']['FIELD_LOAD_CURRENT']);
  $column_start_date = $field['storage']['details']['sql']['FIELD_LOAD_CURRENT'][$table]['value'];
  $column_end_date = $field['storage']['details']['sql']['FIELD_LOAD_CURRENT'][$table]['value2'];
  if ($type == 'project') {

    // TODO Please convert this statement to the D7 database API syntax.
    $result = db_query("SELECT {$column_start_date} AS field_merci_date_value, {$column_end_date} AS field_merci_date_value2 FROM {merci_reservation} mr JOIN node n ON n.vid = mr.vid JOIN og_ancestry og ON og.nid = n.nid JOIN {" . $table . "} ct ON n.vid = ct.revision_id WHERE group_nid = :group_nid", array(
      ':group_nid' => $id,
    ));
  }
  else {
    if ($date) {

      // TODO Please convert this statement to the D7 database API syntax.
      $result = db_query("SELECT {$column_start_date} AS field_merci_date_value, {$column_end_date} AS field_merci_date_value2 FROM {merci_reservation} mr JOIN {node} n ON n.vid = mr.vid JOIN {" . $table . "} ct ON n.vid = ct.revision_id WHERE uid = :uid AND {$column_start_date} > :date", array(
        ':uid' => $id,
        ':date' => $date,
      ));
    }
    else {

      // TODO Please convert this statement to the D7 database API syntax.
      $result = db_query("SELECT {$column_start_date} AS field_merci_date_value, {$column_end_date} AS field_merci_date_value2 FROM {merci_reservation} mr JOIN {node} n ON n.vid = mr.vid JOIN {" . $table . "} ct ON n.vid = ct.revision_id WHERE uid = :uid", array(
        ':uid' => $id,
      ));
    }
  }

  //add the reservation total minutes for each item in the reservation
  foreach ($result as $reservationnode) {
    $minutes = $minutes + (strtotime($reservationnode->field_merci_date_value2) - strtotime($reservationnode->field_merci_date_value));
  }
  return $minutes;
}