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 delete quiz grade items.
31
 *
32
 * The user must have the 'mod/quiz:manage' capability for the quiz.
33
 *
34
 * The grade items to be deleted must all belong to the same quiz,
35
 * and must not be referred to by any slot.
36
 *
37
 * @package   mod_quiz
38
 * @copyright 2023 The Open University
39
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
41
class delete_grade_items extends external_api {
42
 
43
    /**
44
     * Declare the method parameters.
45
     *
46
     * @return external_function_parameters
47
     */
48
    public static function execute_parameters(): external_function_parameters {
49
        return new external_function_parameters([
50
            'quizid' => new external_value(PARAM_INT, 'The quiz to update slots for.'),
51
            'quizgradeitems' => new external_multiple_structure(
52
                new external_single_structure([
53
                    'id' => new external_value(PARAM_INT, 'id of the quiz grade item'),
54
                ])
55
            ),
56
        ]);
57
    }
58
 
59
    /**
60
     * Delete quiz grade items, if they are unused.
61
     *
62
     * @param int $quizid the id of the quiz from which to dlete grade items.
63
     * @param array $gradeitems list of grade items to delete. (They must belong to this quiz.)
64
     */
65
    public static function execute(int $quizid, array $gradeitems): void {
66
        global $DB;
67
 
68
        [
69
            'quizid' => $quizid,
70
            'quizgradeitems' => $gradeitems,
71
        ] = self::validate_parameters(self::execute_parameters(), [
72
            'quizid' => $quizid,
73
            'quizgradeitems' => $gradeitems,
74
        ]);
75
 
76
        // Check the request is valid.
77
        $quizobj = quiz_settings::create($quizid);
78
        require_capability('mod/quiz:manage', $quizobj->get_context());
79
        self::validate_context($quizobj->get_context());
80
 
81
        $transaction = $DB->start_delegated_transaction();
82
 
83
        $structure = $quizobj->get_structure();
84
        foreach ($gradeitems as $gradeitemdata) {
85
            $structure->delete_grade_item($gradeitemdata['id']);
86
        }
87
 
88
        $transaction->allow_commit();
89
    }
90
 
91
    /**
92
     * Define the webservice response.
93
     *
94
     * @return external_description|null always null.
95
     */
96
    public static function execute_returns(): ?external_description {
97
        return null;
98
    }
99
}