MDL-31832 CLI upgrade will return 0 exit status if no upgrade is needed
[moodle.git] / admin / qbehaviours.php
blobce81b1369d291204d3a2dc5a41ad2c094152970b
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 = get_context_instance(CONTEXT_SYSTEM);
37 require_capability('moodle/question:config', $systemcontext);
39 admin_externalpage_setup('manageqbehaviours');
40 $thispageurl = new moodle_url('/admin/qbehaviours.php');
42 $behaviours = 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])) {
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 // Delete any configuration records.
175 if (!unset_all_config_for_plugin('qbehaviour_' . $delete)) {
176 echo $OUTPUT->notification(get_string('errordeletingconfig', 'admin', 'qbehaviour_' . $delete));
178 if (($key = array_search($delete, $disabledbehaviours)) !== false) {
179 unset($disabledbehaviours[$key]);
180 set_config('disabledbehaviours', implode(',', $disabledbehaviours), 'question');
182 $behaviourorder = array_keys($sortedbehaviours);
183 if (($key = array_search($delete, $behaviourorder)) !== false) {
184 unset($behaviourorder[$key]);
185 set_config('behavioursortorder', implode(',', $behaviourorder), 'question');
188 // Then the tables themselves
189 drop_plugin_tables($delete, get_plugin_directory('qbehaviour', $delete) . '/db/install.xml', false);
191 // Remove event handlers and dequeue pending events
192 events_uninstall('qbehaviour_' . $delete);
194 $a = new stdClass();
195 $a->behaviour = $behaviourname;
196 $a->directory = get_plugin_directory('qbehaviour', $delete);
197 echo $OUTPUT->box(get_string('qbehaviourdeletefiles', 'question', $a), 'generalbox', 'notice');
198 echo $OUTPUT->continue_button($thispageurl);
199 echo $OUTPUT->footer();
200 exit;
203 // End of process actions ==================================================
205 // Print the page heading.
206 echo $OUTPUT->header();
207 echo $OUTPUT->heading(get_string('manageqbehaviours', 'admin'));
209 // Set up the table.
210 $table = new flexible_table('qbehaviouradmintable');
211 $table->define_baseurl($thispageurl);
212 $table->define_columns(array('behaviour', 'numqas', 'version', 'requires',
213 'available', 'delete'));
214 $table->define_headers(array(get_string('behaviour', 'question'), get_string('numqas', 'question'),
215 get_string('version'), get_string('requires', 'admin'),
216 get_string('availableq', 'question'), get_string('delete')));
217 $table->set_attribute('id', 'qbehaviours');
218 $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
219 $table->setup();
221 // Add a row for each question type.
222 foreach ($sortedbehaviours as $behaviour => $behaviourname) {
223 $row = array();
225 // Question icon and name.
226 $row[] = $behaviourname;
228 // Count
229 $row[] = $counts[$behaviour];
231 // Question version number.
232 $version = get_config('qbehaviour_' . $behaviour, 'version');
233 if ($version) {
234 $row[] = $version;
235 } else {
236 $row[] = html_writer::tag('span', get_string('nodatabase', 'admin'), array('class' => 'disabled'));
239 // Other question types required by this one.
240 $plugin = $pluginmanager->get_plugin_info('qbehaviour_' . $behaviour);
241 $required = $plugin->get_other_required_plugins();
242 if (!empty($required)) {
243 $strrequired = array();
244 foreach ($required as $component => $notused) {
245 $strrequired[] = $pluginmanager->plugin_name($component);
247 $row[] = implode(', ', $strrequired);
248 } else {
249 $row[] = '';
252 // Are people allowed to select this behaviour?
253 $rowclass = '';
254 if ($archetypal[$behaviour]) {
255 $enabled = array_search($behaviour, $disabledbehaviours) === false;
256 $icons = question_behaviour_enable_disable_icons($behaviour, $enabled);
257 if (!$enabled) {
258 $rowclass = 'dimmed_text';
260 } else {
261 $icons = $OUTPUT->spacer() . ' ';
264 // Move icons.
265 $icons .= question_behaviour_icon_html('up', $behaviour, 't/up', get_string('up'), null);
266 $icons .= question_behaviour_icon_html('down', $behaviour, 't/down', get_string('down'), null);
267 $row[] = $icons;
269 // Delete link, if available.
270 if ($needed[$behaviour]) {
271 $row[] = '';
272 } else {
273 $row[] = html_writer::link(new moodle_url($thispageurl,
274 array('delete' => $behaviour, 'sesskey' => sesskey())), get_string('delete'),
275 array('title' => get_string('uninstallbehaviour', 'question')));
278 $table->add_data($row, $rowclass);
281 $table->finish_output();
283 echo $OUTPUT->footer();
285 function question_behaviour_enable_disable_icons($behaviour, $enabled) {
286 if ($enabled) {
287 return question_behaviour_icon_html('disable', $behaviour, 'i/hide',
288 get_string('enabled', 'question'), get_string('disable'));
289 } else {
290 return question_behaviour_icon_html('enable', $behaviour, 'i/show',
291 get_string('disabled', 'question'), get_string('enable'));
295 function question_behaviour_icon_html($action, $behaviour, $icon, $alt, $tip) {
296 global $OUTPUT;
297 return $OUTPUT->action_icon(new moodle_url('/admin/qbehaviours.php',
298 array($action => $behaviour, 'sesskey' => sesskey())),
299 new pix_icon($icon, $alt, 'moodle', array('title' => '')),
300 null, array('title' => $tip)) . ' ';