2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 defined('MOODLE_INTERNAL') || die();
20 * Backend code for the 'make large course' tool.
22 * @package tool_generator
23 * @copyright 2013 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 class tool_generator_backend {
28 * @var int Lowest (smallest) size index
32 * @var int Highest (largest) size index
36 * @var int Default size index
38 const DEFAULT_SIZE = 3;
41 * @var array Number of sections in course
43 private static $paramsections = array(1, 10, 100, 500, 1000, 2000);
45 * @var array Number of Page activities in course
47 private static $parampages = array(1, 50, 200, 1000, 5000, 10000);
49 * @var array Number of students enrolled in course
51 private static $paramusers = array(1, 100, 1000, 10000, 50000, 100000);
53 * Total size of small files: 1KB, 1MB, 10MB, 100MB, 1GB, 2GB.
55 * @var array Number of small files created in a single file activity
57 private static $paramsmallfilecount = array(1, 64, 128, 1024, 16384, 32768);
59 * @var array Size of small files (to make the totals into nice numbers)
61 private static $paramsmallfilesize = array(1024, 16384, 81920, 102400, 65536, 65536);
63 * Total size of big files: 8KB, 8MB, 80MB, 800MB, 8GB, 16GB.
65 * @var array Number of big files created as individual file activities
67 private static $parambigfilecount = array(1, 2, 5, 10, 10, 10);
69 * @var array Size of each large file
71 private static $parambigfilesize = array(8192, 4194304, 16777216, 83886080,
72 858993459, 1717986918);
74 * @var array Number of forum discussions
76 private static $paramforumdiscussions = array(1, 10, 100, 500, 1000, 2000);
78 * @var array Number of forum posts per discussion
80 private static $paramforumposts = array(2, 2, 5, 10, 10, 10);
83 * @var string Course shortname
88 * @var int Size code (index in the above arrays)
93 * @var bool True if displaying progress
98 * @var testing_data_generator Data generator
103 * @var stdClass Course object
108 * @var int Epoch time at which last dot was displayed
113 * @var int Epoch time at which last percentage was displayed
115 private $lastpercentage;
118 * @var int Epoch time at which current step (current set of dots) started
123 * @var array Array from test user number (1...N) to userid in database
128 * Constructs object ready to create course.
130 * @param string $shortname Course shortname
131 * @param int $size Size as numeric index
132 * @param bool $progress True if progress information should be displayed
133 * @return int Course id
134 * @throws coding_exception If parameters are invalid
136 public function __construct($shortname, $size, $progress = true) {
138 if ($size < self::MIN_SIZE || $size > self::MAX_SIZE) {
139 throw new coding_exception('Invalid size');
143 $this->shortname = $shortname;
145 $this->progress = $progress;
149 * Gets a list of size choices supported by this backend.
151 * @return array List of size (int) => text description for display
153 public static function get_size_choices() {
155 for ($size = self::MIN_SIZE; $size <= self::MAX_SIZE; $size++) {
156 $options[$size] = get_string('size_' . $size, 'tool_generator');
162 * Converts a size name into the numeric constant.
164 * @param string $sizename Size name e.g. 'L'
165 * @return int Numeric version
166 * @throws coding_exception If the size name is not known
168 public static function size_for_name($sizename) {
169 for ($size = self::MIN_SIZE; $size <= self::MAX_SIZE; $size++) {
170 if ($sizename == get_string('shortsize_' . $size, 'tool_generator')) {
174 throw new coding_exception("Unknown size name '$sizename'");
178 * Checks that a shortname is available (unused).
180 * @param string $shortname Proposed course shortname
181 * @return string An error message if the name is unavailable or '' if OK
183 public static function check_shortname_available($shortname) {
185 $fullname = $DB->get_field('course', 'fullname',
186 array('shortname' => $shortname), IGNORE_MISSING);
187 if ($fullname !== false) {
188 // I wanted to throw an exception here but it is not possible to
189 // use strings from moodle.php in exceptions, and I didn't want
190 // to duplicate the string in tool_generator, so I changed this to
191 // not use exceptions.
192 return get_string('shortnametaken', 'moodle', $fullname);
198 * Runs the entire 'make' process.
200 * @return int Course id
202 public function make() {
204 require_once($CFG->dirroot . '/lib/phpunit/classes/util.php');
206 raise_memory_limit(MEMORY_EXTRA);
208 if ($this->progress && !CLI_SCRIPT) {
209 echo html_writer::start_tag('ul');
212 $entirestart = microtime(true);
214 // Start transaction.
215 $transaction = $DB->start_delegated_transaction();
218 $this->generator = phpunit_util::get_data_generator();
221 $this->course = $this->create_course();
222 $this->create_users();
223 $this->create_pages();
224 $this->create_small_files();
225 $this->create_big_files();
226 $this->create_forum();
229 $this->log('complete', round(microtime(true) - $entirestart, 1));
231 if ($this->progress && !CLI_SCRIPT) {
232 echo html_writer::end_tag('ul');
235 // Commit transaction and finish.
236 $transaction->allow_commit();
237 return $this->course->id;
241 * Creates the actual course.
243 * @return stdClass Course record
245 private function create_course() {
246 $this->log('createcourse', $this->shortname);
247 $courserecord = array('shortname' => $this->shortname,
248 'fullname' => get_string('fullname', 'tool_generator',
249 array('size' => get_string('shortsize_' . $this->size, 'tool_generator'))),
250 'numsections' => self::$paramsections[$this->size]);
251 return $this->generator->create_course($courserecord, array('createsections' => true));
255 * Creates a number of user accounts and enrols them on the course.
256 * Note: Existing user accounts that were created by this system are
257 * reused if available.
259 private function create_users() {
262 // Work out total number of users.
263 $count = self::$paramusers[$this->size];
265 // Get existing users in order. We will 'fill up holes' in this up to
266 // the required number.
267 $this->log('checkaccounts', $count);
269 $rs = $DB->get_recordset_select('user', 'username LIKE ?',
270 array('tool_generator_%'), 'username', 'id, username');
271 foreach ($rs as $rec) {
272 // Extract number from username.
274 if (!preg_match('~^tool_generator_([0-9]{6})$~', $rec->username, $matches)) {
277 $number = (int)$matches[1];
279 // Create missing users in range up to this.
280 if ($number != $nextnumber) {
281 $this->create_user_accounts($nextnumber, min($number - 1, $count));
283 $this->userids[$number] = (int)$rec->id;
286 // Stop if we've got enough users.
287 $nextnumber = $number + 1;
288 if ($number >= $count) {
294 // Create users from end of existing range.
295 if ($nextnumber <= $count) {
296 $this->create_user_accounts($nextnumber, $count);
299 // Assign all users to course.
300 $this->log('enrol', $count, true);
302 $enrolplugin = enrol_get_plugin('manual');
303 $instances = enrol_get_instances($this->course->id, true);
304 foreach ($instances as $instance) {
305 if ($instance->enrol === 'manual') {
309 if ($instance->enrol !== 'manual') {
310 throw new coding_exception('No manual enrol plugin in course');
312 $role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
314 for ($number = 1; $number <= $count; $number++) {
316 $enrolplugin->enrol_user($instance, $this->userids[$number], $role->id);
317 $this->dot($number, $count);
324 * Creates user accounts with a numeric range.
326 * @param int $first Number of first user
327 * @param int $last Number of last user
329 private function create_user_accounts($first, $last) {
330 $this->log('createaccounts', (object)array('from' => $first, 'to' => $last), true);
331 $count = $last - $first + 1;
333 for ($number = $first; $number <= $last; $number++, $done++) {
334 // Work out username with 6-digit number.
335 $textnumber = (string)$number;
336 while (strlen($textnumber) < 6) {
337 $textnumber = '0' . $textnumber;
339 $username = 'tool_generator_' . $textnumber;
341 // Create user account.
342 $record = array('firstname' => get_string('firstname', 'tool_generator'),
343 'lastname' => $number, 'username' => $username);
344 $user = $this->generator->create_user($record);
345 $this->userids[$number] = (int)$user->id;
346 $this->dot($done, $count);
352 * Creates a number of Page activities.
354 private function create_pages() {
356 $pagegenerator = $this->generator->get_plugin_generator('mod_page');
359 $number = self::$parampages[$this->size];
360 $this->log('createpages', $number, true);
361 for ($i=0; $i<$number; $i++) {
362 $record = array('course' => $this->course->id);
363 $options = array('section' => $this->get_random_section());
364 $pagegenerator->create_instance($record, $options);
365 $this->dot($i, $number);
372 * Creates one resource activity with a lot of small files.
374 private function create_small_files() {
375 $count = self::$paramsmallfilecount[$this->size];
376 $this->log('createsmallfiles', $count, true);
378 // Create resource with default textfile only.
379 $resourcegenerator = $this->generator->get_plugin_generator('mod_resource');
380 $record = array('course' => $this->course->id,
381 'name' => get_string('smallfiles', 'tool_generator'));
382 $options = array('section' => 0);
383 $resource = $resourcegenerator->create_instance($record, $options);
386 $fs = get_file_storage();
387 $context = context_module::instance($resource->cmid);
388 $filerecord = array('component' => 'mod_resource', 'filearea' => 'content',
389 'contextid' => $context->id, 'itemid' => 0, 'filepath' => '/');
390 for ($i = 0; $i < $count; $i++) {
391 $filerecord['filename'] = 'smallfile' . $i . '.dat';
393 // Generate random binary data (different for each file so it
394 // doesn't compress unrealistically).
395 $data = self::get_random_binary(self::$paramsmallfilesize[$this->size]);
397 $fs->create_file_from_string($filerecord, $data);
398 $this->dot($i, $count);
405 * Creates a string of random binary data. The start of the string includes
406 * the current time, in an attempt to avoid large-scale repetition.
408 * @param int $length Number of bytes
409 * @return Random data
411 private static function get_random_binary($length) {
412 $data = microtime(true);
413 if (strlen($data) > $length) {
414 // Use last digits of data.
415 return substr($data, -$length);
417 $length -= strlen($data);
418 for ($j=0; $j < $length; $j++) {
419 $data .= chr(rand(1, 255));
425 * Creates a number of resource activities with one big file each.
427 private function create_big_files() {
430 // Work out how many files and how many blocks to use (up to 64KB).
431 $count = self::$parambigfilecount[$this->size];
432 $blocks = ceil(self::$parambigfilesize[$this->size] / 65536);
433 $blocksize = floor(self::$parambigfilesize[$this->size] / $blocks);
435 $this->log('createbigfiles', $count, true);
437 // Prepare temp area.
438 $tempfolder = make_temp_directory('tool_generator');
439 $tempfile = $tempfolder . '/' . rand();
441 // Create resources and files.
442 $fs = get_file_storage();
443 $resourcegenerator = $this->generator->get_plugin_generator('mod_resource');
444 for ($i = 0; $i < $count; $i++) {
446 $record = array('course' => $this->course->id,
447 'name' => get_string('bigfile', 'tool_generator', $i));
448 $options = array('section' => $this->get_random_section());
449 $resource = $resourcegenerator->create_instance($record, $options);
452 $handle = fopen($tempfile, 'w');
454 throw new coding_exception('Failed to open temporary file');
456 for ($j = 0; $j < $blocks; $j++) {
457 $data = self::get_random_binary($blocksize);
458 fwrite($handle, $data);
459 $this->dot($i * $blocks + $j, $count * $blocks);
464 $context = context_module::instance($resource->cmid);
465 $filerecord = array('component' => 'mod_resource', 'filearea' => 'content',
466 'contextid' => $context->id, 'itemid' => 0, 'filepath' => '/',
467 'filename' => 'bigfile' . $i . '.dat');
468 $fs->create_file_from_pathname($filerecord, $tempfile);
476 * Creates one forum activity with a bunch of posts.
478 private function create_forum() {
481 $discussions = self::$paramforumdiscussions[$this->size];
482 $posts = self::$paramforumposts[$this->size];
483 $totalposts = $discussions * $posts;
485 $this->log('createforum', $totalposts, true);
487 // Create empty forum.
488 $forumgenerator = $this->generator->get_plugin_generator('mod_forum');
489 $record = array('course' => $this->course->id,
490 'name' => get_string('pluginname', 'forum'));
491 $options = array('section' => 0);
492 $forum = $forumgenerator->create_instance($record, $options);
494 // Add discussions and posts.
496 for ($i=0; $i < $discussions; $i++) {
497 $record = array('forum' => $forum->id, 'course' => $this->course->id,
498 'userid' => $this->get_random_user());
499 $discussion = $forumgenerator->create_discussion($record);
500 $parentid = $DB->get_field('forum_posts', 'id', array('discussion' => $discussion->id), MUST_EXIST);
502 for ($j=0; $j < $posts - 1; $j++, $sofar++) {
503 $record = array('discussion' => $discussion->id,
504 'userid' => $this->get_random_user(), 'parent' => $parentid);
505 $forumgenerator->create_post($record);
506 $this->dot($sofar, $totalposts);
514 * Gets a random section number.
516 * @return int A section number from 1 to the number of sections
518 private function get_random_section() {
519 return rand(1, self::$paramsections[$this->size]);
523 * Gets a random user id.
525 * @return int A user id for a random created user
527 private function get_random_user() {
528 return $this->userids[rand(1, self::$paramusers[$this->size])];
532 * Displays information as part of progress.
533 * @param string $langstring Part of langstring (after progress_)
534 * @param mixed $a Optional lang string parameters
535 * @param bool $leaveopen If true, doesn't close LI tag (ready for dots)
537 private function log($langstring, $a = null, $leaveopen = false) {
538 if (!$this->progress) {
544 echo html_writer::start_tag('li');
546 echo get_string('progress_' . $langstring, 'tool_generator', $a);
551 echo html_writer::end_tag('li');
555 $this->lastdot = time();
556 $this->lastpercentage = $this->lastdot;
557 $this->starttime = microtime(true);
562 * Outputs dots. There is up to one dot per second. Once a minute, it
563 * displays a percentage.
564 * @param int $number Number of completed items
565 * @param int $total Total number of items to complete
567 private function dot($number, $total) {
568 if (!$this->progress) {
572 if ($now == $this->lastdot) {
575 $this->lastdot = $now;
581 if ($now - $this->lastpercentage >= 30) {
582 echo round(100.0 * $number / $total, 1) . '%';
583 $this->lastpercentage = $now;
586 // Update time limit so PHP doesn't time out.
593 * Ends a log string that was started using log function with $leaveopen.
595 private function end_log() {
596 if (!$this->progress) {
599 echo get_string('done', 'tool_generator', round(microtime(true) - $this->starttime, 1));
603 echo html_writer::end_tag('li');