Search moodle.org's
Developer Documentation

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
  • Differences Between: [Versions 37 and 311] [Versions 38 and 311] [Versions 39 and 311]

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