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_multiple_structure;
|
|
|
36 |
use core_external\external_single_structure;
|
|
|
37 |
use core_external\external_value;
|
|
|
38 |
use core_external\external_warnings;
|
|
|
39 |
use moodle_exception;
|
|
|
40 |
use stdClass;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* External grading panel scale API
|
|
|
44 |
*
|
|
|
45 |
* @package core_grades
|
|
|
46 |
* @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
|
|
|
47 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
48 |
*/
|
|
|
49 |
class fetch extends external_api {
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Describes the parameters for fetching the grading panel for a simple grade.
|
|
|
53 |
*
|
|
|
54 |
* @return external_function_parameters
|
|
|
55 |
* @since Moodle 3.8
|
|
|
56 |
*/
|
|
|
57 |
public static function execute_parameters(): external_function_parameters {
|
|
|
58 |
return new external_function_parameters ([
|
|
|
59 |
'component' => new external_value(
|
|
|
60 |
PARAM_ALPHANUMEXT,
|
|
|
61 |
'The name of the component',
|
|
|
62 |
VALUE_REQUIRED
|
|
|
63 |
),
|
|
|
64 |
'contextid' => new external_value(
|
|
|
65 |
PARAM_INT,
|
|
|
66 |
'The ID of the context being graded',
|
|
|
67 |
VALUE_REQUIRED
|
|
|
68 |
),
|
|
|
69 |
'itemname' => new external_value(
|
|
|
70 |
PARAM_ALPHANUM,
|
|
|
71 |
'The grade item itemname being graded',
|
|
|
72 |
VALUE_REQUIRED
|
|
|
73 |
),
|
|
|
74 |
'gradeduserid' => new external_value(
|
|
|
75 |
PARAM_INT,
|
|
|
76 |
'The ID of the user show',
|
|
|
77 |
VALUE_REQUIRED
|
|
|
78 |
),
|
|
|
79 |
]);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Fetch the data required to build a grading panel for a simple grade.
|
|
|
84 |
*
|
|
|
85 |
* @param string $component
|
|
|
86 |
* @param int $contextid
|
|
|
87 |
* @param string $itemname
|
|
|
88 |
* @param int $gradeduserid
|
|
|
89 |
* @return array
|
|
|
90 |
* @throws \dml_exception
|
|
|
91 |
* @throws \invalid_parameter_exception
|
|
|
92 |
* @throws \restricted_context_exception
|
|
|
93 |
* @throws coding_exception
|
|
|
94 |
* @throws moodle_exception
|
|
|
95 |
* @since Moodle 3.8
|
|
|
96 |
*/
|
|
|
97 |
public static function execute(string $component, int $contextid, string $itemname, int $gradeduserid): array {
|
|
|
98 |
global $USER, $CFG;
|
|
|
99 |
require_once("{$CFG->libdir}/gradelib.php");
|
|
|
100 |
[
|
|
|
101 |
'component' => $component,
|
|
|
102 |
'contextid' => $contextid,
|
|
|
103 |
'itemname' => $itemname,
|
|
|
104 |
'gradeduserid' => $gradeduserid,
|
|
|
105 |
] = self::validate_parameters(self::execute_parameters(), [
|
|
|
106 |
'component' => $component,
|
|
|
107 |
'contextid' => $contextid,
|
|
|
108 |
'itemname' => $itemname,
|
|
|
109 |
'gradeduserid' => $gradeduserid,
|
|
|
110 |
]);
|
|
|
111 |
|
|
|
112 |
// Validate the context.
|
|
|
113 |
$context = context::instance_by_id($contextid);
|
|
|
114 |
self::validate_context($context);
|
|
|
115 |
|
|
|
116 |
// Validate that the supplied itemname is a gradable item.
|
|
|
117 |
if (!component_gradeitems::is_valid_itemname($component, $itemname)) {
|
|
|
118 |
throw new coding_exception("The '{$itemname}' item is not valid for the '{$component}' component");
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
// Fetch the gradeitem instance.
|
|
|
122 |
$gradeitem = gradeitem::instance($component, $context, $itemname);
|
|
|
123 |
|
|
|
124 |
if (!$gradeitem->is_using_scale()) {
|
|
|
125 |
throw new moodle_exception("The {$itemname} item in {$component}/{$contextid} is not configured for grading with scales");
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
$gradeduser = \core_user::get_user($gradeduserid, '*', MUST_EXIST);
|
|
|
129 |
|
|
|
130 |
// One can access its own grades. Others just if they're graders.
|
|
|
131 |
if ($gradeduserid != $USER->id) {
|
|
|
132 |
$gradeitem->require_user_can_grade($gradeduser, $USER);
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
// Set up some items we need to return on other interfaces.
|
|
|
136 |
$gradegrade = \grade_grade::fetch(['itemid' => $gradeitem->get_grade_item()->id, 'userid' => $gradeduser->id]);
|
|
|
137 |
$gradername = $gradegrade ? fullname(\core_user::get_user($gradegrade->usermodified)) : null;
|
|
|
138 |
$maxgrade = (int) $gradeitem->get_grade_item()->grademax;
|
|
|
139 |
|
|
|
140 |
return self::get_fetch_data($gradeitem, $gradeduser, $maxgrade, $gradername);
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* Get the data to be fetched.
|
|
|
145 |
*
|
|
|
146 |
* @param gradeitem $gradeitem
|
|
|
147 |
* @param stdClass $gradeduser
|
|
|
148 |
* @param int $maxgrade
|
|
|
149 |
* @param string|null $gradername
|
|
|
150 |
* @return array
|
|
|
151 |
*/
|
|
|
152 |
public static function get_fetch_data(gradeitem $gradeitem, stdClass $gradeduser, int $maxgrade, ?string $gradername): array {
|
|
|
153 |
global $USER;
|
|
|
154 |
|
|
|
155 |
$hasgrade = $gradeitem->user_has_grade($gradeduser);
|
|
|
156 |
$grade = $gradeitem->get_formatted_grade_for_user($gradeduser, $USER);
|
|
|
157 |
$currentgrade = (int) unformat_float($grade->grade);
|
|
|
158 |
|
|
|
159 |
$menu = $gradeitem->get_grade_menu();
|
|
|
160 |
$values = array_map(function($description, $value) use ($currentgrade) {
|
|
|
161 |
return [
|
|
|
162 |
'value' => $value,
|
|
|
163 |
'title' => $description,
|
|
|
164 |
'selected' => ($value == $currentgrade),
|
|
|
165 |
];
|
|
|
166 |
}, $menu, array_keys($menu));
|
|
|
167 |
|
|
|
168 |
return [
|
|
|
169 |
'templatename' => 'core_grades/grades/grader/gradingpanel/scale',
|
|
|
170 |
'hasgrade' => $hasgrade,
|
|
|
171 |
'grade' => [
|
|
|
172 |
'options' => $values,
|
|
|
173 |
'usergrade' => $grade->usergrade,
|
|
|
174 |
'maxgrade' => $maxgrade,
|
|
|
175 |
'gradedby' => $gradername,
|
|
|
176 |
'timecreated' => $grade->timecreated,
|
|
|
177 |
'timemodified' => $grade->timemodified,
|
|
|
178 |
],
|
|
|
179 |
'warnings' => [],
|
|
|
180 |
];
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
/**
|
|
|
184 |
* Describes the data returned from the external function.
|
|
|
185 |
*
|
|
|
186 |
* @return external_single_structure
|
|
|
187 |
* @since Moodle 3.8
|
|
|
188 |
*/
|
|
|
189 |
public static function execute_returns(): external_single_structure {
|
|
|
190 |
return new external_single_structure([
|
|
|
191 |
'templatename' => new external_value(PARAM_SAFEPATH, 'The template to use when rendering this data'),
|
|
|
192 |
'hasgrade' => new external_value(PARAM_BOOL, 'Does the user have a grade?'),
|
|
|
193 |
'grade' => new external_single_structure([
|
|
|
194 |
'options' => new external_multiple_structure(
|
|
|
195 |
new external_single_structure([
|
|
|
196 |
'value' => new external_value(PARAM_FLOAT, 'The grade value'),
|
|
|
197 |
'title' => new external_value(PARAM_RAW, 'The description fo the option'),
|
|
|
198 |
'selected' => new external_value(PARAM_BOOL, 'Whether this item is currently selected'),
|
|
|
199 |
]),
|
|
|
200 |
'The description of the grade option'
|
|
|
201 |
),
|
|
|
202 |
'usergrade' => new external_value(PARAM_RAW, 'Current user grade'),
|
|
|
203 |
'maxgrade' => new external_value(PARAM_RAW, 'Max possible grade'),
|
|
|
204 |
'gradedby' => new external_value(PARAM_RAW, 'The assumed grader of this grading instance'),
|
|
|
205 |
'timecreated' => new external_value(PARAM_INT, 'The time that the grade was created'),
|
|
|
206 |
'timemodified' => new external_value(PARAM_INT, 'The time that the grade was last updated'),
|
|
|
207 |
]),
|
|
|
208 |
'warnings' => new external_warnings(),
|
|
|
209 |
]);
|
|
|
210 |
}
|
|
|
211 |
}
|