Differences Between: [Versions 311 and 402] [Versions 311 and 403]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 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. 8 // 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. 13 // 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/>. 16 17 namespace core\event; 18 19 defined('MOODLE_INTERNAL') || die(); 20 21 global $CFG; 22 23 require_once($CFG->libdir . '/mathslib.php'); 24 25 /** 26 * Tests for event \core\event\user_graded 27 * 28 * @package core 29 * @category test 30 * @copyright 2014 Petr Skoda 31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 32 */ 33 class user_graded_test extends \advanced_testcase { 34 35 /** 36 * Tests set up. 37 */ 38 public function setUp(): void { 39 $this->resetAfterTest(); 40 } 41 42 /** 43 * Tests the event details. 44 */ 45 public function test_event() { 46 global $CFG; 47 require_once("$CFG->libdir/gradelib.php"); 48 49 $course = $this->getDataGenerator()->create_course(); 50 $user = $this->getDataGenerator()->create_user(); 51 $this->getDataGenerator()->enrol_user($user->id, $course->id); 52 53 $grade_category = \grade_category::fetch_course_category($course->id); 54 $grade_category->load_grade_item(); 55 $grade_item = $grade_category->grade_item; 56 57 $grade_item->update_final_grade($user->id, 10, 'gradebook'); 58 59 $grade_grade = new \grade_grade(array('userid' => $user->id, 'itemid' => $grade_item->id), true); 60 $grade_grade->grade_item = $grade_item; 61 62 $event = \core\event\user_graded::create_from_grade($grade_grade); 63 64 $this->assertEventLegacyLogData( 65 array($course->id, 'grade', 'update', '/report/grader/index.php?id=' . $course->id, $grade_item->itemname . ': ' . fullname($user)), 66 $event 67 ); 68 $this->assertEquals(\context_course::instance($course->id), $event->get_context()); 69 $this->assertSame($event->objecttable, 'grade_grades'); 70 $this->assertEquals($event->objectid, $grade_grade->id); 71 $this->assertEquals($event->other['itemid'], $grade_item->id); 72 $this->assertTrue($event->other['overridden']); 73 $this->assertEquals(10, $event->other['finalgrade']); 74 75 // Trigger the events. 76 $sink = $this->redirectEvents(); 77 $event->trigger(); 78 $result = $sink->get_events(); 79 $sink->close(); 80 81 $this->assertCount(1, $result); 82 83 $event = reset($result); 84 $this->assertEventContextNotUsed($event); 85 86 $grade = $event->get_grade(); 87 $this->assertInstanceOf('grade_grade', $grade); 88 $this->assertEquals($grade_grade->id, $grade->id); 89 } 90 91 /** 92 * Tests that the event is fired in the correct locations in core. 93 */ 94 public function test_event_is_triggered() { 95 global $DB; 96 97 // Create the items we need to test with. 98 $course = $this->getDataGenerator()->create_course(); 99 $user = $this->getDataGenerator()->create_user(); 100 $this->getDataGenerator()->enrol_user($user->id, $course->id); 101 $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id)); 102 $quizitemparams = array('itemtype' => 'mod', 'itemmodule' => 'quiz', 'iteminstance' => $quiz->id, 103 'courseid' => $course->id); 104 $gradeitem = \grade_item::fetch($quizitemparams); 105 $courseitem = \grade_item::fetch_course_item($course->id); 106 107 // Now mark the quiz using grade_update as this is the function that modules use. 108 $grade = array(); 109 $grade['userid'] = $user->id; 110 $grade['rawgrade'] = 60; 111 112 $sink = $this->redirectEvents(); 113 grade_update('mod/quiz', $course->id, 'mod', 'quiz', $quiz->id, 0, $grade); 114 $events = $sink->get_events(); 115 $sink->close(); 116 117 // Ensure we have two user_graded events, one for the item, one for the course. 118 $this->assertEquals(2, count($events)); 119 $this->assertInstanceOf('\core\event\user_graded', $events[0]); 120 $this->assertEquals($gradeitem->id, $events[0]->other['itemid']); 121 $this->assertInstanceOf('\core\event\user_graded', $events[1]); 122 $this->assertEquals($courseitem->id, $events[1]->other['itemid']); 123 124 // Remove the grades, force the regrading and re-fetch the item. This is needed because the item 125 // will be set as needing an update when the grades are deleted. 126 $gradeitem->delete_all_grades(); 127 grade_regrade_final_grades($course->id); 128 $gradeitem = \grade_item::fetch($quizitemparams); 129 130 // Now, create a grade using \grade_item::update_final_grade(). 131 $sink = $this->redirectEvents(); 132 $gradeitem->update_raw_grade($user->id, 10); 133 $events = $sink->get_events(); 134 $sink->close(); 135 136 // Ensure we have two user_graded events, one for the item, one for the course. 137 $this->assertEquals(2, count($events)); 138 $this->assertInstanceOf('\core\event\user_graded', $events[0]); 139 $this->assertEquals($gradeitem->id, $events[0]->other['itemid']); 140 $this->assertInstanceOf('\core\event\user_graded', $events[1]); 141 $this->assertEquals($courseitem->id, $events[1]->other['itemid']); 142 143 // Now, update this grade using \grade_item::update_raw_grade(). 144 $sink = $this->redirectEvents(); 145 $gradeitem->update_raw_grade($user->id, 20); 146 $events = $sink->get_events(); 147 $sink->close(); 148 149 // Ensure we have two user_graded events, one for the item, one for the course. 150 $this->assertEquals(2, count($events)); 151 $this->assertInstanceOf('\core\event\user_graded', $events[0]); 152 $this->assertEquals($gradeitem->id, $events[0]->other['itemid']); 153 $this->assertInstanceOf('\core\event\user_graded', $events[1]); 154 $this->assertEquals($courseitem->id, $events[1]->other['itemid']); 155 156 // Remove the grades, force the regrading and re-fetch the item. This is needed because the item 157 // will be set as needing an update when the grades are deleted. 158 $gradeitem->delete_all_grades(); 159 grade_regrade_final_grades($course->id); 160 $gradeitem = \grade_item::fetch($quizitemparams); 161 162 // Now, create a grade using \grade_item::update_final_grade(). 163 $sink = $this->redirectEvents(); 164 $gradeitem->update_final_grade($user->id, 30); 165 $events = $sink->get_events(); 166 $sink->close(); 167 168 // Ensure we have two user_graded events, one for the item, one for the course. 169 $this->assertEquals(2, count($events)); 170 $this->assertInstanceOf('\core\event\user_graded', $events[0]); 171 $this->assertEquals($gradeitem->id, $events[0]->other['itemid']); 172 $this->assertInstanceOf('\core\event\user_graded', $events[1]); 173 $this->assertEquals($courseitem->id, $events[1]->other['itemid']); 174 175 // Now, update this grade using \grade_item::update_final_grade(). 176 $sink = $this->redirectEvents(); 177 $gradeitem->update_final_grade($user->id, 40); 178 $events = $sink->get_events(); 179 $sink->close(); 180 181 // Ensure we have two user_graded events, one for the item, one for the course. 182 $this->assertEquals(2, count($events)); 183 $this->assertInstanceOf('\core\event\user_graded', $events[0]); 184 $this->assertEquals($gradeitem->id, $events[0]->other['itemid']); 185 $this->assertInstanceOf('\core\event\user_graded', $events[1]); 186 $this->assertEquals($courseitem->id, $events[1]->other['itemid']); 187 188 // Remove the overridden flag from the grade, this was set by \grade_item::update_final_grade(). 189 $gradegrade = \grade_grade::fetch(array('itemid' => $gradeitem->id, 'userid' => $user->id)); 190 $gradegrade->set_overridden(false, false); 191 192 // Let's change the calculation to anything that won't cause an error. 193 $calculation = \calc_formula::unlocalize("=3"); 194 $gradeitem->set_calculation($calculation); 195 196 // Now force the computation of the grade. 197 $sink = $this->redirectEvents(); 198 grade_regrade_final_grades($course->id); 199 $events = $sink->get_events(); 200 $sink->close(); 201 202 // Ensure we have two user_graded events, one for the item, one for the course. 203 $this->assertEquals(2, count($events)); 204 $this->assertInstanceOf('\core\event\user_graded', $events[0]); 205 $this->assertEquals($gradeitem->id, $events[0]->other['itemid']); 206 $this->assertInstanceOf('\core\event\user_graded', $events[1]); 207 $this->assertEquals($courseitem->id, $events[1]->other['itemid']); 208 209 // Now, let's trick the gradebook, we manually update a grade, and flag the grade item as 210 // needing a regrading, so we can trigger the event in \grade_item::regrade_final_grades(). 211 $gradeitem = \grade_item::fetch($quizitemparams); 212 $gradeitem->set_calculation(''); 213 $gradegrade = \grade_grade::fetch(array('itemid' => $gradeitem->id, 'userid' => $user->id)); 214 $gradegrade->rawgrade = 50; 215 $gradegrade->update(); 216 217 $sink = $this->redirectEvents(); 218 grade_regrade_final_grades($course->id); 219 $events = $sink->get_events(); 220 $sink->close(); 221 222 // Ensure we have two user_graded events, one for the item, one for the course. 223 $this->assertEquals(2, count($events)); 224 $this->assertInstanceOf('\core\event\user_graded', $events[0]); 225 $this->assertEquals($gradeitem->id, $events[0]->other['itemid']); 226 $this->assertInstanceOf('\core\event\user_graded', $events[1]); 227 $this->assertEquals($courseitem->id, $events[1]->other['itemid']); 228 } 229 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body