Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 311 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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  namespace core;
  18  
  19  use core_grades_external;
  20  use core_external\external_api;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  global $CFG;
  25  
  26  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  27  
  28  /**
  29   * Grades functions unit tests
  30   *
  31   * Unit tests for the grade API at /lib/classes/grades_external.php
  32   *
  33   * @package core
  34   * @category test
  35   * @copyright 2012 Andrew Davis
  36   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class grades_external_test extends \externallib_advanced_testcase {
  39  
  40      /**
  41       * Load initial test information
  42       *
  43       * @param  string $assignmentname   Assignment name
  44       * @param  int $student1rawgrade    Student 1 grade
  45       * @param  int $student2rawgrade    Student 2 grade
  46       * @return array                    Array of vars with test information
  47       */
  48      protected function load_test_data($assignmentname, $student1rawgrade, $student2rawgrade) {
  49          global $DB;
  50  
  51          // Adds a course, a teacher, 2 students, an assignment and grades for the students.
  52          $course = $this->getDataGenerator()->create_course();
  53          $coursecontext = \context_course::instance($course->id);
  54  
  55          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  56  
  57          $student1 = $this->getDataGenerator()->create_user();
  58          $this->getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id);
  59  
  60          $student2 = $this->getDataGenerator()->create_user();
  61          $this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id);
  62  
  63          $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
  64          $teacher = $this->getDataGenerator()->create_user();
  65          $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
  66  
  67          $parent = $this->getDataGenerator()->create_user();
  68          $this->setUser($parent);
  69          $student1context = \context_user::instance($student1->id);
  70          // Creates a new role, gives it the capability and gives $USER that role.
  71          $parentroleid = $this->assignUserCapability('moodle/grade:viewall', $student1context->id);
  72          // Enrol the user in the course using the new role.
  73          $this->getDataGenerator()->enrol_user($parent->id, $course->id, $parentroleid);
  74  
  75          $assignment = $this->getDataGenerator()->create_module('assign', array('name' => $assignmentname, 'course' => $course->id));
  76          $modcontext = get_coursemodule_from_instance('assign', $assignment->id, $course->id);
  77          $assignment->cmidnumber = $modcontext->id;
  78  
  79          $student1grade = array('userid' => $student1->id, 'rawgrade' => $student1rawgrade);
  80          $student2grade = array('userid' => $student2->id, 'rawgrade' => $student2rawgrade);
  81          $studentgrades = array($student1->id => $student1grade, $student2->id => $student2grade);
  82          assign_grade_item_update($assignment, $studentgrades);
  83  
  84          // Insert a custom grade scale to be used by an outcome.
  85          $gradescale = new \grade_scale();
  86          $gradescale->name        = 'unittestscale3';
  87          $gradescale->courseid    = $course->id;
  88          $gradescale->userid      = 0;
  89          $gradescale->scale       = 'Distinction, Very Good, Good, Pass, Fail';
  90          $gradescale->description = 'This scale is used to mark standard assignments.';
  91          $gradescale->insert();
  92  
  93          // Insert an outcome.
  94          $data = new \stdClass();
  95          $data->courseid = $course->id;
  96          $data->fullname = 'Team work';
  97          $data->shortname = 'Team work';
  98          $data->scaleid = $gradescale->id;
  99          $outcome = new \grade_outcome($data, false);
 100          $outcome->insert();
 101  
 102          $outcomegradeitem = new \grade_item();
 103          $outcomegradeitem->itemname = $outcome->shortname;
 104          $outcomegradeitem->itemtype = 'mod';
 105          $outcomegradeitem->itemmodule = 'assign';
 106          $outcomegradeitem->iteminstance = $assignment->id;
 107          $outcomegradeitem->outcomeid = $outcome->id;
 108          $outcomegradeitem->cmid = 0;
 109          $outcomegradeitem->courseid = $course->id;
 110          $outcomegradeitem->aggregationcoef = 0;
 111          $outcomegradeitem->itemnumber = 1; // The activity's original grade item will be 0.
 112          $outcomegradeitem->gradetype = GRADE_TYPE_SCALE;
 113          $outcomegradeitem->scaleid = $outcome->scaleid;
 114          // This next two values for testing that returns parameters are correcly formatted.
 115          $outcomegradeitem->set_locked(true);
 116          $outcomegradeitem->hidden = '';
 117          $outcomegradeitem->insert();
 118  
 119          $assignmentgradeitem = \grade_item::fetch(
 120              array(
 121                  'itemtype' => 'mod',
 122                  'itemmodule' => 'assign',
 123                  'iteminstance' => $assignment->id,
 124                  'itemnumber' => 0,
 125                  'courseid' => $course->id
 126              )
 127          );
 128          $outcomegradeitem->set_parent($assignmentgradeitem->categoryid);
 129          $outcomegradeitem->move_after_sortorder($assignmentgradeitem->sortorder);
 130  
 131          return array($course, $assignment, $student1, $student2, $teacher, $parent);
 132      }
 133  
 134      /**
 135       * Test update_grades()
 136       */
 137      public function test_update_grades() {
 138          global $DB;
 139  
 140          $this->resetAfterTest(true);
 141  
 142          $assignmentname = 'The assignment';
 143          $student1rawgrade = 10;
 144          $student2rawgrade = 20;
 145          list($course, $assignment, $student1, $student2, $teacher, $parent) =
 146              $this->load_test_data($assignmentname, $student1rawgrade, $student2rawgrade);
 147          $assigmentcm = get_coursemodule_from_id('assign', $assignment->cmid, 0, false, MUST_EXIST);
 148  
 149          $this->setUser($teacher);
 150  
 151          // Teacher updating grade item information.
 152          $changedmax = 93;
 153          $result = core_grades_external::update_grades(
 154              'test',
 155              $course->id,
 156              'mod_assign',
 157              $assigmentcm->id,
 158              0,
 159              array(),
 160              array('grademax' => $changedmax)
 161          );
 162          $result = external_api::clean_returnvalue(core_grades_external::update_grades_returns(), $result);
 163          $this->assertTrue($result == GRADE_UPDATE_OK);
 164          $grades = grade_get_grades($course->id, 'mod', 'assign', $assignment->id);
 165          $this->assertTrue($grades->items[0]->grademax == $changedmax);
 166  
 167          // Teacher updating 1 student grade.
 168          $student1grade = 23;
 169          $result = core_grades_external::update_grades(
 170              'test',
 171              $course->id,
 172              'mod_assign',
 173              $assigmentcm->id,
 174              0,
 175              array(array('studentid' => $student1->id, 'grade' => $student1grade))
 176          );
 177          $result = external_api::clean_returnvalue(core_grades_external::update_grades_returns(), $result);
 178          $this->assertTrue($result == GRADE_UPDATE_OK);
 179          $grades = grade_get_grades($course->id, 'mod', 'assign', $assignment->id, array($student1->id));
 180          $this->assertTrue($grades->items[0]->grades[$student1->id]->grade == $student1grade);
 181  
 182          // Teacher updating multiple student grades.
 183          $student1grade = 11;
 184          $student2grade = 13;
 185          $result = core_grades_external::update_grades(
 186              'test',
 187              $course->id,
 188              'mod_assign',
 189              $assigmentcm->id,
 190              0,
 191              array(
 192                  array('studentid' => $student1->id, 'grade' => $student1grade),
 193                  array('studentid' => $student2->id, 'grade' => $student2grade)
 194              )
 195          );
 196          $result = external_api::clean_returnvalue(core_grades_external::update_grades_returns(), $result);
 197          $this->assertTrue($result == GRADE_UPDATE_OK);
 198          $grades = grade_get_grades($course->id, 'mod', 'assign', $assignment->id, array($student1->id, $student2->id));
 199          $this->assertTrue($grades->items[0]->grades[$student1->id]->grade == $student1grade);
 200          $this->assertTrue($grades->items[0]->grades[$student2->id]->grade == $student2grade);
 201  
 202          // Student attempting to update their own grade (should fail).
 203          $this->setUser($student1);
 204          try {
 205              $student1grade = 17;
 206              $result = core_grades_external::update_grades(
 207                  'test',
 208                  $course->id,
 209                  'mod_assign',
 210                  $assigmentcm->id,
 211                  0,
 212                  array( array('studentid' => $student1->id, 'grade' => $student1grade))
 213              );
 214              $this->fail('moodle_exception expected');
 215          } catch (\moodle_exception $ex) {
 216              $this->assertTrue(true);
 217          }
 218  
 219          // Parent attempting to update their child's grade (should fail).
 220          $this->setUser($parent);
 221          try {
 222              $student1grade = 13;
 223              $result = core_grades_external::update_grades(
 224                  'test',
 225                  $course->id,
 226                  'mod_assign',
 227                  $assigmentcm->id,
 228                  0,
 229                  array( array('studentid' => $student1->id, 'grade' => $student1grade))
 230              );
 231              $this->fail('moodle_exception expected');
 232          } catch (\moodle_exception $ex) {
 233              $this->assertTrue(true);
 234          }
 235  
 236          // Student trying to hide a grade item (should fail).
 237          $this->setUser($student1);
 238          try {
 239              $result = core_grades_external::update_grades(
 240                  'test',
 241                  $course->id,
 242                  'mod_assign',
 243                  $assigmentcm->id,
 244                  0,
 245                  array(),
 246                  array('hidden' => 1)
 247              );
 248              $this->fail('moodle_exception expected');
 249          } catch (\moodle_exception $ex) {
 250              $this->assertTrue(true);
 251          }
 252  
 253          // Give the student role 'moodle/grade:hide' and they should now be able to hide the grade item.
 254          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 255          $coursecontext = \context_course::instance($course->id);
 256          assign_capability('moodle/grade:hide', CAP_ALLOW, $studentrole->id, $coursecontext->id);
 257          accesslib_clear_all_caches_for_unit_testing();
 258  
 259          // Check the activity isn't already hidden.
 260          $grades = grade_get_grades($course->id, 'mod', 'assign', $assignment->id);
 261          $this->assertTrue($grades->items[0]->hidden == 0);
 262  
 263          $result = core_grades_external::update_grades(
 264              'test',
 265              $course->id,
 266              'mod_assign',
 267              $assigmentcm->id,
 268              0,
 269              array(),
 270              array('hidden' => 1)
 271          );
 272          $result = external_api::clean_returnvalue(core_grades_external::update_grades_returns(), $result);
 273          $this->assertTrue($result == GRADE_UPDATE_OK);
 274          $grades = grade_get_grades($course->id, 'mod', 'assign', $assignment->id);
 275          $this->assertTrue($grades->items[0]->hidden == 1);
 276      }
 277  }