mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
externallib_test.php
742 lines (612 loc) · 31 KB
/
externallib_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* SCORM module external functions tests
*
* @package mod_scorm
* @category external
* @copyright 2015 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.0
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
require_once($CFG->dirroot . '/mod/scorm/lib.php');
/**
* SCORM module external functions tests
*
* @package mod_scorm
* @category external
* @copyright 2015 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.0
*/
class mod_scorm_external_testcase extends externallib_advanced_testcase {
/**
* Set up for every test
*/
public function setUp() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Setup test data.
$this->course = $this->getDataGenerator()->create_course();
$this->scorm = $this->getDataGenerator()->create_module('scorm', array('course' => $this->course->id));
$this->context = context_module::instance($this->scorm->cmid);
$this->cm = get_coursemodule_from_instance('scorm', $this->scorm->id);
// Create users.
$this->student = self::getDataGenerator()->create_user();
$this->teacher = self::getDataGenerator()->create_user();
// Users enrolments.
$this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
$this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual');
}
/**
* Test view_scorm
*/
public function test_view_scorm() {
global $DB;
// Test invalid instance id.
try {
mod_scorm_external::view_scorm(0);
$this->fail('Exception expected due to invalid mod_scorm instance id.');
} catch (moodle_exception $e) {
$this->assertEquals('invalidrecord', $e->errorcode);
}
// Test not-enrolled user.
$user = self::getDataGenerator()->create_user();
$this->setUser($user);
try {
mod_scorm_external::view_scorm($this->scorm->id);
$this->fail('Exception expected due to not enrolled user.');
} catch (moodle_exception $e) {
$this->assertEquals('requireloginerror', $e->errorcode);
}
// Test user with full capabilities.
$this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($user->id, $this->course->id, $this->studentrole->id);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$result = mod_scorm_external::view_scorm($this->scorm->id);
$result = external_api::clean_returnvalue(mod_scorm_external::view_scorm_returns(), $result);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = array_shift($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_scorm\event\course_module_viewed', $event);
$this->assertEquals($this->context, $event->get_context());
$moodleurl = new \moodle_url('/mod/scorm/view.php', array('id' => $this->cm->id));
$this->assertEquals($moodleurl, $event->get_url());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
}
/**
* Test get scorm attempt count
*/
public function test_mod_scorm_get_scorm_attempt_count_own_empty() {
// Set to the student user.
self::setUser($this->student);
// Retrieve my attempts (should be 0).
$result = mod_scorm_external::get_scorm_attempt_count($this->scorm->id, $this->student->id);
$result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_attempt_count_returns(), $result);
$this->assertEquals(0, $result['attemptscount']);
}
public function test_mod_scorm_get_scorm_attempt_count_own_with_complete() {
// Set to the student user.
self::setUser($this->student);
// Create attempts.
$scoes = scorm_get_scoes($this->scorm->id);
$sco = array_shift($scoes);
scorm_insert_track($this->student->id, $this->scorm->id, $sco->id, 1, 'cmi.core.lesson_status', 'completed');
scorm_insert_track($this->student->id, $this->scorm->id, $sco->id, 2, 'cmi.core.lesson_status', 'completed');
$result = mod_scorm_external::get_scorm_attempt_count($this->scorm->id, $this->student->id);
$result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_attempt_count_returns(), $result);
$this->assertEquals(2, $result['attemptscount']);
}
public function test_mod_scorm_get_scorm_attempt_count_own_incomplete() {
// Set to the student user.
self::setUser($this->student);
// Create a complete attempt, and an incomplete attempt.
$scoes = scorm_get_scoes($this->scorm->id);
$sco = array_shift($scoes);
scorm_insert_track($this->student->id, $this->scorm->id, $sco->id, 1, 'cmi.core.lesson_status', 'completed');
scorm_insert_track($this->student->id, $this->scorm->id, $sco->id, 2, 'cmi.core.credit', '0');
$result = mod_scorm_external::get_scorm_attempt_count($this->scorm->id, $this->student->id, true);
$result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_attempt_count_returns(), $result);
$this->assertEquals(1, $result['attemptscount']);
}
public function test_mod_scorm_get_scorm_attempt_count_others_as_teacher() {
// As a teacher.
self::setUser($this->teacher);
// Create a completed attempt for student.
$scoes = scorm_get_scoes($this->scorm->id);
$sco = array_shift($scoes);
scorm_insert_track($this->student->id, $this->scorm->id, $sco->id, 1, 'cmi.core.lesson_status', 'completed');
// I should be able to view the attempts for my students.
$result = mod_scorm_external::get_scorm_attempt_count($this->scorm->id, $this->student->id);
$result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_attempt_count_returns(), $result);
$this->assertEquals(1, $result['attemptscount']);
}
public function test_mod_scorm_get_scorm_attempt_count_others_as_student() {
// Create a second student.
$student2 = self::getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($student2->id, $this->course->id, $this->studentrole->id, 'manual');
// As a student.
self::setUser($student2);
// I should not be able to view the attempts of another student.
$this->setExpectedException('required_capability_exception');
mod_scorm_external::get_scorm_attempt_count($this->scorm->id, $this->student->id);
}
public function test_mod_scorm_get_scorm_attempt_count_invalid_instanceid() {
// As student.
self::setUser($this->student);
// Test invalid instance id.
$this->setExpectedException('moodle_exception');
mod_scorm_external::get_scorm_attempt_count(0, $this->student->id);
}
public function test_mod_scorm_get_scorm_attempt_count_invalid_userid() {
// As student.
self::setUser($this->student);
$this->setExpectedException('moodle_exception');
mod_scorm_external::get_scorm_attempt_count($this->scorm->id, -1);
}
/**
* Test get scorm scoes
*/
public function test_mod_scorm_get_scorm_scoes() {
global $DB;
$this->resetAfterTest(true);
// Create users.
$student = self::getDataGenerator()->create_user();
$teacher = self::getDataGenerator()->create_user();
// Set to the student user.
self::setUser($student);
// Create courses to add the modules.
$course = self::getDataGenerator()->create_course();
// First scorm, dates restriction.
$record = new stdClass();
$record->course = $course->id;
$record->timeopen = time() + DAYSECS;
$record->timeclose = $record->timeopen + DAYSECS;
$scorm = self::getDataGenerator()->create_module('scorm', $record);
// Users enrolments.
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id, 'manual');
// Retrieve my scoes, warning!.
try {
mod_scorm_external::get_scorm_scoes($scorm->id);
$this->fail('Exception expected due to invalid dates.');
} catch (moodle_exception $e) {
$this->assertEquals('notopenyet', $e->errorcode);
}
$scorm->timeopen = time() - DAYSECS;
$scorm->timeclose = time() - HOURSECS;
$DB->update_record('scorm', $scorm);
try {
mod_scorm_external::get_scorm_scoes($scorm->id);
$this->fail('Exception expected due to invalid dates.');
} catch (moodle_exception $e) {
$this->assertEquals('expired', $e->errorcode);
}
// Retrieve my scoes, user with permission.
self::setUser($teacher);
$result = mod_scorm_external::get_scorm_scoes($scorm->id);
$result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_scoes_returns(), $result);
$this->assertCount(2, $result['scoes']);
$this->assertCount(0, $result['warnings']);
$scoes = scorm_get_scoes($scorm->id);
$sco = array_shift($scoes);
$this->assertEquals((array) $sco, $result['scoes'][0]);
$sco = array_shift($scoes);
// Remove specific sco data.
unset($sco->isvisible);
unset($sco->parameters);
$this->assertEquals((array) $sco, $result['scoes'][1]);
// Use organization.
$organization = 'golf_sample_default_org';
$result = mod_scorm_external::get_scorm_scoes($scorm->id, $organization);
$result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_scoes_returns(), $result);
$this->assertCount(1, $result['scoes']);
$this->assertEquals($organization, $result['scoes'][0]['organization']);
$this->assertCount(0, $result['warnings']);
// Test invalid instance id.
try {
mod_scorm_external::get_scorm_scoes(0);
$this->fail('Exception expected due to invalid instance id.');
} catch (moodle_exception $e) {
$this->assertEquals('invalidrecord', $e->errorcode);
}
}
/*
* Test get scorm user data
*/
public function test_mod_scorm_get_scorm_user_data() {
global $DB;
$this->resetAfterTest(true);
// Create users.
$student1 = self::getDataGenerator()->create_user();
$teacher = self::getDataGenerator()->create_user();
// Set to the student user.
self::setUser($student1);
// Create courses to add the modules.
$course = self::getDataGenerator()->create_course();
// First scorm.
$record = new stdClass();
$record->course = $course->id;
$scorm = self::getDataGenerator()->create_module('scorm', $record);
// Users enrolments.
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
$this->getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id, 'manual');
// Create attempts.
$scoes = scorm_get_scoes($scorm->id);
$sco = array_shift($scoes);
scorm_insert_track($student1->id, $scorm->id, $sco->id, 1, 'cmi.core.lesson_status', 'completed');
scorm_insert_track($student1->id, $scorm->id, $sco->id, 1, 'cmi.core.score.raw', '80');
scorm_insert_track($student1->id, $scorm->id, $sco->id, 2, 'cmi.core.lesson_status', 'completed');
$result = mod_scorm_external::get_scorm_user_data($scorm->id, 1);
$result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_user_data_returns(), $result);
$this->assertCount(2, $result['data']);
// Find our tracking data.
$found = 0;
foreach ($result['data'] as $scodata) {
foreach ($scodata['userdata'] as $userdata) {
if ($userdata['element'] == 'cmi.core.lesson_status' and $userdata['value'] == 'completed') {
$found++;
}
if ($userdata['element'] == 'cmi.core.score.raw' and $userdata['value'] == '80') {
$found++;
}
}
}
$this->assertEquals(2, $found);
// Test invalid instance id.
try {
mod_scorm_external::get_scorm_user_data(0, 1);
$this->fail('Exception expected due to invalid instance id.');
} catch (moodle_exception $e) {
$this->assertEquals('invalidrecord', $e->errorcode);
}
}
/**
* Test insert scorm tracks
*/
public function test_mod_scorm_insert_scorm_tracks() {
global $DB;
$this->resetAfterTest(true);
// Create users.
$student = self::getDataGenerator()->create_user();
// Set to the student user.
self::setUser($student);
// Create courses to add the modules.
$course = self::getDataGenerator()->create_course();
// First scorm, dates restriction.
$record = new stdClass();
$record->course = $course->id;
$record->timeopen = time() + DAYSECS;
$record->timeclose = $record->timeopen + DAYSECS;
$scorm = self::getDataGenerator()->create_module('scorm', $record);
// Get a SCO.
$scoes = scorm_get_scoes($scorm->id);
$sco = array_shift($scoes);
// Tracks.
$tracks = array();
$tracks[] = array(
'element' => 'cmi.core.lesson_status',
'value' => 'completed'
);
$tracks[] = array(
'element' => 'cmi.core.score.raw',
'value' => '80'
);
// Users enrolments.
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
// Exceptions first.
try {
mod_scorm_external::insert_scorm_tracks($sco->id, 1, $tracks);
$this->fail('Exception expected due to dates');
} catch (moodle_exception $e) {
$this->assertEquals('notopenyet', $e->errorcode);
}
$scorm->timeopen = time() - DAYSECS;
$scorm->timeclose = time() - HOURSECS;
$DB->update_record('scorm', $scorm);
try {
mod_scorm_external::insert_scorm_tracks($sco->id, 1, $tracks);
$this->fail('Exception expected due to dates');
} catch (moodle_exception $e) {
$this->assertEquals('expired', $e->errorcode);
}
// Test invalid instance id.
try {
mod_scorm_external::insert_scorm_tracks(0, 1, $tracks);
$this->fail('Exception expected due to invalid sco id.');
} catch (moodle_exception $e) {
$this->assertEquals('cannotfindsco', $e->errorcode);
}
$scorm->timeopen = 0;
$scorm->timeclose = 0;
$DB->update_record('scorm', $scorm);
// Retrieve my tracks.
$result = mod_scorm_external::insert_scorm_tracks($sco->id, 1, $tracks);
$result = external_api::clean_returnvalue(mod_scorm_external::insert_scorm_tracks_returns(), $result);
$this->assertCount(0, $result['warnings']);
$trackids = $DB->get_records('scorm_scoes_track', array('userid' => $student->id, 'scoid' => $sco->id,
'scormid' => $scorm->id, 'attempt' => 1));
// We use asort here to prevent problems with ids ordering.
$expectedkeys = array_keys($trackids);
$this->assertEquals(asort($expectedkeys), asort($result['trackids']));
}
/**
* Test get scorm sco tracks
*/
public function test_mod_scorm_get_scorm_sco_tracks() {
global $DB;
$this->resetAfterTest(true);
// Create users.
$student = self::getDataGenerator()->create_user();
$otherstudent = self::getDataGenerator()->create_user();
$teacher = self::getDataGenerator()->create_user();
// Set to the student user.
self::setUser($student);
// Create courses to add the modules.
$course = self::getDataGenerator()->create_course();
// First scorm.
$record = new stdClass();
$record->course = $course->id;
$scorm = self::getDataGenerator()->create_module('scorm', $record);
// Users enrolments.
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id, 'manual');
// Create attempts.
$scoes = scorm_get_scoes($scorm->id);
$sco = array_shift($scoes);
scorm_insert_track($student->id, $scorm->id, $sco->id, 1, 'cmi.core.lesson_status', 'completed');
scorm_insert_track($student->id, $scorm->id, $sco->id, 1, 'cmi.core.score.raw', '80');
scorm_insert_track($student->id, $scorm->id, $sco->id, 2, 'cmi.core.lesson_status', 'completed');
$result = mod_scorm_external::get_scorm_sco_tracks($sco->id, $student->id, 1);
$result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_sco_tracks_returns(), $result);
// 7 default elements + 2 custom ones.
$this->assertCount(9, $result['data']['tracks']);
$this->assertEquals(1, $result['data']['attempt']);
$this->assertCount(0, $result['warnings']);
// Find our tracking data.
$found = 0;
foreach ($result['data']['tracks'] as $userdata) {
if ($userdata['element'] == 'cmi.core.lesson_status' and $userdata['value'] == 'completed') {
$found++;
}
if ($userdata['element'] == 'cmi.core.score.raw' and $userdata['value'] == '80') {
$found++;
}
}
$this->assertEquals(2, $found);
// Try invalid attempt.
$result = mod_scorm_external::get_scorm_sco_tracks($sco->id, $student->id, 10);
$result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_sco_tracks_returns(), $result);
$this->assertCount(0, $result['data']['tracks']);
$this->assertEquals(10, $result['data']['attempt']);
$this->assertCount(1, $result['warnings']);
$this->assertEquals('notattempted', $result['warnings'][0]['warningcode']);
// Capabilities check.
try {
mod_scorm_external::get_scorm_sco_tracks($sco->id, $otherstudent->id);
$this->fail('Exception expected due to invalid instance id.');
} catch (required_capability_exception $e) {
$this->assertEquals('nopermissions', $e->errorcode);
}
self::setUser($teacher);
// Ommit the attempt parameter, the function should calculate the last attempt.
$result = mod_scorm_external::get_scorm_sco_tracks($sco->id, $student->id);
$result = external_api::clean_returnvalue(mod_scorm_external::get_scorm_sco_tracks_returns(), $result);
// 7 default elements + 1 custom one.
$this->assertCount(8, $result['data']['tracks']);
$this->assertEquals(2, $result['data']['attempt']);
// Test invalid instance id.
try {
mod_scorm_external::get_scorm_sco_tracks(0, 1);
$this->fail('Exception expected due to invalid instance id.');
} catch (moodle_exception $e) {
$this->assertEquals('cannotfindsco', $e->errorcode);
}
// Invalid user.
try {
mod_scorm_external::get_scorm_sco_tracks($sco->id, 0);
$this->fail('Exception expected due to invalid instance id.');
} catch (moodle_exception $e) {
$this->assertEquals('invaliduser', $e->errorcode);
}
}
/*
* Test get scorms by courses
*/
public function test_mod_scorm_get_scorms_by_courses() {
global $DB;
$this->resetAfterTest(true);
// Create users.
$student = self::getDataGenerator()->create_user();
$teacher = self::getDataGenerator()->create_user();
// Set to the student user.
self::setUser($student);
// Create courses to add the modules.
$course1 = self::getDataGenerator()->create_course();
$course2 = self::getDataGenerator()->create_course();
// First scorm.
$record = new stdClass();
$record->introformat = FORMAT_HTML;
$record->course = $course1->id;
$record->hidetoc = 2;
$record->displayattemptstatus = 2;
$record->skipview = 2;
$scorm1 = self::getDataGenerator()->create_module('scorm', $record);
// Second scorm.
$record = new stdClass();
$record->introformat = FORMAT_HTML;
$record->course = $course2->id;
$scorm2 = self::getDataGenerator()->create_module('scorm', $record);
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
// Users enrolments.
$this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($teacher->id, $course1->id, $teacherrole->id, 'manual');
// Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
$enrol = enrol_get_plugin('manual');
$enrolinstances = enrol_get_instances($course2->id, true);
foreach ($enrolinstances as $courseenrolinstance) {
if ($courseenrolinstance->enrol == "manual") {
$instance2 = $courseenrolinstance;
break;
}
}
$enrol->enrol_user($instance2, $student->id, $studentrole->id);
$returndescription = mod_scorm_external::get_scorms_by_courses_returns();
// Test open/close dates.
$timenow = time();
$scorm1->timeopen = $timenow - DAYSECS;
$scorm1->timeclose = $timenow - HOURSECS;
$DB->update_record('scorm', $scorm1);
$result = mod_scorm_external::get_scorms_by_courses(array($course1->id));
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertCount(1, $result['warnings']);
// Only 'id', 'coursemodule', 'course', 'name', 'intro', 'introformat'.
$this->assertCount(6, $result['scorms'][0]);
$this->assertEquals('expired', $result['warnings'][0]['warningcode']);
$scorm1->timeopen = $timenow + DAYSECS;
$scorm1->timeclose = $scorm1->timeopen + DAYSECS;
$DB->update_record('scorm', $scorm1);
$result = mod_scorm_external::get_scorms_by_courses(array($course1->id));
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertCount(1, $result['warnings']);
// Only 'id', 'coursemodule', 'course', 'name', 'intro', 'introformat'.
$this->assertCount(6, $result['scorms'][0]);
$this->assertEquals('notopenyet', $result['warnings'][0]['warningcode']);
// Reset times.
$scorm1->timeopen = 0;
$scorm1->timeclose = 0;
$DB->update_record('scorm', $scorm1);
// Create what we expect to be returned when querying the two courses.
// First for the student user.
$expectedfields = array('id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'version', 'maxgrade',
'grademethod', 'whatgrade', 'maxattempt', 'forcecompleted', 'forcenewattempt', 'lastattemptlock',
'displayattemptstatus', 'displaycoursestructure', 'sha1hash', 'md5hash', 'revision', 'launch',
'skipview', 'hidebrowse', 'hidetoc', 'nav', 'navpositionleft', 'navpositiontop', 'auto',
'popup', 'width', 'height', 'timeopen', 'timeclose', 'displayactivityname', 'packagesize',
'packageurl', 'scormtype', 'reference');
// Add expected coursemodule and data.
$scorm1->coursemodule = $scorm1->cmid;
$scorm1->section = 0;
$scorm1->visible = true;
$scorm1->groupmode = 0;
$scorm1->groupingid = 0;
$scorm2->coursemodule = $scorm2->cmid;
$scorm2->section = 0;
$scorm2->visible = true;
$scorm2->groupmode = 0;
$scorm2->groupingid = 0;
// SCORM size. The same package is used in both SCORMs.
$scormcontext1 = context_module::instance($scorm1->cmid);
$scormcontext2 = context_module::instance($scorm2->cmid);
$fs = get_file_storage();
$packagefile = $fs->get_file($scormcontext1->id, 'mod_scorm', 'package', 0, '/', $scorm1->reference);
$packagesize = $packagefile->get_filesize();
$packageurl1 = moodle_url::make_webservice_pluginfile_url(
$scormcontext1->id, 'mod_scorm', 'package', 0, '/', $scorm1->reference)->out(false);
$packageurl2 = moodle_url::make_webservice_pluginfile_url(
$scormcontext2->id, 'mod_scorm', 'package', 0, '/', $scorm2->reference)->out(false);
$scorm1->packagesize = $packagesize;
$scorm1->packageurl = $packageurl1;
$scorm2->packagesize = $packagesize;
$scorm2->packageurl = $packageurl2;
// Forced to boolean as it is returned as PARAM_BOOL.
$protectpackages = (bool)get_config('scorm', 'protectpackagedownloads');
$expected1 = array('protectpackagedownloads' => $protectpackages);
$expected2 = array('protectpackagedownloads' => $protectpackages);
foreach ($expectedfields as $field) {
// Since we return the fields used as boolean as PARAM_BOOL instead PARAM_INT we need to force casting here.
// From the returned fields definition we obtain the type expected for the field.
$fieldtype = $returndescription->keys['scorms']->content->keys[$field]->type;
if ($fieldtype == PARAM_BOOL) {
$expected1[$field] = (bool) $scorm1->{$field};
$expected2[$field] = (bool) $scorm2->{$field};
} else {
$expected1[$field] = $scorm1->{$field};
$expected2[$field] = $scorm2->{$field};
}
}
$expectedscorms = array();
$expectedscorms[] = $expected2;
$expectedscorms[] = $expected1;
// Call the external function passing course ids.
$result = mod_scorm_external::get_scorms_by_courses(array($course2->id, $course1->id));
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertEquals($expectedscorms, $result['scorms']);
// Call the external function without passing course id.
$result = mod_scorm_external::get_scorms_by_courses();
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertEquals($expectedscorms, $result['scorms']);
// Unenrol user from second course and alter expected scorms.
$enrol->unenrol_user($instance2, $student->id);
array_shift($expectedscorms);
// Call the external function without passing course id.
$result = mod_scorm_external::get_scorms_by_courses();
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertEquals($expectedscorms, $result['scorms']);
// Call for the second course we unenrolled the user from, expected warning.
$result = mod_scorm_external::get_scorms_by_courses(array($course2->id));
$this->assertCount(1, $result['warnings']);
$this->assertEquals('1', $result['warnings'][0]['warningcode']);
$this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
// Now, try as a teacher for getting all the additional fields.
self::setUser($teacher);
$additionalfields = array('updatefreq', 'timemodified', 'options',
'completionstatusrequired', 'completionscorerequired', 'autocommit',
'section', 'visible', 'groupmode', 'groupingid');
foreach ($additionalfields as $field) {
$fieldtype = $returndescription->keys['scorms']->content->keys[$field]->type;
if ($fieldtype == PARAM_BOOL) {
$expectedscorms[0][$field] = (bool) $scorm1->{$field};
} else {
$expectedscorms[0][$field] = $scorm1->{$field};
}
}
$result = mod_scorm_external::get_scorms_by_courses();
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertEquals($expectedscorms, $result['scorms']);
// Even with the SCORM closed in time teacher should retrieve the info.
$scorm1->timeopen = $timenow - DAYSECS;
$scorm1->timeclose = $timenow - HOURSECS;
$DB->update_record('scorm', $scorm1);
$expectedscorms[0]['timeopen'] = $scorm1->timeopen;
$expectedscorms[0]['timeclose'] = $scorm1->timeclose;
$result = mod_scorm_external::get_scorms_by_courses();
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertEquals($expectedscorms, $result['scorms']);
// Admin also should get all the information.
self::setAdminUser();
$result = mod_scorm_external::get_scorms_by_courses(array($course1->id));
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertEquals($expectedscorms, $result['scorms']);
}
}