Automatically generated installer lang files
[moodle.git] / admin / qtypes.php
blobf469eac0e46e745f6f32e18f017c542b17f2bf8f
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(__DIR__ . '/../config.php');
29 require_once($CFG->libdir . '/questionlib.php');
30 require_once($CFG->libdir . '/adminlib.php');
31 require_once($CFG->libdir . '/tablelib.php');
33 admin_externalpage_setup('manageqtypes');
35 $systemcontext = context_system::instance();
36 require_capability('moodle/question:config', $systemcontext);
37 $canviewreports = has_capability('report/questioninstances:view', $systemcontext);
39 $thispageurl = new moodle_url('/admin/qtypes.php');
41 $qtypes = question_bank::get_all_qtypes();
42 $pluginmanager = core_plugin_manager::instance();
44 // Get some data we will need - question counts and which types are needed.
45 $hiddenstatus = \core_question\local\bank\question_version_status::QUESTION_STATUS_HIDDEN;
46 $draftstatus = \core_question\local\bank\question_version_status::QUESTION_STATUS_DRAFT;
48 $sql = "SELECT result.qtype,
49 SUM(result.numquestions) AS numquestions,
50 SUM(result.numhidden) AS numhidden,
51 SUM(result.numdraft) AS numdraft
52 FROM (SELECT data.qtype,
53 data.versionid,
54 COUNT(data.numquestions) AS numquestions,
55 (SELECT COUNT(qv.id)
56 FROM {question_versions} qv
57 WHERE qv.id = data.versionid
58 AND qv.status = :hiddenstatus) AS numhidden,
59 (SELECT COUNT(qv.id)
60 FROM {question_versions} qv
61 WHERE qv.id = data.versionid
62 AND qv.status = :draftstatus) AS numdraft
63 FROM (SELECT q.qtype, qv.id AS versionid, 1 AS numquestions
64 FROM {question} q
65 JOIN {question_versions} qv ON qv.questionid = q.id
66 JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
67 AND qv.version = (SELECT MAX(v.version)
68 FROM {question_versions} v
69 JOIN {question_bank_entries} be ON be.id = v.questionbankentryid
70 WHERE be.id = qbe.id)) data
71 GROUP BY data.qtype, data.versionid) result
72 GROUP BY result.qtype";
74 $counts = $DB->get_records_sql($sql, ['hiddenstatus' => $hiddenstatus, 'draftstatus' => $draftstatus]);
75 $needed = [];
76 foreach ($qtypes as $qtypename => $qtype) {
77 if (!isset($counts[$qtypename])) {
78 $counts[$qtypename] = new stdClass;
79 $counts[$qtypename]->numquestions = 0;
80 $counts[$qtypename]->numhidden = 0;
81 $counts[$qtypename]->numdraft = 0;
83 $needed[$qtypename] = $counts[$qtypename]->numquestions > 0 ||
84 $pluginmanager->other_plugins_that_require($qtype->plugin_name());
85 $counts[$qtypename]->numquestions -= ($counts[$qtypename]->numhidden + $counts[$qtypename]->numdraft);
87 $needed['missingtype'] = true; // The system needs the missing question type.
88 foreach ($counts as $qtypename => $count) {
89 if (!isset($qtypes[$qtypename])) {
90 $counts['missingtype']->numquestions += $count->numquestions - ($count->numhidden + $count->numdraft);
91 $counts['missingtype']->numhidden += $count->numhidden;
92 $counts['missingtype']->numdraft += $count->numdraft;
96 // Work of the correct sort order.
97 $config = get_config('question');
98 $sortedqtypes = array();
99 foreach ($qtypes as $qtypename => $qtype) {
100 $sortedqtypes[$qtypename] = $qtype->local_name();
102 $sortedqtypes = question_bank::sort_qtype_array($sortedqtypes, $config);
104 // Process actions ============================================================
106 // Disable.
107 if (($disable = optional_param('disable', '', PARAM_PLUGIN)) && confirm_sesskey()) {
108 if (!isset($qtypes[$disable])) {
109 print_error('unknownquestiontype', 'question', $thispageurl, $disable);
112 $class = \core_plugin_manager::resolve_plugininfo_class('qtype');
113 $class::enable_plugin($disable, false);
114 redirect($thispageurl);
117 // Enable.
118 if (($enable = optional_param('enable', '', PARAM_PLUGIN)) && confirm_sesskey()) {
119 if (!isset($qtypes[$enable])) {
120 print_error('unknownquestiontype', 'question', $thispageurl, $enable);
123 if (!$qtypes[$enable]->menu_name()) {
124 print_error('cannotenable', 'question', $thispageurl, $enable);
127 $class = \core_plugin_manager::resolve_plugininfo_class('qtype');
128 $class::enable_plugin($enable, true);
129 redirect($thispageurl);
132 // Move up in order.
133 if (($up = optional_param('up', '', PARAM_PLUGIN)) && confirm_sesskey()) {
134 if (!isset($qtypes[$up])) {
135 print_error('unknownquestiontype', 'question', $thispageurl, $up);
138 $neworder = question_reorder_qtypes($sortedqtypes, $up, -1);
139 question_save_qtype_order($neworder, $config);
140 redirect($thispageurl);
143 // Move down in order.
144 if (($down = optional_param('down', '', PARAM_PLUGIN)) && confirm_sesskey()) {
145 if (!isset($qtypes[$down])) {
146 print_error('unknownquestiontype', 'question', $thispageurl, $down);
149 $neworder = question_reorder_qtypes($sortedqtypes, $down, +1);
150 question_save_qtype_order($neworder, $config);
151 redirect($thispageurl);
154 // End of process actions ==================================================
156 // Print the page heading.
157 echo $OUTPUT->header();
158 echo $OUTPUT->heading(get_string('manageqtypes', 'admin'));
160 // Set up the table.
161 $table = new flexible_table('qtypeadmintable');
162 $table->define_baseurl($thispageurl);
163 $table->define_columns(array('questiontype', 'numquestions', 'version', 'requires',
164 'availableto', 'uninstall', 'settings'));
165 $table->define_headers(array(get_string('questiontype', 'question'), get_string('numquestions', 'question'),
166 get_string('version'), get_string('requires', 'admin'), get_string('availableq', 'question'),
167 get_string('settings'), get_string('uninstallplugin', 'core_admin')));
168 $table->set_attribute('id', 'qtypes');
169 $table->set_attribute('class', 'admintable generaltable');
170 $table->setup();
172 // Add a row for each question type.
173 $createabletypes = question_bank::get_creatable_qtypes();
174 foreach ($sortedqtypes as $qtypename => $localname) {
175 $qtype = $qtypes[$qtypename];
176 $row = array();
178 // Question icon and name.
179 $fakequestion = new stdClass;
180 $fakequestion->qtype = $qtypename;
181 $icon = print_question_icon($fakequestion, true);
182 $row[] = $icon . ' ' . $localname;
184 // Number of questions of this type.
185 if ($counts[$qtypename]->numquestions + $counts[$qtypename]->numhidden + $counts[$qtypename]->numdraft > 0) {
186 if ($counts[$qtypename]->numhidden + $counts[$qtypename]->numdraft > 0) {
187 $strcount = get_string('numquestionsandhidden', 'question', $counts[$qtypename]);
188 } else {
189 $strcount = $counts[$qtypename]->numquestions;
191 if ($canviewreports) {
192 $row[] = html_writer::link(new moodle_url('/report/questioninstances/index.php',
193 array('qtype' => $qtypename)), $strcount, array('title' => get_string('showdetails', 'admin')));
194 } else {
195 $strcount;
197 } else {
198 $row[] = 0;
201 // Question version number.
202 $version = get_config('qtype_' . $qtypename, 'version');
203 if ($version) {
204 $row[] = $version;
205 } else {
206 $row[] = html_writer::tag('span', get_string('nodatabase', 'admin'), array('class' => 'text-muted'));
209 // Other question types required by this one.
210 $plugin = $pluginmanager->get_plugin_info($qtype->plugin_name());
211 $requiredtypes = $plugin->get_other_required_plugins();
212 $strtypes = array();
213 if (!empty($requiredtypes)) {
214 foreach ($requiredtypes as $required => $notused) {
215 $strtypes[] = $pluginmanager->plugin_name($required);
217 $row[] = implode(', ', $strtypes);
218 } else {
219 $row[] = '';
222 // Are people allowed to create new questions of this type?
223 $rowclass = '';
224 if ($qtype->menu_name()) {
225 $createable = isset($createabletypes[$qtypename]);
226 $icons = question_types_enable_disable_icons($qtypename, $createable);
227 if (!$createable) {
228 $rowclass = 'dimmed_text';
230 } else {
231 $icons = $OUTPUT->spacer();
234 // Move icons.
235 $icons .= question_type_icon_html('up', $qtypename, 't/up', get_string('up'), '');
236 $icons .= question_type_icon_html('down', $qtypename, 't/down', get_string('down'), '');
237 $row[] = $icons;
239 // Settings link, if available.
240 $settings = admin_get_root()->locate('qtypesetting' . $qtypename);
241 if ($settings instanceof admin_externalpage) {
242 $row[] = html_writer::link($settings->url, get_string('settings'));
243 } else if ($settings instanceof admin_settingpage) {
244 $row[] = html_writer::link(new moodle_url('/admin/settings.php',
245 array('section' => 'qtypesetting' . $qtypename)), get_string('settings'));
246 } else {
247 $row[] = '';
250 // Uninstall link, if available.
251 if ($needed[$qtypename]) {
252 $row[] = '';
253 } else {
254 $uninstallurl = core_plugin_manager::instance()->get_uninstall_url('qtype_'.$qtypename, 'manage');
255 if ($uninstallurl) {
256 $row[] = html_writer::link($uninstallurl, get_string('uninstallplugin', 'core_admin'),
257 array('title' => get_string('uninstallqtype', 'question')));
261 $table->add_data($row, $rowclass);
264 $table->finish_output();
266 echo $OUTPUT->footer();
268 function question_types_enable_disable_icons($qtypename, $createable) {
269 if ($createable) {
270 return question_type_icon_html('disable', $qtypename, 't/hide',
271 get_string('enabled', 'question'), get_string('disable'));
272 } else {
273 return question_type_icon_html('enable', $qtypename, 't/show',
274 get_string('disabled', 'question'), get_string('enable'));
278 function question_type_icon_html($action, $qtypename, $icon, $alt, $tip) {
279 global $OUTPUT;
280 return $OUTPUT->action_icon(new moodle_url('/admin/qtypes.php',
281 array($action => $qtypename, 'sesskey' => sesskey())),
282 new pix_icon($icon, $alt, 'moodle', array('title' => '', 'class' => 'iconsmall')),
283 null, array('title' => $tip));