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
/**
18
 * Web service functions relating to scale grades and grading.
19
 *
20
 * @package    core_grades
21
 * @copyright  2019 Andrew Nicols <andrew@nicols.co.uk>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
declare(strict_types = 1);
26
 
27
namespace core_grades\grades\grader\gradingpanel\scale\external;
28
 
29
use coding_exception;
30
use context;
31
use core_grades\component_gradeitem as gradeitem;
32
use core_grades\component_gradeitems;
33
use core_external\external_api;
34
use core_external\external_function_parameters;
35
use core_external\external_single_structure;
36
use core_external\external_value;
37
use moodle_exception;
38
 
39
/**
40
 * External grading panel scale API
41
 *
42
 * @package    core_grades
43
 * @copyright  2019 Andrew Nicols <andrew@nicols.co.uk>
44
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45
 */
46
class store extends external_api {
47
 
48
    /**
49
     * Describes the parameters for fetching the grading panel for a simple grade.
50
     *
51
     * @return external_function_parameters
52
     * @since Moodle 3.8
53
     */
54
    public static function execute_parameters(): external_function_parameters {
55
        return new external_function_parameters ([
56
            'component' => new external_value(
57
                PARAM_ALPHANUMEXT,
58
                'The name of the component',
59
                VALUE_REQUIRED
60
            ),
61
            'contextid' => new external_value(
62
                PARAM_INT,
63
                'The ID of the context being graded',
64
                VALUE_REQUIRED
65
            ),
66
            'itemname' => new external_value(
67
                PARAM_ALPHANUM,
68
                'The grade item itemname being graded',
69
                VALUE_REQUIRED
70
            ),
71
            'gradeduserid' => new external_value(
72
                PARAM_INT,
73
                'The ID of the user show',
74
                VALUE_REQUIRED
75
            ),
76
            'notifyuser' => new external_value(
77
                PARAM_BOOL,
78
                'Wheteher to notify the user or not',
79
                VALUE_DEFAULT,
80
                false
81
            ),
82
            'formdata' => new external_value(
83
                PARAM_RAW,
84
                'The serialised form data representing the grade',
85
                VALUE_REQUIRED
86
            ),
87
        ]);
88
    }
89
 
90
    /**
91
     * Fetch the data required to build a grading panel for a simple grade.
92
     *
93
     * @param string $component
94
     * @param int $contextid
95
     * @param string $itemname
96
     * @param int $gradeduserid
97
     * @param string $formdata
98
     * @param bool $notifyuser
99
     * @return array
100
     * @throws coding_exception
101
     * @throws moodle_exception
102
     * @since Moodle 3.8
103
     */
104
    public static function execute(string $component, int $contextid, string $itemname, int $gradeduserid,
105
            bool $notifyuser, string $formdata): array {
106
        global $USER, $CFG;
107
        require_once("{$CFG->libdir}/gradelib.php");
108
        [
109
            'component' => $component,
110
            'contextid' => $contextid,
111
            'itemname' => $itemname,
112
            'gradeduserid' => $gradeduserid,
113
            'notifyuser' => $notifyuser,
114
            'formdata' => $formdata,
115
        ] = self::validate_parameters(self::execute_parameters(), [
116
            'component' => $component,
117
            'contextid' => $contextid,
118
            'itemname' => $itemname,
119
            'gradeduserid' => $gradeduserid,
120
            'notifyuser' => $notifyuser,
121
            'formdata' => $formdata,
122
        ]);
123
 
124
        // Validate the context.
125
        $context = context::instance_by_id($contextid);
126
        self::validate_context($context);
127
 
128
        // Validate that the supplied itemname is a gradable item.
129
        if (!component_gradeitems::is_valid_itemname($component, $itemname)) {
130
            throw new coding_exception("The '{$itemname}' item is not valid for the '{$component}' component");
131
        }
132
 
133
        // Fetch the gradeitem instance.
134
        $gradeitem = gradeitem::instance($component, $context, $itemname);
135
 
136
        // Validate that this gradeitem is actually enabled.
137
        if (!$gradeitem->is_grading_enabled()) {
138
            throw new moodle_exception("Grading is not enabled for {$itemname} in this context");
139
        }
140
 
141
        // Fetch the record for the graded user.
142
        $gradeduser = \core_user::get_user($gradeduserid);
143
 
144
        // Require that this user can save grades.
145
        $gradeitem->require_user_can_grade($gradeduser, $USER);
146
 
147
        if (!$gradeitem->is_using_scale()) {
148
            throw new moodle_exception("The {$itemname} item in {$component}/{$contextid} is not configured for grading with scales");
149
        }
150
 
151
        // Parse the serialised string into an object.
152
        $data = [];
153
        parse_str($formdata, $data);
154
 
155
        // Grade.
156
        $gradeitem->store_grade_from_formdata($gradeduser, $USER, (object) $data);
157
 
158
        // Notify.
159
        if ($notifyuser) {
160
            // Send notification.
161
            $gradeitem->send_student_notification($gradeduser, $USER);
162
        }
163
 
164
        $gradegrade = \grade_grade::fetch(['itemid' => $gradeitem->get_grade_item()->id, 'userid' => $gradeduser->id]);
165
        $gradername = $gradegrade ? fullname(\core_user::get_user($gradegrade->usermodified)) : null;
166
        $maxgrade = (int) $gradeitem->get_grade_item()->grademax;
167
 
168
        return fetch::get_fetch_data($gradeitem, $gradeduser, $maxgrade, $gradername);
169
    }
170
 
171
    /**
172
     * Describes the data returned from the external function.
173
     *
174
     * @return external_single_structure
175
     * @since Moodle 3.8
176
     */
177
    public static function execute_returns(): external_single_structure {
178
        return fetch::execute_returns();
179
    }
180
}