Merge branch 'MDL-29064_21' of git://github.com/timhunt/moodle into MOODLE_21_STABLE
[moodle.git] / admin / qbehaviours.php
blob88b75fcf37cdc689d097381f5f70e225ca476d7e
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 . '/tablelib.php');
33 // Check permissions.
34 require_login();
35 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
36 require_capability('moodle/question:config', $systemcontext);
38 admin_externalpage_setup('manageqbehaviours');
39 $thispageurl = new moodle_url('/admin/qbehaviours.php');
41 $behaviours = get_plugin_list('qbehaviour');
43 // Get some data we will need - question counts and which types are needed.
44 $counts = $DB->get_records_sql_menu("
45 SELECT behaviour, COUNT(1)
46 FROM {question_attempts} GROUP BY behaviour");
47 $needed = array();
48 $archetypal = array();
49 foreach ($behaviours as $behaviour => $notused) {
50 if (!array_key_exists($behaviour, $counts)) {
51 $counts[$behaviour] = 0;
53 $needed[$behaviour] = $counts[$behaviour] > 0;
54 $archetypal[$behaviour] = question_engine::is_behaviour_archetypal($behaviour);
57 foreach ($behaviours as $behaviour => $notused) {
58 foreach (question_engine::get_behaviour_required_behaviours($behaviour) as $reqbehaviour) {
59 $needed[$reqbehaviour] = true;
62 foreach ($counts as $behaviour => $count) {
63 if (!array_key_exists($behaviour, $behaviours)) {
64 $counts['missingtype'] += $count;
68 // Work of the correct sort order.
69 $config = get_config('question');
70 $sortedbehaviours = array();
71 foreach ($behaviours as $behaviour => $notused) {
72 $sortedbehaviours[$behaviour] = question_engine::get_behaviour_name($behaviour);
74 if (!empty($config->behavioursortorder)) {
75 $sortedbehaviours = question_engine::sort_behaviours($sortedbehaviours,
76 $config->behavioursortorder, '');
79 if (!empty($config->disabledbehaviours)) {
80 $disabledbehaviours = explode(',', $config->disabledbehaviours);
81 } else {
82 $disabledbehaviours = array();
85 // Process actions ============================================================
87 // Disable.
88 if (($disable = optional_param('disable', '', PARAM_SAFEDIR)) && confirm_sesskey()) {
89 if (!isset($behaviours[$disable])) {
90 print_error('unknownbehaviour', 'question', $thispageurl, $disable);
93 if (array_search($disable, $disabledbehaviours) === false) {
94 $disabledbehaviours[] = $disable;
95 set_config('disabledbehaviours', implode(',', $disabledbehaviours), 'question');
97 redirect($thispageurl);
100 // Enable.
101 if (($enable = optional_param('enable', '', PARAM_SAFEDIR)) && confirm_sesskey()) {
102 if (!isset($behaviours[$enable])) {
103 print_error('unknownbehaviour', 'question', $thispageurl, $enable);
106 if (!$archetypal[$enable]) {
107 print_error('cannotenablebehaviour', 'question', $thispageurl, $enable);
110 if (($key = array_search($enable, $disabledbehaviours)) !== false) {
111 unset($disabledbehaviours[$key]);
112 set_config('disabledbehaviours', implode(',', $disabledbehaviours), 'question');
114 redirect($thispageurl);
117 // Move up in order.
118 if (($up = optional_param('up', '', PARAM_SAFEDIR)) && confirm_sesskey()) {
119 if (!isset($behaviours[$up])) {
120 print_error('unknownbehaviour', 'question', $thispageurl, $up);
123 // This function works fine for behaviours, as well as qtypes.
124 $neworder = question_reorder_qtypes($sortedbehaviours, $up, -1);
125 set_config('behavioursortorder', implode(',', $neworder), 'question');
126 redirect($thispageurl);
129 // Move down in order.
130 if (($down = optional_param('down', '', PARAM_SAFEDIR)) && confirm_sesskey()) {
131 if (!isset($behaviours[$down])) {
132 print_error('unknownbehaviour', 'question', $thispageurl, $down);
135 // This function works fine for behaviours, as well as qtypes.
136 $neworder = question_reorder_qtypes($sortedbehaviours, $down, +1);
137 set_config('behavioursortorder', implode(',', $neworder), 'question');
138 redirect($thispageurl);
141 // Delete.
142 if (($delete = optional_param('delete', '', PARAM_SAFEDIR)) && confirm_sesskey()) {
143 // Check it is OK to delete this question type.
144 if ($delete == 'missing') {
145 print_error('cannotdeletemissingbehaviour', 'question', $thispageurl);
148 if (!isset($behaviours[$delete])) {
149 print_error('unknownbehaviour', 'question', $thispageurl, $delete);
152 $behaviourname = $sortedbehaviours[$delete];
153 if ($counts[$delete] > 0) {
154 print_error('cannotdeletebehaviourinuse', 'question', $thispageurl, $behaviourname);
156 if ($needed[$delete] > 0) {
157 print_error('cannotdeleteneededbehaviour', 'question', $thispageurl, $behaviourname);
160 // If not yet confirmed, display a confirmation message.
161 if (!optional_param('confirm', '', PARAM_BOOL)) {
162 echo $OUTPUT->header();
163 echo $OUTPUT->heading(get_string('deletebehaviourareyousure', 'question', $behaviourname));
164 echo $OUTPUT->confirm(
165 get_string('deletebehaviourareyousuremessage', 'question', $behaviourname),
166 new moodle_url($thispageurl, array('delete' => $delete, 'confirm' => 1)),
167 $thispageurl);
168 echo $OUTPUT->footer();
169 exit;
172 // Do the deletion.
173 echo $OUTPUT->header();
174 echo $OUTPUT->heading(get_string('deletingbehaviour', 'question', $behaviourname));
176 // Delete any configuration records.
177 if (!unset_all_config_for_plugin('qbehaviour_' . $delete)) {
178 echo $OUTPUT->notification(get_string('errordeletingconfig', 'admin', 'qbehaviour_' . $delete));
180 if (($key = array_search($delete, $disabledbehaviours)) !== false) {
181 unset($disabledbehaviours[$key]);
182 set_config('disabledbehaviours', implode(',', $disabledbehaviours), 'question');
184 $behaviourorder = explode(',', $config->behavioursortorder);
185 if (($key = array_search($delete, $behaviourorder)) !== false) {
186 unset($behaviourorder[$key]);
187 set_config('behavioursortorder', implode(',', $behaviourorder), 'question');
190 // Then the tables themselves
191 drop_plugin_tables($delete, get_plugin_directory('qbehaviour', $delete) . '/db/install.xml', false);
193 // Remove event handlers and dequeue pending events
194 events_uninstall('qbehaviour_' . $delete);
196 $a->behaviour = $behaviourname;
197 $a->directory = get_plugin_directory('qbehaviour', $delete);
198 echo $OUTPUT->box(get_string('qbehaviourdeletefiles', 'question', $a), 'generalbox', 'notice');
199 echo $OUTPUT->continue_button($thispageurl);
200 echo $OUTPUT->footer();
201 exit;
204 // End of process actions ==================================================
206 // Print the page heading.
207 echo $OUTPUT->header();
208 echo $OUTPUT->heading(get_string('manageqbehaviours', 'admin'));
210 // Set up the table.
211 $table = new flexible_table('qbehaviouradmintable');
212 $table->define_baseurl($thispageurl);
213 $table->define_columns(array('behaviour', 'numqas', 'version', 'requires',
214 'available', 'delete'));
215 $table->define_headers(array(get_string('behaviour', 'question'), get_string('numqas', 'question'),
216 get_string('version'), get_string('requires', 'admin'),
217 get_string('availableq', 'question'), get_string('delete')));
218 $table->set_attribute('id', 'qbehaviours');
219 $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
220 $table->setup();
222 // Add a row for each question type.
223 foreach ($sortedbehaviours as $behaviour => $behaviourname) {
224 $row = array();
226 // Question icon and name.
227 $row[] = $behaviourname;
229 // Count
230 $row[] = $counts[$behaviour];
232 // Question version number.
233 $version = get_config('qbehaviour_' . $behaviour, 'version');
234 if ($version) {
235 $row[] = $version;
236 } else {
237 $row[] = html_writer::tag('span', get_string('nodatabase', 'admin'), array('class' => 'disabled'));
240 // Other question types required by this one.
241 $requiredbehaviours = question_engine::get_behaviour_required_behaviours($behaviour);
242 if (!empty($requiredbehaviours)) {
243 $strrequiredbehaviours = array();
244 foreach ($requiredbehaviours as $required) {
245 $strrequiredbehaviours[] = $sortedbehaviours[$required];
247 $row[] = implode(', ', $strrequiredbehaviours);
248 } else {
249 $row[] = '';
252 // Are people allowed to create new questions of this type?
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)) . ' ';