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 create quiz grade items for a quiz.
31
 *
32
 * The user must have the 'mod/quiz:manage' capability for the quiz.
33
 *
34
 * The new grade items are added in order, after all the existing ones.
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 create_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
                    'name' => new external_value(PARAM_TEXT,
53
                        'The name for the grade item to create. If empty string, a sensible default is used.'),
54
                ])
55
            ),
56
        ]);
57
    }
58
 
59
    /**
60
     * Add new grade items to this quiz.
61
     *
62
     * New items are added in order, at the end of the sort order.
63
     *
64
     * @param int $quizid the id of the quiz to add the grade items to.
65
     * @param array $gradeitems list of grade items to create. (Each one must have property name.)
66
     */
67
    public static function execute(int $quizid, array $gradeitems): void {
68
        global $DB;
69
 
70
        [
71
            'quizid' => $quizid,
72
            'quizgradeitems' => $gradeitems,
73
        ] = self::validate_parameters(self::execute_parameters(), [
74
            'quizid' => $quizid,
75
            'quizgradeitems' => $gradeitems,
76
        ]);
77
 
78
        // Check the request is valid.
79
        $quizobj = quiz_settings::create($quizid);
80
        require_capability('mod/quiz:manage', $quizobj->get_context());
81
        self::validate_context($quizobj->get_context());
82
 
83
        $transaction = $DB->start_delegated_transaction();
84
 
85
        $structure = $quizobj->get_structure();
86
        foreach ($gradeitems as $gradeitemdata) {
87
            $gradeitem = (object) $gradeitemdata;
88
            $gradeitem->quizid = $quizid;
89
            $structure->create_grade_item($gradeitem);
90
        }
91
 
92
        $transaction->allow_commit();
93
    }
94
 
95
    /**
96
     * Define the webservice response.
97
     *
98
     * @return external_description|null always null.
99
     */
100
    public static function execute_returns(): ?external_description {
101
        return null;
102
    }
103
}