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');
|
|
|
27 |
require_once($CFG->dirroot."/question/editlib.php");
|
|
|
28 |
|
|
|
29 |
use qbank_managecategories\form\question_move_form;
|
|
|
30 |
use qbank_managecategories\helper;
|
|
|
31 |
use qbank_managecategories\question_category_object;
|
|
|
32 |
|
|
|
33 |
require_login();
|
|
|
34 |
core_question\local\bank\helper::require_plugin_enabled(helper::PLUGINNAME);
|
|
|
35 |
|
|
|
36 |
list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) =
|
|
|
37 |
question_edit_setup('categories', '/question/bank/managecategories/category.php');
|
|
|
38 |
|
|
|
39 |
// Get values from form for actions on this page.
|
|
|
40 |
$param = new stdClass();
|
|
|
41 |
$param->moveup = optional_param('moveup', 0, PARAM_INT);
|
|
|
42 |
$param->movedown = optional_param('movedown', 0, PARAM_INT);
|
|
|
43 |
$param->moveupcontext = optional_param('moveupcontext', 0, PARAM_INT);
|
|
|
44 |
$param->movedowncontext = optional_param('movedowncontext', 0, PARAM_INT);
|
|
|
45 |
$param->tocontext = optional_param('tocontext', 0, PARAM_INT);
|
|
|
46 |
$param->left = optional_param('left', 0, PARAM_INT);
|
|
|
47 |
$param->right = optional_param('right', 0, PARAM_INT);
|
|
|
48 |
$param->delete = optional_param('delete', 0, PARAM_INT);
|
|
|
49 |
$param->confirm = optional_param('confirm', 0, PARAM_INT);
|
|
|
50 |
$param->cancel = optional_param('cancel', '', PARAM_ALPHA);
|
|
|
51 |
$param->move = optional_param('move', 0, PARAM_INT);
|
|
|
52 |
$param->moveto = optional_param('moveto', 0, PARAM_INT);
|
|
|
53 |
$param->edit = optional_param('edit', null, PARAM_INT);
|
|
|
54 |
|
|
|
55 |
$url = new moodle_url($thispageurl);
|
|
|
56 |
foreach ((array)$param as $key => $value) {
|
|
|
57 |
if (($key !== 'cancel' && $key !== 'edit' && $value !== 0) ||
|
|
|
58 |
($key === 'cancel' && $value !== '') ||
|
|
|
59 |
($key === 'edit' && $value !== null)) {
|
|
|
60 |
$url->param($key, $value);
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
$PAGE->set_url($url);
|
|
|
64 |
|
|
|
65 |
$qcobject = new question_category_object($pagevars['cpage'], $thispageurl,
|
|
|
66 |
$contexts->having_one_edit_tab_cap('categories'), $param->edit,
|
|
|
67 |
$pagevars['cat'], $param->delete, $contexts->having_cap('moodle/question:add'));
|
|
|
68 |
|
|
|
69 |
if ($param->left || $param->right || $param->moveup || $param->movedown) {
|
|
|
70 |
require_sesskey();
|
|
|
71 |
|
|
|
72 |
foreach ($qcobject->editlists as $list) {
|
|
|
73 |
// Processing of these actions is handled in the method where appropriate and page redirects.
|
|
|
74 |
$list->process_actions($param->left, $param->right, $param->moveup, $param->movedown);
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
if ($param->moveupcontext || $param->movedowncontext) {
|
|
|
79 |
require_sesskey();
|
|
|
80 |
|
|
|
81 |
if ($param->moveupcontext) {
|
|
|
82 |
$catid = $param->moveupcontext;
|
|
|
83 |
} else {
|
|
|
84 |
$catid = $param->movedowncontext;
|
|
|
85 |
}
|
|
|
86 |
$newtopcat = question_get_top_category($param->tocontext);
|
|
|
87 |
if (!$newtopcat) {
|
|
|
88 |
throw new moodle_exception('invalidcontext');
|
|
|
89 |
}
|
|
|
90 |
$oldcat = $DB->get_record('question_categories', ['id' => $catid], '*', MUST_EXIST);
|
|
|
91 |
// Log the move to another context.
|
|
|
92 |
$category = new stdClass();
|
|
|
93 |
$category->id = explode(',', $pagevars['cat'], -1)[0];
|
|
|
94 |
$category->contextid = $param->tocontext;
|
|
|
95 |
$event = \core\event\question_category_moved::create_from_question_category_instance($category);
|
|
|
96 |
$event->trigger();
|
|
|
97 |
// Update the set_reference records when moving a category to a different context.
|
|
|
98 |
move_question_set_references($catid, $catid, $oldcat->contextid, $category->contextid);
|
|
|
99 |
$qcobject->update_category($catid, "{$newtopcat->id},{$param->tocontext}", $oldcat->name, $oldcat->info);
|
|
|
100 |
// The previous line does a redirect().
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
if ($param->delete) {
|
|
|
104 |
if (!$category = $DB->get_record("question_categories", ["id" => $param->delete])) {
|
|
|
105 |
throw new moodle_exception('nocate', 'question', $thispageurl->out(), $param->delete);
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
helper::question_remove_stale_questions_from_category($param->delete);
|
|
|
109 |
|
|
|
110 |
$questionstomove = count($qcobject->get_real_question_ids_in_category($param->delete));
|
|
|
111 |
|
|
|
112 |
// Second pass, if we still have questions to move, setup the form.
|
|
|
113 |
if ($questionstomove) {
|
|
|
114 |
$categorycontext = context::instance_by_id($category->contextid);
|
|
|
115 |
$moveform = new question_move_form($thispageurl,
|
|
|
116 |
['contexts' => [$categorycontext], 'currentcat' => $param->delete]);
|
|
|
117 |
if ($moveform->is_cancelled()) {
|
|
|
118 |
redirect($thispageurl);
|
|
|
119 |
} else if ($formdata = $moveform->get_data()) {
|
|
|
120 |
list($tocategoryid, $tocontextid) = explode(',', $formdata->category);
|
|
|
121 |
$qcobject->move_questions_and_delete_category($formdata->delete, $tocategoryid);
|
|
|
122 |
$thispageurl->remove_params('cat', 'category');
|
|
|
123 |
redirect($thispageurl);
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
} else {
|
|
|
127 |
$questionstomove = 0;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
if ($qcobject->catform->is_cancelled()) {
|
|
|
131 |
redirect($thispageurl);
|
|
|
132 |
} else if ($catformdata = $qcobject->catform->get_data()) {
|
|
|
133 |
$catformdata->infoformat = $catformdata->info['format'];
|
|
|
134 |
$catformdata->info = $catformdata->info['text'];
|
|
|
135 |
if (!$catformdata->id) {// New category.
|
|
|
136 |
$qcobject->add_category($catformdata->parent, $catformdata->name,
|
|
|
137 |
$catformdata->info, false, $catformdata->infoformat, $catformdata->idnumber);
|
|
|
138 |
} else {
|
|
|
139 |
$qcobject->update_category($catformdata->id, $catformdata->parent,
|
|
|
140 |
$catformdata->name, $catformdata->info, $catformdata->infoformat, $catformdata->idnumber);
|
|
|
141 |
}
|
|
|
142 |
redirect($thispageurl);
|
|
|
143 |
} else if ((!empty($param->delete) and (!$questionstomove) and confirm_sesskey())) {
|
|
|
144 |
$qcobject->delete_category($param->delete);// Delete the category now no questions to move.
|
|
|
145 |
$thispageurl->remove_params('cat', 'category');
|
|
|
146 |
redirect($thispageurl);
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
if ($param->edit !== null || $qcobject->catform->is_submitted()) {
|
|
|
150 |
// In the is_submitted case, we only get here if it was submitted,
|
|
|
151 |
// but not valid, so we need to show the validation error.
|
|
|
152 |
$PAGE->navbar->add(get_string('editingcategory', 'question'));
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
$PAGE->set_title(get_string('editcategories', 'question'));
|
|
|
156 |
$PAGE->set_heading($COURSE->fullname);
|
|
|
157 |
$PAGE->activityheader->disable();
|
|
|
158 |
|
|
|
159 |
echo $OUTPUT->header();
|
|
|
160 |
|
|
|
161 |
// Print horizontal nav if needed.
|
|
|
162 |
$renderer = $PAGE->get_renderer('core_question', 'bank');
|
|
|
163 |
|
|
|
164 |
$qbankaction = new \core_question\output\qbank_action_menu($url);
|
|
|
165 |
if (is_null($param->edit)) {
|
|
|
166 |
$actionurl = new moodle_url($url, ['edit' => 0]);
|
|
|
167 |
$qbankaction->set_action_button($actionurl, get_string('addcategory', 'question'));
|
|
|
168 |
}
|
|
|
169 |
echo $renderer->render($qbankaction);
|
|
|
170 |
|
|
|
171 |
// Display the UI.
|
|
|
172 |
if ($param->edit !== null || $qcobject->catform->is_submitted()) {
|
|
|
173 |
// In the is_submitted case, we only get here if it was submitted,
|
|
|
174 |
// but not valid, so we need to show the validation error.
|
|
|
175 |
// In this case, category id is in the 'id' hidden filed.
|
|
|
176 |
$qcobject->edit_single_category($param->edit ?? required_param('id', PARAM_INT));
|
|
|
177 |
} else if ($questionstomove) {
|
|
|
178 |
$vars = new stdClass();
|
|
|
179 |
$vars->name = $category->name;
|
|
|
180 |
$vars->count = $questionstomove;
|
|
|
181 |
echo $OUTPUT->box(get_string('categorymove', 'question', $vars), 'generalbox boxaligncenter');
|
|
|
182 |
$moveform->display();
|
|
|
183 |
} else {
|
|
|
184 |
// Display the user interface.
|
|
|
185 |
$qcobject->display_user_interface();
|
|
|
186 |
}
|
|
|
187 |
echo $OUTPUT->footer();
|