| Línea 20... |
Línea 20... |
| 20 |
|
20 |
|
| 21 |
require_once($CFG->dirroot . '/question/engine/lib.php');
|
21 |
require_once($CFG->dirroot . '/question/engine/lib.php');
|
| 22 |
require_once($CFG->dirroot . '/question/engine/datalib.php');
|
22 |
require_once($CFG->dirroot . '/question/engine/datalib.php');
|
| Línea -... |
Línea 23... |
| - |
|
23 |
require_once($CFG->libdir . '/questionlib.php');
|
| 23 |
require_once($CFG->libdir . '/questionlib.php');
|
24 |
|
| 24 |
|
25 |
use core\context\module;
|
| 25 |
use core_external\external_api;
|
26 |
use core_external\external_api;
|
| 26 |
use core_external\external_function_parameters;
|
27 |
use core_external\external_function_parameters;
|
| 27 |
use core_external\external_single_structure;
|
28 |
use core_external\external_single_structure;
|
| Línea 28... |
Línea 29... |
| 28 |
use core_external\external_value;
|
29 |
use core_external\external_value;
|
| 29 |
use stdClass;
|
30 |
use mod_quiz\quiz_settings;
|
| 30 |
|
31 |
|
| 31 |
/**
|
32 |
/**
|
| Línea 43... |
Línea 44... |
| 43 |
*
|
44 |
*
|
| 44 |
* @return external_function_parameters
|
45 |
* @return external_function_parameters
|
| 45 |
*/
|
46 |
*/
|
| 46 |
public static function execute_parameters(): external_function_parameters {
|
47 |
public static function execute_parameters(): external_function_parameters {
|
| 47 |
return new external_function_parameters([
|
48 |
return new external_function_parameters([
|
| 48 |
'slotid' => new external_value(PARAM_INT, ''),
|
49 |
'slotid' => new external_value(PARAM_INT),
|
| 49 |
'newversion' => new external_value(PARAM_INT, '')
|
50 |
'newversion' => new external_value(PARAM_INT),
|
| 50 |
]);
|
51 |
]);
|
| 51 |
}
|
52 |
}
|
| Línea 52... |
Línea 53... |
| 52 |
|
53 |
|
| 53 |
/**
|
54 |
/**
|
| 54 |
* Set the questions slot parameters to display the question template.
|
55 |
* Set the questions slot parameters to display the question template.
|
| 55 |
*
|
56 |
*
|
| 56 |
* @param int $slotid Slot id to display.
|
57 |
* @param int $slotid Slot ID to display.
|
| - |
|
58 |
* @param int|null $newversion The version to set. Passing null means 'always latest'.
|
| 57 |
* @param int $newversion the version to set. 0 means 'always latest'.
|
59 |
* For historical reasons, 0 also means 'always latest'.
|
| 58 |
* @return array
|
60 |
* @return array
|
| 59 |
*/
|
61 |
*/
|
| 60 |
public static function execute(int $slotid, int $newversion): array {
|
62 |
public static function execute(int $slotid, ?int $newversion): array {
|
| 61 |
global $DB;
|
- |
|
| 62 |
$params = [
|
- |
|
| 63 |
'slotid' => $slotid,
|
- |
|
| 64 |
'newversion' => $newversion
|
- |
|
| 65 |
];
|
63 |
global $DB;
|
| 66 |
$params = self::validate_parameters(self::execute_parameters(), $params);
|
- |
|
| 67 |
$response = [];
|
- |
|
| 68 |
// Get the required data.
|
- |
|
| 69 |
$referencedata = $DB->get_record('question_references',
|
- |
|
| 70 |
['itemid' => $params['slotid'], 'component' => 'mod_quiz', 'questionarea' => 'slot']);
|
64 |
$params = self::validate_parameters(self::execute_parameters(), ['slotid' => $slotid, 'newversion' => $newversion]);
|
| 71 |
$slotdata = $DB->get_record('quiz_slots', ['id' => $slotid]);
|
65 |
$slot = $DB->get_record('quiz_slots', ['id' => $slotid], '*', MUST_EXIST);
|
| 72 |
|
- |
|
| 73 |
// Capability check.
|
66 |
|
| 74 |
[, $cm] = get_course_and_cm_from_instance($slotdata->quizid, 'quiz');
|
- |
|
| 75 |
$context = \context_module::instance($cm->id);
|
67 |
$context = module::instance(get_course_and_cm_from_instance($slot->quizid, 'quiz')[1]->id);
|
| 76 |
self::validate_context($context);
|
68 |
self::validate_context($context);
|
| Línea 77... |
Línea 69... |
| 77 |
require_capability('mod/quiz:manage', $context);
|
69 |
require_capability('mod/quiz:manage', $context);
|
| - |
|
70 |
|
| 78 |
|
71 |
$quizobj = quiz_settings::create($slot->quizid);
|
| 79 |
$reference = new stdClass();
|
72 |
|
| 80 |
$reference->id = $referencedata->id;
|
73 |
// This WS historically (and wrongly) accepted 0 to mean 'always latest'. The correct behaviour
|
| 81 |
if ($params['newversion'] === 0) {
|
- |
|
| 82 |
$reference->version = null;
|
74 |
// is that null implies awlays latest. To preserve backwards compatibility, we continue to accept
|
| 83 |
} else {
|
- |
|
| 84 |
$reference->version = $params['newversion'];
|
75 |
// 0, but just turn it in to null before passing to the appropriate API. See: MDL-82587.
|
| 85 |
}
|
- |
|
| 86 |
$response['result'] = $DB->update_record('question_references', $reference);
|
76 |
$newversionnormalised = $params['newversion'] === 0 ? null : $params['newversion'];
|
| Línea 87... |
Línea 77... |
| 87 |
return $response;
|
77 |
return ['result' => $quizobj->get_structure()->update_slot_version($slot->id, $newversionnormalised)];
|
| 88 |
}
|
78 |
}
|
| 89 |
|
79 |
|