253 lines
8.8 KiB
PHP
253 lines
8.8 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Admin pages for defining study groups
|
|
*/
|
|
|
|
|
|
/**
|
|
* Function for listing the study groups and the ability to edit the group definitions
|
|
*/
|
|
function booking_studygroups_admin() {
|
|
|
|
global $event;
|
|
$output = "";
|
|
$header = array('Study Group', 'Session Count', 'Reading Group?', 'Edit Definition');
|
|
$rows = array();
|
|
$attributes = array('style' => 'max-width:60%');
|
|
//$attributes = array();
|
|
|
|
//get study groups
|
|
$query = db_select('booking_studygroup_list', 's')
|
|
->fields('s')
|
|
->condition('s.booking_eventid', $event->eid, '=');
|
|
$result = $query->execute();
|
|
|
|
foreach ($result as $group) {
|
|
$rows[] = array(
|
|
$group->booking_studygroup_descrip,
|
|
$group->booking_num_group_sessions,
|
|
$group->booking_is_readinggroup == 'Y' ? 'Yes' : 'No',
|
|
l(t('Edit Group Definition', array('!id' => $group->sid)), t('admin/config/booking/studygroups/!id/edit', array('!id' => $group->sid))),
|
|
);
|
|
}
|
|
|
|
//output everything
|
|
$output .= t("<h3>!event Study Groups</h3>", array('!event' => $event->booking_eventname));
|
|
$output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => $attributes));
|
|
|
|
return $output;
|
|
}
|
|
|
|
/**
|
|
* Function for listing the study groups and links to view each one
|
|
*/
|
|
function booking_studygroups_view_summary() {
|
|
|
|
global $event;
|
|
$output = "";
|
|
$header = array('Study Group', 'Session Count', 'Reading Group?', 'View Membership', 'Compute Membership', 'Select Leaders', 'Download CSV');
|
|
//$attributes = array('style' => 'max-width:50%');
|
|
$attributes = array();
|
|
$rows = array();
|
|
|
|
//get study groups
|
|
$query = db_select('booking_studygroup_list', 's')
|
|
->fields('s')
|
|
->condition('s.booking_eventid', $event->eid, '=');
|
|
$result = $query->execute();
|
|
|
|
foreach ($result as $group) {
|
|
$rows[] = array(
|
|
$group->booking_studygroup_descrip,
|
|
$group->booking_num_group_sessions,
|
|
$group->booking_is_readinggroup == 'Y' ? 'Yes' : 'No',
|
|
l(t('Current Membership', array('!id' => $group->sid)), t('admin/booking/studygroups/!id/view', array('!id' => $group->sid))),
|
|
l(t('Compute Membership Update', array('!id' => $group->sid)), t('admin/booking/studygroups/!id/update', array('!id' => $group->sid))),
|
|
l(t('Edit Leaders/Helpers', array('!id' => $group->sid)), t('admin/booking/studygroups/!id/editleaders', array('!id' => $group->sid))),
|
|
l(t('Download', array('!id' => $group->sid)), t('admin/booking/studygroups/!id/view/csv', array('!id' => $group->sid))),
|
|
);
|
|
}
|
|
|
|
//output everything
|
|
$output .= t("<h3>!event Study Groups</h3>", array('!event' => $event->booking_eventname));
|
|
$output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => $attributes));
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
/**
|
|
* Form to view or add a studygroup definition
|
|
*/
|
|
function booking_studygroups_define_form($node, &$form_state, $create, $editid = 0)
|
|
{
|
|
$form = array ();
|
|
$prefix = "<p>Add a new study group definition</p>";
|
|
|
|
if ($create == true) {
|
|
drupal_set_title('Add Study Group');
|
|
$data = $node;
|
|
watchdog('booking_debug', 'Creating new study group: @info', array ('@info' => var_export($form_state, TRUE)));
|
|
}
|
|
else {
|
|
drupal_set_title('Edit Study Group');
|
|
//verify that $editid is a number
|
|
if (! preg_match('/^[0-9]+$/', $editid)) {
|
|
drupal_set_message("Error: Invalid study group ID supplied. Unable to edit study group definition.", 'error', FALSE);
|
|
drupal_goto('admin/booking/config/studygroups');
|
|
return "";
|
|
}
|
|
|
|
$data = db_query("SELECT * FROM {booking_studygroup_list} WHERE sid = :id",
|
|
array(':id' => $editid))->fetchObject();
|
|
$prefix = t("<p>Update the "!event " study group definition.</p>", array('!event' => $data->booking_studygroup_descrip));
|
|
//add this to the form in a hidden field so we can update the right price
|
|
$form['booking_sid'] = array (
|
|
'#type' => 'hidden',
|
|
'#value' => $editid,
|
|
);
|
|
watchdog('booking_debug', "<pre>Editing study group definition:\n@info</pre>", array ('@info' => print_r($data, TRUE)));
|
|
}
|
|
|
|
if(!isset($form_state['storage']['confirm'])) {
|
|
$form['booking_studygroup_descrip'] = array (
|
|
'#type' => 'textfield',
|
|
'#title' => t('Description of this study group (eg Monday)'),
|
|
'#size' => 60,
|
|
'#maxlength' => 150,
|
|
'#required' => TRUE,
|
|
'#default_value' => !empty($data->booking_studygroup_descrip) ? $data->booking_studygroup_descrip : '',
|
|
);
|
|
|
|
$form['booking_num_group_sessions'] = array (
|
|
'#type' => 'textfield',
|
|
'#title' => t('The number of sessions this study group will have'),
|
|
'#size' => 5,
|
|
'#maxlength' => 10,
|
|
'#required' => TRUE,
|
|
'#default_value' => !empty($data->booking_num_group_sessions) ? $data->booking_num_group_sessions : '',
|
|
);
|
|
|
|
$form['booking_is_readinggroup'] = array (
|
|
'#type' => 'radios',
|
|
'#title' => t('Reading group?'),
|
|
'#description' => t('Select whether this study group definition is for a reading group. Leave as No unless you want team colours associated with this group'),
|
|
'#options' => array (0 => t('No'), t('Yes')),
|
|
'#default_value' => !empty($data->booking_is_readinggroup) ? ($data->booking_is_readinggroup == 'Y' ? 1 : 0) : 0,
|
|
);
|
|
//create the buttons
|
|
if ($create == true) {
|
|
$form['submit'] = array
|
|
(
|
|
'#type' => 'submit',
|
|
'#value' => t('Create Study Group'),
|
|
);
|
|
}
|
|
else {
|
|
$form['Update'] = array
|
|
(
|
|
'#type' => 'submit',
|
|
'#value' => t('Update Study Group'),
|
|
);
|
|
$form['Delete'] = array
|
|
(
|
|
'#type' => 'submit',
|
|
'#value' => t("Delete Study Group Definition"),
|
|
);
|
|
}
|
|
//return the render array
|
|
return array (
|
|
'first_para' => array (
|
|
'#type' => 'markup',
|
|
'#markup' => $prefix,
|
|
),
|
|
'form' => $form,
|
|
);
|
|
}
|
|
//confirm delete
|
|
else {
|
|
return confirm_form($form, "Are you sure you wish to delete studygroup definition with id " . $editid . "?",
|
|
current_path(), NULL, "Delete Study Group Definition");
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Process addition/update/delete of a studygroup definition
|
|
*/
|
|
function booking_studygroups_define_form_submit($form, &$form_state) {
|
|
|
|
global $event;
|
|
$values = $form_state['input'];
|
|
$redirect_path = array('admin/config/booking/studygroups');
|
|
$new_count = 0;
|
|
|
|
//get the number of study groups before making this update
|
|
$count = db_query("SELECT count(*) as num FROM {booking_studygroup_list} WHERE booking_eventid = :eid",
|
|
array(':eid' => $event->eid))->fetchObject();
|
|
|
|
//watchdog('booking', 'Checkboxes when setting buttons: @info', array ('@info' => var_export($checkboxes, TRUE)));
|
|
if ($form_state['values']['op'] == 'Create Study Group') {
|
|
db_insert('booking_studygroup_list')
|
|
->fields(array(
|
|
'booking_eventid' => $event->eid,
|
|
'booking_studygroup_descrip' => $values['booking_studygroup_descrip'],
|
|
'booking_num_group_sessions' => $values['booking_num_group_sessions'],
|
|
'booking_is_readinggroup' => $values['booking_is_readinggroup'] == 1 ? 'Y' : 'N',
|
|
))
|
|
->execute();
|
|
|
|
$new_count = $count->num + 1;
|
|
}
|
|
//if we're deleting, add the confirmation to the form if it hasn't been defined yet
|
|
elseif($form_state['values']['op'] == "Delete Study Group Definition" && (!isset($form_state['storage']['confirm']))) {
|
|
watchdog('booking', "<pre>Studygroup deletion confirmation being set:\n@info</pre>", array('@info' => print_r( $form_state, true)));
|
|
$form_state['storage']['confirm'] = TRUE;
|
|
$form_state['rebuild'] = TRUE;
|
|
|
|
$new_count = $count->num;
|
|
}
|
|
elseif ($form_state['values']['op'] == 'Delete Study Group Definition') {
|
|
//verify that booking_pid is a number
|
|
if (! preg_match('/^[0-9]+$/', $values['booking_sid'])) {
|
|
drupal_set_message("Error: Invalid studygroup ID supplied. Unable to delete entry.", 'error', FALSE);
|
|
return "";
|
|
}
|
|
|
|
$num_deleted = db_delete('booking_studygroup_list')
|
|
->condition('sid', $values['booking_sid'])
|
|
->execute();
|
|
|
|
$new_count = $count->num - 1;
|
|
}
|
|
elseif ($form_state['values']['op'] == 'Update Study Group') {
|
|
|
|
//verify that booking_sid is a number
|
|
if (! preg_match('/^[0-9]+$/', $values['booking_sid'])) {
|
|
drupal_set_message("Error: Invalid studygroup ID supplied. Unable to update study group.", 'error', FALSE);
|
|
return "";
|
|
}
|
|
|
|
//update the study group
|
|
db_update('booking_studygroup_list')
|
|
->fields(array (
|
|
'booking_eventid' => $event->eid,
|
|
'booking_studygroup_descrip' => $values['booking_studygroup_descrip'],
|
|
'booking_num_group_sessions' => $values['booking_num_group_sessions'],
|
|
'booking_is_readinggroup' => $values['booking_is_readinggroup'] == 1 ? 'Y' : 'N',
|
|
))
|
|
->condition('sid', $values['booking_sid'])
|
|
->execute();
|
|
|
|
$new_count = $count->num;
|
|
}
|
|
|
|
//update the number of study groups now defined
|
|
variable_set('booking_studygroup_count', $new_count);
|
|
//call the mysql view update function so that the view matches the new number of study groups
|
|
_booking_node_create_mysqlview();
|
|
//redirect to the specified path now that the form has been processed
|
|
$form_state['redirect'] = $redirect_path;
|
|
} |