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 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   * Tests the \core\event\user_graded event.
  19   *
  20   * @package    core
  21   * @category   phpunit
  22   * @copyright  2014 Petr Skoda
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  
  30  require_once($CFG->libdir . '/mathslib.php');
  31  
  32  /**
  33   * Class core_event_user_graded_testcase
  34   *
  35   * Tests for event \core\event\user_graded
  36   *
  37   * @package    core
  38   * @category   test
  39   * @copyright  2014 Petr Skoda
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class core_event_user_graded_testcase extends advanced_testcase {
  43  
  44      /**
  45       * Tests set up.
  46       */
  47      public function setUp(): void {
  48          $this->resetAfterTest();
  49      }
  50  
  51      /**
  52       * Tests the event details.
  53       */
  54      public function test_event() {
  55          global $CFG;
  56          require_once("$CFG->libdir/gradelib.php");
  57  
  58          $course = $this->getDataGenerator()->create_course();
  59          $user = $this->getDataGenerator()->create_user();
  60          $this->getDataGenerator()->enrol_user($user->id, $course->id);
  61  
  62          $grade_category = grade_category::fetch_course_category($course->id);
  63          $grade_category->load_grade_item();
  64          $grade_item = $grade_category->grade_item;
  65  
  66          $grade_item->update_final_grade($user->id, 10, 'gradebook');
  67  
  68          $grade_grade = new grade_grade(array('userid' => $user->id, 'itemid' => $grade_item->id), true);
  69          $grade_grade->grade_item = $grade_item;
  70  
  71          $event = \core\event\user_graded::create_from_grade($grade_grade);
  72  
  73          $this->assertEventLegacyLogData(
  74              array($course->id, 'grade', 'update', '/report/grader/index.php?id=' . $course->id, $grade_item->itemname . ': ' . fullname($user)),
  75              $event
  76          );
  77          $this->assertEquals(context_course::instance($course->id), $event->get_context());
  78          $this->assertSame($event->objecttable, 'grade_grades');
  79          $this->assertEquals($event->objectid, $grade_grade->id);
  80          $this->assertEquals($event->other['itemid'], $grade_item->id);
  81          $this->assertTrue($event->other['overridden']);
  82          $this->assertEquals(10, $event->other['finalgrade']);
  83  
  84          // Trigger the events.
  85          $sink = $this->redirectEvents();
  86          $event->trigger();
  87          $result = $sink->get_events();
  88          $sink->close();
  89  
  90          $this->assertCount(1, $result);
  91  
  92          $event = reset($result);
  93          $this->assertEventContextNotUsed($event);
  94  
  95          $grade = $event->get_grade();
  96          $this->assertInstanceOf('grade_grade', $grade);
  97          $this->assertEquals($grade_grade->id, $grade->id);
  98      }
  99  
 100      /**
 101       * Tests that the event is fired in the correct locations in core.
 102       */
 103      public function test_event_is_triggered() {
 104          global $DB;
 105  
 106          // Create the items we need to test with.
 107          $course = $this->getDataGenerator()->create_course();
 108          $user = $this->getDataGenerator()->create_user();
 109          $this->getDataGenerator()->enrol_user($user->id, $course->id);
 110          $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
 111          $quizitemparams = array('itemtype' => 'mod', 'itemmodule' => 'quiz', 'iteminstance' => $quiz->id,
 112              'courseid' => $course->id);
 113          $gradeitem = grade_item::fetch($quizitemparams);
 114          $courseitem = grade_item::fetch_course_item($course->id);
 115  
 116          // Now mark the quiz using grade_update as this is the function that modules use.
 117          $grade = array();
 118          $grade['userid'] = $user->id;
 119          $grade['rawgrade'] = 60;
 120  
 121          $sink = $this->redirectEvents();
 122          grade_update('mod/quiz', $course->id, 'mod', 'quiz', $quiz->id, 0, $grade);
 123          $events = $sink->get_events();
 124          $sink->close();
 125  
 126          // Ensure we have two user_graded events, one for the item, one for the course.
 127          $this->assertEquals(2, count($events));
 128          $this->assertInstanceOf('\core\event\user_graded', $events[0]);
 129          $this->assertEquals($gradeitem->id, $events[0]->other['itemid']);
 130          $this->assertInstanceOf('\core\event\user_graded', $events[1]);
 131          $this->assertEquals($courseitem->id, $events[1]->other['itemid']);
 132  
 133          // Remove the grades, force the regrading and re-fetch the item. This is needed because the item
 134          // will be set as needing an update when the grades are deleted.
 135          $gradeitem->delete_all_grades();
 136          grade_regrade_final_grades($course->id);
 137          $gradeitem = grade_item::fetch($quizitemparams);
 138  
 139          // Now, create a grade using grade_item::update_final_grade().
 140          $sink = $this->redirectEvents();
 141          $gradeitem->update_raw_grade($user->id, 10);
 142          $events = $sink->get_events();
 143          $sink->close();
 144  
 145          // Ensure we have two user_graded events, one for the item, one for the course.
 146          $this->assertEquals(2, count($events));
 147          $this->assertInstanceOf('\core\event\user_graded', $events[0]);
 148          $this->assertEquals($gradeitem->id, $events[0]->other['itemid']);
 149          $this->assertInstanceOf('\core\event\user_graded', $events[1]);
 150          $this->assertEquals($courseitem->id, $events[1]->other['itemid']);
 151  
 152          // Now, update this grade using grade_item::update_raw_grade().
 153          $sink = $this->redirectEvents();
 154          $gradeitem->update_raw_grade($user->id, 20);
 155          $events = $sink->get_events();
 156          $sink->close();
 157  
 158          // Ensure we have two user_graded events, one for the item, one for the course.
 159          $this->assertEquals(2, count($events));
 160          $this->assertInstanceOf('\core\event\user_graded', $events[0]);
 161          $this->assertEquals($gradeitem->id, $events[0]->other['itemid']);
 162          $this->assertInstanceOf('\core\event\user_graded', $events[1]);
 163          $this->assertEquals($courseitem->id, $events[1]->other['itemid']);
 164  
 165          // Remove the grades, force the regrading and re-fetch the item. This is needed because the item
 166          // will be set as needing an update when the grades are deleted.
 167          $gradeitem->delete_all_grades();
 168          grade_regrade_final_grades($course->id);
 169          $gradeitem = grade_item::fetch($quizitemparams);
 170  
 171          // Now, create a grade using grade_item::update_final_grade().
 172          $sink = $this->redirectEvents();
 173          $gradeitem->update_final_grade($user->id, 30);
 174          $events = $sink->get_events();
 175          $sink->close();
 176  
 177          // Ensure we have two user_graded events, one for the item, one for the course.
 178          $this->assertEquals(2, count($events));
 179          $this->assertInstanceOf('\core\event\user_graded', $events[0]);
 180          $this->assertEquals($gradeitem->id, $events[0]->other['itemid']);
 181          $this->assertInstanceOf('\core\event\user_graded', $events[1]);
 182          $this->assertEquals($courseitem->id, $events[1]->other['itemid']);
 183  
 184          // Now, update this grade using grade_item::update_final_grade().
 185          $sink = $this->redirectEvents();
 186          $gradeitem->update_final_grade($user->id, 40);
 187          $events = $sink->get_events();
 188          $sink->close();
 189  
 190          // Ensure we have two user_graded events, one for the item, one for the course.
 191          $this->assertEquals(2, count($events));
 192          $this->assertInstanceOf('\core\event\user_graded', $events[0]);
 193          $this->assertEquals($gradeitem->id, $events[0]->other['itemid']);
 194          $this->assertInstanceOf('\core\event\user_graded', $events[1]);
 195          $this->assertEquals($courseitem->id, $events[1]->other['itemid']);
 196  
 197          // Remove the overridden flag from the grade, this was set by grade_item::update_final_grade().
 198          $gradegrade = grade_grade::fetch(array('itemid' => $gradeitem->id, 'userid' => $user->id));
 199          $gradegrade->set_overridden(false, false);
 200  
 201          // Let's change the calculation to anything that won't cause an error.
 202          $calculation = calc_formula::unlocalize("=3");
 203          $gradeitem->set_calculation($calculation);
 204  
 205          // Now force the computation of the grade.
 206          $sink = $this->redirectEvents();
 207          grade_regrade_final_grades($course->id);
 208          $events = $sink->get_events();
 209          $sink->close();
 210  
 211          // Ensure we have two user_graded events, one for the item, one for the course.
 212          $this->assertEquals(2, count($events));
 213          $this->assertInstanceOf('\core\event\user_graded', $events[0]);
 214          $this->assertEquals($gradeitem->id, $events[0]->other['itemid']);
 215          $this->assertInstanceOf('\core\event\user_graded', $events[1]);
 216          $this->assertEquals($courseitem->id, $events[1]->other['itemid']);
 217  
 218          // Now, let's trick the gradebook, we manually update a grade, and flag the grade item as
 219          // needing a regrading, so we can trigger the event in grade_item::regrade_final_grades().
 220          $gradeitem = grade_item::fetch($quizitemparams);
 221          $gradeitem->set_calculation('');
 222          $gradegrade = grade_grade::fetch(array('itemid' => $gradeitem->id, 'userid' => $user->id));
 223          $gradegrade->rawgrade = 50;
 224          $gradegrade->update();
 225  
 226          $sink = $this->redirectEvents();
 227          grade_regrade_final_grades($course->id);
 228          $events = $sink->get_events();
 229          $sink->close();
 230  
 231          // Ensure we have two user_graded events, one for the item, one for the course.
 232          $this->assertEquals(2, count($events));
 233          $this->assertInstanceOf('\core\event\user_graded', $events[0]);
 234          $this->assertEquals($gradeitem->id, $events[0]->other['itemid']);
 235          $this->assertInstanceOf('\core\event\user_graded', $events[1]);
 236          $this->assertEquals($courseitem->id, $events[1]->other['itemid']);
 237      }
 238  }