Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
use core_external\external_api;
20
use core_external\external_function_parameters;
21
use core_external\external_multiple_structure;
22
use core_external\external_single_structure;
23
use core_external\external_value;
24
use mod_quiz\quiz_settings;
25
 
26
/**
27
 * Webservice for upserting quiz overrides.
28
 *
29
 * @package   mod_quiz
30
 * @copyright 2024 Matthew Hilton <matthewhilton@catalyst-au.net>
31
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class save_overrides extends external_api {
34
    /**
35
     * Defines parameters
36
     *
37
     * @return external_function_parameters
38
     */
39
    public static function execute_parameters(): external_function_parameters {
40
        $overridestructure = new external_single_structure([
41
            'id' => new external_value(PARAM_INT, 'ID of existing override (if updating)', VALUE_DEFAULT, null),
42
            'groupid' => new external_value(PARAM_INT, 'ID of group', VALUE_DEFAULT, null),
43
            'userid' => new external_value(PARAM_INT, 'ID of user', VALUE_DEFAULT, null),
44
            'timeopen' => new external_value(PARAM_INT, 'Quiz override opening timestamp', VALUE_DEFAULT, null),
45
            'timeclose' => new external_value(PARAM_INT, 'Quiz override closing timestamp', VALUE_OPTIONAL, null),
46
            'timelimit' => new external_value(PARAM_INT, 'Quiz override time limit', VALUE_DEFAULT, null),
47
            'attempts' => new external_value(PARAM_INT, 'Quiz override attempt count', VALUE_DEFAULT, null),
48
            'password' => new external_value(PARAM_TEXT, 'Quiz override password', VALUE_DEFAULT, null),
49
        ]);
50
 
51
        return new external_function_parameters([
52
            // This must be nested in a single structure, because the overrides structure does not play nicely at the top level.
53
            'data' => new external_single_structure([
54
                'quizid' => new external_value(PARAM_INT, 'ID of quiz to save overrides to'),
55
                'overrides' => new external_multiple_structure($overridestructure),
56
            ]),
57
        ]);
58
    }
59
 
60
    /**
61
     * Executes webservice function, saving the requested overrides.
62
     *
63
     * @param array $data array with quizid key and overrides key containing list of overrides to save.
64
     * @return array with ids key which contains ids of created/updated overrides.
65
     */
66
    public static function execute($data): array {
67
        $params = self::validate_parameters(self::execute_parameters(), ['data' => $data])['data'];
68
 
69
        $quizsettings = quiz_settings::create($params['quizid']);
70
        $manager = $quizsettings->get_override_manager();
71
        self::validate_context($manager->context);
72
        $manager->require_manage_capability();
73
 
11 efrain 74
        // Filter for those overrides user can access.
75
        $overrides = array_filter(
76
            $params['overrides'],
77
            fn(array $override) => $manager->can_view_override(
78
                (object) $override,
79
                $quizsettings->get_course(),
80
                $quizsettings->get_cm(),
81
            ),
82
        );
83
 
1 efrain 84
        // Iterate over and save all overrides.
11 efrain 85
        $ids = array_map(fn($override) => $manager->save_override($override), $overrides);
1 efrain 86
 
87
        return ['ids' => $ids];
88
    }
89
 
90
    /**
91
     * Defines return type
92
     *
93
     * @return external_single_structure
94
     */
95
    public static function execute_returns(): external_single_structure {
96
        return new external_single_structure([
97
            'ids' => new external_multiple_structure(new external_value(PARAM_INT, 'ID of created/updated override')),
98
        ]);
99
    }
100
}