Merge branch 'MDL-40262' of git://github.com/kordan/moodle
[moodle.git] / admin / qtypes.php
blob3e865d14160ae489345ff531a2dae286246366ab
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Allows the admin to manage question types.
21 * @package moodlecore
22 * @subpackage questionbank
23 * @copyright 2008 Tim Hunt
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once(dirname(__FILE__) . '/../config.php');
29 require_once($CFG->libdir . '/questionlib.php');
30 require_once($CFG->libdir . '/adminlib.php');
31 require_once($CFG->libdir . '/pluginlib.php');
32 require_once($CFG->libdir . '/tablelib.php');
34 // Check permissions.
35 require_login();
36 $systemcontext = context_system::instance();
37 require_capability('moodle/question:config', $systemcontext);
38 $canviewreports = has_capability('report/questioninstances:view', $systemcontext);
40 admin_externalpage_setup('manageqtypes');
41 $thispageurl = new moodle_url('/admin/qtypes.php');
43 $qtypes = question_bank::get_all_qtypes();
44 $pluginmanager = plugin_manager::instance();
46 // Get some data we will need - question counts and which types are needed.
47 $counts = $DB->get_records_sql("
48 SELECT qtype, COUNT(1) as numquestions, SUM(hidden) as numhidden
49 FROM {question} GROUP BY qtype", array());
50 $needed = array();
51 foreach ($qtypes as $qtypename => $qtype) {
52 if (!isset($counts[$qtypename])) {
53 $counts[$qtypename] = new stdClass;
54 $counts[$qtypename]->numquestions = 0;
55 $counts[$qtypename]->numhidden = 0;
57 $needed[$qtypename] = $counts[$qtypename]->numquestions > 0 ||
58 $pluginmanager->other_plugins_that_require($qtype->plugin_name());
59 $counts[$qtypename]->numquestions -= $counts[$qtypename]->numhidden;
61 $needed['missingtype'] = true; // The system needs the missing question type.
62 foreach ($counts as $qtypename => $count) {
63 if (!isset($qtypes[$qtypename])) {
64 $counts['missingtype']->numquestions += $count->numquestions - $count->numhidden;
65 $counts['missingtype']->numhidden += $count->numhidden;
69 // Work of the correct sort order.
70 $config = get_config('question');
71 $sortedqtypes = array();
72 foreach ($qtypes as $qtypename => $qtype) {
73 $sortedqtypes[$qtypename] = $qtype->local_name();
75 $sortedqtypes = question_bank::sort_qtype_array($sortedqtypes, $config);
77 // Process actions ============================================================
79 // Disable.
80 if (($disable = optional_param('disable', '', PARAM_PLUGIN)) && confirm_sesskey()) {
81 if (!isset($qtypes[$disable])) {
82 print_error('unknownquestiontype', 'question', $thispageurl, $disable);
85 set_config($disable . '_disabled', 1, 'question');
86 redirect($thispageurl);
89 // Enable.
90 if (($enable = optional_param('enable', '', PARAM_PLUGIN)) && confirm_sesskey()) {
91 if (!isset($qtypes[$enable])) {
92 print_error('unknownquestiontype', 'question', $thispageurl, $enable);
95 if (!$qtypes[$enable]->menu_name()) {
96 print_error('cannotenable', 'question', $thispageurl, $enable);
99 unset_config($enable . '_disabled', 'question');
100 redirect($thispageurl);
103 // Move up in order.
104 if (($up = optional_param('up', '', PARAM_PLUGIN)) && confirm_sesskey()) {
105 if (!isset($qtypes[$up])) {
106 print_error('unknownquestiontype', 'question', $thispageurl, $up);
109 $neworder = question_reorder_qtypes($sortedqtypes, $up, -1);
110 question_save_qtype_order($neworder, $config);
111 redirect($thispageurl);
114 // Move down in order.
115 if (($down = optional_param('down', '', PARAM_PLUGIN)) && confirm_sesskey()) {
116 if (!isset($qtypes[$down])) {
117 print_error('unknownquestiontype', 'question', $thispageurl, $down);
120 $neworder = question_reorder_qtypes($sortedqtypes, $down, +1);
121 question_save_qtype_order($neworder, $config);
122 redirect($thispageurl);
125 // Delete.
126 if (($delete = optional_param('delete', '', PARAM_PLUGIN)) && confirm_sesskey()) {
127 // Check it is OK to delete this question type.
128 if ($delete == 'missingtype') {
129 print_error('cannotdeletemissingqtype', 'question', $thispageurl);
132 if (!isset($qtypes[$delete]) && !get_config('qtype_' . $delete, 'version')) {
133 print_error('unknownquestiontype', 'question', $thispageurl, $delete);
136 $qtypename = $qtypes[$delete]->local_name();
137 if ($counts[$delete]->numquestions + $counts[$delete]->numhidden > 0) {
138 print_error('cannotdeleteqtypeinuse', 'question', $thispageurl, $qtypename);
141 if ($needed[$delete] > 0) {
142 print_error('cannotdeleteqtypeneeded', 'question', $thispageurl, $qtypename);
145 // If not yet confirmed, display a confirmation message.
146 if (!optional_param('confirm', '', PARAM_BOOL)) {
147 $qtypename = $qtypes[$delete]->local_name();
148 echo $OUTPUT->header();
149 echo $OUTPUT->heading(get_string('deleteqtypeareyousure', 'question', $qtypename));
150 echo $OUTPUT->confirm(get_string('deleteqtypeareyousuremessage', 'question', $qtypename),
151 new moodle_url($thispageurl, array('delete' => $delete, 'confirm' => 1)),
152 $thispageurl);
153 echo $OUTPUT->footer();
154 exit;
157 // Do the deletion.
158 echo $OUTPUT->header();
159 echo $OUTPUT->heading(get_string('deletingqtype', 'question', $qtypename));
161 // Delete any questoin configuration records mentioning this plugin.
162 unset_config($delete . '_disabled', 'question');
163 unset_config($delete . '_sortorder', 'question');
165 // Then uninstall the plugin.
166 uninstall_plugin('qtype', $delete);
168 $a = new stdClass();
169 $a->qtype = $qtypename;
170 $a->directory = $qtypes[$delete]->plugin_dir();
171 echo $OUTPUT->box(get_string('qtypedeletefiles', 'question', $a), 'generalbox', 'notice');
172 echo $OUTPUT->continue_button($thispageurl);
173 echo $OUTPUT->footer();
174 exit;
177 // End of process actions ==================================================
179 // Print the page heading.
180 echo $OUTPUT->header();
181 echo $OUTPUT->heading(get_string('manageqtypes', 'admin'));
183 // Set up the table.
184 $table = new flexible_table('qtypeadmintable');
185 $table->define_baseurl($thispageurl);
186 $table->define_columns(array('questiontype', 'numquestions', 'version', 'requires',
187 'availableto', 'delete', 'settings'));
188 $table->define_headers(array(get_string('questiontype', 'question'), get_string('numquestions', 'question'),
189 get_string('version'), get_string('requires', 'admin'), get_string('availableq', 'question'),
190 get_string('delete'), get_string('settings')));
191 $table->set_attribute('id', 'qtypes');
192 $table->set_attribute('class', 'admintable generaltable');
193 $table->setup();
195 // Add a row for each question type.
196 $createabletypes = question_bank::get_creatable_qtypes();
197 foreach ($sortedqtypes as $qtypename => $localname) {
198 $qtype = $qtypes[$qtypename];
199 $row = array();
201 // Question icon and name.
202 $fakequestion = new stdClass;
203 $fakequestion->qtype = $qtypename;
204 $icon = print_question_icon($fakequestion, true);
205 $row[] = $icon . ' ' . $localname;
207 // Number of questions of this type.
208 if ($counts[$qtypename]->numquestions + $counts[$qtypename]->numhidden > 0) {
209 if ($counts[$qtypename]->numhidden > 0) {
210 $strcount = get_string('numquestionsandhidden', 'question', $counts[$qtypename]);
211 } else {
212 $strcount = $counts[$qtypename]->numquestions;
214 if ($canviewreports) {
215 $row[] = html_writer::link(new moodle_url('/report/questioninstances/index.php',
216 array('qtype' => $qtypename)), $strcount, array('title' => get_string('showdetails', 'admin')));
217 } else {
218 $strcount;
220 } else {
221 $row[] = 0;
224 // Question version number.
225 $version = get_config('qtype_' . $qtypename, 'version');
226 if ($version) {
227 $row[] = $version;
228 } else {
229 $row[] = html_writer::tag('span', get_string('nodatabase', 'admin'), array('class' => 'disabled'));
232 // Other question types required by this one.
233 $plugin = $pluginmanager->get_plugin_info($qtype->plugin_name());
234 $requiredtypes = $plugin->get_other_required_plugins();
235 $strtypes = array();
236 if (!empty($requiredtypes)) {
237 foreach ($requiredtypes as $required => $notused) {
238 $strtypes[] = $pluginmanager->plugin_name($required);
240 $row[] = implode(', ', $strtypes);
241 } else {
242 $row[] = '';
245 // Are people allowed to create new questions of this type?
246 $rowclass = '';
247 if ($qtype->menu_name()) {
248 $createable = isset($createabletypes[$qtypename]);
249 $icons = question_types_enable_disable_icons($qtypename, $createable);
250 if (!$createable) {
251 $rowclass = 'dimmed_text';
253 } else {
254 $icons = $OUTPUT->spacer();
257 // Move icons.
258 $icons .= question_type_icon_html('up', $qtypename, 't/up', get_string('up'), '');
259 $icons .= question_type_icon_html('down', $qtypename, 't/down', get_string('down'), '');
260 $row[] = $icons;
262 // Delete link, if available.
263 if ($needed[$qtypename]) {
264 $row[] = '';
265 } else {
266 $row[] = html_writer::link(new moodle_url($thispageurl,
267 array('delete' => $qtypename, 'sesskey' => sesskey())), get_string('delete'),
268 array('title' => get_string('uninstallqtype', 'question')));
271 // Settings link, if available.
272 $settings = admin_get_root()->locate('qtypesetting' . $qtypename);
273 if ($settings instanceof admin_externalpage) {
274 $row[] = html_writer::link($settings->url, get_string('settings'));
275 } else if ($settings instanceof admin_settingpage) {
276 $row[] = html_writer::link(new moodle_url('/admin/settings.php',
277 array('section' => 'qtypesetting' . $qtypename)), get_string('settings'));
278 } else {
279 $row[] = '';
282 $table->add_data($row, $rowclass);
285 $table->finish_output();
287 echo $OUTPUT->footer();
289 function question_types_enable_disable_icons($qtypename, $createable) {
290 if ($createable) {
291 return question_type_icon_html('disable', $qtypename, 't/hide',
292 get_string('enabled', 'question'), get_string('disable'));
293 } else {
294 return question_type_icon_html('enable', $qtypename, 't/show',
295 get_string('disabled', 'question'), get_string('enable'));
299 function question_type_icon_html($action, $qtypename, $icon, $alt, $tip) {
300 global $OUTPUT;
301 return $OUTPUT->action_icon(new moodle_url('/admin/qtypes.php',
302 array($action => $qtypename, 'sesskey' => sesskey())),
303 new pix_icon($icon, $alt, 'moodle', array('title' => '', 'class' => 'iconsmall')),
304 null, array('title' => $tip));