From 8edb995edbcaf4b3198c903d7a68c3d6f569afeb Mon Sep 17 00:00:00 2001 From: Nikita Kalinin Date: Mon, 2 Mar 2015 21:35:48 +0300 Subject: [PATCH] MDL-49327 marking guide: Added condition on definitionid. --- grade/grading/form/guide/lib.php | 4 +- grade/grading/form/guide/tests/guide_test.php | 99 +++++++++++++++++++ 2 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 grade/grading/form/guide/tests/guide_test.php diff --git a/grade/grading/form/guide/lib.php b/grade/grading/form/guide/lib.php index 3565ef60734..4a896ea76e9 100644 --- a/grade/grading/form/guide/lib.php +++ b/grade/grading/form/guide/lib.php @@ -563,8 +563,8 @@ class gradingform_guide_controller extends gradingform_controller { return $this->get_instance($instance); } if ($itemid && $raterid) { - if ($rs = $DB->get_records('grading_instances', array('raterid' => $raterid, 'itemid' => $itemid), - 'timemodified DESC', '*', 0, 1)) { + $params = array('definitionid' => $this->definition->id, 'raterid' => $raterid, 'itemid' => $itemid); + if ($rs = $DB->get_records('grading_instances', $params, 'timemodified DESC', '*', 0, 1)) { $record = reset($rs); $currentinstance = $this->get_current_instance($raterid, $itemid); if ($record->status == gradingform_guide_instance::INSTANCE_STATUS_INCOMPLETE && diff --git a/grade/grading/form/guide/tests/guide_test.php b/grade/grading/form/guide/tests/guide_test.php new file mode 100644 index 00000000000..f736bbccafc --- /dev/null +++ b/grade/grading/form/guide/tests/guide_test.php @@ -0,0 +1,99 @@ +. + +/** + * Unit tests for Marking Guide grading method. + * + * @package gradingform_guide + * @category phpunit + * @copyright 2015 Nikita Kalinin + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/grade/grading/lib.php'); +require_once($CFG->dirroot . '/grade/grading/form/guide/lib.php'); + +/** + * Test cases for the Marking Guide. + * + * @package gradingform_guide + * @category phpunit + * @copyright 2015 Nikita Kalinin + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class gradingform_guide_testcase extends advanced_testcase { + /** + * Unit test to get draft instance and create new instance. + */ + public function test_get_or_create_instance() { + global $DB; + + $this->resetAfterTest(true); + + // Create fake areas. + $fakearea = (object)array( + 'contextid' => 1, + 'component' => 'mod_assign', + 'areaname' => 'submissions', + 'activemethod' => 'guide' + ); + $fakearea1id = $DB->insert_record('grading_areas', $fakearea); + $fakearea->contextid = 2; + $fakearea2id = $DB->insert_record('grading_areas', $fakearea); + + // Create fake definitions. + $fakedefinition = (object)array( + 'areaid' => $fakearea1id, + 'method' => 'guide', + 'name' => 'fakedef', + 'status' => gradingform_controller::DEFINITION_STATUS_READY, + 'timecreated' => 0, + 'usercreated' => 1, + 'timemodified' => 0, + 'usermodified' => 1, + ); + $fakedef1id = $DB->insert_record('grading_definitions', $fakedefinition); + $fakedefinition->areaid = $fakearea2id; + $fakedef2id = $DB->insert_record('grading_definitions', $fakedefinition); + + // Create fake guide instance in first area. + $fakeinstance = (object)array( + 'definitionid' => $fakedef1id, + 'raterid' => 1, + 'itemid' => 1, + 'rawgrade' => null, + 'status' => 0, + 'feedback' => null, + 'feedbackformat' => 0, + 'timemodified' => 0 + ); + $fakeinstanceid = $DB->insert_record('grading_instances', $fakeinstance); + + $manager1 = get_grading_manager($fakearea1id); + $manager2 = get_grading_manager($fakearea2id); + $controller1 = $manager1->get_controller('guide'); + $controller2 = $manager2->get_controller('guide'); + + $instance1 = $controller1->get_or_create_instance(0, 1, 1); + $instance2 = $controller2->get_or_create_instance(0, 1, 1); + + // Definitions should not be the same. + $this->assertEquals(false, $instance1->get_data('definitionid') == $instance2->get_data('definitionid')); + } +}