1441 |
ariadna |
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 |
namespace core_question\external;
|
|
|
18 |
|
|
|
19 |
use core\context;
|
|
|
20 |
use core\exception\moodle_exception;
|
|
|
21 |
use core\notification;
|
|
|
22 |
use core_external\external_api;
|
|
|
23 |
use core_external\external_function_parameters;
|
|
|
24 |
use core_external\external_value;
|
|
|
25 |
use core_external\restricted_context_exception;
|
|
|
26 |
use core_question\local\bank\filter_condition_manager;
|
|
|
27 |
use moodle_url;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* API for moving questions from one question bank category to another.
|
|
|
31 |
*
|
|
|
32 |
* @package core_question
|
|
|
33 |
* @copyright 2024 onwards Catalyst IT EU {@link https://catalyst-eu.net}
|
|
|
34 |
* @author Simon Adams <simon.adams@catalyst-eu.net>
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
*/
|
|
|
37 |
class move_questions extends external_api {
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Declare the method parameters.
|
|
|
41 |
*
|
|
|
42 |
* @return external_function_parameters
|
|
|
43 |
*/
|
|
|
44 |
public static function execute_parameters(): external_function_parameters {
|
|
|
45 |
return new external_function_parameters(
|
|
|
46 |
[
|
|
|
47 |
'newcontextid' => new external_value(PARAM_INT, 'Contextid of the target question bank'),
|
|
|
48 |
'newcategoryid' => new external_value(PARAM_INT, 'ID of the target question category'),
|
|
|
49 |
'questionids' => new external_value(PARAM_SEQUENCE, 'Comma separated list of question ids to move'),
|
|
|
50 |
'returnurl' => new external_value(PARAM_LOCALURL,
|
|
|
51 |
desc: 'A URL to add/update the filter param with the new category',
|
|
|
52 |
default: ''
|
|
|
53 |
),
|
|
|
54 |
]
|
|
|
55 |
);
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Define the webservice response.
|
|
|
60 |
*
|
|
|
61 |
* @return external_value
|
|
|
62 |
*/
|
|
|
63 |
public static function execute_returns(): external_value {
|
|
|
64 |
return new external_value(PARAM_URL, 'Modified URL with filter param containing the new question category', VALUE_OPTIONAL);
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Move questions to a new question category.
|
|
|
69 |
* Optionally provide a url to add/update it with the filter param containing the new category.
|
|
|
70 |
*
|
|
|
71 |
* @param int $newcontextid of the target question bank
|
|
|
72 |
* @param int $newcategoryid of the target category
|
|
|
73 |
* @param string $questionids comma separated list of question ids to move
|
|
|
74 |
* @param string $returnurlstring optional, provide this to have the filter url param added/updated to reflect the new category
|
|
|
75 |
* @return null|string if $returnurlstring was provided then an updated url which filters to the new category
|
|
|
76 |
*/
|
|
|
77 |
public static function execute(
|
|
|
78 |
int $newcontextid,
|
|
|
79 |
int $newcategoryid,
|
|
|
80 |
string $questionids,
|
|
|
81 |
string $returnurlstring = ''
|
|
|
82 |
): ?string {
|
|
|
83 |
global $DB;
|
|
|
84 |
|
|
|
85 |
[
|
|
|
86 |
'newcontextid' => $newcontextid,
|
|
|
87 |
'newcategoryid' => $newcategoryid,
|
|
|
88 |
'questionids' => $questionids,
|
|
|
89 |
'returnurl' => $returnurlstring,
|
|
|
90 |
] = self::validate_parameters(self::execute_parameters(), [
|
|
|
91 |
'newcontextid' => $newcontextid,
|
|
|
92 |
'newcategoryid' => $newcategoryid,
|
|
|
93 |
'questionids' => $questionids,
|
|
|
94 |
'returnurl' => $returnurlstring,
|
|
|
95 |
]);
|
|
|
96 |
|
|
|
97 |
$newcontext = context::instance_by_id($newcontextid);
|
|
|
98 |
self::validate_context($newcontext);
|
|
|
99 |
|
|
|
100 |
\core_question\local\bank\helper::require_plugin_enabled('qbank_bulkmove');
|
|
|
101 |
|
|
|
102 |
$contexts = new \core_question\local\bank\question_edit_contexts($newcontext);
|
|
|
103 |
$contexts->require_cap('moodle/question:add');
|
|
|
104 |
|
|
|
105 |
if (!$targetcategory = $DB->get_record('question_categories', ['id' => $newcategoryid, 'contextid' => $newcontextid])) {
|
|
|
106 |
throw new \moodle_exception('cannotfindcate', 'question');
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
\qbank_bulkmove\helper::bulk_move_questions($questionids, $targetcategory);
|
|
|
110 |
notification::success(get_string('questionsmoved', 'qbank_bulkmove'));
|
|
|
111 |
|
|
|
112 |
if ($returnurlstring) {
|
|
|
113 |
$returnurl = new moodle_url($returnurlstring);
|
|
|
114 |
$returnurl->param('cmid', $newcontext->instanceid);
|
|
|
115 |
$returnurl->param('cat', "{$newcategoryid},{$newcontextid}");
|
|
|
116 |
$returnurl->remove_params('category');
|
|
|
117 |
// We can only highlight 1 question, so only highlight if we're moving a single question.
|
|
|
118 |
$qids = explode(',', $questionids);
|
|
|
119 |
if (count($qids) === 1) {
|
|
|
120 |
$returnurl->param('lastchanged', reset($qids));
|
|
|
121 |
} else {
|
|
|
122 |
$returnurl->remove_params('lastchanged');
|
|
|
123 |
};
|
|
|
124 |
$filter = $returnurl->param('filter');
|
|
|
125 |
if ($filter) {
|
|
|
126 |
$returnfilters = filter_condition_manager::update_filter_param_to_category(
|
|
|
127 |
$filter,
|
|
|
128 |
$newcategoryid,
|
|
|
129 |
);
|
|
|
130 |
} else {
|
|
|
131 |
$returnfilters = json_encode(
|
|
|
132 |
filter_condition_manager::get_default_filter("{$newcategoryid},{$newcontextid}"),
|
|
|
133 |
JSON_THROW_ON_ERROR
|
|
|
134 |
);
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
$returnurl->param('filter', $returnfilters);
|
|
|
138 |
return $returnurl->out(false);
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
}
|