grid.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. /**
  3. * @file
  4. * Webform module grid component.
  5. */
  6. // Grid depends on functions provided by select.
  7. webform_component_include('select');
  8. /**
  9. * Implements _webform_defaults_component().
  10. */
  11. function _webform_defaults_grid() {
  12. return array(
  13. 'name' => '',
  14. 'form_key' => NULL,
  15. 'mandatory' => 0,
  16. 'pid' => 0,
  17. 'weight' => 0,
  18. 'extra' => array(
  19. 'options' => '',
  20. 'questions' => '',
  21. 'optrand' => 0,
  22. 'qrand' => 0,
  23. 'title_display' => 0,
  24. 'custom_option_keys' => 0,
  25. 'custom_question_keys' => 0,
  26. 'description' => '',
  27. 'private' => FALSE,
  28. ),
  29. );
  30. }
  31. /**
  32. * Implements _webform_theme_component().
  33. */
  34. function _webform_theme_grid() {
  35. return array(
  36. 'webform_grid' => array(
  37. 'render element' => 'element',
  38. 'file' => 'components/grid.inc',
  39. ),
  40. 'webform_display_grid' => array(
  41. 'render element' => 'element',
  42. 'file' => 'components/grid.inc',
  43. ),
  44. );
  45. }
  46. /**
  47. * Implements _webform_edit_component().
  48. */
  49. function _webform_edit_grid($component) {
  50. $form = array();
  51. if (module_exists('options_element')) {
  52. $form['options'] = array(
  53. '#type' => 'fieldset',
  54. '#title' => t('Options'),
  55. '#collapsible' => TRUE,
  56. '#description' => t('Options to select across the top. Usually these are ratings such as "poor" through "excellent" or "strongly disagree" through "strongly agree".'),
  57. '#attributes' => array('class' => array('webform-options-element')),
  58. '#element_validate' => array('_webform_edit_validate_options'),
  59. );
  60. $form['options']['options'] = array(
  61. '#type' => 'options',
  62. '#options' => _webform_select_options_from_text($component['extra']['options'], TRUE),
  63. '#optgroups' => FALSE,
  64. '#default_value' => FALSE,
  65. '#optgroups' => FALSE,
  66. '#key_type' => 'mixed',
  67. '#key_type_toggle' => t('Customize option keys (Advanced)'),
  68. '#key_type_toggled' => $component['extra']['custom_option_keys'],
  69. );
  70. $form['questions'] = array(
  71. '#type' => 'fieldset',
  72. '#title' => t('Questions'),
  73. '#collapsible' => TRUE,
  74. '#description' => t('Questions list down the side of the grid.'),
  75. '#attributes' => array('class' => array('webform-options-element')),
  76. '#element_validate' => array('_webform_edit_validate_options'),
  77. );
  78. $form['questions']['options'] = array(
  79. '#type' => 'options',
  80. '#options' => _webform_select_options_from_text($component['extra']['questions'], TRUE),
  81. '#optgroups' => FALSE,
  82. '#default_value' => FALSE,
  83. '#optgroups' => FALSE,
  84. '#key_type' => 'mixed',
  85. '#key_type_toggle' => t('Customize question keys (Advanced)'),
  86. '#key_type_toggled' => $component['extra']['custom_question_keys'],
  87. );
  88. }
  89. else {
  90. $form['extra']['options'] = array(
  91. '#type' => 'textarea',
  92. '#title' => t('Options'),
  93. '#default_value' => $component['extra']['options'],
  94. '#description' => t('Options to select across the top. One option per line. <strong>Key-value pairs MUST be specified as "safe_key|Some readable option"</strong>. Use of only alphanumeric characters and underscores is recommended in keys.') . theme('webform_token_help'),
  95. '#cols' => 60,
  96. '#rows' => 5,
  97. '#weight' => -3,
  98. '#required' => TRUE,
  99. '#wysiwyg' => FALSE,
  100. '#element_validate' => array('_webform_edit_validate_select'),
  101. );
  102. $form['extra']['questions'] = array(
  103. '#type' => 'textarea',
  104. '#title' => t('Questions'),
  105. '#default_value' => $component['extra']['questions'],
  106. '#description' => t('Questions list down the side of the grid. One question per line. <strong>Key-value pairs MUST be specified as "safe_key|Some readable option"</strong>. Use of only alphanumeric characters and underscores is recommended in keys.') . theme('webform_token_help'),
  107. '#cols' => 60,
  108. '#rows' => 5,
  109. '#weight' => -2,
  110. '#required' => TRUE,
  111. '#wysiwyg' => FALSE,
  112. '#element_validate' => array('_webform_edit_validate_select'),
  113. );
  114. }
  115. $form['display']['optrand'] = array(
  116. '#type' => 'checkbox',
  117. '#title' => t('Randomize Options'),
  118. '#default_value' => $component['extra']['optrand'],
  119. '#description' => t('Randomizes the order of options on the top when they are displayed in the form.'),
  120. '#parents' => array('extra', 'optrand')
  121. );
  122. $form['display']['qrand'] = array(
  123. '#type' => 'checkbox',
  124. '#title' => t('Randomize Questions'),
  125. '#default_value' => $component['extra']['qrand'],
  126. '#description' => t('Randomize the order of the questions on the side when they are displayed in the form.'),
  127. '#parents' => array('extra', 'qrand')
  128. );
  129. return $form;
  130. }
  131. /**
  132. * Implements _webform_render_component().
  133. */
  134. function _webform_render_grid($component, $value = NULL, $filter = TRUE) {
  135. $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
  136. $element = array(
  137. '#type' => 'webform_grid',
  138. '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
  139. '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
  140. '#required' => $component['mandatory'],
  141. '#weight' => $component['weight'],
  142. '#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
  143. '#grid_questions' => _webform_select_options_from_text($component['extra']['questions'], TRUE),
  144. '#grid_options' => _webform_select_options_from_text($component['extra']['options'], TRUE),
  145. '#optrand' => $component['extra']['optrand'],
  146. '#qrand' => $component['extra']['qrand'],
  147. '#theme' => 'webform_grid',
  148. '#theme_wrappers' => array('webform_element'),
  149. '#process' => array('webform_expand_grid'),
  150. '#translatable' => array('title', 'description', 'grid_options', 'grid_questions'),
  151. );
  152. if ($value) {
  153. $element['#default_value'] = $value;
  154. }
  155. return $element;
  156. }
  157. /**
  158. * A Form API #process function for Webform grid fields.
  159. */
  160. function webform_expand_grid($element) {
  161. $options = $element['#grid_options'];
  162. $questions = $element['#grid_questions'];
  163. if (!empty($element['#optrand'])) {
  164. _webform_shuffle_options($options);
  165. }
  166. if (!empty($element['#qrand'])) {
  167. _webform_shuffle_options($questions);
  168. }
  169. foreach ($questions as $key => $question) {
  170. if ($question != '') {
  171. $element[$key] = array(
  172. '#title' => $question,
  173. '#required' => $element['#required'],
  174. '#options' => $options,
  175. '#type' => 'radios',
  176. '#process' => array('form_process_radios', 'webform_expand_select_ids'),
  177. // Webform handles validation manually.
  178. '#validated' => TRUE,
  179. '#webform_validated' => FALSE,
  180. '#translatable' => array('title'),
  181. );
  182. }
  183. }
  184. $value = isset($element['#default_value']) ? $element['#default_value'] : array();
  185. foreach (element_children($element) as $key) {
  186. if (isset($value[$key])) {
  187. $element[$key]['#default_value'] = ($value[$key] !== '') ? $value[$key] : FALSE;
  188. }
  189. else {
  190. $element[$key]['#default_value'] = FALSE;
  191. }
  192. }
  193. return $element;
  194. }
  195. /**
  196. * Implements _webform_display_component().
  197. */
  198. function _webform_display_grid($component, $value, $format = 'html') {
  199. $questions = _webform_select_options_from_text($component['extra']['questions'], TRUE);
  200. $options = _webform_select_options_from_text($component['extra']['options'], TRUE);
  201. $element = array(
  202. '#title' => $component['name'],
  203. '#weight' => $component['weight'],
  204. '#format' => $format,
  205. '#grid_questions' => $questions,
  206. '#grid_options' => $options,
  207. '#theme' => 'webform_display_grid',
  208. '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
  209. '#sorted' => TRUE,
  210. '#translatable' => array('#title', '#grid_questions', '#grid_options'),
  211. );
  212. foreach ($questions as $key => $question) {
  213. if ($question !== '') {
  214. $element[$key] = array(
  215. '#title' => $question,
  216. '#value' => isset($value[$key]) ? $value[$key] : NULL,
  217. '#translatable' => array('#title', '#value'),
  218. );
  219. }
  220. }
  221. return $element;
  222. }
  223. /**
  224. * Format the text output for this component.
  225. */
  226. function theme_webform_display_grid($variables) {
  227. $element = $variables['element'];
  228. $component = $element['#webform_component'];
  229. $format = $element['#format'];
  230. if ($format == 'html') {
  231. $rows = array();
  232. $header = array(array('data' => '', 'class' => array('webform-grid-question')));
  233. foreach ($element['#grid_options'] as $option) {
  234. $header[] = array('data' => _webform_filter_xss($option), 'class' => array('checkbox', 'webform-grid-option'));
  235. }
  236. foreach ($element['#grid_questions'] as $question_key => $question) {
  237. $row = array();
  238. $row[] = array('data' => _webform_filter_xss($question), 'class' => array('webform-grid-question'));
  239. foreach ($element['#grid_options'] as $option_value => $option_label) {
  240. if (strcmp($element[$question_key]['#value'], $option_value) == 0) {
  241. $row[] = array('data' => '<strong>X</strong>', 'class' => array('checkbox', 'webform-grid-option'));
  242. }
  243. else {
  244. $row[] = array('data' => '&nbsp;', 'class' => array('checkbox', 'webform-grid-option'));
  245. }
  246. }
  247. $rows[] = $row;
  248. }
  249. $option_count = count($header) - 1;
  250. $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('class' => array('webform-grid', 'webform-grid-' . $option_count))));
  251. }
  252. else {
  253. $items = array();
  254. foreach (element_children($element) as $key) {
  255. $items[] = ' - ' . $element[$key]['#title'] . ': ' . (isset($element['#grid_options'][$element[$key]['#value']]) ? $element['#grid_options'][$element[$key]['#value']] : '');
  256. }
  257. $output = implode("\n", $items);
  258. }
  259. return $output;
  260. }
  261. /**
  262. * Implements _webform_analysis_component().
  263. */
  264. function _webform_analysis_grid($component, $sids = array()) {
  265. // Generate the list of options and questions.
  266. $options = _webform_select_options_from_text($component['extra']['options'], TRUE);
  267. $questions = _webform_select_options_from_text($component['extra']['questions'], TRUE);
  268. // Generate a lookup table of results.
  269. $query = db_select('webform_submitted_data', 'wsd')
  270. ->fields('wsd', array('no', 'data'))
  271. ->condition('nid', $component['nid'])
  272. ->condition('cid', $component['cid'])
  273. ->condition('data', '', '<>')
  274. ->groupBy('no')
  275. ->groupBy('data');
  276. $query->addExpression('COUNT(sid)', 'datacount');
  277. if (count($sids)) {
  278. $query->condition('sid', $sids, 'IN');
  279. }
  280. $result = $query->execute();
  281. $counts = array();
  282. foreach ($result as $data) {
  283. $counts[$data->no][$data->data] = $data->datacount;
  284. }
  285. // Create an entire table to be put into the returned row.
  286. $rows = array();
  287. $header = array('');
  288. // Add options as a header row.
  289. foreach ($options as $option) {
  290. $header[] = _webform_filter_xss($option);
  291. }
  292. // Add questions as each row.
  293. foreach ($questions as $qkey => $question) {
  294. $row = array(_webform_filter_xss($question));
  295. foreach ($options as $okey => $option) {
  296. $row[] = !empty($counts[$qkey][$okey]) ? $counts[$qkey][$okey] : 0;
  297. }
  298. $rows[] = $row;
  299. }
  300. $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('class' => array('webform-grid'))));
  301. return array(array(array('data' => $output, 'colspan' => 2)));
  302. }
  303. /**
  304. * Implements _webform_table_component().
  305. */
  306. function _webform_table_grid($component, $value) {
  307. $questions = _webform_select_options_from_text($component['extra']['questions'], TRUE);
  308. $options = _webform_select_options_from_text($component['extra']['options'], TRUE);
  309. $output = '';
  310. // Set the value as a single string.
  311. foreach ($questions as $key => $label) {
  312. if (isset($value[$key]) && isset($options[$value[$key]])) {
  313. $output .= _webform_filter_xss($label) . ': ' . _webform_filter_xss($options[$value[$key]]) . '<br />';
  314. }
  315. }
  316. return $output;
  317. }
  318. /**
  319. * Implements _webform_csv_headers_component().
  320. */
  321. function _webform_csv_headers_grid($component, $export_options) {
  322. $header = array();
  323. $header[0] = array('');
  324. $header[1] = array($component['name']);
  325. $items = _webform_select_options_from_text($component['extra']['questions'], TRUE);
  326. $count = 0;
  327. foreach ($items as $key => $item) {
  328. // Empty column per sub-field in main header.
  329. if ($count != 0) {
  330. $header[0][] = '';
  331. $header[1][] = '';
  332. }
  333. // The value for this option.
  334. $header[2][] = $item;
  335. $count++;
  336. }
  337. return $header;
  338. }
  339. /**
  340. * Implements _webform_csv_data_component().
  341. */
  342. function _webform_csv_data_grid($component, $export_options, $value) {
  343. $questions = _webform_select_options_from_text($component['extra']['questions'], TRUE);
  344. $options = _webform_select_options_from_text($component['extra']['options'], TRUE);
  345. $return = array();
  346. foreach ($questions as $key => $question) {
  347. if (isset($value[$key]) && isset($options[$value[$key]])) {
  348. $return[] = $export_options['select_keys'] ? $value[$key] : $options[$value[$key]];
  349. }
  350. else {
  351. $return[] = '';
  352. }
  353. }
  354. return $return;
  355. }
  356. function theme_webform_grid($variables) {
  357. $element = $variables['element'];
  358. $rows = array();
  359. $header = array(array('data' => '', 'class' => array('webform-grid-question')));
  360. // Set the header for the table.
  361. foreach ($element['#grid_options'] as $option) {
  362. $header[] = array('data' => _webform_filter_xss($option), 'class' => array('checkbox', 'webform-grid-option'));
  363. }
  364. foreach (element_children($element) as $key) {
  365. $question_element = $element[$key];
  366. // Create a row with the question title.
  367. $row = array(array('data' => _webform_filter_xss($question_element['#title']), 'class' => array('webform-grid-question')));
  368. // Render each radio button in the row.
  369. $radios = form_process_radios($question_element);
  370. foreach (element_children($radios) as $key) {
  371. $radios[$key]['#title_display'] = 'invisible';
  372. $row[] = array('data' => drupal_render($radios[$key]), 'class' => array('checkbox', 'webform-grid-option'));
  373. }
  374. $rows[] = $row;
  375. }
  376. $option_count = count($header) - 1;
  377. return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('class' => array('webform-grid', 'webform-grid-' . $option_count))));
  378. }