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