/[drupal]/contributions/modules/volunteer/volunteer.module
ViewVC logotype

Diff of /contributions/modules/volunteer/volunteer.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.53, Sun Feb 12 00:59:52 2006 UTC revision 1.54, Wed Mar 1 23:14:43 2006 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: volunteer.module,v 1.52 2005/12/31 00:44:51 killes Exp $  // $Id: volunteer.module,v 1.53 2006/02/12 00:59:52 killes Exp $
3    
4    
5  /**  /**
# Line 77  function volunteer_settings() { Line 77  function volunteer_settings() {
77      if (is_array($ufGroups) && count($ufGroups)) {      if (is_array($ufGroups) && count($ufGroups)) {
78        $correct_email_exists = FALSE;        $correct_email_exists = FALSE;
79        $email_exists = FALSE;        $email_exists = FALSE;
80          db_set_active('civicrm');
81        foreach ($ufGroups as $id => $title) {        foreach ($ufGroups as $id => $title) {
82          $profile = crm_uf_get_profile_fields($id, TRUE);          $profile = crm_uf_get_profile_fields($id, TRUE);
83          $has_correct_email = FALSE;          $has_correct_email = FALSE;
# Line 93  function volunteer_settings() { Line 94  function volunteer_settings() {
94            $forms[$id] = $title;            $forms[$id] = $title;
95          }          }
96        }        }
97          db_set_active();
98        if ($email_exists) {        if ($email_exists) {
99          if ($correct_email_exists) {          if ($correct_email_exists) {
100            $group_form .= form_select(t('CiviCRM profile form for volunteers'), 'volunteer_form', variable_get('volunteer_form', ''), $forms, t('Select a CiviCRM Profile form for volunteers. Users will fill out required fields in this form when volunteering for an event. You can <a href="%url">add</a> a volunteer specific profile. The profile needs at least an email field and it needs to be set to "Required" and "Active".', array('%url' => url('civicrm/admin/uf/group'))), 0, FALSE, TRUE);            $group_form .= form_select(t('CiviCRM profile form for volunteers'), 'volunteer_form', variable_get('volunteer_form', ''), $forms, t('Select a CiviCRM Profile form for volunteers. Users will fill out required fields in this form when volunteering for an event. You can <a href="%url">add</a> a volunteer specific profile. The profile needs at least an email field and it needs to be set to "Required" and "Active".', array('%url' => url('civicrm/admin/uf/group'))), 0, FALSE, TRUE);
# Line 318  function volunteer_question_coordinator( Line 320  function volunteer_question_coordinator(
320    else {    else {
321      $rating = volunteer_get_rating($cid);      $rating = volunteer_get_rating($cid);
322      $output = form_item(t('Avg. Rating'), l($rating['average'], "volunteer/rating/$cid/$node->nid"));      $output = form_item(t('Avg. Rating'), l($rating['average'], "volunteer/rating/$cid/$node->nid"));
323        $form_title = variable_get('volunteer_form', '');
324      db_set_active('civicrm');      db_set_active('civicrm');
325      $title = crm_uf_get_profile_title(variable_get('volunteer_form', ''));      $title = crm_uf_get_profile_title($form_title);
326      $output .= crm_uf_get_profile_html( $cid, $title, CRM_CORE_ACTION_VIEW, false, true );      $output .= crm_uf_get_profile_html( $cid, $title, CRM_CORE_ACTION_VIEW, false, true );
327      db_set_active();      db_set_active();
328      $form .= form_submit(t('Approve'));      $form .= form_submit(t('Approve'));
# Line 335  function volunteer_question_coordinator( Line 338  function volunteer_question_coordinator(
338    
339  /********************************/  /********************************/
340    
341    /**
342     * Check to see if a user is already signed up
343     * for the event in question
344     *
345     * @param int $nid node_id for the event
346     * @param int $contact_id CiviCRM contact_id for the user
347     */
348    
349    function volunteer_has_user_volunteered($nid, $contact_id){
350      $sql = 'SELECT cid FROM {volunteer_contact_event} WHERE cid = %d AND nid = %d';
351      return db_num_rows(db_query($sql, $contact_id, $nid)) > 0;
352    }
353    
354  function volunteer_volunteer($nid, $wait_list = NULL) {  function volunteer_volunteer($nid, $wait_list = NULL) {
355    global $user;    global $user;
356      $edit = $_POST['edit'];
357      $op = $_POST['op'];
358    
359    civicrm_initialize(TRUE);    civicrm_initialize(TRUE);
360    // Not a Drupal form    // Not a Drupal form, so we need to let CRM have a crack
361    if ($_POST['email'] || $_POST['op'] == t('Volunteer')) {    //at it before we do
362      $edit = $_POST;    $crm_params = array(); //user data if any
363      if (isset($edit['email'])) {    $crm_form = volunteer_volunteer_form($nid, $crm_params);
       $edit['mail'] = $edit['email'];  
     }  
     else {  
       $edit['email'] = $user->mail;  
       $edit['mail'] = $edit['email'];  
     }  
   }  
   $mail = $user->mail ? $user->mail : $edit['email'];  
   
   db_set_active('civicrm');  
364    // check if the user has already signed up    // check if the user has already signed up
365    $contact = crm_get_contact(array('email' => $mail));    $contact_id = NULL;
366    db_set_active();    if ($crm_params['contact_id']) {
367    if (strtolower(get_class($contact)) == 'crm_contact_bao_contact' && db_num_rows(db_query('SELECT cid FROM {volunteer_contact_event} WHERE cid = %d AND nid = %d', $contact->contact_type_object->contact_id, $nid))) {      //CRM knows about this user.  Is he/she signed up?
368      drupal_set_message(t('You have already signed up for this event.'), 'error');      $contact_id = $crm_params['contact_id'];
369      drupal_goto('node/'. $nid);      if (volunteer_has_user_volunteered($nid, $contact_id)) {
370          drupal_set_message(t('You have already signed up for this event.'), 'error');
371          drupal_goto('node/'. $nid);
372        }
373    }    }
374    // the form has been submitted    // the form has been submitted
375    else if(count($edit)) {    if (count($edit) && $contact_id && !form_get_errors()) {
     // Check data  
     db_set_active('civicrm');  
     $title = crm_uf_get_profile_title(variable_get('volunteer_form', ''));  
     if ($user->uid) {  
       civicrm_validate_data($edit, $user, $title);  
     }  
     db_set_active();  
     if (!form_get_errors()) {  
376        // Go ahead and save/update and proceed        // Go ahead and save/update and proceed
377        //insert into volunteer_contact_event        //insert into volunteer_contact_event
378        if ($user->uid) {        db_set_active('civicrm');
379          db_set_active('civicrm');        $contact = crm_get_contact(array('contact_id' => $contact_id));
380          $userID = crm_uf_get_match_id( $user->uid );        db_set_active();
         if ($userID) {  
           civicrm_form_data($edit, $user, $title, false);  
           $contact = crm_get_contact(array('contact_id' => $userID));  
         }  
         else {  
           crm_create_contact($edit);  
           $contact = crm_get_contact(array('email' => $user->mail));  
         }  
         db_set_active();  
       }  
       else {  
         db_set_active('civicrm');  
         crm_create_contact($edit);  
         $contact = crm_get_contact(array('email' => $edit['email']));  
         db_set_active();  
       }  
381        $group_id = variable_get('volunteer_contact_source', 1);        $group_id = variable_get('volunteer_contact_source', 1);
382        db_set_active('civicrm');        db_set_active('civicrm');
383        $group = crm_get_groups(array('id' => $group_id));        $group = crm_get_groups(array('id' => $group_id));
# Line 401  function volunteer_volunteer($nid, $wait Line 388  function volunteer_volunteer($nid, $wait
388        crm_add_group_contacts($group, $contact_array);        crm_add_group_contacts($group, $contact_array);
389        db_set_active();        db_set_active();
390        $stage = $wait_list ? 1 : 2;        $stage = $wait_list ? 1 : 2;
391        db_query('INSERT INTO {volunteer_contact_event} (cid, nid, timestamp, stage) VALUES (%d, %d, %d, %d)', $contact->contact_type_object->contact_id, $nid, time(), $stage);        db_query('INSERT INTO {volunteer_contact_event} (cid, nid, timestamp, stage) VALUES (%d, %d, %d, %d)', $contact_id, $nid, time(), $stage);
392        volunteer_mail($contact->id, $nid, 'new_volunteer');        volunteer_mail($contact_id, $nid, 'new_volunteer');
393        drupal_set_message(variable_get('volunteer_message_thanks', t('Thank you for offering to volunteer! Someone will be in touch with you shortly with details.')));        drupal_set_message(variable_get('volunteer_message_thanks', t('Thank you for offering to volunteer! Someone will be in touch with you shortly with details.')));
394        drupal_goto('node/'. $nid);        drupal_goto('node/'. $nid);
395      }     }
396      else {     else {
397         // display form
398        $event = node_load(array('nid' => $nid));        $event = node_load(array('nid' => $nid));
399        $output = theme('box', t('Volunteer for %event', array('%event' => theme('placeholder', $event->title))), volunteer_volunteer_form($nid, $edit));        $output = '<p>'.
400      }        variable_get('volunteer_message_sign_up',
401    }          t('Fill out this quick form to volunteer for this event.')) .'</p>';
402    else {        $output .= $crm_form;
403      // display form        $output = theme('box', t('Volunteer for %event', array('%event' => theme('placeholder', $event->title))), $output);
     $event = node_load(array('nid' => $nid));  
     $output = '<p>'. variable_get('volunteer_message_sign_up', t('Fill out this quick form to volunteer for this event.')) .'</p>';  
     $output .= volunteer_volunteer_form($nid);  
     $output = theme('box', t('Volunteer for %event', array('%event' => theme('placeholder', $event->title))), $output);  
404    }    }
405    drupal_set_title('Volunteering');    drupal_set_title('Volunteering');
406    print theme('page', $output);    print theme('page', $output);
# Line 424  function volunteer_volunteer($nid, $wait Line 408  function volunteer_volunteer($nid, $wait
408    
409  /********************************/  /********************************/
410    
 function volunteer_volunteer_form($nid, $edit = array()) {  
   global $user;  
411    
412    /**
413     * Utility function to extract CRM email data from POST
414     * This should probably move into civicrm.module...
415     */
416    function _volunteer_get_email(){
417      if(isset($_POST['op'])){
418            foreach($_POST as  $key => $val){
419              if(strncmp($key, 'email', 5) == 0){
420                    //We only need 1 email
421                    return $val;
422              }
423            }
424      }
425      return '';
426    }
427    
428    
429    function volunteer_volunteer_form($nid, &$edit) {
430      global $user;
431      //why factor this? Because the HTML 1.0 designers did not
432      //care about I18n...
433      $submit_value = t('Volunteer');
434      //Preliminaries for getting CRM parameters
435      $op = $_POST['op'];
436      $form_title = variable_get('volunteer_form', '');
437    db_set_active('civicrm');    db_set_active('civicrm');
438    $title = crm_uf_get_profile_title(variable_get('volunteer_form', ''));    $title = crm_uf_get_profile_title($form_title);
439      $reset = $op ? FALSE : TRUE;
440      $register = FALSE; //always FALSE in 1.3 and later
441      $userID = $user->uid ? crm_uf_get_match_id( $user->uid ) : NULL;
442      $action = $userID ? CRM_CORE_ACTION_UPDATE : CRM_CORE_ACTION_ADD;
443    
444      //The following call not only generates HTML, it also
445      //interacts with the CRM data store:
446      $crm_form = crm_uf_get_profile_html( $userID, $title, $action, $register, $reset );
447    db_set_active();    db_set_active();
448    if ( $user->uid ) {    if ( !$userID && $op) {
449        //if everything went well, we have one now
450         $email = $user->mail ? $user->mail : _volunteer_get_email();
451         $params = array('email' => $email);
452          //$contact = crm_get_contact($params);
453         db_set_active('civicrm');
454         list($values, $options) = crm_contact_search($params);
455         db_set_active();
456      }
457      elseif ($userID) {
458        //Fix for 51833
459        $params = array('id' => $userID);
460      db_set_active('civicrm');      db_set_active('civicrm');
461      $userID = crm_uf_get_match_id( $user->uid );      list($values, $options) = crm_contact_search($params);
     $register = TRUE;  
     if (!$userID && count($edit)) {  
       civicrm_register_data($edit, $user, $title, FALSE);  
       $userID = crm_uf_get_match_id( $user->uid );  
       $register = FALSE;  
     }  
     $form = crm_uf_get_profile_html( $userID, $title, null, FALSE, $register );  
462      db_set_active();      db_set_active();
463      $form .= form_submit(t('Volunteer'));    }
464      $url = url("node/$nid");  
465      if ($values && $op == $submit_value) {
466        //We've already done what was needed with CRM
467        //(?? what if there was an error), so do our business
468        //with the Drupal side code:
469        $param_list = array_values($values);
470        $edit = $param_list[0];
471        //Do we need HTML in this case?
472        //I think not.
473        //Redirects should go here, most likely
474        return '';
475    }    }
476    else {    else {
477      db_set_active('civicrm');      //Either an error occurred, or we haven't
478      $_POST['postURL'] = url("volunteer/volunteer/$nid");      //done anything yet.
479      $form = crm_uf_get_create_html(TRUE);      $form = $crm_form;
480      db_set_active();      $form .= form_submit(t('Volunteer'));
481        $url = url("volunteer/volunteer/$nid");
482        return form($form, 'post', $url);
483    }    }
   $url = url("volunteer/volunteer/$nid");  
   
   return form($form, 'post', $url);  
484  }  }
485    
486    
# Line 647  function volunteer_get_volunteers($nid, Line 675  function volunteer_get_volunteers($nid,
675    else{    else{
676      $result = db_query('SELECT * FROM {volunteer_contact_event} WHERE nid = %d ORDER BY stage DESC', $nid);      $result = db_query('SELECT * FROM {volunteer_contact_event} WHERE nid = %d ORDER BY stage DESC', $nid);
677    }    }
678      $form_title = variable_get('volunteer_form', '');
679    db_set_active('civicrm');    db_set_active('civicrm');
680    $header_fields = crm_uf_get_profile_fields(variable_get('volunteer_form', ''));    $header_fields = crm_uf_get_profile_fields($form_title);
681    db_set_active();    db_set_active();
682    if (is_array($header_fields)) {    if (is_array($header_fields)) {
683      foreach($header_fields as $field) {      foreach($header_fields as $field) {

Legend:
Removed from v.1.53  
changed lines
  Added in v.1.54

  ViewVC Help
Powered by ViewVC 1.1.2