Index: volunteer.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/volunteer/volunteer.module,v
retrieving revision 1.57
diff -u -r1.57 volunteer.module
--- volunteer.module	12 May 2006 22:22:26 -0000	1.57
+++ volunteer.module	16 May 2006 04:39:48 -0000
@@ -39,18 +39,10 @@
  * Allows the admin to set different defaults & settings for the module on the module config page
  */
 function volunteer_settings() {
-  $all_volunteer_coordinators = array();
-  // TODO: Check if this still works on 4.7. Probably doesn't since
-  // users_roles does not have entries for "authenticated user"
-  // anymore
-  $result = db_query("SELECT u.uid, u.name FROM {users} u INNER JOIN {users_roles} r ON u.uid = r.uid LEFT JOIN {permission} p ON r.rid = p.rid WHERE p.perm LIKE '%s' OR u.uid = 1", '%volunteer coordinate%');
-  while ($user = db_fetch_object($result)) {
-    $all_volunteer_coordinators[$user->uid] = $user->name;
-  }
+  $all_volunteer_coordinators = _volunteer_get_volunteer_coordinators();
   $variables = _volunteer_message_vars();
   $wildcards = implode(', ',array_keys($variables));
 
-  //end set all_volunteer_coordinators
   $form['default_settings'] = array(
     '#type' => 'fieldset',
     '#title' => t('Default Settings'),
@@ -243,13 +235,14 @@
   $form['email_settings']['wildcards'] = array(
     '#value' => '<p>'. t("FYI - The following 'Wildcards' can be used in messages to insert live data about the volunteer event: $wildcards") .'</p>',
   );
-  
+
   return $form;
 }
 
 /**
  * Provides the volunteer permission information for the drupal system.
  *
+ * @ingroup volunteer_core
  */
 function volunteer_perm() {
   return array('volunteer coordinate');
@@ -295,6 +288,8 @@
 
 /**
  * Implementation of hook_menu().
+ *
+ * @ingroup volunteer_core
  */
 function volunteer_menu($may_cache) {
   $items = array();
@@ -309,7 +304,7 @@
                        'callback' => 'volunteer_volunteer',
                        'type' => MENU_CALLBACK,
                        'access' => user_access('access content'));
- 
+
      $items[] = array('path' => 'volunteer/rating', 'title' => t('volunteer rate'),
                        'callback' => 'volunteer_rate',
                        'type' => MENU_CALLBACK,
@@ -335,7 +330,7 @@
  * Implementation of hook_civicrm_post
  * Delete contacts from volunteer dataset that get deleted in CiviCRM
  */
-function volunteer_civicrm_post( $op, $objectName, $objectId, $objectRef ) { 
+function volunteer_civicrm_post( $op, $objectName, $objectId, $objectRef ) {
   if ($objectName == 'Individual') {
     switch ($op) {
       case 'delete':
@@ -378,7 +373,7 @@
       case '5':
         $stage = l(t('Follow Up'), "volunteer/question_coordinator/$data->cid/$data->nid");
     }
-    $rows[] = array($data->cid, 
+    $rows[] = array($data->cid,
                     l($data->title, "node/$data->nid", array('title' => t('view this event'))),
                     format_date($data->timestamp, 'medium'),
                     $stage
@@ -410,11 +405,11 @@
 function volunteer_question_coordinator($cid, $nid) {
   civicrm_initialize(TRUE);
 
-  if($_POST['op']) {
+  if(isset($_POST['op'])) {
     switch ($_POST['op']) {
       case t('Approve'):
-          $output = volunteer_approve_volunteer($cid, $nid);
-        break;	
+          $form = volunteer_approve_volunteer($cid, $nid);
+        break;
       case t('Deny'):
           volunteer_deny_volunteer($cid, $nid);
           drupal_set_message(t('Volunteer denied'));
@@ -427,7 +422,7 @@
           volunteer_approve_volunteer($cid, $nid);
         break;
     }
-    if(!$output) {
+    if(!isset($form)) {
       drupal_goto('node/'.$nid.'/volunteer');
     }
   }
@@ -436,12 +431,14 @@
     $form['average'] = array(
       '#type' => 'item',
       '#title' => t('Avg. Rating'),
-      '#default_value' => l($rating['average'], "volunteer/rating/$cid/$node->nid"),
+      '#default_value' => l($rating['average'], "volunteer/rating/$cid/$nid"),
     );
     $form_title = variable_get('volunteer_form', '');
     db_set_active('civicrm');
     $title = crm_uf_get_profile_title($form_title);
-    $output .= crm_uf_get_profile_html( $cid, $title, CRM_CORE_ACTION_VIEW, false, true );
+    $form['profile'] = array(
+      '#value' => crm_uf_get_profile_html( $cid, $title, CRM_CORE_ACTION_VIEW, false, true ),
+    );
     db_set_active();
     $form['approve'] = array(
       '#type' => 'submit',
@@ -460,7 +457,6 @@
       '#value' => t('Cancel'),
     );
   }
-  // TODO: rework this function, $form isn't defined in all cases.
   drupal_set_title(t('Volunteer Status'));
   return drupal_get_form('volunteer_question_coordinator', $form);
 }
@@ -483,8 +479,10 @@
 function volunteer_volunteer($nid, $wait_list = NULL) {
   global $user;
   // TODO: convert to validate/submit model for better readability
-  $edit = $_POST['edit'];
-  $op = $_POST['op'];
+  // webchick: I tried to convert this, but because the entire form, including the submit button,
+  // is generated from civicrm, I can't seem to get any Drupalish FAPI stuff to work with it...
+  $edit = isset($_POST['edit']) ? $_POST['edit'] : array();
+  $op = isset($_POST['op']) ? $_POST['op'] : array();
 
   civicrm_initialize(TRUE);
   // Not a Drupal form, so we need to let CRM have a crack
@@ -496,6 +494,8 @@
   if ($crm_params['contact_id']) {
     //CRM knows about this user.  Is he/she signed up?
     $contact_id = $crm_params['contact_id'];
+    // NOTE: I would put this into a #pre_render function, but we're not allowed
+    // to pass in additional arguments ($contact_id) so I can't.
     if (volunteer_has_user_volunteered($nid, $contact_id)) {
       drupal_set_message(t('You have already signed up for this event.'), 'error');
       drupal_goto('node/'. $nid);
@@ -575,7 +575,7 @@
   $userID = $user->uid ? crm_uf_get_match_id( $user->uid ) : NULL;
   $action = $userID ? CRM_CORE_ACTION_UPDATE : CRM_CORE_ACTION_ADD;
 
-  //The following call not only generates HTML, it also 
+  //The following call not only generates HTML, it also
   //interacts with the CRM data store:
   $crm_form = crm_uf_get_profile_html( $userID, $title, $action, $register, $reset );
   db_set_active();
@@ -584,8 +584,8 @@
      $email = $user->mail ? $user->mail : _volunteer_get_email();
      $params = array('email' => $email);
       //$contact = crm_get_contact($params);
-     db_set_active('civicrm'); 
-     list($values, $options) = crm_contact_search($params); 	
+     db_set_active('civicrm');
+     list($values, $options) = crm_contact_search($params);
      db_set_active();
   }
   elseif ($userID) {
@@ -595,13 +595,13 @@
     list($values, $options) = crm_contact_search($params);
     db_set_active();
   }
-  
-  if ($values && $op == $submit_value) { 
+
+  if ($values && $op == $submit_value) {
     //We've already done what was needed with CRM
     //(?? what if there was an error), so do our business
     //with the Drupal side code:
     $param_list = array_values($values);
-    $edit = $param_list[0]; 
+    $edit = $param_list[0];
     //Do we need HTML in this case?
     //I think not.
     //Redirects should go here, most likely
@@ -639,72 +639,75 @@
 /********************************/
 
 function volunteer_rate($cid, $nid = 0) {
-  // TODO: convert to validate/submit model for better radability
   civicrm_initialize(TRUE);
 
   db_set_active('civicrm');
   $contact = crm_get_contact(array('contact_id' => $cid));
   db_set_active();
   $mail = $contact->location[1]->email[1]->email;
-  if($_POST['op'] != 'Submit') {
-    if ($nid) {
-      // rate a specific event
-      $event = db_fetch_object(db_query('SELECT title FROM {node} WHERE nid = %d', $nid));
-      $volunteer_contact_event = db_fetch_object(db_query('SELECT * FROM {volunteer_contact_event} WHERE cid = %d AND nid = %d', $cid, $nid));
-      $form = t('Rate') .' '. "<a href=\"mailto:$mail\">$mail</a>". '\'s performance at: <b>' . $event->title . '</b>.';
-      $rating_list = array(1 => 1,
-                            2 => 2,
-                            3 => 3,
-                            4 => 4,
-                            5 => 5);
-      $form['#method'] = 'post';
-      $form['#action'] = url("volunteer/rating/$cid/$nid");
-      $form[$event->title] = array(
-        '#type' => 'fieldset',
-        '#title' => $event->title,
-      );
-      $form[$event->title]['rating'] = array(
-        '#type' => 'select',
-        '#title' => t('Rating'),
-        '#default_value' => $volunteer_contact_event->rating,
-        '#options' => $rating_list,
-        '#description' => '1 being the worst and 5 being the best.',
-      );
-      $form[$event->title]['comments'] = array(
-        '#type' => 'textarea',
-        '#title' => t('Comments'),
-        '#default_value' => $volunteer_contact_event->comments,
-        '#cols' => 70,
-        '#rows' => 5,
-        '#description' => 'Leave comment specific to this event here',
-      );
-      $form[$event->title]['submit'] = array(
-        '#type' => 'submit',
-        '#value' => t('Submit'),
-      );
-    }
-    // output view of past ratings
-    $data = volunteer_get_rating($cid);
-    if (count($data['events'])) {
-      $header = array('Event', 'Rating', 'Coordinator', 'Comments');
-      $form['other_events'] = array(
-        '#type' => 'fieldset',
-        '#title' => t('Other Events which %name has volunteered for:', array('%name'=>$mail)),
-      );
-      $form['other_events']['events'] = array(
-        '#value' => theme('table', $header, $data['events']),
-      );
-    }
+  if ($nid) {
+    // rate a specific event
+    $form['#method'] = 'post';
+    $form['#action'] = url("volunteer/rating/$cid/$nid");
+    $form['#submit']['volunteer_rate_submit'] = array($cid, $nid);
 
-    return drupal_get_form('volunteer_rate', $form);
+    $event = db_fetch_object(db_query('SELECT title FROM {node} WHERE nid = %d', $nid));
+    $volunteer_contact_event = db_fetch_object(db_query('SELECT * FROM {volunteer_contact_event} WHERE cid = %d AND nid = %d', $cid, $nid));
+    $rating_list = array(1 => 1,
+                          2 => 2,
+                          3 => 3,
+                          4 => 4,
+                          5 => 5);
+   $form['rate'] = array(
+     '#value' => t('Rate') .' '. "<a href=\"mailto:$mail\">$mail</a>". '\'s performance at: <b>' . $event->title . '</b>.',
+   );
+    $form[$event->title] = array(
+      '#type' => 'fieldset',
+      '#title' => $event->title,
+    );
+    $form[$event->title]['rating'] = array(
+      '#type' => 'select',
+      '#title' => t('Rating'),
+      '#default_value' => $volunteer_contact_event->rating,
+      '#options' => $rating_list,
+      '#description' => '1 being the worst and 5 being the best.',
+    );
+    $form[$event->title]['comments'] = array(
+      '#type' => 'textarea',
+      '#title' => t('Comments'),
+      '#default_value' => $volunteer_contact_event->comments,
+      '#cols' => 70,
+      '#rows' => 5,
+      '#description' => 'Leave comment specific to this event here',
+    );
+    $form[$event->title]['submit'] = array(
+      '#type' => 'submit',
+      '#value' => t('Submit'),
+    );
   }
-  else{
-    db_query("UPDATE {volunteer_contact_event} SET comments = '%s', rating = %d WHERE cid = %d AND nid = %d", $_POST['edit']['comments'], $_POST['edit']['rating'], $cid, $nid);
-    drupal_set_message(t('Volunteer rating updated'));
-    drupal_goto("node/$nid/volunteer");
+  // output view of past ratings
+  $data = volunteer_get_rating($cid);
+  if (count($data['events'])) {
+    $header = array('Event', 'Rating', 'Coordinator', 'Comments');
+    $form['other_events'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Other Events which %name has volunteered for:', array('%name'=>$mail)),
+    );
+    $form['other_events']['events'] = array(
+      '#value' => theme('table', $header, $data['events']),
+    );
   }
+
+  return drupal_get_form('volunteer_rate', $form);
+}
+
+function volunteer_rate_submit($form_id, $form_values, $cid, $nid) {
+  db_query("UPDATE {volunteer_contact_event} SET comments = '%s', rating = %d WHERE cid = %d AND nid = %d", $form_values['comments'], $form_values['rating'], $cid, $nid);
+  drupal_set_message(t('Volunteer rating updated'));
+  drupal_goto("node/$nid/volunteer");
 }
 
+
 /**********************************/
 // Needs to be rewritten to use the CiviCRM activity history.
 function volunteer_get_rating($cid) {
@@ -716,7 +719,7 @@
   while($rating = db_fetch_object($result)) {
     $ratings = array();
     $coordinator = db_fetch_object(db_query('SELECT u.name, u.uid FROM {volunteer} v INNER JOIN {users} u ON v.uid = u.uid WHERE nid = %d', $rating->nid));
-    $rating_total = $rating_total + $rating->rating;	
+    $rating_total = $rating_total + $rating->rating;
     $ratings[] = l(t($rating->title), "node/$rating->nid");
     $ratings[] = l(t($rating->rating	), "volunteer/rating/$cid/$rating->nid");
     $ratings[] = l(t($coordinator->name), "node/$coordinator->uid");
@@ -847,7 +850,7 @@
 function volunteer_get_volunteers($nid, $single = TRUE, $cid = NULL) {
   $data = array();
 
-  if($single) {
+  if ($single) {
     $result = db_query('SELECT * FROM {volunteer_contact_event} WHERE nid = %d AND cid = %d', $nid, $cid);
   }
   else{
@@ -865,21 +868,23 @@
   else {
     drupal_set_message(t('You need to <a href="%url">assign</a> a CiviCRM profile form for volunteer data collection.', array('%url' => url('admin/settings/volunteer'))), 'error');
   }
-  if(!$single) {
+  if (!$single) {
     $header['stage'] = t('Stage');
     $header['rating'] = t('Avg. rating');
   }
   $data['header'] = $header;
-  while($volunteer = db_fetch_object($result)) {
+  while ($volunteer = db_fetch_object($result)) {
     $contact_data = array();
     db_set_active('civicrm');
     $contact = crm_get_contact(array('contact_id' => $volunteer->cid));
     db_set_active();
-    if (strtolower(get_class($contact)) != 'crm_contact_bao_contact') { 
+    if (strtolower(get_class($contact)) != 'crm_contact_bao_contact') {
       continue;
     }
+    // TODO @ killes: This does not populate 'first name', 'last name', etc. properly.
+    // Can I refer to $contact['contact_type_object'] directly?
     $contact_data = volunteer_get_properties($contact, $header);
-    if(!$single) {
+    if (!$single) {
       switch ($volunteer->stage) {
         case '1':
           $contact_data['stage'] = l(t('Wait List'), "volunteer/question_coordinator/$volunteer->cid/$nid");
@@ -990,8 +995,9 @@
         }
         break;
       case 'insert':
-        if (variable_get('volunteer_event_default',1)) {
-          $coordinator = user_access('volunteer coordinate', $node->uid) ? $node->uid : variable_get('volunteer_coordinator', 1);
+        if (variable_get('volunteer_event_default', 1)) {
+          $author = user_load(array('uid' => $node->uid));
+          $coordinator = user_access('volunteer coordinate', $author) ? $node->uid : variable_get('volunteer_coordinator', 1);
           $result = db_query("INSERT INTO {volunteer} (nid, wanted, uid) VALUES (%d, %d, %d)", $node->nid, variable_get('volunteer_wanted',2), $coordinator);
         }
         break;
@@ -1013,8 +1019,8 @@
   civicrm_initialize(TRUE);
 
   $nid = arg(1);
-  $op = $_POST['op'];
-  $edit = $_POST['edit'];
+  $op = isset($_POST['op']) ? $_POST['op'] : array();
+  $edit = isset($_POST['edit']) ? $_POST['edit'] : array();
 
   if ($nid) {
     switch ($op) {
@@ -1042,14 +1048,7 @@
     }
   }
   //start set all_volunteer_coordinators
-  $all_volunteer_coordinators = array();
-  // TODO: Check if this still works on 4.7. Probably doesn't since
-  // users_roles does not have entries for "authenticated user"
-  // anymore
-  $result = db_query("SELECT DISTINCT u.uid, u.name FROM {users} u INNER JOIN {users_roles} r ON u.uid = r.uid INNER JOIN {permission} p ON r.rid = p.rid WHERE p.perm LIKE '%volunteer coordinate%'");
-  while ($user = db_fetch_object($result)) {
-    $all_volunteer_coordinators[$user->uid] = $user->name;
-  }
+  $all_volunteer_coordinators = _volunteer_get_volunteer_coordinators();
   if (volunteer_event_is_volunteer($node)) {
     $form['volunteer_status'] = array(
       '#type' => 'fieldset',
@@ -1201,6 +1200,32 @@
 }
 
 /**
+ * Retrieves an array of volunteer coordinators
+ *
+ * @return array
+ *  Array in the form of uid => name, for example:
+ *  array(
+ *    1 => 'admin',
+ *    5 => 'joe user',
+ *  );
+ */
+function _volunteer_get_volunteer_coordinators() {
+  $all_volunteer_coordinators = array();
+
+  // Retrieve list of all users, then check to see which have 'volunteer coordinate' permissions
+  $result = db_query("SELECT u.uid FROM {users} u WHERE status = 1");
+  while ($user = db_fetch_object($result)) {
+    $account = user_load(array('uid' => $user->uid));
+    if (user_access('volunteer coordinate', $account)) {
+      $all_volunteer_coordinators[$account->uid] = $account->name;
+    }
+  }
+
+  return $all_volunteer_coordinators;
+}
+
+
+/**
  * Implementation of hook_form_alter().
  */
 function volunteer_form_alter($form_id, &$form) {
@@ -1235,21 +1260,22 @@
 }
 
 // from php.net
+// Note: Originally this was split into two functions, however this caused
+// infinite looping under PHP 5.1. I've now done a straight copy/paste from
+// http://ca3.php.net/manual/en/function.array-walk-recursive.php#48181
 function _volunteer_array_mergedown() {
+  global $outarray;
+  $outarray = array();
+  function array_walk_recphp4(&$val,$key) {
     global $outarray;
-   $outarray = array();
-   $params = func_get_args();
-   foreach ($params as $subarr) {
-       array_walk_recphp4($subarr, '');
-   }
-   return $outarray;
-} 
-
-function array_walk_recphp4(&$val,$key) {
-   global $outarray;
-   $val = (array)$val;
-   if (is_array($val)) array_walk($val,'array_walk_recphp4');
-   else {
-       $outarray[$key] = $val;
-   }
+    if (is_array($val)) array_walk($val,'array_walk_recphp4');
+    else {
+      $outarray[$key] = $val;
+    }
+  }
+  $params = func_get_args();
+  foreach ($params as $subarr) {
+    array_walk_recphp4($subarr, '');
+  }
+  return $outarray;
 }

