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]

   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_grades\external;
  18  
  19  use core_grades\external\create_gradecategories;
  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   * Unit tests for the core_grades\external\create_gradecategories webservice.
  30   *
  31   * @package    core_grades
  32   * @category   external
  33   * @copyright  2021 Peter Burnett <peterburnett@catalyst-au.net>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   * @since      Moodle 3.11
  36   */
  37  class create_gradecategories_test extends \externallib_advanced_testcase {
  38  
  39      /**
  40       * Test create_gradecategories.
  41       *
  42       * @return void
  43       */
  44      public function test_create_gradecategories() {
  45          global $DB;
  46          $this->resetAfterTest(true);
  47          $course = $this->getDataGenerator()->create_course();
  48          $this->setAdminUser();
  49  
  50          // Test the most basic gradecategory creation.
  51          $status1 = create_gradecategories::execute($course->id,
  52              [['fullname' => 'Test Category 1', 'options' => []]]);
  53          $status1 = external_api::clean_returnvalue(create_gradecategories::execute_returns(), $status1);
  54  
  55          $courseparentcat = \grade_category::fetch_course_category($course->id);
  56          $record1 = $DB->get_record('grade_categories', ['id' => $status1['categoryids'][0]]);
  57          $this->assertEquals('Test Category 1', $record1->fullname);
  58          // Confirm that the parent category for this category is the top level category for the course.
  59          $this->assertEquals($courseparentcat->id, $record1->parent);
  60          $this->assertEquals(2, $record1->depth);
  61  
  62          // Now create a category as a child of the newly created category.
  63          $status2 = create_gradecategories::execute($course->id,
  64              [['fullname' => 'Test Category 2', 'options' => ['parentcategoryid' => $record1->id]]]);
  65          $status2 = external_api::clean_returnvalue(create_gradecategories::execute_returns(), $status2);
  66          $record2 = $DB->get_record('grade_categories', ['id' => $status2['categoryids'][0]]);
  67          $this->assertEquals($record1->id, $record2->parent);
  68          $this->assertEquals(3, $record2->depth);
  69          // Check the path is correct.
  70          $this->assertEquals('/' . implode('/', [$courseparentcat->id, $record1->id, $record2->id]) . '/', $record2->path);
  71  
  72          // Now create a category with some customised data and check the returns. This customises every value.
  73          $customopts = [
  74              'aggregation' => GRADE_AGGREGATE_MEAN,
  75              'aggregateonlygraded' => 0,
  76              'aggregateoutcomes' => 1,
  77              'droplow' => 1,
  78              'itemname' => 'item',
  79              'iteminfo' => 'info',
  80              'idnumber' => 'idnumber',
  81              'gradetype' => GRADE_TYPE_TEXT,
  82              'grademax' => 5,
  83              'grademin' => 2,
  84              'gradepass' => 3,
  85              'display' => GRADE_DISPLAY_TYPE_LETTER,
  86              // Hack. This must be -2 to use the default setting.
  87              'decimals' => 3,
  88              'hiddenuntil' => time(),
  89              'locktime' => time(),
  90              'weightoverride' => 1,
  91              'aggregationcoef2' => 20,
  92              'parentcategoryid' => $record2->id
  93          ];
  94  
  95          $status3 = create_gradecategories::execute($course->id,
  96              [['fullname' => 'Test Category 3', 'options' => $customopts]]);
  97          $status3 = external_api::clean_returnvalue(create_gradecategories::execute_returns(), $status3);
  98          $cat3 = new \grade_category(['courseid' => $course->id, 'id' => $status3['categoryids'][0]], true);
  99          $cat3->load_grade_item();
 100  
 101          // Lets check all of the data is in the right shape.
 102          $this->assertEquals(GRADE_AGGREGATE_MEAN, $cat3->aggregation);
 103          $this->assertEquals(0, $cat3->aggregateonlygraded);
 104          $this->assertEquals(1, $cat3->aggregateoutcomes);
 105          $this->assertEquals(1, $cat3->droplow);
 106          $this->assertEquals('item', $cat3->grade_item->itemname);
 107          $this->assertEquals('info', $cat3->grade_item->iteminfo);
 108          $this->assertEquals('idnumber', $cat3->grade_item->idnumber);
 109          $this->assertEquals(GRADE_TYPE_TEXT, $cat3->grade_item->gradetype);
 110          $this->assertEquals(5, $cat3->grade_item->grademax);
 111          $this->assertEquals(2, $cat3->grade_item->grademin);
 112          $this->assertEquals(3, $cat3->grade_item->gradepass);
 113          $this->assertEquals(GRADE_DISPLAY_TYPE_LETTER, $cat3->grade_item->display);
 114          $this->assertEquals(3, $cat3->grade_item->decimals);
 115          $this->assertGreaterThanOrEqual($cat3->grade_item->hidden, time());
 116          $this->assertGreaterThanOrEqual($cat3->grade_item->locktime, time());
 117          $this->assertEquals(1, $cat3->grade_item->weightoverride);
 118          // Coefficient is converted to percentage.
 119          $this->assertEquals(0.2, $cat3->grade_item->aggregationcoef2);
 120          $this->assertEquals($record2->id, $cat3->parent);
 121  
 122          // Now test creating 2 in parallel, and nesting them.
 123          $status4 = create_gradecategories::execute($course->id, [
 124              [
 125                  'fullname' => 'Test Category 4',
 126                  'options' => [
 127                      'idnumber' => 'secondlevel'
 128                  ],
 129              ],
 130              [
 131                  'fullname' => 'Test Category 5',
 132                  'options' => [
 133                      'idnumber' => 'thirdlevel',
 134                      'parentcategoryidnumber' => 'secondlevel'
 135                  ],
 136              ],
 137          ]);
 138          $status4 = external_api::clean_returnvalue(create_gradecategories::execute_returns(), $status4);
 139  
 140          $secondlevel = $DB->get_record('grade_categories', ['id' => $status4['categoryids'][0]]);
 141          $thirdlevel = $DB->get_record('grade_categories', ['id' => $status4['categoryids'][1]]);
 142  
 143          // Confirm that the parent category for secondlevel is the top level category for the course.
 144          $this->assertEquals($courseparentcat->id, $secondlevel->parent);
 145          $this->assertEquals(2, $record1->depth);
 146  
 147          // Confirm that the parent category for thirdlevel is the secondlevel category.
 148          $this->assertEquals($secondlevel->id, $thirdlevel->parent);
 149          $this->assertEquals(3, $thirdlevel->depth);
 150          // Check the path is correct.
 151          $this->assertEquals('/' . implode('/', [$courseparentcat->id, $secondlevel->id, $thirdlevel->id]) . '/', $thirdlevel->path);
 152      }
 153  }