102 lines
3.7 KiB
PHP
102 lines
3.7 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.booking_firstname, p.booking_lastname 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",
|
|
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
|
|
$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) . ')');
|
|
}
|
|
//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
|
|
$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);
|
|
} |