MDL-69225 h5pactivity: Review when and which banners display
[moodle.git] / mod / h5pactivity / tests / local / manager_test.php
blob65055c8dd4acafda4bfb4ab49193b8d8d5bb0cc9
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.
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/>.
18 namespace mod_h5pactivity\local;
19 use context_module;
20 use stdClass;
22 /**
23 * Manager tests class for mod_h5pactivity.
25 * @package mod_h5pactivity
26 * @covers \mod_h5pactivity\local\manager
27 * @category test
28 * @copyright 2020 Ferran Recio <ferran@moodle.com>
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 class manager_test extends \advanced_testcase {
33 /**
34 * Test for static create methods.
36 public function test_create() {
38 $this->resetAfterTest();
39 $this->setAdminUser();
41 $course = $this->getDataGenerator()->create_course();
42 $activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]);
43 $cm = get_coursemodule_from_id('h5pactivity', $activity->cmid, 0, false, MUST_EXIST);
44 $context = context_module::instance($cm->id);
46 $manager = manager::create_from_instance($activity);
47 $manageractivity = $manager->get_instance();
48 $this->assertEquals($activity->id, $manageractivity->id);
49 $managercm = $manager->get_coursemodule();
50 $this->assertEquals($cm->id, $managercm->id);
51 $managercontext = $manager->get_context();
52 $this->assertEquals($context->id, $managercontext->id);
54 $manager = manager::create_from_coursemodule($cm);
55 $manageractivity = $manager->get_instance();
56 $this->assertEquals($activity->id, $manageractivity->id);
57 $managercm = $manager->get_coursemodule();
58 $this->assertEquals($cm->id, $managercm->id);
59 $managercontext = $manager->get_context();
60 $this->assertEquals($context->id, $managercontext->id);
63 /**
64 * Test for is_tracking_enabled and can_submit methods.
66 * @covers ::is_tracking_enabled
67 * @covers ::can_submit
68 * @dataProvider is_tracking_enabled_data
69 * @param bool $login if the user is logged in
70 * @param string $role user role in course
71 * @param int $enabletracking if tracking is enabled
72 * @param bool $expectedtracking expected result for is_tracking_enabled()
73 * @param bool $expectedsubmit expected result for can_submit()
75 public function test_is_tracking_enabled_and_can_submit(bool $login, string $role, int $enabletracking, bool $expectedtracking,
76 bool $expectedsubmit): void {
78 $this->resetAfterTest();
79 $this->setAdminUser();
81 $course = $this->getDataGenerator()->create_course();
82 $activity = $this->getDataGenerator()->create_module('h5pactivity',
83 ['course' => $course, 'enabletracking' => $enabletracking]);
85 $user = $this->getDataGenerator()->create_and_enrol($course, $role);
86 if ($login) {
87 $this->setUser($user);
88 $param = null;
89 } else {
90 $param = $user;
93 $manager = manager::create_from_instance($activity);
94 $this->assertEquals($expectedtracking, $manager->is_tracking_enabled());
95 $this->assertEquals($expectedsubmit, $manager->can_submit($param));
98 /**
99 * Data provider for test_is_tracking_enabled_and_can_submit.
101 * @return array
103 public function is_tracking_enabled_data(): array {
104 return [
105 'Logged student, tracking enabled' => [
106 true, 'student', 1, true, true,
108 'Logged student, tracking disabled' => [
109 true, 'student', 0, false, true,
111 'Logged teacher, tracking enabled' => [
112 true, 'editingteacher', 1, true, false,
114 'Logged teacher, tracking disabled' => [
115 true, 'editingteacher', 0, false, false,
117 'No logged student, tracking enabled' => [
118 true, 'student', 1, true, true,
120 'No logged student, tracking disabled' => [
121 true, 'student', 0, false, true,
123 'No logged teacher, tracking enabled' => [
124 true, 'editingteacher', 1, true, false,
126 'No logged teacher, tracking disabled' => [
127 true, 'editingteacher', 0, false, false,
133 * Test for get_users_scaled_score.
135 * @dataProvider get_users_scaled_score_data
136 * @param int $enabletracking if tracking is enabled
137 * @param int $gradingmethod new grading method
138 * @param array $result1 student 1 results (scaled, timemodified, attempt number)
139 * @param array $result2 student 2 results (scaled, timemodified, attempt number)
141 public function test_get_users_scaled_score(int $enabletracking, int $gradingmethod, array $result1, array $result2) {
142 global $DB;
144 $this->resetAfterTest();
145 $this->setAdminUser();
147 $course = $this->getDataGenerator()->create_course();
148 $activity = $this->getDataGenerator()->create_module('h5pactivity',
149 ['course' => $course, 'enabletracking' => $enabletracking, 'grademethod' => $gradingmethod]);
151 // Generate two users with 4 attempts each.
152 $user1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
153 $this->generate_fake_attempts($activity, $user1, 1);
154 $user2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
155 $this->generate_fake_attempts($activity, $user2, 2);
157 $manager = manager::create_from_instance($activity);
159 // Get all users scaled scores.
160 $scaleds = $manager->get_users_scaled_score();
162 // No results will be returned if tracking is dsabled or manual grading method is defined.
163 if (empty($result1)) {
164 $this->assertNull($scaleds);
165 return;
168 $this->assertCount(2, $scaleds);
170 // Check expected user1 scaled score.
171 $scaled = $scaleds[$user1->id];
172 $this->assertEquals($user1->id, $scaled->userid);
173 $this->assertEquals($result1[0], $scaled->scaled);
174 $this->assertEquals($result1[1], $scaled->timemodified);
175 if ($result1[2]) {
176 $attempt = $DB->get_record('h5pactivity_attempts', ['id' => $scaled->attemptid]);
177 $this->assertEquals($attempt->h5pactivityid, $activity->id);
178 $this->assertEquals($attempt->userid, $scaled->userid);
179 $this->assertEquals($attempt->scaled, round($scaled->scaled, 5));
180 $this->assertEquals($attempt->timemodified, $scaled->timemodified);
181 $this->assertEquals($result1[2], $attempt->attempt);
182 } else {
183 $this->assertEquals(0, $scaled->attemptid);
186 // Check expected user2 scaled score.
187 $scaled = $scaleds[$user2->id];
188 $this->assertEquals($user2->id, $scaled->userid);
189 $this->assertEquals($result2[0], round($scaled->scaled, 5));
190 $this->assertEquals($result2[1], $scaled->timemodified);
191 if ($result2[2]) {
192 $attempt = $DB->get_record('h5pactivity_attempts', ['id' => $scaled->attemptid]);
193 $this->assertEquals($attempt->h5pactivityid, $activity->id);
194 $this->assertEquals($attempt->userid, $scaled->userid);
195 $this->assertEquals($attempt->scaled, $scaled->scaled);
196 $this->assertEquals($attempt->timemodified, $scaled->timemodified);
197 $this->assertEquals($result2[2], $attempt->attempt);
198 } else {
199 $this->assertEquals(0, $scaled->attemptid);
202 // Now check a single user record.
203 $scaleds = $manager->get_users_scaled_score($user2->id);
204 $this->assertCount(1, $scaleds);
205 $scaled2 = $scaleds[$user2->id];
206 $this->assertEquals($scaled->userid, $scaled2->userid);
207 $this->assertEquals($scaled->scaled, $scaled2->scaled);
208 $this->assertEquals($scaled->attemptid, $scaled2->attemptid);
209 $this->assertEquals($scaled->timemodified, $scaled2->timemodified);
213 * Data provider for get_users_scaled_score.
215 * @return array
217 public function get_users_scaled_score_data(): array {
218 return [
219 'Tracking with max attempt method' => [
220 1, manager::GRADEHIGHESTATTEMPT, [1.00000, 31, 2], [0.66667, 32, 2]
222 'Tracking with average attempt method' => [
223 1, manager::GRADEAVERAGEATTEMPT, [0.61111, 51, 0], [0.52222, 52, 0]
225 'Tracking with last attempt method' => [
226 1, manager::GRADELASTATTEMPT, [0.33333, 51, 3], [0.40000, 52, 3]
228 'Tracking with first attempt method' => [
229 1, manager::GRADEFIRSTATTEMPT, [0.50000, 11, 1], [0.50000, 12, 1]
231 'Tracking with manual attempt grading' => [
232 1, manager::GRADEMANUAL, [], []
234 'No tracking with max attempt method' => [
235 0, manager::GRADEHIGHESTATTEMPT, [], []
237 'No tracking with average attempt method' => [
238 0, manager::GRADEAVERAGEATTEMPT, [], []
240 'No tracking with last attempt method' => [
241 0, manager::GRADELASTATTEMPT, [], []
243 'No tracking with first attempt method' => [
244 0, manager::GRADEFIRSTATTEMPT, [], []
246 'No tracking with manual attempt grading' => [
247 0, manager::GRADEMANUAL, [], []
253 * Test static get_grading_methods.
255 public function test_get_grading_methods() {
256 $methods = manager::get_grading_methods();
257 $this->assertCount(5, $methods);
258 $this->assertNotEmpty($methods[manager::GRADEHIGHESTATTEMPT]);
259 $this->assertNotEmpty($methods[manager::GRADEAVERAGEATTEMPT]);
260 $this->assertNotEmpty($methods[manager::GRADELASTATTEMPT]);
261 $this->assertNotEmpty($methods[manager::GRADEFIRSTATTEMPT]);
262 $this->assertNotEmpty($methods[manager::GRADEMANUAL]);
266 * Test static get_selected_attempt.
268 * @dataProvider get_selected_attempt_data
269 * @param int $enabletracking if tracking is enabled
270 * @param int $gradingmethod new grading method
271 * @param int $result the expected result
273 public function test_get_selected_attempt(int $enabletracking, int $gradingmethod, int $result) {
274 $this->resetAfterTest();
275 $this->setAdminUser();
277 $course = $this->getDataGenerator()->create_course();
278 $activity = $this->getDataGenerator()->create_module('h5pactivity',
279 ['course' => $course, 'enabletracking' => $enabletracking, 'grademethod' => $gradingmethod]);
281 $manager = manager::create_from_instance($activity);
283 $selected = $manager->get_selected_attempt();
285 $this->assertEquals($result, $selected[0]);
286 $this->assertNotEmpty($selected[1]);
290 * Data provider for get_users_scaled_score.
292 * @return array
294 public function get_selected_attempt_data(): array {
295 return [
296 'Tracking with max attempt method' => [
297 1, manager::GRADEHIGHESTATTEMPT, manager::GRADEHIGHESTATTEMPT
299 'Tracking with average attempt method' => [
300 1, manager::GRADEAVERAGEATTEMPT, manager::GRADEAVERAGEATTEMPT
302 'Tracking with last attempt method' => [
303 1, manager::GRADELASTATTEMPT, manager::GRADELASTATTEMPT
305 'Tracking with first attempt method' => [
306 1, manager::GRADEFIRSTATTEMPT, manager::GRADEFIRSTATTEMPT
308 'Tracking with manual attempt grading' => [
309 1, manager::GRADEMANUAL, manager::GRADEMANUAL
311 'No tracking with max attempt method' => [
312 0, manager::GRADEHIGHESTATTEMPT, manager::GRADEMANUAL
314 'No tracking with average attempt method' => [
315 0, manager::GRADEAVERAGEATTEMPT, manager::GRADEMANUAL
317 'No tracking with last attempt method' => [
318 0, manager::GRADELASTATTEMPT, manager::GRADEMANUAL
320 'No tracking with first attempt method' => [
321 0, manager::GRADEFIRSTATTEMPT, manager::GRADEMANUAL
323 'No tracking with manual attempt grading' => [
324 0, manager::GRADEMANUAL, manager::GRADEMANUAL
330 * Test static get_review_modes.
332 public function test_get_review_modes() {
333 $methods = manager::get_review_modes();
334 $this->assertCount(2, $methods);
335 $this->assertNotEmpty($methods[manager::REVIEWCOMPLETION]);
336 $this->assertNotEmpty($methods[manager::REVIEWNONE]);
340 * Test get_grader method.
342 public function test_get_grader() {
343 $this->resetAfterTest();
344 $this->setAdminUser();
346 $course = $this->getDataGenerator()->create_course();
347 $activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]);
348 $cm = get_coursemodule_from_id('h5pactivity', $activity->cmid, 0, false, MUST_EXIST);
349 $context = context_module::instance($cm->id);
351 $manager = manager::create_from_instance($activity);
352 $grader = $manager->get_grader();
354 $this->assertInstanceOf('mod_h5pactivity\local\grader', $grader);
359 * Test static can_view_all_attempts.
361 * @dataProvider can_view_all_attempts_data
362 * @param int $enabletracking if tracking is enabled
363 * @param bool $usestudent if test must be done with a user role
364 * @param bool $useloggedin if test must be done with the loggedin user
365 * @param bool $result the expected result
367 public function test_can_view_all_attempts(int $enabletracking, bool $usestudent, bool $useloggedin, bool $result) {
368 global $USER;
370 $this->resetAfterTest();
371 $this->setAdminUser();
373 $course = $this->getDataGenerator()->create_course();
374 $activity = $this->getDataGenerator()->create_module('h5pactivity',
375 ['course' => $course, 'enabletracking' => $enabletracking]);
377 $manager = manager::create_from_instance($activity);
379 $user = $this->getDataGenerator()->create_and_enrol($course, 'student');
380 $loggedin = $USER;
382 // We want to test what when the method is called to check a different user than $USER.
383 if (!$usestudent) {
384 $loggedin = $user;
385 $user = $USER;
388 if ($useloggedin) {
389 $this->setUser($user);
390 $user = null;
391 } else {
392 $this->setUser($loggedin);
395 $this->assertEquals($result, $manager->can_view_all_attempts($user));
399 * Data provider for test_can_view_all_attempts.
401 * @return array
403 public function can_view_all_attempts_data(): array {
404 return [
405 // No tracking cases.
406 'No tracking with admin using $USER' => [
407 0, false, false, false
409 'No tracking with student using $USER' => [
410 0, true, false, false
412 'No tracking with admin loggedin' => [
413 0, false, true, false
415 'No tracking with student loggedin' => [
416 0, true, true, false
418 // Tracking enabled cases.
419 'Tracking with admin using $USER' => [
420 1, false, false, true
422 'Tracking with student using $USER' => [
423 1, true, false, false
425 'Tracking with admin loggedin' => [
426 1, false, true, true
428 'Tracking with student loggedin' => [
429 1, true, true, false
435 * Test static can_view_own_attempts.
437 * @dataProvider can_view_own_attempts_data
438 * @param int $enabletracking if tracking is enabled
439 * @param int $reviewmode the attempt review mode
440 * @param bool $useloggedin if test must be done with the loggedin user
441 * @param bool $hasattempts if the student have attempts
442 * @param bool $result the expected result
444 public function test_can_view_own_attempts(int $enabletracking, int $reviewmode,
445 bool $useloggedin, bool $hasattempts, bool $result) {
447 $this->resetAfterTest();
448 $this->setAdminUser();
450 $course = $this->getDataGenerator()->create_course();
451 $activity = $this->getDataGenerator()->create_module('h5pactivity',
452 ['course' => $course, 'enabletracking' => $enabletracking, 'reviewmode' => $reviewmode]);
454 $manager = manager::create_from_instance($activity);
456 $user = $this->getDataGenerator()->create_and_enrol($course, 'student');
458 if ($hasattempts) {
459 $this->generate_fake_attempts($activity, $user, 1);
462 if ($useloggedin) {
463 $this->setUser($user);
464 $user = null;
467 $this->assertEquals($result, $manager->can_view_own_attempts($user));
471 * Data provider for test_can_view_own_attempts.
473 * @return array
475 public function can_view_own_attempts_data(): array {
476 return [
477 // No tracking cases.
478 'No tracking, review none, using $USER, without attempts' => [
479 0, manager::REVIEWNONE, false, false, false
481 'No tracking, review enabled, using $USER, without attempts' => [
482 0, manager::REVIEWCOMPLETION, false, false, false
484 'No tracking, review none, loggedin, without attempts' => [
485 0, manager::REVIEWNONE, true, false, false
487 'No tracking, review enabled, loggedin, without attempts' => [
488 0, manager::REVIEWCOMPLETION, true, false, false
490 'No tracking, review none, using $USER, with attempts' => [
491 0, manager::REVIEWNONE, false, true, false
493 'No tracking, review enabled, using $USER, with attempts' => [
494 0, manager::REVIEWCOMPLETION, false, true, false
496 'No tracking, review none, loggedin, with attempts' => [
497 0, manager::REVIEWNONE, true, true, false
499 'No tracking, review enabled, loggedin, with attempts' => [
500 0, manager::REVIEWCOMPLETION, true, true, false
502 // Tracking enabled cases.
503 'Tracking enabled, review none, using $USER, without attempts' => [
504 1, manager::REVIEWNONE, false, false, false
506 'Tracking enabled, review enabled, using $USER, without attempts' => [
507 1, manager::REVIEWCOMPLETION, false, false, true
509 'Tracking enabled, review none, loggedin, without attempts' => [
510 1, manager::REVIEWNONE, true, false, false
512 'Tracking enabled, review enabled, loggedin, without attempts' => [
513 1, manager::REVIEWCOMPLETION, true, false, true
515 'Tracking enabled, review none, using $USER, with attempts' => [
516 1, manager::REVIEWNONE, false, true, false
518 'Tracking enabled, review enabled, using $USER, with attempts' => [
519 1, manager::REVIEWCOMPLETION, false, true, true
521 'Tracking enabled, review none, loggedin, with attempts' => [
522 1, manager::REVIEWNONE, true, true, false
524 'Tracking enabled, review enabled, loggedin, with attempts' => [
525 1, manager::REVIEWCOMPLETION, true, true, true
531 * Test static count_attempts of one user.
533 public function test_count_attempts() {
535 $this->resetAfterTest();
536 $this->setAdminUser();
538 $course = $this->getDataGenerator()->create_course();
539 $activity = $this->getDataGenerator()->create_module('h5pactivity',
540 ['course' => $course]);
542 $manager = manager::create_from_instance($activity);
544 // User without attempts.
545 $user1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
547 // User with 1 attempt.
548 $user2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
549 $this->generate_fake_attempts($activity, $user2, 1);
551 // User with 2 attempts.
552 $user3 = $this->getDataGenerator()->create_and_enrol($course, 'student');
553 $this->generate_fake_attempts($activity, $user3, 1);
555 // Incomplete user2 and 3 has only 3 attempts completed.
556 $this->assertEquals(0, $manager->count_attempts($user1->id));
557 $this->assertEquals(3, $manager->count_attempts($user2->id));
558 $this->assertEquals(3, $manager->count_attempts($user3->id));
562 * Test static count_attempts of all active participants.
564 * @dataProvider count_attempts_all_data
565 * @param bool $canview if the student role has mod_h5pactivity/view capability
566 * @param bool $cansubmit if the student role has mod_h5pactivity/submit capability
567 * @param bool $extrarole if an extra role without submit capability is required
568 * @param int $result the expected result
570 public function test_count_attempts_all(bool $canview, bool $cansubmit, bool $extrarole, int $result) {
571 global $DB;
573 $this->resetAfterTest();
574 $this->setAdminUser();
576 $course = $this->getDataGenerator()->create_course();
577 $activity = $this->getDataGenerator()->create_module(
578 'h5pactivity',
579 ['course' => $course]
582 $manager = manager::create_from_instance($activity);
584 $roleid = $DB->get_field('role', 'id', ['shortname' => 'student']);
586 $newcap = ($canview) ? CAP_ALLOW : CAP_PROHIBIT;
587 role_change_permission($roleid, $manager->get_context(), 'mod/h5pactivity:view', $newcap);
589 $newcap = ($cansubmit) ? CAP_ALLOW : CAP_PROHIBIT;
590 role_change_permission($roleid, $manager->get_context(), 'mod/h5pactivity:submit', $newcap);
592 // Teacher with review capability and attempts (should not be listed).
593 if ($extrarole) {
594 $user1 = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
595 $this->generate_fake_attempts($activity, $user1, 1);
598 // Student with attempts.
599 $user2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
600 $this->generate_fake_attempts($activity, $user2, 1);
602 // Another student with attempts.
603 $user3 = $this->getDataGenerator()->create_and_enrol($course, 'student');
604 $this->generate_fake_attempts($activity, $user3, 1);
606 $this->assertEquals($result, $manager->count_attempts());
610 * Data provider for test_count_attempts_all.
612 * @return array
614 public function count_attempts_all_data(): array {
615 return [
616 'Students with both view and submit capability' => [true, true, false, 6],
617 'Students without view but with submit capability' => [false, true, false, 0],
618 'Students with view but without submit capability' => [true, false, false, 6],
619 'Students without both view and submit capability' => [false, false, false, 0],
620 'Students with both view and submit capability and extra role' => [true, true, true, 6],
621 'Students without view but with submit capability and extra role' => [false, true, true, 0],
622 'Students with view but without submit capability and extra role' => [true, false, true, 6],
623 'Students without both view and submit capability and extra role' => [false, false, true, 0],
628 * Test static test_get_active_users_join of all active participants.
630 * Most method scenarios are tested in test_count_attempts_all so we only
631 * need to test the with $allpotentialusers true and false.
633 * @dataProvider get_active_users_join_data
634 * @param bool $allpotentialusers if the join should return all potential users or only the submitted ones.
635 * @param int $result the expected result
637 public function test_get_active_users_join(bool $allpotentialusers, int $result) {
638 global $DB;
640 $this->resetAfterTest();
641 $this->setAdminUser();
643 $course = $this->getDataGenerator()->create_course();
644 $activity = $this->getDataGenerator()->create_module(
645 'h5pactivity',
646 ['course' => $course]
649 $manager = manager::create_from_instance($activity);
651 $user1 = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
652 $this->generate_fake_attempts($activity, $user1, 1);
654 // Student with attempts.
655 $user2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
656 $this->generate_fake_attempts($activity, $user2, 1);
658 // 2 more students without attempts.
659 $this->getDataGenerator()->create_and_enrol($course, 'student');
660 $this->getDataGenerator()->create_and_enrol($course, 'student');
662 $usersjoin = $manager->get_active_users_join($allpotentialusers);
664 // Final SQL.
665 $num = $DB->count_records_sql(
666 "SELECT COUNT(DISTINCT u.id)
667 FROM {user} u $usersjoin->joins
668 WHERE $usersjoin->wheres",
669 array_merge($usersjoin->params)
672 $this->assertEquals($result, $num);
676 * Data provider for test_get_active_users_join.
678 * @return array
680 public function get_active_users_join_data(): array {
681 return [
682 'All potential users' => [
683 'allpotentialusers' => true,
684 'result' => 3,
686 'Users with attempts' => [
687 'allpotentialusers' => false,
688 'result' => 1,
694 * Test active users joins returns appropriate results for groups
696 public function test_get_active_users_join_groupmode(): void {
697 global $DB;
699 $this->resetAfterTest();
700 $this->setAdminUser();
702 $course = $this->getDataGenerator()->create_course(['groupmode' => SEPARATEGROUPS, 'groupmodeforce' => 1]);
704 // Teacher/user one in group one.
705 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
706 $userone = $this->getDataGenerator()->create_and_enrol($course, 'student');
708 $groupone = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
709 $this->getDataGenerator()->create_group_member(['groupid' => $groupone->id, 'userid' => $teacher->id]);
710 $this->getDataGenerator()->create_group_member(['groupid' => $groupone->id, 'userid' => $userone->id]);
712 // User two in group two.
713 $usertwo = $this->getDataGenerator()->create_and_enrol($course, 'student');
715 $grouptwo = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
716 $this->getDataGenerator()->create_group_member(['groupid' => $grouptwo->id, 'userid' => $usertwo->id]);
718 // User three in no group.
719 $userthree = $this->getDataGenerator()->create_and_enrol($course, 'student');
721 // User four in a non-participation group.
722 $userfour = $this->getDataGenerator()->create_and_enrol($course, 'student');
723 $groupthree = $this->getDataGenerator()->create_group(['courseid' => $course->id, 'participation' => 0]);
724 $this->getDataGenerator()->create_group_member(['groupid' => $groupthree->id, 'userid' => $userfour->id]);
726 $activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]);
727 $manager = manager::create_from_instance($activity);
729 // Admin user can view all participants (any group and none).
730 $usersjoin = $manager->get_active_users_join(true, 0);
731 $users = $DB->get_fieldset_sql("SELECT u.username FROM {user} u {$usersjoin->joins} WHERE {$usersjoin->wheres}",
732 $usersjoin->params);
734 $this->assertEqualsCanonicalizing(
735 [$teacher->username, $userone->username, $usertwo->username, $userthree->username, $userfour->username], $users);
737 // Switch to teacher, who cannot view all participants.
738 $this->setUser($teacher);
740 $usersjoin = $manager->get_active_users_join(true, 0);
741 $users = $DB->get_fieldset_sql("SELECT u.username FROM {user} u {$usersjoin->joins} WHERE {$usersjoin->wheres}",
742 $usersjoin->params);
744 $this->assertEmpty($users);
746 // Teacher can view participants inside group.
747 $usersjoin = $manager->get_active_users_join(true, $groupone->id);
748 $users = $DB->get_fieldset_sql("SELECT u.username FROM {user} u {$usersjoin->joins} WHERE {$usersjoin->wheres}",
749 $usersjoin->params);
751 $this->assertEqualsCanonicalizing([$teacher->username, $userone->username], $users);
755 * Test getting active users join where there are no roles with 'mod/h5pactivity:reviewattempts' capability
757 public function test_get_active_users_join_no_reviewers(): void {
758 global $DB;
760 $this->resetAfterTest();
761 $this->setAdminUser();
763 $course = $this->getDataGenerator()->create_course();
764 $activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]);
765 $user = $this->getDataGenerator()->create_and_enrol($course, 'student');
767 $manager = manager::create_from_instance($activity);
769 // By default manager and editingteacher can review attempts, prohibit both.
770 $rolemanager = $DB->get_field('role', 'id', ['shortname' => 'manager']);
771 role_change_permission($rolemanager, $manager->get_context(), 'mod/h5pactivity:reviewattempts', CAP_PROHIBIT);
773 $roleeditingteacher = $DB->get_field('role', 'id', ['shortname' => 'editingteacher']);
774 role_change_permission($roleeditingteacher, $manager->get_context(), 'mod/h5pactivity:reviewattempts', CAP_PROHIBIT);
776 // Generate users join SQL to find matching users.
777 $usersjoin = $manager->get_active_users_join(true);
778 $usernames = $DB->get_fieldset_sql(
779 "SELECT u.username
780 FROM {user} u
781 {$usersjoin->joins}
782 WHERE {$usersjoin->wheres}",
783 $usersjoin->params
786 $this->assertEquals([$user->username], $usernames);
790 * Test static count_attempts.
792 public function test_count_users_attempts() {
794 $this->resetAfterTest();
795 $this->setAdminUser();
797 $course = $this->getDataGenerator()->create_course();
798 $activity = $this->getDataGenerator()->create_module('h5pactivity',
799 ['course' => $course]);
801 $manager = manager::create_from_instance($activity);
803 // User without attempts.
804 $user1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
806 // User with 1 attempt.
807 $user2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
808 $this->generate_fake_attempts($activity, $user2, 1);
810 // User with 2 attempts.
811 $user3 = $this->getDataGenerator()->create_and_enrol($course, 'student');
812 $this->generate_fake_attempts($activity, $user3, 1);
814 $attempts = $manager->count_users_attempts();
815 $this->assertArrayNotHasKey($user1->id, $attempts);
816 $this->assertArrayHasKey($user2->id, $attempts);
817 $this->assertEquals(4, $attempts[$user2->id]);
818 $this->assertArrayHasKey($user3->id, $attempts);
819 $this->assertEquals(4, $attempts[$user3->id]);
823 * Test static get_report.
825 * @dataProvider get_report_data
826 * @param int $enabletracking if tracking is enabled
827 * @param int $reviewmode the attempt review mode
828 * @param bool $createattempts if the student have attempts
829 * @param string $role the user role (student or editingteacher)
830 * @param array $results the expected classname (or null)
832 public function test_get_report(int $enabletracking, int $reviewmode, bool $createattempts,
833 string $role, array $results) {
835 $this->resetAfterTest();
836 $this->setAdminUser();
838 $course = $this->getDataGenerator()->create_course();
839 $activity = $this->getDataGenerator()->create_module('h5pactivity',
840 ['course' => $course, 'enabletracking' => $enabletracking, 'reviewmode' => $reviewmode]);
842 $manager = manager::create_from_instance($activity);
843 $cm = get_coursemodule_from_id('h5pactivity', $activity->cmid, 0, false, MUST_EXIST);
845 $users = [
846 'editingteacher' => $this->getDataGenerator()->create_and_enrol($course, 'editingteacher'),
847 'student' => $this->getDataGenerator()->create_and_enrol($course, 'student'),
848 'otheruser' => $this->getDataGenerator()->create_and_enrol($course, 'student'),
851 $attempts = [];
852 if ($createattempts) {
853 $this->generate_fake_attempts($activity, $users['student'], 1);
854 $this->generate_fake_attempts($activity, $users['otheruser'], 2);
855 $attempts['student'] = attempt::last_attempt($users['student'], $cm);
856 $attempts['otheruser'] = attempt::last_attempt($users['otheruser'], $cm);
859 $classnamebase = 'mod_h5pactivity\\local\\report\\';
861 $attemptid = null;
862 if (isset($attempts['student'])) {
863 $attemptid = $attempts['student']->get_id() ?? null;
865 $userid = $users['student']->id;
867 // Check reports.
868 $this->setUser($users[$role]);
870 $report = $manager->get_report(null, null);
871 if ($results[0] === null) {
872 $this->assertNull($report);
873 } else {
874 $this->assertEquals($classnamebase.$results[0], get_class($report));
877 $report = $manager->get_report($userid, null);
878 if ($results[1] === null) {
879 $this->assertNull($report);
880 } else {
881 $this->assertEquals($classnamebase.$results[1], get_class($report));
884 $report = $manager->get_report($userid, $attemptid);
885 if ($results[2] === null) {
886 $this->assertNull($report);
887 } else {
888 $this->assertEquals($classnamebase.$results[2], get_class($report));
891 // Check that student cannot access another student reports.
892 if ($role == 'student') {
893 $attemptid = null;
894 if (isset($attempts['otheruser'])) {
895 $attemptid = $attempts['otheruser']->get_id() ?? null;
897 $userid = $users['otheruser']->id;
899 $report = $manager->get_report($userid, null);
900 $this->assertNull($report);
902 $report = $manager->get_report($userid, $attemptid);
903 $this->assertNull($report);
908 * Data provider for test_get_report.
910 * @return array
912 public function get_report_data(): array {
913 return [
914 // No tracking scenarios.
915 'No tracking, review none, no attempts, teacher' => [
916 0, manager::REVIEWNONE, false, 'editingteacher', [null, null, null]
918 'No tracking, review own, no attempts, teacher' => [
919 0, manager::REVIEWCOMPLETION, false, 'editingteacher', [null, null, null]
921 'No tracking, review none, no attempts, student' => [
922 0, manager::REVIEWNONE, false, 'student', [null, null, null]
924 'No tracking, review own, no attempts, student' => [
925 0, manager::REVIEWCOMPLETION, false, 'student', [null, null, null]
927 'No tracking, review none, with attempts, teacher' => [
928 0, manager::REVIEWNONE, true, 'editingteacher', [null, null, null]
930 'No tracking, review own, with attempts, teacher' => [
931 0, manager::REVIEWCOMPLETION, true, 'editingteacher', [null, null, null]
933 'No tracking, review none, with attempts, student' => [
934 0, manager::REVIEWNONE, true, 'student', [null, null, null]
936 'No tracking, review own, with attempts, student' => [
937 0, manager::REVIEWCOMPLETION, true, 'student', [null, null, null]
939 // Tracking enabled scenarios.
940 'Tracking enabled, review none, no attempts, teacher' => [
941 1, manager::REVIEWNONE, false, 'editingteacher', ['participants', 'attempts', 'attempts']
943 'Tracking enabled, review own, no attempts, teacher' => [
944 1, manager::REVIEWCOMPLETION, false, 'editingteacher', ['participants', 'attempts', 'attempts']
946 'Tracking enabled, review none, no attempts, student' => [
947 1, manager::REVIEWNONE, false, 'student', [null, null, null]
949 'Tracking enabled, review own, no attempts, student' => [
950 1, manager::REVIEWCOMPLETION, false, 'student', ['attempts', 'attempts', 'attempts']
952 'Tracking enabled, review none, with attempts, teacher' => [
953 1, manager::REVIEWNONE, true, 'editingteacher', ['participants', 'attempts', 'results']
955 'Tracking enabled, review own, with attempts, teacher' => [
956 1, manager::REVIEWCOMPLETION, true, 'editingteacher', ['participants', 'attempts', 'results']
958 'Tracking enabled, review none, with attempts, student' => [
959 1, manager::REVIEWNONE, true, 'student', [null, null, null]
961 'Tracking enabled, review own, with attempts, student' => [
962 1, manager::REVIEWCOMPLETION, true, 'student', ['attempts', 'attempts', 'results']
968 * Test get_attempt method.
970 * @dataProvider get_attempt_data
971 * @param string $attemptname the attempt to use
972 * @param string|null $result the expected attempt ID or null for none
974 public function test_get_attempt(string $attemptname, ?string $result): void {
976 $this->resetAfterTest();
977 $this->setAdminUser();
979 $course = $this->getDataGenerator()->create_course();
981 $activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]);
982 $cm = get_coursemodule_from_id('h5pactivity', $activity->cmid, 0, false, MUST_EXIST);
984 $otheractivity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]);
985 $othercm = get_coursemodule_from_id('h5pactivity', $otheractivity->cmid, 0, false, MUST_EXIST);
987 $manager = manager::create_from_instance($activity);
989 $user = $this->getDataGenerator()->create_and_enrol($course, 'student');
991 $attempts = ['inexistent' => 0];
993 $this->generate_fake_attempts($activity, $user, 1);
994 $attempt = attempt::last_attempt($user, $cm);
995 $attempts['current'] = $attempt->get_id();
997 $this->generate_fake_attempts($otheractivity, $user, 1);
998 $attempt = attempt::last_attempt($user, $othercm);
999 $attempts['other'] = $attempt->get_id();
1001 $attempt = $manager->get_attempt($attempts[$attemptname]);
1002 if ($result === null) {
1003 $this->assertNull($attempt);
1004 } else {
1005 $this->assertEquals($attempts[$attemptname], $attempt->get_id());
1006 $this->assertEquals($activity->id, $attempt->get_h5pactivityid());
1007 $this->assertEquals($user->id, $attempt->get_userid());
1008 $this->assertEquals(4, $attempt->get_attempt());
1013 * Data provider for test_get_attempt.
1015 * @return array
1017 public function get_attempt_data(): array {
1018 return [
1019 'Get the current activity attempt' => [
1020 'current', 'current'
1022 'Try to get another activity attempt' => [
1023 'other', null
1025 'Try to get an inexistent attempt' => [
1026 'inexistent', null
1032 * Insert fake attempt data into h5pactiviyt_attempts.
1034 * This function insert 4 attempts. 3 of them finished with different gradings
1035 * and timestamps and 1 unfinished.
1037 * @param stdClass $activity the activity record
1038 * @param stdClass $user user record
1039 * @param int $basescore a score to be used to generate all attempts
1041 private function generate_fake_attempts(stdClass $activity, stdClass $user, int $basescore) {
1042 global $DB;
1044 $attempt = (object)[
1045 'h5pactivityid' => $activity->id,
1046 'userid' => $user->id,
1047 'timecreated' => $basescore,
1048 'timemodified' => ($basescore + 10),
1049 'attempt' => 1,
1050 'rawscore' => $basescore,
1051 'maxscore' => ($basescore + $basescore),
1052 'duration' => $basescore,
1053 'completion' => 1,
1054 'success' => 1,
1056 $attempt->scaled = $attempt->rawscore / $attempt->maxscore;
1057 $DB->insert_record('h5pactivity_attempts', $attempt);
1059 $attempt = (object)[
1060 'h5pactivityid' => $activity->id,
1061 'userid' => $user->id,
1062 'timecreated' => ($basescore + 20),
1063 'timemodified' => ($basescore + 30),
1064 'attempt' => 2,
1065 'rawscore' => $basescore,
1066 'maxscore' => ($basescore + $basescore - 1),
1067 'duration' => $basescore,
1068 'completion' => 1,
1069 'success' => 1,
1071 $attempt->scaled = $attempt->rawscore / $attempt->maxscore;
1072 $DB->insert_record('h5pactivity_attempts', $attempt);
1074 $attempt = (object)[
1075 'h5pactivityid' => $activity->id,
1076 'userid' => $user->id,
1077 'timecreated' => ($basescore + 40),
1078 'timemodified' => ($basescore + 50),
1079 'attempt' => 3,
1080 'rawscore' => $basescore,
1081 'maxscore' => ($basescore + $basescore + 1),
1082 'duration' => $basescore,
1083 'completion' => 1,
1084 'success' => 0,
1086 $attempt->scaled = $attempt->rawscore / $attempt->maxscore;
1087 $DB->insert_record('h5pactivity_attempts', $attempt);
1089 // Unfinished attempt.
1090 $attempt = (object)[
1091 'h5pactivityid' => $activity->id,
1092 'userid' => $user->id,
1093 'timecreated' => ($basescore + 60),
1094 'timemodified' => ($basescore + 60),
1095 'attempt' => 4,
1096 'rawscore' => $basescore,
1097 'maxscore' => $basescore,
1098 'duration' => $basescore,
1100 $DB->insert_record('h5pactivity_attempts', $attempt);