Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [Versions 402 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * tool_generator course backend code.
  19   *
  20   * @package tool_generator
  21   * @copyright 2013 The Open University
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Backend code for the 'make large course' tool.
  29   *
  30   * @package tool_generator
  31   * @copyright 2013 The Open University
  32   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class tool_generator_course_backend extends tool_generator_backend {
  35      /**
  36       * @var array Number of sections in course
  37       */
  38      private static $paramsections = array(1, 10, 100, 500, 1000, 2000);
  39      /**
  40       * @var array Number of assignments in course
  41       */
  42      private static $paramassignments = array(1, 10, 100, 500, 1000, 2000);
  43      /**
  44       * @var array Number of Page activities in course
  45       */
  46      private static $parampages = array(1, 50, 200, 1000, 5000, 10000);
  47      /**
  48       * @var array Number of students enrolled in course
  49       */
  50      private static $paramusers = array(1, 100, 1000, 10000, 50000, 100000);
  51      /**
  52       * Total size of small files: 1KB, 1MB, 10MB, 100MB, 1GB, 2GB.
  53       *
  54       * @var array Number of small files created in a single file activity
  55       */
  56      private static $paramsmallfilecount = array(1, 64, 128, 1024, 16384, 32768);
  57      /**
  58       * @var array Size of small files (to make the totals into nice numbers)
  59       */
  60      private static $paramsmallfilesize = array(1024, 16384, 81920, 102400, 65536, 65536);
  61      /**
  62       * Total size of big files: 8KB, 8MB, 80MB, 800MB, 8GB, 16GB.
  63       *
  64       * @var array Number of big files created as individual file activities
  65       */
  66      private static $parambigfilecount = array(1, 2, 5, 10, 10, 10);
  67      /**
  68       * @var array Size of each large file
  69       */
  70      private static $parambigfilesize = array(8192, 4194304, 16777216, 83886080,
  71              858993459, 1717986918);
  72      /**
  73       * @var array Number of forum discussions
  74       */
  75      private static $paramforumdiscussions = array(1, 10, 100, 500, 1000, 2000);
  76      /**
  77       * @var array Number of forum posts per discussion
  78       */
  79      private static $paramforumposts = array(2, 2, 5, 10, 10, 10);
  80  
  81      /**
  82       * @var array Number of assignments in course
  83       */
  84      private static $paramactivities = array(1, 10, 100, 500, 1000, 2000);
  85      /**
  86       * @var string Course shortname
  87       */
  88      private $shortname;
  89  
  90      /**
  91       * @var string Course fullname.
  92       */
  93      private $fullname = "";
  94  
  95      /**
  96       * @var string Course summary.
  97       */
  98      private $summary = "";
  99  
 100      /**
 101       * @var string Course summary format, defaults to FORMAT_HTML.
 102       */
 103      private $summaryformat = FORMAT_HTML;
 104  
 105      /**
 106       * @var testing_data_generator Data generator
 107       */
 108      protected $generator;
 109  
 110      /**
 111       * @var stdClass Course object
 112       */
 113      private $course;
 114  
 115      /**
 116       * @var array Array from test user number (1...N) to userid in database
 117       */
 118      private $userids;
 119  
 120      /**
 121       * @var array $additionalmodules
 122       */
 123      private $additionalmodules;
 124      /**
 125       * Constructs object ready to create course.
 126       *
 127       * @param string $shortname Course shortname
 128       * @param int $size Size as numeric index
 129       * @param bool $fixeddataset To use fixed or random data
 130       * @param int|bool $filesizelimit The max number of bytes for a generated file
 131       * @param bool $progress True if progress information should be displayed
 132       * @param array $additionalmodules potential additional modules to be added (quiz, bigbluebutton...)
 133       */
 134      public function __construct(
 135          $shortname,
 136          $size,
 137          $fixeddataset = false,
 138          $filesizelimit = false,
 139          $progress = true,
 140          $fullname = null,
 141          $summary = null,
 142          $summaryformat = FORMAT_HTML,
 143          $additionalmodules = []
 144      ) {
 145  
 146          // Set parameters.
 147          $this->shortname = $shortname;
 148  
 149          // We can't allow fullname to be set to an empty string.
 150          if (empty($fullname)) {
 151              $this->fullname = get_string(
 152                  'fullname',
 153                  'tool_generator',
 154                  array(
 155                      'size' => get_string('shortsize_' . $size, 'tool_generator')
 156                  )
 157              );
 158          } else {
 159              $this->fullname = $fullname;
 160          }
 161  
 162          // Summary, on the other hand, should be empty-able.
 163          if (!is_null($summary)) {
 164              $this->summary = $summary;
 165              $this->summaryformat = $summaryformat;
 166          }
 167          $this->additionalmodules = $additionalmodules;
 168          parent::__construct($size, $fixeddataset, $filesizelimit, $progress);
 169      }
 170  
 171      /**
 172       * Returns the relation between users and course sizes.
 173       *
 174       * @return array
 175       */
 176      public static function get_users_per_size() {
 177          return self::$paramusers;
 178      }
 179  
 180      /**
 181       * Gets a list of size choices supported by this backend.
 182       *
 183       * @return array List of size (int) => text description for display
 184       */
 185      public static function get_size_choices() {
 186          $options = array();
 187          for ($size = self::MIN_SIZE; $size <= self::MAX_SIZE; $size++) {
 188              $options[$size] = get_string('coursesize_' . $size, 'tool_generator');
 189          }
 190          return $options;
 191      }
 192  
 193      /**
 194       * Checks that a shortname is available (unused).
 195       *
 196       * @param string $shortname Proposed course shortname
 197       * @return string An error message if the name is unavailable or '' if OK
 198       */
 199      public static function check_shortname_available($shortname) {
 200          global $DB;
 201          $fullname = $DB->get_field('course', 'fullname',
 202                  array('shortname' => $shortname), IGNORE_MISSING);
 203          if ($fullname !== false) {
 204              // I wanted to throw an exception here but it is not possible to
 205              // use strings from moodle.php in exceptions, and I didn't want
 206              // to duplicate the string in tool_generator, so I changed this to
 207              // not use exceptions.
 208              return get_string('shortnametaken', 'moodle', $fullname);
 209          }
 210          return '';
 211      }
 212  
 213      /**
 214       * Runs the entire 'make' process.
 215       *
 216       * @return int Course id
 217       */
 218      public function make() {
 219          global $DB, $CFG, $USER;
 220          require_once($CFG->dirroot . '/lib/phpunit/classes/util.php');
 221  
 222          raise_memory_limit(MEMORY_EXTRA);
 223  
 224          if ($this->progress && !CLI_SCRIPT) {
 225              echo html_writer::start_tag('ul');
 226          }
 227  
 228          $entirestart = microtime(true);
 229  
 230          // Get generator.
 231          $this->generator = phpunit_util::get_data_generator();
 232  
 233          // Make course.
 234          $this->course = $this->create_course();
 235  
 236          $this->create_assignments();
 237          $this->create_pages();
 238          $this->create_small_files();
 239          $this->create_big_files();
 240  
 241          // Create users as late as possible to reduce regarding in the gradebook.
 242          $this->create_users();
 243          $this->create_forum();
 244  
 245          // Let plugins hook into user settings navigation.
 246          $pluginsfunction = get_plugins_with_function('course_backend_generator_create_activity');
 247          foreach ($pluginsfunction as $plugintype => $plugins) {
 248              foreach ($plugins as $pluginname => $pluginfunction) {
 249                  if (in_array($pluginname, $this->additionalmodules)) {
 250                      $pluginfunction($this, $this->generator, $this->course->id, self::$paramactivities[$this->size]);
 251                  }
 252              }
 253          }
 254  
 255          // We are checking 'enroladminnewcourse' setting to decide to enrol admins or not.
 256          if (!empty($CFG->creatornewroleid) && !empty($CFG->enroladminnewcourse) && is_siteadmin($USER->id)) {
 257              // Deal with course creators - enrol them internally with default role.
 258              enrol_try_internal_enrol($this->course->id, $USER->id, $CFG->creatornewroleid);
 259          }
 260  
 261          // Log total time.
 262          $this->log('coursecompleted', round(microtime(true) - $entirestart, 1));
 263  
 264          if ($this->progress && !CLI_SCRIPT) {
 265              echo html_writer::end_tag('ul');
 266          }
 267  
 268          return $this->course->id;
 269      }
 270  
 271      /**
 272       * Creates the actual course.
 273       *
 274       * @return stdClass Course record
 275       */
 276      private function create_course() {
 277          $this->log('createcourse', $this->shortname);
 278          $courserecord = array(
 279              'shortname' => $this->shortname,
 280              'fullname' => $this->fullname,
 281              'numsections' => self::$paramsections[$this->size],
 282              'startdate' => usergetmidnight(time())
 283          );
 284          if (strlen($this->summary) > 0) {
 285              $courserecord['summary'] = $this->summary;
 286              $courserecord['summary_format'] = $this->summaryformat;
 287          }
 288  
 289          return $this->generator->create_course($courserecord, array('createsections' => true));
 290      }
 291  
 292      /**
 293       * Creates a number of user accounts and enrols them on the course.
 294       * Note: Existing user accounts that were created by this system are
 295       * reused if available.
 296       */
 297      private function create_users() {
 298          global $DB;
 299  
 300          // Work out total number of users.
 301          $count = self::$paramusers[$this->size];
 302  
 303          // Get existing users in order. We will 'fill up holes' in this up to
 304          // the required number.
 305          $this->log('checkaccounts', $count);
 306          $nextnumber = 1;
 307          $rs = $DB->get_recordset_select('user', $DB->sql_like('username', '?'),
 308                  array('tool_generator_%'), 'username', 'id, username');
 309          foreach ($rs as $rec) {
 310              // Extract number from username.
 311              $matches = array();
 312              if (!preg_match('~^tool_generator_([0-9]{6})$~', $rec->username, $matches)) {
 313                  continue;
 314              }
 315              $number = (int)$matches[1];
 316  
 317              // Create missing users in range up to this.
 318              if ($number != $nextnumber) {
 319                  $this->create_user_accounts($nextnumber, min($number - 1, $count));
 320              } else {
 321                  $this->userids[$number] = (int)$rec->id;
 322              }
 323  
 324              // Stop if we've got enough users.
 325              $nextnumber = $number + 1;
 326              if ($number >= $count) {
 327                  break;
 328              }
 329          }
 330          $rs->close();
 331  
 332          // Create users from end of existing range.
 333          if ($nextnumber <= $count) {
 334              $this->create_user_accounts($nextnumber, $count);
 335          }
 336  
 337          // Assign all users to course.
 338          $this->log('enrol', $count, true);
 339  
 340          $enrolplugin = enrol_get_plugin('manual');
 341          $instances = enrol_get_instances($this->course->id, true);
 342          foreach ($instances as $instance) {
 343              if ($instance->enrol === 'manual') {
 344                  break;
 345              }
 346          }
 347          if ($instance->enrol !== 'manual') {
 348              throw new coding_exception('No manual enrol plugin in course');
 349          }
 350          $role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
 351  
 352          for ($number = 1; $number <= $count; $number++) {
 353              // Enrol user.
 354              $enrolplugin->enrol_user($instance, $this->userids[$number], $role->id);
 355              $this->dot($number, $count);
 356          }
 357  
 358          // Sets the pointer at the beginning to be aware of the users we use.
 359          reset($this->userids);
 360  
 361          $this->end_log();
 362      }
 363  
 364      /**
 365       * Creates user accounts with a numeric range.
 366       *
 367       * @param int $first Number of first user
 368       * @param int $last Number of last user
 369       */
 370      private function create_user_accounts($first, $last) {
 371          global $CFG;
 372  
 373          $this->log('createaccounts', (object)array('from' => $first, 'to' => $last), true);
 374          $count = $last - $first + 1;
 375          $done = 0;
 376          for ($number = $first; $number <= $last; $number++, $done++) {
 377              // Work out username with 6-digit number.
 378              $textnumber = (string)$number;
 379              while (strlen($textnumber) < 6) {
 380                  $textnumber = '0' . $textnumber;
 381              }
 382              $username = 'tool_generator_' . $textnumber;
 383  
 384              // Create user account.
 385              $record = array('username' => $username, 'idnumber' => $number);
 386  
 387              // We add a user password if it has been specified.
 388              if (!empty($CFG->tool_generator_users_password)) {
 389                  $record['password'] = $CFG->tool_generator_users_password;
 390              }
 391  
 392              $user = $this->generator->create_user($record);
 393              $this->userids[$number] = (int)$user->id;
 394              $this->dot($done, $count);
 395          }
 396          $this->end_log();
 397      }
 398  
 399      /**
 400       * Creates a number of Assignment activities.
 401       */
 402      private function create_assignments() {
 403          // Set up generator.
 404          $assigngenerator = $this->generator->get_plugin_generator('mod_assign');
 405  
 406          // Create assignments.
 407          $number = self::$paramassignments[$this->size];
 408          $this->log('createassignments', $number, true);
 409          for ($i = 0; $i < $number; $i++) {
 410              $record = array('course' => $this->course);
 411              $options = array('section' => $this->get_target_section());
 412              $assigngenerator->create_instance($record, $options);
 413              $this->dot($i, $number);
 414          }
 415  
 416          $this->end_log();
 417      }
 418  
 419      /**
 420       * Creates a number of Page activities.
 421       */
 422      private function create_pages() {
 423          // Set up generator.
 424          $pagegenerator = $this->generator->get_plugin_generator('mod_page');
 425  
 426          // Create pages.
 427          $number = self::$parampages[$this->size];
 428          $this->log('createpages', $number, true);
 429          for ($i = 0; $i < $number; $i++) {
 430              $record = array('course' => $this->course);
 431              $options = array('section' => $this->get_target_section());
 432              $pagegenerator->create_instance($record, $options);
 433              $this->dot($i, $number);
 434          }
 435  
 436          $this->end_log();
 437      }
 438  
 439      /**
 440       * Creates one resource activity with a lot of small files.
 441       */
 442      private function create_small_files() {
 443          $count = self::$paramsmallfilecount[$this->size];
 444          $this->log('createsmallfiles', $count, true);
 445  
 446          // Create resource with default textfile only.
 447          $resourcegenerator = $this->generator->get_plugin_generator('mod_resource');
 448          $record = array('course' => $this->course,
 449                  'name' => get_string('smallfiles', 'tool_generator'));
 450          $options = array('section' => 0);
 451          $resource = $resourcegenerator->create_instance($record, $options);
 452  
 453          // Add files.
 454          $fs = get_file_storage();
 455          $context = context_module::instance($resource->cmid);
 456          $filerecord = array('component' => 'mod_resource', 'filearea' => 'content',
 457                  'contextid' => $context->id, 'itemid' => 0, 'filepath' => '/');
 458          for ($i = 0; $i < $count; $i++) {
 459              $filerecord['filename'] = 'smallfile' . $i . '.dat';
 460  
 461              // Generate random binary data (different for each file so it
 462              // doesn't compress unrealistically).
 463              $data = random_bytes_emulate($this->limit_filesize(self::$paramsmallfilesize[$this->size]));
 464  
 465              $fs->create_file_from_string($filerecord, $data);
 466              $this->dot($i, $count);
 467          }
 468  
 469          $this->end_log();
 470      }
 471  
 472      /**
 473       * Creates a number of resource activities with one big file each.
 474       */
 475      private function create_big_files() {
 476          // Work out how many files and how many blocks to use (up to 64KB).
 477          $count = self::$parambigfilecount[$this->size];
 478          $filesize = $this->limit_filesize(self::$parambigfilesize[$this->size]);
 479          $blocks = ceil($filesize / 65536);
 480          $blocksize = floor($filesize / $blocks);
 481  
 482          $this->log('createbigfiles', $count, true);
 483  
 484          // Prepare temp area.
 485          $tempfolder = make_temp_directory('tool_generator');
 486          $tempfile = $tempfolder . '/' . rand();
 487  
 488          // Create resources and files.
 489          $fs = get_file_storage();
 490          $resourcegenerator = $this->generator->get_plugin_generator('mod_resource');
 491          for ($i = 0; $i < $count; $i++) {
 492              // Create resource.
 493              $record = array('course' => $this->course,
 494                      'name' => get_string('bigfile', 'tool_generator', $i));
 495              $options = array('section' => $this->get_target_section());
 496              $resource = $resourcegenerator->create_instance($record, $options);
 497  
 498              // Write file.
 499              $handle = fopen($tempfile, 'w');
 500              if (!$handle) {
 501                  throw new coding_exception('Failed to open temporary file');
 502              }
 503              for ($j = 0; $j < $blocks; $j++) {
 504                  $data = random_bytes_emulate($blocksize);
 505                  fwrite($handle, $data);
 506                  $this->dot($i * $blocks + $j, $count * $blocks);
 507              }
 508              fclose($handle);
 509  
 510              // Add file.
 511              $context = context_module::instance($resource->cmid);
 512              $filerecord = array('component' => 'mod_resource', 'filearea' => 'content',
 513                      'contextid' => $context->id, 'itemid' => 0, 'filepath' => '/',
 514                      'filename' => 'bigfile' . $i . '.dat');
 515              $fs->create_file_from_pathname($filerecord, $tempfile);
 516          }
 517  
 518          unlink($tempfile);
 519          $this->end_log();
 520      }
 521  
 522      /**
 523       * Creates one forum activity with a bunch of posts.
 524       */
 525      private function create_forum() {
 526          global $DB;
 527  
 528          $discussions = self::$paramforumdiscussions[$this->size];
 529          $posts = self::$paramforumposts[$this->size];
 530          $totalposts = $discussions * $posts;
 531  
 532          $this->log('createforum', $totalposts, true);
 533  
 534          // Create empty forum.
 535          $forumgenerator = $this->generator->get_plugin_generator('mod_forum');
 536          $record = array('course' => $this->course,
 537                  'name' => get_string('pluginname', 'forum'));
 538          $options = array('section' => 0);
 539          $forum = $forumgenerator->create_instance($record, $options);
 540  
 541          // Add discussions and posts.
 542          $sofar = 0;
 543          for ($i = 0; $i < $discussions; $i++) {
 544              $record = array('forum' => $forum->id, 'course' => $this->course->id,
 545                      'userid' => $this->get_target_user());
 546              $discussion = $forumgenerator->create_discussion($record);
 547              $parentid = $DB->get_field('forum_posts', 'id', array('discussion' => $discussion->id), MUST_EXIST);
 548              $sofar++;
 549              for ($j = 0; $j < $posts - 1; $j++, $sofar++) {
 550                  $record = array('discussion' => $discussion->id,
 551                          'userid' => $this->get_target_user(), 'parent' => $parentid);
 552                  $forumgenerator->create_post($record);
 553                  $this->dot($sofar, $totalposts);
 554              }
 555          }
 556  
 557          $this->end_log();
 558      }
 559  
 560      /**
 561       * Gets a section number.
 562       *
 563       * Depends on $this->fixeddataset.
 564       *
 565       * @return int A section number from 1 to the number of sections
 566       */
 567      public function get_target_section() {
 568  
 569          if (!$this->fixeddataset) {
 570              $key = rand(1, self::$paramsections[$this->size]);
 571          } else {
 572              // Using section 1.
 573              $key = 1;
 574          }
 575  
 576          return $key;
 577      }
 578  
 579      /**
 580       * Gets a user id.
 581       *
 582       * Depends on $this->fixeddataset.
 583       *
 584       * @return int A user id for a random created user
 585       */
 586      private function get_target_user() {
 587  
 588          if (!$this->fixeddataset) {
 589              $userid = $this->userids[rand(1, self::$paramusers[$this->size])];
 590          } else if ($userid = current($this->userids)) {
 591              // Moving pointer to the next user.
 592              next($this->userids);
 593          } else {
 594              // Returning to the beginning if we reached the end.
 595              $userid = reset($this->userids);
 596          }
 597  
 598          return $userid;
 599      }
 600  
 601      /**
 602       * Restricts the binary file size if necessary
 603       *
 604       * @param int $length The total length
 605       * @return int The limited length if a limit was specified.
 606       */
 607      private function limit_filesize($length) {
 608  
 609          // Limit to $this->filesizelimit.
 610          if (is_numeric($this->filesizelimit) && $length > $this->filesizelimit) {
 611              $length = floor($this->filesizelimit);
 612          }
 613  
 614          return $length;
 615      }
 616  }