PageRenderTime 24ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/grade/grading/form/guide/classes/grades/grader/gradingpanel/external/fetch.php

https://bitbucket.org/moodle/moodle
PHP | 314 lines | 208 code | 28 blank | 78 comment | 10 complexity | a10675646a5660d745475737306de0db MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * Web services relating to fetching of a marking guide for the grading panel.
  18. *
  19. * @package gradingform_guide
  20. * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. declare(strict_types = 1);
  24. namespace gradingform_guide\grades\grader\gradingpanel\external;
  25. global $CFG;
  26. use coding_exception;
  27. use context;
  28. use core_user;
  29. use core_grades\component_gradeitem as gradeitem;
  30. use core_grades\component_gradeitems;
  31. use external_api;
  32. use external_format_value;
  33. use external_function_parameters;
  34. use external_multiple_structure;
  35. use external_single_structure;
  36. use external_value;
  37. use external_warnings;
  38. use moodle_exception;
  39. use stdClass;
  40. require_once($CFG->dirroot.'/grade/grading/form/guide/lib.php');
  41. /**
  42. * Web services relating to fetching of a marking guide for the grading panel.
  43. *
  44. * @package gradingform_guide
  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. * Describes the parameters for fetching the grading panel for a simple grade.
  51. *
  52. * @return external_function_parameters
  53. * @since Moodle 3.8
  54. */
  55. public static function execute_parameters(): external_function_parameters {
  56. return new external_function_parameters ([
  57. 'component' => new external_value(
  58. PARAM_ALPHANUMEXT,
  59. 'The name of the component',
  60. VALUE_REQUIRED
  61. ),
  62. 'contextid' => new external_value(
  63. PARAM_INT,
  64. 'The ID of the context being graded',
  65. VALUE_REQUIRED
  66. ),
  67. 'itemname' => new external_value(
  68. PARAM_ALPHANUM,
  69. 'The grade item itemname being graded',
  70. VALUE_REQUIRED
  71. ),
  72. 'gradeduserid' => new external_value(
  73. PARAM_INT,
  74. 'The ID of the user show',
  75. VALUE_REQUIRED
  76. ),
  77. ]);
  78. }
  79. /**
  80. * Fetch the data required to build a grading panel for a simple grade.
  81. *
  82. * @param string $component
  83. * @param int $contextid
  84. * @param string $itemname
  85. * @param int $gradeduserid
  86. * @return array
  87. * @throws \dml_exception
  88. * @throws \invalid_parameter_exception
  89. * @throws \restricted_context_exception
  90. * @throws coding_exception
  91. * @throws moodle_exception
  92. * @since Moodle 3.8
  93. */
  94. public static function execute(string $component, int $contextid, string $itemname, int $gradeduserid): array {
  95. global $CFG, $USER;
  96. require_once("{$CFG->libdir}/gradelib.php");
  97. [
  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,
  107. ]);
  108. // Validate the context.
  109. $context = context::instance_by_id($contextid);
  110. self::validate_context($context);
  111. // Validate that the supplied itemname is a gradable item.
  112. if (!component_gradeitems::is_valid_itemname($component, $itemname)) {
  113. throw new coding_exception("The '{$itemname}' item is not valid for the '{$component}' component");
  114. }
  115. // Fetch the gradeitem instance.
  116. $gradeitem = gradeitem::instance($component, $context, $itemname);
  117. if (MARKING_GUIDE !== $gradeitem->get_advanced_grading_method()) {
  118. throw new moodle_exception(
  119. "The {$itemname} item in {$component}/{$contextid} is not configured for advanced grading with a marking guide"
  120. );
  121. }
  122. // Fetch the actual data.
  123. $gradeduser = core_user::get_user($gradeduserid, '*', MUST_EXIST);
  124. // One can access its own grades. Others just if they're graders.
  125. if ($gradeduserid != $USER->id) {
  126. $gradeitem->require_user_can_grade($gradeduser, $USER);
  127. }
  128. return self::get_fetch_data($gradeitem, $gradeduser);
  129. }
  130. /**
  131. * Get the data to be fetched.
  132. *
  133. * @param gradeitem $gradeitem
  134. * @param stdClass $gradeduser
  135. * @return array
  136. */
  137. public static function get_fetch_data(gradeitem $gradeitem, stdClass $gradeduser): array {
  138. global $USER;
  139. $hasgrade = $gradeitem->user_has_grade($gradeduser);
  140. $grade = $gradeitem->get_formatted_grade_for_user($gradeduser, $USER);
  141. $instance = $gradeitem->get_advanced_grading_instance($USER, $grade);
  142. if (!$instance) {
  143. throw new moodle_exception('error:gradingunavailable', 'grading');
  144. }
  145. $controller = $instance->get_controller();
  146. $definition = $controller->get_definition();
  147. $fillings = $instance->get_guide_filling();
  148. $context = $controller->get_context();
  149. $definitionid = (int) $definition->id;
  150. // Set up some items we need to return on other interfaces.
  151. $gradegrade = \grade_grade::fetch(['itemid' => $gradeitem->get_grade_item()->id, 'userid' => $gradeduser->id]);
  152. $gradername = $gradegrade ? fullname(\core_user::get_user($gradegrade->usermodified)) : null;
  153. $maxgrade = max(array_keys($controller->get_grade_range()));
  154. $criterion = [];
  155. if ($definition->guide_criteria) {
  156. $criterion = array_map(function($criterion) use ($definitionid, $fillings, $context) {
  157. $result = [
  158. 'id' => $criterion['id'],
  159. 'name' => $criterion['shortname'],
  160. 'maxscore' => $criterion['maxscore'],
  161. 'description' => self::get_formatted_text(
  162. $context,
  163. $definitionid,
  164. 'description',
  165. $criterion['description'],
  166. (int) $criterion['descriptionformat']
  167. ),
  168. 'descriptionmarkers' => self::get_formatted_text(
  169. $context,
  170. $definitionid,
  171. 'descriptionmarkers',
  172. $criterion['descriptionmarkers'],
  173. (int) $criterion['descriptionmarkersformat']
  174. ),
  175. 'score' => null,
  176. 'remark' => null,
  177. ];
  178. if (array_key_exists($criterion['id'], $fillings['criteria'])) {
  179. $filling = $fillings['criteria'][$criterion['id']];
  180. $result['score'] = $filling['score'];
  181. $result['remark'] = self::get_formatted_text(
  182. $context,
  183. $definitionid,
  184. 'remark',
  185. $filling['remark'],
  186. (int) $filling['remarkformat']
  187. );
  188. }
  189. return $result;
  190. }, $definition->guide_criteria);
  191. }
  192. $comments = [];
  193. if ($definition->guide_comments) {
  194. $comments = array_map(function($comment) use ($definitionid, $context) {
  195. return [
  196. 'id' => $comment['id'],
  197. 'sortorder' => $comment['sortorder'],
  198. 'description' => self::get_formatted_text(
  199. $context,
  200. $definitionid,
  201. 'description',
  202. $comment['description'],
  203. (int) $comment['descriptionformat']
  204. ),
  205. ];
  206. }, $definition->guide_comments);
  207. }
  208. return [
  209. 'templatename' => 'gradingform_guide/grades/grader/gradingpanel',
  210. 'hasgrade' => $hasgrade,
  211. 'grade' => [
  212. 'instanceid' => $instance->get_id(),
  213. 'criterion' => $criterion,
  214. 'hascomments' => !empty($comments),
  215. 'comments' => $comments,
  216. 'usergrade' => $grade->usergrade,
  217. 'maxgrade' => $maxgrade,
  218. 'gradedby' => $gradername,
  219. 'timecreated' => $grade->timecreated,
  220. 'timemodified' => $grade->timemodified,
  221. ],
  222. 'warnings' => [],
  223. ];
  224. }
  225. /**
  226. * Describes the data returned from the external function.
  227. *
  228. * @return external_single_structure
  229. * @since Moodle 3.8
  230. */
  231. public static function execute_returns(): external_single_structure {
  232. return new external_single_structure([
  233. 'templatename' => new external_value(PARAM_SAFEPATH, 'The template to use when rendering this data'),
  234. 'hasgrade' => new external_value(PARAM_BOOL, 'Does the user have a grade?'),
  235. 'grade' => new external_single_structure([
  236. 'instanceid' => new external_value(PARAM_INT, 'The id of the current grading instance'),
  237. 'criterion' => new external_multiple_structure(
  238. new external_single_structure([
  239. 'id' => new external_value(PARAM_INT, 'The id of the criterion'),
  240. 'name' => new external_value(PARAM_RAW, 'The name of the criterion'),
  241. 'maxscore' => new external_value(PARAM_FLOAT, 'The maximum score for this criterion'),
  242. 'description' => new external_value(PARAM_RAW, 'The description of the criterion'),
  243. 'descriptionmarkers' => new external_value(PARAM_RAW, 'The description of the criterion for markers'),
  244. 'score' => new external_value(PARAM_FLOAT, 'The current score for user being assessed', VALUE_OPTIONAL),
  245. 'remark' => new external_value(PARAM_RAW, 'Any remarks for this criterion for the user being assessed', VALUE_OPTIONAL),
  246. ]),
  247. 'The criterion by which this item will be graded'
  248. ),
  249. 'hascomments' => new external_value(PARAM_BOOL, 'Whether there are any frequently-used comments'),
  250. 'comments' => new external_multiple_structure(
  251. new external_single_structure([
  252. 'id' => new external_value(PARAM_INT, 'Comment id'),
  253. 'sortorder' => new external_value(PARAM_INT, 'The sortorder of this comment'),
  254. 'description' => new external_value(PARAM_RAW, 'The comment value'),
  255. ]),
  256. 'Frequently used comments'
  257. ),
  258. 'usergrade' => new external_value(PARAM_RAW, 'Current user grade'),
  259. 'maxgrade' => new external_value(PARAM_RAW, 'Max possible grade'),
  260. 'gradedby' => new external_value(PARAM_RAW, 'The assumed grader of this grading instance'),
  261. 'timecreated' => new external_value(PARAM_INT, 'The time that the grade was created'),
  262. 'timemodified' => new external_value(PARAM_INT, 'The time that the grade was last updated'),
  263. ]),
  264. 'warnings' => new external_warnings(),
  265. ]);
  266. }
  267. /**
  268. * Get a formatted version of the remark/description/etc.
  269. *
  270. * @param context $context
  271. * @param int $definitionid
  272. * @param string $filearea The file area of the field
  273. * @param string $text The text to be formatted
  274. * @param int $format The input format of the string
  275. * @return string
  276. */
  277. protected static function get_formatted_text(context $context, int $definitionid, string $filearea, string $text, int $format): string {
  278. $formatoptions = [
  279. 'noclean' => false,
  280. 'trusted' => false,
  281. 'filter' => true,
  282. ];
  283. [$newtext, ] = external_format_text($text, $format, $context, 'grading', $filearea, $definitionid, $formatoptions);
  284. return $newtext;
  285. }
  286. }