--- rsvp.module	2005-07-29 20:02:36.000000000 -0500
+++ /home/paul/Desktop/rsvp.module	2005-11-02 14:39:42.274658080 -0600
@@ -1,5 +1,5 @@
 <?php
-// $Id: rsvp.module,v 1.7 2005/07/29 23:56:41 ankur Exp $
+// $Id: rsvp.module,v 1.1.2.1 2005/10/03 21:28:16 tod Exp $
 
 /**
  * @module rsvp
@@ -9,6 +9,10 @@
  *
  */
 
+
+// PS - changed $node->start to $node->event_start, for proper display of dates.
+
+
 include(drupal_get_path('module', 'rsvp') .'/rsvp.theme');
 
 /**
@@ -59,7 +63,7 @@
  * @ingroup rsvp_core
  */
 function rsvp_perm() {
-  return array("administer rsvp", "create rsvp");
+  return array("administer rsvp", "create rsvp", "direct register rsvp");
 }
 
 /**
@@ -80,6 +84,9 @@
       if(user_access('create rsvp')) {
         $links[] =l(t('create rsvp'), "rsvp/create/$node->nid", array("title" => t('Create an rsvp and invite others to this event')));
       }
+      if(_rsvp_can_register_node($node->nid)){
+        $links[] = l(t('register'), "rsvp/register/$node->nid", array("title" => t('register for this event now')));
+      }
     }
     break;
     case 'event_node_table' :
@@ -95,12 +102,30 @@
       elseif(user_access('create rsvp')) {
         $links[] = l(t('rsvp'), "rsvp/create/$node->nid", array("title" => t('create an rsvp and invite others to this event')));
       }
+      if(_rsvp_can_register_node($node->nid)){
+        $links[] = l(t('register'), "rsvp/register/$node->nid", array("title" => t('register for this event now')));
+      }
       break;
   }
   return $links ? $links : array();
 }
 
 /**
+ * Implementation of hook_settings()
+ */
+function rsvp_settings(){
+  $output = $group = '';
+  $group .= form_radios(t('Allow "Maybe" Replies'), 'rsvp_allow_maybe', variable_get('rsvp_allow_maybe', 1),
+     array(1 => 'Yes', 0 => 'No'),
+     t('When people reply to invitations, they may choose "Yes", "No", or "Maybe" as their response. If you do not wish to offer the "Maybe" option, set this to "No". NOTE: this will also disable the "Might Attend" list in the invite page. Any existing "maybes" will not be shown.'));
+  $group .= form_radios(t('Allow Direct Registration'), 'rsvp_direct_register', variable_get('rsvp_direct_register', 0),
+     array(1 => 'Yes', 0 => 'No'),
+     t('Display events with a link that allows direct registration, bypassing the invitation process. You must add the "direct register rsvp" permission to the user roles that you would like to allow to directly register in the access control page.'));
+  $output .= form_group(t("RSVP Settings"), $group);
+  return $output;
+}
+
+/**
  * Implementation of hook_menu().
  */
 function rsvp_menu($may_cache) {
@@ -131,7 +156,15 @@
                      'callback' => 'rsvp_manage',
                      'type' => MENU_LOCAL_TASK,
                      'weight' => '1');
-
+    // direct registration callback
+    if(variable_get('rsvp_direct_register', 0)){
+      $items[] = array('path' => 'rsvp/register',
+                       'title' => t('register'),
+                       'access' => user_access('direct register rsvp'),
+                       'callback' => 'rsvp_register',
+                       'type' => MENU_CALLBACK,
+                       'weight' => '1');
+    }
     // callbacks
     $items[] = array('path' => 'rsvp/create',
                      'title' => t('create rsvp'),
@@ -204,7 +237,7 @@
 
     if($node->nid) {
       drupal_set_title(t('Your RSVPs for %event', array('%event' => $node->title)));
-      $title = l($node->title, 'node/'. $node->nid, array('title' => t('view this event'))) .' - '. format_date($node->event_start, 'small', '', $node->start_offset);
+      $title = l($node->title, 'node/'. $node->nid, array('title' => t('view this event'))) .' - '. date("M d, Y", $node->event_start);
     }
     else {
       drupal_set_message(t('The event associatted with this rsvp has been deleted'));
@@ -232,7 +265,7 @@
   foreach($list as $nid => $rsvps) {
     $node = node_load(array('nid' => $nid));
     if($node->nid) {
-      $content .= theme('item_list', $rsvps, l($node->title, 'node/'. $node->nid, array('title' => t('view this event'))) .' - '. format_date($node->event_start, 'small', '', $node->start_offset));
+      $content .= theme('item_list', $rsvps, l($node->title, 'node/'. $node->nid, array('title' => t('view this event'))) .' - '. date("M d, Y", $node->event_start));
     }
     else {
       // node has been deleted
@@ -244,6 +277,32 @@
 }
 
 /**
+ * This handles direct event registrations.
+ */
+function rsvp_register($nid){
+  global $user;
+  // Get RSVP records and RIDs for this node.
+  $rsvps = db_query('SELECT * FROM {rsvp} WHERE nid = %d', $nid);
+  if(db_num_rows($rsvps) < 1){
+    print theme('page', "There are no RSVPs set for this node.");
+  }
+  else if(db_num_rows($rsvps) == 1){
+    $rsvp = db_fetch_object($rsvps);
+    rsvp_set_invites($user->mail, $rsvp->rid);
+    $invite_result = db_query(
+      "select * from {rsvp_invite} where rid = %d and email = '%s'",
+      $rsvp->rid, $user->mail);
+    $invite = db_fetch_object($invite_result);
+    drupal_goto("rsvp/email/$invite->hash");
+  }
+  else if(db_num_rows($rsvps) > 1){
+    // TODO: this should be handled better.
+    print theme('page', "There are multiple RSVPs set for this node. Direct registration is allowed only when there is one RSVP set per node.");
+  }
+
+}
+
+/**
 * Handles the rsvp create requests.
 *
 * @ingroup rsvp_core
@@ -266,7 +325,7 @@
     default :
       $node = node_load(array("nid" => $nid));
       $content = rsvp_create_form($nid);
-      drupal_set_title(t('Create RSVP for: %title on %date', array('%title' => $node->title, '%date' => format_date($node->event_start, 'small', '', $node->start_offset))));
+      drupal_set_title(t('Create RSVP for: '). $node->title ." on ". date("M d,  Y", $node->event_start));
       break;
   }
   print theme('page', $content);
@@ -457,8 +516,8 @@
       }
 
       if($node) {
-        drupal_set_title(t('Your Invites for: %title on %date', array('%title' => $node->title, '%date' => format_date($node->event_start, 'small', '', $node->start_offset))));
-        $title = l($node->title, 'node/'. $node->nid, array('title' => t('view this event'))) .' - '. format_date($node->event_start, 'small', '', $node->start_offset);
+        drupal_set_title(t('Your Invites for: %title on %date', array('%title' => $node->title, '%date' => date("M d,  Y", $node->event_start))));
+        $title = l($node->title, 'node/'. $node->nid, array('title' => t('view this event'))) .' - '. date("M d, Y", $node->event_start);
       }
       else {
         // it's ok if the event is deleted, we just let the user know that
@@ -477,7 +536,7 @@
 
     $list = array();
     while ($invite = db_fetch_object($invites)) {
-      $list[$invite->nid][] = l($invite->name, "rsvp/email/". $invite->hash, array('title'=> t('view invite')));
+      $list[$invite->nid][] = l($invite->name, "rsvp/email/". $invite->hash, array('title'=>'view invite'));
     }
 
     if($nid) {
@@ -487,8 +546,9 @@
     else {
       foreach($list as $nid => $links) {
         $node = node_load(array("nid" => $nid));
+//print_r($node);
         if($node->nid) {
-          $content .= theme('item_list', $links, t('for: ') . l($node->title, 'node/'. $node->nid, array('title' => t('view this event'))) .' - '. format_date($node->event_start, 'small', '', $node->start_offset));
+          $content .= theme('item_list', $links, t('for: ') . l($node->title, 'node/'. $node->nid, array('title' => t('view this event'))) .' - '. date("M d, Y", $node->event_start));
         }
         else {
           // node has been deleted
@@ -770,7 +830,9 @@
     $content .= theme('item_list', $noreply, t('No reply'));
     $content .= theme('item_list', $yes, t('Attending'));
     $content .= theme('item_list', $no, t('Not attending'));
-    $content .= theme('item_list', $maybe, t('Might attend'));
+    if(variable_get('rsvp_allow_maybe', 1)){
+      $content .= theme('item_list', $maybe, t('Might attend'));
+    }
   }
 
   return form_group(t('Responses'), $content);
@@ -785,7 +847,7 @@
 */
 function rsvp_reply_form($hash) {
   $invite = _rsvp_get_invite($hash);
-  $responses = array('yes' => t('Yes'), 'no' => t('No'), 'maybe' => t('Maybe'));
+  $responses = _rsvp_get_response_array();
   if($invite->response == 'none') {
     $responses['none'] = t('None');
   }
@@ -1056,7 +1118,7 @@
 * @return dbresultset of the requested attendees.
 */
 function _rsvp_attendance_totals($rid) {
-  $responses = array(t('No Response') => 'none', t('Yes') => 'yes', t('No') => 'no', t('Maybe') => 'maybe');
+  $responses = array_merge(array(t('No Response') => 'none'), _rsvp_get_response_array());
   foreach($responses as $key => $response) {
     $total = db_fetch_object(db_query('SELECT count(*) as total FROM {rsvp_invite} WHERE rid = %d AND response = \'%s\'', $rid, $response));
     $totals[$key] = $total->total;
@@ -1171,6 +1233,35 @@
 }
 
 /**
+ * Returns true if one or more RSVPs exist for this node, which
+ * the user can register for. The user is not considered to be
+ * allowed to register for events that they're already invited to.
+ * @param $nid The node ID to check.
+ */
+function _rsvp_can_register_node($nid){
+  global $user;
+  /* Check that direct registration is enabled, and that the user can access
+   * that feature. */
+  if(!variable_get('rsvp_direct_register', 0) || !user_access('direct register rsvp')){
+    return false;
+  }
+  /* Try to find rsvps for this node that the user is not already
+   * invited to. */
+  if(db_num_rows(db_query('
+       SELECT rsvp.rid
+       FROM {rsvp}
+       LEFT OUTER JOIN {rsvp_invite}
+         ON {rsvp}.rid = {rsvp_invite}.rid
+            AND {rsvp_invite}.uid = %d
+            AND {rsvp_invite}.invited = 1
+       WHERE {rsvp}.nid = %d
+         AND {rsvp_invite}.rid is null', $user->uid, $nid)) < 1){
+    return false;
+  }
+  return true;
+}
+
+/**
 * Checks to see if an rsvp has enabled attendee invite sending.
 *
 * @ingroup rsvp_functions.
@@ -1255,7 +1346,7 @@
   $from = $user->name .' <'. $user->mail .'>';
   $subject = $site ." - ". t('Event Invitation');
   $to = $invite->email;
-  $body = t("Hello!\n\nYou have been invited to attend an event by %owner at %site.\n\nYou can view the event itself by following this link: \n%eventurl\n\nYou can view the full invitation by following this link: \n%url\n\nHere is the invitation message:\n", array('%owner'=>$user->name, '%site'=>$site, '%eventurl'=>url("node/$invite->nid", NULL, NULL, 1), '%url'=>url("rsvp/email/$invite->hash", NULL, NULL, 1))) .$invite->invite_text;
+  $body = strtr("Hello!\n\nYou have been invited to attend an event by %owner at %site.\n\nYou can view the event itself by following this link: \n%eventurl\n\nYou can view the full invitation by following this link: \n%url", array('%owner'=>$user->name, '%site'=>$site, '%eventurl'=>url("node/$invite->nid", NULL, NULL, 1), '%url'=>url("rsvp/email/$invite->hash", NULL, NULL, 1))) ."\n\nHere is the invitation message:\n$invite->invite_text";
   return mail($to, $subject, $body, "From: $from\nReply-to: $from\nX-Mailer: Drupal (rsvp.module)\nReturn-path: <$from>\nErrors-to: $from\n",$webmaster);
 }
 
@@ -1283,7 +1374,7 @@
   while($attendee = db_fetch_object($attendees)) {
     $invite = _rsvp_get_invite($attendee->hash);
 
-    $body = t("Hello!\n\nYou have been sent a message by %sender at %site.\n\nYou can view the invitation from where it originated by following this link: \n%url\n\nHere is the message:\n", array('%sender'=>$user->name, '%site'=>$site, '%url'=>url("rsvp/email/$invite->hash", NULL, NULL, 1))) . $edit['body'];
+    $body = strtr("Hello!\n\nYou have been sent a message by %sender at %site.\n\nYou can view the invitation from where it originated by following this link: \n%url", array('%sender'=>$user->name, '%site'=>$site, '%url'=>url("rsvp/email/$invite->hash", NULL, NULL, 1))) ."\n\nHere is the message:\n". $edit['body'];
 
     $to = "$attendee->email <". $attendee->email .'>';
     if(mail($to, $subject, $body, "From: $from\nReply-to: $from\nX-Mailer: Drupal (rsvp.module)\nReturn-path: <$from>\nErrors-to: $from\n",$webmaster)) {
@@ -1296,4 +1387,22 @@
 
   return $status;
 }
+
+
+/**
+ * This returns a list of response choices. It excludes the "maybe" option
+ * if that has been selected in the RSVP options.
+ *
+ * @return An associative array of response identifiers and their labels.
+ */
+function _rsvp_get_response_array(){
+  $responses = array('yes' => t('Yes'), 'no' => t('No'));
+  if(variable_get('rsvp_allow_maybe', 1)){
+    $responses['maybe'] = t('Maybe');
+  }
+  return $responses;
+}
+
+
+
 ?>
