[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/testing/generator/ -> data_generator.php (source)

   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   * Data generator.
  19   *
  20   * @package    core
  21   * @category   test
  22   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Data generator class for unit tests and other tools that need to create fake test sites.
  30   *
  31   * @package    core
  32   * @category   test
  33   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class testing_data_generator {
  37      /** @var int The number of grade categories created */
  38      protected $gradecategorycounter = 0;
  39      /** @var int The number of grade items created */
  40      protected $gradeitemcounter = 0;
  41      /** @var int The number of grade outcomes created */
  42      protected $gradeoutcomecounter = 0;
  43      protected $usercounter = 0;
  44      protected $categorycount = 0;
  45      protected $cohortcount = 0;
  46      protected $coursecount = 0;
  47      protected $scalecount = 0;
  48      protected $groupcount = 0;
  49      protected $groupingcount = 0;
  50      protected $rolecount = 0;
  51      protected $tagcount = 0;
  52  
  53      /** @var array list of plugin generators */
  54      protected $generators = array();
  55  
  56      /** @var array lis of common last names */
  57      public $lastnames = array(
  58          'Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Miller', 'Davis', 'García', 'Rodríguez', 'Wilson',
  59          'Müller', 'Schmidt', 'Schneider', 'Fischer', 'Meyer', 'Weber', 'Schulz', 'Wagner', 'Becker', 'Hoffmann',
  60          'Novák', 'Svoboda', 'Novotný', 'Dvořák', 'Černý', 'Procházková', 'Kučerová', 'Veselá', 'Horáková', 'Němcová',
  61          'Смирнов', 'Иванов', 'Кузнецов', 'Соколов', 'Попов', 'Лебедева', 'Козлова', 'Новикова', 'Морозова', 'Петрова',
  62          '王', '李', '张', '刘', '陈', '楊', '黃', '趙', '吳', '周',
  63          '佐藤', '鈴木', '高橋', '田中', '渡辺', '伊藤', '山本', '中村', '小林', '斎藤',
  64      );
  65  
  66      /** @var array lis of common first names */
  67      public $firstnames = array(
  68          'Jacob', 'Ethan', 'Michael', 'Jayden', 'William', 'Isabella', 'Sophia', 'Emma', 'Olivia', 'Ava',
  69          'Lukas', 'Leon', 'Luca', 'Timm', 'Paul', 'Leonie', 'Leah', 'Lena', 'Hanna', 'Laura',
  70          'Jakub', 'Jan', 'Tomáš', 'Lukáš', 'Matěj', 'Tereza', 'Eliška', 'Anna', 'Adéla', 'Karolína',
  71          'Даниил', 'Максим', 'Артем', 'Иван', 'Александр', 'София', 'Анастасия', 'Дарья', 'Мария', 'Полина',
  72          '伟', '伟', '芳', '伟', '秀英', '秀英', '娜', '秀英', '伟', '敏',
  73          '翔', '大翔', '拓海', '翔太', '颯太', '陽菜', 'さくら', '美咲', '葵', '美羽',
  74      );
  75  
  76      public $loremipsum = <<<EOD
  77  Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nulla non arcu lacinia neque faucibus fringilla. Vivamus porttitor turpis ac leo. Integer in sapien. Nullam eget nisl. Aliquam erat volutpat. Cras elementum. Mauris suscipit, ligula sit amet pharetra semper, nibh ante cursus purus, vel sagittis velit mauris vel metus. Integer malesuada. Nullam lectus justo, vulputate eget mollis sed, tempor sed magna. Mauris elementum mauris vitae tortor. Aliquam erat volutpat.
  78  Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Pellentesque ipsum. Cras pede libero, dapibus nec, pretium sit amet, tempor quis. Aliquam ante. Proin in tellus sit amet nibh dignissim sagittis. Vivamus porttitor turpis ac leo. Duis bibendum, lectus ut viverra rhoncus, dolor nunc faucibus libero, eget facilisis enim ipsum id lacus. In sem justo, commodo ut, suscipit at, pharetra vitae, orci. Aliquam erat volutpat. Nulla est.
  79  Vivamus luctus egestas leo. Aenean fermentum risus id tortor. Mauris dictum facilisis augue. Aliquam erat volutpat. Aliquam ornare wisi eu metus. Aliquam id dolor. Duis condimentum augue id magna semper rutrum. Donec iaculis gravida nulla. Pellentesque ipsum. Etiam dictum tincidunt diam. Quisque tincidunt scelerisque libero. Etiam egestas wisi a erat.
  80  Integer lacinia. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Mauris tincidunt sem sed arcu. Nullam feugiat, turpis at pulvinar vulputate, erat libero tristique tellus, nec bibendum odio risus sit amet ante. Aliquam id dolor. Maecenas sollicitudin. Et harum quidem rerum facilis est et expedita distinctio. Mauris suscipit, ligula sit amet pharetra semper, nibh ante cursus purus, vel sagittis velit mauris vel metus. Nullam dapibus fermentum ipsum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Pellentesque sapien. Duis risus. Mauris elementum mauris vitae tortor. Suspendisse nisl. Integer rutrum, orci vestibulum ullamcorper ultricies, lacus quam ultricies odio, vitae placerat pede sem sit amet enim.
  81  In laoreet, magna id viverra tincidunt, sem odio bibendum justo, vel imperdiet sapien wisi sed libero. Proin pede metus, vulputate nec, fermentum fringilla, vehicula vitae, justo. Nullam justo enim, consectetuer nec, ullamcorper ac, vestibulum in, elit. Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? Maecenas lorem. Etiam posuere lacus quis dolor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Curabitur ligula sapien, pulvinar a vestibulum quis, facilisis vel sapien. Nam sed tellus id magna elementum tincidunt. Suspendisse nisl. Vivamus luctus egestas leo. Nulla non arcu lacinia neque faucibus fringilla. Etiam dui sem, fermentum vitae, sagittis id, malesuada in, quam. Etiam dictum tincidunt diam. Etiam commodo dui eget wisi. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Proin pede metus, vulputate nec, fermentum fringilla, vehicula vitae, justo. Duis ante orci, molestie vitae vehicula venenatis, tincidunt ac pede. Pellentesque sapien.
  82  EOD;
  83  
  84      /**
  85       * To be called from data reset code only,
  86       * do not use in tests.
  87       * @return void
  88       */
  89      public function reset() {
  90          $this->usercounter = 0;
  91          $this->categorycount = 0;
  92          $this->coursecount = 0;
  93          $this->scalecount = 0;
  94  
  95          foreach ($this->generators as $generator) {
  96              $generator->reset();
  97          }
  98      }
  99  
 100      /**
 101       * Return generator for given plugin or component.
 102       * @param string $component the component name, e.g. 'mod_forum' or 'core_question'.
 103       * @return component_generator_base or rather an instance of the appropriate subclass.
 104       */
 105      public function get_plugin_generator($component) {
 106          list($type, $plugin) = core_component::normalize_component($component);
 107          $cleancomponent = $type . '_' . $plugin;
 108          if ($cleancomponent != $component) {
 109              debugging("Please specify the component you want a generator for as " .
 110                      "{$cleancomponent}, not {$component}.", DEBUG_DEVELOPER);
 111              $component = $cleancomponent;
 112          }
 113  
 114          if (isset($this->generators[$component])) {
 115              return $this->generators[$component];
 116          }
 117  
 118          $dir = core_component::get_component_directory($component);
 119          $lib = $dir . '/tests/generator/lib.php';
 120          if (!$dir || !is_readable($lib)) {
 121              throw new coding_exception("Component {$component} does not support " .
 122                      "generators yet. Missing tests/generator/lib.php.");
 123          }
 124  
 125          include_once($lib);
 126          $classname = $component . '_generator';
 127  
 128          if (!class_exists($classname)) {
 129              throw new coding_exception("Component {$component} does not support " .
 130                      "data generators yet. Class {$classname} not found.");
 131          }
 132  
 133          $this->generators[$component] = new $classname($this);
 134          return $this->generators[$component];
 135      }
 136  
 137      /**
 138       * Create a test user
 139       * @param array|stdClass $record
 140       * @param array $options
 141       * @return stdClass user record
 142       */
 143      public function create_user($record=null, array $options=null) {
 144          global $DB, $CFG;
 145  
 146          $this->usercounter++;
 147          $i = $this->usercounter;
 148  
 149          $record = (array)$record;
 150  
 151          if (!isset($record['auth'])) {
 152              $record['auth'] = 'manual';
 153          }
 154  
 155          if (!isset($record['firstname']) and !isset($record['lastname'])) {
 156              $country = rand(0, 5);
 157              $firstname = rand(0, 4);
 158              $lastname = rand(0, 4);
 159              $female = rand(0, 1);
 160              $record['firstname'] = $this->firstnames[($country*10) + $firstname + ($female*5)];
 161              $record['lastname'] = $this->lastnames[($country*10) + $lastname + ($female*5)];
 162  
 163          } else if (!isset($record['firstname'])) {
 164              $record['firstname'] = 'Firstname'.$i;
 165  
 166          } else if (!isset($record['lastname'])) {
 167              $record['lastname'] = 'Lastname'.$i;
 168          }
 169  
 170          if (!isset($record['firstnamephonetic'])) {
 171              $firstnamephonetic = rand(0, 59);
 172              $record['firstnamephonetic'] = $this->firstnames[$firstnamephonetic];
 173          }
 174  
 175          if (!isset($record['lasttnamephonetic'])) {
 176              $lastnamephonetic = rand(0, 59);
 177              $record['lastnamephonetic'] = $this->lastnames[$lastnamephonetic];
 178          }
 179  
 180          if (!isset($record['middlename'])) {
 181              $middlename = rand(0, 59);
 182              $record['middlename'] = $this->firstnames[$middlename];
 183          }
 184  
 185          if (!isset($record['alternatename'])) {
 186              $alternatename = rand(0, 59);
 187              $record['alternatename'] = $this->firstnames[$alternatename];
 188          }
 189  
 190          if (!isset($record['idnumber'])) {
 191              $record['idnumber'] = '';
 192          }
 193  
 194          if (!isset($record['mnethostid'])) {
 195              $record['mnethostid'] = $CFG->mnet_localhost_id;
 196          }
 197  
 198          if (!isset($record['username'])) {
 199              $record['username'] = 'username'.$i;
 200              $j = 2;
 201              while ($DB->record_exists('user', array('username'=>$record['username'], 'mnethostid'=>$record['mnethostid']))) {
 202                  $record['username'] = 'username'.$i.'_'.$j;
 203                  $j++;
 204              }
 205          }
 206  
 207          if (isset($record['password'])) {
 208              $record['password'] = hash_internal_user_password($record['password']);
 209          } else {
 210              // The auth plugin may not fully support this,
 211              // but it is still better/faster than hashing random stuff.
 212              $record['password'] = AUTH_PASSWORD_NOT_CACHED;
 213          }
 214  
 215          if (!isset($record['email'])) {
 216              $record['email'] = $record['username'].'@example.com';
 217          }
 218  
 219          if (!isset($record['confirmed'])) {
 220              $record['confirmed'] = 1;
 221          }
 222  
 223          if (!isset($record['lang'])) {
 224              $record['lang'] = 'en';
 225          }
 226  
 227          if (!isset($record['maildisplay'])) {
 228              $record['maildisplay'] = $CFG->defaultpreference_maildisplay;
 229          }
 230  
 231          if (!isset($record['mailformat'])) {
 232              $record['mailformat'] = $CFG->defaultpreference_mailformat;
 233          }
 234  
 235          if (!isset($record['maildigest'])) {
 236              $record['maildigest'] = $CFG->defaultpreference_maildigest;
 237          }
 238  
 239          if (!isset($record['autosubscribe'])) {
 240              $record['autosubscribe'] = $CFG->defaultpreference_autosubscribe;
 241          }
 242  
 243          if (!isset($record['trackforums'])) {
 244              $record['trackforums'] = $CFG->defaultpreference_trackforums;
 245          }
 246  
 247          if (!isset($record['deleted'])) {
 248              $record['deleted'] = 0;
 249          }
 250  
 251          if (!isset($record['timecreated'])) {
 252              $record['timecreated'] = time();
 253          }
 254  
 255          $record['timemodified'] = $record['timecreated'];
 256          $record['lastip'] = '0.0.0.0';
 257  
 258          if ($record['deleted']) {
 259              $delname = $record['email'].'.'.time();
 260              while ($DB->record_exists('user', array('username'=>$delname))) {
 261                  $delname++;
 262              }
 263              $record['idnumber'] = '';
 264              $record['email']    = md5($record['username']);
 265              $record['username'] = $delname;
 266              $record['picture']  = 0;
 267          }
 268  
 269          $userid = $DB->insert_record('user', $record);
 270  
 271          if (!$record['deleted']) {
 272              context_user::instance($userid);
 273          }
 274  
 275          return $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
 276      }
 277  
 278      /**
 279       * Create a test course category
 280       * @param array|stdClass $record
 281       * @param array $options
 282       * @return coursecat course category record
 283       */
 284      public function create_category($record=null, array $options=null) {
 285          global $DB, $CFG;
 286          require_once("$CFG->libdir/coursecatlib.php");
 287  
 288          $this->categorycount++;
 289          $i = $this->categorycount;
 290  
 291          $record = (array)$record;
 292  
 293          if (!isset($record['name'])) {
 294              $record['name'] = 'Course category '.$i;
 295          }
 296  
 297          if (!isset($record['description'])) {
 298              $record['description'] = "Test course category $i\n$this->loremipsum";
 299          }
 300  
 301          if (!isset($record['idnumber'])) {
 302              $record['idnumber'] = '';
 303          }
 304  
 305          return coursecat::create($record);
 306      }
 307  
 308      /**
 309       * Create test cohort.
 310       * @param array|stdClass $record
 311       * @param array $options
 312       * @return stdClass cohort record
 313       */
 314      public function create_cohort($record=null, array $options=null) {
 315          global $DB, $CFG;
 316          require_once("$CFG->dirroot/cohort/lib.php");
 317  
 318          $this->cohortcount++;
 319          $i = $this->cohortcount;
 320  
 321          $record = (array)$record;
 322  
 323          if (!isset($record['contextid'])) {
 324              $record['contextid'] = context_system::instance()->id;
 325          }
 326  
 327          if (!isset($record['name'])) {
 328              $record['name'] = 'Cohort '.$i;
 329          }
 330  
 331          if (!isset($record['idnumber'])) {
 332              $record['idnumber'] = '';
 333          }
 334  
 335          if (!isset($record['description'])) {
 336              $record['description'] = "Test cohort $i\n$this->loremipsum";
 337          }
 338  
 339          if (!isset($record['descriptionformat'])) {
 340              $record['descriptionformat'] = FORMAT_MOODLE;
 341          }
 342  
 343          if (!isset($record['visible'])) {
 344              $record['visible'] = 1;
 345          }
 346  
 347          if (!isset($record['component'])) {
 348              $record['component'] = '';
 349          }
 350  
 351          $id = cohort_add_cohort((object)$record);
 352  
 353          return $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
 354      }
 355  
 356      /**
 357       * Create a test course
 358       * @param array|stdClass $record
 359       * @param array $options with keys:
 360       *      'createsections'=>bool precreate all sections
 361       * @return stdClass course record
 362       */
 363      public function create_course($record=null, array $options=null) {
 364          global $DB, $CFG;
 365          require_once("$CFG->dirroot/course/lib.php");
 366  
 367          $this->coursecount++;
 368          $i = $this->coursecount;
 369  
 370          $record = (array)$record;
 371  
 372          if (!isset($record['fullname'])) {
 373              $record['fullname'] = 'Test course '.$i;
 374          }
 375  
 376          if (!isset($record['shortname'])) {
 377              $record['shortname'] = 'tc_'.$i;
 378          }
 379  
 380          if (!isset($record['idnumber'])) {
 381              $record['idnumber'] = '';
 382          }
 383  
 384          if (!isset($record['format'])) {
 385              $record['format'] = 'topics';
 386          }
 387  
 388          if (!isset($record['newsitems'])) {
 389              $record['newsitems'] = 0;
 390          }
 391  
 392          if (!isset($record['numsections'])) {
 393              $record['numsections'] = 5;
 394          }
 395  
 396          if (!isset($record['summary'])) {
 397              $record['summary'] = "Test course $i\n$this->loremipsum";
 398          }
 399  
 400          if (!isset($record['summaryformat'])) {
 401              $record['summaryformat'] = FORMAT_MOODLE;
 402          }
 403  
 404          if (!isset($record['category'])) {
 405              $record['category'] = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
 406          }
 407  
 408          $course = create_course((object)$record);
 409          context_course::instance($course->id);
 410          if (!empty($options['createsections'])) {
 411              if (isset($course->numsections)) {
 412                  course_create_sections_if_missing($course, range(0, $course->numsections));
 413              } else {
 414                  course_create_sections_if_missing($course, 0);
 415              }
 416          }
 417  
 418          return $course;
 419      }
 420  
 421      /**
 422       * Create course section if does not exist yet
 423       * @param array|stdClass $record must contain 'course' and 'section' attributes
 424       * @param array|null $options
 425       * @return stdClass
 426       * @throws coding_exception
 427       */
 428      public function create_course_section($record = null, array $options = null) {
 429          global $DB;
 430  
 431          $record = (array)$record;
 432  
 433          if (empty($record['course'])) {
 434              throw new coding_exception('course must be present in testing_data_generator::create_course_section() $record');
 435          }
 436  
 437          if (!isset($record['section'])) {
 438              throw new coding_exception('section must be present in testing_data_generator::create_course_section() $record');
 439          }
 440  
 441          course_create_sections_if_missing($record['course'], $record['section']);
 442          return get_fast_modinfo($record['course'])->get_section_info($record['section']);
 443      }
 444  
 445      /**
 446       * Create a test block
 447       * @param string $blockname
 448       * @param array|stdClass $record
 449       * @param array $options
 450       * @return stdClass block instance record
 451       */
 452      public function create_block($blockname, $record=null, array $options=null) {
 453          $generator = $this->get_plugin_generator('block_'.$blockname);
 454          return $generator->create_instance($record, $options);
 455      }
 456  
 457      /**
 458       * Create a test module
 459       * @param string $modulename
 460       * @param array|stdClass $record
 461       * @param array $options
 462       * @return stdClass activity record
 463       */
 464      public function create_module($modulename, $record=null, array $options=null) {
 465          $generator = $this->get_plugin_generator('mod_'.$modulename);
 466          return $generator->create_instance($record, $options);
 467      }
 468  
 469      /**
 470       * Create a test group for the specified course
 471       *
 472       * $record should be either an array or a stdClass containing infomation about the group to create.
 473       * At the very least it needs to contain courseid.
 474       * Default values are added for name, description, and descriptionformat if they are not present.
 475       *
 476       * This function calls groups_create_group() to create the group within the database.
 477       * @see groups_create_group
 478       * @param array|stdClass $record
 479       * @return stdClass group record
 480       */
 481      public function create_group($record) {
 482          global $DB, $CFG;
 483  
 484          require_once($CFG->dirroot . '/group/lib.php');
 485  
 486          $this->groupcount++;
 487          $i = $this->groupcount;
 488  
 489          $record = (array)$record;
 490  
 491          if (empty($record['courseid'])) {
 492              throw new coding_exception('courseid must be present in testing_data_generator::create_group() $record');
 493          }
 494  
 495          if (!isset($record['name'])) {
 496              $record['name'] = 'group-' . $i;
 497          }
 498  
 499          if (!isset($record['description'])) {
 500              $record['description'] = "Test Group $i\n{$this->loremipsum}";
 501          }
 502  
 503          if (!isset($record['descriptionformat'])) {
 504              $record['descriptionformat'] = FORMAT_MOODLE;
 505          }
 506  
 507          $id = groups_create_group((object)$record);
 508  
 509          return $DB->get_record('groups', array('id'=>$id));
 510      }
 511  
 512      /**
 513       * Create a test group member
 514       * @param array|stdClass $record
 515       * @throws coding_exception
 516       * @return boolean
 517       */
 518      public function create_group_member($record) {
 519          global $DB, $CFG;
 520  
 521          require_once($CFG->dirroot . '/group/lib.php');
 522  
 523          $record = (array)$record;
 524  
 525          if (empty($record['userid'])) {
 526              throw new coding_exception('user must be present in testing_util::create_group_member() $record');
 527          }
 528  
 529          if (!isset($record['groupid'])) {
 530              throw new coding_exception('group must be present in testing_util::create_group_member() $record');
 531          }
 532  
 533          if (!isset($record['component'])) {
 534              $record['component'] = null;
 535          }
 536          if (!isset($record['itemid'])) {
 537              $record['itemid'] = 0;
 538          }
 539  
 540          return groups_add_member($record['groupid'], $record['userid'], $record['component'], $record['itemid']);
 541      }
 542  
 543      /**
 544       * Create a test grouping for the specified course
 545       *
 546       * $record should be either an array or a stdClass containing infomation about the grouping to create.
 547       * At the very least it needs to contain courseid.
 548       * Default values are added for name, description, and descriptionformat if they are not present.
 549       *
 550       * This function calls groups_create_grouping() to create the grouping within the database.
 551       * @see groups_create_grouping
 552       * @param array|stdClass $record
 553       * @return stdClass grouping record
 554       */
 555      public function create_grouping($record) {
 556          global $DB, $CFG;
 557  
 558          require_once($CFG->dirroot . '/group/lib.php');
 559  
 560          $this->groupingcount++;
 561          $i = $this->groupingcount;
 562  
 563          $record = (array)$record;
 564  
 565          if (empty($record['courseid'])) {
 566              throw new coding_exception('courseid must be present in testing_data_generator::create_grouping() $record');
 567          }
 568  
 569          if (!isset($record['name'])) {
 570              $record['name'] = 'grouping-' . $i;
 571          }
 572  
 573          if (!isset($record['description'])) {
 574              $record['description'] = "Test Grouping $i\n{$this->loremipsum}";
 575          }
 576  
 577          if (!isset($record['descriptionformat'])) {
 578              $record['descriptionformat'] = FORMAT_MOODLE;
 579          }
 580  
 581          $id = groups_create_grouping((object)$record);
 582  
 583          return $DB->get_record('groupings', array('id'=>$id));
 584      }
 585  
 586      /**
 587       * Create a test grouping group
 588       * @param array|stdClass $record
 589       * @throws coding_exception
 590       * @return boolean
 591       */
 592      public function create_grouping_group($record) {
 593          global $DB, $CFG;
 594  
 595          require_once($CFG->dirroot . '/group/lib.php');
 596  
 597          $record = (array)$record;
 598  
 599          if (empty($record['groupingid'])) {
 600              throw new coding_exception('grouping must be present in testing::create_grouping_group() $record');
 601          }
 602  
 603          if (!isset($record['groupid'])) {
 604              throw new coding_exception('group must be present in testing_util::create_grouping_group() $record');
 605          }
 606  
 607          return groups_assign_grouping($record['groupingid'], $record['groupid']);
 608      }
 609  
 610      /**
 611       * Create an instance of a repository.
 612       *
 613       * @param string type of repository to create an instance for.
 614       * @param array|stdClass $record data to use to up set the instance.
 615       * @param array $options options
 616       * @return stdClass repository instance record
 617       * @since Moodle 2.5.1
 618       */
 619      public function create_repository($type, $record=null, array $options = null) {
 620          $generator = $this->get_plugin_generator('repository_'.$type);
 621          return $generator->create_instance($record, $options);
 622      }
 623  
 624      /**
 625       * Create an instance of a repository.
 626       *
 627       * @param string type of repository to create an instance for.
 628       * @param array|stdClass $record data to use to up set the instance.
 629       * @param array $options options
 630       * @return repository_type object
 631       * @since Moodle 2.5.1
 632       */
 633      public function create_repository_type($type, $record=null, array $options = null) {
 634          $generator = $this->get_plugin_generator('repository_'.$type);
 635          return $generator->create_type($record, $options);
 636      }
 637  
 638  
 639      /**
 640       * Create a test scale
 641       * @param array|stdClass $record
 642       * @param array $options
 643       * @return stdClass block instance record
 644       */
 645      public function create_scale($record=null, array $options=null) {
 646          global $DB;
 647  
 648          $this->scalecount++;
 649          $i = $this->scalecount;
 650  
 651          $record = (array)$record;
 652  
 653          if (!isset($record['name'])) {
 654              $record['name'] = 'Test scale '.$i;
 655          }
 656  
 657          if (!isset($record['scale'])) {
 658              $record['scale'] = 'A,B,C,D,F';
 659          }
 660  
 661          if (!isset($record['courseid'])) {
 662              $record['courseid'] = 0;
 663          }
 664  
 665          if (!isset($record['userid'])) {
 666              $record['userid'] = 0;
 667          }
 668  
 669          if (!isset($record['description'])) {
 670              $record['description'] = 'Test scale description '.$i;
 671          }
 672  
 673          if (!isset($record['descriptionformat'])) {
 674              $record['descriptionformat'] = FORMAT_MOODLE;
 675          }
 676  
 677          $record['timemodified'] = time();
 678  
 679          if (isset($record['id'])) {
 680              $DB->import_record('scale', $record);
 681              $DB->get_manager()->reset_sequence('scale');
 682              $id = $record['id'];
 683          } else {
 684              $id = $DB->insert_record('scale', $record);
 685          }
 686  
 687          return $DB->get_record('scale', array('id'=>$id), '*', MUST_EXIST);
 688      }
 689  
 690      /**
 691       * Creates a new role in the system.
 692       *
 693       * You can fill $record with the role 'name',
 694       * 'shortname', 'description' and 'archetype'.
 695       *
 696       * If an archetype is specified it's capabilities,
 697       * context where the role can be assigned and
 698       * all other properties are copied from the archetype;
 699       * if no archetype is specified it will create an
 700       * empty role.
 701       *
 702       * @param array|stdClass $record
 703       * @return int The new role id
 704       */
 705      public function create_role($record=null) {
 706          global $DB;
 707  
 708          $this->rolecount++;
 709          $i = $this->rolecount;
 710  
 711          $record = (array)$record;
 712  
 713          if (empty($record['shortname'])) {
 714              $record['shortname'] = 'role-' . $i;
 715          }
 716  
 717          if (empty($record['name'])) {
 718              $record['name'] = 'Test role ' . $i;
 719          }
 720  
 721          if (empty($record['description'])) {
 722              $record['description'] = 'Test role ' . $i . ' description';
 723          }
 724  
 725          if (empty($record['archetype'])) {
 726              $record['archetype'] = '';
 727          } else {
 728              $archetypes = get_role_archetypes();
 729              if (empty($archetypes[$record['archetype']])) {
 730                  throw new coding_exception('\'role\' requires the field \'archetype\' to specify a ' .
 731                      'valid archetype shortname (editingteacher, student...)');
 732              }
 733          }
 734  
 735          // Creates the role.
 736          if (!$newroleid = create_role($record['name'], $record['shortname'], $record['description'], $record['archetype'])) {
 737              throw new coding_exception('There was an error creating \'' . $record['shortname'] . '\' role');
 738          }
 739  
 740          // If no archetype was specified we allow it to be added to all contexts,
 741          // otherwise we allow it in the archetype contexts.
 742          if (!$record['archetype']) {
 743              $contextlevels = array_keys(context_helper::get_all_levels());
 744          } else {
 745              // Copying from the archetype default rol.
 746              $archetyperoleid = $DB->get_field(
 747                  'role',
 748                  'id',
 749                  array('shortname' => $record['archetype'], 'archetype' => $record['archetype'])
 750              );
 751              $contextlevels = get_role_contextlevels($archetyperoleid);
 752          }
 753          set_role_contextlevels($newroleid, $contextlevels);
 754  
 755          if ($record['archetype']) {
 756  
 757              // We copy all the roles the archetype can assign, override and switch to.
 758              if ($record['archetype']) {
 759                  $types = array('assign', 'override', 'switch');
 760                  foreach ($types as $type) {
 761                      $rolestocopy = get_default_role_archetype_allows($type, $record['archetype']);
 762                      foreach ($rolestocopy as $tocopy) {
 763                          $functionname = 'allow_' . $type;
 764                          $functionname($newroleid, $tocopy);
 765                      }
 766                  }
 767              }
 768  
 769              // Copying the archetype capabilities.
 770              $sourcerole = $DB->get_record('role', array('id' => $archetyperoleid));
 771              role_cap_duplicate($sourcerole, $newroleid);
 772          }
 773  
 774          return $newroleid;
 775      }
 776  
 777      /**
 778       * Create a tag.
 779       *
 780       * @param array|stdClass $record
 781       * @return stdClass the tag record
 782       */
 783      public function create_tag($record = null) {
 784          global $DB, $USER;
 785  
 786          $this->tagcount++;
 787          $i = $this->tagcount;
 788  
 789          $record = (array) $record;
 790  
 791          if (!isset($record['userid'])) {
 792              $record['userid'] = $USER->id;
 793          }
 794  
 795          if (!isset($record['name'])) {
 796              $record['name'] = 'Tag name ' . $i;
 797          }
 798  
 799          if (!isset($record['rawname'])) {
 800              $record['rawname'] = 'Raw tag name ' . $i;
 801          }
 802  
 803          if (!isset($record['tagtype'])) {
 804              $record['tagtype'] = 'default';
 805          }
 806  
 807          if (!isset($record['description'])) {
 808              $record['description'] = 'Tag description';
 809          }
 810  
 811          if (!isset($record['descriptionformat'])) {
 812              $record['descriptionformat'] = FORMAT_MOODLE;
 813          }
 814  
 815          if (!isset($record['flag'])) {
 816              $record['flag'] = 0;
 817          }
 818  
 819          if (!isset($record['timemodified'])) {
 820              $record['timemodified'] = time();
 821          }
 822  
 823          $id = $DB->insert_record('tag', $record);
 824  
 825          return $DB->get_record('tag', array('id' => $id), '*', MUST_EXIST);
 826      }
 827  
 828      /**
 829       * Helper method which combines $defaults with the values specified in $record.
 830       * If $record is an object, it is converted to an array.
 831       * Then, for each key that is in $defaults, but not in $record, the value
 832       * from $defaults is copied.
 833       * @param array $defaults the default value for each field with
 834       * @param array|stdClass $record
 835       * @return array updated $record.
 836       */
 837      public function combine_defaults_and_record(array $defaults, $record) {
 838          $record = (array) $record;
 839  
 840          foreach ($defaults as $key => $defaults) {
 841              if (!array_key_exists($key, $record)) {
 842                  $record[$key] = $defaults;
 843              }
 844          }
 845          return $record;
 846      }
 847  
 848      /**
 849       * Simplified enrolment of user to course using default options.
 850       *
 851       * It is strongly recommended to use only this method for 'manual' and 'self' plugins only!!!
 852       *
 853       * @param int $userid
 854       * @param int $courseid
 855       * @param int $roleid optional role id, use only with manual plugin
 856       * @param string $enrol name of enrol plugin,
 857       *     there must be exactly one instance in course,
 858       *     it must support enrol_user() method.
 859       * @param int $timestart (optional) 0 means unknown
 860       * @param int $timeend (optional) 0 means forever
 861       * @param int $status (optional) default to ENROL_USER_ACTIVE for new enrolments
 862       * @return bool success
 863       */
 864      public function enrol_user($userid, $courseid, $roleid = null, $enrol = 'manual', $timestart = 0, $timeend = 0, $status = null) {
 865          global $DB;
 866  
 867          if (!$plugin = enrol_get_plugin($enrol)) {
 868              return false;
 869          }
 870  
 871          $instances = $DB->get_records('enrol', array('courseid'=>$courseid, 'enrol'=>$enrol));
 872          if (count($instances) != 1) {
 873              return false;
 874          }
 875          $instance = reset($instances);
 876  
 877          if (is_null($roleid) and $instance->roleid) {
 878              $roleid = $instance->roleid;
 879          }
 880  
 881          $plugin->enrol_user($instance, $userid, $roleid, $timestart, $timeend, $status);
 882          return true;
 883      }
 884  
 885      /**
 886       * Assigns the specified role to a user in the context.
 887       *
 888       * @param int $roleid
 889       * @param int $userid
 890       * @param int $contextid Defaults to the system context
 891       * @return int new/existing id of the assignment
 892       */
 893      public function role_assign($roleid, $userid, $contextid = false) {
 894  
 895          // Default to the system context.
 896          if (!$contextid) {
 897              $context = context_system::instance();
 898              $contextid = $context->id;
 899          }
 900  
 901          if (empty($roleid)) {
 902              throw new coding_exception('roleid must be present in testing_data_generator::role_assign() arguments');
 903          }
 904  
 905          if (empty($userid)) {
 906              throw new coding_exception('userid must be present in testing_data_generator::role_assign() arguments');
 907          }
 908  
 909          return role_assign($roleid, $userid, $contextid);
 910      }
 911  
 912      /**
 913       * Create a grade_category.
 914       *
 915       * @param array|stdClass $record
 916       * @return stdClass the grade category record
 917       */
 918      public function create_grade_category($record = null) {
 919          global $CFG;
 920  
 921          $this->gradecategorycounter++;
 922  
 923          $record = (array)$record;
 924  
 925          if (empty($record['courseid'])) {
 926              throw new coding_exception('courseid must be present in testing::create_grade_category() $record');
 927          }
 928  
 929          if (!isset($record['fullname'])) {
 930              $record['fullname'] = 'Grade category ' . $this->gradecategorycounter;
 931          }
 932  
 933          // For gradelib classes.
 934          require_once($CFG->libdir . '/gradelib.php');
 935          // Create new grading category in this course.
 936          $gradecategory = new grade_category(array('courseid' => $record['courseid']), false);
 937          $gradecategory->apply_default_settings();
 938          grade_category::set_properties($gradecategory, $record);
 939          $gradecategory->apply_forced_settings();
 940          $gradecategory->insert();
 941  
 942          // This creates a default grade item for the category
 943          $gradeitem = $gradecategory->load_grade_item();
 944  
 945          $gradecategory->update_from_db();
 946          return $gradecategory->get_record_data();
 947      }
 948  
 949      /**
 950       * Create a grade_item.
 951       *
 952       * @param array|stdClass $record
 953       * @return stdClass the grade item record
 954       */
 955      public function create_grade_item($record = null) {
 956          global $CFG;
 957          require_once("$CFG->libdir/gradelib.php");
 958  
 959          $this->gradeitemcounter++;
 960  
 961          if (!isset($record['itemtype'])) {
 962              $record['itemtype'] = 'manual';
 963          }
 964  
 965          if (!isset($record['itemname'])) {
 966              $record['itemname'] = 'Grade item ' . $this->gradeitemcounter;
 967          }
 968  
 969          if (isset($record['outcomeid'])) {
 970              $outcome = new grade_outcome(array('id' => $record['outcomeid']));
 971              $record['scaleid'] = $outcome->scaleid;
 972          }
 973          if (isset($record['scaleid'])) {
 974              $record['gradetype'] = GRADE_TYPE_SCALE;
 975          } else if (!isset($record['gradetype'])) {
 976              $record['gradetype'] = GRADE_TYPE_VALUE;
 977          }
 978  
 979          // Create new grade item in this course.
 980          $gradeitem = new grade_item($record, false);
 981          $gradeitem->insert();
 982  
 983          $gradeitem->update_from_db();
 984          return $gradeitem->get_record_data();
 985      }
 986  
 987      /**
 988       * Create a grade_outcome.
 989       *
 990       * @param array|stdClass $record
 991       * @return stdClass the grade outcome record
 992       */
 993      public function create_grade_outcome($record = null) {
 994          global $CFG;
 995  
 996          $this->gradeoutcomecounter++;
 997          $i = $this->gradeoutcomecounter;
 998  
 999          if (!isset($record['fullname'])) {
1000              $record['fullname'] = 'Grade outcome ' . $i;
1001          }
1002  
1003          // For gradelib classes.
1004          require_once($CFG->libdir . '/gradelib.php');
1005          // Create new grading outcome in this course.
1006          $gradeoutcome = new grade_outcome($record, false);
1007          $gradeoutcome->insert();
1008  
1009          $gradeoutcome->update_from_db();
1010          return $gradeoutcome->get_record_data();
1011      }
1012  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1