3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * Allows the admin to manage question types.
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');
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());
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 ============================================================
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);
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);
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);
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)),
155 echo $OUTPUT->footer();
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->qtype
= $qtypename;
177 $a->directory
= $qtypes[$delete]->plugin_dir();
178 echo $OUTPUT->box(get_string('qtypedeletefiles', 'question', $a), 'generalbox', 'notice');
179 echo $OUTPUT->continue_button($thispageurl);
180 echo $OUTPUT->footer();
184 // End of process actions ==================================================
186 // Print the page heading.
187 echo $OUTPUT->header();
188 echo $OUTPUT->heading(get_string('manageqtypes', 'admin'));
191 $table = new flexible_table('qtypeadmintable');
192 $table->define_baseurl($thispageurl);
193 $table->define_columns(array('questiontype', 'numquestions', 'version', 'requires',
194 'availableto', 'delete', 'settings'));
195 $table->define_headers(array(get_string('questiontype', 'question'), get_string('numquestions', 'question'),
196 get_string('version'), get_string('requires', 'admin'), get_string('availableq', 'question'),
197 get_string('delete'), get_string('settings')));
198 $table->set_attribute('id', 'qtypes');
199 $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
202 // Add a row for each question type.
203 $createabletypes = question_bank
::get_creatable_qtypes();
204 foreach ($sortedqtypes as $qtypename => $localname) {
205 $qtype = $qtypes[$qtypename];
208 // Question icon and name.
209 $fakequestion = new stdClass
;
210 $fakequestion->qtype
= $qtypename;
211 $icon = print_question_icon($fakequestion, true);
212 $row[] = $icon . ' ' . $localname;
214 // Number of questions of this type.
215 if ($counts[$qtypename]->numquestions +
$counts[$qtypename]->numhidden
> 0) {
216 if ($counts[$qtypename]->numhidden
> 0) {
217 $strcount = get_string('numquestionsandhidden', 'question', $counts[$qtypename]);
219 $strcount = $counts[$qtypename]->numquestions
;
221 if ($canviewreports) {
222 $row[] = html_writer
::link(new moodle_url('/admin/report/questioninstances/index.php',
223 array('qtype' => $qtypename)), $strcount, array('title' => get_string('showdetails', 'admin')));
231 // Question version number.
232 $version = get_config('qtype_' . $qtypename, 'version');
236 $row[] = html_writer
::tag('span', get_string('nodatabase', 'admin'), array('class' => 'disabled'));
239 // Other question types required by this one.
240 $requiredtypes = $qtype->requires_qtypes();
242 if (!empty($requiredtypes)) {
243 foreach ($requiredtypes as $required) {
244 $strtypes[] = $qtypes[$required]->local_name();
246 $row[] = implode(', ', $strtypes);
251 // Are people allowed to create new questions of this type?
253 if ($qtype->menu_name()) {
254 $createable = isset($createabletypes[$qtypename]);
255 $icons = question_types_enable_disable_icons($qtypename, $createable);
257 $rowclass = 'dimmed_text';
260 $icons = $OUTPUT->spacer() . ' ';
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'), '');
268 // Delete link, if available.
269 if ($needed[$qtypename]) {
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'));
288 $table->add_data($row, $rowclass);
291 $table->finish_output();
293 echo $OUTPUT->footer();
295 function question_types_enable_disable_icons($qtypename, $createable) {
297 return question_type_icon_html('disable', $qtypename, 'i/hide',
298 get_string('enabled', 'question'), get_string('disable'));
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) {
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)) . ' ';