113 lines
3.6 KiB
PHP
113 lines
3.6 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 "";
|
|
}
|
|
|
|
//set options for the CSV file
|
|
$name = 'bookings-studygroup-' . $group_id . '-' . 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, '/');
|
|
|
|
//is this study group a readings group?
|
|
// @todo do we even care?
|
|
/*
|
|
if ($group_id == variable_get('booking_readinggroup_id','7')) {
|
|
$is_reading_group = TRUE;
|
|
}
|
|
else {
|
|
$is_reading_group = FALSE;
|
|
}
|
|
*/
|
|
|
|
//get the list of study group session memberships
|
|
$session_members_query = db_query("SELECT * FROM {booking_studygroup_mapping} WHERE booking_studygroup_id = :sid ORDER BY 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();
|
|
}
|
|
// @todo
|
|
// lookup the actual name and put this instead of just the node id
|
|
|
|
$data[$member->booking_session_id][] = $member->booking_node_id;
|
|
}
|
|
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();
|
|
foreach ($header_array as $column) {
|
|
$maximums[] = count($data[$column]);
|
|
}
|
|
|
|
//add the column headings to the CSV
|
|
$header = implode( $delimiter, $header_array );
|
|
$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 taken from http://php.net/manual/en/function.fputcsv.php#87120
|
|
*/
|
|
function fputcsv2 ($fh, array $fields, $delimiter = ',', $enclosure = '"', $mysql_null = false) {
|
|
$delimiter_esc = preg_quote($delimiter, '/');
|
|
$enclosure_esc = preg_quote($enclosure, '/');
|
|
|
|
$output = array();
|
|
foreach ($fields as $field) {
|
|
if ($field === null && $mysql_null) {
|
|
$output[] = 'NULL';
|
|
continue;
|
|
}
|
|
|
|
$output[] = preg_match("/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field) ? (
|
|
$enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure
|
|
) : $field;
|
|
}
|
|
|
|
fwrite($fh, join($delimiter, $output) . "\n");
|
|
}
|