webform.report.inc 36 KB

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