accessibility.admin.inc

  1. cis7 sites/all/modules/ulmus/accessibility/accessibility.admin.inc
  2. cle7 sites/all/modules/ulmus/accessibility/accessibility.admin.inc
  3. ecd7 sites/all/modules/ulmus/accessibility/accessibility.admin.inc
  4. elmsmedia7 sites/all/modules/ulmus/accessibility/accessibility.admin.inc
  5. harmony7 sites/all/modules/ulmus/accessibility/accessibility.admin.inc
  6. icor7 sites/all/modules/ulmus/accessibility/accessibility.admin.inc
  7. meedjum_blog7 sites/all/modules/ulmus/accessibility/accessibility.admin.inc
  8. mooc7 sites/all/modules/ulmus/accessibility/accessibility.admin.inc

Functions

Namesort descending Description
accessibility_admin_existing_tests List existing accessibility tests. @todo - add a filter form
accessibility_admin_filter_form Form to filter accessibility tests.
accessibility_admin_filter_form_submit Form submit callback for filter form.
accessibility_tests_list Form for importing tests.
accessibility_tests_list_done Batch import finished callback.
accessibility_tests_list_submit Form submit callback for importing tests.
_accessibility_admin_get_enabled_tests Helper function to get enabled tests.
_accessibility_create_test_from_quail Creates a test entity from a quail test.

File

sites/all/modules/ulmus/accessibility/accessibility.admin.inc
View source
  1. <?php
  2. /**
  3. * Helper function to get enabled tests.
  4. */
  5. function _accessibility_admin_get_enabled_tests() {
  6. static $tests;
  7. if ($tests) {
  8. return $tests;
  9. }
  10. $tests = db_select('accessibility_test', 'a')
  11. ->fields('a', array('quail_name', 'name'))
  12. ->execute()
  13. ->fetchAllKeyed();
  14. return $tests;
  15. }
  16. /**
  17. * Form for importing tests.
  18. */
  19. function accessibility_tests_list($form, $form_state) {
  20. $library_path = libraries_get_path('quail');
  21. $options = array();
  22. $form = array();
  23. $tests = module_invoke_all('accessibility_tests', TRUE);
  24. $guidelines = module_invoke_all('accessibility_guidelines', TRUE);
  25. $session = isset($_SESSION['accessibility_tests_filter']) ? $_SESSION['accessibility_tests_filter'] : array();
  26. $form['filter'] = array(
  27. '#type' => 'fieldset',
  28. '#title' => t('Filter tests'),
  29. '#collapsible' => TRUE,
  30. );
  31. $guideline_options = array();
  32. foreach($guidelines as $k => $guideline) {
  33. $guideline_options[$k] = $guideline['title'];
  34. }
  35. $form['filter']['guideline'] = array(
  36. '#type' => 'select',
  37. '#title' => t('Guideline'),
  38. '#options' => $guideline_options,
  39. '#default_value' => isset($session['guideline']) ? $session['guideline'] : '',
  40. '#empty_option' => t('-- Any --'),
  41. );
  42. $categories = array();
  43. foreach($tests as $test) {
  44. foreach($test['tags'] as $tag) {
  45. $categories[$tag] = $tag;
  46. }
  47. }
  48. ksort($categories);
  49. $form['filter']['category'] = array(
  50. '#type' => 'select',
  51. '#title' => t('Category'),
  52. '#options' => $categories,
  53. '#default_value' => isset($session['category']) ? $session['category'] : '',
  54. '#empty_option' => t('-- Any --'),
  55. );
  56. $form['filter']['severity'] = array(
  57. '#type' => 'select',
  58. '#title' => t('Severity'),
  59. '#options' => array(
  60. ACCESSIBILITY_TEST_SEVERE => t('Severe'),
  61. ACCESSIBILITY_TEST_MODERATE => t('Moderate'),
  62. ACCESSIBILITY_TEST_SUGGESTION => t('Suggestion'),
  63. ),
  64. '#default_value' => isset($session['severity']) ? $session['severity'] : '',
  65. '#empty_option' => t('-- Any --'),
  66. );
  67. $form['filter']['filter'] = array(
  68. '#type' => 'submit',
  69. '#value' => t('Filter'),
  70. );
  71. $form['filter']['reset'] = array(
  72. '#type' => 'submit',
  73. '#value' => t('Reset'),
  74. );
  75. $enabled_tests = _accessibility_admin_get_enabled_tests();
  76. foreach ($tests as $testname => $test) {
  77. if (!isset($enabled_tests[$testname]) &&
  78. (!isset($session['category']) || !$session['category'] || in_array($session['category'], $test['tags'])) &&
  79. (!isset($session['guideline']) || !$session['guideline'] || in_array($testname, $guidelines[$session['guideline']]['tests'])) &&
  80. (!isset($session['severity']) || !$session['severity'] || $test['severity'] == $session['severity'])) {
  81. $options[$testname] = array('test' => $test['title'],
  82. 'severity' => t(ucfirst($test['severity'])),
  83. 'categories' => implode(', ', $test['tags']),
  84. );
  85. }
  86. }
  87. $header = array('test' => t('Test'),
  88. 'categories' => t('Categories'),
  89. 'severity' => t('Severity'),
  90. );
  91. $form['tests'] = array(
  92. '#type' => 'tableselect',
  93. '#title' => t('Available accessibility tests'),
  94. '#options' => $options,
  95. '#header' => $header,
  96. '#default_value' => $enabled_tests,
  97. );
  98. $form['submit'] = array(
  99. '#type' => 'submit',
  100. '#value' => t('Import tests'),
  101. );
  102. return $form;
  103. }
  104. /**
  105. * Form submit callback for importing tests.
  106. */
  107. function accessibility_tests_list_submit($form, &$form_state) {
  108. if($form_state['clicked_button']['#value'] == t('Filter')) {
  109. $_SESSION['accessibility_tests_filter'] = array(
  110. 'guideline' => $form_state['values']['guideline'],
  111. 'category' => $form_state['values']['category'],
  112. 'severity' => $form_state['values']['severity'],
  113. );
  114. return;
  115. }
  116. if($form_state['clicked_button']['#value'] == t('Reset')) {
  117. $_SESSION['accessibility_tests_filter'] = array();
  118. return;
  119. }
  120. $tests = module_invoke_all('accessibility_tests', TRUE);
  121. $enabled_tests = _accessibility_admin_get_enabled_tests();
  122. $batch = array(
  123. 'operations' => array(),
  124. 'finished' => 'accessibility_tests_list_done',
  125. 'title' => t('Importing tests'),
  126. 'init_message' => t('Starting to import tests.'),
  127. 'progress_message' => t('Imported @current out of @total.'),
  128. 'error_message' => t('An error occurred while importing tests.'),
  129. 'file' => drupal_get_path('module', 'accessibility') . '/accessibility.admin.inc',
  130. );
  131. foreach ($form_state['values']['tests'] as $test => $enabled) {
  132. if ($enabled && !isset($enabled_tests[$test])) {
  133. $batch['operations'][] = array('_accessibility_create_test_from_quail', array($test, $tests[$test]));
  134. }
  135. }
  136. batch_set($batch);
  137. }
  138. /**
  139. * Batch import finished callback.
  140. */
  141. function accessibility_tests_list_done($success, $results, $operations) {
  142. if ($success) {
  143. // Here we do something meaningful with the results.
  144. $message = count($results) . ' imported.';
  145. $message .= theme('item_list', $results);
  146. }
  147. else {
  148. // An error occurred.
  149. // $operations contains the operations that remained unprocessed.
  150. $error_operation = reset($operations);
  151. $message = t('An error occurred while processing %error_operation with arguments: @arguments', array('%error_operation' => $error_operation[0], '@arguments' => print_r($error_operation[1], TRUE)));
  152. }
  153. drupal_set_message($message);
  154. }
  155. /**
  156. * Creates a test entity from a quail test.
  157. */
  158. function _accessibility_create_test_from_quail($quail_name, $test, &$context) {
  159. $language = language_default();
  160. $new_test = new stdClass();
  161. $new_test->language = $language->language;
  162. $new_test->quail_name = $quail_name;
  163. $new_test->name = $test['title'];
  164. $new_test->severity = (isset($test['severity'])) ? $test['severity'] : 'suggestion';
  165. $wrapper = entity_metadata_wrapper('accessibility_test', $new_test);
  166. $wrapper->error_description->set(array('value' => $test['description'],
  167. 'format' => filter_default_format()
  168. ));
  169. $wrapper->save(TRUE);
  170. accessibility_test_save($new_test);
  171. $context['message'] = t('Done importing @name', array('@name' => $new_test->name));
  172. $context['results'][] = $new_test->name;
  173. }
  174. /**
  175. * List existing accessibility tests.
  176. * @todo - add a filter form
  177. */
  178. function accessibility_admin_existing_tests() {
  179. $build = array('filter_form' => drupal_get_form('accessibility_admin_filter_form'));
  180. $header = array(t('Name'), t('Severity'), t('Status'), t('Edit'), t('Delete'));
  181. $rows = array();
  182. $query = db_select('accessibility_test', 't')
  183. ->fields('t', array('test_id', 'name', 'quail_name', 'severity', 'status'))
  184. ->orderBy('t.name');
  185. $filter = (isset($_SESSION['accessibility_admin_filter'])) ? $_SESSION['accessibility_admin_filter'] : array();
  186. if (isset($filter['severity'])) {
  187. $in = array();
  188. foreach ($filter['severity'] as $severity) {
  189. if ($severity) {
  190. $in[] = $severity;
  191. }
  192. }
  193. if (count($in)) {
  194. $query->condition('t.severity', $in, 'IN');
  195. }
  196. }
  197. if (isset($filter['name']) && strlen($filter['name'])) {
  198. $query->condition('t.name', '%' . $filter['name'] . '%', 'LIKE');
  199. }
  200. $tests = $query->execute()
  201. ->fetchAll();
  202. foreach ($tests as $test) {
  203. $rows[] = array(l($test->name, 'accessibility-test/' . $test->test_id),
  204. t(ucfirst($test->severity)),
  205. (($test->status) ? t('Active') : t('Inactive')),
  206. l(t('edit'), 'accessibility-test/' . $test->test_id . '/edit', array('query' => array('destination' => 'admin/config/accessibility/tests'))),
  207. l(t('delete'), 'accessibility-test/' . $test->test_id . '/delete', array('query' => array('destination' => 'admin/config/accessibility/tests')))
  208. );
  209. }
  210. $build['result_table'] = array('#theme' => 'table',
  211. '#header' => $header,
  212. '#rows' => $rows,
  213. );
  214. if (!count($rows)) {
  215. $build['result_table'] = array('#markup' => t('No tests, found. !link.', array('!link' => l(t('Import tests'), 'admin/config/accessibility/tests/import'))));
  216. }
  217. return $build;
  218. }
  219. /**
  220. * Form to filter accessibility tests.
  221. */
  222. function accessibility_admin_filter_form($form, $form_state) {
  223. $form = array();
  224. $filter = (isset($_SESSION['accessibility_admin_filter'])) ? $_SESSION['accessibility_admin_filter'] : array();
  225. $form['filters'] = array(
  226. '#type' => 'fieldset'
  227. );
  228. $form['filters']['name'] = array(
  229. '#title' => t('Test name contains'),
  230. '#type' => 'textfield',
  231. '#default_value' => (isset($filter['name']) ? $filter['name'] : array())
  232. );
  233. $form['filters']['severity'] = array(
  234. '#title' => t('Severity level'),
  235. '#type' => 'checkboxes',
  236. '#options' => array(
  237. ACCESSIBILITY_TEST_SEVERE => t('Severe'),
  238. ACCESSIBILITY_TEST_MODERATE => t('Moderate'),
  239. ACCESSIBILITY_TEST_SUGGESTION => t('Suggestion'),
  240. ),
  241. '#default_value' => (isset($filter['severity']) ? $filter['severity'] : array())
  242. );
  243. $form['filters']['submit'] = array(
  244. '#type' => 'submit',
  245. '#value' => t('Search'),
  246. );
  247. $form['filters']['reset'] = array(
  248. '#type' => 'submit',
  249. '#value' => t('Reset'),
  250. );
  251. return $form;
  252. }
  253. /**
  254. * Form submit callback for filter form.
  255. */
  256. function accessibility_admin_filter_form_submit($form, $form_state) {
  257. $_SESSION['accessibility_admin_filter'] = $form_state['values'];
  258. if ($form_state['clicked_button']['#value'] == t('Reset')) {
  259. $_SESSION['accessibility_admin_filter'] = array();
  260. }
  261. }