2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Tests the \core\event\user_graded event.
22 * @copyright 2014 Petr Skoda
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
30 require_once($CFG->libdir
. '/mathslib.php');
33 * Class core_event_user_graded_testcase
35 * Tests for event \core\event\user_graded
39 * @copyright 2014 Petr Skoda
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class core_event_user_graded_testcase
extends advanced_testcase
{
47 public function setUp() {
48 $this->resetAfterTest();
52 * Tests the event details.
54 public function test_event() {
56 require_once("$CFG->libdir/gradelib.php");
58 $course = $this->getDataGenerator()->create_course();
59 $user = $this->getDataGenerator()->create_user();
60 $this->getDataGenerator()->enrol_user($user->id
, $course->id
);
62 $grade_category = grade_category
::fetch_course_category($course->id
);
63 $grade_category->load_grade_item();
64 $grade_item = $grade_category->grade_item
;
66 $grade_item->update_final_grade($user->id
, 10, 'gradebook');
68 $grade_grade = new grade_grade(array('userid' => $user->id
, 'itemid' => $grade_item->id
), true);
69 $grade_grade->grade_item
= $grade_item;
71 $event = \core\event\user_graded
::create_from_grade($grade_grade);
73 $this->assertEventLegacyLogData(
74 array($course->id
, 'grade', 'update', '/report/grader/index.php?id=' . $course->id
, $grade_item->itemname
. ': ' . fullname($user)),
77 $this->assertEquals(context_course
::instance($course->id
), $event->get_context());
78 $this->assertSame($event->objecttable
, 'grade_grades');
79 $this->assertEquals($event->objectid
, $grade_grade->id
);
80 $this->assertEquals($event->other
['itemid'], $grade_item->id
);
81 $this->assertTrue($event->other
['overridden']);
82 $this->assertEquals(10, $event->other
['finalgrade']);
84 // Trigger the events.
85 $sink = $this->redirectEvents();
87 $result = $sink->get_events();
90 $this->assertCount(1, $result);
92 $event = reset($result);
93 $this->assertEventContextNotUsed($event);
95 $grade = $event->get_grade();
96 $this->assertInstanceOf('grade_grade', $grade);
97 $this->assertEquals($grade_grade->id
, $grade->id
);
101 * Tests that the event is fired in the correct locations in core.
103 public function test_event_is_triggered() {
106 // Create the items we need to test with.
107 $course = $this->getDataGenerator()->create_course();
108 $user = $this->getDataGenerator()->create_user();
109 $this->getDataGenerator()->enrol_user($user->id
, $course->id
);
110 $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id
));
111 $quizitemparams = array('itemtype' => 'mod', 'itemmodule' => 'quiz', 'iteminstance' => $quiz->id
,
112 'courseid' => $course->id
);
113 $gradeitem = grade_item
::fetch($quizitemparams);
114 $courseitem = grade_item
::fetch_course_item($course->id
);
116 // Now mark the quiz using grade_update as this is the function that modules use.
118 $grade['userid'] = $user->id
;
119 $grade['rawgrade'] = 60;
121 $sink = $this->redirectEvents();
122 grade_update('mod/quiz', $course->id
, 'mod', 'quiz', $quiz->id
, 0, $grade);
123 $events = $sink->get_events();
126 // Ensure we have two user_graded events, one for the item, one for the course.
127 $this->assertEquals(2, count($events));
128 $this->assertInstanceOf('\core\event\user_graded', $events[0]);
129 $this->assertEquals($gradeitem->id
, $events[0]->other
['itemid']);
130 $this->assertInstanceOf('\core\event\user_graded', $events[1]);
131 $this->assertEquals($courseitem->id
, $events[1]->other
['itemid']);
133 // Remove the grades, force the regrading and re-fetch the item. This is needed because the item
134 // will be set as needing an update when the grades are deleted.
135 $gradeitem->delete_all_grades();
136 grade_regrade_final_grades($course->id
);
137 $gradeitem = grade_item
::fetch($quizitemparams);
139 // Now, create a grade using grade_item::update_final_grade().
140 $sink = $this->redirectEvents();
141 $gradeitem->update_raw_grade($user->id
, 10);
142 $events = $sink->get_events();
145 // Ensure we have two user_graded events, one for the item, one for the course.
146 $this->assertEquals(2, count($events));
147 $this->assertInstanceOf('\core\event\user_graded', $events[0]);
148 $this->assertEquals($gradeitem->id
, $events[0]->other
['itemid']);
149 $this->assertInstanceOf('\core\event\user_graded', $events[1]);
150 $this->assertEquals($courseitem->id
, $events[1]->other
['itemid']);
152 // Now, update this grade using grade_item::update_raw_grade().
153 $sink = $this->redirectEvents();
154 $gradeitem->update_raw_grade($user->id
, 20);
155 $events = $sink->get_events();
158 // Ensure we have two user_graded events, one for the item, one for the course.
159 $this->assertEquals(2, count($events));
160 $this->assertInstanceOf('\core\event\user_graded', $events[0]);
161 $this->assertEquals($gradeitem->id
, $events[0]->other
['itemid']);
162 $this->assertInstanceOf('\core\event\user_graded', $events[1]);
163 $this->assertEquals($courseitem->id
, $events[1]->other
['itemid']);
165 // Remove the grades, force the regrading and re-fetch the item. This is needed because the item
166 // will be set as needing an update when the grades are deleted.
167 $gradeitem->delete_all_grades();
168 grade_regrade_final_grades($course->id
);
169 $gradeitem = grade_item
::fetch($quizitemparams);
171 // Now, create a grade using grade_item::update_final_grade().
172 $sink = $this->redirectEvents();
173 $gradeitem->update_final_grade($user->id
, 30);
174 $events = $sink->get_events();
177 // Ensure we have two user_graded events, one for the item, one for the course.
178 $this->assertEquals(2, count($events));
179 $this->assertInstanceOf('\core\event\user_graded', $events[0]);
180 $this->assertEquals($gradeitem->id
, $events[0]->other
['itemid']);
181 $this->assertInstanceOf('\core\event\user_graded', $events[1]);
182 $this->assertEquals($courseitem->id
, $events[1]->other
['itemid']);
184 // Now, update this grade using grade_item::update_final_grade().
185 $sink = $this->redirectEvents();
186 $gradeitem->update_final_grade($user->id
, 40);
187 $events = $sink->get_events();
190 // Ensure we have two user_graded events, one for the item, one for the course.
191 $this->assertEquals(2, count($events));
192 $this->assertInstanceOf('\core\event\user_graded', $events[0]);
193 $this->assertEquals($gradeitem->id
, $events[0]->other
['itemid']);
194 $this->assertInstanceOf('\core\event\user_graded', $events[1]);
195 $this->assertEquals($courseitem->id
, $events[1]->other
['itemid']);
197 // Remove the overridden flag from the grade, this was set by grade_item::update_final_grade().
198 $gradegrade = grade_grade
::fetch(array('itemid' => $gradeitem->id
, 'userid' => $user->id
));
199 $gradegrade->set_overridden(false, false);
201 // Let's change the calculation to anything that won't cause an error.
202 $calculation = calc_formula
::unlocalize("=3");
203 $gradeitem->set_calculation($calculation);
205 // Now force the computation of the grade.
206 $sink = $this->redirectEvents();
207 grade_regrade_final_grades($course->id
);
208 $events = $sink->get_events();
211 // Ensure we have two user_graded events, one for the item, one for the course.
212 $this->assertEquals(2, count($events));
213 $this->assertInstanceOf('\core\event\user_graded', $events[0]);
214 $this->assertEquals($gradeitem->id
, $events[0]->other
['itemid']);
215 $this->assertInstanceOf('\core\event\user_graded', $events[1]);
216 $this->assertEquals($courseitem->id
, $events[1]->other
['itemid']);
218 // Now, let's trick the gradebook, we manually update a grade, and flag the grade item as
219 // needing a regrading, so we can trigger the event in grade_item::regrade_final_grades().
220 $gradeitem = grade_item
::fetch($quizitemparams);
221 $gradeitem->set_calculation('');
222 $gradegrade = grade_grade
::fetch(array('itemid' => $gradeitem->id
, 'userid' => $user->id
));
223 $gradegrade->rawgrade
= 50;
224 $gradegrade->update();
226 $sink = $this->redirectEvents();
227 grade_regrade_final_grades($course->id
);
228 $events = $sink->get_events();
231 // Ensure we have two user_graded events, one for the item, one for the course.
232 $this->assertEquals(2, count($events));
233 $this->assertInstanceOf('\core\event\user_graded', $events[0]);
234 $this->assertEquals($gradeitem->id
, $events[0]->other
['itemid']);
235 $this->assertInstanceOf('\core\event\user_graded', $events[1]);
236 $this->assertEquals($courseitem->id
, $events[1]->other
['itemid']);