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 310 and 403] [Versions 311 and 403] [Versions 39 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  /**
  18   * @package    core_grades
  19   * @category   phpunit
  20   * @copyright  nicolas@moodle.com
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  defined('MOODLE_INTERNAL') || die();
  25  
  26  global $CFG;
  27  require_once($CFG->libdir . '/adminlib.php');
  28  require_once($CFG->libdir . '/gradelib.php');
  29  
  30  
  31  /**
  32   * Shared code for all grade related tests.
  33   *
  34   * Here is a brief explanation of the test data set up in these unit tests.
  35   * category1 => array(category2 => array(grade_item1, grade_item2), category3 => array(grade_item3))
  36   * 3 users for 3 grade_items
  37   */
  38  abstract class grade_base_testcase extends advanced_testcase {
  39  
  40      protected $course;
  41      protected $activities = array();
  42      protected $grade_items = array();
  43      protected $grade_categories = array();
  44      protected $grade_grades = array();
  45      protected $grade_outcomes = array();
  46      protected $scale = array();
  47      protected $scalemax = array();
  48  
  49      protected $courseid;
  50      protected $userid;
  51  
  52      /** @var array user object collection. */
  53      protected $user = [];
  54  
  55      /** @var array module object collection. */
  56      protected $course_module = [];
  57  
  58      protected function setUp(): void {
  59          global $CFG;
  60          parent::setup();
  61  
  62          $this->resetAfterTest(true);
  63  
  64          $CFG->grade_droplow = -1;
  65          $CFG->grade_keephigh = -1;
  66          $CFG->grade_aggregation = -1;
  67          $CFG->grade_aggregateonlygraded = -1;
  68          $CFG->grade_aggregateoutcomes = -1;
  69  
  70          $this->course = $this->getDataGenerator()->create_course();
  71          $this->courseid = $this->course->id;
  72  
  73          $this->user[0] = $this->getDataGenerator()->create_user();
  74          $this->user[1] = $this->getDataGenerator()->create_user();
  75          $this->user[2] = $this->getDataGenerator()->create_user();
  76          $this->user[3] = $this->getDataGenerator()->create_user();
  77          $this->userid = $this->user[0]->id;
  78  
  79          $this->load_modules();
  80  
  81          $this->load_scales();
  82          $this->load_grade_categories();
  83          $this->load_grade_items();
  84          $this->load_grade_grades();
  85          $this->load_grade_outcomes();
  86      }
  87  
  88      private function load_modules() {
  89          $this->activities[0] = $this->getDataGenerator()->create_module('assign', array('course'=>$this->course->id));
  90          $this->course_module[0] = get_coursemodule_from_instance('assign', $this->activities[0]->id);
  91  
  92          $this->activities[1] = $this->getDataGenerator()->create_module('assign', array('course'=>$this->course->id));
  93          $this->course_module[1] = get_coursemodule_from_instance('assign', $this->activities[1]->id);
  94  
  95          $this->activities[2] = $this->getDataGenerator()->create_module('forum', array('course'=>$this->course->id));
  96          $this->course_module[2] = get_coursemodule_from_instance('forum', $this->activities[2]->id);
  97  
  98          $this->activities[3] = $this->getDataGenerator()->create_module('page', array('course'=>$this->course->id));
  99          $this->course_module[3] = get_coursemodule_from_instance('page', $this->activities[3]->id);
 100  
 101          $this->activities[4] = $this->getDataGenerator()->create_module('forum', array('course'=>$this->course->id));
 102          $this->course_module[4] = get_coursemodule_from_instance('forum', $this->activities[4]->id);
 103  
 104          $this->activities[5] = $this->getDataGenerator()->create_module('forum', array('course'=>$this->course->id));
 105          $this->course_module[5] = get_coursemodule_from_instance('forum', $this->activities[5]->id);
 106  
 107          $this->activities[6] = $this->getDataGenerator()->create_module('forum', array('course'=>$this->course->id));
 108          $this->course_module[6] = get_coursemodule_from_instance('forum', $this->activities[6]->id);
 109  
 110          $this->activities[7] = $this->getDataGenerator()->create_module('quiz', array('course'=>$this->course->id));
 111          $this->course_module[7] = get_coursemodule_from_instance('quiz', $this->activities[7]->id);
 112      }
 113  
 114      private function load_scales() {
 115          $scale = new stdClass();
 116          $scale->name        = 'unittestscale1';
 117          $scale->courseid    = $this->course->id;
 118          $scale->userid      = $this->user[0]->id;
 119          $scale->scale       = 'Way off topic, Not very helpful, Fairly neutral, Fairly helpful, Supportive, Some good information, Perfect answer!';
 120          $scale->description = 'This scale defines some of qualities that make posts helpful within the Moodle help forums.\n Your feedback will help others see how their posts are being received.';
 121  
 122          $this->scale[0] = $this->getDataGenerator()->create_scale($scale);
 123          $this->scalemax[0] = substr_count($scale->scale, ',');
 124  
 125          $scale = new stdClass();
 126          $scale->name        = 'unittestscale2';
 127          $scale->courseid    = $this->course->id;
 128          $scale->userid      = $this->user[0]->id;
 129          $scale->scale       = 'Distinction, Very Good, Good, Pass, Fail';
 130          $scale->description = 'This scale is used to mark standard assignments.';
 131  
 132          $this->scale[1] = $this->getDataGenerator()->create_scale($scale);
 133          $this->scalemax[1] = substr_count($scale->scale, ',');
 134  
 135          $scale = new stdClass();
 136          $scale->name        = 'unittestscale3';
 137          $scale->courseid    = $this->course->id;
 138          $scale->userid      = $this->user[0]->id;
 139          $scale->scale       = 'Loner, Contentious, Disinterested, Participative, Follower, Leader';
 140          $scale->description = 'Describes the level of teamwork of a student.';
 141          $temp  = explode(',', $scale->scale);
 142          $scale->max         = count($temp) -1;
 143  
 144          $this->scale[2] = $this->getDataGenerator()->create_scale($scale);
 145          $this->scalemax[2] = substr_count($scale->scale, ',');
 146  
 147          $scale = new stdClass();
 148          $scale->name        = 'unittestscale4';
 149          $scale->courseid    = $this->course->id;
 150          $scale->userid      = $this->user[0]->id;
 151          $scale->scale       = 'Does not understand theory, Understands theory but fails practice, Manages through, Excels';
 152          $scale->description = 'Level of expertise at a technical task, with a theoretical framework.';
 153          $temp  = explode(',', $scale->scale);
 154          $scale->max         = count($temp) -1;
 155  
 156          $this->scale[3] = $this->getDataGenerator()->create_scale($scale);
 157          $this->scalemax[3] = substr_count($scale->scale, ',');
 158  
 159          $scale = new stdClass();
 160          $scale->name        = 'unittestscale5';
 161          $scale->courseid    = $this->course->id;
 162          $scale->userid      = $this->user[0]->id;
 163          $scale->scale       = 'Insufficient, Acceptable, Excellent.';
 164          $scale->description = 'Description of skills.';
 165  
 166          $this->scale[4] = $this->getDataGenerator()->create_scale($scale);
 167          $this->scalemax[4] = substr_count($scale->scale, ',');
 168      }
 169  
 170      /**
 171       * Load grade_category data into the database, and adds the corresponding objects to this class' variable.
 172       * category structure:
 173                                course category
 174                                      |
 175                             +--------+-------------+-----------------------+
 176                             |                      |                       |
 177               unittestcategory1               level1category       unittestcategory7
 178                    |                                                          |
 179           +--------+-------------+                               +------------+---------------+
 180           |                      |                               |                            |
 181          unittestcategory2  unittestcategory3          unittestcategory5               unittestcategory6
 182       */
 183      private function load_grade_categories() {
 184          global $DB;
 185  
 186          $course_category = grade_category::fetch_course_category($this->course->id);
 187  
 188          $grade_category = new stdClass();
 189  
 190          $grade_category->fullname    = 'unittestcategory1 &';
 191          $grade_category->courseid    = $this->course->id;
 192          $grade_category->aggregation = GRADE_AGGREGATE_MEAN;
 193          $grade_category->aggregateonlygraded = 1;
 194          $grade_category->keephigh    = 0;
 195          $grade_category->droplow     = 0;
 196          $grade_category->parent      = $course_category->id;
 197          $grade_category->timecreated = time();
 198          $grade_category->timemodified = time();
 199          $grade_category->depth = 2;
 200  
 201          $grade_category->id = $DB->insert_record('grade_categories', $grade_category);
 202          $grade_category->path = '/'.$course_category->id.'/'.$grade_category->id.'/';
 203          $DB->update_record('grade_categories', $grade_category);
 204          $this->grade_categories[0] = $grade_category;
 205  
 206          $grade_category = new stdClass();
 207  
 208          $grade_category->fullname    = 'unittestcategory2';
 209          $grade_category->courseid    = $this->course->id;
 210          $grade_category->aggregation = GRADE_AGGREGATE_MEAN;
 211          $grade_category->aggregateonlygraded = 1;
 212          $grade_category->keephigh    = 0;
 213          $grade_category->droplow     = 0;
 214          $grade_category->parent      = $this->grade_categories[0]->id;
 215          $grade_category->timecreated = time();
 216          $grade_category->timemodified = time();
 217          $grade_category->depth = 3;
 218  
 219          $grade_category->id = $DB->insert_record('grade_categories', $grade_category);
 220          $grade_category->path = $this->grade_categories[0]->path.$grade_category->id.'/';
 221          $DB->update_record('grade_categories', $grade_category);
 222          $this->grade_categories[1] = $grade_category;
 223  
 224          $grade_category = new stdClass();
 225  
 226          $grade_category->fullname    = 'unittestcategory3';
 227          $grade_category->courseid    = $this->course->id;
 228          $grade_category->aggregation = GRADE_AGGREGATE_MEAN;
 229          $grade_category->aggregateonlygraded = 1;
 230          $grade_category->keephigh    = 0;
 231          $grade_category->droplow     = 0;
 232          $grade_category->parent      = $this->grade_categories[0]->id;
 233          $grade_category->timecreated = time();
 234          $grade_category->timemodified = time();
 235          $grade_category->depth = 3;
 236  
 237          $grade_category->id = $DB->insert_record('grade_categories', $grade_category);
 238          $grade_category->path = $this->grade_categories[0]->path.$grade_category->id.'/';
 239          $DB->update_record('grade_categories', $grade_category);
 240          $this->grade_categories[2] = $grade_category;
 241  
 242          // A category with no parent, but grade_items as children.
 243  
 244          $grade_category = new stdClass();
 245  
 246          $grade_category->fullname    = 'level1category';
 247          $grade_category->courseid    = $this->course->id;
 248          $grade_category->aggregation = GRADE_AGGREGATE_MEAN;
 249          $grade_category->aggregateonlygraded = 1;
 250          $grade_category->keephigh    = 0;
 251          $grade_category->droplow     = 0;
 252          $grade_category->parent      = $course_category->id;
 253          $grade_category->timecreated = time();
 254          $grade_category->timemodified = time();
 255          $grade_category->depth = 2;
 256  
 257          $grade_category->id = $DB->insert_record('grade_categories', $grade_category);
 258          $grade_category->path = '/'.$course_category->id.'/'.$grade_category->id.'/';
 259          $DB->update_record('grade_categories', $grade_category);
 260          $this->grade_categories[3] = $grade_category;
 261  
 262          $grade_category = new stdClass();
 263  
 264          $grade_category->fullname    = 'unittestcategory7';
 265          $grade_category->courseid    = $this->course->id;
 266          $grade_category->aggregation = GRADE_AGGREGATE_MEAN;
 267          $grade_category->aggregateonlygraded = 1;
 268          $grade_category->keephigh    = 0;
 269          $grade_category->droplow     = 0;
 270          $grade_category->parent      = $course_category->id;
 271          $grade_category->timecreated = time();
 272          $grade_category->timemodified = time();
 273          $grade_category->depth = 2;
 274  
 275          $grade_category->id = $DB->insert_record('grade_categories', $grade_category);
 276          $grade_category->path = '/'.$course_category->id.'/'.$grade_category->id.'/';
 277          $DB->update_record('grade_categories', $grade_category);
 278          $this->grade_categories[4] = $grade_category;
 279  
 280          $grade_category = new stdClass();
 281  
 282          $grade_category->fullname    = 'unittestcategory5';
 283          $grade_category->courseid    = $this->course->id;
 284          $grade_category->aggregation = GRADE_AGGREGATE_MEAN;
 285          $grade_category->aggregateonlygraded = 1;
 286          $grade_category->keephigh    = 0;
 287          $grade_category->droplow     = 0;
 288          $grade_category->parent      = $this->grade_categories[4]->id;
 289          $grade_category->timecreated = time();
 290          $grade_category->timemodified = time();
 291          $grade_category->depth = 3;
 292  
 293          $grade_category->id = $DB->insert_record('grade_categories', $grade_category);
 294          $grade_category->path = $this->grade_categories[4]->path.$grade_category->id.'/';
 295          $DB->update_record('grade_categories', $grade_category);
 296          $this->grade_categories[5] = $grade_category;
 297  
 298          $grade_category = new stdClass();
 299  
 300          $grade_category->fullname    = 'unittestcategory6';
 301          $grade_category->courseid    = $this->course->id;
 302          $grade_category->aggregation = GRADE_AGGREGATE_MEAN;
 303          $grade_category->aggregateonlygraded = 1;
 304          $grade_category->keephigh    = 0;
 305          $grade_category->droplow     = 0;
 306          $grade_category->parent      = $this->grade_categories[4]->id;
 307          $grade_category->timecreated = time();
 308          $grade_category->timemodified = time();
 309          $grade_category->depth = 3;
 310  
 311          $grade_category->id = $DB->insert_record('grade_categories', $grade_category);
 312          $grade_category->path = $this->grade_categories[4]->path.$grade_category->id.'/';
 313          $DB->update_record('grade_categories', $grade_category);
 314          $this->grade_categories[6] = $grade_category;
 315      }
 316  
 317      /**
 318       * Load grade_item data into the database, and adds the corresponding objects to this class' variable.
 319       */
 320      protected function load_grade_items() {
 321          global $DB;
 322  
 323          // Purge all items created by module generators.
 324          $DB->delete_records('grade_items', array('itemtype'=>'mod'));
 325  
 326          $course_category = grade_category::fetch_course_category($this->course->id);
 327  
 328          // id = 0
 329          $grade_item = new stdClass();
 330  
 331          $grade_item->courseid = $this->course->id;
 332          $grade_item->categoryid = $this->grade_categories[1]->id;
 333          $grade_item->itemname = 'unittestgradeitem1 &';
 334          $grade_item->itemtype = 'mod';
 335          $grade_item->itemmodule = $this->course_module[0]->modname;
 336          $grade_item->iteminstance = $this->course_module[0]->instance;
 337          $grade_item->gradetype = GRADE_TYPE_VALUE;
 338          $grade_item->grademin = 30;
 339          $grade_item->grademax = 110;
 340          $grade_item->itemnumber = 1;
 341          $grade_item->idnumber = 'item id 0';
 342          $grade_item->iteminfo = 'Grade item 0 used for unit testing';
 343          $grade_item->timecreated = time();
 344          $grade_item->timemodified = time();
 345          $grade_item->sortorder = 3;
 346  
 347          $grade_item->id = $DB->insert_record('grade_items', $grade_item);
 348          $this->grade_items[0] = $grade_item;
 349  
 350          // id = 1
 351          $grade_item = new stdClass();
 352  
 353          $grade_item->courseid = $this->course->id;
 354          $grade_item->categoryid = $this->grade_categories[1]->id;
 355          $grade_item->itemname = 'unittestgradeitem2';
 356          $grade_item->itemtype = 'import';
 357          $grade_item->itemmodule = $this->course_module[1]->modname;
 358          $grade_item->iteminstance = $this->course_module[1]->instance;
 359          $grade_item->calculation = '= ##gi'.$this->grade_items[0]->id.'## + 30 + [[item id 0]] - [[item id 0]]';
 360          $grade_item->gradetype = GRADE_TYPE_VALUE;
 361          $grade_item->itemnumber = null;
 362          $grade_item->grademin = 0;
 363          $grade_item->grademax = 100;
 364          $grade_item->iteminfo = 'Grade item 1 used for unit testing';
 365          $grade_item->timecreated = time();
 366          $grade_item->timemodified = time();
 367          $grade_item->sortorder = 4;
 368  
 369          $grade_item->id = $DB->insert_record('grade_items', $grade_item);
 370          $this->grade_items[1] = $grade_item;
 371  
 372          // id = 2
 373          $grade_item = new stdClass();
 374  
 375          $grade_item->courseid = $this->course->id;
 376          $grade_item->categoryid = $this->grade_categories[2]->id;
 377          $grade_item->itemname = 'unittestgradeitem3';
 378          $grade_item->itemtype = 'mod';
 379          $grade_item->itemmodule = $this->course_module[2]->modname;
 380          $grade_item->iteminstance = $this->course_module[2]->instance;
 381          $grade_item->gradetype = GRADE_TYPE_SCALE;
 382          $grade_item->scaleid = $this->scale[0]->id;
 383          $grade_item->grademin = 0;
 384          $grade_item->grademax = $this->scalemax[0];
 385          $grade_item->iteminfo = 'Grade item 2 used for unit testing';
 386          $grade_item->timecreated = time();
 387          $grade_item->timemodified = time();
 388          $grade_item->sortorder = 6;
 389  
 390          $grade_item->id = $DB->insert_record('grade_items', $grade_item);
 391          $this->grade_items[2] = $grade_item;
 392  
 393          // Load grade_items associated with the 3 categories.
 394          // id = 3
 395          $grade_item = new stdClass();
 396  
 397          $grade_item->courseid = $this->course->id;
 398          $grade_item->iteminstance = $this->grade_categories[0]->id;
 399          $grade_item->itemname = 'unittestgradeitemcategory1';
 400          $grade_item->needsupdate = 0;
 401          $grade_item->itemtype = 'category';
 402          $grade_item->gradetype = GRADE_TYPE_VALUE;
 403          $grade_item->grademin = 0;
 404          $grade_item->grademax = 100;
 405          $grade_item->iteminfo = 'Grade item 3 used for unit testing';
 406          $grade_item->timecreated = time();
 407          $grade_item->timemodified = time();
 408          $grade_item->sortorder = 1;
 409  
 410          $grade_item->id = $DB->insert_record('grade_items', $grade_item);
 411          $this->grade_items[3] = $grade_item;
 412  
 413          // id = 4
 414          $grade_item = new stdClass();
 415  
 416          $grade_item->courseid = $this->course->id;
 417          $grade_item->iteminstance = $this->grade_categories[1]->id;
 418          $grade_item->itemname = 'unittestgradeitemcategory2';
 419          $grade_item->itemtype = 'category';
 420          $grade_item->gradetype = GRADE_TYPE_VALUE;
 421          $grade_item->needsupdate = 0;
 422          $grade_item->grademin = 0;
 423          $grade_item->grademax = 100;
 424          $grade_item->iteminfo = 'Grade item 4 used for unit testing';
 425          $grade_item->timecreated = time();
 426          $grade_item->timemodified = time();
 427          $grade_item->sortorder = 2;
 428  
 429          $grade_item->id = $DB->insert_record('grade_items', $grade_item);
 430          $this->grade_items[4] = $grade_item;
 431  
 432          // id = 5
 433          $grade_item = new stdClass();
 434  
 435          $grade_item->courseid = $this->course->id;
 436          $grade_item->iteminstance = $this->grade_categories[2]->id;
 437          $grade_item->itemname = 'unittestgradeitemcategory3';
 438          $grade_item->itemtype = 'category';
 439          $grade_item->gradetype = GRADE_TYPE_VALUE;
 440          $grade_item->needsupdate = true;
 441          $grade_item->grademin = 0;
 442          $grade_item->grademax = 100;
 443          $grade_item->iteminfo = 'Grade item 5 used for unit testing';
 444          $grade_item->timecreated = time();
 445          $grade_item->timemodified = time();
 446          $grade_item->sortorder = 5;
 447  
 448          $grade_item->id = $DB->insert_record('grade_items', $grade_item);
 449          $this->grade_items[5] = $grade_item;
 450  
 451          // Orphan grade_item.
 452          // id = 6
 453          $grade_item = new stdClass();
 454  
 455          $grade_item->courseid = $this->course->id;
 456          $grade_item->categoryid = $course_category->id;
 457          $grade_item->itemname = 'unittestorphangradeitem1';
 458          $grade_item->itemtype = 'mod';
 459          $grade_item->itemmodule = $this->course_module[4]->modname;
 460          $grade_item->iteminstance = $this->course_module[4]->instance;
 461          $grade_item->itemnumber = 0;
 462          $grade_item->gradetype = GRADE_TYPE_VALUE;
 463          $grade_item->grademin = 10;
 464          $grade_item->grademax = 120;
 465          $grade_item->locked = time();
 466          $grade_item->iteminfo = 'Orphan Grade 6 item used for unit testing';
 467          $grade_item->timecreated = time();
 468          $grade_item->timemodified = time();
 469          $grade_item->sortorder = 7;
 470  
 471          $grade_item->id = $DB->insert_record('grade_items', $grade_item);
 472          $this->grade_items[6] = $grade_item;
 473  
 474          // 2 grade items under level1category.
 475          // id = 7
 476          $grade_item = new stdClass();
 477  
 478          $grade_item->courseid = $this->course->id;
 479          $grade_item->categoryid = $this->grade_categories[3]->id;
 480          $grade_item->itemname = 'singleparentitem1';
 481          $grade_item->itemtype = 'mod';
 482          $grade_item->itemmodule = $this->course_module[5]->modname;
 483          $grade_item->iteminstance = $this->course_module[5]->instance;
 484          $grade_item->gradetype = GRADE_TYPE_SCALE;
 485          $grade_item->scaleid = $this->scale[0]->id;
 486          $grade_item->grademin = 0;
 487          $grade_item->grademax = $this->scalemax[0];
 488          $grade_item->iteminfo = 'Grade item 7 used for unit testing';
 489          $grade_item->timecreated = time();
 490          $grade_item->timemodified = time();
 491          $grade_item->sortorder = 9;
 492  
 493          $grade_item->id = $DB->insert_record('grade_items', $grade_item);
 494          $this->grade_items[7] = $grade_item;
 495  
 496          // id = 8
 497          $grade_item = new stdClass();
 498  
 499          $grade_item->courseid = $this->course->id;
 500          $grade_item->categoryid = $this->grade_categories[3]->id;
 501          $grade_item->itemname = 'singleparentitem2';
 502          $grade_item->itemtype = 'mod';
 503          $grade_item->itemmodule = $this->course_module[6]->modname;
 504          $grade_item->iteminstance = $this->course_module[6]->instance;
 505          $grade_item->gradetype = GRADE_TYPE_VALUE;
 506          $grade_item->grademin = 0;
 507          $grade_item->grademax = 100;
 508          $grade_item->iteminfo = 'Grade item 8 used for unit testing';
 509          $grade_item->timecreated = time();
 510          $grade_item->timemodified = time();
 511          $grade_item->sortorder = 10;
 512  
 513          $grade_item->id = $DB->insert_record('grade_items', $grade_item);
 514          $this->grade_items[8] = $grade_item;
 515  
 516          // Grade_item for level1category.
 517          // id = 9
 518          $grade_item = new stdClass();
 519  
 520          $grade_item->courseid = $this->course->id;
 521          $grade_item->itemname = 'grade_item for level1 category';
 522          $grade_item->itemtype = 'category';
 523          $grade_item->iteminstance = $this->grade_categories[3]->id;
 524          $grade_item->needsupdate = true;
 525          $grade_item->gradetype = GRADE_TYPE_VALUE;
 526          $grade_item->grademin = 0;
 527          $grade_item->grademax = 100;
 528          $grade_item->iteminfo = 'Orphan Grade item 9 used for unit testing';
 529          $grade_item->timecreated = time();
 530          $grade_item->timemodified = time();
 531          $grade_item->sortorder = 8;
 532  
 533          $grade_item->id = $DB->insert_record('grade_items', $grade_item);
 534          $this->grade_items[9] = $grade_item;
 535  
 536          // Manual grade_item.
 537          // id = 10
 538          $grade_item = new stdClass();
 539  
 540          $grade_item->courseid = $this->course->id;
 541          $grade_item->categoryid = $course_category->id;
 542          $grade_item->itemname = 'manual grade_item';
 543          $grade_item->itemtype = 'manual';
 544          $grade_item->itemnumber = 0;
 545          $grade_item->needsupdate = false;
 546          $grade_item->gradetype = GRADE_TYPE_VALUE;
 547          $grade_item->grademin = 0;
 548          $grade_item->grademax = 100;
 549          $grade_item->iteminfo = 'Manual grade item 10 used for unit testing';
 550          $grade_item->timecreated = time();
 551          $grade_item->timemodified = time();
 552          $grade_item->sortorder = 10;
 553  
 554          $grade_item->id = $DB->insert_record('grade_items', $grade_item);
 555          $this->grade_items[10] = $grade_item;
 556  
 557          // Quiz grade_item (course_module = 7).
 558          // id = 11
 559          $grade_item = new stdClass();
 560  
 561          $grade_item->courseid = $this->course->id;
 562          $grade_item->categoryid = $course_category->id;
 563          $grade_item->itemname = 'Quiz grade item';
 564          $grade_item->itemtype = 'mod';
 565          $grade_item->itemmodule = $this->course_module[7]->modname;
 566          $grade_item->iteminstance = $this->course_module[7]->instance;
 567          $grade_item->itemnumber = 0;
 568          $grade_item->gradetype = GRADE_TYPE_VALUE;
 569          $grade_item->grademin = 0;
 570          $grade_item->grademax = 100;
 571          $grade_item->locked = 0;
 572          $grade_item->iteminfo = 'Quiz grade item used for unit testing';
 573          $grade_item->timecreated = time();
 574          $grade_item->timemodified = time();
 575          $grade_item->sortorder = 11;
 576  
 577          $grade_item->id = $DB->insert_record('grade_items', $grade_item);
 578          $this->grade_items[11] = $grade_item;
 579  
 580          // id = 12
 581          $grade_item = new stdClass();
 582  
 583          $grade_item->courseid = $this->course->id;
 584          $grade_item->iteminstance = $this->grade_categories[4]->id;
 585          $grade_item->itemname = 'unittestgradeitemcategory7';
 586          $grade_item->itemtype = 'category';
 587          $grade_item->gradetype = GRADE_TYPE_VALUE;
 588          $grade_item->needsupdate = true;
 589          $grade_item->grademin = 0;
 590          $grade_item->grademax = 100;
 591          $grade_item->iteminfo = 'Grade item 12 used for unit testing';
 592          $grade_item->timecreated = time();
 593          $grade_item->timemodified = time();
 594          $grade_item->sortorder = 12;
 595  
 596          $grade_item->id = $DB->insert_record('grade_items', $grade_item);
 597          $this->grade_items[12] = $grade_item;
 598  
 599          // id = 13
 600          $grade_item = new stdClass();
 601  
 602          $grade_item->courseid = $this->course->id;
 603          $grade_item->iteminstance = $this->grade_categories[5]->id;
 604          $grade_item->itemname = 'unittestgradeitemcategory5';
 605          $grade_item->itemtype = 'category';
 606          $grade_item->gradetype = GRADE_TYPE_VALUE;
 607          $grade_item->needsupdate = true;
 608          $grade_item->grademin = 0;
 609          $grade_item->grademax = 100;
 610          $grade_item->iteminfo = 'Grade item 13 used for unit testing';
 611          $grade_item->timecreated = time();
 612          $grade_item->timemodified = time();
 613          $grade_item->sortorder = 13;
 614  
 615          $grade_item->id = $DB->insert_record('grade_items', $grade_item);
 616          $this->grade_items[13] = $grade_item;
 617  
 618          // id = 14
 619          $grade_item = new stdClass();
 620  
 621          $grade_item->courseid = $this->course->id;
 622          $grade_item->iteminstance = $this->grade_categories[6]->id;
 623          $grade_item->itemname = 'unittestgradeitemcategory6';
 624          $grade_item->itemtype = 'category';
 625          $grade_item->gradetype = GRADE_TYPE_VALUE;
 626          $grade_item->needsupdate = true;
 627          $grade_item->grademin = 0;
 628          $grade_item->grademax = 100;
 629          $grade_item->iteminfo = 'Grade item 14 used for unit testing';
 630          $grade_item->timecreated = time();
 631          $grade_item->timemodified = time();
 632          $grade_item->sortorder = 14;
 633  
 634          $grade_item->id = $DB->insert_record('grade_items', $grade_item);
 635          $this->grade_items[14] = $grade_item;
 636  
 637          // Manual grade_item
 638          // id = 15
 639          $grade_item = new stdClass();
 640  
 641          $grade_item->courseid = $this->course->id;
 642          $grade_item->categoryid = $this->grade_categories[5]->id;
 643          $grade_item->itemname = 'manual grade_item';
 644          $grade_item->itemtype = 'manual';
 645          $grade_item->itemnumber = 0;
 646          $grade_item->needsupdate = false;
 647          $grade_item->gradetype = GRADE_TYPE_VALUE;
 648          $grade_item->grademin = 0;
 649          $grade_item->grademax = 100;
 650          $grade_item->iteminfo = 'Manual grade item 15 used for unit testing';
 651          $grade_item->timecreated = time();
 652          $grade_item->timemodified = time();
 653          $grade_item->sortorder = 15;
 654  
 655          $grade_item->id = $DB->insert_record('grade_items', $grade_item);
 656          $this->grade_items[15] = $grade_item;
 657  
 658          // Manual grade_item
 659          // id = 16
 660          $grade_item = new stdClass();
 661  
 662          $grade_item->courseid = $this->course->id;
 663          $grade_item->categoryid = $this->grade_categories[6]->id;
 664          $grade_item->itemname = 'manual grade_item';
 665          $grade_item->itemtype = 'manual';
 666          $grade_item->itemnumber = 0;
 667          $grade_item->needsupdate = false;
 668          $grade_item->gradetype = GRADE_TYPE_SCALE;
 669          $grade_item->grademin = 0;
 670          $grade_item->grademax = 100;
 671          $grade_item->iteminfo = 'Manual grade item 16 used for unit testing';
 672          $grade_item->timecreated = time();
 673          $grade_item->timemodified = time();
 674          $grade_item->sortorder = 16;
 675  
 676          $grade_item->id = $DB->insert_record('grade_items', $grade_item);
 677          $this->grade_items[16] = $grade_item;
 678  
 679          // $this->grade_items[17] loaded in load_grade_outcomes() in order to use an outcome id.
 680      }
 681  
 682      /**
 683       * Load grade_grades data into the database, and adds the corresponding objects to this class' variable.
 684       */
 685      private function load_grade_grades() {
 686          global $DB;
 687  
 688          // This method is called once for each test method. Avoid adding things to $this->grade_grades multiple times.
 689          $this->grade_grades = array();
 690  
 691          // Grades for grade_item 1.
 692          $grade = new stdClass();
 693          $grade->itemid = $this->grade_items[0]->id;
 694          $grade->userid = $this->user[1]->id;
 695          $grade->rawgrade = 15; // too small
 696          $grade->finalgrade = 30;
 697          $grade->timecreated = time();
 698          $grade->timemodified = time();
 699          $grade->information = '1 of 17 grade_grades';
 700          $grade->informationformat = FORMAT_PLAIN;
 701          $grade->feedback = 'Good, but not good enough..';
 702          $grade->feedbackformat = FORMAT_PLAIN;
 703  
 704          $grade->id = $DB->insert_record('grade_grades', $grade);
 705          $this->grade_grades[0] = $grade;
 706  
 707          $grade = new stdClass();
 708          $grade->itemid = $this->grade_items[0]->id;
 709          $grade->userid = $this->user[2]->id;
 710          $grade->rawgrade = 40;
 711          $grade->finalgrade = 40;
 712          $grade->timecreated = time();
 713          $grade->timemodified = time();
 714          $grade->information = '2 of 17 grade_grades';
 715  
 716          $grade->id = $DB->insert_record('grade_grades', $grade);
 717          $this->grade_grades[1] = $grade;
 718  
 719          $grade = new stdClass();
 720          $grade->itemid = $this->grade_items[0]->id;
 721          $grade->userid = $this->user[3]->id;
 722          $grade->rawgrade = 170; // too big
 723          $grade->finalgrade = 110;
 724          $grade->timecreated = time();
 725          $grade->timemodified = time();
 726          $grade->information = '3 of 17 grade_grades';
 727  
 728          $grade->id = $DB->insert_record('grade_grades', $grade);
 729          $this->grade_grades[2] = $grade;
 730  
 731  
 732          // No raw grades for grade_item 2 - it is calculated.
 733  
 734          $grade = new stdClass();
 735          $grade->itemid = $this->grade_items[1]->id;
 736          $grade->userid = $this->user[1]->id;
 737          $grade->finalgrade = 60;
 738          $grade->timecreated = time();
 739          $grade->timemodified = time();
 740          $grade->information = '4 of 17 grade_grades';
 741  
 742          $grade->id = $DB->insert_record('grade_grades', $grade);
 743          $this->grade_grades[3] = $grade;
 744  
 745          $grade = new stdClass();
 746          $grade->itemid = $this->grade_items[1]->id;
 747          $grade->userid = $this->user[2]->id;
 748          $grade->finalgrade = 70;
 749          $grade->timecreated = time();
 750          $grade->timemodified = time();
 751          $grade->information = '5 of 17 grade_grades';
 752  
 753          $grade->id = $DB->insert_record('grade_grades', $grade);
 754          $this->grade_grades[4] = $grade;
 755  
 756          $grade = new stdClass();
 757          $grade->itemid = $this->grade_items[1]->id;
 758          $grade->userid = $this->user[3]->id;
 759          $grade->finalgrade = 100;
 760          $grade->timecreated = time();
 761          $grade->timemodified = time();
 762          $grade->information = '6 of 17 grade_grades';
 763  
 764          $grade->id = $DB->insert_record('grade_grades', $grade);
 765          $this->grade_grades[5] = $grade;
 766  
 767  
 768          // Grades for grade_item 3.
 769  
 770          $grade = new stdClass();
 771          $grade->itemid = $this->grade_items[2]->id;
 772          $grade->userid = $this->user[1]->id;
 773          $grade->rawgrade = 2;
 774          $grade->finalgrade = 6;
 775          $grade->scaleid = $this->scale[3]->id;
 776          $grade->timecreated = time();
 777          $grade->timemodified = time();
 778          $grade->information = '7 of 17 grade_grades';
 779  
 780          $grade->id = $DB->insert_record('grade_grades', $grade);
 781          $this->grade_grades[6] = $grade;
 782  
 783          $grade = new stdClass();
 784          $grade->itemid = $this->grade_items[2]->id;
 785          $grade->userid = $this->user[2]->id;
 786          $grade->rawgrade = 3;
 787          $grade->finalgrade = 2;
 788          $grade->scaleid = $this->scale[3]->id;
 789          $grade->timecreated = time();
 790          $grade->timemodified = time();
 791          $grade->information = '8 of 17 grade_grades';
 792  
 793          $grade->id = $DB->insert_record('grade_grades', $grade);
 794          $this->grade_grades[] = $grade;
 795  
 796          $grade = new stdClass();
 797          $grade->itemid = $this->grade_items[2]->id;
 798          $grade->userid = $this->user[3]->id;
 799          $grade->rawgrade = 1;
 800          $grade->finalgrade = 3;
 801          $grade->scaleid = $this->scale[3]->id;
 802          $grade->timecreated = time();
 803          $grade->timemodified = time();
 804          $grade->information = '9 of 17 grade_grades';
 805  
 806          $grade->id = $DB->insert_record('grade_grades', $grade);
 807          $this->grade_grades[] = $grade;
 808  
 809          // Grades for grade_item 7.
 810  
 811          $grade = new stdClass();
 812          $grade->itemid = $this->grade_items[6]->id;
 813          $grade->userid = $this->user[1]->id;
 814          $grade->rawgrade = 97;
 815          $grade->finalgrade = 69;
 816          $grade->timecreated = time();
 817          $grade->timemodified = time();
 818          $grade->information = '10 of 17 grade_grades';
 819  
 820          $grade->id = $DB->insert_record('grade_grades', $grade);
 821          $this->grade_grades[] = $grade;
 822  
 823          $grade = new stdClass();
 824          $grade->itemid = $this->grade_items[6]->id;
 825          $grade->userid = $this->user[2]->id;
 826          $grade->rawgrade = 49;
 827          $grade->finalgrade = 87;
 828          $grade->timecreated = time();
 829          $grade->timemodified = time();
 830          $grade->information = '11 of 17 grade_grades';
 831  
 832          $grade->id = $DB->insert_record('grade_grades', $grade);
 833          $this->grade_grades[] = $grade;
 834  
 835          $grade = new stdClass();
 836          $grade->itemid = $this->grade_items[6]->id;
 837          $grade->userid = $this->user[3]->id;
 838          $grade->rawgrade = 67;
 839          $grade->finalgrade = 94;
 840          $grade->timecreated = time();
 841          $grade->timemodified = time();
 842          $grade->information = '12 of 17 grade_grades';
 843  
 844          $grade->id = $DB->insert_record('grade_grades', $grade);
 845          $this->grade_grades[] = $grade;
 846  
 847          // Grades for grade_item 8.
 848  
 849          $grade = new stdClass();
 850          $grade->itemid = $this->grade_items[7]->id;
 851          $grade->userid = $this->user[2]->id;
 852          $grade->rawgrade = 3;
 853          $grade->finalgrade = 3;
 854          $grade->timecreated = time();
 855          $grade->timemodified = time();
 856          $grade->information = '13 of 17 grade_grades';
 857  
 858          $grade->id = $DB->insert_record('grade_grades', $grade);
 859          $this->grade_grades[] = $grade;
 860  
 861          $grade = new stdClass();
 862          $grade->itemid = $this->grade_items[7]->id;
 863          $grade->userid = $this->user[3]->id;
 864          $grade->rawgrade = 6;
 865          $grade->finalgrade = 6;
 866          $grade->timecreated = time();
 867          $grade->timemodified = time();
 868          $grade->information = '14 of 17 grade_grades';
 869  
 870          $grade->id = $DB->insert_record('grade_grades', $grade);
 871          $this->grade_grades[] = $grade;
 872  
 873          // Grades for grade_item 9.
 874  
 875          $grade = new stdClass();
 876          $grade->itemid = $this->grade_items[8]->id;
 877          $grade->userid = $this->user[1]->id;
 878          $grade->rawgrade = 20;
 879          $grade->finalgrade = 20;
 880          $grade->timecreated = time();
 881          $grade->timemodified = time();
 882          $grade->information = '15 of 17 grade_grades';
 883  
 884          $grade->id = $DB->insert_record('grade_grades', $grade);
 885          $this->grade_grades[] = $grade;
 886  
 887          $grade = new stdClass();
 888          $grade->itemid = $this->grade_items[8]->id;
 889          $grade->userid = $this->user[2]->id;
 890          $grade->rawgrade = 50;
 891          $grade->finalgrade = 50;
 892          $grade->timecreated = time();
 893          $grade->timemodified = time();
 894          $grade->information = '16 of 17 grade_grades';
 895  
 896          $grade->id = $DB->insert_record('grade_grades', $grade);
 897          $this->grade_grades[] = $grade;
 898  
 899          $grade = new stdClass();
 900          $grade->itemid = $this->grade_items[8]->id;
 901          $grade->userid = $this->user[3]->id;
 902          $grade->rawgrade = 100;
 903          $grade->finalgrade = 100;
 904          $grade->timecreated = time();
 905          $grade->timemodified = time();
 906          $grade->information = '17 of 17 grade_grades';
 907  
 908          $grade->id = $DB->insert_record('grade_grades', $grade);
 909          $this->grade_grades[] = $grade;
 910      }
 911  
 912      /**
 913       * Load grade_outcome data into the database, and adds the corresponding objects to this class' variable.
 914       */
 915      private function load_grade_outcomes() {
 916          global $DB;
 917  
 918          // This method is called once for each test method. Avoid adding things to $this->grade_outcomes multiple times.
 919          $this->grade_outcomes = array();
 920  
 921          // Calculation for grade_item 1.
 922          $grade_outcome = new stdClass();
 923          $grade_outcome->fullname = 'Team work';
 924          $grade_outcome->shortname = 'Team work';
 925          $grade_outcome->fullname = 'Team work outcome';
 926          $grade_outcome->timecreated = time();
 927          $grade_outcome->timemodified = time();
 928          $grade_outcome->scaleid = $this->scale[2]->id;
 929  
 930          $grade_outcome->id = $DB->insert_record('grade_outcomes', $grade_outcome);
 931          $this->grade_outcomes[] = $grade_outcome;
 932  
 933          // Calculation for grade_item 2.
 934          $grade_outcome = new stdClass();
 935          $grade_outcome->fullname = 'Complete circuit board';
 936          $grade_outcome->shortname = 'Complete circuit board';
 937          $grade_outcome->fullname = 'Complete circuit board';
 938          $grade_outcome->timecreated = time();
 939          $grade_outcome->timemodified = time();
 940          $grade_outcome->scaleid = $this->scale[3]->id;
 941  
 942          $grade_outcome->id = $DB->insert_record('grade_outcomes', $grade_outcome);
 943          $this->grade_outcomes[] = $grade_outcome;
 944  
 945          // Calculation for grade_item 3.
 946          $grade_outcome = new stdClass();
 947          $grade_outcome->fullname = 'Debug Java program';
 948          $grade_outcome->shortname = 'Debug Java program';
 949          $grade_outcome->fullname = 'Debug Java program';
 950          $grade_outcome->timecreated = time();
 951          $grade_outcome->timemodified = time();
 952          $grade_outcome->scaleid = $this->scale[4]->id;
 953  
 954          $grade_outcome->id = $DB->insert_record('grade_outcomes', $grade_outcome);
 955          $this->grade_outcomes[] = $grade_outcome;
 956  
 957          // Manual grade_item with outcome
 958          // id = 17
 959          $grade_item = new stdClass();
 960  
 961          $grade_item->courseid = $this->course->id;
 962          $grade_item->categoryid = $this->grade_categories[6]->id;
 963          $grade_item->itemname = 'manual grade_item';
 964          $grade_item->itemtype = 'manual';
 965          $grade_item->itemnumber = 0;
 966          $grade_item->needsupdate = false;
 967          $grade_item->gradetype = GRADE_TYPE_SCALE;
 968          $grade_item->grademin = 0;
 969          $grade_item->grademax = 100;
 970          $grade_item->iteminfo = 'Manual grade item 16 with outcome used for unit testing';
 971          $grade_item->timecreated = time();
 972          $grade_item->timemodified = time();
 973          $grade_item->outcomeid = $this->grade_outcomes[2]->id;
 974          $grade_item->sortorder = 17;
 975  
 976          $grade_item->id = $DB->insert_record('grade_items', $grade_item);
 977          $this->grade_items[17] = $grade_item;
 978      }
 979  }
 980  
 981  /**
 982   * Allow calling protected method.
 983   */
 984  class test_grade_grade_flatten_dependencies_array extends grade_grade {
 985      public static function test_flatten_dependencies_array(&$a,&$b) {
 986          return self::flatten_dependencies_array($a, $b);
 987      }
 988  }
 989