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

Contents of /contributions/modules/signup_scheduler/signup_scheduler.module

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


Revision 1.1 - (show annotations) (download) (as text)
Thu Dec 20 19:12:21 2007 UTC (23 months ago) by jrbeeman
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
Adding container for signup scheduler module, which allows signups to be opened and closed on a schedule
1 <?php
2
3 /**
4 * Implementation of hook_form_alter
5 */
6 function signup_scheduler_form_alter($form_id, &$form) {
7 switch ($form_id) {
8 // Node edit form
9 case $form['type']['#value'] .'_node_form':
10 signup_scheduler_alter_node_form($form_id, $form);
11 break;
12 // Signup admin node form
13 case 'signup_admin_node_form':
14 signup_scheduler_alter_signup_admin_node_form($form_id, $form);
15 break;
16 }
17 }
18
19
20 function signup_scheduler_alter_node_form($form_id, &$form) {
21 // Simple check to see if signups are enabled
22 // All logic for this is handled in signup.module
23 if (isset($form['signup'])) {
24 global $user;
25
26 // Load the node if it already exists.
27 if (isset($form['nid']['#value'])) {
28 $node = node_load($form['nid']['#value']);
29 }
30 else {
31 $node = NULL;
32 }
33
34 $form['signup']['node_settings']['schedule'] = array(
35 '#type' => 'fieldset',
36 '#title' => t('Schedule'),
37 );
38 $form['signup']['node_settings']['schedule']['signup_scheduler_status'] = array(
39 '#type' => 'checkbox',
40 '#title' => t('Enable scheduling of signups for this node'),
41 '#default_value' => $node->signup_scheduler_status,
42 '#description' => t('If enabled, signups for this node will automatically be turned on and off at midnight on the dates selected.'),
43 );
44 $form['signup']['node_settings']['schedule']['signup_scheduler_open'] = array(
45 '#type' => 'date',
46 '#title' => t('Open signups on'),
47 '#default_value' => signup_scheduler_timestamp_to_date($node->signup_scheduler_open),
48 '#description' => t('If this date is later than now, signups will be enabled, but closed, for this node until the specified date.'),
49 );
50 $form['signup']['node_settings']['schedule']['signup_scheduler_close'] = array(
51 '#type' => 'date',
52 '#title' => t('Close signups on'),
53 '#default_value' => signup_scheduler_timestamp_to_date($node->signup_scheduler_close),
54 );
55 }
56 }
57
58
59 function signup_scheduler_alter_signup_admin_node_form($form_id, &$form) {
60 if (isset($form['nid']['#value'])) {
61 $node = node_load($form['nid']['#value']);
62 }
63 if ($node->signup_scheduler_status) {
64 if (isset($form['status']['#options'])) {
65 $form['status']['#value'] = $form['status']['#options'][$form['status']['#default_value']];
66 unset($form['status']['#type']);
67 unset($form['status']['#options']);
68 unset($form['status']['#default_value']);
69 }
70 $form['limit']['#value'] = $form['limit']['#default_value'];
71 unset($form['limit']['#type']);
72 $form['submit']['#value'] = t('N/A (Edit node to change signup settings)');
73 $form['submit']['#description'] = 'Signup open and close status is being managed by the signup scheduled open and close dates.';
74 unset($form['submit']['#type']);
75 //dpm($form);
76 }
77 }
78
79
80 /**
81 * Implementation of hook_nodeapi
82 */
83 function signup_scheduler_nodeapi(&$node, $op, $a3, $a4) {
84 /**
85 * Validate
86 */
87 if ($op == 'validate' && $node->signup) {
88 $open = signup_scheduler_date_to_timestamp($node->signup_scheduler_open);
89 $close = signup_scheduler_date_to_timestamp($node->signup_scheduler_close);
90
91 // make sure close date is after open date
92 if (!($close > $open)) {
93 form_set_error('signup_scheduler_close', t('The signup scheduled close date must be after the scheduled open date.'));
94 }
95
96 if (!$node->signup_enabled) {
97 form_set_error('signup_scheduler_status', t('You have enabled signup scheduling, but have not enabled signups.'));
98 }
99 }
100
101 /**
102 * Insert
103 */
104 if ($op == 'insert' && $node->signup) {
105 $open = signup_scheduler_date_to_timestamp($node->signup_scheduler_open);
106 $close = signup_scheduler_date_to_timestamp($node->signup_scheduler_close);
107 db_query("INSERT INTO {signup_scheduler} (nid, status, open, close) VALUES (%d, %d, %d, %d)", $node->nid, $node->signup_scheduler_status, $open, $close);
108 // Close signups if the open date is later than now
109 if ($open > time()) {
110 signup_close_signup($node->nid);
111 }
112 }
113
114 /**
115 * Update
116 */
117 if ($op == 'update' && $node->signup) {
118 $has_record = db_result(db_query('SELECT COUNT(*) FROM {signup_scheduler} WHERE nid = %d', $node->nid));
119 $open = signup_scheduler_date_to_timestamp($node->signup_scheduler_open);
120 $close = signup_scheduler_date_to_timestamp($node->signup_scheduler_close);
121 if ($has_record) {
122 db_query("UPDATE {signup_scheduler} SET status = %d, open = %d, close = %d WHERE nid = %d", $node->signup_scheduler_status, $open, $close, $node->nid);
123 }
124 else {
125 db_query("INSERT INTO {signup_scheduler} (nid, status, open, close) VALUES (%d, %d, %d, %d)", $node->nid, $node->signup_scheduler_status, $open, $close);
126 }
127 // Close signups if the open date is later than now
128 if ($open > time()) {
129 signup_close_signup($node->nid);
130 }
131 // Open signups, if applicable
132 if (!$node->signup_status && $open < time() && $close > time() && ($node->signup_close_signup_limit == 0 || $node->signup_total < $node->signup_close_signup_limit)) {
133 signup_open_signup($node->nid);
134 drupal_set_message(t('Signups opened for !title.', array('!title' => l($node->title, "node/$node->nid"))));
135 }
136 // If using signup schedule, don't allow seat limit change to reopen the node
137 // This is really fragile, but I'm not sure of another way around it without hacking signup module
138 foreach ($_SESSION['messages']['status'] as $key => $message) {
139 if (stristr($message, t('signups re-opened'))) {
140 if ($open > time() || $close < time()) {
141 signup_close_signup($node->nid);
142 unset($_SESSION['messages']['status'][$key]);
143 if (!count($_SESSION['messages']['status'])) {
144 unset($_SESSION['messages']['status']);
145 }
146 }
147 }
148 }
149 }
150
151 /**
152 * Delete
153 */
154 if ($op == 'delete') {
155 db_query("DELETE FROM {signup_scheduler} WHERE nid = %d", $node->nid);
156 }
157
158 /**
159 * Load
160 */
161 if ($op == 'load') {
162 $result = db_query("SELECT * FROM {signup_scheduler} WHERE nid = %d", $node->nid);
163 if (db_num_rows($result)) {
164 $schedule = db_fetch_object($result);
165 $node->signup_scheduler_status = $schedule->status;
166 $node->signup_scheduler_open = $schedule->open;
167 $node->signup_scheduler_close = $schedule->close;
168 }
169 }
170 }
171
172
173 /**
174 * Implementation of hook_cron
175 * Change "enabled" status of signup-enabled modules based on scheduled open and close dates
176 */
177 function signup_scheduler_cron() {
178 // Close open signups
179 $result = db_query("SELECT ss.nid FROM {signup_scheduler} ss, {signup} s WHERE ss.close < %d AND ss.nid = s.nid AND s.status = 1", time());
180 while ($signup = db_fetch_object($result)) {
181 signup_close_signup($signup->nid, $cron = 'yes');
182 $node = node_load($signup->nid);
183 foreach (module_implements('signup_close') as $module) {
184 $function = $module .'_signup_close';
185 $function($node);
186 }
187 watchdog('signup', t('Signups closed for %event by cron.', array('%event' => $node->title)), WATCHDOG_NOTICE, l(t('view'), 'node/'. $node->nid));
188 }
189
190 // Open closed signups (only open if the close date is after now)
191 $result = db_query("SELECT ss.nid FROM {signup_scheduler} ss, {signup} s WHERE ss.open < %d AND ss.close > %d AND ss.nid = s.nid AND s.status = 0", time(), time());
192 while ($signup = db_fetch_object($result)) {
193 $nid = $signup->nid;
194 db_query("UPDATE {signup} SET status = 1 WHERE nid = %d", $nid);
195 $node = node_load(array('nid' => $nid));
196 foreach (module_implements('signup_open') as $module) {
197 $function = $module .'_signup_open';
198 $function($node);
199 }
200 watchdog('signup', t('Signups opened for %title by cron.', array('%title' => $node->title)), WATCHDOG_NOTICE, l(t('view'), 'node/'. $nid));
201 }
202 }
203
204
205 /**
206 * Convert a Drupal date field array to a timestamp
207 */
208 function signup_scheduler_date_to_timestamp($date) {
209 return mktime(0, 0, 0, $date['month'], $date['day'], $date['year']);
210 }
211
212
213 /**
214 * Convert a timestamp to a Drupal date field array
215 */
216 function signup_scheduler_timestamp_to_date($timestamp) {
217 $month = date('m', $timestamp);
218 $day = date('d', $timestamp);
219 $year = date('Y', $timestamp);
220 return array('month' => $month, 'day' => $day, 'year' => $year);
221 }

  ViewVC Help
Powered by ViewVC 1.1.2