-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebform_event.module
342 lines (314 loc) · 12.7 KB
/
webform_event.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
/**
* @file
* Code for the Webform Queue feature.
*/
include_once 'webform_event.features.inc';
function webform_event_theme() {
return array(
'webform_event' => array(
'template' => 'webform_event',
'variables' => array('headers' => NULL,
'submissions' => NULL,
'queue' => NULL,
'cancel' => NULL),
),
);
}
/**
* Implements hook_permission().
*/
function webform_event_permission() {
return array(
'view expired events' => array(
'title' => t(
'View events that have have passed due date.'
),
'description' => t(
'Allows to see expired events. Set this for event moderators.'
),
),
'administer events' => array(
'title' => t(
'Access administration tools'
),
'description' => t(
'Grants access to administration page for events'
),
),
);
}
/**
* Implements hook_form_alter().
*/
function webform_event_form_alter(&$form, &$form_state, $form_id) {
// CHECKS AND MISCALLENEOUS
// This is to get rid of notices from undefined indexes
if (!isset($form['#node'])) {
return;
}
// Check that we are dealing with webform with queue
$nodetype = $form['#node']->type;
// Get nid
$nid = "";
if (!empty($form['#node']->nid)) {
$nid = $form['#node']->nid;
}
// Return current path
$path = current_path();
// Assign check for node view
$check = "node/" . $nid;
$node = $node = $form['#node'];
// Check we have node of type event and we are on node view.
// We don't want form handling to trigger on edit or add pages.
if ($nodetype == "event" && $path == $check) {
// BASE INFORMATION SETUP
// Get Participant limit
$participants = "";
$participantField = field_view_field('node', $node, 'field_webform_event_participants');
if (isset($participantField['#items'][0]['value'])) {
$participants = $participantField['#items'][0]['value'];
}
// Get all results.
$actions = db_select('webform_submitted_data', 'data')
->fields('data', array('data'))
->condition('data.nid', $nid, '=')
->condition('data.data', array('signup', 'cancel', 'queue'), 'IN');
$actions->addExpression('COUNT(nid)', 'count');
$actions->groupBy('data.data');
$result = $actions->execute();
// Setup vars
$queue = 0;
$cancel = 0;
$dbresult = 0;
while ($action = $result->fetchAssoc()) {
switch($action['data']) {
case 'queue':
$queue = $action['count'];
break;
case 'cancel':
$cancel = $action['count'];
break;
case 'signup':
$dbresult = $action['count'];
break;
default:
break;
}
}
// PARTICIPATION LIMIT FUNCTIONALITY
// Limit is full
if ($dbresult >= $participants && isset($participants) && $participants > 0) {
// Append this to normal page title
$fullTitle = $form['#node']->title . t(" - Participation limit reached");
// Participation limit is full, modify page title and unset the form until there
// is more space. User can free up space to course by deleting submissions if he wants to.
drupal_set_title($fullTitle);
// Notify user that submission limit has been reached. Clear possible previous messages.
unset($form['submitted']['event_action']['#options']['signup']);
if (!empty($_SESSION['messages'])) {
unset($_SESSION['messages']);
}
drupal_set_message(t('This event is currently full. However you can queue for the event.'), 'warning');
}
// Don't allow signing up if there is people in queue even if there is space on course,
// in other words - maintainer has not updated someone from queue to course or is currently doing so.
// This protects course from signups while maintainer is working on with signups.
elseif ($dbresult < $participants && isset($participants) && $participants > 0 && $queue > 0) {
unset($form['submitted']['event_action']['#options']['signup']);
}
else
// Unset queue so it's not an option when there is space on course.
{
unset($form['submitted']['event_action']['#options']['queue']);
}
// END DATE FUNCTIONALITY
// Let's check if ending date has passed and disable submission
if (!user_access('view expired events')) {
// Convert ending date to epoch time stamp for comparison. Field must have no timezone for this to work
$endDateField = field_view_field('node', $node, 'field_webform_event_end_date');
$endDate = "";
if (isset($endDateField['#items'][0]['value'])) {
$endDate = strtotime($endDateField['#items'][0]['value']);
}
// Get current date and set variable for comparison
$today = getdate();
// Get last key epoch value from date array for epoch stamp comparison
$today = array_slice($today, -1, 1, TRUE);
$today = $today[0];
// Compare date today to subscription end date and disable edit button if it is equal or greater than end date
// If epoch value of today is greater or same than epoch value of end date disable submit.
if ($today >= $endDate && !empty($endDate)) {
// Unset form components
unset($form['actions']['submit']);
unset($form['submitted']);
// Due date has passed, modify page title
$dueTitle = $form['#node']->title . t(" - due date has passed");
drupal_set_title($dueTitle);
// Notify user that due date for signups has passed. Clear possible previous messages.
if (!empty($_SESSION['messages'])) {
unset($_SESSION['messages']);
}
drupal_set_message(t('Ending date for event subscriptions has passed. Signup is disabled.'), 'warning');
}
}
// MARKUP FOR THE PAGE
// How many are currently participating for markup
$currentParticipants = t("Currently participating %dbresult / %participants. ", array('%dbresult' => $dbresult, '%participants' => $participants));
// If there is queue, tell how much
if ($queue > 0) {
$currentParticipants .= t("There is %queue person(s) in the queue.", array('%queue' => $queue));
}
// Has someone canceled? TODO: Make this message shown only to administrators or implement custom permission for this.
if ($cancel > 0 && user_access('show webform administrator messages')) {
drupal_set_message(t('ADMINISTRATOR NOTICE: There is person(s) that have canceled their participation. Remove their signups to free up space from <a href="/node/'.$nid.'/manage-event">here.</a>'), 'warning');
}
// Move information to markup
if (isset($participantField['#items'][0]['value'])) {
$form['#node']->content['field_webform_event_participants'][0]['#markup'] = $currentParticipants;
}
// VALIDATION
// Custom validation to prevent overflowing
array_unshift($form['#validate'], 'webform_event_validate');
}
// FORM PROTECTION
// Let's give a event_action -compononent a slight protection.
if (isset($form['form_token']['#id']) && $form['form_token']['#id'] == 'edit-webform-components-form-form-token') {
unset($form['#node']->webform['components']['99']);
// Instant diet to also remove lingering weight
unset($form['components']['99']);
}
}
/**
* Implements hook_validate().
*/
function webform_event_validate($form) {
// Declare nid and participant limit.
$node = $node = $form['#node'];
// Get Participant limit
$participants = "";
$participantField = field_view_field('node', $node, 'field_webform_event_participants');
if (!empty($node->nid)) {
$nid = $node->nid;
}
if (isset($participantField['#items'][0]['value'])) {
$participants = $participantField['#items'][0]['value'];
}
// Count participants at the moment submit is launched.
$dbresult = db_query("SELECT COUNT(nid) as count FROM {webform_submitted_data} WHERE nid = :nid AND data = :data", array(":nid" => $nid, ":data" => "signup"))->fetchAll();
$dbresult = $dbresult[0]->count;
// Check if course has became full meanwhile user has been filling the form.
if ($dbresult >= $participants && isset($participants) && $participants > 0 && $form['submitted']['event_action']['#webform_component']['value'] == "signup") {
// Prevent submission and give error notice to user stating course has became full.
unset($_SESSION['messages']);
$form['submitted']['event_action']['#webform_component']['value'] = "queue|Queue";
form_set_error('full', t('Unfortunately event has became full. You can\'t signup at this moment. However you can queue for the event.'));
}
}
/**
* Implements hook_node_insert().
*/
function webform_event_node_insert($node) {
// Here we will force signup action field (and possibly other fields) that is default for all forms when they're created. When user adds component to the webform, select element for action to take is generated.
if($node->type == "event") {
// Include webform component functions. If we don't do this, we can't use webform functions needed to create element.
module_load_include('inc', 'webform', 'includes/webform.components');
// Check if component exists by doing database query and checking for cid = 99 on this node. TODO: This check is maybe too failsafe. Cid can propably be set to 1
$dbresultwf = db_select('webform_component', 'comps')
->fields('comps')
->condition('cid', 99, '=')
->condition('nid', $node->nid, '=')
->execute()
->fetchAll();
// It doesn't exists, create it.
if(!$dbresultwf) {
$component = array(
// We set cid to 99 so it wont interfere with other components (This might need to be increased further for very complex forms. Does webform check for existing cid? If so we are fine. Check previous TODO.
'cid' => 99,
'pid' => 0,
'nid' => (int)$node->nid,
// Set machine name and display name
'form_key' => 'event_action',
'name' => t('Select an action'),
// Type we need is a select options
'type' => 'select',
'extra' => array(
'description' => 'Select an action to perform',
'items' => 'signup|Signup' . "\n" . 'queue|Queue' . "\n" . 'cancel|Cancel',
),
// This needs to be mandatory as it will be used for other functionality.
'mandatory' => 1,
'weight' => -99,
'page_num' => 1,
);
// Insert component to webform
webform_component_insert($component);
}
}
}
/**
* Implements hook_webform_submission_delete().
*/
function webform_event_webform_submission_delete($node, $submission) {
// TODO: DO CONTENT TYPE CHECK!
// Get sid of the first in the queue
// If there is someone in the queue and there is free space, let event moderator know about it.
$queueFirstSid = db_select('webform_submitted_data', 'queue')
->fields('queue', array('sid'))
->condition('data', 'queue', '=')
->condition('nid', $node->nid, '=')
->execute()
->fetchAll();
$firstQueued = array();
// There is someone in queue
if (!empty($queueFirstSid)) {
$firstQueued = min($queueFirstSid);
$firstQueued = $firstQueued->sid;
unset($_SESSION['messages']);
drupal_set_message('You have a person waiting in queue. You might want to update him/her to signed up status <a href="submission/'.$firstQueued.'/edit?destination=node/'.$node->nid.'/manage-event"">here</a>');
}
}
/**
* Implements hook_menu().
*/
function webform_event_menu() {
$items = array();
// Mass emailing page
$items['node/%node/manage-event'] = array(
'title' => 'Manage event',
// Function to build form
'page callback' => 'drupal_get_form',
// Attachs arguments to drupal_get_form (node/id/something == 1/2/3 as arguments)
'page arguments' => array('webform_event_form', 1),
//'access callback' => 'webform_mass_email_form_access',
//'access arguments' => array(1),
'access arguments' => array('administer events'),
// File that contains requested page
'file' => 'includes/webform_event.pages.inc',
'weight' => 20,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_webform_component_insert().
*/
function webform_event_webform_component_insert($component) {
// Get all
$submissions = db_select('webform_submitted_data', 'data')
->fields('data', array('sid'))
->groupBy('data.sid')
->execute()
->fetchAll();
foreach($submissions as $submission) {
db_insert('webform_submitted_data')
->fields(array(
'nid' => $component['nid'],
'sid' => $submission->sid,
'cid' => $component['cid'],
'no' => 0,
'data' => '',
))
->execute();
}
}