MDL-78962 core/loadingicon: remove jQuery requirement in the API
[moodle.git] / admin / qbehaviours.php
blobd047669b92201ed14ece4bbd5c303195ec05ff30
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(__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 // Check permissions.
34 require_login(null, false);
35 $systemcontext = context_system::instance();
36 require_capability('moodle/question:config', $systemcontext);
38 admin_externalpage_setup('manageqbehaviours');
39 $thispageurl = new moodle_url('/admin/qbehaviours.php');
41 $behaviours = core_component::get_plugin_list('qbehaviour');
42 $pluginmanager = core_plugin_manager::instance();
44 // Get some data we will need - question counts and which types are needed.
45 $counts = $DB->get_records_sql_menu("
46 SELECT behaviour, COUNT(1)
47 FROM {question_attempts} GROUP BY behaviour");
48 $needed = array();
49 $archetypal = array();
50 foreach ($behaviours as $behaviour => $notused) {
51 if (!array_key_exists($behaviour, $counts)) {
52 $counts[$behaviour] = 0;
54 $needed[$behaviour] = ($counts[$behaviour] > 0) ||
55 $pluginmanager->other_plugins_that_require('qbehaviour_' . $behaviour);
56 $archetypal[$behaviour] = question_engine::is_behaviour_archetypal($behaviour);
58 foreach ($counts as $behaviour => $count) {
59 if (!array_key_exists($behaviour, $behaviours)) {
60 $counts['missing'] += $count;
63 $needed['missing'] = true;
65 // Work of the correct sort order.
66 $config = get_config('question');
67 $sortedbehaviours = array();
68 foreach ($behaviours as $behaviour => $notused) {
69 $sortedbehaviours[$behaviour] = question_engine::get_behaviour_name($behaviour);
71 if (!empty($config->behavioursortorder)) {
72 $sortedbehaviours = question_engine::sort_behaviours($sortedbehaviours,
73 $config->behavioursortorder, '');
76 if (!empty($config->disabledbehaviours)) {
77 $disabledbehaviours = explode(',', $config->disabledbehaviours);
78 } else {
79 $disabledbehaviours = array();
82 // Process actions ============================================================
84 // Disable.
85 if (($disable = optional_param('disable', '', PARAM_PLUGIN)) && confirm_sesskey()) {
86 if (!isset($behaviours[$disable])) {
87 throw new \moodle_exception('unknownbehaviour', 'question', $thispageurl, $disable);
90 if (array_search($disable, $disabledbehaviours) === false) {
91 $class = \core_plugin_manager::resolve_plugininfo_class('qbehaviour');
92 $class::enable_plugin($disable, false);
94 redirect($thispageurl);
97 // Enable.
98 if (($enable = optional_param('enable', '', PARAM_PLUGIN)) && confirm_sesskey()) {
99 if (!isset($behaviours[$enable])) {
100 throw new \moodle_exception('unknownbehaviour', 'question', $thispageurl, $enable);
103 if (!$archetypal[$enable]) {
104 throw new \moodle_exception('cannotenablebehaviour', 'question', $thispageurl, $enable);
107 if (($key = array_search($enable, $disabledbehaviours)) !== false) {
108 $class = \core_plugin_manager::resolve_plugininfo_class('qbehaviour');
109 $class::enable_plugin($enable, true);
111 redirect($thispageurl);
114 // Move up in order.
115 if (($up = optional_param('up', '', PARAM_PLUGIN)) && confirm_sesskey()) {
116 if (!isset($behaviours[$up])) {
117 throw new \moodle_exception('unknownbehaviour', 'question', $thispageurl, $up);
120 // This function works fine for behaviours, as well as qtypes.
121 $neworder = question_reorder_qtypes($sortedbehaviours, $up, -1);
122 set_config('behavioursortorder', implode(',', $neworder), 'question');
123 redirect($thispageurl);
126 // Move down in order.
127 if (($down = optional_param('down', '', PARAM_PLUGIN)) && confirm_sesskey()) {
128 if (!isset($behaviours[$down])) {
129 throw new \moodle_exception('unknownbehaviour', 'question', $thispageurl, $down);
132 // This function works fine for behaviours, as well as qtypes.
133 $neworder = question_reorder_qtypes($sortedbehaviours, $down, +1);
134 set_config('behavioursortorder', implode(',', $neworder), 'question');
135 redirect($thispageurl);
138 // End of process actions ==================================================
140 // Print the page heading.
141 echo $OUTPUT->header();
142 echo $OUTPUT->heading(get_string('manageqbehaviours', 'admin'));
144 // Set up the table.
145 $table = new flexible_table('qbehaviouradmintable');
146 $table->define_baseurl($thispageurl);
147 $table->define_columns(array('behaviour', 'numqas', 'version', 'requires',
148 'available', 'uninstall'));
149 $table->define_headers(array(get_string('behaviour', 'question'), get_string('numqas', 'question'),
150 get_string('version'), get_string('requires', 'admin'),
151 get_string('availableq', 'question'), get_string('uninstallplugin', 'core_admin')));
152 $table->set_attribute('id', 'qbehaviours');
153 $table->set_attribute('class', 'generaltable admintable');
154 $table->setup();
156 // Add a row for each question type.
157 foreach ($sortedbehaviours as $behaviour => $behaviourname) {
158 $row = array();
160 // Question icon and name.
161 $row[] = $behaviourname;
163 // Count
164 $row[] = $counts[$behaviour];
166 // Question version number.
167 $version = get_config('qbehaviour_' . $behaviour, 'version');
168 if ($version) {
169 $row[] = $version;
170 } else {
171 $row[] = html_writer::tag('span', get_string('nodatabase', 'admin'), array('class' => 'text-muted'));
174 // Other question types required by this one.
175 $plugin = $pluginmanager->get_plugin_info('qbehaviour_' . $behaviour);
176 $required = $plugin->get_other_required_plugins();
177 if (!empty($required)) {
178 $strrequired = array();
179 foreach ($required as $component => $notused) {
180 $strrequired[] = $pluginmanager->plugin_name($component);
182 $row[] = implode(', ', $strrequired);
183 } else {
184 $row[] = '';
187 // Are people allowed to select this behaviour?
188 $rowclass = '';
189 if ($archetypal[$behaviour]) {
190 $enabled = array_search($behaviour, $disabledbehaviours) === false;
191 $icons = question_behaviour_enable_disable_icons($behaviour, $enabled);
192 if (!$enabled) {
193 $rowclass = 'dimmed_text';
195 } else {
196 $icons = $OUTPUT->spacer(array('class' => 'iconsmall'));
199 // Move icons.
200 $icons .= question_behaviour_icon_html('up', $behaviour, 't/up', get_string('up'), null);
201 $icons .= question_behaviour_icon_html('down', $behaviour, 't/down', get_string('down'), null);
202 $row[] = $icons;
204 // Delete link, if available.
205 if ($needed[$behaviour]) {
206 $row[] = '';
207 } else {
208 $uninstallurl = core_plugin_manager::instance()->get_uninstall_url('qbehaviour_'.$behaviour, 'manage');
209 if ($uninstallurl) {
210 $row[] = html_writer::link($uninstallurl, get_string('uninstallplugin', 'core_admin'),
211 array('title' => get_string('uninstallbehaviour', 'question')));
215 $table->add_data($row, $rowclass);
218 $table->finish_output();
220 echo $OUTPUT->footer();
222 function question_behaviour_enable_disable_icons($behaviour, $enabled) {
223 if ($enabled) {
224 return question_behaviour_icon_html('disable', $behaviour, 't/hide',
225 get_string('enabled', 'question'), get_string('disable'));
226 } else {
227 return question_behaviour_icon_html('enable', $behaviour, 't/show',
228 get_string('disabled', 'question'), get_string('enable'));
232 function question_behaviour_icon_html($action, $behaviour, $icon, $alt, $tip) {
233 global $OUTPUT;
234 return $OUTPUT->action_icon(new moodle_url('/admin/qbehaviours.php',
235 array($action => $behaviour, 'sesskey' => sesskey())),
236 new pix_icon($icon, $alt, 'moodle', array('title' => '', 'class' => 'iconsmall')),
237 null, array('title' => $tip));