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