MDL-31651 course/info: Fix hardcoded string
[moodle.git] / admin / qtypes.php
blobfcae6cf4856e486ebf554a2039c7540a099f1658
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 . '/tablelib.php');
33 // Check permissions.
34 require_login();
35 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
36 require_capability('moodle/question:config', $systemcontext);
37 $canviewreports = has_capability('report/questioninstances:view', $systemcontext);
39 admin_externalpage_setup('manageqtypes');
40 $thispageurl = new moodle_url('/admin/qtypes.php');
42 $qtypes = question_bank::get_all_qtypes();
44 // Get some data we will need - question counts and which types are needed.
45 $counts = $DB->get_records_sql("
46 SELECT qtype, COUNT(1) as numquestions, SUM(hidden) as numhidden
47 FROM {question} GROUP BY qtype", array());
48 $needed = array();
49 foreach ($qtypes as $qtypename => $qtype) {
50 if (!isset($counts[$qtypename])) {
51 $counts[$qtypename] = new stdClass;
52 $counts[$qtypename]->numquestions = 0;
53 $counts[$qtypename]->numhidden = 0;
55 $needed[$qtypename] = $counts[$qtypename]->numquestions > 0;
56 $counts[$qtypename]->numquestions -= $counts[$qtypename]->numhidden;
58 $needed['missingtype'] = true; // The system needs the missing question type.
59 foreach ($qtypes as $qtypename => $qtype) {
60 foreach ($qtype->requires_qtypes() as $reqtype) {
61 $needed[$reqtype] = true;
64 foreach ($counts as $qtypename => $count) {
65 if (!isset($qtypes[$qtypename])) {
66 $counts['missingtype']->numquestions += $count->numquestions - $count->numhidden;
67 $counts['missingtype']->numhidden += $count->numhidden;
71 // Work of the correct sort order.
72 $config = get_config('question');
73 $sortedqtypes = array();
74 foreach ($qtypes as $qtypename => $qtype) {
75 $sortedqtypes[$qtypename] = $qtype->local_name();
77 $sortedqtypes = question_bank::sort_qtype_array($sortedqtypes, $config);
79 // Process actions ============================================================
81 // Disable.
82 if (($disable = optional_param('disable', '', PARAM_SAFEDIR)) && confirm_sesskey()) {
83 if (!isset($qtypes[$disable])) {
84 print_error('unknownquestiontype', 'question', $thispageurl, $disable);
87 set_config($disable . '_disabled', 1, 'question');
88 redirect($thispageurl);
91 // Enable.
92 if (($enable = optional_param('enable', '', PARAM_SAFEDIR)) && confirm_sesskey()) {
93 if (!isset($qtypes[$enable])) {
94 print_error('unknownquestiontype', 'question', $thispageurl, $enable);
97 if (!$qtypes[$enable]->menu_name()) {
98 print_error('cannotenable', 'question', $thispageurl, $enable);
101 unset_config($enable . '_disabled', 'question');
102 redirect($thispageurl);
105 // Move up in order.
106 if (($up = optional_param('up', '', PARAM_SAFEDIR)) && confirm_sesskey()) {
107 if (!isset($qtypes[$up])) {
108 print_error('unknownquestiontype', 'question', $thispageurl, $up);
111 $neworder = question_reorder_qtypes($sortedqtypes, $up, -1);
112 question_save_qtype_order($neworder, $config);
113 redirect($thispageurl);
116 // Move down in order.
117 if (($down = optional_param('down', '', PARAM_SAFEDIR)) && confirm_sesskey()) {
118 if (!isset($qtypes[$down])) {
119 print_error('unknownquestiontype', 'question', $thispageurl, $down);
122 $neworder = question_reorder_qtypes($sortedqtypes, $down, +1);
123 question_save_qtype_order($neworder, $config);
124 redirect($thispageurl);
127 // Delete.
128 if (($delete = optional_param('delete', '', PARAM_SAFEDIR)) && confirm_sesskey()) {
129 // Check it is OK to delete this question type.
130 if ($delete == 'missingtype') {
131 print_error('cannotdeletemissingqtype', 'question', $thispageurl);
134 if (!isset($qtypes[$delete])) {
135 print_error('unknownquestiontype', 'question', $thispageurl, $delete);
138 $qtypename = $qtypes[$delete]->local_name();
139 if ($counts[$delete]->numquestions + $counts[$delete]->numhidden > 0) {
140 print_error('cannotdeleteqtypeinuse', 'question', $thispageurl, $qtypename);
143 if ($needed[$delete] > 0) {
144 print_error('cannotdeleteqtypeneeded', 'question', $thispageurl, $qtypename);
147 // If not yet confirmed, display a confirmation message.
148 if (!optional_param('confirm', '', PARAM_BOOL)) {
149 $qtypename = $qtypes[$delete]->local_name();
150 echo $OUTPUT->header();
151 echo $OUTPUT->heading(get_string('deleteqtypeareyousure', 'question', $qtypename));
152 echo $OUTPUT->confirm(get_string('deleteqtypeareyousuremessage', 'question', $qtypename),
153 new moodle_url($thispageurl, array('delete' => $delete, 'confirm' => 1)),
154 $thispageurl);
155 echo $OUTPUT->footer();
156 exit;
159 // Do the deletion.
160 echo $OUTPUT->header();
161 echo $OUTPUT->heading(get_string('deletingqtype', 'question', $qtypename));
163 // Delete any configuration records.
164 if (!unset_all_config_for_plugin('qtype_' . $delete)) {
165 echo $OUTPUT->notification(get_string('errordeletingconfig', 'admin', 'qtype_' . $delete));
167 unset_config($delete . '_disabled', 'question');
168 unset_config($delete . '_sortorder', 'question');
170 // Then the tables themselves
171 drop_plugin_tables($delete, $qtypes[$delete]->plugin_dir() . '/db/install.xml', false);
173 // Remove event handlers and dequeue pending events
174 events_uninstall('qtype_' . $delete);
176 $a = new stdClass();
177 $a->qtype = $qtypename;
178 $a->directory = $qtypes[$delete]->plugin_dir();
179 echo $OUTPUT->box(get_string('qtypedeletefiles', 'question', $a), 'generalbox', 'notice');
180 echo $OUTPUT->continue_button($thispageurl);
181 echo $OUTPUT->footer();
182 exit;
185 // End of process actions ==================================================
187 // Print the page heading.
188 echo $OUTPUT->header();
189 echo $OUTPUT->heading(get_string('manageqtypes', 'admin'));
191 // Set up the table.
192 $table = new flexible_table('qtypeadmintable');
193 $table->define_baseurl($thispageurl);
194 $table->define_columns(array('questiontype', 'numquestions', 'version', 'requires',
195 'availableto', 'delete', 'settings'));
196 $table->define_headers(array(get_string('questiontype', 'question'), get_string('numquestions', 'question'),
197 get_string('version'), get_string('requires', 'admin'), get_string('availableq', 'question'),
198 get_string('delete'), get_string('settings')));
199 $table->set_attribute('id', 'qtypes');
200 $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
201 $table->setup();
203 // Add a row for each question type.
204 $createabletypes = question_bank::get_creatable_qtypes();
205 foreach ($sortedqtypes as $qtypename => $localname) {
206 $qtype = $qtypes[$qtypename];
207 $row = array();
209 // Question icon and name.
210 $fakequestion = new stdClass;
211 $fakequestion->qtype = $qtypename;
212 $icon = print_question_icon($fakequestion, true);
213 $row[] = $icon . ' ' . $localname;
215 // Number of questions of this type.
216 if ($counts[$qtypename]->numquestions + $counts[$qtypename]->numhidden > 0) {
217 if ($counts[$qtypename]->numhidden > 0) {
218 $strcount = get_string('numquestionsandhidden', 'question', $counts[$qtypename]);
219 } else {
220 $strcount = $counts[$qtypename]->numquestions;
222 if ($canviewreports) {
223 $row[] = html_writer::link(new moodle_url('/admin/report/questioninstances/index.php',
224 array('qtype' => $qtypename)), $strcount, array('title' => get_string('showdetails', 'admin')));
225 } else {
226 $strcount;
228 } else {
229 $row[] = 0;
232 // Question version number.
233 $version = get_config('qtype_' . $qtypename, 'version');
234 if ($version) {
235 $row[] = $version;
236 } else {
237 $row[] = html_writer::tag('span', get_string('nodatabase', 'admin'), array('class' => 'disabled'));
240 // Other question types required by this one.
241 $requiredtypes = $qtype->requires_qtypes();
242 $strtypes = array();
243 if (!empty($requiredtypes)) {
244 foreach ($requiredtypes as $required) {
245 $strtypes[] = $qtypes[$required]->local_name();
247 $row[] = implode(', ', $strtypes);
248 } else {
249 $row[] = '';
252 // Are people allowed to create new questions of this type?
253 $rowclass = '';
254 if ($qtype->menu_name()) {
255 $createable = isset($createabletypes[$qtypename]);
256 $icons = question_types_enable_disable_icons($qtypename, $createable);
257 if (!$createable) {
258 $rowclass = 'dimmed_text';
260 } else {
261 $icons = $OUTPUT->spacer() . ' ';
264 // Move icons.
265 $icons .= question_type_icon_html('up', $qtypename, 't/up', get_string('up'), '');
266 $icons .= question_type_icon_html('down', $qtypename, 't/down', get_string('down'), '');
267 $row[] = $icons;
269 // Delete link, if available.
270 if ($needed[$qtypename]) {
271 $row[] = '';
272 } else {
273 $row[] = html_writer::link(new moodle_url($thispageurl,
274 array('delete' => $qtypename, 'sesskey' => sesskey())), get_string('delete'),
275 array('title' => get_string('uninstallqtype', 'question')));
278 // Settings link, if available.
279 $settings = admin_get_root()->locate('qtypesetting' . $qtypename);
280 if ($settings instanceof admin_externalpage) {
281 $row[] = html_writer::link($settings->url, get_string('settings'));
282 } else if ($settings instanceof admin_settingpage) {
283 $row[] = html_writer::link(new moodle_url('/admin/settings.php',
284 array('section' => 'qtypesetting' . $qtypename)), get_string('settings'));
285 } else {
286 $row[] = '';
289 $table->add_data($row, $rowclass);
292 $table->finish_output();
294 echo $OUTPUT->footer();
296 function question_types_enable_disable_icons($qtypename, $createable) {
297 if ($createable) {
298 return question_type_icon_html('disable', $qtypename, 'i/hide',
299 get_string('enabled', 'question'), get_string('disable'));
300 } else {
301 return question_type_icon_html('enable', $qtypename, 'i/show',
302 get_string('disabled', 'question'), get_string('enable'));
306 function question_type_icon_html($action, $qtypename, $icon, $alt, $tip) {
307 global $OUTPUT;
308 return $OUTPUT->action_icon(new moodle_url('/admin/qtypes.php',
309 array($action => $qtypename, 'sesskey' => sesskey())),
310 new pix_icon($icon, $alt, 'moodle', array('title' => '')),
311 null, array('title' => $tip)) . ' ';