Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   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   * Unit tests for grade/report/user/lib.php.
  19   *
  20   * @package  core_grades
  21   * @category phpunit
  22   * @copyright 2012 Andrew Davis
  23   * @license  http://www.gnu.org/copyleft/gpl.html GNU Public License
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot.'/grade/lib.php');
  30  require_once($CFG->dirroot.'/grade/report/grader/lib.php');
  31  
  32  /**
  33   * Tests grade_report_grader (the grader report)
  34   */
  35  class core_grade_report_graderlib_testcase extends advanced_testcase {
  36  
  37      /**
  38       * Tests grade_report_grader::process_data()
  39       *
  40       * process_data() processes submitted grade and feedback data
  41       */
  42      public function test_process_data() {
  43          global $DB, $CFG;
  44  
  45          $this->resetAfterTest(true);
  46  
  47          $course = $this->getDataGenerator()->create_course();
  48  
  49          // Create and enrol a student.
  50          $student = $this->getDataGenerator()->create_user(array('username' => 'student_sam'));
  51          $role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
  52          $this->getDataGenerator()->enrol_user($student->id, $course->id, $role->id);
  53  
  54          // Test with limited grades.
  55          $CFG->unlimitedgrades = 0;
  56  
  57          $forummax = 80;
  58          $forum1 = $this->getDataGenerator()->create_module('forum', array('assessed' => 1, 'scale' => $forummax, 'course' => $course->id));
  59          // Switch the stdClass instance for a grade item instance.
  60          $forum1 = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'forum', 'iteminstance' => $forum1->id, 'courseid' => $course->id));
  61  
  62          $report = $this->create_report($course);
  63          $testgrade = 60.00;
  64  
  65          $data = new stdClass();
  66          $data->id = $course->id;
  67          $data->report = 'grader';
  68          $data->timepageload = time();
  69  
  70          $data->grade = array();
  71          $data->grade[$student->id] = array();
  72          $data->grade[$student->id][$forum1->id] = $testgrade;
  73  
  74          $warnings = $report->process_data($data);
  75          $this->assertEquals(count($warnings), 0);
  76  
  77          $studentgrade = grade_grade::fetch(array('itemid' => $forum1->id, '' => $student->id));
  78          $this->assertEquals($studentgrade->finalgrade, $testgrade);
  79  
  80          // Grade above max. Should be pulled down to max.
  81          $toobig = 200.00;
  82          $data->grade[$student->id][$forum1->id] = $toobig;
  83          $data->timepageload = time();
  84          $warnings = $report->process_data($data);
  85          $this->assertEquals(count($warnings), 1);
  86  
  87          $studentgrade = grade_grade::fetch(array('itemid' => $forum1->id, '' => $student->id));
  88          $this->assertEquals($studentgrade->finalgrade, $forummax);
  89  
  90          // Grade below min. Should be pulled up to min.
  91          $toosmall = -10.00;
  92          $data->grade[$student->id][$forum1->id] = $toosmall;
  93          $data->timepageload = time();
  94          $warnings = $report->process_data($data);
  95          $this->assertEquals(count($warnings), 1);
  96  
  97          $studentgrade = grade_grade::fetch(array('itemid' => $forum1->id, '' => $student->id));
  98          $this->assertEquals($studentgrade->finalgrade, 0);
  99  
 100          // Test unlimited grades so we can give a student a grade about max.
 101          $CFG->unlimitedgrades = 1;
 102  
 103          $data->grade[$student->id][$forum1->id] = $toobig;
 104          $data->timepageload = time();
 105          $warnings = $report->process_data($data);
 106          $this->assertEquals(count($warnings), 0);
 107  
 108          $studentgrade = grade_grade::fetch(array('itemid' => $forum1->id, '' => $student->id));
 109          $this->assertEquals($studentgrade->finalgrade, $toobig);
 110      }
 111  
 112      public function test_collapsed_preferences() {
 113          $this->resetAfterTest(true);
 114  
 115          $emptypreferences = array('aggregatesonly' => array(), 'gradesonly' => array());
 116  
 117          $user1 = $this->getDataGenerator()->create_user();
 118          $course1 = $this->getDataGenerator()->create_course();
 119          $course2 = $this->getDataGenerator()->create_course();
 120          $course3 = $this->getDataGenerator()->create_course();
 121  
 122          $this->setUser($user1);
 123  
 124          $report = $this->create_report($course1);
 125          $this->assertEquals($emptypreferences, $report->collapsed);
 126  
 127          // Validating preferences set/get for one course.
 128          $report->process_action('cg13', 'switch_minus');
 129          $report = $this->create_report($course1);
 130          $this->assertEquals(array(13), $report->collapsed['aggregatesonly']);
 131          $this->assertEmpty($report->collapsed['gradesonly']);
 132  
 133          $report->process_action('cg13', 'switch_plus');
 134          $report = $this->create_report($course1);
 135          $this->assertEmpty($report->collapsed['aggregatesonly']);
 136          $this->assertEquals(array(13), $report->collapsed['gradesonly']);
 137  
 138          $report->process_action('cg13', 'switch_whole');
 139          $report = $this->create_report($course1);
 140          $this->assertEquals($emptypreferences, $report->collapsed);
 141  
 142          // Validating preferences set/get for several courses.
 143  
 144          $course1cats = $course2cats = $course3cats = array();
 145          for ($i=0;$i<10;$i++) {
 146              $course1cats[] = $this->create_grade_category($course1)->id;
 147              $course2cats[] = $this->create_grade_category($course2)->id;
 148              $course3cats[] = $this->create_grade_category($course3)->id;
 149          }
 150  
 151          $report1 = $this->create_report($course1);
 152          foreach ($course1cats as $catid) {
 153              $report1->process_action('cg'.$catid, 'switch_minus');
 154          }
 155          $report2 = $this->create_report($course2);
 156          foreach ($course2cats as $catid) {
 157              $report2->process_action('cg'.$catid, 'switch_minus');
 158              $report2->process_action('cg'.$catid, 'switch_plus');
 159          }
 160          $report3 = $this->create_report($course3);
 161          foreach ($course3cats as $catid) {
 162              $report3->process_action('cg'.$catid, 'switch_minus');
 163              if (($i++)%2) {
 164                  $report3->process_action('cg'.$catid, 'switch_plus');
 165              }
 166          }
 167  
 168          $report1 = $this->create_report($course1);
 169          $this->assertEquals(10, count($report1->collapsed['aggregatesonly']));
 170          $this->assertEquals(0, count($report1->collapsed['gradesonly']));
 171          $report2 = $this->create_report($course2);
 172          $this->assertEquals(0, count($report2->collapsed['aggregatesonly']));
 173          $this->assertEquals(10, count($report2->collapsed['gradesonly']));
 174          $report3 = $this->create_report($course3);
 175          $this->assertEquals(5, count($report3->collapsed['aggregatesonly']));
 176          $this->assertEquals(5, count($report3->collapsed['gradesonly']));
 177  
 178          // Test upgrade script.
 179          // Combine data generated for user1 and set it in the old format for user2, Try to retrieve it and make sure it is converted.
 180  
 181          $user2 = $this->getDataGenerator()->create_user();
 182          $alldata = array(
 183              'aggregatesonly' => array_merge($report1->collapsed['aggregatesonly'], $report2->collapsed['aggregatesonly'], $report3->collapsed['aggregatesonly']),
 184              'gradesonly' => array_merge($report1->collapsed['gradesonly'], $report2->collapsed['gradesonly'], $report3->collapsed['gradesonly']),
 185          );
 186          set_user_preference('grade_report_grader_collapsed_categories', serialize($alldata), $user2);
 187  
 188          $this->setUser($user2);
 189          $convertedreport1 = $this->create_report($course1);
 190          $this->assertEquals($report1->collapsed, $convertedreport1->collapsed);
 191          $convertedreport2 = $this->create_report($course2);
 192          $this->assertEquals($report2->collapsed, $convertedreport2->collapsed);
 193          $convertedreport3 = $this->create_report($course3);
 194          $this->assertEquals($report3->collapsed, $convertedreport3->collapsed);
 195          // Make sure the old style user preference is removed now.
 196          $this->assertEmpty(get_user_preferences('grade_report_grader_collapsed_categories'));
 197  
 198          // Test overflowing the setting with non-existing categories (only validated if new setting size exceeds 1333 chars).
 199  
 200          $toobigvalue = $expectedvalue = $report1->collapsed;
 201          for ($i = 0; strlen(json_encode($toobigvalue)) < 1333; $i++) {
 202              $toobigvalue[($i < 7) ? 'gradesonly' : 'aggregatesonly'][] = $course1cats[9] + 1 + $i;
 203          }
 204          $lastvalue = array_pop($toobigvalue['gradesonly']);
 205          set_user_preference('grade_report_grader_collapsed_categories'.$course1->id, json_encode($toobigvalue));
 206  
 207          $report1 = $this->create_report($course1);
 208          $report1->process_action('cg'.$lastvalue, 'switch_minus');
 209  
 210          $report1 = $this->create_report($course1);
 211          $this->assertEquals($expectedvalue, $report1->collapsed);
 212  
 213          // Test overflowing the setting with existing categories.
 214  
 215          $toobigvalue = $report1->collapsed;
 216          for ($i = 0; strlen(json_encode($toobigvalue)) < 1333; $i++) {
 217              $catid = $this->create_grade_category($course1)->id;
 218              $toobigvalue[($i < 7) ? 'gradesonly' : 'aggregatesonly'][] = $catid;
 219          }
 220          $lastcatid = array_pop($toobigvalue['gradesonly']);
 221          set_user_preference('grade_report_grader_collapsed_categories'.$course1->id, json_encode($toobigvalue));
 222          $toobigvalue['aggregatesonly'][] = $lastcatid;
 223  
 224          $report1 = $this->create_report($course1);
 225          $report1->process_action('cg'.$lastcatid, 'switch_minus');
 226  
 227          // One last value should be removed from both arrays.
 228          $report1 = $this->create_report($course1);
 229          $this->assertEquals(count($toobigvalue['aggregatesonly']) - 1, count($report1->collapsed['aggregatesonly']));
 230          $this->assertEquals(count($toobigvalue['gradesonly']) - 1, count($report1->collapsed['gradesonly']));
 231      }
 232  
 233      /**
 234       * Tests the get_right_rows function with one 'normal' and one 'ungraded' quiz.
 235       *
 236       * Previously, with an ungraded quiz (which results in a grade item with type GRADETYPE_NONE)
 237       * there was a bug in get_right_rows in some situations.
 238       */
 239      public function test_get_right_rows() {
 240          global $USER, $DB;
 241          $this->resetAfterTest(true);
 242  
 243          // Create manager and student on a course.
 244          $generator = $this->getDataGenerator();
 245          $manager = $generator->create_user();
 246          $student = $generator->create_user();
 247          $course = $generator->create_course();
 248          $generator->enrol_user($manager->id, $course->id, 'manager');
 249          $generator->enrol_user($student->id, $course->id, 'student');
 250  
 251          // Create a couple of quizzes on the course.
 252          $normalquiz = $generator->create_module('quiz', array('course' => $course->id,
 253                  'name' => 'NormalQuiz'));
 254          $ungradedquiz = $generator->create_module('quiz', array('course' => $course->id,
 255                  'name' => 'UngradedQuiz'));
 256  
 257          // Set the grade for the second one to 0 (note, you have to do this after creating it,
 258          // otherwise it doesn't create an ungraded grade item).
 259          $ungradedquiz->instance = $ungradedquiz->id;
 260          quiz_set_grade(0, $ungradedquiz);
 261  
 262          // Set current user.
 263          $this->setUser($manager);
 264          $USER->gradeediting[$course->id] = false;
 265  
 266          // Get the report.
 267          $report = $this->create_report($course);
 268          $report->load_users();
 269          $report->load_final_grades();
 270          $result = $report->get_right_rows(false);
 271  
 272          // There should be 3 rows (2 header rows, plus the grades for the first user).
 273          $this->assertCount(3, $result);
 274  
 275          // The second row should contain 2 cells - one for the graded quiz and course total.
 276          $this->assertCount(2, $result[1]->cells);
 277          $this->assertStringContainsString('NormalQuiz', $result[1]->cells[0]->text);
 278          $this->assertStringContainsString('Course total', $result[1]->cells[1]->text);
 279  
 280          // User row should contain grade values '-'.
 281          $this->assertCount(2, $result[2]->cells);
 282          $this->assertStringContainsString('>-<', $result[2]->cells[0]->text);
 283          $this->assertStringContainsString('>-<', $result[2]->cells[1]->text);
 284  
 285          // Supposing the user cannot view hidden grades, this shouldn't make any difference (due
 286          // to a bug, it previously did).
 287          $context = context_course::instance($course->id);
 288          $managerroleid = $DB->get_field('role', 'id', array('shortname' => 'manager'));
 289          assign_capability('moodle/grade:viewhidden', CAP_PROHIBIT, $managerroleid, $context->id, true);
 290          $this->assertFalse(has_capability('moodle/grade:viewhidden', $context));
 291  
 292          // Recreate the report. Confirm it returns successfully still.
 293          $report = $this->create_report($course);
 294          $report->load_users();
 295          $report->load_final_grades();
 296          $result = $report->get_right_rows(false);
 297          $this->assertCount(3, $result);
 298      }
 299  
 300      private function create_grade_category($course) {
 301          static $cnt = 0;
 302          $cnt++;
 303          $grade_category = new grade_category(array('courseid' => $course->id, 'fullname' => 'Cat '.$cnt), false);
 304          $grade_category->apply_default_settings();
 305          $grade_category->apply_forced_settings();
 306          $grade_category->insert();
 307          return $grade_category;
 308      }
 309  
 310      private function create_report($course) {
 311  
 312          $coursecontext = context_course::instance($course->id);
 313          $gpr = new grade_plugin_return(array('type' => 'report', 'plugin'=>'grader', 'courseid' => $course->id));
 314          $report = new grade_report_grader($course->id, $gpr, $coursecontext);
 315  
 316          return $report;
 317      }
 318  }