comparison sites/all/modules/webform/includes/webform.report.inc @ 0:ff03f76ab3fe

initial version
author danieleb <danielebarchiesi@me.com>
date Wed, 21 Aug 2013 18:51:11 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ff03f76ab3fe
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'] = array('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'] = array('webform-set-active');
595 $element['end']['#attributes']['class'] = array('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 $header[0] = array_merge($header[0], (array) $component_header[0]);
734 $header[1] = array_merge($header[1], (array) $component_header[1]);
735 $header[2] = array_merge($header[2], (array) $component_header[2]);
736 }
737 }
738 }
739
740 // Add headers to the file.
741 foreach ($header as $row) {
742 $exporter->add_row($handle, $row);
743 }
744
745 // Get all the required submissions for the download.
746 $filters['nid'] = $node->nid;
747 if (!empty($options['sids'])){
748 $filters['sid'] = $options['sids'];
749 }
750 $submissions = webform_get_submissions($filters);
751
752 // Generate a row for each submission.
753 $row_count = 0;
754 foreach ($submissions as $sid => $submission) {
755 $row_count++;
756
757 $row = array();
758 if (isset($submission_information['serial'])) {
759 $row[] = $row_count;
760 }
761 if (isset($submission_information['sid'])) {
762 $row[] = $sid;
763 }
764 if (isset($submission_information['time'])) {
765 $row[] = format_date($submission->submitted, 'short');
766 }
767 if (isset($submission_information['draft'])) {
768 $row[] = $submission->is_draft;
769 }
770 if (isset($submission_information['ip_address'])) {
771 $row[] = $submission->remote_addr;
772 }
773 if (isset($submission_information['uid'])) {
774 $row[] = $submission->uid;
775 }
776 if (isset($submission_information['username'])) {
777 $row[] = $submission->name;
778 }
779
780 foreach ($options['components'] as $cid) {
781 if (isset($node->webform['components'][$cid])) {
782 $component = $node->webform['components'][$cid];
783 // Let each component add its data.
784 $raw_data = isset($submission->data[$cid]['value']) ? $submission->data[$cid]['value'] : NULL;
785 if (webform_component_feature($component['type'], 'csv')) {
786 $data = webform_component_invoke($component['type'], 'csv_data', $component, $options, $raw_data);
787 if (is_array($data)) {
788 $row = array_merge($row, array_values($data));
789 }
790 else {
791 $row[] = isset($data) ? $data : '';
792 }
793 }
794 }
795 }
796
797 // Write data from submissions.
798 $data = $exporter->add_row($handle, $row);
799 }
800
801 // Add the closing bytes.
802 $exporter->eof($handle);
803
804 // Close the file.
805 @fclose($handle);
806
807 $export_info['options'] = $options;
808 $export_info['file_name'] = $file_name;
809 $export_info['exporter'] = $exporter;
810 $export_info['row_count'] = $row_count;
811 $export_info['last_sid'] = $sid;
812
813 return $export_info;
814 }
815
816 /**
817 * Send a generated webform results file to the user's browser.
818 *
819 * @param $node
820 * The webform node.
821 * @param $export_info
822 * Export information array retrieved from webform_results_export().
823 */
824 function webform_results_download($node, $export_info) {
825 global $user;
826
827 // $exporter, $file_name, $row_count
828 $export_name = _webform_safe_name($node->title);
829 $export_info['exporter']->set_headers($export_name);
830 @readfile($export_info['file_name']); // The @ makes it silent.
831 @unlink($export_info['file_name']); // Clean up, the @ makes it silent.
832
833 // Update user last downloaded sid if required.
834 if ($export_info['options']['range_type'] != 'range' && $export_info['row_count'] > 0) {
835 // Delete existing record.
836 db_delete('webform_last_download')
837 ->condition('nid', $node->nid)
838 ->condition('uid', $user->uid)
839 ->execute();
840 // Write new record.
841 db_insert('webform_last_download')
842 ->fields(array(
843 'nid' => $node->nid,
844 'uid' => $user->uid,
845 'sid' => $export_info['last_sid'],
846 'requested' => REQUEST_TIME,
847 ))
848 ->execute();
849 }
850
851 exit();
852 }
853
854 /**
855 * Provides a simple analysis of all submissions to a webform.
856 *
857 * @param $node
858 * The webform node on which to generate the analysis.
859 * @param $sids
860 * An array of submission IDs to which this analysis may be filtered. May be
861 * used to generate results that are per-user or other groups of submissions.
862 * @param $analysis_component
863 * A webform component. If passed in, additional information may be returned
864 * relating specifically to that component's analysis, such as a list of
865 * "Other" values within a select list.
866 */
867 function webform_results_analysis($node, $sids = array(), $analysis_component = NULL) {
868 if (!is_array($sids)) {
869 $sids = array();
870 }
871
872 // If showing a component's details, we don't want to loose the menu tabs.
873 if ($analysis_component) {
874 $item = menu_get_item('node/' . $node->nid . '/webform-results/analysis');
875 menu_set_item(NULL, $item);
876 }
877
878 $components = isset($analysis_component) ? array($analysis_component['cid'] => $analysis_component) : $node->webform['components'];
879 $data = array();
880 foreach ($components as $cid => $component) {
881 // Do component specific call.
882 if ($row_data = webform_component_invoke($component['type'], 'analysis', $component, $sids, isset($analysis_component))) {
883 $data[$cid] = $row_data;
884 }
885 }
886
887 return theme('webform_results_analysis', array('node' => $node, 'data' => $data, 'sids' => $sids, 'component' => $analysis_component));
888 }
889
890 /**
891 * Output the content of the Analysis page.
892 *
893 * @see webform_results_analysis()
894 */
895 function theme_webform_results_analysis($variables) {
896 $node = $variables['node'];
897 $data = $variables['data'];
898 $sids = $variables['sids'];
899 $analysis_component = $variables['component'];
900
901 $rows = array();
902 $question_number = 0;
903 $single = isset($analysis_component);
904
905 $header = array(
906 $single ? $analysis_component['name'] : t('Q'),
907 array('data' => $single ? '&nbsp;' : t('responses'), 'colspan' => '10')
908 );
909
910 foreach ($data as $cid => $row_data) {
911 $question_number++;
912
913 if (is_array($row_data)) {
914 $row = array();
915 if (!$single) {
916 $row['data'][] = array('data' => '<strong>' . $question_number . '</strong>', 'rowspan' => count($row_data) + 1, 'valign' => 'top');
917 $row['data'][] = array('data' => '<strong>' . check_plain($node->webform['components'][$cid]['name']) . '</strong>', 'colspan' => '10');
918 $row['class'][] = 'webform-results-question';
919 }
920 $rows = array_merge($rows, array_merge(array($row), $row_data));
921 }
922 }
923
924 if (count($rows) == 0) {
925 $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));
926 }
927
928 return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('class' => array('webform-results-analysis'))));
929 }
930
931 /**
932 * Given a set of range options, retrieve a set of SIDs for a webform node.
933 */
934 function webform_download_sids($nid, $range_options, $uid = NULL) {
935 $query = db_select('webform_submissions', 'ws')
936 ->fields('ws', array('sid'))
937 ->condition('nid', $nid);
938
939 switch ($range_options['range_type']) {
940 case 'all':
941 // All Submissions.
942 $query->orderBy('sid', 'ASC');
943 break;
944 case 'new':
945 // All Since Last Download.
946 $download_info = webform_download_last_download_info($nid, $uid);
947 $last_sid = $download_info ? $download_info['sid'] : 0;
948 $query
949 ->condition('sid', $last_sid, '>')
950 ->orderBy('sid', 'ASC');
951 break;
952 case 'latest':
953 // Last x Submissions.
954 $query
955 ->orderBy('sid', 'DESC')
956 ->range(0, $range_options['latest']);
957 break;
958 case 'range':
959 // Submissions Start-End.
960 $query->condition('sid', $range_options['start'], '>=');
961 if ($range_options['end']){
962 $query->condition('sid', $range_options['end'], '<=');
963 }
964 $query->orderBy('sid', 'ASC');
965 break;
966 }
967
968 $sids = $query->execute()->fetchCol();
969
970 // The last x submissions option has SIDs that are in reverse order.
971 if ($range_options['range_type'] == 'latest') {
972 $sids = array_reverse($sids);
973 }
974
975 return $sids;
976 }
977
978 /**
979 * Get this user's last download information, including the SID and timestamp.
980 *
981 * This function provides an array of information about the last download that
982 * a user had for a particular Webform node. Currently it only returns an array
983 * with two keys:
984 * - sid: The last submission ID that was downloaded.
985 * - requested: The timestamp of the last download request.
986 *
987 * @param $nid
988 * The Webform NID.
989 * @param $uid
990 * The user account ID for which to retrieve download information.
991 * @return
992 * An array of download information or FALSE if this user has never downloaded
993 * results for this particular node.
994 */
995 function webform_download_last_download_info($nid, $uid = NULL) {
996 $uid = isset($uid) ? $uid : $GLOBALS['user']->uid;
997
998 $info = db_select('webform_last_download', 'wld')
999 ->fields('wld')
1000 ->condition('nid', $nid)
1001 ->condition('uid', $uid)
1002 ->execute()
1003 ->fetchAssoc();
1004
1005 return $info;
1006 }