Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of the Zoom plugin for 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
/**
18
 * Unit tests for get_meeting_reports task class.
19
 *
20
 * @package    mod_zoom
21
 * @copyright  2019 UC Regents
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace mod_zoom;
26
 
27
use advanced_testcase;
28
use context_course;
29
use html_writer;
30
use moodle_url;
31
use stdClass;
32
 
33
/**
34
 * PHPunit testcase class.
35
 * @covers \mod_zoom\task\get_meeting_reports
36
 */
37
final class get_meeting_reports_test extends advanced_testcase {
38
    /**
39
     * Scheduled task object.
40
     * @var \mod_zoom\task\get_meeting_reports
41
     */
42
    private $meetingtask;
43
 
44
    /**
45
     * Fake data to return for mocked get_meeting_participants() call.
46
     * @var array
47
     */
48
    private $mockparticipantsdata;
49
 
50
    /**
51
     * Fake data from get_meeting_participants().
52
     * @var object
53
     */
54
    private $zoomdata;
55
 
56
    /**
57
     * Mocks the mod_zoom\webservice->get_meeting_participants() call, so we
58
     * don't actually call the real Zoom API.
59
     *
60
     * @param string $meetinguuid The meeting or webinar's UUID.
61
     * @param bool $webinar Whether the meeting or webinar whose information you want is a webinar.
62
     * @return array    The specified meeting participants array for given meetinguuid.
63
     */
64
    public function mock_get_meeting_participants($meetinguuid, $webinar) {
65
        return $this->mockparticipantsdata[$meetinguuid] ?? null;
66
    }
67
 
68
    /**
69
     * Setup.
70
     */
71
    public function setUp(): void {
72
        $this->resetAfterTest(true);
73
 
74
        $this->meetingtask = new \mod_zoom\task\get_meeting_reports();
75
 
76
        $data = [
77
            'id' => 'ARANDOMSTRINGFORUUID',
78
            'user_id' => 123456789,
79
            'name' => 'SMITH, JOE',
80
            'user_email' => 'joe@test.com',
81
            'join_time' => '2019-01-01T00:00:00Z',
82
            'leave_time' => '2019-01-01T00:01:00Z',
83
            'duration' => 60,
84
        ];
85
        $this->zoomdata = (object) $data;
86
    }
87
 
88
    /**
89
     * Make sure that format_participant() filters bad data from Zoom.
90
     */
91
    public function test_format_participant_filtering(): void {
92
        // Sometimes Zoom has a # instead of comma in the name.
93
        $this->zoomdata->name = 'SMITH# JOE';
94
        $participant = $this->meetingtask->format_participant($this->zoomdata, 1, [], []);
95
        $this->assertEquals('SMITH, JOE', $participant['name']);
96
    }
97
 
98
    /**
99
     * Make sure that format_participant() can match Moodle users.
100
     */
101
    public function test_format_participant_matching(): void {
102
        global $DB;
103
        return;
104
 
105
        // 1) If user does not match, verify that we are using data from Zoom.
106
        $participant = $this->meetingtask->format_participant($this->zoomdata, 1, [], []);
107
        $this->assertEquals($this->zoomdata->name, $participant['name']);
108
        $this->assertEquals($this->zoomdata->user_email, $participant['user_email']);
109
        $this->assertNull($participant['userid']);
110
 
111
        // 2) Try to match view via system email.
112
 
113
        // Add user's email to Moodle system.
114
        $user = $this->getDataGenerator()->create_user(
115
            ['email' => $this->zoomdata->user_email]
116
        );
117
 
118
        $participant = $this->meetingtask->format_participant($this->zoomdata, 1, [], []);
119
        $this->assertEquals($user->id, $participant['userid']);
120
        $this->assertEquals(strtoupper(fullname($user)), $participant['name']);
121
        $this->assertEquals($user->email, $participant['user_email']);
122
 
123
        // 3) Try to match view via enrolled name.
124
 
125
        // Change user's name to make sure we are matching on name.
126
        $user->firstname = 'Firstname';
127
        $user->lastname = 'Lastname';
128
        $DB->update_record('user', $user);
129
        // Set to blank so previous test does not trigger.
130
        $this->zoomdata->user_email = '';
131
 
132
        // Create course and enroll user.
133
        $course = $this->getDataGenerator()->create_course();
134
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
135
        [$names, $emails] = $this->meetingtask->get_enrollments($course->id);
136
 
137
        // Before Zoom data is changed, should return nothing.
138
        $participant = $this->meetingtask->format_participant($this->zoomdata, 1, $names, $emails);
139
        $this->assertNull($participant['userid']);
140
 
141
        // Change Zoom data and now user should be found.
142
        $this->zoomdata->name = strtoupper(fullname($user));
143
        $participant = $this->meetingtask->format_participant($this->zoomdata, 1, $names, $emails);
144
        $this->assertEquals($user->id, $participant['userid']);
145
        $this->assertEquals($names[$participant['userid']], $participant['name']);
146
        // Email should match what Zoom gives us.
147
        $this->assertEquals($this->zoomdata->user_email, $participant['user_email']);
148
 
149
        // 4) Try to match view via enrolled email.
150
 
151
        // Change user's email to make sure we are matching on email.
152
        $user->email = 'smith@test.com';
153
        $DB->update_record('user', $user);
154
        // Change name so previous test does not trigger.
155
        $this->zoomdata->name = 'Something Else';
156
        // Since email changed, update enrolled user data.
157
        [$names, $emails] = $this->meetingtask->get_enrollments($course->id);
158
 
159
        // Before Zoom data is changed, should return nothing.
160
        $participant = $this->meetingtask->format_participant($this->zoomdata, 1, $names, $emails);
161
        $this->assertNull($participant['userid']);
162
 
163
        // Change Zoom data and now user should be found.
164
        $this->zoomdata->user_email = $user->email;
165
        $participant = $this->meetingtask->format_participant($this->zoomdata, 1, $names, $emails);
166
        $this->assertEquals($user->id, $participant['userid']);
167
        $this->assertEquals($names[$participant['userid']], $participant['name']);
168
        // Email should match what Zoom gives us.
169
        $this->assertEquals($this->zoomdata->user_email, $participant['user_email']);
170
 
171
        // 5) Try to match user via id (uuid).
172
 
173
        // Insert previously generated $participant data, but with UUID set.
174
        $participant['uuid'] = $this->zoomdata->id;
175
        // Set userid to a given value so we know we got a match.
176
        $participant['userid'] = 999;
177
        $recordid = $DB->insert_record('zoom_meeting_participants', $participant);
178
 
179
        // Should return the found entry in zoom_meeting_participants.
180
        $newparticipant = $this->meetingtask->format_participant($this->zoomdata, 1, $names, $emails);
181
        $this->assertEquals($participant['uuid'], $newparticipant['uuid']);
182
        $this->assertEquals(999, $newparticipant['userid']);
183
        $this->assertEquals($participant['name'], $newparticipant['name']);
184
        // Email should match what Zoom gives us.
185
        $this->assertEquals($this->zoomdata->user_email, $newparticipant['user_email']);
186
    }
187
 
188
    /**
189
     * Make sure that format_participant() can match Moodle users more
190
     * aggressively on name.
191
     */
192
    public function test_format_participant_name_matching(): void {
193
        // Enroll a bunch of users. Note: names were generated by
194
        // https://www.behindthename.com/random/ and any similarity to anyone
195
        // real or ficitional is concidence and not intentional.
196
        $users[0] = $this->getDataGenerator()->create_user([
197
            'lastname' => 'VAN ANTWERPEN',
198
            'firstname' => 'LORETO ZAHIRA',
199
        ]);
200
        $users[1] = $this->getDataGenerator()->create_user([
201
            'lastname' => 'POWER',
202
            'firstname' => 'TEIMURAZI ELLI',
203
        ]);
204
        $users[2] = $this->getDataGenerator()->create_user([
205
            'lastname' => 'LITTLE',
206
            'firstname' => 'BASEMATH ALIZA',
207
        ]);
208
        $users[3] = $this->getDataGenerator()->create_user([
209
            'lastname' => 'MUTTON',
210
            'firstname' => 'RADOVAN BRIANNA',
211
        ]);
212
        $users[4] = $this->getDataGenerator()->create_user([
213
            'lastname' => 'MUTTON',
214
            'firstname' => 'BRUNO EVGENIJA',
215
        ]);
216
        $course = $this->getDataGenerator()->create_course();
217
        foreach ($users as $user) {
218
            $this->getDataGenerator()->enrol_user($user->id, $course->id);
219
        }
220
 
221
        [$names, $emails] = $this->meetingtask->get_enrollments($course->id);
222
 
223
        // 1) Make sure we match someone with middle name missing.
224
        $users[0]->firstname = 'LORETO';
225
        $this->zoomdata->name = fullname($users[0]);
226
        $participant = $this->meetingtask->format_participant($this->zoomdata, 1, $names, $emails);
227
        $this->assertEquals($users[0]->id, $participant['userid']);
228
 
229
        // 2) Make sure that name matches even if there are no spaces.
230
        $users[1]->firstname = str_replace(' ', '', $users[1]->firstname);
231
        $this->zoomdata->name = fullname($users[1]);
232
        $participant = $this->meetingtask->format_participant($this->zoomdata, 1, $names, $emails);
233
        $this->assertEquals($users[1]->id, $participant['userid']);
234
 
235
        // 3) Make sure that name matches even if we have different ordering.
236
        $this->zoomdata->name = 'MUTTON, RADOVAN BRIANNA';
237
        $participant = $this->meetingtask->format_participant($this->zoomdata, 1, $names, $emails);
238
        $this->assertEquals($users[3]->id, $participant['userid']);
239
 
240
        // 4) Make sure we do not match users if just last name is the same.
241
        $users[2]->firstname = 'JOSH';
242
        $this->zoomdata->name = fullname($users[2]);
243
        $participant = $this->meetingtask->format_participant($this->zoomdata, 1, $names, $emails);
244
        $this->assertEmpty($participant['userid']);
245
 
246
        // 5) Make sure we do not match users if name is not similar to anything.
247
        $users[4]->firstname = 'JOSH';
248
        $users[4]->lastname = 'SMITH';
249
        $this->zoomdata->name = fullname($users[4]);
250
        $participant = $this->meetingtask->format_participant($this->zoomdata, 1, $names, $emails);
251
        $this->assertEmpty($participant['userid']);
252
    }
253
 
254
    /**
255
     * Tests that we can handle when the Zoom API sometimes returns invalid
256
     * userids in the report/meeting/participants call.
257
     */
258
    public function test_invalid_userids(): void {
259
        global $DB, $SITE;
260
 
261
        // Make sure we start with nothing.
262
        $this->assertEquals(0, $DB->count_records('zoom_meeting_details'));
263
        $this->assertEquals(0, $DB->count_records('zoom_meeting_participants'));
264
        $this->mockparticipantsdata = [];
265
 
266
        // First mock the webservice object, so we can inject the return values
267
        // for get_meeting_participants.
268
        $mockwwebservice = $this->createMock('\mod_zoom\webservice');
269
 
270
        // What we want get_meeting_participants to return.
271
        $participant1 = new stdClass();
272
        // Sometimes Zoom returns timestamps appended to user_ids.
273
        $participant1->id = '';
274
        $participant1->user_id = '02020-04-01 15:02:01:040';
275
        $participant1->name = 'John Smith';
276
        $participant1->user_email = 'john@test.com';
277
        $participant1->join_time = '2020-04-01T15:02:01Z';
278
        $participant1->leave_time = '2020-04-01T15:02:01Z';
279
        $participant1->duration = 0;
280
        $this->mockparticipantsdata['someuuid'][] = $participant1;
281
        // Have another participant with normal data.
282
        $participant2 = new stdClass();
283
        $participant2->id = '';
284
        $participant2->user_id = 123;
285
        $participant2->name = 'Jane Smith';
286
        $participant2->user_email = 'jane@test.com';
287
        $participant2->join_time = '2020-04-01T15:00:00Z';
288
        $participant2->leave_time = '2020-04-01T15:10:00Z';
289
        $participant2->duration = 10;
290
        $this->mockparticipantsdata['someuuid'][] = $participant2;
291
 
292
        // Make get_meeting_participants() return our results array.
293
        $mockwwebservice->method('get_meeting_participants')
294
            ->will($this->returnCallback([$this, 'mock_get_meeting_participants']));
295
 
296
        $this->assertEquals(
297
            $this->mockparticipantsdata['someuuid'],
298
            $mockwwebservice->get_meeting_participants('someuuid', false)
299
        );
300
 
301
        // Now fake the meeting details.
302
        $meeting = new stdClass();
303
        $meeting->id = 12345;
304
        $meeting->topic = 'Some meeting';
305
        $meeting->start_time = '2020-04-01T15:00:00Z';
306
        $meeting->end_time = '2020-04-01T16:00:00Z';
307
        $meeting->uuid = 'someuuid';
308
        $meeting->duration = 60;
309
        $meeting->participants = 3;
310
 
311
        // Insert stub data for zoom table.
312
        $DB->insert_record('zoom', [
313
            'course' => $SITE->id,
314
            'meeting_id' => $meeting->id,
315
            'name' => 'Zoom',
316
            'exists_on_zoom' => ZOOM_MEETING_EXISTS,
317
        ]);
318
 
319
        // Run task process_meeting_reports() and should insert participants.
320
        $this->meetingtask->service = $mockwwebservice;
321
        $meeting = $this->meetingtask->normalize_meeting($meeting);
322
        $this->assertTrue($this->meetingtask->process_meeting_reports($meeting));
323
 
324
        // Make sure that only one details is added and two participants.
325
        $this->assertEquals(1, $DB->count_records('zoom_meeting_details'));
326
        $this->assertEquals(2, $DB->count_records('zoom_meeting_participants'));
327
 
328
        // Add in one more participant, make sure we update details and added
329
        // one more participant.
330
        $participant3 = new stdClass();
331
        $participant3->id = 'someuseruuid';
332
        $participant3->user_id = 234;
333
        $participant3->name = 'Joe Smith';
334
        $participant3->user_email = 'joe@test.com';
335
        $participant3->join_time = '2020-04-01T15:05:00Z';
336
        $participant3->leave_time = '2020-04-01T15:35:00Z';
337
        $participant3->duration = 30;
338
        $this->mockparticipantsdata['someuuid'][] = $participant3;
339
        $this->assertTrue($this->meetingtask->process_meeting_reports($meeting));
340
        $this->assertEquals(1, $DB->count_records('zoom_meeting_details'));
341
        $this->assertEquals(3, $DB->count_records('zoom_meeting_participants'));
342
    }
343
 
344
    /**
345
     * Tests that normalize_meeting() can handle different meeting records from
346
     * Dashboard API versus the Report API.
347
     */
348
    public function test_normalize_meeting(): void {
349
        $dashboardmeeting = [
350
            'uuid' => 'sfsdfsdfc6122222d',
351
            'id' => 1000000,
352
            'topic' => 'Awesome meeting',
353
            'host' => 'John Doe',
354
            'email' => 'test@email.com',
355
            'user_type' => 2,
356
            'start_time' => '2019-07-14T09:05:19.754Z',
357
            'end_time' => '2019-08-14T09:05:19.754Z',
358
            'duration' => '01:21:18',
359
            'participants' => 4,
360
            'has_pstn' => false,
361
            'has_voip' => false,
362
            'has_3rd_party_audio' => false,
363
            'has_video' => false,
364
            'has_screen_share' => false,
365
            'has_recording' => false,
366
            'has_sip' => false,
367
        ];
368
        $meeting = $this->meetingtask->normalize_meeting((object) $dashboardmeeting);
369
 
370
        $this->assertEquals($dashboardmeeting['uuid'], $meeting->uuid);
371
        $this->assertFalse(isset($meeting->id));
372
        $this->assertEquals($dashboardmeeting['id'], $meeting->meeting_id);
373
        $this->assertEquals($dashboardmeeting['topic'], $meeting->topic);
374
        $this->assertIsInt($meeting->start_time);
375
        $this->assertIsInt($meeting->end_time);
376
        $this->assertEquals($meeting->duration, 81);
377
        $this->assertEquals($dashboardmeeting['participants'], $meeting->participants_count);
378
        $this->assertNull($meeting->total_minutes);
379
 
380
        // Try duration under an hour.
381
        $dashboardmeeting['duration'] = '10:01';
382
        $meeting = $this->meetingtask->normalize_meeting((object) $dashboardmeeting);
383
        $this->assertEquals($meeting->duration, 10);
384
 
385
        $reportmeeting = [
386
            'uuid' => 'sfsdfsdfc6122222d',
387
            'id' => 1000000,
388
            'type' => 2,
389
            'topic' => 'Awesome meeting',
390
            'user_name' => 'John Doe',
391
            'user_email' => 'test@email.com',
392
            'start_time' => '2019-07-14T09:05:19.754Z',
393
            'end_time' => '2019-08-14T09:05:19.754Z',
394
            'duration' => 11,
395
            'total_minutes' => 11,
396
            'participants_count' => 4,
397
        ];
398
 
399
        $meeting = $this->meetingtask->normalize_meeting((object) $reportmeeting);
400
 
401
        $this->assertEquals($reportmeeting['uuid'], $meeting->uuid);
402
        $this->assertFalse(isset($meeting->id));
403
        $this->assertEquals($reportmeeting['id'], $meeting->meeting_id);
404
        $this->assertEquals($reportmeeting['topic'], $meeting->topic);
405
        $this->assertIsInt($meeting->start_time);
406
        $this->assertIsInt($meeting->end_time);
407
        $this->assertEquals($reportmeeting['participants_count'], $meeting->participants_count);
408
        $this->assertEquals($reportmeeting['total_minutes'], $meeting->total_minutes);
409
    }
410
 
411
    /**
412
     * Testing the grading method according to users duration in a meeting.
413
     * @return void
414
     */
415
    public function test_grading_method(): void {
416
        global $DB;
417
        $this->setAdminUser();
418
        // Make sure we start with nothing.
419
        // Deleting all records from previous tests.
420
        if ($DB->count_records('zoom_meeting_details') > 0) {
421
            $DB->delete_records('zoom_meeting_details');
422
        }
423
 
424
        if ($DB->count_records('zoom_meeting_participants') > 0) {
425
            $DB->delete_records('zoom_meeting_participants');
426
        }
427
 
428
        // Generate fake course.
429
        $course = $this->getDataGenerator()->create_course();
430
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
431
 
432
        // Check that this teacher has the required capability to receive notification.
433
        $context = context_course::instance($course->id);
434
        $graders = get_users_by_capability($context, 'moodle/grade:edit');
435
        $this->assertEquals(1, count($graders));
436
        $firstkey = key($graders);
437
        $this->assertEquals($graders[$firstkey]->id, $teacher->id);
438
        // Now fake the meeting details.
439
        $meeting = new stdClass();
440
        $meeting->id = 456123;
441
        $meeting->topic = 'Some meeting';
442
        $meeting->start_time = '2020-04-01T15:00:00Z';
443
        $meeting->end_time = '2020-04-01T17:00:00Z';
444
        $meeting->uuid = 'someuuid123';
445
        $meeting->duration = 120; // In minutes.
446
        $meeting->participants = 4;
447
 
448
        // Create a new zoom instance.
449
        $params = [
450
            'course' => $course->id,
451
            'meeting_id' => $meeting->id,
452
            'grade' => 60,
453
            'name' => 'Zoom',
454
            'exists_on_zoom' => ZOOM_MEETING_EXISTS,
455
            'start_time' => strtotime('2020-04-01T15:00:00Z'),
456
            'duration' => 120 * 60, // In seconds.
457
        ];
458
 
459
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_zoom');
460
        $instance = $generator->create_instance($params);
461
        $id = $instance->id;
462
        // Normalize the meeting.
463
        $meeting = $this->meetingtask->normalize_meeting($meeting);
464
        $meeting->zoomid = $id;
465
 
466
        $detailsid = $DB->insert_record('zoom_meeting_details', $meeting);
467
 
468
        $zoomrecord = $DB->get_record('zoom', ['id' => $id]);
469
        // Create users and corresponding meeting participants.
470
        $rawparticipants = [];
471
        $participants = [];
472
        // Enroll a bunch of users. Note: names were generated by
473
        // https://www.behindthename.com/random/ and any similarity to anyone
474
        // real or fictional is coincidence and not intentional.
475
        $users[0] = $this->getDataGenerator()->create_user([
476
            'lastname' => 'Arytis',
477
            'firstname' => 'Oitaa',
478
        ]);
479
 
480
        $users[1] = $this->getDataGenerator()->create_user([
481
            'lastname' => 'Chouxuong',
482
            'firstname' => 'Khah',
483
        ]);
484
        $users[2] = $this->getDataGenerator()->create_user([
485
            'lastname' => 'Spialdiouniem',
486
            'firstname' => 'Basem',
487
        ]);
488
        $users[3] = $this->getDataGenerator()->create_user([
489
            'lastname' => 'Padhzinnuj',
490
            'firstname' => 'Nibba',
491
        ]);
492
        $users[4] = $this->getDataGenerator()->create_user([
493
            'lastname' => 'Apea',
494
            'firstname' => 'Ziqit',
495
        ]);
496
 
497
        foreach ($users as $user) {
498
            $this->getDataGenerator()->enrol_user($user->id, $course->id);
499
        }
500
        [$names, $emails] = $this->meetingtask->get_enrollments($course->id);
501
 
502
        // Create a participant with 5 min overlap.
503
        // Total time 35 min, total grade 17.5 .
504
        $rawparticipants[1] = (object) [
505
            'id' => 32132165,
506
            'user_id' => 4456,
507
            'name' => 'Oitaa Arytis',
508
            'user_email' => '',
509
            'join_time' => '2023-05-01T15:05:00Z',
510
            'leave_time' => '2023-05-01T15:35:00Z',
511
            'duration' => 30 * 60,
512
        ];
513
        $participants[1] = (object) $this->meetingtask->format_participant($rawparticipants[1], $detailsid, $names, $emails);
514
        $rawparticipants[2] = (object) [
515
            'id' => 32132165,
516
            'user_id' => 4456,
517
            'name' => 'Oitaa Arytis',
518
            'user_email' => '',
519
            'join_time' => '2023-05-01T15:30:00Z',
520
            'leave_time' => '2023-05-01T15:40:00Z',
521
            'duration' => 10 * 60,
522
        ];
523
        $participants[2] = (object) $this->meetingtask->format_participant($rawparticipants[2], $detailsid, $names, $emails);
524
        $overlap = $this->meetingtask->get_participant_overlap_time($participants[1], $participants[2]);
525
        $this->assertEquals(5 * 60, $overlap);
526
        // Also check for the same result if the data inverted.
527
        $overlap = $this->meetingtask->get_participant_overlap_time($participants[2], $participants[1]);
528
        $this->assertEquals(5 * 60, $overlap);
529
 
530
        // Create a participant with 30 min overlap.
531
        // Total duration 60 min. expect a mark of 30 .
532
        $rawparticipants[3] = (object) [
533
            'id' => '',
534
            'user_id' => 1234,
535
            'name' => 'Chouxuong Khah',
536
            'user_email' => '',
537
            'join_time' => '2023-05-01T15:00:00Z',
538
            'leave_time' => '2023-05-01T16:00:00Z',
539
            'duration' => 60 * 60,
540
        ];
541
        $participants[3] = (object) $this->meetingtask->format_participant($rawparticipants[3], $detailsid, $names, $emails);
542
        $rawparticipants[4] = (object) [
543
            'id' => '',
544
            'user_id' => 1234,
545
            'name' => 'Chouxuong Khah',
546
            'user_email' => '',
547
            'join_time' => '2023-05-01T15:30:00Z',
548
            'leave_time' => '2023-05-01T16:00:00Z',
549
            'duration' => 30 * 60,
550
        ];
551
        $participants[4] = (object) $this->meetingtask->format_participant($rawparticipants[4], $detailsid, $names, $emails);
552
        $overlap = $this->meetingtask->get_participant_overlap_time($participants[3], $participants[4]);
553
        $this->assertEquals(30 * 60, $overlap);
554
        // Also check for the same result if the data inverted.
555
        $overlap = $this->meetingtask->get_participant_overlap_time($participants[4], $participants[3]);
556
        $this->assertEquals(30 * 60, $overlap);
557
 
558
        // Another user with no overlaping.
559
        // Total duration 60 min. Expect mark 30 .
560
        $rawparticipants[5] = (object) [
561
            'id' => '',
562
            'user_id' => 564312,
563
            'name' => 'Spialdiouniem Basem',
564
            'user_email' => '',
565
            'join_time' => '2023-05-01T15:10:00Z',
566
            'leave_time' => '2023-05-01T16:00:00Z',
567
            'duration' => 50 * 60,
568
        ];
569
        $participants[5] = (object) $this->meetingtask->format_participant($rawparticipants[5], $detailsid, $names, $emails);
570
        $rawparticipants[6] = (object) [
571
            'id' => '',
572
            'user_id' => 564312,
573
            'name' => 'Spialdiouniem Basem',
574
            'user_email' => '',
575
            'join_time' => '2023-05-01T16:30:00Z',
576
            'leave_time' => '2023-05-01T16:40:00Z',
577
            'duration' => 10 * 60,
578
        ];
579
        $participants[6] = (object) $this->meetingtask->format_participant($rawparticipants[6], $detailsid, $names, $emails);
580
 
581
        $overlap = $this->meetingtask->get_participant_overlap_time($participants[5], $participants[6]);
582
        $this->assertEquals(0, $overlap);
583
        // Also check for the same result if the data inverted.
584
        $overlap = $this->meetingtask->get_participant_overlap_time($participants[6], $participants[5]);
585
        $this->assertEquals(0, $overlap);
586
 
587
        // Adding another participant.
588
        // Total duration 90 min, expect mark 45 .
589
        $rawparticipants[7] = (object) [
590
            'id' => '',
591
            'user_id' => 789453,
592
            'name' => 'Padhzinnuj Nibba',
593
            'user_email' => '',
594
            'join_time' => '2023-05-01T15:30:00Z',
595
            'leave_time' => '2023-05-01T17:00:00Z',
596
            'duration' => 90 * 60,
597
        ];
598
 
599
        // Adding a participant at which matching names will fail.
600
        // His duration is 110 min, this grant him a grade of 55.
601
        $rawparticipants[8] = (object) [
602
            'id' => '',
603
            'user_id' => 168452,
604
            'name' => 'Farouk',
605
            'user_email' => '',
606
            'join_time' => '2023-05-01T15:10:00Z',
607
            'leave_time' => '2023-05-01T17:00:00Z',
608
            'duration' => 110 * 60,
609
        ];
610
        $this->mockparticipantsdata['someuuid123'] = $rawparticipants;
611
        // First mock the webservice object, so we can inject the return values
612
        // for get_meeting_participants.
613
        $mockwwebservice = $this->createMock('\mod_zoom\webservice');
614
        $this->meetingtask->service = $mockwwebservice;
615
        // Make get_meeting_participants() return our results array.
616
        $mockwwebservice->method('get_meeting_participants')
617
            ->will($this->returnCallback([$this, 'mock_get_meeting_participants']));
618
 
619
        $this->assertEquals(
620
            $this->mockparticipantsdata['someuuid123'],
621
            $mockwwebservice->get_meeting_participants('someuuid123', false)
622
        );
623
 
624
        // Now let's test the grades.
625
        $DB->set_field('zoom', 'grading_method', 'period', ['id' => $zoomrecord->id]);
626
 
627
        // Prepare messages.
628
        $this->preventResetByRollback(); // Messaging does not like transactions...
629
        $sink = $this->redirectMessages();
630
        // Process meeting reports should call the function grading_participant_upon_duration
631
        // and insert grades.
632
        $this->assertTrue($this->meetingtask->process_meeting_reports($meeting));
633
        $this->assertEquals(1, $DB->count_records('zoom_meeting_details'));
634
        $this->assertEquals(8, $DB->count_records('zoom_meeting_participants'));
635
 
636
        $usersids = [];
637
        foreach ($users as $user) {
638
            $usersids[] = $user->id;
639
        }
640
        // Get the gradelist for all users created.
641
        $gradelist = grade_get_grades($course->id, 'mod', 'zoom', $zoomrecord->id, $usersids);
642
 
643
        $gradelistitems = $gradelist->items;
644
        $grades = $gradelistitems[0]->grades;
645
        // Check grades of first user.
646
        $grade = $grades[$users[0]->id]->grade;
647
        $this->assertEquals(17.5, $grade);
648
        // Check grades of second user.
649
        $grade = $grades[$users[1]->id]->grade;
650
        $this->assertEquals(30, $grade);
651
        // Check grades of third user.
652
        $grade = $grades[$users[2]->id]->grade;
653
        $this->assertEquals(30, $grade);
654
        // Check grades for fourth user.
655
        $grade = $grades[$users[3]->id]->grade;
656
        $this->assertEquals(45, $grade);
657
        // This user didn't enter the meeting.
658
        $grade = $grades[$users[4]->id]->grade;
659
        $this->assertEquals(null, $grade);
660
        // Let's check the teacher notification if it is ok?
661
        $messages = $sink->get_messages();
662
        // Only one teacher, means only one message.
663
        $this->assertEquals(1, count($messages));
664
        // Verify that it has been sent to the teacher.
665
        $this->assertEquals($teacher->id, $messages[0]->useridto);
666
        // Check the content of the message.
667
        // Grading item url.
668
        $gurl = new moodle_url(
669
            '/grade/report/singleview/index.php',
670
            [
671
                'id' => $course->id,
672
                'item' => 'grade',
673
                'itemid' => $gradelistitems[0]->id,
674
            ]
675
        );
676
        $gradeurl = html_writer::link($gurl, get_string('gradinglink', 'mod_zoom'));
677
 
678
        // Zoom instance url.
679
        $zurl = new moodle_url('/mod/zoom/view.php', ['id' => $id]);
680
        $zoomurl = html_writer::link($zurl, $zoomrecord->name);
681
        // The user need grading.
682
        $needgradestr = get_string('grading_needgrade', 'mod_zoom');
683
        $needgrade[] = '(Name: Farouk, grade: 55)';
684
        $needgrade = $needgradestr . implode('<br>', $needgrade) . "\n";
685
 
686
        $a = (object) [
687
            'name' => $zoomrecord->name,
688
            'graded' => 4,
689
            'alreadygraded' => 0,
690
            'needgrade' => $needgrade,
691
            'number' => 1,
692
            'gradeurl' => $gradeurl,
693
            'zoomurl' => $zoomurl,
694
            'notfound' => '',
695
            'notenrolled' => '',
696
        ];
697
        $messagecontent = get_string('gradingmessagebody', 'mod_zoom', $a);
698
        $this->assertStringContainsString($messagecontent, $messages[0]->fullmessage);
699
 
700
        // Redo the process again to be sure that no grades have been changed.
701
        $this->assertTrue($this->meetingtask->process_meeting_reports($meeting));
702
        $this->assertEquals(1, $DB->count_records('zoom_meeting_details'));
703
        $this->assertEquals(8, $DB->count_records('zoom_meeting_participants'));
704
        $gradelist = grade_get_grades($course->id, 'mod', 'zoom', $zoomrecord->id, $usersids);
705
        $gradelistitems = $gradelist->items;
706
        $grades = $gradelistitems[0]->grades;
707
        // Check grade for first user.
708
        $grade = $grades[$users[0]->id]->grade;
709
        $this->assertEquals(17.5, $grade);
710
        // Check grade for second user.
711
        $grade = $grades[$users[1]->id]->grade;
712
        $this->assertEquals(30, $grade);
713
        // Check grade for third user.
714
        $grade = $grades[$users[2]->id]->grade;
715
        $this->assertEquals(30, $grade);
716
        // Check grade for fourth user.
717
        $grade = $grades[$users[3]->id]->grade;
718
        $this->assertEquals(45, $grade);
719
        // This user didn't enter the meeting.
720
        $grade = $grades[$users[4]->id]->grade;
721
        $this->assertEquals(null, $grade);
722
 
723
        // Let's check if the teacher notification is ok.
724
        $messages = $sink->get_messages();
725
        // No new messages as there has not been an update for participants.
726
        $this->assertEquals(1, count($messages));
727
    }
728
}