Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

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