1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* This script allows a teacher to create, edit and delete question categories.
|
|
|
19 |
*
|
|
|
20 |
* @package qbank_managecategories
|
|
|
21 |
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
|
|
|
22 |
* @author 2021, Guillermo Gomez Arias <guillermogomez@catalyst-au.net>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
require_once(__DIR__ . '/../../../config.php');
|
1441 |
ariadna |
27 |
require_once($CFG->dirroot . '/question/editlib.php');
|
1 |
efrain |
28 |
|
1441 |
ariadna |
29 |
use core_question\output\qbank_actionbar;
|
|
|
30 |
use core_question\category_manager;
|
1 |
efrain |
31 |
use qbank_managecategories\form\question_move_form;
|
|
|
32 |
use qbank_managecategories\helper;
|
1441 |
ariadna |
33 |
use qbank_managecategories\output\categories;
|
|
|
34 |
use qbank_managecategories\output\categories_header;
|
|
|
35 |
use qbank_managecategories\question_categories;
|
1 |
efrain |
36 |
|
|
|
37 |
require_login();
|
|
|
38 |
core_question\local\bank\helper::require_plugin_enabled(helper::PLUGINNAME);
|
|
|
39 |
|
1441 |
ariadna |
40 |
// Since Moodle 5.0 any request with the courseid parameter is deprecated and will redirect to the banks management page.
|
|
|
41 |
if ($courseid = optional_param('courseid', 0, PARAM_INT)) {
|
|
|
42 |
redirect(new moodle_url('/question/banks.php', ['courseid' => $courseid]));
|
1 |
efrain |
43 |
}
|
|
|
44 |
|
1441 |
ariadna |
45 |
list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) =
|
|
|
46 |
question_edit_setup('categories', '/question/bank/managecategories/category.php');
|
1 |
efrain |
47 |
|
1441 |
ariadna |
48 |
$thiscontext = context_module::instance($cmid)->id;
|
1 |
efrain |
49 |
|
1441 |
ariadna |
50 |
$todelete = optional_param('delete', 0, PARAM_INT); // The ID of a category to delete.
|
1 |
efrain |
51 |
|
1441 |
ariadna |
52 |
$PAGE->set_url($thispageurl);
|
|
|
53 |
$PAGE->add_body_class('limitedwidth');
|
1 |
efrain |
54 |
|
1441 |
ariadna |
55 |
$manager = new category_manager($thispageurl);
|
1 |
efrain |
56 |
|
1441 |
ariadna |
57 |
if ($todelete) {
|
|
|
58 |
if (!$category = $DB->get_record("question_categories", ["id" => $todelete])) {
|
|
|
59 |
throw new moodle_exception('nocate', 'question', $thispageurl->out(), $todelete);
|
1 |
efrain |
60 |
}
|
|
|
61 |
|
1441 |
ariadna |
62 |
helper::question_remove_stale_questions_from_category($todelete);
|
1 |
efrain |
63 |
|
1441 |
ariadna |
64 |
$questionstomove = count($manager->get_real_question_ids_in_category($todelete));
|
1 |
efrain |
65 |
|
|
|
66 |
// Second pass, if we still have questions to move, setup the form.
|
|
|
67 |
if ($questionstomove) {
|
|
|
68 |
$categorycontext = context::instance_by_id($category->contextid);
|
|
|
69 |
$moveform = new question_move_form($thispageurl,
|
1441 |
ariadna |
70 |
['contexts' => [$categorycontext], 'currentcat' => "$todelete"]);
|
1 |
efrain |
71 |
if ($moveform->is_cancelled()) {
|
1441 |
ariadna |
72 |
$thispageurl->remove_all_params();
|
|
|
73 |
if (!is_null($cmid)) {
|
|
|
74 |
$thispageurl->param('cmid', $cmid);
|
|
|
75 |
} else {
|
|
|
76 |
$thispageurl->param('courseid', $courseid);
|
|
|
77 |
}
|
1 |
efrain |
78 |
redirect($thispageurl);
|
|
|
79 |
} else if ($formdata = $moveform->get_data()) {
|
|
|
80 |
list($tocategoryid, $tocontextid) = explode(',', $formdata->category);
|
1441 |
ariadna |
81 |
$manager->move_questions_and_delete_category($formdata->delete, $tocategoryid);
|
1 |
efrain |
82 |
$thispageurl->remove_params('cat', 'category');
|
|
|
83 |
redirect($thispageurl);
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
} else {
|
|
|
87 |
$questionstomove = 0;
|
|
|
88 |
}
|
|
|
89 |
|
1441 |
ariadna |
90 |
if ((!empty($todelete) && (!$questionstomove) && confirm_sesskey())) {
|
|
|
91 |
$manager->delete_category($todelete);// Delete the category now no questions to move.
|
1 |
efrain |
92 |
$thispageurl->remove_params('cat', 'category');
|
|
|
93 |
redirect($thispageurl);
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
$PAGE->set_title(get_string('editcategories', 'question'));
|
|
|
97 |
$PAGE->set_heading($COURSE->fullname);
|
|
|
98 |
$PAGE->activityheader->disable();
|
|
|
99 |
|
|
|
100 |
// Print horizontal nav if needed.
|
|
|
101 |
$renderer = $PAGE->get_renderer('core_question', 'bank');
|
|
|
102 |
|
1441 |
ariadna |
103 |
echo $OUTPUT->header();
|
|
|
104 |
$qbankaction = new \core_question\output\qbank_action_menu($thispageurl);
|
1 |
efrain |
105 |
echo $renderer->render($qbankaction);
|
1441 |
ariadna |
106 |
if ($questionstomove) {
|
1 |
efrain |
107 |
$vars = new stdClass();
|
|
|
108 |
$vars->name = $category->name;
|
|
|
109 |
$vars->count = $questionstomove;
|
|
|
110 |
echo $OUTPUT->box(get_string('categorymove', 'question', $vars), 'generalbox boxaligncenter');
|
|
|
111 |
$moveform->display();
|
|
|
112 |
} else {
|
1441 |
ariadna |
113 |
// Get module contexts we have capabilities to manage.
|
|
|
114 |
$contextswithcaps = $contexts->having_one_edit_tab_cap('categories');
|
|
|
115 |
$modcontexts = array_filter($contextswithcaps, static fn ($context) => $context->contextlevel === CONTEXT_MODULE);
|
1 |
efrain |
116 |
// Display the user interface.
|
1441 |
ariadna |
117 |
$questioncategories = new question_categories(
|
|
|
118 |
$thispageurl,
|
|
|
119 |
$modcontexts,
|
|
|
120 |
$cmid,
|
|
|
121 |
$courseid,
|
|
|
122 |
$thiscontext,
|
|
|
123 |
);
|
|
|
124 |
$PAGE->requires->js_call_amd('qbank_managecategories/categorymanager', 'init'); // Load reactive module.
|
|
|
125 |
echo $OUTPUT->render(new categories_header($questioncategories));
|
|
|
126 |
echo $OUTPUT->render(new categories($questioncategories));
|
1 |
efrain |
127 |
}
|
1441 |
ariadna |
128 |
|
1 |
efrain |
129 |
echo $OUTPUT->footer();
|