source: moodle/trunk/fuentes/admin/qtypes.php @ 136

Last change on this file since 136 was 136, checked in by mabarracus, 4 years ago

Ported code to xenial

File size: 9.2 KB
Line 
1<?php
2
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.
14//
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/>.
17
18/**
19 * Allows the admin to manage question types.
20 *
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
25 */
26
27
28require_once(dirname(__FILE__) . '/../config.php');
29require_once($CFG->libdir . '/questionlib.php');
30require_once($CFG->libdir . '/adminlib.php');
31require_once($CFG->libdir . '/tablelib.php');
32
33// Check permissions.
34require_login();
35$systemcontext = context_system::instance();
36require_capability('moodle/question:config', $systemcontext);
37$canviewreports = has_capability('report/questioninstances:view', $systemcontext);
38
39admin_externalpage_setup('manageqtypes');
40$thispageurl = new moodle_url('/admin/qtypes.php');
41
42$qtypes = question_bank::get_all_qtypes();
43$pluginmanager = core_plugin_manager::instance();
44
45// Get some data we will need - question counts and which types are needed.
46$counts = $DB->get_records_sql("
47        SELECT qtype, COUNT(1) as numquestions, SUM(hidden) as numhidden
48        FROM {question} GROUP BY qtype", array());
49$needed = array();
50foreach ($qtypes as $qtypename => $qtype) {
51    if (!isset($counts[$qtypename])) {
52        $counts[$qtypename] = new stdClass;
53        $counts[$qtypename]->numquestions = 0;
54        $counts[$qtypename]->numhidden = 0;
55    }
56    $needed[$qtypename] = $counts[$qtypename]->numquestions > 0 ||
57            $pluginmanager->other_plugins_that_require($qtype->plugin_name());
58    $counts[$qtypename]->numquestions -= $counts[$qtypename]->numhidden;
59}
60$needed['missingtype'] = true; // The system needs the missing question type.
61foreach ($counts as $qtypename => $count) {
62    if (!isset($qtypes[$qtypename])) {
63        $counts['missingtype']->numquestions += $count->numquestions - $count->numhidden;
64        $counts['missingtype']->numhidden += $count->numhidden;
65    }
66}
67
68// Work of the correct sort order.
69$config = get_config('question');
70$sortedqtypes = array();
71foreach ($qtypes as $qtypename => $qtype) {
72    $sortedqtypes[$qtypename] = $qtype->local_name();
73}
74$sortedqtypes = question_bank::sort_qtype_array($sortedqtypes, $config);
75
76// Process actions ============================================================
77
78// Disable.
79if (($disable = optional_param('disable', '', PARAM_PLUGIN)) && confirm_sesskey()) {
80    if (!isset($qtypes[$disable])) {
81        print_error('unknownquestiontype', 'question', $thispageurl, $disable);
82    }
83
84    set_config($disable . '_disabled', 1, 'question');
85    redirect($thispageurl);
86}
87
88// Enable.
89if (($enable = optional_param('enable', '', PARAM_PLUGIN)) && confirm_sesskey()) {
90    if (!isset($qtypes[$enable])) {
91        print_error('unknownquestiontype', 'question', $thispageurl, $enable);
92    }
93
94    if (!$qtypes[$enable]->menu_name()) {
95        print_error('cannotenable', 'question', $thispageurl, $enable);
96    }
97
98    unset_config($enable . '_disabled', 'question');
99    redirect($thispageurl);
100}
101
102// Move up in order.
103if (($up = optional_param('up', '', PARAM_PLUGIN)) && confirm_sesskey()) {
104    if (!isset($qtypes[$up])) {
105        print_error('unknownquestiontype', 'question', $thispageurl, $up);
106    }
107
108    $neworder = question_reorder_qtypes($sortedqtypes, $up, -1);
109    question_save_qtype_order($neworder, $config);
110    redirect($thispageurl);
111}
112
113// Move down in order.
114if (($down = optional_param('down', '', PARAM_PLUGIN)) && confirm_sesskey()) {
115    if (!isset($qtypes[$down])) {
116        print_error('unknownquestiontype', 'question', $thispageurl, $down);
117    }
118
119    $neworder = question_reorder_qtypes($sortedqtypes, $down, +1);
120    question_save_qtype_order($neworder, $config);
121    redirect($thispageurl);
122}
123
124// End of process actions ==================================================
125
126// Print the page heading.
127echo $OUTPUT->header();
128echo $OUTPUT->heading(get_string('manageqtypes', 'admin'));
129
130// Set up the table.
131$table = new flexible_table('qtypeadmintable');
132$table->define_baseurl($thispageurl);
133$table->define_columns(array('questiontype', 'numquestions', 'version', 'requires',
134        'availableto', 'uninstall', 'settings'));
135$table->define_headers(array(get_string('questiontype', 'question'), get_string('numquestions', 'question'),
136        get_string('version'), get_string('requires', 'admin'), get_string('availableq', 'question'),
137        get_string('settings'), get_string('uninstallplugin', 'core_admin')));
138$table->set_attribute('id', 'qtypes');
139$table->set_attribute('class', 'admintable generaltable');
140$table->setup();
141
142// Add a row for each question type.
143$createabletypes = question_bank::get_creatable_qtypes();
144foreach ($sortedqtypes as $qtypename => $localname) {
145    $qtype = $qtypes[$qtypename];
146    $row = array();
147
148    // Question icon and name.
149    $fakequestion = new stdClass;
150    $fakequestion->qtype = $qtypename;
151    $icon = print_question_icon($fakequestion, true);
152    $row[] = $icon . ' ' . $localname;
153
154    // Number of questions of this type.
155    if ($counts[$qtypename]->numquestions + $counts[$qtypename]->numhidden > 0) {
156        if ($counts[$qtypename]->numhidden > 0) {
157            $strcount = get_string('numquestionsandhidden', 'question', $counts[$qtypename]);
158        } else {
159            $strcount = $counts[$qtypename]->numquestions;
160        }
161        if ($canviewreports) {
162            $row[] = html_writer::link(new moodle_url('/report/questioninstances/index.php',
163                    array('qtype' => $qtypename)), $strcount, array('title' => get_string('showdetails', 'admin')));
164        } else {
165            $strcount;
166        }
167    } else {
168        $row[] = 0;
169    }
170
171    // Question version number.
172    $version = get_config('qtype_' . $qtypename, 'version');
173    if ($version) {
174        $row[] = $version;
175    } else {
176        $row[] = html_writer::tag('span', get_string('nodatabase', 'admin'), array('class' => 'disabled'));
177    }
178
179    // Other question types required by this one.
180    $plugin = $pluginmanager->get_plugin_info($qtype->plugin_name());
181    $requiredtypes = $plugin->get_other_required_plugins();
182    $strtypes = array();
183    if (!empty($requiredtypes)) {
184        foreach ($requiredtypes as $required => $notused) {
185            $strtypes[] = $pluginmanager->plugin_name($required);
186        }
187        $row[] = implode(', ', $strtypes);
188    } else {
189        $row[] = '';
190    }
191
192    // Are people allowed to create new questions of this type?
193    $rowclass = '';
194    if ($qtype->menu_name()) {
195        $createable = isset($createabletypes[$qtypename]);
196        $icons = question_types_enable_disable_icons($qtypename, $createable);
197        if (!$createable) {
198            $rowclass = 'dimmed_text';
199        }
200    } else {
201        $icons = $OUTPUT->spacer();
202    }
203
204    // Move icons.
205    $icons .= question_type_icon_html('up', $qtypename, 't/up', get_string('up'), '');
206    $icons .= question_type_icon_html('down', $qtypename, 't/down', get_string('down'), '');
207    $row[] = $icons;
208
209    // Settings link, if available.
210    $settings = admin_get_root()->locate('qtypesetting' . $qtypename);
211    if ($settings instanceof admin_externalpage) {
212        $row[] = html_writer::link($settings->url, get_string('settings'));
213    } else if ($settings instanceof admin_settingpage) {
214        $row[] = html_writer::link(new moodle_url('/admin/settings.php',
215                array('section' => 'qtypesetting' . $qtypename)), get_string('settings'));
216    } else {
217        $row[] = '';
218    }
219
220    // Uninstall link, if available.
221    if ($needed[$qtypename]) {
222        $row[] = '';
223    } else {
224        $uninstallurl = core_plugin_manager::instance()->get_uninstall_url('qtype_'.$qtypename, 'manage');
225        if ($uninstallurl) {
226            $row[] = html_writer::link($uninstallurl, get_string('uninstallplugin', 'core_admin'),
227                array('title' => get_string('uninstallqtype', 'question')));
228        }
229    }
230
231    $table->add_data($row, $rowclass);
232}
233
234$table->finish_output();
235
236echo $OUTPUT->footer();
237
238function question_types_enable_disable_icons($qtypename, $createable) {
239    if ($createable) {
240        return question_type_icon_html('disable', $qtypename, 't/hide',
241                get_string('enabled', 'question'), get_string('disable'));
242    } else {
243        return question_type_icon_html('enable', $qtypename, 't/show',
244                get_string('disabled', 'question'), get_string('enable'));
245    }
246}
247
248function question_type_icon_html($action, $qtypename, $icon, $alt, $tip) {
249    global $OUTPUT;
250    return $OUTPUT->action_icon(new moodle_url('/admin/qtypes.php',
251            array($action => $qtypename, 'sesskey' => sesskey())),
252            new pix_icon($icon, $alt, 'moodle', array('title' => '', 'class' => 'iconsmall')),
253            null, array('title' => $tip));
254}
255
Note: See TracBrowser for help on using the repository browser.