| 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 |
namespace mod_quiz\external;
|
|
|
18 |
|
|
|
19 |
defined('MOODLE_INTERNAL') || die();
|
|
|
20 |
|
|
|
21 |
require_once($CFG->dirroot . '/question/engine/lib.php');
|
|
|
22 |
require_once($CFG->dirroot . '/question/engine/datalib.php');
|
|
|
23 |
require_once($CFG->libdir . '/questionlib.php');
|
|
|
24 |
|
| 1441 |
ariadna |
25 |
use core\context\module;
|
| 1 |
efrain |
26 |
use core_external\external_api;
|
|
|
27 |
use core_external\external_function_parameters;
|
|
|
28 |
use core_external\external_single_structure;
|
|
|
29 |
use core_external\external_value;
|
| 1441 |
ariadna |
30 |
use mod_quiz\quiz_settings;
|
| 1 |
efrain |
31 |
|
|
|
32 |
/**
|
|
|
33 |
* External api for changing the question version in the quiz.
|
|
|
34 |
*
|
|
|
35 |
* @package mod_quiz
|
|
|
36 |
* @copyright 2021 Catalyst IT Australia Pty Ltd
|
|
|
37 |
* @author Safat Shahin <safatshahin@catalyst-au.net>
|
|
|
38 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
39 |
*/
|
|
|
40 |
class submit_question_version extends external_api {
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Parameters for the submit_question_version.
|
|
|
44 |
*
|
|
|
45 |
* @return external_function_parameters
|
|
|
46 |
*/
|
|
|
47 |
public static function execute_parameters(): external_function_parameters {
|
|
|
48 |
return new external_function_parameters([
|
| 1441 |
ariadna |
49 |
'slotid' => new external_value(PARAM_INT),
|
|
|
50 |
'newversion' => new external_value(PARAM_INT),
|
| 1 |
efrain |
51 |
]);
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Set the questions slot parameters to display the question template.
|
|
|
56 |
*
|
| 1441 |
ariadna |
57 |
* @param int $slotid Slot ID to display.
|
|
|
58 |
* @param int|null $newversion The version to set. Passing null means 'always latest'.
|
|
|
59 |
* For historical reasons, 0 also means 'always latest'.
|
| 1 |
efrain |
60 |
* @return array
|
|
|
61 |
*/
|
| 1441 |
ariadna |
62 |
public static function execute(int $slotid, ?int $newversion): array {
|
| 1 |
efrain |
63 |
global $DB;
|
| 1441 |
ariadna |
64 |
$params = self::validate_parameters(self::execute_parameters(), ['slotid' => $slotid, 'newversion' => $newversion]);
|
|
|
65 |
$slot = $DB->get_record('quiz_slots', ['id' => $slotid], '*', MUST_EXIST);
|
| 1 |
efrain |
66 |
|
| 1441 |
ariadna |
67 |
$context = module::instance(get_course_and_cm_from_instance($slot->quizid, 'quiz')[1]->id);
|
| 1 |
efrain |
68 |
self::validate_context($context);
|
|
|
69 |
require_capability('mod/quiz:manage', $context);
|
|
|
70 |
|
| 1441 |
ariadna |
71 |
$quizobj = quiz_settings::create($slot->quizid);
|
|
|
72 |
|
|
|
73 |
// This WS historically (and wrongly) accepted 0 to mean 'always latest'. The correct behaviour
|
|
|
74 |
// is that null implies awlays latest. To preserve backwards compatibility, we continue to accept
|
|
|
75 |
// 0, but just turn it in to null before passing to the appropriate API. See: MDL-82587.
|
|
|
76 |
$newversionnormalised = $params['newversion'] === 0 ? null : $params['newversion'];
|
|
|
77 |
return ['result' => $quizobj->get_structure()->update_slot_version($slot->id, $newversionnormalised)];
|
| 1 |
efrain |
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Define the webservice response.
|
|
|
82 |
*
|
|
|
83 |
* @return \core_external\external_description
|
|
|
84 |
*/
|
|
|
85 |
public static function execute_returns() {
|
|
|
86 |
return new external_single_structure([
|
|
|
87 |
'result' => new external_value(PARAM_BOOL, '')
|
|
|
88 |
|
|
|
89 |
]);
|
|
|
90 |
}
|
|
|
91 |
}
|