Skip to content

Commit

Permalink
Merge branch 'MDL-71439_master' of https://github.com/mdjnelson/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Nov 5, 2021
2 parents 4ed7cf0 + 7bf0b6a commit 69ec437
Show file tree
Hide file tree
Showing 17 changed files with 375 additions and 41 deletions.
50 changes: 45 additions & 5 deletions grade/classes/component_gradeitem.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static function instance(string $component, context $context, string $ite

$classname = "{$component}\\grades\\{$itemname}_gradeitem";
if (!class_exists($classname)) {
throw new coding_exception("Unknown gradeitem {$itemname} for component {$classname}");
throw new \coding_exception("Unknown gradeitem {$itemname} for component {$classname}");
}

return $classname::load_from_context($context);
Expand Down Expand Up @@ -145,7 +145,7 @@ abstract public function user_can_grade(stdClass $gradeduser, stdClass $grader):
*
* @param stdClass $gradeduser The user being graded
* @param stdClass $grader The user who is grading
* @throws required_capability_exception
* @throws \required_capability_exception
*/
abstract public function require_user_can_grade(stdClass $gradeduser, stdClass $grader): void;

Expand Down Expand Up @@ -377,9 +377,7 @@ abstract public function create_empty_grade(stdClass $gradeduser, stdClass $grad
public function get_grade(int $gradeid): stdClass {
global $DB;

$grade = $DB->get_record($this->get_table_name(), ['id' => $gradeid]);

return $grade ?: null;
return $DB->get_record($this->get_table_name(), ['id' => $gradeid]);
}

/**
Expand All @@ -391,6 +389,48 @@ public function get_grade(int $gradeid): stdClass {
*/
abstract public function get_grade_for_user(stdClass $gradeduser, stdClass $grader): ?stdClass;

/**
* Returns the grade that should be displayed to the user.
*
* The grade does not necessarily return a float value, this method takes grade settings into considering so
* the correct value be shown, eg. a float vs a letter.
*
* @param stdClass $gradeduser
* @param stdClass $grader
* @return stdClass|null
*/
public function get_formatted_grade_for_user(stdClass $gradeduser, stdClass $grader): ?stdClass {
global $DB;

if ($grade = $this->get_grade_for_user($gradeduser, $grader)) {
$gradeitem = $this->get_grade_item();
if (!$this->is_using_scale()) {
$grade->usergrade = grade_format_gradevalue($grade->grade, $gradeitem);
$grade->maxgrade = format_float($gradeitem->grademax, $gradeitem->get_decimals());
// If displaying the raw grade, also display the total value.
if ($gradeitem->get_displaytype() == GRADE_DISPLAY_TYPE_REAL) {
$grade->usergrade .= ' / ' . $grade->maxgrade;
}
} else {
$grade->usergrade = '-';
if ($scale = $DB->get_record('scale', ['id' => $gradeitem->scaleid])) {
$options = make_menu_from_list($scale->scale);

$gradeint = (int) $grade->grade;
if (isset($options[$gradeint])) {
$grade->usergrade = $options[$gradeint];
}
}

$grade->maxgrade = format_float($gradeitem->grademax, $gradeitem->get_decimals());
}

return $grade;
}

return null;
}

/**
* Get the grade status for the specified user.
* If the user has a grade as defined by the implementor return true else return false.
Expand Down
30 changes: 22 additions & 8 deletions grade/classes/grades/grader/gradingpanel/point/external/fetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,33 +136,47 @@ public static function execute(string $component, int $contextid, string $itemna
}

$hasgrade = $gradeitem->user_has_grade($gradeduser);
$grade = $gradeitem->get_grade_for_user($gradeduser, $USER);
$grade = $gradeitem->get_formatted_grade_for_user($gradeduser, $USER);
$isgrading = $gradeitem->user_can_grade($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, $gradername);
return self::get_fetch_data($grade, $hasgrade, $gradeitem, $gradername, $isgrading);
}

/**
* Get the data to be fetched.
*
* @param stdClass $grade
* @param bool $hasgrade
* @param int $maxgrade
* @param gradeitem $gradeitem
* @param string|null $gradername
* @param bool $isgrading
* @return array
*/
public static function get_fetch_data(stdClass $grade, bool $hasgrade, int $maxgrade, ?string $gradername): array {
public static function get_fetch_data(stdClass $grade,
bool $hasgrade,
gradeitem $gradeitem,
?string $gradername,
bool $isgrading = false
): array {
$templatename = 'core_grades/grades/grader/gradingpanel/point';

// We do not want to display anything if we are showing the grade as a letter. For example the 'Grade' might
// read 'B-'. We do not want to show the user the actual point they were given. See MDL-71439.
if (($gradeitem->get_grade_item()->get_displaytype() == GRADE_DISPLAY_TYPE_LETTER) && !$isgrading) {
$templatename = 'core_grades/grades/grader/gradingpanel/point_blank';
}

return [
'templatename' => 'core_grades/grades/grader/gradingpanel/point',
'templatename' => $templatename,
'hasgrade' => $hasgrade,
'grade' => [
'grade' => $grade->grade,
'usergrade' => $grade->grade,
'maxgrade' => $maxgrade,
'usergrade' => $grade->usergrade,
'maxgrade' => (int) $grade->maxgrade,
'gradedby' => $gradername,
'timecreated' => $grade->timecreated,
'timemodified' => $grade->timemodified,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,12 @@ public static function execute(string $component, int $contextid, string $itemna
}

// Fetch the updated grade back out.
$grade = $gradeitem->get_grade_for_user($gradeduser, $USER);
$grade = $gradeitem->get_formatted_grade_for_user($gradeduser, $USER);

$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);
return fetch::get_fetch_data($grade, $hasgrade, $gradeitem, $gradername);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static function get_fetch_data(gradeitem $gradeitem, stdClass $gradeduser
global $USER;

$hasgrade = $gradeitem->user_has_grade($gradeduser);
$grade = $gradeitem->get_grade_for_user($gradeduser, $USER);
$grade = $gradeitem->get_formatted_grade_for_user($gradeduser, $USER);
$currentgrade = (int) unformat_float($grade->grade);

$menu = $gradeitem->get_grade_menu();
Expand All @@ -172,7 +172,7 @@ public static function get_fetch_data(gradeitem $gradeitem, stdClass $gradeduser
'hasgrade' => $hasgrade,
'grade' => [
'options' => $values,
'usergrade' => $grade->grade,
'usergrade' => $grade->usergrade,
'maxgrade' => $maxgrade,
'gradedby' => $gradername,
'timecreated' => $grade->timecreated,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public static function get_fetch_data(gradeitem $gradeitem, stdClass $gradeduser
global $USER;

$hasgrade = $gradeitem->user_has_grade($gradeduser);
$grade = $gradeitem->get_grade_for_user($gradeduser, $USER);
$grade = $gradeitem->get_formatted_grade_for_user($gradeduser, $USER);
$instance = $gradeitem->get_advanced_grading_instance($USER, $grade);
if (!$instance) {
throw new moodle_exception('error:gradingunavailable', 'grading');
Expand Down Expand Up @@ -237,7 +237,7 @@ public static function get_fetch_data(gradeitem $gradeitem, stdClass $gradeduser
'criterion' => $criterion,
'hascomments' => !empty($comments),
'comments' => $comments,
'usergrade' => $grade->grade,
'usergrade' => $grade->usergrade,
'maxgrade' => $maxgrade,
'gradedby' => $gradername,
'timecreated' => $grade->timecreated,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function test_execute_fetch_empty(): void {
$this->assertIsInt($result['grade']['timemodified']);

$this->assertArrayHasKey('usergrade', $result['grade']);
$this->assertEquals(0, $result['grade']['usergrade']);
$this->assertEquals('- / 100.00', $result['grade']['usergrade']);

$this->assertArrayHasKey('maxgrade', $result['grade']);
$this->assertIsInt($result['grade']['maxgrade']);
Expand Down Expand Up @@ -275,7 +275,7 @@ private function execute_and_assert_fetch ($forum, $controller, $definition, $fe
$this->assertIsInt($result['grade']['timemodified']);

$this->assertArrayHasKey('usergrade', $result['grade']);
$this->assertEquals(25, $result['grade']['usergrade']);
$this->assertEquals('25.00 / 100.00', $result['grade']['usergrade']);

$this->assertArrayHasKey('maxgrade', $result['grade']);
$this->assertIsInt($result['grade']['maxgrade']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function test_execute_store_graded(): void {
$this->assertIsInt($result['grade']['timemodified']);

$this->assertArrayHasKey('usergrade', $result['grade']);
$this->assertEquals(0.5, $result['grade']['usergrade']);
$this->assertEquals('0.50 / 2.00', $result['grade']['usergrade']);

$this->assertArrayHasKey('maxgrade', $result['grade']);
$this->assertIsInt($result['grade']['maxgrade']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public static function get_fetch_data(gradeitem $gradeitem, stdClass $gradeduser
global $USER;
// Set up all the controllers etc that we'll be needing.
$hasgrade = $gradeitem->user_has_grade($gradeduser);
$grade = $gradeitem->get_grade_for_user($gradeduser, $USER);
$grade = $gradeitem->get_formatted_grade_for_user($gradeduser, $USER);
$instance = $gradeitem->get_advanced_grading_instance($USER, $grade);
if (!$instance) {
throw new moodle_exception('error:gradingunavailable', 'grading');
Expand Down Expand Up @@ -252,7 +252,7 @@ public static function get_fetch_data(gradeitem $gradeitem, stdClass $gradeduser
'rubricmode' => 'evaluate editable',
'teacherdescription' => $teacherdescription,
'canedit' => false,
'usergrade' => $grade->grade,
'usergrade' => $grade->usergrade,
'maxgrade' => $maxgrade,
'gradedby' => $gradername,
'timecreated' => $grade->timecreated,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function test_execute_fetch_empty(): void {
$this->assertIsInt($result['grade']['timemodified']);

$this->assertArrayHasKey('usergrade', $result['grade']);
$this->assertEquals(0, $result['grade']['usergrade']);
$this->assertEquals('- / 100.00', $result['grade']['usergrade']);

$this->assertArrayHasKey('maxgrade', $result['grade']);
$this->assertIsInt($result['grade']['maxgrade']);
Expand Down Expand Up @@ -282,7 +282,7 @@ private function execute_and_assert_fetch ($forum, $controller, $definition, $fe
$this->assertIsInt($result['grade']['timemodified']);

$this->assertArrayHasKey('usergrade', $result['grade']);
$this->assertEquals(50, $result['grade']['usergrade']);
$this->assertEquals('50.00 / 100.00', $result['grade']['usergrade']);

$this->assertArrayHasKey('maxgrade', $result['grade']);
$this->assertIsInt($result['grade']['maxgrade']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function test_execute_store_graded(): void {
$this->assertIsInt($result['grade']['timemodified']);

$this->assertArrayHasKey('usergrade', $result['grade']);
$this->assertEquals(1, $result['grade']['usergrade']);
$this->assertEquals('1.00 / 2.00', $result['grade']['usergrade']);

$this->assertArrayHasKey('maxgrade', $result['grade']);
$this->assertIsInt($result['grade']['maxgrade']);
Expand Down
28 changes: 28 additions & 0 deletions grade/templates/grades/grader/gradingpanel/point_blank.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{{!
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 <http://www.gnu.org/licenses/>.
}}
{{!
@template core_grades/grades/grader/gradingpanel/point_black
Point-based grading template for use in the grading panel.
Context variables required for this template:
Example context (json):
{
}
}}
Loading

0 comments on commit 69ec437

Please sign in to comment.