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

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

https://bitbucket.org/moodle/moodle
PHP | 322 lines | 211 code | 30 blank | 81 comment | 15 complexity | 9e1cc41e4fe63dab3516ce3bd9a17f3c 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 rubric for the grading panel.
  18. *
  19. * @package gradingform_rubric
  20. * @copyright 2019 Mathew May <mathew.solutions>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. declare(strict_types = 1);
  24. namespace gradingform_rubric\grades\grader\gradingpanel\external;
  25. global $CFG;
  26. use coding_exception;
  27. use context;
  28. use core_grades\component_gradeitem as gradeitem;
  29. use core_grades\component_gradeitems;
  30. use external_api;
  31. use external_function_parameters;
  32. use external_multiple_structure;
  33. use external_single_structure;
  34. use external_value;
  35. use external_warnings;
  36. use stdClass;
  37. use moodle_exception;
  38. require_once($CFG->dirroot.'/grade/grading/form/rubric/lib.php');
  39. /**
  40. * Web services relating to fetching of a rubric for the grading panel.
  41. *
  42. * @package gradingform_rubric
  43. * @copyright 2019 Mathew May <mathew.solutions>
  44. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45. */
  46. class fetch extends external_api {
  47. /**
  48. * Describes the parameters for fetching the grading panel for a simple grade.
  49. *
  50. * @return external_function_parameters
  51. * @since Moodle 3.8
  52. */
  53. public static function execute_parameters(): external_function_parameters {
  54. return new external_function_parameters ([
  55. 'component' => new external_value(
  56. PARAM_ALPHANUMEXT,
  57. 'The name of the component',
  58. VALUE_REQUIRED
  59. ),
  60. 'contextid' => new external_value(
  61. PARAM_INT,
  62. 'The ID of the context being graded',
  63. VALUE_REQUIRED
  64. ),
  65. 'itemname' => new external_value(
  66. PARAM_ALPHANUM,
  67. 'The grade item itemname being graded',
  68. VALUE_REQUIRED
  69. ),
  70. 'gradeduserid' => new external_value(
  71. PARAM_INT,
  72. 'The ID of the user show',
  73. VALUE_REQUIRED
  74. ),
  75. ]);
  76. }
  77. /**
  78. * Fetch the data required to build a grading panel for a simple grade.
  79. *
  80. * @param string $component
  81. * @param int $contextid
  82. * @param string $itemname
  83. * @param int $gradeduserid
  84. * @return array
  85. * @since Moodle 3.8
  86. */
  87. public static function execute(string $component, int $contextid, string $itemname, int $gradeduserid): array {
  88. global $CFG, $USER;
  89. require_once("{$CFG->libdir}/gradelib.php");
  90. [
  91. 'component' => $component,
  92. 'contextid' => $contextid,
  93. 'itemname' => $itemname,
  94. 'gradeduserid' => $gradeduserid,
  95. ] = self::validate_parameters(self::execute_parameters(), [
  96. 'component' => $component,
  97. 'contextid' => $contextid,
  98. 'itemname' => $itemname,
  99. 'gradeduserid' => $gradeduserid,
  100. ]);
  101. // Validate the context.
  102. $context = context::instance_by_id($contextid);
  103. self::validate_context($context);
  104. // Validate that the supplied itemname is a gradable item.
  105. if (!component_gradeitems::is_valid_itemname($component, $itemname)) {
  106. throw new coding_exception("The '{$itemname}' item is not valid for the '{$component}' component");
  107. }
  108. // Fetch the gradeitem instance.
  109. $gradeitem = gradeitem::instance($component, $context, $itemname);
  110. if (RUBRIC !== $gradeitem->get_advanced_grading_method()) {
  111. throw new moodle_exception(
  112. "The {$itemname} item in {$component}/{$contextid} is not configured for advanced grading with a rubric"
  113. );
  114. }
  115. // Fetch the actual data.
  116. $gradeduser = \core_user::get_user($gradeduserid, '*', MUST_EXIST);
  117. // One can access its own grades. Others just if they're graders.
  118. if ($gradeduserid != $USER->id) {
  119. $gradeitem->require_user_can_grade($gradeduser, $USER);
  120. }
  121. return self::get_fetch_data($gradeitem, $gradeduser);
  122. }
  123. /**
  124. * Get the data to be fetched and create the structure ready for Mustache.
  125. *
  126. * @param gradeitem $gradeitem
  127. * @param stdClass $gradeduser
  128. * @return array
  129. */
  130. public static function get_fetch_data(gradeitem $gradeitem, stdClass $gradeduser): array {
  131. global $USER;
  132. // Set up all the controllers etc that we'll be needing.
  133. $hasgrade = $gradeitem->user_has_grade($gradeduser);
  134. $grade = $gradeitem->get_formatted_grade_for_user($gradeduser, $USER);
  135. $instance = $gradeitem->get_advanced_grading_instance($USER, $grade);
  136. if (!$instance) {
  137. throw new moodle_exception('error:gradingunavailable', 'grading');
  138. }
  139. $controller = $instance->get_controller();
  140. $definition = $controller->get_definition();
  141. $fillings = $instance->get_rubric_filling();
  142. $context = $controller->get_context();
  143. $definitionid = (int) $definition->id;
  144. // Set up some items we need to return on other interfaces.
  145. $gradegrade = \grade_grade::fetch(['itemid' => $gradeitem->get_grade_item()->id, 'userid' => $gradeduser->id]);
  146. $gradername = $gradegrade ? fullname(\core_user::get_user($gradegrade->usermodified)) : null;
  147. $maxgrade = max(array_keys($controller->get_grade_range()));
  148. $teacherdescription = self::get_formatted_text(
  149. $context,
  150. $definitionid,
  151. 'description',
  152. $definition->description,
  153. (int) $definition->descriptionformat
  154. );
  155. $criterion = [];
  156. if ($definition->rubric_criteria) {
  157. // Iterate over the defined criterion in the rubric and map out what we need to render each item.
  158. $criterion = array_map(function($criterion) use ($definitionid, $fillings, $context, $hasgrade) {
  159. // The general structure we'll be returning, we still need to get the remark (if any) and the levels associated.
  160. $result = [
  161. 'id' => $criterion['id'],
  162. 'description' => self::get_formatted_text(
  163. $context,
  164. $definitionid,
  165. 'description',
  166. $criterion['description'],
  167. (int) $criterion['descriptionformat']
  168. ),
  169. ];
  170. // Do we have an existing grade filling? if so lets get the remark associated to this criteria.
  171. $filling = [];
  172. if (array_key_exists($criterion['id'], $fillings['criteria'])) {
  173. $filling = $fillings['criteria'][$criterion['id']];
  174. $result['remark'] = self::get_formatted_text($context,
  175. $definitionid,
  176. 'remark',
  177. $filling['remark'],
  178. (int) $filling['remarkformat']
  179. );
  180. }
  181. // Lets build the levels within a criteria and figure out what needs to go where.
  182. $result['levels'] = array_map(function($level) use ($criterion, $filling, $context, $definitionid) {
  183. // The bulk of what'll be returned can be defined easily we'll add to this further down.
  184. $result = [
  185. 'id' => $level['id'],
  186. 'criterionid' => $criterion['id'],
  187. 'score' => $level['score'],
  188. 'definition' => self::get_formatted_text(
  189. $context,
  190. $definitionid,
  191. 'definition',
  192. $level['definition'],
  193. (int) $level['definitionformat']
  194. ),
  195. 'checked' => null,
  196. ];
  197. // Consult the grade filling to see if a level has been selected and if it is the current level.
  198. if (array_key_exists('levelid', $filling) && $filling['levelid'] == $level['id']) {
  199. $result['checked'] = true;
  200. }
  201. return $result;
  202. }, $criterion['levels']);
  203. $nulllevel = [
  204. 'id' => null,
  205. 'criterionid' => $criterion['id'],
  206. 'score' => '-',
  207. 'definition' => get_string('notset', 'gradingform_rubric'),
  208. 'checked' => !$hasgrade,
  209. ];
  210. // Consult the grade filling to see if a level has been selected and if it is the current level.
  211. if (array_key_exists('levelid', $filling) && $filling['levelid'] == 0) {
  212. $nulllevel['checked'] = true;
  213. }
  214. array_unshift($result['levels'], $nulllevel);
  215. return $result;
  216. }, $definition->rubric_criteria);
  217. }
  218. return [
  219. 'templatename' => 'gradingform_rubric/grades/grader/gradingpanel',
  220. 'hasgrade' => $hasgrade,
  221. 'grade' => [
  222. 'instanceid' => $instance->get_id(),
  223. 'criteria' => $criterion,
  224. 'rubricmode' => 'evaluate editable',
  225. 'teacherdescription' => $teacherdescription,
  226. 'canedit' => false,
  227. 'usergrade' => $grade->usergrade,
  228. 'maxgrade' => $maxgrade,
  229. 'gradedby' => $gradername,
  230. 'timecreated' => $grade->timecreated,
  231. 'timemodified' => $grade->timemodified,
  232. ],
  233. 'warnings' => [],
  234. ];
  235. }
  236. /**
  237. * Describes the data returned from the external function.
  238. *
  239. * @return external_single_structure
  240. * @since Moodle 3.8
  241. */
  242. public static function execute_returns(): external_single_structure {
  243. return new external_single_structure([
  244. 'templatename' => new external_value(PARAM_SAFEPATH, 'The template to use when rendering this data'),
  245. 'hasgrade' => new external_value(PARAM_BOOL, 'Does the user have a grade?'),
  246. 'grade' => new external_single_structure([
  247. 'instanceid' => new external_value(PARAM_INT, 'The id of the current grading instance'),
  248. 'rubricmode' => new external_value(PARAM_RAW, 'The mode i.e. evaluate editable'),
  249. 'canedit' => new external_value(PARAM_BOOL, 'Can the user edit this'),
  250. 'criteria' => new external_multiple_structure(
  251. new external_single_structure([
  252. 'id' => new external_value(PARAM_INT, 'ID of the Criteria'),
  253. 'description' => new external_value(PARAM_RAW, 'Description of the Criteria'),
  254. 'remark' => new external_value(PARAM_RAW, 'Any remarks for this criterion for the user being assessed', VALUE_OPTIONAL),
  255. 'levels' => new external_multiple_structure(new external_single_structure([
  256. 'id' => new external_value(PARAM_INT, 'ID of level'),
  257. 'criterionid' => new external_value(PARAM_INT, 'ID of the criterion this matches to'),
  258. 'score' => new external_value(PARAM_RAW, 'What this level is worth'),
  259. 'definition' => new external_value(PARAM_RAW, 'Definition of the level'),
  260. 'checked' => new external_value(PARAM_BOOL, 'Selected flag'),
  261. ])),
  262. ])
  263. ),
  264. 'timecreated' => new external_value(PARAM_INT, 'The time that the grade was created'),
  265. 'usergrade' => new external_value(PARAM_RAW, 'Current user grade'),
  266. 'maxgrade' => new external_value(PARAM_RAW, 'Max possible grade'),
  267. 'gradedby' => new external_value(PARAM_RAW, 'The assumed grader of this grading instance'),
  268. 'timemodified' => new external_value(PARAM_INT, 'The time that the grade was last updated'),
  269. ]),
  270. 'warnings' => new external_warnings(),
  271. ]);
  272. }
  273. /**
  274. * Get a formatted version of the remark/description/etc.
  275. *
  276. * @param context $context
  277. * @param int $definitionid
  278. * @param string $filearea The file area of the field
  279. * @param string $text The text to be formatted
  280. * @param int $format The input format of the string
  281. * @return string
  282. */
  283. protected static function get_formatted_text(context $context, int $definitionid, string $filearea, string $text, int $format): string {
  284. $formatoptions = [
  285. 'noclean' => false,
  286. 'trusted' => false,
  287. 'filter' => true,
  288. ];
  289. [$newtext, ] = external_format_text($text, $format, $context, 'grading', $filearea, $definitionid, $formatoptions);
  290. return $newtext;
  291. }
  292. }