See Release Notes
Long Term Support Release
Differences Between: [Versions 401 and 402] [Versions 401 and 403]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 18 namespace mod_h5pactivity; 19 20 use advanced_testcase; 21 use mod_h5pactivity\local\manager; 22 23 /** 24 * Unit tests for (some of) mod/h5pactivity/lib.php. 25 * 26 * @copyright 2021 Ilya Tregubov <ilya@moodle.com> 27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 28 */ 29 class lib_test extends advanced_testcase { 30 31 /** 32 * Load required test libraries 33 */ 34 public static function setUpBeforeClass(): void { 35 global $CFG; 36 require_once("{$CFG->dirroot}/mod/h5pactivity/lib.php"); 37 } 38 39 /** 40 * Test that assign_print_recent_activity shows ungraded submitted assignments. 41 * 42 * @covers ::h5pactivity_print_recent_activity 43 */ 44 public function test_print_recent_activity() { 45 $this->resetAfterTest(); 46 $this->setAdminUser(); 47 48 $course = $this->getDataGenerator()->create_course(); 49 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 50 $activity = $this->getDataGenerator()->create_module('h5pactivity', 51 ['course' => $course, 'enabletracking' => 1, 'grademethod' => manager::GRADEHIGHESTATTEMPT]); 52 53 $generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity'); 54 55 $manager = manager::create_from_instance($activity); 56 $cm = $manager->get_coursemodule(); 57 58 $user = $student; 59 $params = ['cmid' => $cm->id, 'userid' => $user->id]; 60 $generator->create_content($activity, $params); 61 $this->setUser($student); 62 $this->expectOutputRegex('/submitted:/'); 63 h5pactivity_print_recent_activity($course, true, time() - 3600); 64 } 65 66 /** 67 * Test that h5pactivity_print_recent_activity does not display any warnings when a custom fullname has been configured. 68 * 69 * @covers ::h5pactivity_print_recent_activity 70 */ 71 public function test_print_recent_activity_fullname() { 72 $this->resetAfterTest(); 73 $this->setAdminUser(); 74 75 $course = $this->getDataGenerator()->create_course(); 76 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher'); 77 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 78 $activity = $this->getDataGenerator()->create_module('h5pactivity', 79 ['course' => $course, 'enabletracking' => 1, 'grademethod' => manager::GRADEHIGHESTATTEMPT]); 80 81 $generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity'); 82 83 $manager = manager::create_from_instance($activity); 84 $cm = $manager->get_coursemodule(); 85 86 $user = $student; 87 $params = ['cmid' => $cm->id, 'userid' => $user->id]; 88 $generator->create_content($activity, $params); 89 90 $this->setUser($teacher); 91 92 $this->expectOutputRegex('/submitted:/'); 93 set_config('fullnamedisplay', 'firstname, lastnamephonetic'); 94 h5pactivity_print_recent_activity($course, false, time() - 3600); 95 } 96 97 /** 98 * Test that h5pactivity_get_recent_mod_activity fetches the h5pactivity correctly. 99 * 100 * @covers ::h5pactivity_get_recent_mod_activity 101 */ 102 public function test_h5pactivity_get_recent_mod_activity() { 103 $this->resetAfterTest(); 104 $this->setAdminUser(); 105 106 $course = $this->getDataGenerator()->create_course(); 107 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher'); 108 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 109 $activity = $this->getDataGenerator()->create_module('h5pactivity', 110 ['course' => $course, 'enabletracking' => 1, 'grademethod' => manager::GRADEHIGHESTATTEMPT]); 111 112 $generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity'); 113 114 $manager = manager::create_from_instance($activity); 115 $cm = $manager->get_coursemodule(); 116 117 $user = $student; 118 $params = ['cmid' => $cm->id, 'userid' => $user->id]; 119 $generator->create_content($activity, $params); 120 121 $index = 1; 122 $activities = [ 123 $index => (object) [ 124 'type' => 'h5pactivity', 125 'cmid' => $cm->id, 126 ], 127 ]; 128 129 $this->setUser($teacher); 130 h5pactivity_get_recent_mod_activity($activities, $index, time() - HOURSECS, $course->id, $cm->id); 131 132 $activity = $activities[1]; 133 $this->assertEquals("h5pactivity", $activity->type); 134 $this->assertEquals($student->id, $activity->user->id); 135 } 136 137 /** 138 * Test that h5pactivity_get_recent_mod_activity fetches activity correctly. 139 * 140 * @covers ::h5pactivity_fetch_recent_activity 141 */ 142 public function test_h5pactivity_fetch_recent_activity() { 143 global $DB; 144 145 $this->resetAfterTest(); 146 $this->setAdminUser(); 147 148 $course = $this->getDataGenerator()->create_course(); 149 150 // Create users and groups. 151 $students = array(); 152 $groups = array(); 153 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher'); 154 155 for ($i = 1; $i < 4; $i++) { 156 $students[$i] = $this->getDataGenerator()->create_and_enrol($course, 'student'); 157 $groups[$i] = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); 158 } 159 160 // Update the course set the groupmode SEPARATEGROUPS and forced. 161 update_course((object)array('id' => $course->id, 'groupmode' => SEPARATEGROUPS, 'groupmodeforce' => true)); 162 163 // Student 1 is in groups 1 and 3. 164 groups_add_member($groups[1], $students[1]); 165 groups_add_member($groups[3], $students[1]); 166 167 // Student 2 is in groups 1 and 2. 168 groups_add_member($groups[1], $students[2]); 169 groups_add_member($groups[2], $students[2]); 170 171 // Student 3 is only in group 3. 172 groups_add_member($groups[3], $students[3]); 173 174 // Grader is only in group 3. 175 groups_add_member($groups[3], $teacher); 176 177 $timestart = time() - 1; 178 // Create h5pactivity. 179 $activity = $this->getDataGenerator()->create_module('h5pactivity', 180 ['course' => $course->id, 'enabletracking' => 1, 'grademethod' => manager::GRADEHIGHESTATTEMPT, 181 'groupmode' => SEPARATEGROUPS]); 182 183 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); 184 $cmcontext = \context_module::instance($activity->cmid); 185 assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $teacherrole->id, $cmcontext, true); 186 187 $manager = manager::create_from_instance($activity); 188 $cm = $manager->get_coursemodule(); 189 190 // Create attempts. 191 $generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity'); 192 foreach ($students as $student) { 193 $params = ['cmid' => $cm->id, 'userid' => $student->id]; 194 $generator->create_content($activity, $params); 195 } 196 197 // Get all attempts. 198 $dbparams = [$timestart, $course->id, 'h5pactivity']; 199 $userfieldsapi = \core_user\fields::for_userpic(); 200 $namefields = $userfieldsapi->get_sql('u', false, '', 'userid', false)->selects;; 201 202 $sql = "SELECT h5pa.id, h5pa.timemodified, cm.id as cmid, $namefields 203 FROM {h5pactivity_attempts} h5pa 204 JOIN {h5pactivity} h5p ON h5p.id = h5pa.h5pactivityid 205 JOIN {course_modules} cm ON cm.instance = h5p.id 206 JOIN {modules} md ON md.id = cm.module 207 JOIN {user} u ON u.id = h5pa.userid 208 WHERE h5pa.timemodified > ? 209 AND h5p.course = ? 210 AND md.name = ? 211 ORDER BY h5pa.timemodified ASC"; 212 213 $submissions = $DB->get_records_sql($sql, $dbparams); 214 $this->assertCount(count($students), $submissions); 215 216 // Fetch activity for student (only his own). 217 $this->setUser($students[1]); 218 $recentactivity = h5pactivity_fetch_recent_activity($submissions, $course->id); 219 $this->assertCount(1, $recentactivity); 220 $this->assertEquals($students[1]->id, $recentactivity[$students[1]->id]->userid); 221 222 // Fetch users group info for grader. 223 $this->setUser($teacher); 224 $recentactivity = h5pactivity_fetch_recent_activity($submissions, $course->id); 225 $this->assertCount(2, $recentactivity); 226 // Grader, Student 1 and 3 are in Group 3. 227 $this->assertEquals($students[1]->id, $recentactivity[$students[1]->id]->userid); 228 $this->assertEquals($students[3]->id, $recentactivity[$students[3]->id]->userid); 229 230 // Grader is in Group 2. 231 groups_remove_member($groups[3], $teacher); 232 groups_add_member($groups[2], $teacher); 233 get_fast_modinfo($course->id, 0, true); 234 $recentactivity = h5pactivity_fetch_recent_activity($submissions, $course->id); 235 $this->assertCount(1, $recentactivity); 236 // Grader, Student 2 are in Group 2. 237 $this->assertEquals($students[2]->id, $recentactivity[$students[2]->id]->userid); 238 239 // Grader is in Group 1. 240 groups_remove_member($groups[2], $teacher); 241 groups_add_member($groups[1], $teacher); 242 get_fast_modinfo($course->id, 0, true); 243 $recentactivity = h5pactivity_fetch_recent_activity($submissions, $course->id); 244 $this->assertCount(2, $recentactivity); 245 // Grader, Student 1 and 2 are in Group 1. 246 $this->assertEquals($students[1]->id, $recentactivity[$students[1]->id]->userid); 247 $this->assertEquals($students[2]->id, $recentactivity[$students[2]->id]->userid); 248 } 249 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body