PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/qtypes.php

https://bitbucket.org/kudutest/moodlegit
PHP | 312 lines | 216 code | 47 blank | 49 comment | 45 complexity | 53d350d6b8130bf43251203d46fae5fa 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 . '/pluginlib.php');
  28. require_once($CFG->libdir . '/tablelib.php');
  29. // Check permissions.
  30. require_login();
  31. $systemcontext = context_system::instance();
  32. require_capability('moodle/question:config', $systemcontext);
  33. $canviewreports = has_capability('report/questioninstances:view', $systemcontext);
  34. admin_externalpage_setup('manageqtypes');
  35. $thispageurl = new moodle_url('/admin/qtypes.php');
  36. $qtypes = question_bank::get_all_qtypes();
  37. $pluginmanager = plugin_manager::instance();
  38. // Get some data we will need - question counts and which types are needed.
  39. $counts = $DB->get_records_sql("
  40. SELECT qtype, COUNT(1) as numquestions, SUM(hidden) as numhidden
  41. FROM {question} GROUP BY qtype", array());
  42. $needed = array();
  43. foreach ($qtypes as $qtypename => $qtype) {
  44. if (!isset($counts[$qtypename])) {
  45. $counts[$qtypename] = new stdClass;
  46. $counts[$qtypename]->numquestions = 0;
  47. $counts[$qtypename]->numhidden = 0;
  48. }
  49. $needed[$qtypename] = $counts[$qtypename]->numquestions > 0 ||
  50. $pluginmanager->other_plugins_that_require($qtype->plugin_name());
  51. $counts[$qtypename]->numquestions -= $counts[$qtypename]->numhidden;
  52. }
  53. $needed['missingtype'] = true; // The system needs the missing question type.
  54. foreach ($counts as $qtypename => $count) {
  55. if (!isset($qtypes[$qtypename])) {
  56. $counts['missingtype']->numquestions += $count->numquestions - $count->numhidden;
  57. $counts['missingtype']->numhidden += $count->numhidden;
  58. }
  59. }
  60. // Work of the correct sort order.
  61. $config = get_config('question');
  62. $sortedqtypes = array();
  63. foreach ($qtypes as $qtypename => $qtype) {
  64. $sortedqtypes[$qtypename] = $qtype->local_name();
  65. }
  66. $sortedqtypes = question_bank::sort_qtype_array($sortedqtypes, $config);
  67. // Process actions ============================================================
  68. // Disable.
  69. if (($disable = optional_param('disable', '', PARAM_PLUGIN)) && confirm_sesskey()) {
  70. if (!isset($qtypes[$disable])) {
  71. print_error('unknownquestiontype', 'question', $thispageurl, $disable);
  72. }
  73. set_config($disable . '_disabled', 1, 'question');
  74. redirect($thispageurl);
  75. }
  76. // Enable.
  77. if (($enable = optional_param('enable', '', PARAM_PLUGIN)) && confirm_sesskey()) {
  78. if (!isset($qtypes[$enable])) {
  79. print_error('unknownquestiontype', 'question', $thispageurl, $enable);
  80. }
  81. if (!$qtypes[$enable]->menu_name()) {
  82. print_error('cannotenable', 'question', $thispageurl, $enable);
  83. }
  84. unset_config($enable . '_disabled', 'question');
  85. redirect($thispageurl);
  86. }
  87. // Move up in order.
  88. if (($up = optional_param('up', '', PARAM_PLUGIN)) && confirm_sesskey()) {
  89. if (!isset($qtypes[$up])) {
  90. print_error('unknownquestiontype', 'question', $thispageurl, $up);
  91. }
  92. $neworder = question_reorder_qtypes($sortedqtypes, $up, -1);
  93. question_save_qtype_order($neworder, $config);
  94. redirect($thispageurl);
  95. }
  96. // Move down in order.
  97. if (($down = optional_param('down', '', PARAM_PLUGIN)) && confirm_sesskey()) {
  98. if (!isset($qtypes[$down])) {
  99. print_error('unknownquestiontype', 'question', $thispageurl, $down);
  100. }
  101. $neworder = question_reorder_qtypes($sortedqtypes, $down, +1);
  102. question_save_qtype_order($neworder, $config);
  103. redirect($thispageurl);
  104. }
  105. // Delete.
  106. if (($delete = optional_param('delete', '', PARAM_PLUGIN)) && confirm_sesskey()) {
  107. // Check it is OK to delete this question type.
  108. if ($delete == 'missingtype') {
  109. print_error('cannotdeletemissingqtype', 'question', $thispageurl);
  110. }
  111. if (!isset($qtypes[$delete])) {
  112. print_error('unknownquestiontype', 'question', $thispageurl, $delete);
  113. }
  114. $qtypename = $qtypes[$delete]->local_name();
  115. if ($counts[$delete]->numquestions + $counts[$delete]->numhidden > 0) {
  116. print_error('cannotdeleteqtypeinuse', 'question', $thispageurl, $qtypename);
  117. }
  118. if ($needed[$delete] > 0) {
  119. print_error('cannotdeleteqtypeneeded', 'question', $thispageurl, $qtypename);
  120. }
  121. // If not yet confirmed, display a confirmation message.
  122. if (!optional_param('confirm', '', PARAM_BOOL)) {
  123. $qtypename = $qtypes[$delete]->local_name();
  124. echo $OUTPUT->header();
  125. echo $OUTPUT->heading(get_string('deleteqtypeareyousure', 'question', $qtypename));
  126. echo $OUTPUT->confirm(get_string('deleteqtypeareyousuremessage', 'question', $qtypename),
  127. new moodle_url($thispageurl, array('delete' => $delete, 'confirm' => 1)),
  128. $thispageurl);
  129. echo $OUTPUT->footer();
  130. exit;
  131. }
  132. // Do the deletion.
  133. echo $OUTPUT->header();
  134. echo $OUTPUT->heading(get_string('deletingqtype', 'question', $qtypename));
  135. // Delete any configuration records.
  136. if (!unset_all_config_for_plugin('qtype_' . $delete)) {
  137. echo $OUTPUT->notification(get_string('errordeletingconfig', 'admin', 'qtype_' . $delete));
  138. }
  139. unset_config($delete . '_disabled', 'question');
  140. unset_config($delete . '_sortorder', 'question');
  141. // Then the tables themselves
  142. drop_plugin_tables($delete, $qtypes[$delete]->plugin_dir() . '/db/install.xml', false);
  143. // Remove event handlers and dequeue pending events
  144. events_uninstall('qtype_' . $delete);
  145. $a = new stdClass();
  146. $a->qtype = $qtypename;
  147. $a->directory = $qtypes[$delete]->plugin_dir();
  148. echo $OUTPUT->box(get_string('qtypedeletefiles', 'question', $a), 'generalbox', 'notice');
  149. echo $OUTPUT->continue_button($thispageurl);
  150. echo $OUTPUT->footer();
  151. exit;
  152. }
  153. // End of process actions ==================================================
  154. // Print the page heading.
  155. echo $OUTPUT->header();
  156. echo $OUTPUT->heading(get_string('manageqtypes', 'admin'));
  157. // Set up the table.
  158. $table = new flexible_table('qtypeadmintable');
  159. $table->define_baseurl($thispageurl);
  160. $table->define_columns(array('questiontype', 'numquestions', 'version', 'requires',
  161. 'availableto', 'delete', 'settings'));
  162. $table->define_headers(array(get_string('questiontype', 'question'), get_string('numquestions', 'question'),
  163. get_string('version'), get_string('requires', 'admin'), get_string('availableq', 'question'),
  164. get_string('delete'), get_string('settings')));
  165. $table->set_attribute('id', 'qtypes');
  166. $table->set_attribute('class', 'admintable generaltable');
  167. $table->setup();
  168. // Add a row for each question type.
  169. $createabletypes = question_bank::get_creatable_qtypes();
  170. foreach ($sortedqtypes as $qtypename => $localname) {
  171. $qtype = $qtypes[$qtypename];
  172. $row = array();
  173. // Question icon and name.
  174. $fakequestion = new stdClass;
  175. $fakequestion->qtype = $qtypename;
  176. $icon = print_question_icon($fakequestion, true);
  177. $row[] = $icon . ' ' . $localname;
  178. // Number of questions of this type.
  179. if ($counts[$qtypename]->numquestions + $counts[$qtypename]->numhidden > 0) {
  180. if ($counts[$qtypename]->numhidden > 0) {
  181. $strcount = get_string('numquestionsandhidden', 'question', $counts[$qtypename]);
  182. } else {
  183. $strcount = $counts[$qtypename]->numquestions;
  184. }
  185. if ($canviewreports) {
  186. $row[] = html_writer::link(new moodle_url('/report/questioninstances/index.php',
  187. array('qtype' => $qtypename)), $strcount, array('title' => get_string('showdetails', 'admin')));
  188. } else {
  189. $strcount;
  190. }
  191. } else {
  192. $row[] = 0;
  193. }
  194. // Question version number.
  195. $version = get_config('qtype_' . $qtypename, 'version');
  196. if ($version) {
  197. $row[] = $version;
  198. } else {
  199. $row[] = html_writer::tag('span', get_string('nodatabase', 'admin'), array('class' => 'disabled'));
  200. }
  201. // Other question types required by this one.
  202. $plugin = $pluginmanager->get_plugin_info($qtype->plugin_name());
  203. $requiredtypes = $plugin->get_other_required_plugins();
  204. $strtypes = array();
  205. if (!empty($requiredtypes)) {
  206. foreach ($requiredtypes as $required => $notused) {
  207. $strtypes[] = $pluginmanager->plugin_name($required);
  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, 't/hide',
  253. get_string('enabled', 'question'), get_string('disable'));
  254. } else {
  255. return question_type_icon_html('enable', $qtypename, 't/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' => '', 'class' => 'iconsmall')),
  264. null, array('title' => $tip));
  265. }