initial code for exporting study groups as CSV
This commit is contained in:
@@ -56,6 +56,7 @@ module_load_include('inc', 'booking', 'booking.variety');
|
||||
module_load_include('inc', 'booking', 'booking.studygroups');
|
||||
// Load the include for study group definitions
|
||||
module_load_include('inc', 'booking', 'booking.studygroups_admin');
|
||||
module_load_include('inc', 'booking', 'booking.studygroups_report');
|
||||
module_load_include('inc', 'booking', 'booking.studygroup_leaders');
|
||||
// Load the include for the travel form
|
||||
module_load_include('inc', 'booking', 'booking.travel');
|
||||
@@ -480,6 +481,15 @@ function booking_menu() {
|
||||
//'type' => MENU_NORMAL_ITEM,
|
||||
);
|
||||
|
||||
$items['admin/booking/studygroups/%/view/csv'] = array(
|
||||
'title' => 'Export Study Group',
|
||||
'description' => 'Export Study Group memberships as CSV',
|
||||
'page callback' => 'drupal_get_form',
|
||||
'page arguments' => array('booking_studygroups_csv_report', 3),
|
||||
'access arguments' => array('view study groups'),
|
||||
//'type' => MENU_NORMAL_ITEM,
|
||||
);
|
||||
|
||||
$items['admin/booking/studygroups/%/selectleaders'] = array(
|
||||
'title' => 'Select Study Group Leaders',
|
||||
'description' => 'Define attendees to lead or help study groups',
|
||||
|
97
booking.studygroups_report.inc
Normal file
97
booking.studygroups_report.inc
Normal file
@@ -0,0 +1,97 @@
|
||||
<?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;
|
||||
|
||||
//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, '/');
|
||||
|
||||
$header_array = array();
|
||||
$data_array = array();
|
||||
|
||||
//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;
|
||||
}
|
||||
*/
|
||||
|
||||
// @todo
|
||||
// get a listing of all the registrations so we can lookup people's names based on their node id
|
||||
|
||||
//get the list of study group sessions
|
||||
$studygroups_query = db_query("SELECT * FROM {booking_studygroup_list} WHERE booking_eventid = :eid and sid = :sid",
|
||||
array(':eid' => $event->eid, ':sid' => $group_id));
|
||||
$studygroups = $studygroups_query->fetchAllAssoc('sid');
|
||||
|
||||
//get the list of study group session memberships
|
||||
$session_members_query = db_query("SELECT * FROM {booking_studygroup_mapping} WHERE booking_studygroup_id = :sid",
|
||||
array(':sid' => $group_id));
|
||||
$session_members = $session_members_query->fetchAll();
|
||||
|
||||
//set up the column headings
|
||||
foreach ($studygroups as $studygroup) {
|
||||
$header_array[] = $studygroup->sid;
|
||||
}
|
||||
$header = implode( $delimiter, $header_array );
|
||||
$csv .= $header . "\n";
|
||||
|
||||
//calculate the row data
|
||||
foreach ($session_members as $member) {
|
||||
if (! isset($data_array[$member->booking_session_id])) {
|
||||
$data_array[$member->booking_session_id] = new stdClass();
|
||||
}
|
||||
// @todo
|
||||
// lookup the actual name and put this instead of just the node id
|
||||
|
||||
$data_array[$member->booking_session_id][] = $member->booking_node_id;
|
||||
}
|
||||
|
||||
$out = fopen('php://output', 'w');
|
||||
fputcsv2 ($out, $data_array);
|
||||
fclose($out);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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");
|
||||
}
|
Reference in New Issue
Block a user