Skip to content

Commit

Permalink
MDL-67165 core_grade: Add the grader name to webservices
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocolate-lightning committed Dec 17, 2019
1 parent 6aacd8d commit 97ba609
Show file tree
Hide file tree
Showing 16 changed files with 423 additions and 103 deletions.
15 changes: 11 additions & 4 deletions grade/classes/grades/grader/gradingpanel/point/external/fetch.php
Expand Up @@ -97,8 +97,8 @@ public static function execute_parameters(): external_function_parameters {
* @since Moodle 3.8
*/
public static function execute(string $component, int $contextid, string $itemname, int $gradeduserid): array {
global $USER;

global $USER, $CFG;
require_once("{$CFG->libdir}/gradelib.php");
[
'component' => $component,
'contextid' => $contextid,
Expand Down Expand Up @@ -131,9 +131,13 @@ public static function execute(string $component, int $contextid, string $itemna
$gradeduser = \core_user::get_user($gradeduserid);
$hasgrade = $gradeitem->user_has_grade($gradeduser);
$grade = $gradeitem->get_grade_for_user($gradeduser, $USER);

// Set up some items we need to return on other interfaces.
$gradegrade = \grade_grade::fetch(['itemid' => $gradeitem->get_grade_item()->id, 'userid' => $gradeduser->id]);
$gradername = $gradegrade ? fullname(\core_user::get_user($gradegrade->usermodified)) : null;
$maxgrade = (int) $gradeitem->get_grade_item()->grademax;

return self::get_fetch_data($grade, $hasgrade, $maxgrade);
return self::get_fetch_data($grade, $hasgrade, $maxgrade, $gradername);
}

/**
Expand All @@ -142,16 +146,18 @@ public static function execute(string $component, int $contextid, string $itemna
* @param stdClass $grade
* @param bool $hasgrade
* @param int $maxgrade
* @param string|null $gradername
* @return array
*/
public static function get_fetch_data(stdClass $grade, bool $hasgrade, int $maxgrade): array {
public static function get_fetch_data(stdClass $grade, bool $hasgrade, int $maxgrade, ?string $gradername): array {
return [
'templatename' => 'core_grades/grades/grader/gradingpanel/point',
'hasgrade' => $hasgrade,
'grade' => [
'grade' => $grade->grade,
'usergrade' => $grade->grade,
'maxgrade' => $maxgrade,
'gradedby' => $gradername,
'timecreated' => $grade->timecreated,
'timemodified' => $grade->timemodified,
],
Expand All @@ -173,6 +179,7 @@ public static function execute_returns(): external_single_structure {
'grade' => new external_value(PARAM_FLOAT, 'The numeric grade'),
'usergrade' => new external_value(PARAM_RAW, 'Current user grade'),
'maxgrade' => new external_value(PARAM_RAW, 'Max possible grade'),
'gradedby' => new external_value(PARAM_RAW, 'The assumed grader of this grading instance'),
'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'),
]),
Expand Down
Expand Up @@ -110,8 +110,8 @@ public static function execute_parameters(): external_function_parameters {
*/
public static function execute(string $component, int $contextid, string $itemname, int $gradeduserid,
bool $notifyuser, string $formdata): array {
global $USER;

global $USER, $CFG;
require_once("{$CFG->libdir}/gradelib.php");
[
'component' => $component,
'contextid' => $contextid,
Expand Down Expand Up @@ -172,7 +172,11 @@ public static function execute(string $component, int $contextid, string $itemna
// Fetch the updated grade back out.
$grade = $gradeitem->get_grade_for_user($gradeduser, $USER);

return fetch::get_fetch_data($grade, $hasgrade, 0);
$gradegrade = \grade_grade::fetch(['itemid' => $gradeitem->get_grade_item()->id, 'userid' => $gradeduser->id]);
$gradername = $gradegrade ? fullname(\core_user::get_user($gradegrade->usermodified)) : null;
$maxgrade = (int) $gradeitem->get_grade_item()->grademax;

return fetch::get_fetch_data($grade, $hasgrade, $maxgrade, $gradername);
}

/**
Expand Down
14 changes: 10 additions & 4 deletions grade/classes/grades/grader/gradingpanel/scale/external/fetch.php
Expand Up @@ -97,8 +97,8 @@ public static function execute_parameters(): external_function_parameters {
* @since Moodle 3.8
*/
public static function execute(string $component, int $contextid, string $itemname, int $gradeduserid): array {
global $USER;

global $USER, $CFG;
require_once("{$CFG->libdir}/gradelib.php");
[
'component' => $component,
'contextid' => $contextid,
Expand Down Expand Up @@ -129,9 +129,12 @@ public static function execute(string $component, int $contextid, string $itemna

$gradeduser = \core_user::get_user($gradeduserid);

// Set up some items we need to return on other interfaces.
$gradegrade = \grade_grade::fetch(['itemid' => $gradeitem->get_grade_item()->id, 'userid' => $gradeduser->id]);
$gradername = $gradegrade ? fullname(\core_user::get_user($gradegrade->usermodified)) : null;
$maxgrade = (int) $gradeitem->get_grade_item()->grademax;

return self::get_fetch_data($gradeitem, $gradeduser, $maxgrade);
return self::get_fetch_data($gradeitem, $gradeduser, $maxgrade, $gradername);
}

/**
Expand All @@ -140,9 +143,10 @@ public static function execute(string $component, int $contextid, string $itemna
* @param gradeitem $gradeitem
* @param stdClass $gradeduser
* @param int $maxgrade
* @param string|null $gradername
* @return array
*/
public static function get_fetch_data(gradeitem $gradeitem, stdClass $gradeduser, int $maxgrade): array {
public static function get_fetch_data(gradeitem $gradeitem, stdClass $gradeduser, int $maxgrade, ?string $gradername): array {
global $USER;

$hasgrade = $gradeitem->user_has_grade($gradeduser);
Expand All @@ -165,6 +169,7 @@ public static function get_fetch_data(gradeitem $gradeitem, stdClass $gradeduser
'options' => $values,
'usergrade' => $grade->grade,
'maxgrade' => $maxgrade,
'gradedby' => $gradername,
'timecreated' => $grade->timecreated,
'timemodified' => $grade->timemodified,
],
Expand Down Expand Up @@ -193,6 +198,7 @@ public static function execute_returns(): external_single_structure {
),
'usergrade' => new external_value(PARAM_RAW, 'Current user grade'),
'maxgrade' => new external_value(PARAM_RAW, 'Max possible grade'),
'gradedby' => new external_value(PARAM_RAW, 'The assumed grader of this grading instance'),
'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'),
]),
Expand Down
Expand Up @@ -107,8 +107,8 @@ public static function execute_parameters(): external_function_parameters {
*/
public static function execute(string $component, int $contextid, string $itemname, int $gradeduserid,
bool $notifyuser, string $formdata): array {
global $USER;

global $USER, $CFG;
require_once("{$CFG->libdir}/gradelib.php");
[
'component' => $component,
'contextid' => $contextid,
Expand Down Expand Up @@ -165,7 +165,11 @@ public static function execute(string $component, int $contextid, string $itemna
$gradeitem->send_student_notification($gradeduser, $USER);
}

return fetch::get_fetch_data($gradeitem, $gradeduser, 0);
$gradegrade = \grade_grade::fetch(['itemid' => $gradeitem->get_grade_item()->id, 'userid' => $gradeduser->id]);
$gradername = $gradegrade ? fullname(\core_user::get_user($gradegrade->usermodified)) : null;
$maxgrade = (int) $gradeitem->get_grade_item()->grademax;

return fetch::get_fetch_data($gradeitem, $gradeduser, $maxgrade, $gradername);
}

/**
Expand Down
Expand Up @@ -100,8 +100,8 @@ public static function execute_parameters(): external_function_parameters {
* @since Moodle 3.8
*/
public static function execute(string $component, int $contextid, string $itemname, int $gradeduserid): array {
global $USER;

global $CFG;
require_once("{$CFG->libdir}/gradelib.php");
[
'component' => $component,
'contextid' => $contextid,
Expand Down Expand Up @@ -156,6 +156,10 @@ public static function get_fetch_data(gradeitem $gradeitem, stdClass $gradeduser
$fillings = $instance->get_guide_filling();
$context = $controller->get_context();
$definitionid = (int) $definition->id;

// Set up some items we need to return on other interfaces.
$gradegrade = \grade_grade::fetch(['itemid' => $gradeitem->get_grade_item()->id, 'userid' => $gradeduser->id]);
$gradername = $gradegrade ? fullname(\core_user::get_user($gradegrade->usermodified)) : null;
$maxgrade = max(array_keys($controller->get_grade_range()));

$criterion = [];
Expand Down Expand Up @@ -227,6 +231,7 @@ public static function get_fetch_data(gradeitem $gradeitem, stdClass $gradeduser
'comments' => $comments,
'usergrade' => $grade->grade,
'maxgrade' => $maxgrade,
'gradedby' => $gradername,
'timecreated' => $grade->timecreated,
'timemodified' => $grade->timemodified,
],
Expand Down Expand Up @@ -269,6 +274,7 @@ public static function execute_returns(): external_single_structure {
),
'usergrade' => new external_value(PARAM_RAW, 'Current user grade'),
'maxgrade' => new external_value(PARAM_RAW, 'Max possible grade'),
'gradedby' => new external_value(PARAM_RAW, 'The assumed grader of this grading instance'),
'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'),
]),
Expand Down
Expand Up @@ -123,16 +123,27 @@ public function test_execute_fetch_empty(): void {

$this->assertEquals('gradingform_guide/grades/grader/gradingpanel', $result['templatename']);

$this->assertArrayHasKey('warnings', $result);
$this->assertIsArray($result['warnings']);
$this->assertEmpty($result['warnings']);

// Test the grade array items.
$this->assertArrayHasKey('grade', $result);
$this->assertIsArray($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']);
$this->assertArrayHasKey('usergrade', $result['grade']);
$this->assertEquals(0, $result['grade']['usergrade']);

$this->assertArrayHasKey('maxgrade', $result['grade']);
$this->assertIsInt($result['grade']['maxgrade']);
$this->assertEquals(100, $result['grade']['maxgrade']);

$this->assertArrayHasKey('gradedby', $result['grade']);
$this->assertEquals(null, $result['grade']['gradedby']);

$this->assertArrayHasKey('criterion', $result['grade']);
$criteria = $result['grade']['criterion'];
Expand Down Expand Up @@ -193,7 +204,7 @@ public function test_execute_fetch_graded(): void {
'instanceid' => $instance->get_id(),
'advancedgrading' => $submissiondata,
]);

// Set up some items we need to return on other interfaces.
$result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id);
$result = external_api::clean_returnvalue(fetch::execute_returns(), $result);

Expand All @@ -202,16 +213,27 @@ public function test_execute_fetch_graded(): void {

$this->assertEquals('gradingform_guide/grades/grader/gradingpanel', $result['templatename']);

$this->assertArrayHasKey('warnings', $result);
$this->assertIsArray($result['warnings']);
$this->assertEmpty($result['warnings']);

// Test the grade array items.
$this->assertArrayHasKey('grade', $result);
$this->assertIsArray($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']);
$this->assertArrayHasKey('usergrade', $result['grade']);
$this->assertEquals(25, $result['grade']['usergrade']);

$this->assertArrayHasKey('maxgrade', $result['grade']);
$this->assertIsInt($result['grade']['maxgrade']);
$this->assertEquals(100, $result['grade']['maxgrade']);

$this->assertArrayHasKey('gradedby', $result['grade']);
$this->assertEquals(fullname($teacher), $result['grade']['gradedby']);

$this->assertArrayHasKey('criterion', $result['grade']);
$criteria = $result['grade']['criterion'];
Expand Down Expand Up @@ -254,7 +276,7 @@ protected function get_forum_instance(array $config = []): forum_entity {

$datagenerator = $this->getDataGenerator();
$course = $datagenerator->create_course();
$forum = $datagenerator->create_module('forum', array_merge($config, ['course' => $course->id]));
$forum = $datagenerator->create_module('forum', array_merge($config, ['course' => $course->id, 'grade_forum' => 100]));

$vaultfactory = \mod_forum\local\container::get_vault_factory();
$vault = $vaultfactory->get_forum_vault();
Expand Down Expand Up @@ -284,7 +306,20 @@ protected function get_test_data(): array {
$controller = $guidegenerator->get_test_guide($forum->get_context(), 'forum', 'forum');
$definition = $controller->get_definition();

$DB->set_field('forum', 'grade_forum', count($definition->guide_criteria), ['id' => $forum->get_id()]);
// In the situation of mod_forum this would be the id from forum_grades.
$itemid = 1;
$instance = $controller->create_instance($student->id, $itemid);

$data = $this->get_test_form_data(
$controller,
$itemid,
5, 'This user made several mistakes.',
10, 'This user has two pictures.'
);

// Update this instance with data.
$instance->update($data);

return [
'forum' => $forum,
'controller' => $controller,
Expand All @@ -293,4 +328,36 @@ protected function get_test_data(): array {
'teacher' => $teacher,
];
}

/**
* Fetch a set of sample data.
*
* @param \gradingform_guide_controller $controller
* @param int $itemid
* @param float $spellingscore
* @param string $spellingremark
* @param float $picturescore
* @param string $pictureremark
* @return array
*/
protected function get_test_form_data(
\gradingform_guide_controller $controller,
int $itemid,
float $spellingscore,
string $spellingremark,
float $picturescore,
string $pictureremark
): array {
$generator = \testing_util::get_data_generator();
$guidegenerator = $generator->get_plugin_generator('gradingform_guide');

return $guidegenerator->get_test_form_data(
$controller,
$itemid,
$spellingscore,
$spellingremark,
$picturescore,
$pictureremark
);
}
}
Expand Up @@ -155,16 +155,27 @@ public function test_execute_store_graded(): void {

$this->assertEquals('gradingform_guide/grades/grader/gradingpanel', $result['templatename']);

$this->assertArrayHasKey('warnings', $result);
$this->assertIsArray($result['warnings']);
$this->assertEmpty($result['warnings']);

// Test the grade array items.
$this->assertArrayHasKey('grade', $result);
$this->assertIsArray($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']);
$this->assertArrayHasKey('usergrade', $result['grade']);
$this->assertEquals(0.5, $result['grade']['usergrade']);

$this->assertArrayHasKey('maxgrade', $result['grade']);
$this->assertIsInt($result['grade']['maxgrade']);
$this->assertEquals(2, $result['grade']['maxgrade']);

$this->assertArrayHasKey('gradedby', $result['grade']);
$this->assertEquals(fullname($teacher), $result['grade']['gradedby']);

$this->assertArrayHasKey('criterion', $result['grade']);
$criteria = $result['grade']['criterion'];
Expand Down

0 comments on commit 97ba609

Please sign in to comment.