Files
booking/booking.studygroups_report.inc
2018-01-14 12:35:28 +11:00

172 lines
6.3 KiB
PHP

<?php
/**
* Function to generate a CSV file listing the session membership for the specified study group
*/
function booking_studygroups_csv_report($group_id) {
global $event;
$data = array();
//verify that $group_id is a number
if (! preg_match('/^[0-9]+$/', $group_id)) {
drupal_set_message("Error: Invalid study group ID '" . $group_id . "' supplied. Unable to view group membership.", 'error', FALSE);
drupal_goto('admin/booking/studygroups');
return "";
}
//retrieve the name of the study group for the specified ID
$group = db_query("SELECT * FROM {booking_studygroup_list} WHERE booking_eventid = :eid and sid = :sid",
array(':eid' => $event->eid, ':sid' => $group_id))
->fetchObject();
if (! $group)
{
drupal_set_message("Error: Could not find matching study group ID. Unable to view group membership.", 'error', FALSE);
drupal_goto('admin/booking/studygroups');
return "";
}
//set options for the CSV file
$name = 'bookings-studygroup-' . $group->booking_studygroup_descrip . '-' . format_date(time(), 'custom', 'Y-m-d-His');
$filename = file_directory_temp() . '/' . $name;
$csv = '';
$delimiter = ',';
$enclosure = '"';
$encloseAll = true;
$nullToMysqlNull = true;
$delimiter_esc = preg_quote($delimiter, '/');
$enclosure_esc = preg_quote($enclosure, '/');
//get the list of study group session memberships
$session_members_query = db_query("SELECT m.*, p.* FROM {booking_studygroup_mapping} m
inner join {booking_person} p on p.nid = m.booking_node_id
WHERE m.booking_studygroup_id = :sid ORDER BY m.booking_session_id, m.booking_studygroup_role DESC, p.booking_lastname",
array(':sid' => $group_id));
$session_members = $session_members_query->fetchAll();
//generate the row data
foreach ($session_members as $member) {
if (! isset($data[$member->booking_session_id])) {
$data[$member->booking_session_id] = array();
}
// lookup the name and role for this entry in the study group session and optionally include age
if (variable_get('booking_studygroup_csv_ages', 0) == 1) {
$text = array($member->booking_firstname, $member->booking_lastname, '[' . _booking_get_age_years($member->booking_dob) .']');
}
else {
$text = array($member->booking_firstname, $member->booking_lastname);
}
//add their role if they're leading/helping etc
if ($member->booking_studygroup_role > 0) {
array_push($text, '(' . _booking_studygroup_role_lookup($member->booking_studygroup_role) . ')');
}
//also tag committee members
if ($member->booking_committee_member == 'Y') {
array_push($text, '(committee)');
}
//add the spaces and put this element in the right array
$data[$member->booking_session_id][] = implode(' ', $text);
}
//watchdog('booking_debug', "<pre>Study Group CSV Report\n@info</pre>", array('@info' => print_r( $data_array, true)));
//calculate the CSV layout
$header_array = array_keys($data);
$maximums = array();
$column_headings = array();
foreach ($header_array as $column) {
$maximums[] = count($data[$column]);
//make the column headings a bit more user friendly
if ($group->booking_is_readinggroup == 'Y') {
$column_headings[] = _booking_readinggroup_colour_lookup($column);
}
else {
$column_headings[] = "Session " . $column;
}
}
//add the column headings to the CSV
$header = implode( $delimiter, $column_headings );
$csv .= $header . "\n";
//generate each row for the CSV
for ($i = 0; $i < max($maximums); $i++) {
$output = array();
foreach ($header_array as $column) {
$field = isset($data[$column][$i]) ? $data[$column][$i] : '';
//enclose $field if necessary
if ( $encloseAll || preg_match( "/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field ) ) {
$output[] = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure;
}
else {
$output[] = $field;
}
} //loop through columns
$row = implode($delimiter, $output) . "\n";
$csv .= $row;
}
//output the CSV to the browser
drupal_add_http_header("Content-type", "application/octet-stream; charset=utf-8");
drupal_add_http_header("Content-Disposition", "attachment; filename=" . $name . ".csv");
print $csv;
exit(0);
}
/**
* Function to generate table listing all males and groups they're leading/helping
*/
function booking_studygroups_leadhelp_view_summary() {
global $event;
$output = "";
$header = array('First Name', 'Last Name');
$rows = array();
$attributes = array('style' => 'max-width:100%');
//get study group definitions
$query = db_select('booking_studygroup_list', 's')
->fields('s')
->condition('s.booking_eventid', $event->eid, '=')
->orderBy('sid');
$studygroup_list = $query->execute()->fetchAllAssoc('sid');
//add columns for the study groups
foreach ($studygroup_list as $group) {
$header[] = $group->booking_studygroup_descrip;
}
$person_query = db_query("SELECT * FROM {booking_person_view} WHERE booking_gender = 'M' " .
" AND (booking_status = 1 OR booking_status = 2)" .
" ORDER BY booking_lastname, booking_firstname")->fetchAllAssoc('nid');
//loop through each matching person
foreach ($person_query as $person) {
//add the name to an array for this line
$newline = array($person->booking_firstname, $person->booking_lastname);
foreach ($studygroup_list as $group) {
$session = 'session' . $group->sid;
$session_role = 'session' . $group->sid . '_role';
//add details if they have a role for this studygroup
if ($person->$session_role > 0) {
$text = _booking_studygroup_role_lookup($person->$session_role);
$newline[] .= "<b>" . $text . "</b>, session #" . $person->$session;
}
//otherwise put a blank entry for this studygroup
else {
$newline[] = "";
}
} //end iterate studygroup list
//add the line to the array of rows
$rows[] = $newline;
} //end iterate person list
//output everything
$output .= t("<h3>!event Study Group Leaders and Helpers</h3>", array('!event' => $event->booking_eventname));
$output .= t("<p>This page lists all males that are either booked in or on the waiting list.</p>");
$output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => $attributes));
return $output;
}