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