1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* This file includes helper functions for creating reports for webform.module
|
6
|
*
|
7
|
* @author Nathan Haug <nate@lullabot.com>
|
8
|
*/
|
9
|
|
10
|
// All functions within this file need the webform.submissions.inc.
|
11
|
module_load_include('inc', 'webform', 'includes/webform.submissions');
|
12
|
|
13
|
/**
|
14
|
* Retrieve lists of submissions for a given webform.
|
15
|
*/
|
16
|
function webform_results_submissions($node, $user_filter, $pager_count) {
|
17
|
global $user;
|
18
|
|
19
|
if (isset($_GET['results']) && is_numeric($_GET['results'])) {
|
20
|
$pager_count = $_GET['results'];
|
21
|
}
|
22
|
|
23
|
$header = theme('webform_results_submissions_header', array('node' => $node));
|
24
|
if ($user_filter) {
|
25
|
if ($user->uid) {
|
26
|
drupal_set_title(t('Submissions for %user', array('%user' => $user->name)), PASS_THROUGH);
|
27
|
}
|
28
|
else {
|
29
|
drupal_set_title(t('Your submissions'));
|
30
|
webform_disable_page_cache();
|
31
|
}
|
32
|
webform_set_breadcrumb($node);
|
33
|
$submissions = webform_get_submissions(array('nid' => $node->nid, 'uid' => $user->uid), $header, $pager_count);
|
34
|
$count = webform_get_submission_count($node->nid, $user->uid);
|
35
|
}
|
36
|
else {
|
37
|
$submissions = webform_get_submissions($node->nid, $header, $pager_count);
|
38
|
$count = webform_get_submission_count($node->nid);
|
39
|
}
|
40
|
|
41
|
$operation_column = end($header);
|
42
|
$operation_total = $operation_column['colspan'];
|
43
|
|
44
|
$rows = array();
|
45
|
foreach ($submissions as $sid => $submission) {
|
46
|
$row = array(
|
47
|
$submission->is_draft ? t('@sid (draft)', array('@sid' => $sid)) : $sid,
|
48
|
format_date($submission->submitted, 'short'),
|
49
|
);
|
50
|
if (webform_results_access($node, $user)) {
|
51
|
$row[] = theme('username', array('account' => $submission));
|
52
|
$row[] = $submission->remote_addr;
|
53
|
}
|
54
|
$row[] = l(t('View'), "node/$node->nid/submission/$sid");
|
55
|
$operation_count = 1;
|
56
|
// No need to call this multiple times, just reference this in a variable.
|
57
|
$destination = drupal_get_destination();
|
58
|
if (webform_submission_access($node, $submission, 'edit', $user)) {
|
59
|
$row[] = l(t('Edit'), "node/$node->nid/submission/$sid/edit", array('query' => $destination));
|
60
|
$operation_count++;
|
61
|
}
|
62
|
if (webform_submission_access($node, $submission, 'delete', $user)) {
|
63
|
$row[] = l(t('Delete'), "node/$node->nid/submission/$sid/delete", array('query' => $destination));
|
64
|
$operation_count++;
|
65
|
}
|
66
|
if ($operation_count < $operation_total) {
|
67
|
$row[count($row) - 1] = array('data' => $row[count($row) - 1], 'colspan' => $operation_total - $operation_count + 1);
|
68
|
}
|
69
|
$rows[] = $row;
|
70
|
}
|
71
|
|
72
|
$element['#theme'] = 'webform_results_submissions';
|
73
|
$element['#node'] = $node;
|
74
|
$element['#submissions'] = $submissions;
|
75
|
$element['#total_count'] = $count;
|
76
|
$element['#pager_count'] = $pager_count;
|
77
|
$element['#attached']['library'][] = array('webform', 'admin');
|
78
|
|
79
|
$element['table']['#theme'] = 'table';
|
80
|
$element['table']['#header'] = $header;
|
81
|
$element['table']['#rows'] = $rows;
|
82
|
$element['table']['#operation_total'] = $operation_total;
|
83
|
|
84
|
return drupal_render($element);
|
85
|
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* Theme the list of links for selecting the number of results per page.
|
90
|
*
|
91
|
* @param $total_count
|
92
|
* The total number of results available.
|
93
|
* @param $pager_count
|
94
|
* The current number of results displayed per page.
|
95
|
*/
|
96
|
function theme_webform_results_per_page($variables) {
|
97
|
$total_count = $variables['total_count'];
|
98
|
$pager_count = $variables['pager_count'];
|
99
|
$output = '';
|
100
|
|
101
|
// Create a list of results-per-page options.
|
102
|
$counts = array(
|
103
|
'20' => '20',
|
104
|
'50' => '50',
|
105
|
'100' => '100',
|
106
|
'200' => '200',
|
107
|
'500' => '500',
|
108
|
'1000' => '1000',
|
109
|
'0' => t('All'),
|
110
|
);
|
111
|
|
112
|
$count_links = array();
|
113
|
|
114
|
foreach ($counts as $number => $text) {
|
115
|
if ($number < $total_count) {
|
116
|
$count_links[] = l($text, $_GET['q'], array('query' => array('results' => $number), 'attributes' => array('class' => array($pager_count == $number ? 'selected' : ''))));
|
117
|
}
|
118
|
}
|
119
|
|
120
|
$output .= '<div class="webform-results-per-page">';
|
121
|
if (count($count_links) > 1) {
|
122
|
$output .= t('Show !count results per page.', array('!count' => implode(' | ', $count_links)));
|
123
|
}
|
124
|
else {
|
125
|
$output .= t('Showing all results.');
|
126
|
}
|
127
|
if ($total_count > 1) {
|
128
|
$output .= ' ' . t('@total results total.', array('@total' => $total_count));
|
129
|
}
|
130
|
$output .= '</div>';
|
131
|
|
132
|
return $output;
|
133
|
}
|
134
|
|
135
|
/**
|
136
|
* Theme the header of the submissions table.
|
137
|
*
|
138
|
* This is done in it's own function so that webform can retrieve the header and
|
139
|
* use it for sorting the results.
|
140
|
*/
|
141
|
function theme_webform_results_submissions_header($variables) {
|
142
|
$node = $variables['node'];
|
143
|
|
144
|
$columns = array(
|
145
|
array('data' => t('#'), 'field' => 'sid', 'sort' => 'desc'),
|
146
|
array('data' => t('Submitted'), 'field' => 'submitted'),
|
147
|
);
|
148
|
if (webform_results_access($node)) {
|
149
|
$columns[] = array('data' => t('User'), 'field' => 'name');
|
150
|
$columns[] = array('data' => t('IP Address'), 'field' => 'remote_addr');
|
151
|
}
|
152
|
$columns[] = array('data' => t('Operations'), 'colspan' => module_exists('print') ? 5 : 3);
|
153
|
|
154
|
return $columns;
|
155
|
}
|
156
|
|
157
|
/**
|
158
|
* Preprocess function for webform-results-submissions.tpl.php
|
159
|
*/
|
160
|
function template_preprocess_webform_results_submissions(&$vars) {
|
161
|
$vars['node'] = $vars['element']['#node'];
|
162
|
$vars['submissions'] = $vars['element']['#submissions'];
|
163
|
$vars['table'] = $vars['element']['table'];
|
164
|
$vars['total_count'] = $vars['element']['#total_count'];
|
165
|
$vars['pager_count'] = $vars['element']['#pager_count'];
|
166
|
$vars['is_submissions'] = (arg(2) == 'submissions')? 1 : 0;
|
167
|
|
168
|
unset($vars['element']);
|
169
|
}
|
170
|
|
171
|
/**
|
172
|
* Create a table containing all submitted values for a webform node.
|
173
|
*/
|
174
|
function webform_results_table($node, $pager_count = 0) {
|
175
|
if (isset($_GET['results']) && is_numeric($_GET['results'])) {
|
176
|
$pager_count = $_GET['results'];
|
177
|
}
|
178
|
|
179
|
// Get all the submissions for the node.
|
180
|
$header = theme('webform_results_table_header', array('node' => $node));
|
181
|
$submissions = webform_get_submissions($node->nid, $header, $pager_count);
|
182
|
$total_count = webform_get_submission_count($node->nid);
|
183
|
|
184
|
$output = theme('webform_results_table', array('node' => $node, 'components' => $node->webform['components'], 'submissions' => $submissions, 'total_count' => $total_count, 'pager_count' => $pager_count));
|
185
|
if ($pager_count) {
|
186
|
$output .= theme('pager');
|
187
|
}
|
188
|
return $output;
|
189
|
}
|
190
|
|
191
|
function theme_webform_results_table_header($variables) {
|
192
|
return array(
|
193
|
array('data' => t('#'), 'field' => 'sid', 'sort' => 'desc'),
|
194
|
array('data' => t('Submitted'), 'field' => 'submitted'),
|
195
|
array('data' => t('User'), 'field' => 'name'),
|
196
|
array('data' => t('IP Address'), 'field' => 'remote_addr'),
|
197
|
);
|
198
|
}
|
199
|
|
200
|
/**
|
201
|
* Theme the results table displaying all the submissions for a particular node.
|
202
|
*
|
203
|
* @param $node
|
204
|
* The node whose results are being displayed.
|
205
|
* @param $components
|
206
|
* An associative array of the components for this webform.
|
207
|
* @param $submissions
|
208
|
* An array of all submissions for this webform.
|
209
|
* @param $total_count
|
210
|
* The total number of submissions to this webform.
|
211
|
* @param $pager_count
|
212
|
* The number of results to be shown per page.
|
213
|
*/
|
214
|
function theme_webform_results_table($variables) {
|
215
|
drupal_add_library('webform', 'admin');
|
216
|
|
217
|
$node = $variables['node'];
|
218
|
$components = $variables['components'];
|
219
|
$submissions = $variables['submissions'];
|
220
|
$total_count = $variables['total_count'];
|
221
|
$pager_count = $variables['pager_count'];
|
222
|
|
223
|
$header = array();
|
224
|
$rows = array();
|
225
|
$cell = array();
|
226
|
|
227
|
// This header has to be generated separately so we can add the SQL necessary.
|
228
|
// to sort the results.
|
229
|
$header = theme('webform_results_table_header', array('node' => $node));
|
230
|
|
231
|
// Generate a row for each submission.
|
232
|
foreach ($submissions as $sid => $submission) {
|
233
|
$cell[] = l($sid, 'node/' . $node->nid . '/submission/' . $sid);
|
234
|
$cell[] = format_date($submission->submitted, 'short');
|
235
|
$cell[] = theme('username', array('account' => $submission));
|
236
|
$cell[] = $submission->remote_addr;
|
237
|
$component_headers = array();
|
238
|
|
239
|
// Generate a cell for each component.
|
240
|
foreach ($node->webform['components'] as $component) {
|
241
|
$data = isset($submission->data[$component['cid']]['value']) ? $submission->data[$component['cid']]['value'] : NULL;
|
242
|
$submission_output = webform_component_invoke($component['type'], 'table', $component, $data);
|
243
|
if ($submission_output !== NULL) {
|
244
|
$component_headers[] = check_plain($component['name']);
|
245
|
$cell[] = $submission_output;
|
246
|
}
|
247
|
}
|
248
|
|
249
|
$rows[] = $cell;
|
250
|
unset($cell);
|
251
|
}
|
252
|
if (!empty($component_headers)) {
|
253
|
$header = array_merge($header, $component_headers);
|
254
|
}
|
255
|
|
256
|
if (count($rows) == 0) {
|
257
|
$rows[] = array(array('data' => t('There are no submissions for this form. <a href="!url">View this form</a>.', array('!url' => url('node/' . $node->nid))), 'colspan' => 4));
|
258
|
}
|
259
|
|
260
|
|
261
|
$output = '';
|
262
|
$output .= theme('webform_results_per_page', array('total_count' => $total_count, 'pager_count' => $pager_count));
|
263
|
$output .= theme('table', array('header' => $header, 'rows' => $rows));
|
264
|
return $output;
|
265
|
}
|
266
|
|
267
|
/**
|
268
|
* Delete all submissions for a node.
|
269
|
*
|
270
|
* @param $nid
|
271
|
* The node id whose submissions will be deleted.
|
272
|
*/
|
273
|
function webform_results_clear($nid) {
|
274
|
$node = node_load($nid);
|
275
|
$submissions = webform_get_submissions($nid);
|
276
|
foreach ($submissions as $submission) {
|
277
|
webform_submission_delete($node, $submission);
|
278
|
}
|
279
|
}
|
280
|
|
281
|
/**
|
282
|
* Confirmation form to delete all submissions for a node.
|
283
|
*
|
284
|
* @param $nid
|
285
|
* ID of node for which to clear submissions.
|
286
|
*/
|
287
|
function webform_results_clear_form($form, $form_state, $node) {
|
288
|
drupal_set_title(t('Clear Form Submissions'));
|
289
|
|
290
|
$form = array();
|
291
|
$form['nid'] = array('#type' => 'value', '#value' => $node->nid);
|
292
|
$question = t('Are you sure you want to delete all submissions for this form?');
|
293
|
|
294
|
return confirm_form($form, $question, 'node/' . $node->nid . '/webform-results', NULL, t('Clear'), t('Cancel'));
|
295
|
}
|
296
|
|
297
|
function webform_results_clear_form_submit($form, &$form_state) {
|
298
|
webform_results_clear($form_state['values']['nid']);
|
299
|
$node = node_load($form_state['values']['nid']);
|
300
|
$title = $node->title;
|
301
|
$message = t('Webform %title entries cleared.', array('%title' => $title));
|
302
|
drupal_set_message($message);
|
303
|
watchdog('webform', $message);
|
304
|
$form_state['redirect'] = 'node/' . $form_state['values']['nid'] . '/webform-results';
|
305
|
}
|
306
|
|
307
|
/**
|
308
|
* Form to configure the download of CSV files.
|
309
|
*/
|
310
|
function webform_results_download_form($form, &$form_state, $node) {
|
311
|
module_load_include('inc', 'webform', 'includes/webform.export');
|
312
|
module_load_include('inc', 'webform', 'includes/webform.components');
|
313
|
|
314
|
$form = array();
|
315
|
|
316
|
$form['node'] = array(
|
317
|
'#type' => 'value',
|
318
|
'#value' => $node,
|
319
|
);
|
320
|
|
321
|
$form['format'] = array(
|
322
|
'#type' => 'radios',
|
323
|
'#title' => t('Export format'),
|
324
|
'#options' => webform_export_list(),
|
325
|
'#default_value' => isset($form_state['values']['format']) ? $form_state['values']['format'] : variable_get('webform_export_format', 'delimited'),
|
326
|
);
|
327
|
|
328
|
$form['delimiter'] = array(
|
329
|
'#type' => 'select',
|
330
|
'#title' => t('Delimited text format'),
|
331
|
'#description' => t('This is the delimiter used in the CSV/TSV file when downloading Webform results. Using tabs in the export is the most reliable method for preserving non-latin characters. You may want to change this to another character depending on the program with which you anticipate importing results.'),
|
332
|
'#default_value' => isset($form_state['values']['delimiter']) ? $form_state['values']['delimiter'] : variable_get('webform_csv_delimiter', '\t'),
|
333
|
'#options' => array(
|
334
|
',' => t('Comma (,)'),
|
335
|
'\t' => t('Tab (\t)'),
|
336
|
';' => t('Semicolon (;)'),
|
337
|
':' => t('Colon (:)'),
|
338
|
'|' => t('Pipe (|)'),
|
339
|
'.' => t('Period (.)'),
|
340
|
' ' => t('Space ( )'),
|
341
|
),
|
342
|
);
|
343
|
|
344
|
$form['select_options'] = array(
|
345
|
'#type' => 'fieldset',
|
346
|
'#title' => t('Select list options'),
|
347
|
'#collapsible' => TRUE,
|
348
|
'#collapsed' => TRUE,
|
349
|
);
|
350
|
|
351
|
$form['select_options']['select_keys'] = array(
|
352
|
'#type' => 'radios',
|
353
|
'#title' => t('Select keys'),
|
354
|
'#options' => array(
|
355
|
0 => t('Full, human-readable options (values)'),
|
356
|
1 => t('Short, raw options (keys)'),
|
357
|
),
|
358
|
'#default_value' => isset($form_state['values']['select_options']['select_keys']) ? $form_state['values']['select_options']['select_keys'] : 0,
|
359
|
'#description' => t('Choose which part of options should be displayed from key|value pairs.'),
|
360
|
);
|
361
|
|
362
|
$form['select_options']['select_format'] = array(
|
363
|
'#type' => 'radios',
|
364
|
'#title' => t('Select list format'),
|
365
|
'#options' => array(
|
366
|
'separate' => t('Separate'),
|
367
|
'compact' => t('Compact'),
|
368
|
),
|
369
|
'#default_value' => isset($form_state['values']['select_options']['select_format']) ? $form_state['values']['select_options']['select_format'] : 'separate',
|
370
|
'#attributes' => array('class' => array('webform-select-list-format')),
|
371
|
'#theme' => 'webform_results_download_select_format',
|
372
|
);
|
373
|
|
374
|
$csv_components = array(
|
375
|
'info' => t('Submission information'),
|
376
|
'serial' => '-' . t('Submission Number'),
|
377
|
'sid' => '-' . t('Submission ID'),
|
378
|
'time' => '-' . t('Time'),
|
379
|
'draft' => '-' . t('Draft'),
|
380
|
'ip_address' => '-' . t('IP Address'),
|
381
|
'uid' => '-' . t('User ID'),
|
382
|
'username' => '-' . t('Username'),
|
383
|
);
|
384
|
$csv_components += webform_component_list($node, 'csv', TRUE);
|
385
|
|
386
|
$form['components'] = array(
|
387
|
'#type' => 'select',
|
388
|
'#title' => t('Included export components'),
|
389
|
'#options' => $csv_components,
|
390
|
'#default_value' => isset($form_state['values']['components']) ? $form_state['values']['components'] : array_keys($csv_components),
|
391
|
'#multiple' => TRUE,
|
392
|
'#size' => 10,
|
393
|
'#description' => t('The selected components will be included in the export.'),
|
394
|
'#process' => array('webform_component_select'),
|
395
|
);
|
396
|
|
397
|
$form['range'] = array(
|
398
|
'#type' => 'fieldset',
|
399
|
'#title' => t('Download range options'),
|
400
|
'#collapsible' => TRUE,
|
401
|
'#collapsed' => TRUE,
|
402
|
'#tree' => TRUE,
|
403
|
'#theme' => 'webform_results_download_range',
|
404
|
'#element_validate' => array('webform_results_download_range_validate'),
|
405
|
'#after_build' => array('webform_results_download_range_after_build'),
|
406
|
);
|
407
|
|
408
|
$form['range']['range_type'] = array(
|
409
|
'#type' => 'radios',
|
410
|
'#options' => array(
|
411
|
'all' => t('All submissions'),
|
412
|
'new' => t('Only new submissions since your last download'),
|
413
|
'latest' => t('Only the latest'),
|
414
|
'range' => t('All submissions starting from'),
|
415
|
),
|
416
|
'#default_value' => 'all',
|
417
|
);
|
418
|
$form['range']['latest'] = array(
|
419
|
'#type' => 'textfield',
|
420
|
'#size' => 5,
|
421
|
'#maxlength' => 8,
|
422
|
'#default_value' => isset($form_state['values']['latest']) ? $form_state['values']['latest'] : '',
|
423
|
);
|
424
|
$form['range']['start'] = array(
|
425
|
'#type' => 'textfield',
|
426
|
'#size' => 5,
|
427
|
'#maxlength' => 8,
|
428
|
'#default_value' => '',
|
429
|
);
|
430
|
$form['range']['end'] = array(
|
431
|
'#type' => 'textfield',
|
432
|
'#size' => 5,
|
433
|
'#maxlength' => 8,
|
434
|
'#default_value' => '',
|
435
|
'#description' => '',
|
436
|
);
|
437
|
|
438
|
// By default results are downloaded. User can override this value if
|
439
|
// programmatically submitting this form.
|
440
|
$form['download'] = array(
|
441
|
'#type' => 'value',
|
442
|
'#default_value' => TRUE
|
443
|
);
|
444
|
|
445
|
$form['actions'] = array('#type' => 'actions');
|
446
|
$form['actions']['submit'] = array(
|
447
|
'#type' => 'submit',
|
448
|
'#value' => t('Download'),
|
449
|
);
|
450
|
|
451
|
return $form;
|
452
|
}
|
453
|
|
454
|
/**
|
455
|
* FormAPI element validate function for the range fieldset.
|
456
|
*/
|
457
|
function webform_results_download_range_validate($element, $form_state) {
|
458
|
switch ($element['range_type']['#value']) {
|
459
|
case 'latest':
|
460
|
// Download latest x submissions.
|
461
|
if ($element['latest']['#value'] == '') {
|
462
|
form_error($element['latest'], t('Latest number of submissions field is required.'));
|
463
|
}
|
464
|
else{
|
465
|
if (!is_numeric($element['latest']['#value'])) {
|
466
|
form_error($element['latest'], t('Latest number of submissions must be numeric.'));
|
467
|
}
|
468
|
else{
|
469
|
if ($element['latest']['#value'] <= 0) {
|
470
|
form_error($element['latest'], t('Latest number of submissions must be greater than 0.'));
|
471
|
}
|
472
|
}
|
473
|
}
|
474
|
break;
|
475
|
case 'range':
|
476
|
// Download Start-End range of submissions.
|
477
|
// Start submission number.
|
478
|
if ($element['start']['#value'] == '') {
|
479
|
form_error($element['start'], t('Start submission number is required.'));
|
480
|
}
|
481
|
else{
|
482
|
if (!is_numeric($element['start']['#value'])) {
|
483
|
form_error($element['start'], t('Start submission number must be numeric.'));
|
484
|
}
|
485
|
else{
|
486
|
if ($element['start']['#value'] <= 0) {
|
487
|
form_error($element['start'], t('Start submission number must be greater than 0.'));
|
488
|
}
|
489
|
}
|
490
|
}
|
491
|
// End submission number.
|
492
|
if ($element['end']['#value'] != '') {
|
493
|
if (!is_numeric($element['end']['#value'])) {
|
494
|
form_error($element['end'], t('End submission number must be numeric.'));
|
495
|
}
|
496
|
else{
|
497
|
if ($element['end']['#value'] <= 0) {
|
498
|
form_error($element['end'], t('End submission number must be greater than 0.'));
|
499
|
}
|
500
|
else{
|
501
|
if ($element['end']['#value'] < $element['start']['#value']) {
|
502
|
form_error($element['end'], t('End submission number may not be less than Start submission number.'));
|
503
|
}
|
504
|
}
|
505
|
}
|
506
|
}
|
507
|
break;
|
508
|
}
|
509
|
|
510
|
}
|
511
|
|
512
|
/**
|
513
|
* Validate handler for webform_results_download_form().
|
514
|
*/
|
515
|
function webform_results_download_form_submit(&$form, &$form_state) {
|
516
|
$options = array(
|
517
|
'delimiter' => $form_state['values']['delimiter'],
|
518
|
'components' => array_keys(array_filter($form_state['values']['components'])),
|
519
|
'select_keys' => $form_state['values']['select_keys'],
|
520
|
'select_format' => $form_state['values']['select_format'],
|
521
|
'range_type' => $form_state['values']['range']['range_type'],
|
522
|
'download' => $form_state['values']['download'],
|
523
|
);
|
524
|
|
525
|
// Retrieve the list of required SIDs.
|
526
|
if ($options['range_type'] != 'all') {
|
527
|
$options['sids'] = webform_download_sids($form_state['values']['node']->nid, $form_state['values']['range']);
|
528
|
}
|
529
|
|
530
|
$export_info = webform_results_export($form_state['values']['node'], $form_state['values']['format'], $options);
|
531
|
|
532
|
// If webform result file should be downloaded, send the file to the browser,
|
533
|
// otherwise save information about the created file in $form_state.
|
534
|
if ($options['download']) {
|
535
|
webform_results_download($form_state['values']['node'], $export_info);
|
536
|
}
|
537
|
else {
|
538
|
$form_state['export_info'] = $export_info;
|
539
|
}
|
540
|
}
|
541
|
|
542
|
/**
|
543
|
* FormAPI after build function for the download range fieldset.
|
544
|
*/
|
545
|
function webform_results_download_range_after_build($element, &$form_state) {
|
546
|
$node = $form_state['values']['node'];
|
547
|
|
548
|
// Build a list of counts of new and total submissions.
|
549
|
$count = webform_get_submission_count($node->nid);
|
550
|
$sids = webform_download_sids($node->nid, array('range_type' => 'new'));
|
551
|
|
552
|
$last_download = webform_download_last_download_info($node->nid);
|
553
|
|
554
|
$element['#webform_download_info']['sid'] = $last_download ? $last_download['sid'] : 0;
|
555
|
$element['#webform_download_info']['requested'] = $last_download ? $last_download['requested'] : $node->created;
|
556
|
$element['#webform_download_info']['total'] = $count;
|
557
|
$element['#webform_download_info']['new'] = count($sids);
|
558
|
|
559
|
return $element;
|
560
|
}
|
561
|
|
562
|
/**
|
563
|
* Theme the output of the export range fieldset.
|
564
|
*/
|
565
|
function theme_webform_results_download_range($variables) {
|
566
|
drupal_add_library('webform', 'admin');
|
567
|
|
568
|
$element = $variables['element'];
|
569
|
$download_info = $element['#webform_download_info'];
|
570
|
|
571
|
// Set description for total of all submissions.
|
572
|
$element['range_type']['all']['#theme_wrappers'] = array('webform_inline_radio');
|
573
|
$element['range_type']['all']['#description'] = '(' . t('@count total', array('@count' => $download_info['total'])) . ')';
|
574
|
|
575
|
// Set description for "New submissions since last download".
|
576
|
$format = webform_date_format('short');
|
577
|
$requested_date = format_date($download_info['requested'], 'custom', $format);
|
578
|
$element['range_type']['new']['#theme_wrappers'] = array('webform_inline_radio');
|
579
|
$element['range_type']['new']['#description'] = '(' . t('@count new since @date', array('@count' => $download_info['new'], '@date' => $requested_date)) . ')';
|
580
|
|
581
|
|
582
|
// Disable option if there are no new submissions.
|
583
|
if ($download_info['new'] == 0) {
|
584
|
$element['range_type']['new']['#attributes']['disabled'] = 'disabled';
|
585
|
}
|
586
|
|
587
|
// Render latest x submissions option.
|
588
|
$element['latest']['#attributes']['class'][] = 'webform-set-active';
|
589
|
$element['range_type']['latest']['#theme_wrappers'] = array('webform_inline_radio');
|
590
|
$element['range_type']['latest']['#inline_element'] = t('Only the latest !number submissions', array('!number' => drupal_render($element['latest'])));
|
591
|
$element['range_type']['latest']['#title'] = NULL;
|
592
|
|
593
|
// Render Start-End submissions option.
|
594
|
$element['start']['#attributes']['class'][] = 'webform-set-active';
|
595
|
$element['end']['#attributes']['class'][] = 'webform-set-active';
|
596
|
$element['range_type']['range']['#theme_wrappers'] = array('webform_inline_radio');
|
597
|
$element['range_type']['range']['#inline_element'] = t('All submissions starting from: !start and optionally to: !end', array('!start' => drupal_render($element['start']), '!end' => drupal_render($element['end'])));
|
598
|
$element['range_type']['range']['#title'] = NULL;
|
599
|
|
600
|
$last_sid = $download_info['sid'] ? $download_info['sid'] : drupal_placeholder(t('none'));
|
601
|
$element['range_type']['range']['#description'] = '(' . t('Use submission IDs for the range. Last downloaded end SID: !sid.', array('!sid' => $last_sid)) . ')';
|
602
|
|
603
|
return drupal_render_children($element);
|
604
|
}
|
605
|
|
606
|
/**
|
607
|
* Theme the output of the select list format radio buttons.
|
608
|
*/
|
609
|
function theme_webform_results_download_select_format($variables) {
|
610
|
drupal_add_library('webform', 'admin');
|
611
|
|
612
|
$element = $variables['element'];
|
613
|
$output = '';
|
614
|
|
615
|
// Build an example table for the separate option.
|
616
|
$header = array(t('Option A'), t('Option B'), t('Option C'));
|
617
|
$rows = array(
|
618
|
array('X', '', ''),
|
619
|
array('X', '', 'X'),
|
620
|
array('', 'X', 'X'),
|
621
|
);
|
622
|
|
623
|
$element['separate']['#attributes']['class'] = array();
|
624
|
$element['separate']['#description'] = theme('table', array('header' => $header, 'rows' => $rows));
|
625
|
$element['separate']['#description'] .= t('Separate options are more suitable for building reports, graphs, and statistics in a spreadsheet application.');
|
626
|
$output .= drupal_render($element['separate']);
|
627
|
|
628
|
// Build an example table for the compact option.
|
629
|
$header = array(t('My select list'));
|
630
|
$rows = array(
|
631
|
array('Option A'),
|
632
|
array('Option A,Option C'),
|
633
|
array('Option B,Option C'),
|
634
|
);
|
635
|
|
636
|
$element['compact']['#attributes']['class'] = array();
|
637
|
$element['compact']['#description'] = theme('table', array('header' => $header, 'rows' => $rows));
|
638
|
$element['compact']['#description'] .= t('Compact options are more suitable for importing data into other systems.');
|
639
|
$output .= drupal_render($element['compact']);
|
640
|
|
641
|
return $output;
|
642
|
}
|
643
|
|
644
|
/**
|
645
|
* Generate a Excel-readable CSV file containing all submissions for a Webform.
|
646
|
*
|
647
|
* The CSV requires that the data be presented in a flat file. In order
|
648
|
* to maximize usability to the Excel community and minimize subsequent
|
649
|
* stats or spreadsheet programming this program extracts data from the
|
650
|
* various records for a given session and presents them as a single file
|
651
|
* where each row represents a single record.
|
652
|
* The structure of the file is:
|
653
|
* Heading Line 1: Gives group overviews padded by empty cells to the
|
654
|
* next group. A group may be a question and corresponds
|
655
|
* to a component in the webform philosophy. Each group
|
656
|
* overview will have a fixed number of columns beneath it.
|
657
|
* Heading line 2: gives column headings
|
658
|
* Data line 1 .....
|
659
|
* Data line 2 .....
|
660
|
*
|
661
|
* An example of this format is given below. Note the columns have had spaces
|
662
|
* added so the columns line up. This is not the case with actual file where
|
663
|
* a column may be null. Note also, that multiple choice questions as produced
|
664
|
* by checkboxes or radio buttons have been presented as "yes" or "no" and the
|
665
|
* actual choice text is retained only in the header line 2.
|
666
|
* Data from text boxes and input fields are written out in the body of the table.
|
667
|
*
|
668
|
* Submission Details, , , ,Question 1, , ,.., ,Question 2, , ,.., ,Question n
|
669
|
* timestamp ,time,SID,userid,Choice 1 ,Choice 2,Choice 3,..,Choice n,Choice 1 ,Choice 2,Choice 3,..,Choice n,Comment
|
670
|
* 21 Feb 2005 ,1835,23 ,34 ,X , , ,.., ,X ,X ,X ,..,X ,My comment
|
671
|
* 23 Feb 2005 ,1125,24 ,89 ,X ,X , ,.., ,X ,X ,X ,..,X ,Hello
|
672
|
* .................................................................................................................................
|
673
|
* 27 Feb 2005 ,1035,56 ,212 ,X , , ,.., ,X ,X ,X ,..,X ,How is this?
|
674
|
*
|
675
|
*/
|
676
|
function webform_results_export($node, $format = 'delimited', $options = array()) {
|
677
|
global $user;
|
678
|
module_load_include('inc', 'webform', 'includes/webform.export');
|
679
|
module_load_include('inc', 'webform', 'includes/webform.components');
|
680
|
|
681
|
$submission_information = array(
|
682
|
'serial' => t('Serial'),
|
683
|
'sid' => t('SID'),
|
684
|
'time' => t('Time'),
|
685
|
'draft' => t('Draft'),
|
686
|
'ip_address' => t('IP Address'),
|
687
|
'uid' => t('UID'),
|
688
|
'username' => t('Username'),
|
689
|
);
|
690
|
|
691
|
if (empty($options)) {
|
692
|
$options = array(
|
693
|
'delimiter' => variable_get('webform_csv_delimiter', '\t'),
|
694
|
'components' => array_merge(array_keys($submission_information), array_keys(webform_component_list($node, 'csv', TRUE))),
|
695
|
'select_keys' => 0,
|
696
|
'select_format' => 'separate',
|
697
|
'range_type' => 'all',
|
698
|
);
|
699
|
}
|
700
|
else {
|
701
|
foreach ($submission_information as $key => $label) {
|
702
|
if (!in_array($key, $options['components'])) {
|
703
|
unset($submission_information[$key]);
|
704
|
}
|
705
|
}
|
706
|
}
|
707
|
|
708
|
// Open a new Webform exporter object.
|
709
|
$exporter = webform_export_create_handler($format, $options);
|
710
|
|
711
|
$file_name = drupal_tempnam('temporary://', 'webform_');
|
712
|
$handle = @fopen($file_name, 'w'); // The @ suppresses errors.
|
713
|
$exporter->bof($handle);
|
714
|
|
715
|
// Fill in the header for the submission information (if any).
|
716
|
$header[2] = $header[1] = $header[0] = count($submission_information) ? array_fill(0, count($submission_information), '') : array();
|
717
|
if (count($submission_information)) {
|
718
|
$header[0][0] = $node->title;
|
719
|
$header[1][0] = t('Submission Details');
|
720
|
foreach (array_values($submission_information) as $column => $label) {
|
721
|
$header[2][$column] = $label;
|
722
|
}
|
723
|
}
|
724
|
|
725
|
// Compile header information for components.
|
726
|
foreach ($options['components'] as $cid) {
|
727
|
if (isset($node->webform['components'][$cid])) {
|
728
|
$component = $node->webform['components'][$cid];
|
729
|
|
730
|
// Let each component determine its headers.
|
731
|
if (webform_component_feature($component['type'], 'csv')) {
|
732
|
$component_header = (array) webform_component_invoke($component['type'], 'csv_headers', $component, $options);
|
733
|
// Allow modules to modify the component CSV header.
|
734
|
drupal_alter('webform_csv_header', $component_header, $component);
|
735
|
|
736
|
// Merge component CSV header to overall CSV header
|
737
|
$header[0] = array_merge($header[0], (array) $component_header[0]);
|
738
|
$header[1] = array_merge($header[1], (array) $component_header[1]);
|
739
|
$header[2] = array_merge($header[2], (array) $component_header[2]);
|
740
|
}
|
741
|
}
|
742
|
}
|
743
|
|
744
|
// Add headers to the file.
|
745
|
foreach ($header as $row) {
|
746
|
$exporter->add_row($handle, $row);
|
747
|
}
|
748
|
|
749
|
// Get all the required submissions for the download.
|
750
|
$filters['nid'] = $node->nid;
|
751
|
if (!empty($options['sids'])){
|
752
|
$filters['sid'] = $options['sids'];
|
753
|
}
|
754
|
$submissions = webform_get_submissions($filters);
|
755
|
|
756
|
// Generate a row for each submission.
|
757
|
$row_count = 0;
|
758
|
$sid = 0;
|
759
|
foreach ($submissions as $sid => $submission) {
|
760
|
$row_count++;
|
761
|
|
762
|
$row = array();
|
763
|
if (isset($submission_information['serial'])) {
|
764
|
$row[] = $row_count;
|
765
|
}
|
766
|
if (isset($submission_information['sid'])) {
|
767
|
$row[] = $sid;
|
768
|
}
|
769
|
if (isset($submission_information['time'])) {
|
770
|
$row[] = format_date($submission->submitted, 'short');
|
771
|
}
|
772
|
if (isset($submission_information['draft'])) {
|
773
|
$row[] = $submission->is_draft;
|
774
|
}
|
775
|
if (isset($submission_information['ip_address'])) {
|
776
|
$row[] = $submission->remote_addr;
|
777
|
}
|
778
|
if (isset($submission_information['uid'])) {
|
779
|
$row[] = $submission->uid;
|
780
|
}
|
781
|
if (isset($submission_information['username'])) {
|
782
|
$row[] = $submission->name;
|
783
|
}
|
784
|
|
785
|
foreach ($options['components'] as $cid) {
|
786
|
if (isset($node->webform['components'][$cid])) {
|
787
|
$component = $node->webform['components'][$cid];
|
788
|
// Let each component add its data.
|
789
|
$raw_data = isset($submission->data[$cid]['value']) ? $submission->data[$cid]['value'] : NULL;
|
790
|
if (webform_component_feature($component['type'], 'csv')) {
|
791
|
$data = webform_component_invoke($component['type'], 'csv_data', $component, $options, $raw_data);
|
792
|
|
793
|
// Allow modules to modify the CSV data.
|
794
|
drupal_alter('webform_csv_data', $data, $component, $submission);
|
795
|
|
796
|
if (is_array($data)) {
|
797
|
$row = array_merge($row, array_values($data));
|
798
|
}
|
799
|
else {
|
800
|
$row[] = isset($data) ? $data : '';
|
801
|
}
|
802
|
}
|
803
|
}
|
804
|
}
|
805
|
|
806
|
// Write data from submissions.
|
807
|
$data = $exporter->add_row($handle, $row);
|
808
|
}
|
809
|
|
810
|
// Add the closing bytes.
|
811
|
$exporter->eof($handle);
|
812
|
|
813
|
// Close the file.
|
814
|
@fclose($handle);
|
815
|
|
816
|
$export_info['options'] = $options;
|
817
|
$export_info['file_name'] = $file_name;
|
818
|
$export_info['exporter'] = $exporter;
|
819
|
$export_info['row_count'] = $row_count;
|
820
|
$export_info['last_sid'] = $sid;
|
821
|
|
822
|
return $export_info;
|
823
|
}
|
824
|
|
825
|
/**
|
826
|
* Send a generated webform results file to the user's browser.
|
827
|
*
|
828
|
* @param $node
|
829
|
* The webform node.
|
830
|
* @param $export_info
|
831
|
* Export information array retrieved from webform_results_export().
|
832
|
*/
|
833
|
function webform_results_download($node, $export_info) {
|
834
|
global $user;
|
835
|
|
836
|
// $exporter, $file_name, $row_count
|
837
|
$export_name = _webform_safe_name($node->title);
|
838
|
$export_info['exporter']->set_headers($export_name);
|
839
|
@readfile($export_info['file_name']); // The @ makes it silent.
|
840
|
@unlink($export_info['file_name']); // Clean up, the @ makes it silent.
|
841
|
|
842
|
// Update user last downloaded sid if required.
|
843
|
if ($export_info['options']['range_type'] != 'range' && $export_info['row_count'] > 0) {
|
844
|
// Delete existing record.
|
845
|
db_delete('webform_last_download')
|
846
|
->condition('nid', $node->nid)
|
847
|
->condition('uid', $user->uid)
|
848
|
->execute();
|
849
|
// Write new record.
|
850
|
db_insert('webform_last_download')
|
851
|
->fields(array(
|
852
|
'nid' => $node->nid,
|
853
|
'uid' => $user->uid,
|
854
|
'sid' => $export_info['last_sid'],
|
855
|
'requested' => REQUEST_TIME,
|
856
|
))
|
857
|
->execute();
|
858
|
}
|
859
|
|
860
|
exit();
|
861
|
}
|
862
|
|
863
|
/**
|
864
|
* Provides a simple analysis of all submissions to a webform.
|
865
|
*
|
866
|
* @param $node
|
867
|
* The webform node on which to generate the analysis.
|
868
|
* @param $sids
|
869
|
* An array of submission IDs to which this analysis may be filtered. May be
|
870
|
* used to generate results that are per-user or other groups of submissions.
|
871
|
* @param $analysis_component
|
872
|
* A webform component. If passed in, additional information may be returned
|
873
|
* relating specifically to that component's analysis, such as a list of
|
874
|
* "Other" values within a select list.
|
875
|
*/
|
876
|
function webform_results_analysis($node, $sids = array(), $analysis_component = NULL) {
|
877
|
if (!is_array($sids)) {
|
878
|
$sids = array();
|
879
|
}
|
880
|
|
881
|
// If showing a component's details, we don't want to loose the menu tabs.
|
882
|
if ($analysis_component) {
|
883
|
$item = menu_get_item('node/' . $node->nid . '/webform-results/analysis');
|
884
|
menu_set_item(NULL, $item);
|
885
|
}
|
886
|
|
887
|
$components = isset($analysis_component) ? array($analysis_component['cid'] => $analysis_component) : $node->webform['components'];
|
888
|
$data = array();
|
889
|
foreach ($components as $cid => $component) {
|
890
|
// Do component specific call.
|
891
|
if ($row_data = webform_component_invoke($component['type'], 'analysis', $component, $sids, isset($analysis_component))) {
|
892
|
$data[$cid] = $row_data;
|
893
|
}
|
894
|
}
|
895
|
|
896
|
return theme('webform_results_analysis', array('node' => $node, 'data' => $data, 'sids' => $sids, 'component' => $analysis_component));
|
897
|
}
|
898
|
|
899
|
/**
|
900
|
* Output the content of the Analysis page.
|
901
|
*
|
902
|
* @see webform_results_analysis()
|
903
|
*/
|
904
|
function theme_webform_results_analysis($variables) {
|
905
|
$node = $variables['node'];
|
906
|
$data = $variables['data'];
|
907
|
$sids = $variables['sids'];
|
908
|
$analysis_component = $variables['component'];
|
909
|
|
910
|
$rows = array();
|
911
|
$question_number = 0;
|
912
|
$single = isset($analysis_component);
|
913
|
|
914
|
$header = array(
|
915
|
$single ? $analysis_component['name'] : t('Q'),
|
916
|
array('data' => $single ? ' ' : t('responses'), 'colspan' => '10')
|
917
|
);
|
918
|
|
919
|
foreach ($data as $cid => $row_data) {
|
920
|
$question_number++;
|
921
|
|
922
|
if (is_array($row_data)) {
|
923
|
$row = array();
|
924
|
if (!$single) {
|
925
|
$row['data'][] = array('data' => '<strong>' . $question_number . '</strong>', 'rowspan' => count($row_data) + 1, 'valign' => 'top');
|
926
|
$row['data'][] = array('data' => '<strong>' . check_plain($node->webform['components'][$cid]['name']) . '</strong>', 'colspan' => '10');
|
927
|
$row['class'][] = 'webform-results-question';
|
928
|
}
|
929
|
$rows = array_merge($rows, array_merge(array($row), $row_data));
|
930
|
}
|
931
|
}
|
932
|
|
933
|
if (count($rows) == 0) {
|
934
|
$rows[] = array(array('data' => t('There are no submissions for this form. <a href="!url">View this form</a>.', array('!url' => url('node/' . $node->nid))), 'colspan' => 20));
|
935
|
}
|
936
|
|
937
|
return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('class' => array('webform-results-analysis'))));
|
938
|
}
|
939
|
|
940
|
/**
|
941
|
* Given a set of range options, retrieve a set of SIDs for a webform node.
|
942
|
*/
|
943
|
function webform_download_sids($nid, $range_options, $uid = NULL) {
|
944
|
$query = db_select('webform_submissions', 'ws')
|
945
|
->fields('ws', array('sid'))
|
946
|
->condition('nid', $nid);
|
947
|
|
948
|
switch ($range_options['range_type']) {
|
949
|
case 'all':
|
950
|
// All Submissions.
|
951
|
$query->orderBy('sid', 'ASC');
|
952
|
break;
|
953
|
case 'new':
|
954
|
// All Since Last Download.
|
955
|
$download_info = webform_download_last_download_info($nid, $uid);
|
956
|
$last_sid = $download_info ? $download_info['sid'] : 0;
|
957
|
$query
|
958
|
->condition('sid', $last_sid, '>')
|
959
|
->orderBy('sid', 'ASC');
|
960
|
break;
|
961
|
case 'latest':
|
962
|
// Last x Submissions.
|
963
|
$query
|
964
|
->orderBy('sid', 'DESC')
|
965
|
->range(0, $range_options['latest']);
|
966
|
break;
|
967
|
case 'range':
|
968
|
// Submissions Start-End.
|
969
|
$query->condition('sid', $range_options['start'], '>=');
|
970
|
if ($range_options['end']){
|
971
|
$query->condition('sid', $range_options['end'], '<=');
|
972
|
}
|
973
|
$query->orderBy('sid', 'ASC');
|
974
|
break;
|
975
|
}
|
976
|
|
977
|
$sids = $query->execute()->fetchCol();
|
978
|
|
979
|
// The last x submissions option has SIDs that are in reverse order.
|
980
|
if ($range_options['range_type'] == 'latest') {
|
981
|
$sids = array_reverse($sids);
|
982
|
}
|
983
|
|
984
|
return $sids;
|
985
|
}
|
986
|
|
987
|
/**
|
988
|
* Get this user's last download information, including the SID and timestamp.
|
989
|
*
|
990
|
* This function provides an array of information about the last download that
|
991
|
* a user had for a particular Webform node. Currently it only returns an array
|
992
|
* with two keys:
|
993
|
* - sid: The last submission ID that was downloaded.
|
994
|
* - requested: The timestamp of the last download request.
|
995
|
*
|
996
|
* @param $nid
|
997
|
* The Webform NID.
|
998
|
* @param $uid
|
999
|
* The user account ID for which to retrieve download information.
|
1000
|
* @return
|
1001
|
* An array of download information or FALSE if this user has never downloaded
|
1002
|
* results for this particular node.
|
1003
|
*/
|
1004
|
function webform_download_last_download_info($nid, $uid = NULL) {
|
1005
|
$uid = isset($uid) ? $uid : $GLOBALS['user']->uid;
|
1006
|
|
1007
|
$info = db_select('webform_last_download', 'wld')
|
1008
|
->fields('wld')
|
1009
|
->condition('nid', $nid)
|
1010
|
->condition('uid', $uid)
|
1011
|
->execute()
|
1012
|
->fetchAssoc();
|
1013
|
|
1014
|
return $info;
|
1015
|
}
|