From 30dd9930ba9467cbeb721e75bf714a531cca7bdd Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Wed, 21 Nov 2012 11:05:16 +0800 Subject: [PATCH] MDL-36680 core_grade: fixed grade_report::blank_hidden_total() and added unit tests --- grade/report/lib.php | 10 +- grade/tests/reportlib_test.php | 197 +++++++++++++++++++++++++++++++++ 2 files changed, 204 insertions(+), 3 deletions(-) create mode 100644 grade/tests/reportlib_test.php diff --git a/grade/report/lib.php b/grade/report/lib.php index 2475550aec5..6e6db0b7946 100644 --- a/grade/report/lib.php +++ b/grade/report/lib.php @@ -351,17 +351,21 @@ abstract class grade_report { global $CFG, $DB; static $hiding_affected = null;//array of items in this course affected by hiding - //if we're dealing with multiple users we need to know when we've moved on to a new user + // If we're dealing with multiple users we need to know when we've moved on to a new user. static $previous_userid = null; + // If we're dealing with multiple courses we need to know when we've moved on to a new course. + static $previous_courseid = null; + if( $this->showtotalsifcontainhidden==GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN ) { return $finalgrade; } - //if we've moved on to another user don't return the previous user's affected grades - if ($previous_userid!=$this->user->id) { + // If we've moved on to another course or user, reload the grades. + if ($previous_userid != $this->user->id || $previous_courseid != $courseid) { $hiding_affected = null; $previous_userid = $this->user->id; + $previous_courseid = $courseid; } if( !$hiding_affected ) { diff --git a/grade/tests/reportlib_test.php b/grade/tests/reportlib_test.php new file mode 100644 index 00000000000..48b0eaaff7f --- /dev/null +++ b/grade/tests/reportlib_test.php @@ -0,0 +1,197 @@ +. + +/** + * Unit tests for grade/report/lib.php. + * + * @pacakge core_grade + * @category phpunit + * @author Andrew Davis + * @license http://www.gnu.org/copyleft/gpl.html GNU Public License + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot.'/grade/lib.php'); +require_once($CFG->dirroot.'/grade/report/lib.php'); + +/** + * A test class used to test grade_report, the abstract grade report parent class + */ +class grade_report_test extends grade_report { + public function __construct($courseid, $gpr, $context, $user) { + parent::__construct($courseid, $gpr, $context); + $this->user = $user; + } + + /** + * A wrapper around blank_hidden_total() to allow test code to call it directly + */ + public function blank_hidden_total($courseid, $courseitem, $finalgrade) { + return parent::blank_hidden_total($courseid, $courseitem, $finalgrade); + } + + /** + * Implementation of the abstract method process_data() + */ + public function process_data($data) { + } + + /** + * Implementation of the abstract method process_action() + */ + public function process_action($target, $action) { + } +} + +/** + * Tests grade_report, the parent class for all grade reports. + */ +class gradereportlib_testcase extends advanced_testcase { + + /** + * Tests grade_report::blank_hidden_total() + */ + public function test_blank_hidden_total() { + global $DB; + + $this->resetAfterTest(true); + + $student = $this->getDataGenerator()->create_user(); + $this->setUser($student); + + // Create a course and two activities. + // One activity will be hidden. + $course = $this->getDataGenerator()->create_course(); + $coursegradeitem = grade_item::fetch_course_item($course->id); + $coursecontext = context_course::instance($course->id); + + $data = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id)); + $datacm = get_coursemodule_from_id('data', $data->cmid); + + $forum = $this->getDataGenerator()->create_module('forum', array('assessed' => 1, 'scale' => 100, 'course' => $course->id)); + $forumcm = get_coursemodule_from_id('forum', $forum->cmid); + + // Insert student grades for the two activities. + $gi = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'data', 'iteminstance' => $data->id, 'courseid' => $course->id)); + $datagrade = 50; + $grade_grade = new grade_grade(); + $grade_grade->itemid = $gi->id; + $grade_grade->userid = $student->id; + $grade_grade->rawgrade = $datagrade; + $grade_grade->finalgrade = $datagrade; + $grade_grade->rawgrademax = 100; + $grade_grade->rawgrademin = 0; + $grade_grade->timecreated = time(); + $grade_grade->timemodified = time(); + $grade_grade->insert(); + + $gi = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'forum', 'iteminstance' => $forum->id, 'courseid' => $course->id)); + $forumgrade = 70; + $grade_grade = new grade_grade(); + $grade_grade->itemid = $gi->id; + $grade_grade->userid = $student->id; + $grade_grade->rawgrade = $forumgrade; + $grade_grade->finalgrade = $forumgrade; + $grade_grade->rawgrademax = 100; + $grade_grade->rawgrademin = 0; + $grade_grade->timecreated = time(); + $grade_grade->timemodified = time(); + $grade_grade->insert(); + + // Hide the database activity. + set_coursemodule_visible($datacm->id, 0); + + $gpr = new grade_plugin_return(array('type' => 'report', 'courseid' => $course->id)); + $report = new grade_report_test($course->id, $gpr, $coursecontext, $student); + + // Should return the supplied student total grade regardless of hiding. + $report->showtotalsifcontainhidden = GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN; + $this->assertEquals($datagrade + $forumgrade, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade)); + + // Should blank the student total as course grade depends on a hidden item. + $report->showtotalsifcontainhidden = GRADE_REPORT_HIDE_TOTAL_IF_CONTAINS_HIDDEN; + $this->assertEquals(null, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade)); + + // Should return the course total minus the hidden database activity grade. + $report->showtotalsifcontainhidden = GRADE_REPORT_SHOW_TOTAL_IF_CONTAINS_HIDDEN; + $this->assertEquals($forumgrade, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade)); + + // Note: we cannot simply hide modules and call $report->blank_hidden_total() again. + // It stores grades in a static variable so $report->blank_hidden_total() will return incorrect totals + // In practice this isn't a problem. Grade visibility isn't altered mid-request outside of the unit tests. + + // Add a second course to test: + // 1) How a course with no visible activities behaves. + // 2) That $report->blank_hidden_total() correctly moves on to the new course. + $course = $this->getDataGenerator()->create_course(); + $coursegradeitem = grade_item::fetch_course_item($course->id); + $coursecontext = context_course::instance($course->id); + + $data = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id)); + $datacm = get_coursemodule_from_id('data', $data->cmid); + + $forum = $this->getDataGenerator()->create_module('forum', array('assessed' => 1, 'scale' => 100, 'course' => $course->id)); + $forumcm = get_coursemodule_from_id('forum', $forum->cmid); + + $gi = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'data', 'iteminstance' => $data->id, 'courseid' => $course->id)); + $datagrade = 50; + $grade_grade = new grade_grade(); + $grade_grade->itemid = $gi->id; + $grade_grade->userid = $student->id; + $grade_grade->rawgrade = $datagrade; + $grade_grade->finalgrade = $datagrade; + $grade_grade->rawgrademax = 100; + $grade_grade->rawgrademin = 0; + $grade_grade->timecreated = time(); + $grade_grade->timemodified = time(); + $grade_grade->insert(); + + $gi = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'forum', 'iteminstance' => $forum->id, 'courseid' => $course->id)); + $forumgrade = 70; + $grade_grade = new grade_grade(); + $grade_grade->itemid = $gi->id; + $grade_grade->userid = $student->id; + $grade_grade->rawgrade = $forumgrade; + $grade_grade->finalgrade = $forumgrade; + $grade_grade->rawgrademax = 100; + $grade_grade->rawgrademin = 0; + $grade_grade->timecreated = time(); + $grade_grade->timemodified = time(); + $grade_grade->insert(); + + // Hide both activities. + set_coursemodule_visible($datacm->id, 0); + set_coursemodule_visible($forumcm->id, 0); + + $gpr = new grade_plugin_return(array('type' => 'report', 'courseid' => $course->id)); + $report = new grade_report_test($course->id, $gpr, $coursecontext, $student); + + // Should return the supplied student total grade regardless of hiding. + $report->showtotalsifcontainhidden = GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN; + $this->assertEquals($datagrade + $forumgrade, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade)); + + // Should blank the student total as course grade depends on a hidden item. + $report->showtotalsifcontainhidden = GRADE_REPORT_HIDE_TOTAL_IF_CONTAINS_HIDDEN; + $this->assertEquals(null, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade)); + + // Should return the course total minus the hidden activity grades. + // They are both hidden so should return null. + $report->showtotalsifcontainhidden = GRADE_REPORT_SHOW_TOTAL_IF_CONTAINS_HIDDEN; + $this->assertEquals(null, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade)); + } +}