Skip to content

Commit

Permalink
MDL-69797 core_grades: Implement authorization into fetch method
Browse files Browse the repository at this point in the history
  • Loading branch information
juancs authored and Jenkins committed Jan 8, 2021
1 parent fdf9d2c commit bbf25a7
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
Expand Up @@ -127,7 +127,12 @@ public static function execute(string $component, int $contextid, string $itemna
throw new moodle_exception("The {$itemname} item in {$component}/{$contextid} is not configured for grading with scales");
}

$gradeduser = \core_user::get_user($gradeduserid);
$gradeduser = \core_user::get_user($gradeduserid, '*', MUST_EXIST);

// One can access its own grades. Others just if they're graders.
if ($gradeduserid != $USER->id) {
$gradeitem->require_user_can_grade($gradeduser, $USER);
}

$maxgrade = (int) $gradeitem->get_grade_item()->grademax;

Expand Down
Expand Up @@ -178,22 +178,83 @@ public function test_execute_fetch_graded(): void {
$course = $forum->get_course_record();
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
$this->setUser($teacher);

$this->execute_and_assert_fetch($forum, $options, $scale, $teacher, $teacher, $student);
}

/**
* Class mates should not get other's grades.
*/
public function test_execute_fetch_does_not_return_data_to_other_students(): void {
$this->resetAfterTest();

$options = [
'A',
'B',
'C'
];
$scale = $this->getDataGenerator()->create_scale(['scale' => implode(',', $options)]);

$forum = $this->get_forum_instance([
// Negative numbers mean a scale.
'grade_forum' => -1 * $scale->id
]);
$course = $forum->get_course_record();
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
$evilstudent = $this->getDataGenerator()->create_and_enrol($course, 'student');

$this->expectException(\required_capability_exception::class);
$this->execute_and_assert_fetch($forum, $options, $scale, $evilstudent, $teacher, $student);
}

/**
* Grades can be returned to graded user.
*/
public function test_execute_fetch_return_data_to_graded_user(): void {
$this->resetAfterTest();

$options = [
'A',
'B',
'C'
];
$scale = $this->getDataGenerator()->create_scale(['scale' => implode(',', $options)]);

$forum = $this->get_forum_instance([
// Negative numbers mean a scale.
'grade_forum' => -1 * $scale->id
]);
$course = $forum->get_course_record();
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');

$this->execute_and_assert_fetch($forum, $options, $scale, $student, $teacher, $student);
}

/**
* Executes the fetch method with the given users and returns the result.
*/
private function execute_and_assert_fetch ($forum, $options, $scale, $fetcheruser, $grader, $gradeduser) {

$this->setUser($grader);

$gradeitem = component_gradeitem::instance('mod_forum', $forum->get_context(), 'forum');
$gradeitem->store_grade_from_formdata($student, $teacher, (object) [
$gradeitem->store_grade_from_formdata($gradeduser, $grader, (object) [
'grade' => 2,
]);

$result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id);
$this->setUser($fetcheruser);

$result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $gradeduser->id);
$result = external_api::clean_returnvalue(fetch::execute_returns(), $result);

$this->assertIsArray($result);
$this->assertArrayHasKey('templatename', $result);

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

$result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $student->id);
$result = fetch::execute('mod_forum', (int) $forum->get_context()->id, 'forum', (int) $gradeduser->id);
$result = external_api::clean_returnvalue(fetch::execute_returns(), $result);

$this->assertIsArray($result);
Expand Down

0 comments on commit bbf25a7

Please sign in to comment.