Proyectos de Subversion Moodle

Rev

Rev 1 | | 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 gradereport_history;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
/**
22
 * Grade history report test class.
23
 *
24
 * @package    gradereport_history
25
 * @copyright  2014 Frédéric Massart - FMCorz.net
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
11 efrain 28
final class report_test extends \advanced_testcase {
1 efrain 29
 
30
    /**
31
     * Create some grades.
32
     */
11 efrain 33
    public function test_query_db(): void {
1 efrain 34
        $this->resetAfterTest();
35
 
36
        // Making the setup.
37
        $c1 = $this->getDataGenerator()->create_course();
38
        $c2 = $this->getDataGenerator()->create_course();
39
        $c1ctx = \context_course::instance($c1->id);
40
        $c2ctx = \context_course::instance($c2->id);
41
 
42
        // Users.
43
        $u1 = $this->getDataGenerator()->create_user();
44
        $u2 = $this->getDataGenerator()->create_user();
45
        $u3 = $this->getDataGenerator()->create_user();
46
        $u4 = $this->getDataGenerator()->create_user();
47
        $u5 = $this->getDataGenerator()->create_user();
48
        $grader1 = $this->getDataGenerator()->create_user();
49
        $grader2 = $this->getDataGenerator()->create_user();
50
        self::getDataGenerator()->enrol_user($grader1->id, $c1->id, 'teacher');
51
        self::getDataGenerator()->enrol_user($grader2->id, $c1->id, 'teacher');
52
        self::getDataGenerator()->enrol_user($u2->id, $c1->id, 'student');
53
        self::getDataGenerator()->enrol_user($u3->id, $c1->id, 'student');
54
        self::getDataGenerator()->enrol_user($u4->id, $c1->id, 'student');
55
        self::getDataGenerator()->enrol_user($u5->id, $c1->id, 'student');
56
 
57
        self::getDataGenerator()->enrol_user($grader1->id, $c2->id, 'teacher');
58
        self::getDataGenerator()->enrol_user($u5->id, $c2->id, 'student');
59
 
60
        // Modules.
61
        $c1m1 = $this->getDataGenerator()->create_module('assign', array('course' => $c1));
62
        $c1m2 = $this->getDataGenerator()->create_module('assign', array('course' => $c1));
63
        $c1m3 = $this->getDataGenerator()->create_module('assign', array('course' => $c1));
64
        $c2m1 = $this->getDataGenerator()->create_module('assign', array('course' => $c2));
65
        $c2m2 = $this->getDataGenerator()->create_module('assign', array('course' => $c2));
66
 
67
        // Creating fake history data.
68
        $giparams = array('itemtype' => 'mod', 'itemmodule' => 'assign');
69
        $grades = array();
70
 
71
        $this->setUser($grader1);
72
 
73
        $gi = \grade_item::fetch($giparams + array('iteminstance' => $c1m1->id));
74
        $grades['c1m1u1'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id,
75
                'timemodified' => time() - 3600));
76
        $grades['c1m1u2'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u2->id,
77
                'timemodified' => time() + 3600));
78
        $grades['c1m1u3'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u3->id));
79
        $grades['c1m1u4'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u4->id));
80
        $grades['c1m1u5'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u5->id));
81
 
82
        $gi = \grade_item::fetch($giparams + array('iteminstance' => $c1m2->id));
83
        $grades['c1m2u1'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id));
84
        $grades['c1m2u2'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u2->id));
85
 
86
        $gi = \grade_item::fetch($giparams + array('iteminstance' => $c1m3->id));
87
        $grades['c1m3u1'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id));
88
 
89
        $gi = \grade_item::fetch($giparams + array('iteminstance' => $c2m1->id));
90
        $grades['c2m1u1'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id,
91
            'usermodified' => $grader1->id));
92
        $grades['c2m1u2'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u2->id,
93
            'usermodified' => $grader1->id));
94
        $grades['c2m1u3'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u3->id,
95
            'usermodified' => $grader1->id));
96
        $grades['c2m1u4'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u4->id,
97
            'usermodified' => $grader2->id));
98
 
99
        // Histories where grades have not been revised..
100
        $grades['c2m1u5a'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u5->id,
101
            'timemodified' => time() - 60));
102
        $grades['c2m1u5b'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u5->id,
103
            'timemodified' => time()));
104
        $grades['c2m1u5c'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u5->id,
105
            'timemodified' => time() + 60));
106
 
107
        // Histories where grades have been revised and not revised.
108
        $now = time();
109
        $gi = \grade_item::fetch($giparams + array('iteminstance' => $c2m2->id));
110
        $grades['c2m2u1a'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id,
111
            'timemodified' => $now - 60, 'finalgrade' => 50));
112
        $grades['c2m2u1b'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id,
113
            'timemodified' => $now - 50, 'finalgrade' => 50));      // Not revised.
114
        $grades['c2m2u1c'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id,
115
            'timemodified' => $now, 'finalgrade' => 75));
116
        $grades['c2m2u1d'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id,
117
            'timemodified' => $now + 10, 'finalgrade' => 75));      // Not revised.
118
        $grades['c2m2u1e'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id,
119
            'timemodified' => $now + 60, 'finalgrade' => 25));
120
        $grades['c2m2u1f'] = $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id,
121
            'timemodified' => $now + 70, 'finalgrade' => 25));      // Not revised.
122
 
123
        // TODO MDL-46736 Handle deleted/non-existing grade items.
124
        // Histories with missing grade items, considered as deleted.
125
        // $grades['c2x1u5'] = $this->create_grade_history($giparams + array('itemid' => -1, 'userid' => $u5->id, 'courseid' => $c1->id));
126
        // $grades['c2x2u5'] = $this->create_grade_history($giparams + array('itemid' => 999999, 'userid' => $u5->id, 'courseid' => $c1->id));
127
 
128
        // Basic filtering based on course id.
129
        $this->assertEquals(8, $this->get_tablelog_results($c1ctx, array(), true));
130
        $this->assertEquals(13, $this->get_tablelog_results($c2ctx, array(), true));
131
 
132
        // Filtering on 1 user the current user cannot access should return all records.
133
        $this->assertEquals(8, $this->get_tablelog_results($c1ctx, array('userids' => $u1->id), true));
134
 
135
        // Filtering on 2 users, only one of whom the current user can access.
136
        $this->assertEquals(1, $this->get_tablelog_results($c1ctx, ['userids' => "$u1->id,$u3->id"], true));
137
        $results = $this->get_tablelog_results($c1ctx, ['userids' => "$u1->id,$u3->id"]);
138
        $this->assertGradeHistoryIds([$grades['c1m1u3']->id], $results);
139
 
140
        // Filtering on 2 users, both of whom the current user can access.
141
        $this->assertEquals(3, $this->get_tablelog_results($c1ctx, ['userids' => "$u2->id,$u3->id"], true));
142
        $results = $this->get_tablelog_results($c1ctx, ['userids' => "$u2->id,$u3->id"]);
143
        $this->assertGradeHistoryIds([$grades['c1m1u2']->id, $grades['c1m1u3']->id, $grades['c1m2u2']->id], $results);
144
 
145
        // Filtering based on one grade item.
146
        $gi = \grade_item::fetch($giparams + array('iteminstance' => $c1m1->id));
147
        $this->assertEquals(5, $this->get_tablelog_results($c1ctx, array('itemid' => $gi->id), true));
148
        $gi = \grade_item::fetch($giparams + array('iteminstance' => $c1m3->id));
149
        $this->assertEquals(1, $this->get_tablelog_results($c1ctx, array('itemid' => $gi->id), true));
150
 
151
        // Filtering based on the grader.
152
        $this->assertEquals(3, $this->get_tablelog_results($c2ctx, array('grader' => $grader1->id), true));
153
        $this->assertEquals(1, $this->get_tablelog_results($c2ctx, array('grader' => $grader2->id), true));
154
 
155
        // Filtering based on date.
156
        $results = $this->get_tablelog_results($c1ctx, array('datefrom' => time() + 1800));
157
        $this->assertGradeHistoryIds(array($grades['c1m1u2']->id), $results);
158
        $results = $this->get_tablelog_results($c1ctx, array('datetill' => time() - 1800));
159
        $this->assertGradeHistoryIds(array($grades['c1m1u1']->id), $results);
160
        $results = $this->get_tablelog_results($c1ctx, array('datefrom' => time() - 1800, 'datetill' => time() + 1800));
161
        $this->assertGradeHistoryIds(array($grades['c1m1u3']->id, $grades['c1m1u4']->id, $grades['c1m1u5']->id,
162
            $grades['c1m2u1']->id, $grades['c1m2u2']->id, $grades['c1m3u1']->id), $results);
163
 
164
        // Filtering based on revised only.
165
        $this->assertEquals(3, $this->get_tablelog_results($c2ctx, array('userids' => $u5->id), true));
166
        $this->assertEquals(1, $this->get_tablelog_results($c2ctx, array('userids' => $u5->id, 'revisedonly' => true), true));
167
 
168
        // More filtering based on revised only.
169
        $gi = \grade_item::fetch($giparams + array('iteminstance' => $c2m2->id));
170
        $this->assertEquals(6, $this->get_tablelog_results($c2ctx, array('userids' => $u1->id, 'itemid' => $gi->id), true));
171
        $results = $this->get_tablelog_results($c2ctx, array('userids' => $u1->id, 'itemid' => $gi->id, 'revisedonly' => true));
172
        $this->assertGradeHistoryIds(array($grades['c2m2u1a']->id, $grades['c2m2u1c']->id, $grades['c2m2u1e']->id), $results);
173
 
174
        // Checking the value of the previous grade.
175
        $this->assertEquals(null, $results[$grades['c2m2u1a']->id]->prevgrade);
176
        $this->assertEquals($grades['c2m2u1a']->finalgrade, $results[$grades['c2m2u1c']->id]->prevgrade);
177
        $this->assertEquals($grades['c2m2u1c']->finalgrade, $results[$grades['c2m2u1e']->id]->prevgrade);
178
 
179
        // Put course in separate groups mode, add grader1 and two students to the same group.
180
        $c1->groupmode = SEPARATEGROUPS;
181
        update_course($c1);
182
        $this->assertFalse(has_capability('moodle/site:accessallgroups', \context_course::instance($c1->id)));
183
        $g1 = self::getDataGenerator()->create_group(['courseid' => $c1->id, 'name' => 'g1']);
184
        self::getDataGenerator()->create_group_member(['groupid' => $g1->id, 'userid' => $grader1->id]);
185
        self::getDataGenerator()->create_group_member(['groupid' => $g1->id, 'userid' => $u1->id]);
186
        self::getDataGenerator()->create_group_member(['groupid' => $g1->id, 'userid' => $u2->id]);
187
        $this->assertEquals(2, $this->get_tablelog_results($c1ctx, array(), true));
188
 
189
        // Grader2 is not in any groups.
190
        $this->setUser($grader2);
191
        $this->assertEquals(0, $this->get_tablelog_results($c1ctx, array(), true));
192
    }
193
 
194
    /**
195
     * Test the get users helper method.
196
     */
11 efrain 197
    public function test_get_users(): void {
1 efrain 198
        $this->resetAfterTest();
199
 
200
        // Making the setup.
201
        $c1 = $this->getDataGenerator()->create_course();
202
        $c2 = $this->getDataGenerator()->create_course();
203
        $c1ctx = \context_course::instance($c1->id);
204
        $c2ctx = \context_course::instance($c2->id);
205
 
206
        $c1m1 = $this->getDataGenerator()->create_module('assign', array('course' => $c1));
207
        $c2m1 = $this->getDataGenerator()->create_module('assign', array('course' => $c2));
208
 
209
        // Users.
210
        $u1 = $this->getDataGenerator()->create_user(array('firstname' => 'Eric', 'lastname' => 'Cartman'));
211
        $u2 = $this->getDataGenerator()->create_user(array('firstname' => 'Stan', 'lastname' => 'Marsh'));
212
        $u3 = $this->getDataGenerator()->create_user(array('firstname' => 'Kyle', 'lastname' => 'Broflovski'));
213
        $u4 = $this->getDataGenerator()->create_user(array('firstname' => 'Kenny', 'lastname' => 'McCormick'));
214
 
215
        // Creating grade history for some users.
216
        $gi = \grade_item::fetch(array('iteminstance' => $c1m1->id, 'itemtype' => 'mod', 'itemmodule' => 'assign'));
217
        $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id));
218
        $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u2->id));
219
        $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u3->id));
220
 
221
        $gi = \grade_item::fetch(array('iteminstance' => $c2m1->id, 'itemtype' => 'mod', 'itemmodule' => 'assign'));
222
        $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u4->id));
223
 
224
        // Checking fetching some users.
225
        $users = \gradereport_history\helper::get_users($c1ctx);
226
        $this->assertCount(3, $users);
227
        $this->assertArrayHasKey($u3->id, $users);
228
        $users = \gradereport_history\helper::get_users($c2ctx);
229
        $this->assertCount(1, $users);
230
        $this->assertArrayHasKey($u4->id, $users);
231
        $users = \gradereport_history\helper::get_users($c1ctx, 'c');
232
        $this->assertCount(1, $users);
233
        $this->assertArrayHasKey($u1->id, $users);
234
        $users = \gradereport_history\helper::get_users($c1ctx, '', 0, 2);
235
        $this->assertCount(2, $users);
236
        $this->assertArrayHasKey($u3->id, $users);
237
        $this->assertArrayHasKey($u1->id, $users);
238
        $users = \gradereport_history\helper::get_users($c1ctx, '', 1, 2);
239
        $this->assertCount(1, $users);
240
        $this->assertArrayHasKey($u2->id, $users);
241
 
242
        // Checking the count of users.
243
        $this->assertEquals(3, \gradereport_history\helper::get_users_count($c1ctx));
244
        $this->assertEquals(1, \gradereport_history\helper::get_users_count($c2ctx));
245
        $this->assertEquals(1, \gradereport_history\helper::get_users_count($c1ctx, 'c'));
246
    }
247
 
248
    /**
249
     * Data provider for \gradereport_history_report_testcase::test_get_users_with_profile_fields()
250
     * Testing get_users() and get_users_count() test cases.
251
     *
252
     * @return array List of data sets (test cases)
253
     */
11 efrain 254
    public static function get_users_with_profile_fields_provider(): array {
1 efrain 255
        return [
256
            // User identity check boxes, 'email', 'profile_field_lang' and 'profile_field_height' are checked.
257
                'show email,lang and height;search for all users' =>
258
                        ['email,profile_field_lang,profile_field_height', '', ['u1', 'u2', 'u3', 'u4']],
259
                'show email,lang and height;search for users on .org ' =>
260
                        ['email,profile_field_lang,profile_field_height', '.org', ['u1', 'u2', 'u4']],
261
                'show email,lang and height;search for users on .com ' =>
262
                        ['email,profile_field_lang,profile_field_height', '.com', []],
263
                'show email,lang and height;search for users on .uk ' =>
264
                        ['email,profile_field_lang,profile_field_height', '.uk', ['u3']],
265
                'show email,lang and height,search for Spanish speakers' =>
266
                        ['email,profile_field_lang,profile_field_height', 'spanish', ['u1', 'u4']],
11 efrain 267
                'show email,lang and height,search for Spanish speakers (using spa)' =>
1 efrain 268
                        ['email,profile_field_lang,profile_field_height', 'spa', ['u1', 'u4']],
269
                'show email,lang and height,search for German speakers' =>
270
                        ['email,profile_field_lang,profile_field_height', 'german', ['u2']],
11 efrain 271
                'show email,lang and height,search for German speakers (using ger)' =>
1 efrain 272
                        ['email,profile_field_lang,profile_field_height', 'ger', ['u2']],
273
                'show email,lang and height,search for English speakers' =>
274
                        ['email,profile_field_lang,profile_field_height', 'english', ['u3']],
11 efrain 275
                'show email,lang and height,search for English speakers (using eng)' =>
1 efrain 276
                        ['email,profile_field_lang,profile_field_height', 'eng', ['u3']],
277
                'show email,lang and height,search for users with height 180cm' =>
278
                        ['email,profile_field_lang,profile_field_height', '180', ['u2', 'u3', 'u4']],
279
                'show email,lang and height,search for users with height 170cm' =>
280
                        ['email,profile_field_lang,profile_field_height', '170', ['u1']],
281
 
282
            // User identity check boxes, 'email' and 'profile_field_height' are checked.
283
                'show email and height;search for users on .org' =>
284
                        ['email,profile_field_height', '.org', ['u1', 'u2', 'u4']],
285
                'show email and height;search for users on .com' =>
286
                        ['email,profile_field_height', '.com', []],
287
                'show email and height;search for users on .co' =>
288
                        ['email,profile_field_height', '.co', ['u3']],
11 efrain 289
                'show email and height,search for Spanish speakers' =>
1 efrain 290
                        ['email,profile_field_height', 'spanish', []],
11 efrain 291
                'show email and height,search for German speakers' =>
1 efrain 292
                        ['email,profile_field_height', 'german', []],
11 efrain 293
                'show email and height,search for English speakers' =>
1 efrain 294
                        ['email,profile_field_height', 'english', []],
295
                'show email and height,search for users with height 180cm' =>
296
                        ['email,profile_field_height', '180', ['u2', 'u3', 'u4']],
11 efrain 297
                'show email and height,search for users with height 170cm' =>
1 efrain 298
                        ['email,profile_field_height', '170', ['u1']],
299
 
300
            // User identity check boxes, only 'email' is checked.
301
                'show email only;search for users on .org' => ['email', '.org', ['u1', 'u2', 'u4']],
302
                'show email only;search for users on .com' => ['email', '.com', []],
303
                'show email only;search for users on .co.uk' => ['email', 'co.uk', ['u3']],
304
                'show email only;search for users on .uk' => ['email', '.uk', ['u3']],
305
                'show email only;search for users on .co' => ['email', '.co', ['u3']],
306
                'show email only;search for Spanish speakers' => ['email', 'spanish', []],
307
                'show email only;search for German speakers' => ['email', 'german', []],
308
                'show email only;search for English speakers' => ['email', 'english', []],
309
                'show email only;search for users with height 180cm' => ['email', '180', []],
310
                'show email only;search for users with height 170cm' => ['email', '170', []],
311
        ];
312
    }
313
 
314
    /**
315
     * Testing the search functionality on get_users() and get_users_count() and their inner methods.
316
     *
317
     * @dataProvider get_users_with_profile_fields_provider
318
     *
319
     * @param string $showuseridentity, list of user identities to be shown.
320
     * @param string $searchstring, the string to be searched.
321
     * @param array $expectedusernames, a list of expected usernames.
322
     * @return void
323
     */
324
    public function test_get_users_with_profile_fields(string $showuseridentity, string $searchstring,
325
            array $expectedusernames): void {
326
        global $CFG, $DB;
327
        require_once($CFG->dirroot . '/user/profile/lib.php');
328
        $this->resetAfterTest();
329
 
330
        // Create a couple of custom profile fields, which are in user identity.
331
        $generator = $this->getDataGenerator();
332
        $generator->create_custom_profile_field(['datatype' => 'text',
333
                'shortname' => 'lang', 'name' => 'Language']);
334
        $generator->create_custom_profile_field(['datatype' => 'text',
335
                'shortname' => 'height', 'name' => 'Height']);
336
 
337
        // Create a couple of test users.
338
        $u1 = $generator->create_user(['firstname' => 'Eduardo', 'lastname' => 'Gomes',
339
                'username' => 'u1', 'email' => 'u1@x.org', 'profile_field_lang' => 'Spanish',
340
                'profile_field_height' => '170cm']);
341
        $u2 = $generator->create_user(['firstname' => 'Dieter', 'lastname' => 'Schmitt',
342
                'username' => 'u2', 'email' => 'u2@x.org', 'profile_field_lang' => 'German',
343
                'profile_field_height' => '180cm']);
344
 
345
        $u3 = $generator->create_user(['firstname' => 'Peter', 'lastname' => 'Jones',
346
                'username' => 'u3', 'email' => 'u3@x.co.uk', 'profile_field_lang' => 'English',
347
                'profile_field_height' => '180cm']);
348
        $u4 = $generator->create_user(['firstname' => 'Pedro', 'lastname' => 'Gomes',
349
                'username' => 'u4', 'email' => 'u3@x.org', 'profile_field_lang' => 'Spanish',
350
                'profile_field_height' => '180cm']);
351
 
352
        // Do this as admin user.
353
        $this->setAdminUser();
354
 
355
        // Making the setup.
356
        $c1 = $this->getDataGenerator()->create_course();
357
        $c1ctx = \context_course::instance($c1->id);
358
        $c1m1 = $this->getDataGenerator()->create_module('assign', array('course' => $c1));
359
 
360
        // Creating grade history for some users.
361
        $gi = \grade_item::fetch(array('iteminstance' => $c1m1->id, 'itemtype' => 'mod', 'itemmodule' => 'assign'));
362
        $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id));
363
        $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u2->id));
364
        $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u3->id));
365
        $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u4->id));
366
 
367
        // Checking fetching some users with this config settings.
368
        set_config('showuseridentity', $showuseridentity);
369
        $numberofexpectedusers = count($expectedusernames);
370
        $users = \gradereport_history\helper::get_users($c1ctx, $searchstring);
371
        $userscount = \gradereport_history\helper::get_users_count($c1ctx, $searchstring);
372
        $this->assertEquals($numberofexpectedusers, $userscount);
373
        $this->assertCount($numberofexpectedusers, $users);
374
        foreach ($users as $user) {
375
            if (in_array($user->username, $expectedusernames)) {
376
                $this->assertArrayHasKey($user->id, $users);
377
            } else {
378
                $this->assertArrayNotHasKey($user->id, $users);
379
            }
380
        }
381
    }
382
 
383
    /**
384
     * Data provider method for \gradereport_history_report_testcase::test_get_users_with_groups()
385
     */
11 efrain 386
    public static function get_users_provider(): array {
1 efrain 387
        return [
388
            'Visible groups, non-editing teacher, not in any group' => [
389
                VISIBLEGROUPS, 'teacher', ['g1', 'g2'], ['s1', 's2', 's3', 's4', 's5']
390
            ],
391
            'Visible groups, non-editing teacher' => [
392
                VISIBLEGROUPS, 'teacher', [], ['s1', 's2', 's3', 's4', 's5']
393
            ],
394
            'Visible groups, editing teacher' => [
395
                VISIBLEGROUPS, 'editingteacher', ['g1', 'g2'], ['s1', 's2', 's3', 's4', 's5']
396
            ],
397
            'Separate groups, non-editing teacher' => [
398
                SEPARATEGROUPS, 'teacher', ['g1', 'g2'], ['s1', 's2']
399
            ],
400
            'Separate groups, non-editing teacher, not in any group' => [
401
                SEPARATEGROUPS, 'teacher', [], []
402
            ],
403
            'Separate groups, non-editing teacher and student share two groups' => [
404
                SEPARATEGROUPS, 'teacher', ['g4', 'g5'], ['s5']
405
            ],
406
            'Separate groups, editing teacher' => [
407
                SEPARATEGROUPS, 'editingteacher', ['g1', 'g2'], ['s1', 's2', 's3', 's4', 's5']
408
            ],
409
        ];
410
    }
411
 
412
    /**
413
     * Test for helper::get_users() with course group mode set.
414
     *
415
     * @dataProvider get_users_provider
416
     * @param $groupmode
417
     * @param $teacherrole
418
     * @param $teachergroups
419
     * @param $expectedusers
420
     */
11 efrain 421
    public function test_get_users_with_groups($groupmode, $teacherrole, $teachergroups, $expectedusers): void {
1 efrain 422
        global $DB;
423
        $this->resetAfterTest();
424
 
425
        $generator = $this->getDataGenerator();
426
 
427
        // Create a test course.
428
        $course = $generator->create_course(['groupmode' => $groupmode]);
429
 
430
        // Create an assignment module.
431
        $assign = $generator->create_module('assign', ['course' => $course]);
432
 
433
        // Fetch roles.
434
        $role = $DB->get_record('role', ['shortname' => $teacherrole], '*', MUST_EXIST);
435
        $studentrole =  $DB->get_record('role', ['shortname' => 'student'], '*', MUST_EXIST);
436
 
437
        // Create users.
438
        $t1 = $generator->create_user(['username' => 't1', 'email' => 't1@example.com']);
439
        $s1 = $generator->create_user(['username' => 's1', 'email' => 's1@example.com']);
440
        $s2 = $generator->create_user(['username' => 's2', 'email' => 's2@example.com']);
441
        $s3 = $generator->create_user(['username' => 's3', 'email' => 's3@example.com']);
442
        $s4 = $generator->create_user(['username' => 's4', 'email' => 's4@example.com']);
443
        $s5 = $generator->create_user(['username' => 's5', 'email' => 's5@example.com']);
444
 
445
        // Enrol users.
446
        $generator->enrol_user($t1->id, $course->id, $role->id);
447
        $generator->enrol_user($s1->id, $course->id, $studentrole->id);
448
        $generator->enrol_user($s2->id, $course->id, $studentrole->id);
449
        $generator->enrol_user($s3->id, $course->id, $studentrole->id);
450
        $generator->enrol_user($s4->id, $course->id, $studentrole->id);
451
        $generator->enrol_user($s5->id, $course->id, $studentrole->id);
452
 
453
        // Create groups.
454
        $groups = [];
455
        $groups['g1'] = $generator->create_group(['courseid' => $course->id, 'name' => 'g1']);
456
        $groups['g2'] = $generator->create_group(['courseid' => $course->id, 'name' => 'g2']);
457
        $groups['g3'] = $generator->create_group(['courseid' => $course->id, 'name' => 'g3']);
458
        $groups['g4'] = $generator->create_group(['courseid' => $course->id, 'name' => 'g4']);
459
        $groups['g5'] = $generator->create_group(['courseid' => $course->id, 'name' => 'g5']);
460
 
461
        // Add teacher to the assigned groups.
462
        foreach ($teachergroups as $groupname) {
463
            $group = $groups[$groupname];
464
            $generator->create_group_member(['groupid' => $group->id, 'userid' => $t1->id]);
465
        }
466
 
467
        // Add students to groups.
468
        $generator->create_group_member(['groupid' => $groups['g1']->id, 'userid' => $s1->id]);
469
        $generator->create_group_member(['groupid' => $groups['g2']->id, 'userid' => $s2->id]);
470
        $generator->create_group_member(['groupid' => $groups['g3']->id, 'userid' => $s3->id]);
471
        $generator->create_group_member(['groupid' => $groups['g4']->id, 'userid' => $s5->id]);
472
        $generator->create_group_member(['groupid' => $groups['g5']->id, 'userid' => $s5->id]);
473
 
474
        // Creating grade history for the students.
475
        $gi = \grade_item::fetch(['iteminstance' => $assign->id, 'itemtype' => 'mod', 'itemmodule' => 'assign']);
476
        $this->create_grade_history(['itemid' => $gi->id, 'userid' => $s1->id]);
477
        $this->create_grade_history(['itemid' => $gi->id, 'userid' => $s2->id]);
478
        $this->create_grade_history(['itemid' => $gi->id, 'userid' => $s3->id]);
479
        $this->create_grade_history(['itemid' => $gi->id, 'userid' => $s4->id]);
480
        $this->create_grade_history(['itemid' => $gi->id, 'userid' => $s5->id]);
481
 
482
        // Log in as the teacher.
483
        $this->setUser($t1);
484
 
485
        // Fetch the users.
486
        $users = \gradereport_history\helper::get_users(\context_course::instance($course->id));
487
        // Confirm that the number of users fetched is the same as the count of expected users.
488
        $this->assertCount(count($expectedusers), $users);
489
        foreach ($users as $user) {
490
            // Confirm that each user returned is in the list of expected users.
491
            $this->assertTrue(in_array($user->username, $expectedusers));
492
        }
493
    }
494
 
495
    /**
496
     * Test the get graders helper method.
497
     */
11 efrain 498
    public function test_graders(): void {
1 efrain 499
        $this->resetAfterTest();
500
 
501
        // Making the setup.
502
        $c1 = $this->getDataGenerator()->create_course();
503
        $c2 = $this->getDataGenerator()->create_course();
504
        $c3 = $this->getDataGenerator()->create_course(['groupmode' => SEPARATEGROUPS]);
505
 
506
        $c1m1 = $this->getDataGenerator()->create_module('assign', array('course' => $c1));
507
        $c2m1 = $this->getDataGenerator()->create_module('assign', array('course' => $c2));
508
        $c3m1 = $this->getDataGenerator()->create_module('assign', array('course' => $c3));
509
 
510
        // Users.
511
        $u1 = $this->getDataGenerator()->create_user(array('firstname' => 'Eric', 'lastname' => 'Cartman'));
512
        $u2 = $this->getDataGenerator()->create_user(array('firstname' => 'Stan', 'lastname' => 'Marsh'));
513
        $u3 = $this->getDataGenerator()->create_user(array('firstname' => 'Kyle', 'lastname' => 'Broflovski'));
514
        $u4 = $this->getDataGenerator()->create_user(array('firstname' => 'Kenny', 'lastname' => 'McCormick'));
515
 
516
        foreach ([$c1, $c2, $c3] as $course) {
517
            foreach ([$u1, $u2, $u3, $u4] as $user) {
518
                self::getDataGenerator()->enrol_user($user->id, $course->id, 'student');
519
            }
520
        }
521
 
522
        // Creating grade history for some users.
523
        $gi = \grade_item::fetch(array('iteminstance' => $c1m1->id, 'itemtype' => 'mod', 'itemmodule' => 'assign'));
524
        $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id, 'usermodified' => $u1->id));
525
        $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id, 'usermodified' => $u2->id));
526
        $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id, 'usermodified' => $u3->id));
527
 
528
        $gi = \grade_item::fetch(array('iteminstance' => $c2m1->id, 'itemtype' => 'mod', 'itemmodule' => 'assign'));
529
        $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id, 'usermodified' => $u4->id));
530
 
531
        $gi = \grade_item::fetch(array('iteminstance' => $c3m1->id, 'itemtype' => 'mod', 'itemmodule' => 'assign'));
532
        $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u1->id, 'usermodified' => $u1->id));
533
        $this->create_grade_history(array('itemid' => $gi->id, 'userid' => $u2->id, 'usermodified' => $u2->id));
534
 
535
        // Checking fetching some users.
536
        $graders = \gradereport_history\helper::get_graders($c1->id);
537
        $this->assertCount(4, $graders); // Including "all graders" .
538
        $this->assertArrayHasKey($u1->id, $graders);
539
        $this->assertArrayHasKey($u2->id, $graders);
540
        $this->assertArrayHasKey($u3->id, $graders);
541
        $graders = \gradereport_history\helper::get_graders($c2->id);
542
        $this->assertCount(2, $graders); // Including "all graders" .
543
        $this->assertArrayHasKey($u4->id, $graders);
544
 
545
        // Third course is in separate groups mode. Only graders from the same group will be returned.
546
        $g = self::getDataGenerator()->create_group(['courseid' => $course->id, 'name' => 'g1']);
547
        self::getDataGenerator()->create_group_member(['groupid' => $g->id, 'userid' => $u1->id]);
548
        self::getDataGenerator()->create_group_member(['groupid' => $g->id, 'userid' => $u2->id]);
549
        $this->setUser($u1);
550
        $graders = \gradereport_history\helper::get_graders($c3->id);
551
        $this->assertCount(3, $graders); // Including "all graders" .
552
        $this->setUser($u3);
553
        $graders = \gradereport_history\helper::get_graders($c3->id);
554
        $this->assertCount(1, $graders); // Including "all graders" .
555
    }
556
 
557
    /**
558
     * Asserts that the array of grade objects contains exactly the right IDs.
559
     *
560
     * @param array $expectedids Array of expected IDs.
561
     * @param array $objects Array of objects returned by the table.
562
     */
563
    protected function assertGradeHistoryIds(array $expectedids, array $objects) {
564
        $this->assertCount(count($expectedids), $objects);
565
        $expectedids = array_flip($expectedids);
566
        foreach ($objects as $object) {
567
            $this->assertArrayHasKey($object->id, $expectedids);
568
            unset($expectedids[$object->id]);
569
        }
570
        $this->assertCount(0, $expectedids);
571
    }
572
 
573
    /**
574
     * Create a new grade history entry.
575
     *
576
     * @param array $params Of values.
577
     * @return object The grade object.
578
     */
579
    protected function create_grade_history($params) {
580
        global $DB;
581
        $params = (array) $params;
582
 
583
        if (!isset($params['itemid'])) {
584
            throw new \coding_exception('Missing itemid key.');
585
        }
586
        if (!isset($params['userid'])) {
587
            throw new \coding_exception('Missing userid key.');
588
        }
589
 
590
        // Default object.
591
        $grade = new \stdClass();
592
        $grade->itemid = 0;
593
        $grade->userid = 0;
594
        $grade->oldid = 123;
595
        $grade->rawgrade = 50;
596
        $grade->finalgrade = 50;
597
        $grade->timecreated = time();
598
        $grade->timemodified = time();
599
        $grade->information = '';
600
        $grade->informationformat = FORMAT_PLAIN;
601
        $grade->feedback = '';
602
        $grade->feedbackformat = FORMAT_PLAIN;
603
        $grade->usermodified = 2;
604
 
605
        // Merge with data passed.
606
        $grade = (object) array_merge((array) $grade, $params);
607
 
608
        // Insert record.
609
        $grade->id = $DB->insert_record('grade_grades_history', $grade);
610
 
611
        return $grade;
612
    }
613
 
614
    /**
615
     * Returns a table log object.
616
     *
617
     * @param context_course $coursecontext The course context.
618
     * @param array $filters An array of filters.
619
     * @param boolean $count When true, returns a count rather than an array of objects.
620
     * @return mixed Count or array of objects.
621
     */
622
    protected function get_tablelog_results($coursecontext, $filters = array(), $count = false) {
623
        $table = new gradereport_history_tests_tablelog('something', $coursecontext, new \moodle_url(''), $filters);
624
        return $table->get_test_results($count);
625
    }
626
 
627
}
628
 
629
/**
630
 * Extended table log class.
631
 */
632
class gradereport_history_tests_tablelog extends \gradereport_history\output\tablelog {
633
 
634
    /**
635
     * Get the test results.
636
     *
637
     * @param boolean $count Whether or not we want the count.
638
     * @return mixed Count or array of objects.
639
     */
640
    public function get_test_results($count = false) {
641
        global $DB;
642
        if ($count) {
643
            list($sql, $params) = $this->get_sql_and_params(true);
644
            return $DB->count_records_sql($sql, $params);
645
        } else {
646
            $this->setup();
647
            list($sql, $params) = $this->get_sql_and_params();
648
            return $DB->get_records_sql($sql, $params);
649
        }
650
    }
651
 
652
}