Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

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