MDL-41311 tool_generator: remove legacy code
[moodle.git] / admin / qbehaviours.php
blob85f0c8ef9053d204d1970251dde81475427be6d5
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 behaviours.
21 * @package moodlecore
22 * @subpackage questionengine
23 * @copyright 2011 The Open University
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 = context_system::instance();
37 require_capability('moodle/question:config', $systemcontext);
39 admin_externalpage_setup('manageqbehaviours');
40 $thispageurl = new moodle_url('/admin/qbehaviours.php');
42 $behaviours = core_component::get_plugin_list('qbehaviour');
43 $pluginmanager = plugin_manager::instance();
45 // Get some data we will need - question counts and which types are needed.
46 $counts = $DB->get_records_sql_menu("
47 SELECT behaviour, COUNT(1)
48 FROM {question_attempts} GROUP BY behaviour");
49 $needed = array();
50 $archetypal = array();
51 foreach ($behaviours as $behaviour => $notused) {
52 if (!array_key_exists($behaviour, $counts)) {
53 $counts[$behaviour] = 0;
55 $needed[$behaviour] = ($counts[$behaviour] > 0) ||
56 $pluginmanager->other_plugins_that_require('qbehaviour_' . $behaviour);
57 $archetypal[$behaviour] = question_engine::is_behaviour_archetypal($behaviour);
59 foreach ($counts as $behaviour => $count) {
60 if (!array_key_exists($behaviour, $behaviours)) {
61 $counts['missing'] += $count;
64 $needed['missing'] = true;
66 // Work of the correct sort order.
67 $config = get_config('question');
68 $sortedbehaviours = array();
69 foreach ($behaviours as $behaviour => $notused) {
70 $sortedbehaviours[$behaviour] = question_engine::get_behaviour_name($behaviour);
72 if (!empty($config->behavioursortorder)) {
73 $sortedbehaviours = question_engine::sort_behaviours($sortedbehaviours,
74 $config->behavioursortorder, '');
77 if (!empty($config->disabledbehaviours)) {
78 $disabledbehaviours = explode(',', $config->disabledbehaviours);
79 } else {
80 $disabledbehaviours = array();
83 // Process actions ============================================================
85 // Disable.
86 if (($disable = optional_param('disable', '', PARAM_PLUGIN)) && confirm_sesskey()) {
87 if (!isset($behaviours[$disable])) {
88 print_error('unknownbehaviour', 'question', $thispageurl, $disable);
91 if (array_search($disable, $disabledbehaviours) === false) {
92 $disabledbehaviours[] = $disable;
93 set_config('disabledbehaviours', implode(',', $disabledbehaviours), 'question');
95 redirect($thispageurl);
98 // Enable.
99 if (($enable = optional_param('enable', '', PARAM_PLUGIN)) && confirm_sesskey()) {
100 if (!isset($behaviours[$enable])) {
101 print_error('unknownbehaviour', 'question', $thispageurl, $enable);
104 if (!$archetypal[$enable]) {
105 print_error('cannotenablebehaviour', 'question', $thispageurl, $enable);
108 if (($key = array_search($enable, $disabledbehaviours)) !== false) {
109 unset($disabledbehaviours[$key]);
110 set_config('disabledbehaviours', implode(',', $disabledbehaviours), 'question');
112 redirect($thispageurl);
115 // Move up in order.
116 if (($up = optional_param('up', '', PARAM_PLUGIN)) && confirm_sesskey()) {
117 if (!isset($behaviours[$up])) {
118 print_error('unknownbehaviour', 'question', $thispageurl, $up);
121 // This function works fine for behaviours, as well as qtypes.
122 $neworder = question_reorder_qtypes($sortedbehaviours, $up, -1);
123 set_config('behavioursortorder', implode(',', $neworder), 'question');
124 redirect($thispageurl);
127 // Move down in order.
128 if (($down = optional_param('down', '', PARAM_PLUGIN)) && confirm_sesskey()) {
129 if (!isset($behaviours[$down])) {
130 print_error('unknownbehaviour', 'question', $thispageurl, $down);
133 // This function works fine for behaviours, as well as qtypes.
134 $neworder = question_reorder_qtypes($sortedbehaviours, $down, +1);
135 set_config('behavioursortorder', implode(',', $neworder), 'question');
136 redirect($thispageurl);
139 // Delete.
140 if (($delete = optional_param('delete', '', PARAM_PLUGIN)) && confirm_sesskey()) {
141 // Check it is OK to delete this question type.
142 if ($delete == 'missing') {
143 print_error('cannotdeletemissingbehaviour', 'question', $thispageurl);
146 if (!isset($behaviours[$delete]) && !get_config('qbehaviour_' . $delete, 'version')) {
147 print_error('unknownbehaviour', 'question', $thispageurl, $delete);
150 $behaviourname = $sortedbehaviours[$delete];
151 if ($counts[$delete] > 0) {
152 print_error('cannotdeletebehaviourinuse', 'question', $thispageurl, $behaviourname);
154 if ($needed[$delete] > 0) {
155 print_error('cannotdeleteneededbehaviour', 'question', $thispageurl, $behaviourname);
158 // If not yet confirmed, display a confirmation message.
159 if (!optional_param('confirm', '', PARAM_BOOL)) {
160 echo $OUTPUT->header();
161 echo $OUTPUT->heading(get_string('deletebehaviourareyousure', 'question', $behaviourname));
162 echo $OUTPUT->confirm(
163 get_string('deletebehaviourareyousuremessage', 'question', $behaviourname),
164 new moodle_url($thispageurl, array('delete' => $delete, 'confirm' => 1)),
165 $thispageurl);
166 echo $OUTPUT->footer();
167 exit;
170 // Do the deletion.
171 echo $OUTPUT->header();
172 echo $OUTPUT->heading(get_string('deletingbehaviour', 'question', $behaviourname));
174 // Remove this behaviour from configurations where it might appear.
175 if (($key = array_search($delete, $disabledbehaviours)) !== false) {
176 unset($disabledbehaviours[$key]);
177 set_config('disabledbehaviours', implode(',', $disabledbehaviours), 'question');
179 $behaviourorder = array_keys($sortedbehaviours);
180 if (($key = array_search($delete, $behaviourorder)) !== false) {
181 unset($behaviourorder[$key]);
182 set_config('behavioursortorder', implode(',', $behaviourorder), 'question');
185 // Then uninstall the plugin.
186 uninstall_plugin('qbehaviour', $delete);
188 // Display a message.
189 $a = new stdClass();
190 $a->behaviour = $behaviourname;
191 $a->directory = core_component::get_plugin_directory('qbehaviour', $delete);
192 echo $OUTPUT->box(get_string('qbehaviourdeletefiles', 'question', $a), 'generalbox', 'notice');
193 echo $OUTPUT->continue_button($thispageurl);
194 echo $OUTPUT->footer();
195 exit;
198 // End of process actions ==================================================
200 // Print the page heading.
201 echo $OUTPUT->header();
202 echo $OUTPUT->heading(get_string('manageqbehaviours', 'admin'));
204 // Set up the table.
205 $table = new flexible_table('qbehaviouradmintable');
206 $table->define_baseurl($thispageurl);
207 $table->define_columns(array('behaviour', 'numqas', 'version', 'requires',
208 'available', 'delete'));
209 $table->define_headers(array(get_string('behaviour', 'question'), get_string('numqas', 'question'),
210 get_string('version'), get_string('requires', 'admin'),
211 get_string('availableq', 'question'), get_string('delete')));
212 $table->set_attribute('id', 'qbehaviours');
213 $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
214 $table->setup();
216 // Add a row for each question type.
217 foreach ($sortedbehaviours as $behaviour => $behaviourname) {
218 $row = array();
220 // Question icon and name.
221 $row[] = $behaviourname;
223 // Count
224 $row[] = $counts[$behaviour];
226 // Question version number.
227 $version = get_config('qbehaviour_' . $behaviour, 'version');
228 if ($version) {
229 $row[] = $version;
230 } else {
231 $row[] = html_writer::tag('span', get_string('nodatabase', 'admin'), array('class' => 'disabled'));
234 // Other question types required by this one.
235 $plugin = $pluginmanager->get_plugin_info('qbehaviour_' . $behaviour);
236 $required = $plugin->get_other_required_plugins();
237 if (!empty($required)) {
238 $strrequired = array();
239 foreach ($required as $component => $notused) {
240 $strrequired[] = $pluginmanager->plugin_name($component);
242 $row[] = implode(', ', $strrequired);
243 } else {
244 $row[] = '';
247 // Are people allowed to select this behaviour?
248 $rowclass = '';
249 if ($archetypal[$behaviour]) {
250 $enabled = array_search($behaviour, $disabledbehaviours) === false;
251 $icons = question_behaviour_enable_disable_icons($behaviour, $enabled);
252 if (!$enabled) {
253 $rowclass = 'dimmed_text';
255 } else {
256 $icons = $OUTPUT->spacer(array('class' => 'iconsmall'));
259 // Move icons.
260 $icons .= question_behaviour_icon_html('up', $behaviour, 't/up', get_string('up'), null);
261 $icons .= question_behaviour_icon_html('down', $behaviour, 't/down', get_string('down'), null);
262 $row[] = $icons;
264 // Delete link, if available.
265 if ($needed[$behaviour]) {
266 $row[] = '';
267 } else {
268 $row[] = html_writer::link(new moodle_url($thispageurl,
269 array('delete' => $behaviour, 'sesskey' => sesskey())), get_string('delete'),
270 array('title' => get_string('uninstallbehaviour', 'question')));
273 $table->add_data($row, $rowclass);
276 $table->finish_output();
278 echo $OUTPUT->footer();
280 function question_behaviour_enable_disable_icons($behaviour, $enabled) {
281 if ($enabled) {
282 return question_behaviour_icon_html('disable', $behaviour, 't/hide',
283 get_string('enabled', 'question'), get_string('disable'));
284 } else {
285 return question_behaviour_icon_html('enable', $behaviour, 't/show',
286 get_string('disabled', 'question'), get_string('enable'));
290 function question_behaviour_icon_html($action, $behaviour, $icon, $alt, $tip) {
291 global $OUTPUT;
292 return $OUTPUT->action_icon(new moodle_url('/admin/qbehaviours.php',
293 array($action => $behaviour, 'sesskey' => sesskey())),
294 new pix_icon($icon, $alt, 'moodle', array('title' => '', 'class' => 'iconsmall')),
295 null, array('title' => $tip));