#98 Add ability to display the date and grade for all grade items

This commit is contained in:
Mark Nelson 2017-11-11 17:16:00 +08:00
parent 572025716b
commit bd9a59631f
4 changed files with 130 additions and 3 deletions

View file

@ -475,6 +475,23 @@ class element_helper {
}
}
if ($gradeitems = \grade_item::fetch_all(['courseid' => $course->id])) {
$arrgradeitems = [];
foreach ($gradeitems as $gi) {
// Skip the course and mod items since we already have them.
if ($gi->itemtype == 'mod' || $gi->itemtype == 'course') {
continue;
}
$arrgradeitems['gradeitem:' . $gi->id] = get_string('gradeitem', 'grades') . ' : ' . $gi->get_name(true);
}
// Alphabetise this.
asort($arrgradeitems);
// Merge results.
$modules = $modules + $arrgradeitems;
}
return $modules;
}
@ -562,4 +579,33 @@ class element_helper {
$dategraded
);
}
/**
* Helper function to return the grade information for a grade item for a specified user.
*
* @param int $gradeitemid
* @param int $gradeformat
* @param int $userid
* @return grade_information|bool the grade information, or false if there is none.
*/
public static function get_grade_item_info($gradeitemid, $gradeformat, $userid) {
if (!$gradeitem = \grade_item::fetch(['id' => $gradeitemid])) {
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->get_dategraded()
);
}
}

View file

@ -164,6 +164,13 @@ class element extends \mod_customcert\element {
GRADE_DISPLAY_TYPE_DEFAULT,
$user->id
);
} else if (strpos($dateitem, 'gradeitem:') === 0) {
$gradeitemid = substr($dateitem, 10);
$grade = \mod_customcert\element_helper::get_grade_item_info(
$gradeitemid,
$dateitem,
$user->id
);
} else {
$grade = \mod_customcert\element_helper::get_mod_grade_info(
$dateitem,

View file

@ -55,7 +55,6 @@ class element extends \mod_customcert\element {
// The grade items.
$mform->addElement('select', 'gradeitem', get_string('gradeitem', 'customcertelement_grade'), $gradeitems);
$mform->setType('gradeitem', PARAM_INT);
$mform->addHelpButton('gradeitem', 'gradeitem', 'customcertelement_grade');
// The grade format.
@ -122,6 +121,13 @@ class element extends \mod_customcert\element {
$gradeformat,
$user->id
);
} else if (strpos($gradeitem, 'gradeitem:') === 0) {
$gradeitemid = substr($gradeitem, 10);
$grade = \mod_customcert\element_helper::get_grade_item_info(
$gradeitemid,
$gradeformat,
$user->id
);
} else {
$grade = \mod_customcert\element_helper::get_mod_grade_info(
$gradeitem,

View file

@ -105,6 +105,8 @@ class mod_customcert_element_helper_testcase extends advanced_testcase {
* Test we return the correct grade items in a course.
*/
public function test_get_grade_items() {
global $DB;
// Create a course.
$course = $this->getDataGenerator()->create_course();
@ -113,12 +115,21 @@ class mod_customcert_element_helper_testcase extends advanced_testcase {
$assign2 = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
$assign3 = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
$gradeitems = \mod_customcert\element_helper::get_grade_items($course);
// Create a manual grade item.
$gi = $this->getDataGenerator()->create_grade_item(['courseid' => $course->id]);
$this->assertCount(3, $gradeitems);
// Create a category grade item.
$gc = $this->getDataGenerator()->create_grade_category(['courseid' => $course->id]);
$gc = $DB->get_record('grade_items', ['itemtype' => 'category', 'iteminstance' => $gc->id]);
// Confirm the function returns the correct number of grade items.
$gradeitems = \mod_customcert\element_helper::get_grade_items($course);
$this->assertCount(5, $gradeitems);
$this->assertArrayHasKey($assign1->cmid, $gradeitems);
$this->assertArrayHasKey($assign2->cmid, $gradeitems);
$this->assertArrayHasKey($assign3->cmid, $gradeitems);
$this->assertArrayHasKey('gradeitem:' . $gi->id, $gradeitems);
$this->assertArrayHasKey('gradeitem:' . $gc->id, $gradeitems);
}
/**
@ -244,4 +255,61 @@ class mod_customcert_element_helper_testcase extends advanced_testcase {
$this->assertEquals('-', $grade->get_displaygrade());
$this->assertEquals(null, $grade->get_dategraded());
}
/**
* Test we return the correct grade information for a grade item.
*/
public function test_get_grade_item_info() {
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create two users.
$student1 = $this->getDataGenerator()->create_user();
$student2 = $this->getDataGenerator()->create_user();
// Enrol them into the course.
$this->getDataGenerator()->enrol_user($student1->id, $course->id);
$this->getDataGenerator()->enrol_user($student2->id, $course->id);
// Create a manual grade item.
$gi = $this->getDataGenerator()->create_grade_item(['itemname' => 'Grade item yo', 'courseid' => $course->id]);
// Give a grade to the student.
$gi = grade_item::fetch(['id' => $gi->id]);
$datagrade = 50;
$time = time();
$grade = new grade_grade();
$grade->itemid = $gi->id;
$grade->userid = $student1->id;
$grade->rawgrade = $datagrade;
$grade->finalgrade = $datagrade;
$grade->rawgrademax = 100;
$grade->rawgrademin = 0;
$grade->timecreated = $time;
$grade->timemodified = $time;
$grade->insert();
// Check that the user received the grade.
$grade = \mod_customcert\element_helper::get_grade_item_info(
$gi->id,
GRADE_DISPLAY_TYPE_PERCENTAGE,
$student1->id
);
$this->assertEquals('Grade item yo', $grade->get_name());
$this->assertEquals('50.00000', $grade->get_grade());
$this->assertEquals('50 %', $grade->get_displaygrade());
$this->assertEquals($time, $grade->get_dategraded());
// Check that the user we did not grade has no grade.
$grade = \mod_customcert\element_helper::get_grade_item_info(
$gi->id,
GRADE_DISPLAY_TYPE_PERCENTAGE,
$student2->id
);
$this->assertEquals('Grade item yo', $grade->get_name());
$this->assertEquals(null, $grade->get_grade());
$this->assertEquals('-', $grade->get_displaygrade());
$this->assertEquals(null, $grade->get_dategraded());
}
}