See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 401] [Versions 401 and 402] [Versions 401 and 403]
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_user; 32 use core_grades\component_gradeitem as gradeitem; 33 use core_grades\component_gradeitems; 34 use external_api; 35 use external_function_parameters; 36 use external_multiple_structure; 37 use external_single_structure; 38 use external_value; 39 use external_warnings; 40 use moodle_exception; 41 use required_capability_exception; 42 43 /** 44 * External grading panel point API 45 * 46 * @package core_grades 47 * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk> 48 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 49 */ 50 class store extends external_api { 51 52 /** 53 * Describes the parameters for fetching the grading panel for a simple grade. 54 * 55 * @return external_function_parameters 56 * @since Moodle 3.8 57 */ 58 public static function execute_parameters(): external_function_parameters { 59 return new external_function_parameters ([ 60 'component' => new external_value( 61 PARAM_ALPHANUMEXT, 62 'The name of the component', 63 VALUE_REQUIRED 64 ), 65 'contextid' => new external_value( 66 PARAM_INT, 67 'The ID of the context being graded', 68 VALUE_REQUIRED 69 ), 70 'itemname' => new external_value( 71 PARAM_ALPHANUM, 72 'The grade item itemname being graded', 73 VALUE_REQUIRED 74 ), 75 'gradeduserid' => new external_value( 76 PARAM_INT, 77 'The ID of the user show', 78 VALUE_REQUIRED 79 ), 80 'notifyuser' => new external_value( 81 PARAM_BOOL, 82 'Wheteher to notify the user or not', 83 VALUE_DEFAULT, 84 false 85 ), 86 'formdata' => new external_value( 87 PARAM_RAW, 88 'The serialised form data representing the grade', 89 VALUE_REQUIRED 90 ), 91 ]); 92 } 93 94 /** 95 * Fetch the data required to build a grading panel for a simple grade. 96 * 97 * @param string $component 98 * @param int $contextid 99 * @param string $itemname 100 * @param int $gradeduserid 101 * @param bool $notifyuser 102 * @param string $formdata 103 * @return array 104 * @throws \dml_exception 105 * @throws \invalid_parameter_exception 106 * @throws \restricted_context_exception 107 * @throws coding_exception 108 * @throws moodle_exception 109 * @since Moodle 3.8 110 */ 111 public static function execute(string $component, int $contextid, string $itemname, int $gradeduserid, 112 bool $notifyuser, string $formdata): array { 113 global $USER, $CFG; 114 require_once("{$CFG->libdir}/gradelib.php"); 115 [ 116 'component' => $component, 117 'contextid' => $contextid, 118 'itemname' => $itemname, 119 'gradeduserid' => $gradeduserid, 120 'notifyuser' => $notifyuser, 121 'formdata' => $formdata, 122 ] = self::validate_parameters(self::execute_parameters(), [ 123 'component' => $component, 124 'contextid' => $contextid, 125 'itemname' => $itemname, 126 'gradeduserid' => $gradeduserid, 127 'notifyuser' => $notifyuser, 128 'formdata' => $formdata, 129 ]); 130 131 // Validate the context. 132 $context = context::instance_by_id($contextid); 133 self::validate_context($context); 134 135 // Validate that the supplied itemname is a gradable item. 136 if (!component_gradeitems::is_valid_itemname($component, $itemname)) { 137 throw new coding_exception("The '{$itemname}' item is not valid for the '{$component}' component"); 138 } 139 140 // Fetch the gradeitem instance. 141 $gradeitem = gradeitem::instance($component, $context, $itemname); 142 143 // Validate that this gradeitem is actually enabled. 144 if (!$gradeitem->is_grading_enabled()) { 145 throw new moodle_exception("Grading is not enabled for {$itemname} in this context"); 146 } 147 148 // Fetch the record for the graded user. 149 $gradeduser = \core_user::get_user($gradeduserid); 150 151 // Require that this user can save grades. 152 $gradeitem->require_user_can_grade($gradeduser, $USER); 153 154 if (!$gradeitem->is_using_direct_grading()) { 155 throw new moodle_exception("The {$itemname} item in {$component}/{$contextid} is not configured for direct grading"); 156 } 157 158 // Parse the serialised string into an object. 159 $data = []; 160 parse_str($formdata, $data); 161 162 // Grade. 163 $gradeitem->store_grade_from_formdata($gradeduser, $USER, (object) $data); 164 $hasgrade = $gradeitem->user_has_grade($gradeduser); 165 166 // Notify. 167 if ($notifyuser) { 168 // Send notification. 169 $gradeitem->send_student_notification($gradeduser, $USER); 170 } 171 172 // Fetch the updated grade back out. 173 $grade = $gradeitem->get_formatted_grade_for_user($gradeduser, $USER); 174 175 $gradegrade = \grade_grade::fetch(['itemid' => $gradeitem->get_grade_item()->id, 'userid' => $gradeduser->id]); 176 $gradername = $gradegrade ? fullname(\core_user::get_user($gradegrade->usermodified)) : null; 177 178 return fetch::get_fetch_data($grade, $hasgrade, $gradeitem, $gradername); 179 } 180 181 /** 182 * Describes the data returned from the external function. 183 * 184 * @return external_single_structure 185 * @since Moodle 3.8 186 */ 187 public static function execute_returns(): external_single_structure { 188 return fetch::execute_returns(); 189 } 190 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body