From 9adedccd2b95609622bce641d8728d3d2e465ee7 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 25 Sep 2019 15:28:23 +0800 Subject: [PATCH] MDL-66722 core_grades: Add simple direct grading to gradingpanel Part of MDL-66074 --- .../grades/grader/gradingpanel/point.min.js | Bin 0 -> 566 bytes .../grader/gradingpanel/point.min.js.map | Bin 0 -> 2077 bytes .../grader/gradingpanel/repository.min.js | Bin 0 -> 669 bytes .../grader/gradingpanel/repository.min.js.map | Bin 0 -> 2596 bytes .../src/grades/grader/gradingpanel/point.js | 34 ++ .../grades/grader/gradingpanel/repository.js | 49 +++ .../gradingpanel/point/external/fetch.php | 167 ++++++++++ .../gradingpanel/point/external/store.php | 164 +++++++++ .../grades/grader/gradingpanel/point.mustache | 35 ++ ...gradingpanel_point_external_fetch_test.php | 200 +++++++++++ ...gradingpanel_point_external_store_test.php | 315 ++++++++++++++++++ lang/en/grades.php | 1 + lib/db/services.php | 17 + 13 files changed, 982 insertions(+) create mode 100644 grade/amd/build/grades/grader/gradingpanel/point.min.js create mode 100644 grade/amd/build/grades/grader/gradingpanel/point.min.js.map create mode 100644 grade/amd/build/grades/grader/gradingpanel/repository.min.js create mode 100644 grade/amd/build/grades/grader/gradingpanel/repository.min.js.map create mode 100644 grade/amd/src/grades/grader/gradingpanel/point.js create mode 100644 grade/amd/src/grades/grader/gradingpanel/repository.js create mode 100644 grade/classes/grades/grader/gradingpanel/point/external/fetch.php create mode 100644 grade/classes/grades/grader/gradingpanel/point/external/store.php create mode 100644 grade/templates/grades/grader/gradingpanel/point.mustache create mode 100644 grade/tests/grades_grader_gradingpanel_point_external_fetch_test.php create mode 100644 grade/tests/grades_grader_gradingpanel_point_external_store_test.php diff --git a/grade/amd/build/grades/grader/gradingpanel/point.min.js b/grade/amd/build/grades/grader/gradingpanel/point.min.js new file mode 100644 index 0000000000000000000000000000000000000000..a4191f06c49f89d83860345c6945d7665f7ccd8b GIT binary patch literal 566 zcmZvZF;BxV5QX>tieRM5mMo`pqlkflg$e{>La4erm(*2ihqGN#s{VJJL{$rAvd=!h z@7;5SwXv9SrXAt3A*j&jmkSM*u}udS+q`qe1}5Jbeszw5XOgL$uyfu7NBdN6J|m{h zhpeO3!8pr7uA~+R7CmM@kkNq^Ppb{;psE4?Os+!;`_##nOY{p@#TJ=7?4XS}znL9H z0m>%;-bW&|;h{HLf~qm-^{=y?GgUS#ba}>$17V0{Gnh_6odI_+KO_SX+c1aYk?Wpv zt#}g3uCDk=na|{Ed^s2soQ*jN1zp$f`DjW4HF2BVdvPpwKv{KRQdSaWEgO-Brfgj{ zYRK^w+mw?d&T2*C0q9|ee|RauQ71L%KHYve!Pi20Bm=GaMlKTlzcj0q@+8l1 cGw&j4TqNu1+uvRu%V8L`HCAoB+Cn$^0pp3uB>(^b literal 0 HcmV?d00001 diff --git a/grade/amd/build/grades/grader/gradingpanel/point.min.js.map b/grade/amd/build/grades/grader/gradingpanel/point.min.js.map new file mode 100644 index 0000000000000000000000000000000000000000..6712014fbe774dafef228e56456a9192557b4ceb GIT binary patch literal 2077 zcma)7ZByGg5dJGW`I3aw+3oeqm9`9u36L2GAql12adV^CO03kDTuDwy8UA~BSdpOR{M6}T1o4a(XIVAtmRB?Q|Vg!f48D2-u*e7#tSPl>6#xKew1ERLdznq zjM6?@y4F9erA683&!tcEq_$RSe?qz~g!jhEANEpHRz~9`JWq}G@{T0sWl2yd)=t_% zhJrQ54^04?8(ZSems;A*L>4kdU|i!(B?|Rb5|dI?6;ecJTI(Ty)#I!_Vy4IYDq)wL zSGbXI{4mwy-1OLla}nfgcER~4w!!TgOYlIH$nW1 z#4o)d+i>rrDJ(xM18l^Vf)lBw z6$OmyS)o!mQ>oNWf-aH!FA~G;G z(CG+Iw*PK_#|*W zn4Vr-O@R+@;F^!ed^o*%hqrxh@Kmm3gQd!PhD8RsSV;_WD%A@42}n&v?EWKu!L%rhUIbsaX@t}UbI`^!a7gj}Ht*K!b+piR zNIc8|ioc5wkPV{j?cHkF18P**Gca|ruK^Kv^khg~AvX`y1o0eopiwx)1g!C#J}LV6 z@AEeKi*6aL9abX!EEYlgnR0VR4`9_I_W*yqdi@r-&a7O+P^G4D@E-FYmxnqmqtrz8 z=YvLZ>l!ioiQ@!Y$hnUfRT1UBESl;VoxuuI*)|zUKv8#V;5ow^dc9)_3lf_#t?*dUAAkH@wpN|>%0G%j`gk88Ud^mXKL#G{v zSG&7Ae+l`m|6_$vl&{9=;O>{@Hn!9)q8x_kS2j6PB6rIybygi$~tWt xaP2N~Aq^{iZ8hwI@I?Cmv1necLzvyCFm$?v|A7BAEvaeaq-;icsWgrAhi{5auTlU2 literal 0 HcmV?d00001 diff --git a/grade/amd/build/grades/grader/gradingpanel/repository.min.js b/grade/amd/build/grades/grader/gradingpanel/repository.min.js new file mode 100644 index 0000000000000000000000000000000000000000..d019b21f46592e1cb394c64352d440aa357894eb GIT binary patch literal 669 zcmb`F!D_=W42JK0ip;wtV4R#&dfsJaW3W>xMYWaGC9#d{l$MZppXZXcyX-J_64EbQ z`afG?qYY*`la_GT5-9ZLW1y62(>gHNm4wcE9V}fK-!MEoOTjaqj+bzRGu!fJFfwRs za^U-7WP``d2huXI<=g&!)femLNQw@a222 z6^I6dJY18L)her^{!p6-XZKnT#Yh+iGTE%=k}Hn9nT5RKdm*9gihMKn7!Fn$=&^o8 zawS74%q(_HNNXg7w0|1O3J%ozdX%IcQZYt^@Yza^wlJif||@%`|O` zO|_k-ACBjhvm1n8{P6aF9r^gm_;*LVvD7OFP`5X8@j(}5`J8z>ki<1O7h!&Wysqxw NO7z-@qZd86#TV=5^y&Zr literal 0 HcmV?d00001 diff --git a/grade/amd/build/grades/grader/gradingpanel/repository.min.js.map b/grade/amd/build/grades/grader/gradingpanel/repository.min.js.map new file mode 100644 index 0000000000000000000000000000000000000000..2d08834e0a31511ef96d177bc2f866f7cf6e9140 GIT binary patch literal 2596 zcmd5;4NoIC5dABosL}!5X6fDOq~`7rCM1wHfshYq;1FkaovdNk-rC-T6y?8n#=9Y$ zii&hn)vh3GKaa<6p2wbjtj&e7Qmb0yT~M>SFp;pe##OCe51;<5iNcxTv9RUK_?J|( zoGX!rM&#N`r_Hi{XKU9%P4TRBG8HaLj%a2rsJUfMuSjRPRzf-7R!%%P8Dm>Ikb+O$&liK7;uRICroB(aq1kMjNwQqVR#Dt;x?5LbY&!z z6~K`uAvO^)+?I}LN}E^=D;Ch!=$SjbDFP4@#|>0B2fSM^Qh5wO8&7A8J0fpD=QME} z880E_NNhc~{zCMdAY+iqvrlx6@g(RDBV5Qdg@GGGtaM2kZpN>bsP9|`{ z1_O37ntViSygYDJ%th%-W_cw$ zn;Z!_M}8b4ofErP_RlZPQ>|t`)+&vVT4xGMy8tXw{8Z)LVzH>tRN)g3bAa~WhI_~c zz3l9KQLqDpr`sHewI#ifw?twN=w`<274sS|ew+s7oPEr}0GKSmsr4^ND*Y(3296`od|!5bnS z@;mA`kRXk!`HkD. + +/** + * Grading panel for simple direct grading. + * + * @module core_grades/grades/grader/gradingpanel/point + * @package core_grades + * @copyright 2019 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +import {saveGrade, fetchGrade} from './repository'; +// Note: We use jQuery.serializer here until we can rewrite Ajax to use XHR.send() +import jQuery from 'jquery'; + +export const fetchCurrentGrade = (...args) => fetchGrade('point')(...args); + +export const storeCurrentGrade = (component, context, itemname, userId, rootNode) => { + const form = rootNode.querySelector('form'); + return saveGrade('point')(component, context, itemname, userId, jQuery(form).serialize()); +}; diff --git a/grade/amd/src/grades/grader/gradingpanel/repository.js b/grade/amd/src/grades/grader/gradingpanel/repository.js new file mode 100644 index 00000000000..65ade57b70b --- /dev/null +++ b/grade/amd/src/grades/grader/gradingpanel/repository.js @@ -0,0 +1,49 @@ +// This file is part of Moodle - http://moodle.org/ +// +// Moodle is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Moodle is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see . + +/** + * Repository for simple direct grading panel. + * + * @module core_grades/grades/grader/gradingpanel/repository + * @package core_grades + * @copyright 2019 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +import {call as fetchMany} from 'core/ajax'; + +export const fetchGrade = type => (component, contextid, itemname, gradeduserid) => { + return fetchMany([{ + methodname: `core_grades_grader_gradingpanel_${type}_fetch`, + args: { + component, + contextid, + itemname, + gradeduserid, + }, + }])[0]; +}; + +export const saveGrade = type => (component, contextid, itemname, gradeduserid, formdata) => { + return fetchMany([{ + methodname: `core_grades_grader_gradingpanel_${type}_store`, + args: { + component, + contextid, + itemname, + gradeduserid, + formdata, + }, + }])[0]; +}; diff --git a/grade/classes/grades/grader/gradingpanel/point/external/fetch.php b/grade/classes/grades/grader/gradingpanel/point/external/fetch.php new file mode 100644 index 00000000000..63260083d2e --- /dev/null +++ b/grade/classes/grades/grader/gradingpanel/point/external/fetch.php @@ -0,0 +1,167 @@ +. + +/** + * Web service functions relating to point grades and grading. + * + * @package core_grades + * @copyright 2019 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +declare(strict_types = 1); + +namespace core_grades\grades\grader\gradingpanel\point\external; + +use coding_exception; +use context; +use core_user; +use core_grades\component_gradeitem as gradeitem; +use core_grades\component_gradeitems; +use external_api; +use external_function_parameters; +use external_multiple_structure; +use external_single_structure; +use external_value; +use external_warnings; +use moodle_exception; +use required_capability_exception; +use stdClass; + +/** + * External grading panel point API + * + * @package core_grades + * @copyright 2019 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class fetch extends external_api { + + /** + * Describes the parameters for fetching the grading panel for a simple grade. + * + * @return external_function_parameters + * @since Moodle 3.8 + */ + public static function execute_parameters(): external_function_parameters { + return new external_function_parameters ([ + 'component' => new external_value( + PARAM_ALPHANUMEXT, + 'The name of the component', + VALUE_REQUIRED + ), + 'contextid' => new external_value( + PARAM_INT, + 'The ID of the context being graded', + VALUE_REQUIRED + ), + 'itemname' => new external_value( + PARAM_ALPHANUM, + 'The grade item itemname being graded', + VALUE_REQUIRED + ), + 'gradeduserid' => new external_value( + PARAM_INT, + 'The ID of the user show', + VALUE_REQUIRED + ), + ]); + } + + /** + * Fetch the data required to build a grading panel for a simple grade. + * + * @param string $component + * @param int $contextid + * @param string $itemname + * @param int $gradeduserid + * @return array + * @since Moodle 3.8 + */ + public static function execute(string $component, int $contextid, string $itemname, int $gradeduserid): array { + global $USER; + + [ + 'component' => $component, + 'contextid' => $contextid, + 'itemname' => $itemname, + 'gradeduserid' => $gradeduserid, + ] = self::validate_parameters(self::execute_parameters(), [ + 'component' => $component, + 'contextid' => $contextid, + 'itemname' => $itemname, + 'gradeduserid' => $gradeduserid, + ]); + + // Validate the context. + $context = context::instance_by_id($contextid); + self::validate_context($context); + + // Validate that the supplied itemname is a gradable item. + if (!component_gradeitems::is_valid_itemname($component, $itemname)) { + throw new coding_exception("The '{$itemname}' item is not valid for the '{$component}' component"); + } + + // Fetch the gradeitem instance. + $gradeitem = gradeitem::instance($component, $context, $itemname); + + if (!$gradeitem->is_using_direct_grading()) { + throw new moodle_exception("The {$itemname} item in {$component}/{$contextid} is not configured for direct grading"); + } + + // Fetch the actual data. + $gradeduser = \core_user::get_user($gradeduserid); + $grade = $gradeitem->get_grade_for_user($gradeduser, $USER); + + return self::get_fetch_data($grade); + } + + /** + * Get the data to be fetched. + * + * @param component_gradeitem $gradeitem + * @return array + */ + public static function get_fetch_data(stdClass $grade): array { + return [ + 'templatename' => 'core_grades/grades/grader/gradingpanel/point', + 'grade' => [ + 'grade' => $grade->grade, + 'timecreated' => $grade->timecreated, + 'timemodified' => $grade->timemodified, + ], + 'warnings' => [], + ]; + } + + /** + * Describes the data returned from the external function. + * + * @return external_single_structure + * @since Moodle 3.8 + */ + public static function execute_returns(): external_single_structure { + return new external_single_structure([ + 'templatename' => new external_value(PARAM_SAFEPATH, 'The template to use when rendering this data'), + 'grade' => new external_single_structure([ + 'grade' => new external_value(PARAM_FLOAT, 'The numeric grade'), + 'timecreated' => new external_value(PARAM_INT, 'The time that the grade was created'), + 'timemodified' => new external_value(PARAM_INT, 'The time that the grade was last updated'), + ]), + 'warnings' => new external_warnings(), + ]); + } +} diff --git a/grade/classes/grades/grader/gradingpanel/point/external/store.php b/grade/classes/grades/grader/gradingpanel/point/external/store.php new file mode 100644 index 00000000000..4f54a33ffff --- /dev/null +++ b/grade/classes/grades/grader/gradingpanel/point/external/store.php @@ -0,0 +1,164 @@ +. + +/** + * Web service functions relating to point grades and grading. + * + * @package core_grades + * @copyright 2019 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +declare(strict_types = 1); + +namespace core_grades\grades\grader\gradingpanel\point\external; + +use coding_exception; +use context; +use core_user; +use core_grades\component_gradeitem as gradeitem; +use core_grades\component_gradeitems; +use external_api; +use external_function_parameters; +use external_multiple_structure; +use external_single_structure; +use external_value; +use external_warnings; +use moodle_exception; +use required_capability_exception; + +/** + * External grading panel point API + * + * @package core_grades + * @copyright 2019 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class store extends external_api { + + /** + * Describes the parameters for fetching the grading panel for a simple grade. + * + * @return external_function_parameters + * @since Moodle 3.8 + */ + public static function execute_parameters(): external_function_parameters { + return new external_function_parameters ([ + 'component' => new external_value( + PARAM_ALPHANUMEXT, + 'The name of the component', + VALUE_REQUIRED + ), + 'contextid' => new external_value( + PARAM_INT, + 'The ID of the context being graded', + VALUE_REQUIRED + ), + 'itemname' => new external_value( + PARAM_ALPHANUM, + 'The grade item itemname being graded', + VALUE_REQUIRED + ), + 'gradeduserid' => new external_value( + PARAM_INT, + 'The ID of the user show', + VALUE_REQUIRED + ), + 'formdata' => new external_value( + PARAM_RAW, + 'The serialised form data representing the grade', + VALUE_REQUIRED + ), + ]); + } + + /** + * Fetch the data required to build a grading panel for a simple grade. + * + * @param string $component + * @param int $contextid + * @param string $itemname + * @param int $gradeduserid + * @return array + * @since Moodle 3.8 + */ + public static function execute(string $component, int $contextid, string $itemname, int $gradeduserid, string $formdata): array { + global $USER; + + [ + 'component' => $component, + 'contextid' => $contextid, + 'itemname' => $itemname, + 'gradeduserid' => $gradeduserid, + 'formdata' => $formdata, + ] = self::validate_parameters(self::execute_parameters(), [ + 'component' => $component, + 'contextid' => $contextid, + 'itemname' => $itemname, + 'gradeduserid' => $gradeduserid, + 'formdata' => $formdata, + ]); + + // Validate the context. + $context = context::instance_by_id($contextid); + self::validate_context($context); + + // Validate that the supplied itemname is a gradable item. + if (!component_gradeitems::is_valid_itemname($component, $itemname)) { + throw new coding_exception("The '{$itemname}' item is not valid for the '{$component}' component"); + } + + // Fetch the gradeitem instance. + $gradeitem = gradeitem::instance($component, $context, $itemname); + + // Validate that this gradeitem is actually enabled. + if (!$gradeitem->is_grading_enabled()) { + throw new moodle_exception("Grading is not enabled for {$itemname} in this context"); + } + + // Fetch the record for the graded user. + $gradeduser = \core_user::get_user($gradeduserid); + + // Require that this user can save grades. + $gradeitem->require_user_can_grade($gradeduser, $USER); + + if (!$gradeitem->is_using_direct_grading()) { + throw new moodle_exception("The {$itemname} item in {$component}/{$contextid} is not configured for direct grading"); + } + + // Parse the serialised string into an object. + $data = []; + parse_str($formdata, $data); + + // Grade. + $gradeitem->store_grade_from_formdata($gradeduser, $USER, (object) $data); + + // Fetch the updated grade back out. + $grade = $gradeitem->get_grade_for_user($gradeduser, $USER); + + return fetch::get_fetch_data($grade); + } + + /** + * Describes the data returned from the external function. + * + * @return external_single_structure + * @since Moodle 3.8 + */ + public static function execute_returns(): external_single_structure { + return fetch::execute_returns(); + } +} diff --git a/grade/templates/grades/grader/gradingpanel/point.mustache b/grade/templates/grades/grader/gradingpanel/point.mustache new file mode 100644 index 00000000000..ebf77c30a97 --- /dev/null +++ b/grade/templates/grades/grader/gradingpanel/point.mustache @@ -0,0 +1,35 @@ +{{! + This file is part of Moodle - http://moodle.org/ + + Moodle is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Moodle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Moodle. If not, see . +}} +{{! + @template core_grades/grades/grader/gradingpanel/point + + Point-based grading template for use in the grading panel. + + Context variables required for this template: + + Example context (json): + { + "grade": 47 + } +}} +
+
+ + + {{#str}}grade_help, core_grades{{/str}} +
+
diff --git a/grade/tests/grades_grader_gradingpanel_point_external_fetch_test.php b/grade/tests/grades_grader_gradingpanel_point_external_fetch_test.php new file mode 100644 index 00000000000..66db17e7d0a --- /dev/null +++ b/grade/tests/grades_grader_gradingpanel_point_external_fetch_test.php @@ -0,0 +1,200 @@ +. + +/** + * Unit tests for core_grades\component_gradeitems; + * + * @package core_grades + * @category test + * @copyright 2019 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU Public License + */ + +declare(strict_types = 1); + +namespace core_grades\grades\grader\gradingpanel\point\external; + +use advanced_testcase; +use coding_exception; +use core_grades\component_gradeitem; +use external_api; +use mod_forum\local\entities\forum as forum_entity; +use moodle_exception; + +/** + * Unit tests for core_grades\component_gradeitems; + * + * @package core_grades + * @category test + * @copyright 2019 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class fetch_test extends advanced_testcase { + + public static function setupBeforeClass(): void { + global $CFG; + require_once("{$CFG->libdir}/externallib.php"); + } + + /** + * Ensure that an execute with an invalid component is rejected. + */ + public function test_execute_invalid_component(): void { + $this->resetAfterTest(); + $user = $this->getDataGenerator()->create_user(); + $this->setUser($user); + + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'foo' item is not valid for the 'mod_invalid' component"); + fetch::execute('mod_invalid', 1, 'foo', 2); + } + + /** + * Ensure that an execute with an invalid itemname on a valid component is rejected. + */ + public function test_execute_invalid_itemname(): void { + $this->resetAfterTest(); + $user = $this->getDataGenerator()->create_user(); + $this->setUser($user); + + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'foo' item is not valid for the 'mod_forum' component"); + fetch::execute('mod_forum', 1, 'foo', 2); + } + + /** + * Ensure that an execute against a different grading method is rejected. + */ + public function test_execute_incorrect_type(): void { + $this->resetAfterTest(); + + $forum = $this->get_forum_instance([ + // Negative numbers mean a scale. + 'grade_forum' => -1, + ]); + $course = $forum->get_course_record(); + $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + $this->setUser($teacher); + + $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum'); + + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage("not configured for direct grading"); + fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id); + } + + /** + * Ensure that an execute against the correct grading method returns the current state of the user. + */ + public function test_execute_fetch_empty(): void { + $this->resetAfterTest(); + + $forum = $this->get_forum_instance([ + // Negative numbers mean a scale. + 'grade_forum' => 5, + ]); + $course = $forum->get_course_record(); + $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + $this->setUser($teacher); + + $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum'); + + $result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id); + $result = external_api::clean_returnvalue(fetch::execute_returns(), $result); + + $this->assertIsArray($result); + $this->assertArrayHasKey('templatename', $result); + + $this->assertEquals('core_grades/grades/grader/gradingpanel/point', $result['templatename']); + + $this->assertArrayHasKey('grade', $result); + $this->assertIsArray($result['grade']); + $this->assertArrayHasKey('grade', $result['grade']); + $this->assertEmpty($result['grade']['grade']); + $this->assertArrayHasKey('timecreated', $result['grade']); + $this->assertIsInt($result['grade']['timecreated']); + $this->assertArrayHasKey('timemodified', $result['grade']); + $this->assertIsInt($result['grade']['timemodified']); + + $this->assertArrayHasKey('warnings', $result); + $this->assertIsArray($result['warnings']); + $this->assertEmpty($result['warnings']); + } + + /** + * Ensure that an execute against the correct grading method returns the current state of the user. + */ + public function test_execute_fetch_graded(): void { + $this->resetAfterTest(); + + $forum = $this->get_forum_instance([ + // Negative numbers mean a scale. + 'grade_forum' => 5, + ]); + $course = $forum->get_course_record(); + $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + $this->setUser($teacher); + + $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum'); + $gradeitem->store_grade_from_formdata($student, $teacher, (object) [ + 'grade' => 4, + ]); + + $result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id); + $result = external_api::clean_returnvalue(fetch::execute_returns(), $result); + + $this->assertIsArray($result); + $this->assertArrayHasKey('templatename', $result); + + $this->assertEquals('core_grades/grades/grader/gradingpanel/point', $result['templatename']); + + $this->assertArrayHasKey('grade', $result); + $this->assertIsArray($result['grade']); + $this->assertArrayHasKey('grade', $result['grade']); + $this->assertIsFloat($result['grade']['grade']); + $this->assertEquals(grade_floatval(unformat_float(4)), $result['grade']['grade']); + $this->assertArrayHasKey('timecreated', $result['grade']); + $this->assertIsInt($result['grade']['timecreated']); + $this->assertArrayHasKey('timemodified', $result['grade']); + $this->assertIsInt($result['grade']['timemodified']); + + $this->assertArrayHasKey('warnings', $result); + $this->assertIsArray($result['warnings']); + $this->assertEmpty($result['warnings']); + } + + /** + * Get a forum instance. + * + * @param array $config + * @return forum_entity + */ + protected function get_forum_instance(array $config = []): forum_entity { + $this->resetAfterTest(); + + $datagenerator = $this->getDataGenerator(); + $course = $datagenerator->create_course(); + $forum = $datagenerator->create_module('forum', array_merge($config, ['course' => $course->id])); + + $vaultfactory = \mod_forum\local\container::get_vault_factory(); + $vault = $vaultfactory->get_forum_vault(); + + return $vault->get_from_id((int) $forum->id); + } +} diff --git a/grade/tests/grades_grader_gradingpanel_point_external_store_test.php b/grade/tests/grades_grader_gradingpanel_point_external_store_test.php new file mode 100644 index 00000000000..b9c47603479 --- /dev/null +++ b/grade/tests/grades_grader_gradingpanel_point_external_store_test.php @@ -0,0 +1,315 @@ +. + +/** + * Unit tests for core_grades\component_gradeitems; + * + * @package core_grades + * @category test + * @copyright 2019 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU Public License + */ + +declare(strict_types = 1); + +namespace core_grades\grades\grader\gradingpanel\point\external; + +use advanced_testcase; +use coding_exception; +use core_grades\component_gradeitem; +use external_api; +use mod_forum\local\entities\forum as forum_entity; +use moodle_exception; +use grade_grade; +use grade_item; + +/** + * Unit tests for core_grades\component_gradeitems; + * + * @package core_grades + * @category test + * @copyright 2019 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class store_test extends advanced_testcase { + + public static function setupBeforeClass(): void { + global $CFG; + require_once("{$CFG->libdir}/externallib.php"); + } + + /** + * Ensure that an execute with an invalid component is rejected. + */ + public function test_execute_invalid_component(): void { + $this->resetAfterTest(); + $user = $this->getDataGenerator()->create_user(); + $this->setUser($user); + + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'foo' item is not valid for the 'mod_invalid' component"); + store::execute('mod_invalid', 1, 'foo', 2, 'formdata'); + } + + /** + * Ensure that an execute with an invalid itemname on a valid component is rejected. + */ + public function test_execute_invalid_itemname(): void { + $this->resetAfterTest(); + $user = $this->getDataGenerator()->create_user(); + $this->setUser($user); + + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'foo' item is not valid for the 'mod_forum' component"); + store::execute('mod_forum', 1, 'foo', 2, 'formdata'); + } + + /** + * Ensure that an execute against a different grading method is rejected. + */ + public function test_execute_incorrect_type(): void { + $this->resetAfterTest(); + + $forum = $this->get_forum_instance([ + // Negative numbers mean a scale. + 'grade_forum' => -1, + ]); + $course = $forum->get_course_record(); + $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + $this->setUser($teacher); + + $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum'); + + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage("not configured for direct grading"); + store::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id, 'formdata'); + } + + /** + * Ensure that an execute against a different grading method is rejected. + */ + public function test_execute_disabled(): void { + $this->resetAfterTest(); + + $forum = $this->get_forum_instance(); + $course = $forum->get_course_record(); + $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + $this->setUser($teacher); + + $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum'); + + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage("Grading is not enabled"); + store::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id, 'formdata'); + } + + /** + * Ensure that an execute against the correct grading method returns the current state of the user. + */ + public function test_execute_store_empty(): void { + $this->resetAfterTest(); + + $forum = $this->get_forum_instance([ + // Negative numbers mean a scale. + 'grade_forum' => 5, + ]); + $course = $forum->get_course_record(); + $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + $this->setUser($teacher); + + $formdata = [ + 'grade' => null, + ]; + + $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum'); + + $result = store::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id, http_build_query($formdata)); + $result = external_api::clean_returnvalue(store::execute_returns(), $result); + + // The result should still be empty. + $this->assertIsArray($result); + $this->assertArrayHasKey('templatename', $result); + + $this->assertEquals('core_grades/grades/grader/gradingpanel/point', $result['templatename']); + + $this->assertArrayHasKey('grade', $result); + $this->assertIsArray($result['grade']); + $this->assertArrayHasKey('grade', $result['grade']); + $this->assertEmpty($result['grade']['grade']); + $this->assertArrayHasKey('timecreated', $result['grade']); + $this->assertIsInt($result['grade']['timecreated']); + $this->assertArrayHasKey('timemodified', $result['grade']); + $this->assertIsInt($result['grade']['timemodified']); + + $this->assertArrayHasKey('warnings', $result); + $this->assertIsArray($result['warnings']); + $this->assertEmpty($result['warnings']); + + // Compare against the grade stored in the database. + $storedgradeitem = grade_item::fetch([ + 'courseid' => $forum->get_course_id(), + 'itemtype' => 'mod', + 'itemmodule' => 'forum', + 'iteminstance' => $forum->get_id(), + 'itemnumber' => $gradeitem->get_grade_itemid(), + ]); + $storedgrade = grade_grade::fetch([ + 'userid' => $student->id, + 'itemid' => $storedgradeitem->id, + ]); + + $this->assertEmpty($storedgrade->rawgrade); + } + + /** + * Ensure that an execute against the correct grading method returns the current state of the user. + */ + public function test_execute_store_graded(): void { + $this->resetAfterTest(); + + $forum = $this->get_forum_instance([ + // Negative numbers mean a scale. + 'grade_forum' => 5, + ]); + $course = $forum->get_course_record(); + $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + $this->setUser($teacher); + + $formdata = [ + 'grade' => 4, + ]; + $formattedvalue = grade_floatval(unformat_float(4)); + + $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum'); + + $result = store::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id, http_build_query($formdata)); + $result = external_api::clean_returnvalue(store::execute_returns(), $result); + + // The result should still be empty. + $this->assertIsArray($result); + $this->assertArrayHasKey('templatename', $result); + + $this->assertEquals('core_grades/grades/grader/gradingpanel/point', $result['templatename']); + + $this->assertArrayHasKey('grade', $result); + $this->assertIsArray($result['grade']); + $this->assertArrayHasKey('grade', $result['grade']); + $this->assertEquals($formattedvalue, $result['grade']['grade']); + $this->assertArrayHasKey('timecreated', $result['grade']); + $this->assertIsInt($result['grade']['timecreated']); + $this->assertArrayHasKey('timemodified', $result['grade']); + $this->assertIsInt($result['grade']['timemodified']); + + $this->assertArrayHasKey('warnings', $result); + $this->assertIsArray($result['warnings']); + $this->assertEmpty($result['warnings']); + + // Compare against the grade stored in the database. + $storedgradeitem = grade_item::fetch([ + 'courseid' => $forum->get_course_id(), + 'itemtype' => 'mod', + 'itemmodule' => 'forum', + 'iteminstance' => $forum->get_id(), + 'itemnumber' => $gradeitem->get_grade_itemid(), + ]); + $storedgrade = grade_grade::fetch([ + 'userid' => $student->id, + 'itemid' => $storedgradeitem->id, + ]); + + $this->assertEquals($formattedvalue, $storedgrade->rawgrade); + } + + /** + * Ensure that an out-of-range value is rejected. + * + * @dataProvider execute_out_of_range_provider + * @param int $maxvalue The max value of the forum + * @param int $suppliedvalue The value that was submitted + */ + public function test_execute_store_out_of__range(int $maxvalue, int $suppliedvalue): void { + $this->resetAfterTest(); + + $forum = $this->get_forum_instance([ + // Negative numbers mean a scale. + 'grade_forum' => $maxvalue, + ]); + $course = $forum->get_course_record(); + $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + $this->setUser($teacher); + + $formdata = [ + 'grade' => $suppliedvalue, + ]; + + $gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum'); + + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage("Invalid grade '{$suppliedvalue}' provided. Grades must be between 0 and {$maxvalue}."); + store::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id, http_build_query($formdata)); + } + + /** + * Data provider for out of range tests. + * + * @return array + */ + public function execute_out_of_range_provider(): array { + return [ + 'above' => [ + 'max' => 100, + 'supplied' => 101, + ], + 'above just' => [ + 'max' => 100, + 'supplied' => 101.001, + ], + 'below' => [ + 'max' => 100, + 'supplied' => -100, + ], + '-1' => [ + 'max' => 100, + 'supplied' => -1, + ], + ]; + } + + + /** + * Get a forum instance. + * + * @param array $config + * @return forum_entity + */ + protected function get_forum_instance(array $config = []): forum_entity { + $this->resetAfterTest(); + + $datagenerator = $this->getDataGenerator(); + $course = $datagenerator->create_course(); + $forum = $datagenerator->create_module('forum', array_merge($config, ['course' => $course->id])); + + $vaultfactory = \mod_forum\local\container::get_vault_factory(); + $vault = $vaultfactory->get_forum_vault(); + + return $vault->get_from_id((int) $forum->id); + } +} diff --git a/lang/en/grades.php b/lang/en/grades.php index ff8fc09613e..8afc3ed4bc7 100644 --- a/lang/en/grades.php +++ b/lang/en/grades.php @@ -245,6 +245,7 @@ $string['full'] = 'Full'; $string['fullmode'] = 'Change to full view'; $string['generalsettings'] = 'General settings'; $string['grade'] = 'Grade'; +$string['grade_help'] = 'The grade to award this person for their work.'; $string['gradeadministration'] = 'Grade administration'; $string['gradealreadyupdated'] = '{$a} grades have not been imported because the grades in the import file are older than in the grader report. To proceed with the grade import anyway, use the force import option.'; $string['gradeanalysis'] = 'Grade analysis'; diff --git a/lib/db/services.php b/lib/db/services.php index 147079d167c..a0a34c6354c 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -810,6 +810,23 @@ $functions = array( 'description' => 'Update a grade item and associated student grades.', 'type' => 'write', ), + 'core_grades_grader_gradingpanel_point_fetch' => [ + 'classname' => 'core_grades\\grades\\grader\\gradingpanel\\point\\external\\fetch', + 'methodname' => 'execute', + 'description' => 'Fetch the data required to display the grader grading panel for simple grading, ' . + 'creating the grade item if required', + 'type' => 'write', + 'ajax' => true, + 'services' => [MOODLE_OFFICIAL_MOBILE_SERVICE], + ], + 'core_grades_grader_gradingpanel_point_store' => [ + 'classname' => 'core_grades\\grades\\grader\\gradingpanel\\point\\external\\store', + 'methodname' => 'execute', + 'description' => 'Store the data required to display the grader grading panel for simple grading', + 'type' => 'write', + 'ajax' => true, + 'services' => [MOODLE_OFFICIAL_MOBILE_SERVICE], + ], 'core_grading_get_definitions' => array( 'classname' => 'core_grading_external', 'methodname' => 'get_definitions', -- 2.43.0