diff --git a/booking.module b/booking.module index f11afe5..fb235a8 100644 --- a/booking.module +++ b/booking.module @@ -532,6 +532,23 @@ function booking_menu() { 'access arguments' => array('edit study groups'), //'type' => MENU_LOCAL_ACTION, ); + $items['admin/booking/studygroups/%/editleaders'] = array( + 'title' => 'Edit Study Group Leaders', + 'description' => 'Edit Study Group Leaders and Helpers', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('booking_studygroup_leadhelp_edit_form', 3), + 'access arguments' => array('edit study groups'), + //'type' => MENU_LOCAL_ACTION, + ); + + //callback for autocomplete lookup + $items['booking/studygroups/autocomplete'] = array( + 'title' => 'Autocomplete for studygroup allocations', + 'page callback' => '_booking_studygroups_name_autocomplete', + 'access arguments' => array('edit study groups'), + 'type' => MENU_CALLBACK + ); + /* $items['admin/booking/studygroups/calculate'] = array( 'title' => 'Calculate Study Groups', diff --git a/booking.studygroup_leaders.inc b/booking.studygroup_leaders.inc index 678aad7..679b409 100644 --- a/booking.studygroup_leaders.inc +++ b/booking.studygroup_leaders.inc @@ -140,8 +140,6 @@ function _booking_studygroups_retrieve_eligible_people() { } - - /** * Function for */ @@ -178,4 +176,168 @@ function _booking_studygroups_retrieve_groups() { //watchdog('booking', "
Study Group Sessions:\n@info
", array('@info' => print_r( $groups, true))); return $groups; +} + +/** + * autocomplete helper to look up names for room allocations + * based on https://www.drupal.org/node/854216 + * $string = string for search + */ +function _booking_studygroups_name_autocomplete($string) { + global $event; + $matches = array(); + + $query = db_select('booking_person', 'p') + ->fields('p', array('nid', 'booking_firstname', 'booking_lastname')); + $db_or = db_or()->condition('p.booking_lastname', '%' . db_like($string) . '%', 'LIKE')->condition('p.booking_firstname', '%' . db_like($string) . '%', 'LIKE'); + $db_and = db_and()->condition($db_or)->condition('p.booking_eventid', $event->eid, '='); + $result = $query->condition($db_and) + ->execute(); + + // save the query to matches + foreach ($result as $row) { + $name = $row->booking_lastname . ', ' . $row->booking_firstname . ' [' . $row->nid . ']'; + $matches[$name] = $name; + } + + // Return the result to the form in json + drupal_json_output($matches); +} + +/** + * Function to allow admin to edit leaders and helpers for a study group + */ +function booking_studygroup_leadhelp_edit_form($node, &$form_state, $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 edit group leaders and helpers.", 'error', FALSE); + drupal_goto('admin/booking/studygroups'); + return ""; + } + + //try to select the study group for this $group_id + $studygroup = db_query("SELECT * FROM {booking_studygroup_list} WHERE sid = :sid", array(':sid' => $group_id)) + ->fetchObject(); + + if (! $studygroup) { + drupal_set_message("Error: Could not find matching study group ID. Unable to edit leaders/helpers.", 'error', FALSE); + drupal_goto('admin/booking/studygroups'); + return ""; + } + + $prefix = t("

Study Group ‐ !descrip

Edit leaders and helpers for this group.

", + array('!descrip' => $studygroup->booking_studygroup_descrip)); + + //define the table header + $header = array ( + 'sid' => array('data' => t('Study Group Session ID'), 'field' => 'sid'), + 'booking_assign_leader' => array('data' => t('Leader')), + 'booking_assign_helper' => array('data' => t('Helper')), + 'booking_assign_reserveleader' => array('data' => t('Reserve Leader')), + ); + + //attach the custom css + $form['#attached']['css'] = array( + drupal_get_path('module', 'booking') . '/booking.css', + ); + + //create the container element for the whole table + $form['studygroups'] = array( + '#prefix' => '
', + '#suffix' => '
', + '#tree' => TRUE, + '#theme' => 'table', + '#header' => $header, + '#rows' => array(), + ); + + //define the default fields in a table row + $default_row = array(); + $default_row['sid'] = ""; + $default_row['booking_studygroup_leader'] = ""; + $default_row['booking_studygroup_helper'] = ""; + $default_row['booking_studygroup_reserveleader'] = ""; + + //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 = :gid AND m.booking_studygroup_role > 0", + array(':gid' => $group_id)); + $session_members = $session_members_query->fetchAllAssoc('booking_node_id'); + + //create an array representing the existing leaders/helpers for this group + $existing_leaders = array(); + foreach ($session_members as $person) { + $existing_leaders[$person->booking_session_id][$person->booking_studygroup_role] = $person->booking_lastname + . ', ' . $person->booking_firstname . ' [' . $person->booking_nodeid . ']'; + } + + //create the rows for the individual sessions (instances really) of this study group + for ($i = 0; $i < $studygroup->booking_num_group_sessions; $i++) { + + $leader = array ( + '#id' => 'booking-studygroup-leader-' . $i, + '#type' => 'textfield', + '#title' => 'Name', + '#title_display' => 'invisible', + '#name' => 'booking_studygroup_leader[' . $i . ']', + '#size' => 100, + '#autocomplete_path' => 'booking/studygroups/autocomplete', + '#value' => (!empty($existing_leaders[$i][1])) ? $existing_leaders[$i][1] : '', + '#attributes' => array('style' => array('width:200px')), + ); + + $helper = array ( + '#id' => 'booking-studygroup-helper-' . $i, + '#type' => 'textfield', + '#title' => 'Name', + '#title_display' => 'invisible', + '#name' => 'booking_studygroup_helper[' . $i . ']', + '#size' => 100, + '#autocomplete_path' => 'booking/studygroups/autocomplete', + '#value' => (!empty($existing_leaders[$i][2])) ? $existing_leaders[$i][2] : '', + '#attributes' => array('style' => array('width:200px')), + ); + + $reserveleader = array ( + '#id' => 'booking-studygroup-reserveleader-' . $i, + '#type' => 'textfield', + '#title' => 'Name', + '#title_display' => 'invisible', + '#name' => 'booking_studygroup_reserveleader[' . $i . ']', + '#size' => 100, + '#autocomplete_path' => 'booking/studygroups/autocomplete', + '#value' => (!empty($existing_leaders[$i][3])) ? $existing_leaders[$i][3] : '', + '#attributes' => array('style' => array('width:200px')), + ); + + $form['studygroups'][$i] = array( + 'booking-studygroup-leader' => &$leader, + 'booking-studygroup-helper' => &$helper, + 'booking-studygroup-reserveleader' => &$reserveleader, + ); + + $new_row = array(); + $new_row['sid'] = $i; + $new_row['booking_studygroup_leader'] = array('data' => &$leader); + $new_row['booking_studygroup_helper'] = array('data' => &$helper); + $new_row['booking_studygroup_reserveleader'] = array('data' => &$reserveleader); + $form['studygroups']['#rows'][$i] = $new_row; + + unset($leader); + unset($helper); + unset($reserveleader); + } + + //close out the form + $form['submit'] = array ( + '#type' => 'submit', + '#value' => t('Submit'), + ); + + return array ( + 'form' => $form, + ); } \ No newline at end of file