Moodle release 2.7.19
[moodle.git] / admin / qtypes.php
blob80c6c6d76d2552f782834d106f7136289aeb70b0
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 = context_system::instance();
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();
43 $pluginmanager = core_plugin_manager::instance();
45 // Get some data we will need - question counts and which types are needed.
46 $counts = $DB->get_records_sql("
47 SELECT qtype, COUNT(1) as numquestions, SUM(hidden) as numhidden
48 FROM {question} GROUP BY qtype", array());
49 $needed = array();
50 foreach ($qtypes as $qtypename => $qtype) {
51 if (!isset($counts[$qtypename])) {
52 $counts[$qtypename] = new stdClass;
53 $counts[$qtypename]->numquestions = 0;
54 $counts[$qtypename]->numhidden = 0;
56 $needed[$qtypename] = $counts[$qtypename]->numquestions > 0 ||
57 $pluginmanager->other_plugins_that_require($qtype->plugin_name());
58 $counts[$qtypename]->numquestions -= $counts[$qtypename]->numhidden;
60 $needed['missingtype'] = true; // The system needs the missing question type.
61 foreach ($counts as $qtypename => $count) {
62 if (!isset($qtypes[$qtypename])) {
63 $counts['missingtype']->numquestions += $count->numquestions - $count->numhidden;
64 $counts['missingtype']->numhidden += $count->numhidden;
68 // Work of the correct sort order.
69 $config = get_config('question');
70 $sortedqtypes = array();
71 foreach ($qtypes as $qtypename => $qtype) {
72 $sortedqtypes[$qtypename] = $qtype->local_name();
74 $sortedqtypes = question_bank::sort_qtype_array($sortedqtypes, $config);
76 // Process actions ============================================================
78 // Disable.
79 if (($disable = optional_param('disable', '', PARAM_PLUGIN)) && confirm_sesskey()) {
80 if (!isset($qtypes[$disable])) {
81 print_error('unknownquestiontype', 'question', $thispageurl, $disable);
84 set_config($disable . '_disabled', 1, 'question');
85 redirect($thispageurl);
88 // Enable.
89 if (($enable = optional_param('enable', '', PARAM_PLUGIN)) && confirm_sesskey()) {
90 if (!isset($qtypes[$enable])) {
91 print_error('unknownquestiontype', 'question', $thispageurl, $enable);
94 if (!$qtypes[$enable]->menu_name()) {
95 print_error('cannotenable', 'question', $thispageurl, $enable);
98 unset_config($enable . '_disabled', 'question');
99 redirect($thispageurl);
102 // Move up in order.
103 if (($up = optional_param('up', '', PARAM_PLUGIN)) && confirm_sesskey()) {
104 if (!isset($qtypes[$up])) {
105 print_error('unknownquestiontype', 'question', $thispageurl, $up);
108 $neworder = question_reorder_qtypes($sortedqtypes, $up, -1);
109 question_save_qtype_order($neworder, $config);
110 redirect($thispageurl);
113 // Move down in order.
114 if (($down = optional_param('down', '', PARAM_PLUGIN)) && confirm_sesskey()) {
115 if (!isset($qtypes[$down])) {
116 print_error('unknownquestiontype', 'question', $thispageurl, $down);
119 $neworder = question_reorder_qtypes($sortedqtypes, $down, +1);
120 question_save_qtype_order($neworder, $config);
121 redirect($thispageurl);
124 // End of process actions ==================================================
126 // Print the page heading.
127 echo $OUTPUT->header();
128 echo $OUTPUT->heading(get_string('manageqtypes', 'admin'));
130 // Set up the table.
131 $table = new flexible_table('qtypeadmintable');
132 $table->define_baseurl($thispageurl);
133 $table->define_columns(array('questiontype', 'numquestions', 'version', 'requires',
134 'availableto', 'uninstall', 'settings'));
135 $table->define_headers(array(get_string('questiontype', 'question'), get_string('numquestions', 'question'),
136 get_string('version'), get_string('requires', 'admin'), get_string('availableq', 'question'),
137 get_string('settings'), get_string('uninstallplugin', 'core_admin')));
138 $table->set_attribute('id', 'qtypes');
139 $table->set_attribute('class', 'admintable generaltable');
140 $table->setup();
142 // Add a row for each question type.
143 $createabletypes = question_bank::get_creatable_qtypes();
144 foreach ($sortedqtypes as $qtypename => $localname) {
145 $qtype = $qtypes[$qtypename];
146 $row = array();
148 // Question icon and name.
149 $fakequestion = new stdClass;
150 $fakequestion->qtype = $qtypename;
151 $icon = print_question_icon($fakequestion, true);
152 $row[] = $icon . ' ' . $localname;
154 // Number of questions of this type.
155 if ($counts[$qtypename]->numquestions + $counts[$qtypename]->numhidden > 0) {
156 if ($counts[$qtypename]->numhidden > 0) {
157 $strcount = get_string('numquestionsandhidden', 'question', $counts[$qtypename]);
158 } else {
159 $strcount = $counts[$qtypename]->numquestions;
161 if ($canviewreports) {
162 $row[] = html_writer::link(new moodle_url('/report/questioninstances/index.php',
163 array('qtype' => $qtypename)), $strcount, array('title' => get_string('showdetails', 'admin')));
164 } else {
165 $strcount;
167 } else {
168 $row[] = 0;
171 // Question version number.
172 $version = get_config('qtype_' . $qtypename, 'version');
173 if ($version) {
174 $row[] = $version;
175 } else {
176 $row[] = html_writer::tag('span', get_string('nodatabase', 'admin'), array('class' => 'disabled'));
179 // Other question types required by this one.
180 $plugin = $pluginmanager->get_plugin_info($qtype->plugin_name());
181 $requiredtypes = $plugin->get_other_required_plugins();
182 $strtypes = array();
183 if (!empty($requiredtypes)) {
184 foreach ($requiredtypes as $required => $notused) {
185 $strtypes[] = $pluginmanager->plugin_name($required);
187 $row[] = implode(', ', $strtypes);
188 } else {
189 $row[] = '';
192 // Are people allowed to create new questions of this type?
193 $rowclass = '';
194 if ($qtype->menu_name()) {
195 $createable = isset($createabletypes[$qtypename]);
196 $icons = question_types_enable_disable_icons($qtypename, $createable);
197 if (!$createable) {
198 $rowclass = 'dimmed_text';
200 } else {
201 $icons = $OUTPUT->spacer();
204 // Move icons.
205 $icons .= question_type_icon_html('up', $qtypename, 't/up', get_string('up'), '');
206 $icons .= question_type_icon_html('down', $qtypename, 't/down', get_string('down'), '');
207 $row[] = $icons;
209 // Settings link, if available.
210 $settings = admin_get_root()->locate('qtypesetting' . $qtypename);
211 if ($settings instanceof admin_externalpage) {
212 $row[] = html_writer::link($settings->url, get_string('settings'));
213 } else if ($settings instanceof admin_settingpage) {
214 $row[] = html_writer::link(new moodle_url('/admin/settings.php',
215 array('section' => 'qtypesetting' . $qtypename)), get_string('settings'));
216 } else {
217 $row[] = '';
220 // Uninstall link, if available.
221 if ($needed[$qtypename]) {
222 $row[] = '';
223 } else {
224 $uninstallurl = core_plugin_manager::instance()->get_uninstall_url('qtype_'.$qtypename, 'manage');
225 if ($uninstallurl) {
226 $row[] = html_writer::link($uninstallurl, get_string('uninstallplugin', 'core_admin'),
227 array('title' => get_string('uninstallqtype', 'question')));
231 $table->add_data($row, $rowclass);
234 $table->finish_output();
236 echo $OUTPUT->footer();
238 function question_types_enable_disable_icons($qtypename, $createable) {
239 if ($createable) {
240 return question_type_icon_html('disable', $qtypename, 't/hide',
241 get_string('enabled', 'question'), get_string('disable'));
242 } else {
243 return question_type_icon_html('enable', $qtypename, 't/show',
244 get_string('disabled', 'question'), get_string('enable'));
248 function question_type_icon_html($action, $qtypename, $icon, $alt, $tip) {
249 global $OUTPUT;
250 return $OUTPUT->action_icon(new moodle_url('/admin/qtypes.php',
251 array($action => $qtypename, 'sesskey' => sesskey())),
252 new pix_icon($icon, $alt, 'moodle', array('title' => '', 'class' => 'iconsmall')),
253 null, array('title' => $tip));