2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Web service functions relating to point grades and grading.
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
25 declare(strict_types = 1);
27 namespace core_grades\grades\grader\gradingpanel\point\external;
32 use core_grades\component_gradeitem as gradeitem;
33 use core_grades\component_gradeitems;
35 use external_function_parameters;
36 use external_multiple_structure;
37 use external_single_structure;
39 use external_warnings;
41 use required_capability_exception;
44 * External grading panel point API
46 * @package core_grades
47 * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
48 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
50 class store extends external_api {
53 * Describes the parameters for fetching the grading panel for a simple grade.
55 * @return external_function_parameters
58 public static function execute_parameters(): external_function_parameters {
59 return new external_function_parameters ([
60 'component' => new external_value(
62 'The name of the component',
65 'contextid' => new external_value(
67 'The ID of the context being graded',
70 'itemname' => new external_value(
72 'The grade item itemname being graded',
75 'gradeduserid' => new external_value(
77 'The ID of the user show',
80 'formdata' => new external_value(
82 'The serialised form data representing the grade',
89 * Fetch the data required to build a grading panel for a simple grade.
91 * @param string $component
92 * @param int $contextid
93 * @param string $itemname
94 * @param int $gradeduserid
98 public static function execute(string $component, int $contextid, string $itemname, int $gradeduserid, string $formdata): array {
102 'component' => $component,
103 'contextid' => $contextid,
104 'itemname' => $itemname,
105 'gradeduserid' => $gradeduserid,
106 'formdata' => $formdata,
107 ] = self::validate_parameters(self::execute_parameters(), [
108 'component' => $component,
109 'contextid' => $contextid,
110 'itemname' => $itemname,
111 'gradeduserid' => $gradeduserid,
112 'formdata' => $formdata,
115 // Validate the context.
116 $context = context::instance_by_id($contextid);
117 self::validate_context($context);
119 // Validate that the supplied itemname is a gradable item.
120 if (!component_gradeitems::is_valid_itemname($component, $itemname)) {
121 throw new coding_exception("The '{$itemname}' item is not valid for the '{$component}' component");
124 // Fetch the gradeitem instance.
125 $gradeitem = gradeitem::instance($component, $context, $itemname);
127 // Validate that this gradeitem is actually enabled.
128 if (!$gradeitem->is_grading_enabled()) {
129 throw new moodle_exception("Grading is not enabled for {$itemname} in this context");
132 // Fetch the record for the graded user.
133 $gradeduser = \core_user::get_user($gradeduserid);
135 // Require that this user can save grades.
136 $gradeitem->require_user_can_grade($gradeduser, $USER);
138 if (!$gradeitem->is_using_direct_grading()) {
139 throw new moodle_exception("The {$itemname} item in {$component}/{$contextid} is not configured for direct grading");
142 // Parse the serialised string into an object.
144 parse_str($formdata, $data);
147 $gradeitem->store_grade_from_formdata($gradeduser, $USER, (object) $data);
149 // Fetch the updated grade back out.
150 $grade = $gradeitem->get_grade_for_user($gradeduser, $USER);
152 return fetch::get_fetch_data($grade);
156 * Describes the data returned from the external function.
158 * @return external_single_structure
161 public static function execute_returns(): external_single_structure {
162 return fetch::execute_returns();