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 scale 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\scale\external;
31 use core_grades\component_gradeitem as gradeitem;
32 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;
45 * External grading panel scale API
47 * @package core_grades
48 * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
49 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
51 class fetch extends external_api {
54 * Describes the parameters for fetching the grading panel for a simple grade.
56 * @return external_function_parameters
59 public static function execute_parameters(): external_function_parameters {
60 return new external_function_parameters ([
61 'component' => new external_value(
63 'The name of the component',
66 'contextid' => new external_value(
68 'The ID of the context being graded',
71 'itemname' => new external_value(
73 'The grade item itemname being graded',
76 'gradeduserid' => new external_value(
78 'The ID of the user show',
85 * Fetch the data required to build a grading panel for a simple grade.
87 * @param string $component
88 * @param int $contextid
89 * @param string $itemname
90 * @param int $gradeduserid
94 public static function execute(string $component, int $contextid, string $itemname, int $gradeduserid): array {
98 'component' => $component,
99 'contextid' => $contextid,
100 'itemname' => $itemname,
101 'gradeduserid' => $gradeduserid,
102 ] = self::validate_parameters(self::execute_parameters(), [
103 'component' => $component,
104 'contextid' => $contextid,
105 'itemname' => $itemname,
106 'gradeduserid' => $gradeduserid,
109 // Validate the context.
110 $context = context::instance_by_id($contextid);
111 self::validate_context($context);
113 // Validate that the supplied itemname is a gradable item.
114 if (!component_gradeitems::is_valid_itemname($component, $itemname)) {
115 throw new coding_exception("The '{$itemname}' item is not valid for the '{$component}' component");
118 // Fetch the gradeitem instance.
119 $gradeitem = gradeitem::instance($component, $context, $itemname);
121 if (!$gradeitem->is_using_scale()) {
122 throw new moodle_exception("The {$itemname} item in {$component}/{$contextid} is not configured for grading with scales");
125 $gradeduser = \core_user::get_user($gradeduserid);
127 return self::get_fetch_data($gradeitem, $gradeduser);
131 * Get the data to be fetched.
133 * @param component_gradeitem $gradeitem
136 public static function get_fetch_data(gradeitem $gradeitem, stdClass $gradeduser): array {
139 $grade = $gradeitem->get_grade_for_user($gradeduser, $USER);
140 $currentgrade = (int) unformat_float($grade->grade);
142 $menu = $gradeitem->get_grade_menu();
143 $values = array_map(function($description, $value) use ($currentgrade) {
146 'title' => $description,
147 'selected' => ($value == $currentgrade),
149 }, $menu, array_keys($menu));
152 'templatename' => 'core_grades/grades/grader/gradingpanel/scale',
154 'options' => $values,
155 'timecreated' => $grade->timecreated,
156 'timemodified' => $grade->timemodified,
163 * Describes the data returned from the external function.
165 * @return external_single_structure
168 public static function execute_returns(): external_single_structure {
169 return new external_single_structure([
170 'templatename' => new external_value(PARAM_SAFEPATH, 'The template to use when rendering this data'),
171 'grade' => new external_single_structure([
172 'options' => new external_multiple_structure(
173 new external_single_structure([
174 'value' => new external_value(PARAM_FLOAT, 'The grade value'),
175 'title' => new external_value(PARAM_RAW, 'The description fo the option'),
176 'selected' => new external_value(PARAM_BOOL, 'Whether this item is currently selected'),
178 'The description of the grade option'
180 'timecreated' => new external_value(PARAM_INT, 'The time that the grade was created'),
181 'timemodified' => new external_value(PARAM_INT, 'The time that the grade was last updated'),
183 'warnings' => new external_warnings(),