'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 = ""; foreach ($booths as $booth){ $table .= ""; $people = ($roster[$booth->bid] != NULL && $roster[$booth->bid][0] != NULL)?$roster[$booth->bid][0]:array(); $table .= ""; $people = ($roster[$booth->bid] != NULL && $roster[$booth->bid][1] != NULL)?$roster[$booth->bid][1]:array(); $table .= ""; $table .= ""; } $table .= "
BoothMorningAfternoon
".l($booth->name,'node/'.arg(1).'/booth/'.$booth->bid). "
".$booth->last_votes.'/'.$booth->last_votes_total."
"; 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 .= ""; 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 .= "
"; $node->content['roster'] = array( '#value' => $table, '#weight' => 0, ); // Set body to null to hide on the view $node->content['body'] = NULL; return $node; } function roster_csv($node, $teaser = FALSE, $page = FALSE) { if (!user_access('assign booths')) return FALSE; $node = node_load(arg(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']; } drupal_set_header('Content-Type: text/csv; charset=utf-8'); drupal_set_header('Content-Disposition: attachment; filename="roster.csv";'); print '"Booth","Morning shift","Afternoon shift"'."\n"; foreach ($booths as $booth){ $morn = $roster[$booth->bid][0]; $after = $roster[$booth->bid][1]; print '"'.$booth->name.' '.$booth->last_votes.'/'.$booth->last_votes_total.'","'; $once = false; foreach ($morn as $p){ if ($once)print ' & '; print $users[$p->person].'('.$p->notes.')'; $once = true; } print '","'; $once = false; foreach ($after as $p){ if ($once)print ' & '; print $users[$p->person].'('.$p->notes.')'; $once = true; } print "\"\n"; } } /** * Setup new elections and booths */ function roster_booth($bid=NULL) { $form = drupal_get_form('roster_booth_form'); if ($bid != NULL){ $booth=roster_get_booth($bid); $form = drupal_get_form('roster_booth_form', $booth); } else { $form = drupal_get_form('roster_booth_form'); } return $form; } function roster_booth_form_submit($form_id, $form_values){ if ( $form_values['bid'] == null ){ db_query( "INSERT INTO {roster_booths} (nid, name, last_votes, last_votes_total) VALUES (%d, '%s', %d, %d)",arg(1), $form_values['name'], $form_values['last_votes'], $form_values['last_votes_total'] ); } else { db_query( "UPDATE {roster_booths} SET name='%s', last_votes=%d, last_votes_total=%d WHERE bid=%d", $form_values['name'], $form_values['last_votes'], $form_values['last_votes_total'], $form_values['bid'] ); } return 'node/'.arg(1); } /** * Get the CiviCRM group members by group id */ function roster_get_group_members($group_id) { // Check if CiviCRM is installed here. if (!module_exists('civicrm')) return false; // Initialization call is required to use CiviCRM APIs. civicrm_initialize(true); // Pass group 'title' to crm_get_groups to retrieve the group_id. $groups =& crm_get_groups(array('id' => $group_id)); $params = array('group' => array($groups[0]->id => 1)); // Define which contact fields should be retrieved $return_properties = array('sort_name' => 1, 'email' => 1, 'phone' => 1); // Define how we want the results sorted - also as an array. $sort = array('sort_name' => 'ASC'); $contacts =& crm_contact_search( $params, $return_properties, $sort , 0, 200); if (!$contacts) return false; return $contacts; } /** * Get a list of the CiviCRM groups */ function roster_get_groups() { // Check if CiviCRM is installed here. if (!module_exists('civicrm')) return false; // Initialization call is required to use CiviCRM APIs. civicrm_initialize(true); $_groups =& crm_get_groups(null,array('id','title')); $groups = array(); foreach ($_groups as $g){ $groups[$g->id] = $g->title; } return $groups; } ?>