'roster/elections', 'title' => t('roster elections'), 'callback' => 'roster_elections', 'access' => user_access('assign booths'), 'type' => MENU_CALLBACK ); } else { if (arg(0) == 'node' && is_numeric(arg(1))){ $items[] = array( 'path' => 'node/'.arg(1).'/booth', 'title' => t('add booth'), 'callback' => 'roster_booth', 'access' => user_access('assign booths'), 'type' => MENU_LOCAL_TASK ); $items[] = array( 'path' => 'node/'.arg(1).'/csv', 'title' => t('csv export'), 'callback' => 'roster_csv', 'access' => user_access('assign booths'), 'type' => MENU_LOCAL_TASK ); } } return $items; } function roster_node_info() { return array( 'roster' => array( 'name' => t('roster'), 'module' => 'roster', 'description' => t('This is a polling day rostering module'), ) ); } function roster_perm(){ return array('assign booths'); } function roster_access($op, $node){ global $user; return user_access('assign booths'); } function roster_form(&$node) { $type = node_get_types('type', $node); // We need to define form elements for the node's title and body. $form['title'] = array( '#type' => 'textfield', '#title' => check_plain($type->title_label), '#required' => TRUE, '#default_value' => $node->title, '#weight' => -5 ); $form['group_id'] = array( '#type' => 'select', '#title' => t('CiviCRM group'), '#options' => roster_get_groups(), '#weight' => -5 ); return $form; } /** * Rewrite the form callbacks so that roster_assignment_form* * use the roster_assignment_form method */ function roster_forms() { # get the form id $args = func_get_args(); $form_id = $args[0][0]; # Ensure we map a callback for our form and not something else $forms = array(); if (strpos($form_id, "roster_assignment_form") === 0) { # Let the forms API know where to get the form data corresponding # to this form id. $forms[$form_id] = array('callback' => 'roster_assignment_form'); } return $forms; } /** * Get the booth information */ function roster_get_booth($bid){ $results = db_query("SELECT bid, name, last_votes, last_votes_total FROM {roster_booths} WHERE bid = %d", $bid); return db_fetch_object($results); } /** * Get all the booths for an election */ function roster_get_booths($nid){ $res = array(); $results = db_query("SELECT bid, name, last_votes, last_votes_total FROM {roster_booths} WHERE nid = %d", $nid); while ($row = db_fetch_object($results)){ $res[] = $row; } return $res; } /** * Get the roster for an election, and use the booth_id then period as the key */ function roster_get_roster($nid){ $res = array(); $results = db_query("SELECT aid, {roster_assignment}.bid, person, period, notes FROM {roster_assignment} INNER JOIN {roster_booths} ON ({roster_assignment}.bid = {roster_booths}.bid) WHERE {roster_booths}.nid = %d", $nid); while ($row = db_fetch_object($results)){ if ($res[$row->bid] != NULL) $booth = $res[$row->bid]; else $booth = array(); if ($booth[$row->period] == NULL) $booth[$row->period] = array($row); else $booth[$row->period][] = $row; $res[$row->bid] = $booth; } return $res; } /** * Intercept load and save calls so we can use the body to store the extra details */ function roster_nodeapi(&$node, $op){ switch($op){ case 'load': $node->group_id = $node->body; break; case 'submit': $node->body = $node->group_id; break; } } function roster_booth_form($booth=NULL) { $form = array(); $form['bid'] = array( '#type' => 'hidden', ); $form['name'] = array( '#type' => 'textfield', '#title' => t('name'), ); $form['last_votes'] = array( '#type' => 'textfield', '#title' => t('last vote'), ); $form['last_votes_total'] = array( '#type' => 'textfield', '#title' => t('last vote total'), ); $form['submit'] = array ( '#type' => 'submit', '#value' => t('Save'), ); foreach ($form as $id=>$val){ if ($booth->$id != NULL){ $val['#value'] = $booth->$id; $form[$id] = $val; } } return $form; } function roster_assignment_form($users, $selected=NULL, $bid, $period){ $form = array(); $form['person'] = array( '#type' => 'select', '#title' => t('Working'), '#options' => $users, ); $form['notes'] = array( '#type' => 'textfield', '#title' => t('Notes') ); $form['bid'] = array( '#type' => 'hidden', '#value' => $bid ); $form['period'] = array( '#type' => 'hidden', '#value' => $period ); $form['save'] = array( '#type' => 'submit', '#value' => t('Save') ); $form['aid'] = array( '#type' => 'hidden', ); $form['#submit'] = array("roster_assignment_form_submit" => 1); if ($selected != NULL){ $form['aid']['#value'] = $selected->aid; $form['notes']['#default_value'] = $selected->notes; $form['person']['#default_value'] = $selected->person; } return $form; } function roster_assignment_form_submit($form_id, $form_values){ if ($form_values['aid'] != NULL){ db_query("UPDATE {roster_assignment} SET person=%d, notes='%s' WHERE aid=%d;", $form_values['person'], $form_values['notes'], $form_values['aid']); } else { db_query("INSERT INTO {roster_assignment} (bid, period, person, notes) VALUES (%d, %d, %d, '%s');", $form_values['bid'], $form_values['period'], $form_values['person'], $form_values['notes']); } return 'node/'.arg(1); } /** * Display the matrix of booths for this election */ function roster_view($node, $teaser = FALSE, $page = FALSE) { if (!user_access('assign booths')) return FALSE; $node = node_prepare($node, $teaser); $node->content['myfield'] = array( '#value' => theme('node_example_order_info', $node), '#weight' => 1, ); // Get roster data $booths = roster_get_booths($node->nid); $roster = roster_get_roster($node->nid); $_users = roster_get_group_members($node->group_id); $users = array(''=>'None'); foreach ($_users[0] as $contact){ $users[$contact['contact_id']] = $contact['sort_name']; } // Init unique form ID $rosterFormId = 0; // Format roster table for display $table = "
| Booth | Morning | Afternoon |
|---|---|---|
| ".l($booth->name,'node/'.arg(1).'/booth/'.$booth->bid).
" ".$booth->last_votes.'/'.$booth->last_votes_total." | ";
$people = ($roster[$booth->bid] != NULL && $roster[$booth->bid][0] != NULL)?$roster[$booth->bid][0]:array();
$table .= ""; foreach ($people as $person) $table .= drupal_get_form('roster_assignment_form'.$rosterFormId++, $users, $person, $booth->bid, 0); // A new one too $table .= drupal_get_form('roster_assignment_form'.$rosterFormId++, $users, NULL, $booth->bid, 0); $table .= " | "; $people = ($roster[$booth->bid] != NULL && $roster[$booth->bid][1] != NULL)?$roster[$booth->bid][1]:array(); $table .= ""; foreach ($people as $person) $table .= drupal_get_form('roster_assignment_form'.$rosterFormId++, $users, $person, $booth->bid, 1); // A new one too $table .= drupal_get_form('roster_assignment_form'.$rosterFormId++, $users, NULL, $booth->bid, 1); $table .= " | "; $table .= "