Merge branch 'wip-mdl-30993' of git://github.com/rajeshtaneja/moodle
[moodle.git] / admin / qtypes.php
blobcbe828fdf1092c0e97320e10b58534289acadd93
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 = get_context_instance(CONTEXT_SYSTEM);
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])) {
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 configuration records.
162 if (!unset_all_config_for_plugin('qtype_' . $delete)) {
163 echo $OUTPUT->notification(get_string('errordeletingconfig', 'admin', 'qtype_' . $delete));
165 unset_config($delete . '_disabled', 'question');
166 unset_config($delete . '_sortorder', 'question');
168 // Then the tables themselves
169 drop_plugin_tables($delete, $qtypes[$delete]->plugin_dir() . '/db/install.xml', false);
171 // Remove event handlers and dequeue pending events
172 events_uninstall('qtype_' . $delete);
174 $a = new stdClass();
175 $a->qtype = $qtypename;
176 $a->directory = $qtypes[$delete]->plugin_dir();
177 echo $OUTPUT->box(get_string('qtypedeletefiles', 'question', $a), 'generalbox', 'notice');
178 echo $OUTPUT->continue_button($thispageurl);
179 echo $OUTPUT->footer();
180 exit;
183 // End of process actions ==================================================
185 // Print the page heading.
186 echo $OUTPUT->header();
187 echo $OUTPUT->heading(get_string('manageqtypes', 'admin'));
189 // Set up the table.
190 $table = new flexible_table('qtypeadmintable');
191 $table->define_baseurl($thispageurl);
192 $table->define_columns(array('questiontype', 'numquestions', 'version', 'requires',
193 'availableto', 'delete', 'settings'));
194 $table->define_headers(array(get_string('questiontype', 'question'), get_string('numquestions', 'question'),
195 get_string('version'), get_string('requires', 'admin'), get_string('availableq', 'question'),
196 get_string('delete'), get_string('settings')));
197 $table->set_attribute('id', 'qtypes');
198 $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
199 $table->setup();
201 // Add a row for each question type.
202 $createabletypes = question_bank::get_creatable_qtypes();
203 foreach ($sortedqtypes as $qtypename => $localname) {
204 $qtype = $qtypes[$qtypename];
205 $row = array();
207 // Question icon and name.
208 $fakequestion = new stdClass;
209 $fakequestion->qtype = $qtypename;
210 $icon = print_question_icon($fakequestion, true);
211 $row[] = $icon . ' ' . $localname;
213 // Number of questions of this type.
214 if ($counts[$qtypename]->numquestions + $counts[$qtypename]->numhidden > 0) {
215 if ($counts[$qtypename]->numhidden > 0) {
216 $strcount = get_string('numquestionsandhidden', 'question', $counts[$qtypename]);
217 } else {
218 $strcount = $counts[$qtypename]->numquestions;
220 if ($canviewreports) {
221 $row[] = html_writer::link(new moodle_url('/report/questioninstances/index.php',
222 array('qtype' => $qtypename)), $strcount, array('title' => get_string('showdetails', 'admin')));
223 } else {
224 $strcount;
226 } else {
227 $row[] = 0;
230 // Question version number.
231 $version = get_config('qtype_' . $qtypename, 'version');
232 if ($version) {
233 $row[] = $version;
234 } else {
235 $row[] = html_writer::tag('span', get_string('nodatabase', 'admin'), array('class' => 'disabled'));
238 // Other question types required by this one.
239 $plugin = $pluginmanager->get_plugin_info($qtype->plugin_name());
240 $requiredtypes = $plugin->get_other_required_plugins();
241 $strtypes = array();
242 if (!empty($requiredtypes)) {
243 foreach ($requiredtypes as $required => $notused) {
244 $strtypes[] = $pluginmanager->plugin_name($required);
246 $row[] = implode(', ', $strtypes);
247 } else {
248 $row[] = '';
251 // Are people allowed to create new questions of this type?
252 $rowclass = '';
253 if ($qtype->menu_name()) {
254 $createable = isset($createabletypes[$qtypename]);
255 $icons = question_types_enable_disable_icons($qtypename, $createable);
256 if (!$createable) {
257 $rowclass = 'dimmed_text';
259 } else {
260 $icons = $OUTPUT->spacer() . ' ';
263 // Move icons.
264 $icons .= question_type_icon_html('up', $qtypename, 't/up', get_string('up'), '');
265 $icons .= question_type_icon_html('down', $qtypename, 't/down', get_string('down'), '');
266 $row[] = $icons;
268 // Delete link, if available.
269 if ($needed[$qtypename]) {
270 $row[] = '';
271 } else {
272 $row[] = html_writer::link(new moodle_url($thispageurl,
273 array('delete' => $qtypename, 'sesskey' => sesskey())), get_string('delete'),
274 array('title' => get_string('uninstallqtype', 'question')));
277 // Settings link, if available.
278 $settings = admin_get_root()->locate('qtypesetting' . $qtypename);
279 if ($settings instanceof admin_externalpage) {
280 $row[] = html_writer::link($settings->url, get_string('settings'));
281 } else if ($settings instanceof admin_settingpage) {
282 $row[] = html_writer::link(new moodle_url('/admin/settings.php',
283 array('section' => 'qtypesetting' . $qtypename)), get_string('settings'));
284 } else {
285 $row[] = '';
288 $table->add_data($row, $rowclass);
291 $table->finish_output();
293 echo $OUTPUT->footer();
295 function question_types_enable_disable_icons($qtypename, $createable) {
296 if ($createable) {
297 return question_type_icon_html('disable', $qtypename, 'i/hide',
298 get_string('enabled', 'question'), get_string('disable'));
299 } else {
300 return question_type_icon_html('enable', $qtypename, 'i/show',
301 get_string('disabled', 'question'), get_string('enable'));
305 function question_type_icon_html($action, $qtypename, $icon, $alt, $tip) {
306 global $OUTPUT;
307 return $OUTPUT->action_icon(new moodle_url('/admin/qtypes.php',
308 array($action => $qtypename, 'sesskey' => sesskey())),
309 new pix_icon($icon, $alt, 'moodle', array('title' => '')),
310 null, array('title' => $tip)) . ' ';