PageRenderTime 65ms CodeModel.GetById 20ms RepoModel.GetById 2ms app.codeStats 0ms

/admin/qtypes.php

https://github.com/kpike/moodle
PHP | 312 lines | 216 code | 47 blank | 49 comment | 45 complexity | 854e4c5605fa94e08aa1beceffe3f7dc MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Allows the admin to manage question types.
  18. *
  19. * @package moodlecore
  20. * @subpackage questionbank
  21. * @copyright 2008 Tim Hunt
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. require_once(dirname(__FILE__) . '/../config.php');
  25. require_once($CFG->libdir . '/questionlib.php');
  26. require_once($CFG->libdir . '/adminlib.php');
  27. require_once($CFG->libdir . '/tablelib.php');
  28. // Check permissions.
  29. require_login();
  30. $systemcontext = get_context_instance(CONTEXT_SYSTEM);
  31. require_capability('moodle/question:config', $systemcontext);
  32. $canviewreports = has_capability('report/questioninstances:view', $systemcontext);
  33. admin_externalpage_setup('manageqtypes');
  34. $thispageurl = new moodle_url('/admin/qtypes.php');
  35. $qtypes = question_bank::get_all_qtypes();
  36. // Get some data we will need - question counts and which types are needed.
  37. $counts = $DB->get_records_sql("
  38. SELECT qtype, COUNT(1) as numquestions, SUM(hidden) as numhidden
  39. FROM {question} GROUP BY qtype", array());
  40. $needed = array();
  41. foreach ($qtypes as $qtypename => $qtype) {
  42. if (!isset($counts[$qtypename])) {
  43. $counts[$qtypename] = new stdClass;
  44. $counts[$qtypename]->numquestions = 0;
  45. $counts[$qtypename]->numhidden = 0;
  46. }
  47. $needed[$qtypename] = $counts[$qtypename]->numquestions > 0;
  48. $counts[$qtypename]->numquestions -= $counts[$qtypename]->numhidden;
  49. }
  50. $needed['missingtype'] = true; // The system needs the missing question type.
  51. foreach ($qtypes as $qtypename => $qtype) {
  52. foreach ($qtype->requires_qtypes() as $reqtype) {
  53. $needed[$reqtype] = true;
  54. }
  55. }
  56. foreach ($counts as $qtypename => $count) {
  57. if (!isset($qtypes[$qtypename])) {
  58. $counts['missingtype']->numquestions += $count->numquestions - $count->numhidden;
  59. $counts['missingtype']->numhidden += $count->numhidden;
  60. }
  61. }
  62. // Work of the correct sort order.
  63. $config = get_config('question');
  64. $sortedqtypes = array();
  65. foreach ($qtypes as $qtypename => $qtype) {
  66. $sortedqtypes[$qtypename] = $qtype->local_name();
  67. }
  68. $sortedqtypes = question_bank::sort_qtype_array($sortedqtypes, $config);
  69. // Process actions ============================================================
  70. // Disable.
  71. if (($disable = optional_param('disable', '', PARAM_SAFEDIR)) && confirm_sesskey()) {
  72. if (!isset($qtypes[$disable])) {
  73. print_error('unknownquestiontype', 'question', $thispageurl, $disable);
  74. }
  75. set_config($disable . '_disabled', 1, 'question');
  76. redirect($thispageurl);
  77. }
  78. // Enable.
  79. if (($enable = optional_param('enable', '', PARAM_SAFEDIR)) && confirm_sesskey()) {
  80. if (!isset($qtypes[$enable])) {
  81. print_error('unknownquestiontype', 'question', $thispageurl, $enable);
  82. }
  83. if (!$qtypes[$enable]->menu_name()) {
  84. print_error('cannotenable', 'question', $thispageurl, $enable);
  85. }
  86. unset_config($enable . '_disabled', 'question');
  87. redirect($thispageurl);
  88. }
  89. // Move up in order.
  90. if (($up = optional_param('up', '', PARAM_SAFEDIR)) && confirm_sesskey()) {
  91. if (!isset($qtypes[$up])) {
  92. print_error('unknownquestiontype', 'question', $thispageurl, $up);
  93. }
  94. $neworder = question_reorder_qtypes($sortedqtypes, $up, -1);
  95. question_save_qtype_order($neworder, $config);
  96. redirect($thispageurl);
  97. }
  98. // Move down in order.
  99. if (($down = optional_param('down', '', PARAM_SAFEDIR)) && confirm_sesskey()) {
  100. if (!isset($qtypes[$down])) {
  101. print_error('unknownquestiontype', 'question', $thispageurl, $down);
  102. }
  103. $neworder = question_reorder_qtypes($sortedqtypes, $down, +1);
  104. question_save_qtype_order($neworder, $config);
  105. redirect($thispageurl);
  106. }
  107. // Delete.
  108. if (($delete = optional_param('delete', '', PARAM_SAFEDIR)) && confirm_sesskey()) {
  109. // Check it is OK to delete this question type.
  110. if ($delete == 'missingtype') {
  111. print_error('cannotdeletemissingqtype', 'question', $thispageurl);
  112. }
  113. if (!isset($qtypes[$delete])) {
  114. print_error('unknownquestiontype', 'question', $thispageurl, $delete);
  115. }
  116. $qtypename = $qtypes[$delete]->local_name();
  117. if ($counts[$delete]->numquestions + $counts[$delete]->numhidden > 0) {
  118. print_error('cannotdeleteqtypeinuse', 'question', $thispageurl, $qtypename);
  119. }
  120. if ($needed[$delete] > 0) {
  121. print_error('cannotdeleteqtypeneeded', 'question', $thispageurl, $qtypename);
  122. }
  123. // If not yet confirmed, display a confirmation message.
  124. if (!optional_param('confirm', '', PARAM_BOOL)) {
  125. $qtypename = $qtypes[$delete]->local_name();
  126. echo $OUTPUT->header();
  127. echo $OUTPUT->heading(get_string('deleteqtypeareyousure', 'question', $qtypename));
  128. echo $OUTPUT->confirm(get_string('deleteqtypeareyousuremessage', 'question', $qtypename),
  129. new moodle_url($thispageurl, array('delete' => $delete, 'confirm' => 1)),
  130. $thispageurl);
  131. echo $OUTPUT->footer();
  132. exit;
  133. }
  134. // Do the deletion.
  135. echo $OUTPUT->header();
  136. echo $OUTPUT->heading(get_string('deletingqtype', 'question', $qtypename));
  137. // Delete any configuration records.
  138. if (!unset_all_config_for_plugin('qtype_' . $delete)) {
  139. echo $OUTPUT->notification(get_string('errordeletingconfig', 'admin', 'qtype_' . $delete));
  140. }
  141. unset_config($delete . '_disabled', 'question');
  142. unset_config($delete . '_sortorder', 'question');
  143. // Then the tables themselves
  144. drop_plugin_tables($delete, $qtypes[$delete]->plugin_dir() . '/db/install.xml', false);
  145. // Remove event handlers and dequeue pending events
  146. events_uninstall('qtype_' . $delete);
  147. $a->qtype = $qtypename;
  148. $a->directory = $qtypes[$delete]->plugin_dir();
  149. echo $OUTPUT->box(get_string('qtypedeletefiles', 'question', $a), 'generalbox', 'notice');
  150. echo $OUTPUT->continue_button($thispageurl);
  151. echo $OUTPUT->footer();
  152. exit;
  153. }
  154. // End of process actions ==================================================
  155. // Print the page heading.
  156. echo $OUTPUT->header();
  157. echo $OUTPUT->heading(get_string('manageqtypes', 'admin'));
  158. // Set up the table.
  159. $table = new flexible_table('qtypeadmintable');
  160. $table->define_baseurl($thispageurl);
  161. $table->define_columns(array('questiontype', 'numquestions', 'version', 'requires',
  162. 'availableto', 'delete', 'settings'));
  163. $table->define_headers(array(get_string('questiontype', 'question'), get_string('numquestions', 'question'),
  164. get_string('version'), get_string('requires', 'admin'), get_string('availableq', 'question'),
  165. get_string('delete'), get_string('settings')));
  166. $table->set_attribute('id', 'qtypes');
  167. $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
  168. $table->setup();
  169. // Add a row for each question type.
  170. $createabletypes = question_bank::get_creatable_qtypes();
  171. foreach ($sortedqtypes as $qtypename => $localname) {
  172. $qtype = $qtypes[$qtypename];
  173. $row = array();
  174. // Question icon and name.
  175. $fakequestion = new stdClass;
  176. $fakequestion->qtype = $qtypename;
  177. $icon = print_question_icon($fakequestion, true);
  178. $row[] = $icon . ' ' . $localname;
  179. // Number of questions of this type.
  180. if ($counts[$qtypename]->numquestions + $counts[$qtypename]->numhidden > 0) {
  181. if ($counts[$qtypename]->numhidden > 0) {
  182. $strcount = get_string('numquestionsandhidden', 'question', $counts[$qtypename]);
  183. } else {
  184. $strcount = $counts[$qtypename]->numquestions;
  185. }
  186. if ($canviewreports) {
  187. $row[] = html_writer::link(new moodle_url('/admin/report/questioninstances/index.php',
  188. array('qtype' => $qtypename)), $strcount, array('title' => get_string('showdetails', 'admin')));
  189. } else {
  190. $strcount;
  191. }
  192. } else {
  193. $row[] = 0;
  194. }
  195. // Question version number.
  196. $version = get_config('qtype_' . $qtypename, 'version');
  197. if ($version) {
  198. $row[] = $version;
  199. } else {
  200. $row[] = html_writer::tag('span', get_string('nodatabase', 'admin'), array('class' => 'disabled'));
  201. }
  202. // Other question types required by this one.
  203. $requiredtypes = $qtype->requires_qtypes();
  204. $strtypes = array();
  205. if (!empty($requiredtypes)) {
  206. foreach ($requiredtypes as $required) {
  207. $strtypes[] = $qtypes[$required]->local_name();
  208. }
  209. $row[] = implode(', ', $strtypes);
  210. } else {
  211. $row[] = '';
  212. }
  213. // Are people allowed to create new questions of this type?
  214. $rowclass = '';
  215. if ($qtype->menu_name()) {
  216. $createable = isset($createabletypes[$qtypename]);
  217. $icons = question_types_enable_disable_icons($qtypename, $createable);
  218. if (!$createable) {
  219. $rowclass = 'dimmed_text';
  220. }
  221. } else {
  222. $icons = $OUTPUT->spacer() . ' ';
  223. }
  224. // Move icons.
  225. $icons .= question_type_icon_html('up', $qtypename, 't/up', get_string('up'), '');
  226. $icons .= question_type_icon_html('down', $qtypename, 't/down', get_string('down'), '');
  227. $row[] = $icons;
  228. // Delete link, if available.
  229. if ($needed[$qtypename]) {
  230. $row[] = '';
  231. } else {
  232. $row[] = html_writer::link(new moodle_url($thispageurl,
  233. array('delete' => $qtypename, 'sesskey' => sesskey())), get_string('delete'),
  234. array('title' => get_string('uninstallqtype', 'question')));
  235. }
  236. // Settings link, if available.
  237. $settings = admin_get_root()->locate('qtypesetting' . $qtypename);
  238. if ($settings instanceof admin_externalpage) {
  239. $row[] = html_writer::link($settings->url, get_string('settings'));
  240. } else if ($settings instanceof admin_settingpage) {
  241. $row[] = html_writer::link(new moodle_url('/admin/settings.php',
  242. array('section' => 'qtypesetting' . $qtypename)), get_string('settings'));
  243. } else {
  244. $row[] = '';
  245. }
  246. $table->add_data($row, $rowclass);
  247. }
  248. $table->finish_output();
  249. echo $OUTPUT->footer();
  250. function question_types_enable_disable_icons($qtypename, $createable) {
  251. if ($createable) {
  252. return question_type_icon_html('disable', $qtypename, 'i/hide',
  253. get_string('enabled', 'question'), get_string('disable'));
  254. } else {
  255. return question_type_icon_html('enable', $qtypename, 'i/show',
  256. get_string('disabled', 'question'), get_string('enable'));
  257. }
  258. }
  259. function question_type_icon_html($action, $qtypename, $icon, $alt, $tip) {
  260. global $OUTPUT;
  261. return $OUTPUT->action_icon(new moodle_url('/admin/qtypes.php',
  262. array($action => $qtypename, 'sesskey' => sesskey())),
  263. new pix_icon($icon, $alt, 'moodle', array('title' => '')),
  264. null, array('title' => $tip)) . ' ';
  265. }