Make use of the decimal value setting

We should use this instead of our own logic.
This commit is contained in:
Mark Nelson 2018-12-19 11:44:30 +08:00
parent 83f3fce127
commit 74a8a2f06c
3 changed files with 21 additions and 38 deletions

View file

@ -513,18 +513,12 @@ class element_helper {
return false;
}
// Define how many decimals to display.
$decimals = 2;
if ($gradeformat == GRADE_DISPLAY_TYPE_PERCENTAGE) {
$decimals = 0;
}
$grade = new \grade_grade(array('itemid' => $courseitem->id, 'userid' => $userid));
return new grade_information(
$courseitem->get_name(),
$grade->finalgrade,
grade_format_gradevalue($grade->finalgrade, $courseitem, true, $gradeformat, $decimals),
grade_format_gradevalue($grade->finalgrade, $courseitem, true, $gradeformat),
$grade->get_dategraded()
);
}
@ -554,12 +548,6 @@ class element_helper {
return false;
}
// Define how many decimals to display.
$decimals = 2;
if ($gradeformat == GRADE_DISPLAY_TYPE_PERCENTAGE) {
$decimals = 0;
}
$item = new \grade_item();
$item->gradetype = GRADE_TYPE_VALUE;
$item->courseid = $cm->course;
@ -578,7 +566,7 @@ class element_helper {
return new grade_information(
$item->name,
$objgrade->grade,
grade_format_gradevalue($objgrade->grade, $item, true, $gradeformat, $decimals),
grade_format_gradevalue($objgrade->grade, $item, true, $gradeformat),
$dategraded
);
}
@ -596,18 +584,12 @@ class element_helper {
return false;
}
// Define how many decimals to display.
$decimals = 2;
if ($gradeformat == GRADE_DISPLAY_TYPE_PERCENTAGE) {
$decimals = 0;
}
$grade = new \grade_grade(array('itemid' => $gradeitem->id, 'userid' => $userid));
return new grade_information(
$gradeitem->get_name(),
$grade->finalgrade,
grade_format_gradevalue($grade->finalgrade, $gradeitem, true, $gradeformat, $decimals),
grade_format_gradevalue($grade->finalgrade, $gradeitem, true, $gradeformat),
$grade->get_dategraded()
);
}

View file

@ -106,14 +106,8 @@ class element extends \mod_customcert\element {
// If we are previewing this certificate then just show a demonstration grade.
if ($preview) {
// Define how many decimals to display.
$decimals = 2;
if ($gradeinfo->gradeformat == GRADE_DISPLAY_TYPE_PERCENTAGE) {
$decimals = 0;
}
$courseitem = \grade_item::fetch_course_item($courseid);
$grade = grade_format_gradevalue('100', $courseitem, true, $gradeinfo->gradeformat, $decimals);
$grade = grade_format_gradevalue('100', $courseitem, true, $gradeinfo->gradeformat);
} else {
if ($gradeitem == CUSTOMCERT_GRADE_COURSE) {
$grade = \mod_customcert\element_helper::get_course_grade_info(
@ -165,13 +159,7 @@ class element extends \mod_customcert\element {
$courseitem = \grade_item::fetch_course_item($COURSE->id);
// Define how many decimals to display.
$decimals = 2;
if ($gradeinfo->gradeformat == GRADE_DISPLAY_TYPE_PERCENTAGE) {
$decimals = 0;
}
$grade = grade_format_gradevalue('100', $courseitem, true, $gradeinfo->gradeformat, $decimals);
$grade = grade_format_gradevalue('100', $courseitem, true, $gradeinfo->gradeformat);
return \mod_customcert\element_helper::render_html_content($this, $grade);
}

View file

@ -136,6 +136,11 @@ class mod_customcert_element_helper_testcase extends advanced_testcase {
* Test we return the correct grade information for an activity.
*/
public function test_get_mod_grade_info() {
global $CFG;
// Set that we want 3 decimals to display.
$CFG->grade_decimalpoints = 3;
// Create a course.
$course = $this->getDataGenerator()->create_course();
@ -181,7 +186,7 @@ class mod_customcert_element_helper_testcase extends advanced_testcase {
$this->assertEquals($assign->name, $grade->get_name());
$this->assertEquals('50.00000', $grade->get_grade());
$this->assertEquals('50 %', $grade->get_displaygrade());
$this->assertEquals('50.000 %', $grade->get_displaygrade());
$this->assertEquals($time, $grade->get_dategraded());
// Check that the user we did not grade has no grade.
@ -205,6 +210,9 @@ class mod_customcert_element_helper_testcase extends advanced_testcase {
// Including to use constant.
require_once($CFG->dirroot . '/mod/customcert/element/grade/classes/element.php');
// Set that we want 3 decimals to display.
$CFG->grade_decimalpoints = 3;
// Create a course.
$course = $this->getDataGenerator()->create_course();
@ -241,7 +249,7 @@ class mod_customcert_element_helper_testcase extends advanced_testcase {
$this->assertEquals(get_string('coursetotal', 'grades'), $grade->get_name());
$this->assertEquals('50.00000', $grade->get_grade());
$this->assertEquals('50 %', $grade->get_displaygrade());
$this->assertEquals('50.000 %', $grade->get_displaygrade());
$this->assertEquals($time, $grade->get_dategraded());
// Check that the user we did not grade has no grade.
@ -260,6 +268,11 @@ class mod_customcert_element_helper_testcase extends advanced_testcase {
* Test we return the correct grade information for a grade item.
*/
public function test_get_grade_item_info() {
global $CFG;
// Set that we want 3 decimals to display.
$CFG->grade_decimalpoints = 3;
// Create a course.
$course = $this->getDataGenerator()->create_course();
@ -298,7 +311,7 @@ class mod_customcert_element_helper_testcase extends advanced_testcase {
$this->assertEquals('Grade item yo', $grade->get_name());
$this->assertEquals('50.00000', $grade->get_grade());
$this->assertEquals('50 %', $grade->get_displaygrade());
$this->assertEquals('50.000 %', $grade->get_displaygrade());
$this->assertEquals($time, $grade->get_dategraded());
// Check that the user we did not grade has no grade.