Proyectos de Subversion Moodle

Rev

| 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_description;
21
use core_external\external_function_parameters;
22
use core_external\external_multiple_structure;
23
use core_external\external_single_structure;
24
use core_external\external_value;
25
use mod_quiz\quiz_attempt;
26
use mod_quiz\quiz_settings;
27
use moodle_exception;
28
 
29
/**
30
 * Web service method to update the properties of quiz grade items.
31
 *
32
 * The user must have the 'mod/quiz:manage' capability for the quiz.
33
 *
34
 * All the properties that can be set are optional. Only the ones passed are changed.
35
 *
36
 * @package   mod_quiz
37
 * @copyright 2023 The Open University
38
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
40
class update_grade_items extends external_api {
41
 
42
    /**
43
     * Declare the method parameters.
44
     *
45
     * @return external_function_parameters
46
     */
47
    public static function execute_parameters(): external_function_parameters {
48
        return new external_function_parameters([
49
            'quizid' => new external_value(PARAM_INT, 'The quiz to update slots for.'),
50
            'quizgradeitems' => new external_multiple_structure(
51
                new external_single_structure([
52
                    'id' => new external_value(PARAM_INT, 'id of the quiz grade item'),
53
                    'name' => new external_value(
54
                        PARAM_TEXT,
55
                        'If passed, new name to set. Null, or not specified, to leave unchanged.',
56
                        VALUE_OPTIONAL
57
                    ),
58
                ])
59
            ),
60
        ]);
61
    }
62
 
63
    /**
64
     * Update grade items to this quiz.
65
     *
66
     * @param int $quizid the id of the quiz from which to dlete grade items.
67
     * @param array $gradeitems list of grade items to update. Must have properties id and name
68
     */
69
    public static function execute(int $quizid, array $gradeitems): void {
70
        global $DB;
71
 
72
        [
73
            'quizid' => $quizid,
74
            'quizgradeitems' => $gradeitems,
75
        ] = self::validate_parameters(self::execute_parameters(), [
76
            'quizid' => $quizid,
77
            'quizgradeitems' => $gradeitems,
78
        ]);
79
 
80
        // Check the request is valid.
81
        $quizobj = quiz_settings::create($quizid);
82
        require_capability('mod/quiz:manage', $quizobj->get_context());
83
        self::validate_context($quizobj->get_context());
84
 
85
        $transaction = $DB->start_delegated_transaction();
86
 
87
        $structure = $quizobj->get_structure();
88
        foreach ($gradeitems as $gradeitemdata) {
89
            if ($gradeitemdata['name'] !== null) {
90
                $structure->update_grade_item((object) $gradeitemdata);
91
            }
92
        }
93
 
94
        $transaction->allow_commit();
95
    }
96
 
97
    /**
98
     * Define the webservice response.
99
     *
100
     * @return external_description|null always null.
101
     */
102
    public static function execute_returns(): ?external_description {
103
        return null;
104
    }
105
}