Merge branch 'MDL-41402_master' of git://github.com/dmonllao/moodle
This commit is contained in:
@@ -14,16 +14,25 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Backend code for the 'make large course' tool.
|
||||
* Backend generic code.
|
||||
*
|
||||
* @package tool_generator
|
||||
* @copyright 2013 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class tool_generator_backend {
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Backend generic code for all tool_generator commands.
|
||||
*
|
||||
* @abstract
|
||||
* @package tool_generator
|
||||
* @copyright 2013 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
abstract class tool_generator_backend {
|
||||
/**
|
||||
* @var int Lowest (smallest) size index
|
||||
*/
|
||||
@@ -37,134 +46,57 @@ class tool_generator_backend {
|
||||
*/
|
||||
const DEFAULT_SIZE = 3;
|
||||
|
||||
/**
|
||||
* @var array Number of sections in course
|
||||
*/
|
||||
private static $paramsections = array(1, 10, 100, 500, 1000, 2000);
|
||||
/**
|
||||
* @var array Number of Page activities in course
|
||||
*/
|
||||
private static $parampages = array(1, 50, 200, 1000, 5000, 10000);
|
||||
/**
|
||||
* @var array Number of students enrolled in course
|
||||
*/
|
||||
private static $paramusers = array(1, 100, 1000, 10000, 50000, 100000);
|
||||
/**
|
||||
* Total size of small files: 1KB, 1MB, 10MB, 100MB, 1GB, 2GB.
|
||||
*
|
||||
* @var array Number of small files created in a single file activity
|
||||
*/
|
||||
private static $paramsmallfilecount = array(1, 64, 128, 1024, 16384, 32768);
|
||||
/**
|
||||
* @var array Size of small files (to make the totals into nice numbers)
|
||||
*/
|
||||
private static $paramsmallfilesize = array(1024, 16384, 81920, 102400, 65536, 65536);
|
||||
/**
|
||||
* Total size of big files: 8KB, 8MB, 80MB, 800MB, 8GB, 16GB.
|
||||
*
|
||||
* @var array Number of big files created as individual file activities
|
||||
*/
|
||||
private static $parambigfilecount = array(1, 2, 5, 10, 10, 10);
|
||||
/**
|
||||
* @var array Size of each large file
|
||||
*/
|
||||
private static $parambigfilesize = array(8192, 4194304, 16777216, 83886080,
|
||||
858993459, 1717986918);
|
||||
/**
|
||||
* @var array Number of forum discussions
|
||||
*/
|
||||
private static $paramforumdiscussions = array(1, 10, 100, 500, 1000, 2000);
|
||||
/**
|
||||
* @var array Number of forum posts per discussion
|
||||
*/
|
||||
private static $paramforumposts = array(2, 2, 5, 10, 10, 10);
|
||||
|
||||
/**
|
||||
* @var string Course shortname
|
||||
*/
|
||||
private $shortname;
|
||||
|
||||
/**
|
||||
* @var int Size code (index in the above arrays)
|
||||
*/
|
||||
private $size;
|
||||
|
||||
/**
|
||||
* @var bool True if we want a fixed dataset or false to generate random data
|
||||
*/
|
||||
private $fixeddataset;
|
||||
protected $fixeddataset;
|
||||
|
||||
/**
|
||||
* @var bool True if displaying progress
|
||||
*/
|
||||
private $progress;
|
||||
|
||||
/**
|
||||
* @var testing_data_generator Data generator
|
||||
*/
|
||||
private $generator;
|
||||
|
||||
/**
|
||||
* @var stdClass Course object
|
||||
*/
|
||||
private $course;
|
||||
protected $progress;
|
||||
|
||||
/**
|
||||
* @var int Epoch time at which last dot was displayed
|
||||
*/
|
||||
private $lastdot;
|
||||
protected $lastdot;
|
||||
|
||||
/**
|
||||
* @var int Epoch time at which last percentage was displayed
|
||||
*/
|
||||
private $lastpercentage;
|
||||
protected $lastpercentage;
|
||||
|
||||
/**
|
||||
* @var int Epoch time at which current step (current set of dots) started
|
||||
*/
|
||||
private $starttime;
|
||||
protected $starttime;
|
||||
|
||||
/**
|
||||
* @var array Array from test user number (1...N) to userid in database
|
||||
* @var int Size code (index in the above arrays)
|
||||
*/
|
||||
private $userids;
|
||||
protected $size;
|
||||
|
||||
/**
|
||||
* Constructs object ready to create course.
|
||||
* Generic generator class
|
||||
*
|
||||
* @param string $shortname Course shortname
|
||||
* @param int $size Size as numeric index
|
||||
* @param bool $fixeddataset To use fixed or random data
|
||||
* @param bool $progress True if progress information should be displayed
|
||||
* @return int Course id
|
||||
* @throws coding_exception If parameters are invalid
|
||||
*/
|
||||
public function __construct($shortname, $size, $fixeddataset = false, $progress = true) {
|
||||
public function __construct($size, $fixeddataset = false, $progress = true) {
|
||||
|
||||
// Check parameter.
|
||||
if ($size < self::MIN_SIZE || $size > self::MAX_SIZE) {
|
||||
throw new coding_exception('Invalid size');
|
||||
}
|
||||
|
||||
// Set parameters.
|
||||
$this->shortname = $shortname;
|
||||
$this->size = $size;
|
||||
$this->fixeddataset = $fixeddataset;
|
||||
$this->progress = $progress;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of size choices supported by this backend.
|
||||
*
|
||||
* @return array List of size (int) => text description for display
|
||||
*/
|
||||
public static function get_size_choices() {
|
||||
$options = array();
|
||||
for ($size = self::MIN_SIZE; $size <= self::MAX_SIZE; $size++) {
|
||||
$options[$size] = get_string('size_' . $size, 'tool_generator');
|
||||
}
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a size name into the numeric constant.
|
||||
*
|
||||
@@ -181,393 +113,13 @@ class tool_generator_backend {
|
||||
throw new coding_exception("Unknown size name '$sizename'");
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that a shortname is available (unused).
|
||||
*
|
||||
* @param string $shortname Proposed course shortname
|
||||
* @return string An error message if the name is unavailable or '' if OK
|
||||
*/
|
||||
public static function check_shortname_available($shortname) {
|
||||
global $DB;
|
||||
$fullname = $DB->get_field('course', 'fullname',
|
||||
array('shortname' => $shortname), IGNORE_MISSING);
|
||||
if ($fullname !== false) {
|
||||
// I wanted to throw an exception here but it is not possible to
|
||||
// use strings from moodle.php in exceptions, and I didn't want
|
||||
// to duplicate the string in tool_generator, so I changed this to
|
||||
// not use exceptions.
|
||||
return get_string('shortnametaken', 'moodle', $fullname);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the entire 'make' process.
|
||||
*
|
||||
* @return int Course id
|
||||
*/
|
||||
public function make() {
|
||||
global $DB, $CFG;
|
||||
require_once($CFG->dirroot . '/lib/phpunit/classes/util.php');
|
||||
|
||||
raise_memory_limit(MEMORY_EXTRA);
|
||||
|
||||
if ($this->progress && !CLI_SCRIPT) {
|
||||
echo html_writer::start_tag('ul');
|
||||
}
|
||||
|
||||
$entirestart = microtime(true);
|
||||
|
||||
// Start transaction.
|
||||
$transaction = $DB->start_delegated_transaction();
|
||||
|
||||
// Get generator.
|
||||
$this->generator = phpunit_util::get_data_generator();
|
||||
|
||||
// Make course.
|
||||
$this->course = $this->create_course();
|
||||
$this->create_users();
|
||||
$this->create_pages();
|
||||
$this->create_small_files();
|
||||
$this->create_big_files();
|
||||
$this->create_forum();
|
||||
|
||||
// Log total time.
|
||||
$this->log('complete', round(microtime(true) - $entirestart, 1));
|
||||
|
||||
if ($this->progress && !CLI_SCRIPT) {
|
||||
echo html_writer::end_tag('ul');
|
||||
}
|
||||
|
||||
// Commit transaction and finish.
|
||||
$transaction->allow_commit();
|
||||
return $this->course->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the actual course.
|
||||
*
|
||||
* @return stdClass Course record
|
||||
*/
|
||||
private function create_course() {
|
||||
$this->log('createcourse', $this->shortname);
|
||||
$courserecord = array('shortname' => $this->shortname,
|
||||
'fullname' => get_string('fullname', 'tool_generator',
|
||||
array('size' => get_string('shortsize_' . $this->size, 'tool_generator'))),
|
||||
'numsections' => self::$paramsections[$this->size]);
|
||||
return $this->generator->create_course($courserecord, array('createsections' => true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a number of user accounts and enrols them on the course.
|
||||
* Note: Existing user accounts that were created by this system are
|
||||
* reused if available.
|
||||
*/
|
||||
private function create_users() {
|
||||
global $DB;
|
||||
|
||||
// Work out total number of users.
|
||||
$count = self::$paramusers[$this->size];
|
||||
|
||||
// Get existing users in order. We will 'fill up holes' in this up to
|
||||
// the required number.
|
||||
$this->log('checkaccounts', $count);
|
||||
$nextnumber = 1;
|
||||
$rs = $DB->get_recordset_select('user', $DB->sql_like('username', '?'),
|
||||
array('tool_generator_%'), 'username', 'id, username');
|
||||
foreach ($rs as $rec) {
|
||||
// Extract number from username.
|
||||
$matches = array();
|
||||
if (!preg_match('~^tool_generator_([0-9]{6})$~', $rec->username, $matches)) {
|
||||
continue;
|
||||
}
|
||||
$number = (int)$matches[1];
|
||||
|
||||
// Create missing users in range up to this.
|
||||
if ($number != $nextnumber) {
|
||||
$this->create_user_accounts($nextnumber, min($number - 1, $count));
|
||||
} else {
|
||||
$this->userids[$number] = (int)$rec->id;
|
||||
}
|
||||
|
||||
// Stop if we've got enough users.
|
||||
$nextnumber = $number + 1;
|
||||
if ($number >= $count) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
$rs->close();
|
||||
|
||||
// Create users from end of existing range.
|
||||
if ($nextnumber <= $count) {
|
||||
$this->create_user_accounts($nextnumber, $count);
|
||||
}
|
||||
|
||||
// Assign all users to course.
|
||||
$this->log('enrol', $count, true);
|
||||
|
||||
$enrolplugin = enrol_get_plugin('manual');
|
||||
$instances = enrol_get_instances($this->course->id, true);
|
||||
foreach ($instances as $instance) {
|
||||
if ($instance->enrol === 'manual') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($instance->enrol !== 'manual') {
|
||||
throw new coding_exception('No manual enrol plugin in course');
|
||||
}
|
||||
$role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
|
||||
|
||||
for ($number = 1; $number <= $count; $number++) {
|
||||
// Enrol user.
|
||||
$enrolplugin->enrol_user($instance, $this->userids[$number], $role->id);
|
||||
$this->dot($number, $count);
|
||||
}
|
||||
|
||||
// Sets the pointer at the beginning to be aware of the users we use.
|
||||
reset($this->userids);
|
||||
|
||||
$this->end_log();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates user accounts with a numeric range.
|
||||
*
|
||||
* @param int $first Number of first user
|
||||
* @param int $last Number of last user
|
||||
*/
|
||||
private function create_user_accounts($first, $last) {
|
||||
$this->log('createaccounts', (object)array('from' => $first, 'to' => $last), true);
|
||||
$count = $last - $first + 1;
|
||||
$done = 0;
|
||||
for ($number = $first; $number <= $last; $number++, $done++) {
|
||||
// Work out username with 6-digit number.
|
||||
$textnumber = (string)$number;
|
||||
while (strlen($textnumber) < 6) {
|
||||
$textnumber = '0' . $textnumber;
|
||||
}
|
||||
$username = 'tool_generator_' . $textnumber;
|
||||
|
||||
// Create user account.
|
||||
$record = array('firstname' => get_string('firstname', 'tool_generator'),
|
||||
'lastname' => $number, 'username' => $username);
|
||||
$user = $this->generator->create_user($record);
|
||||
$this->userids[$number] = (int)$user->id;
|
||||
$this->dot($done, $count);
|
||||
}
|
||||
$this->end_log();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a number of Page activities.
|
||||
*/
|
||||
private function create_pages() {
|
||||
// Set up generator.
|
||||
$pagegenerator = $this->generator->get_plugin_generator('mod_page');
|
||||
|
||||
// Create pages.
|
||||
$number = self::$parampages[$this->size];
|
||||
$this->log('createpages', $number, true);
|
||||
for ($i=0; $i<$number; $i++) {
|
||||
$record = array('course' => $this->course->id);
|
||||
$options = array('section' => $this->get_target_section());
|
||||
$pagegenerator->create_instance($record, $options);
|
||||
$this->dot($i, $number);
|
||||
}
|
||||
|
||||
$this->end_log();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates one resource activity with a lot of small files.
|
||||
*/
|
||||
private function create_small_files() {
|
||||
$count = self::$paramsmallfilecount[$this->size];
|
||||
$this->log('createsmallfiles', $count, true);
|
||||
|
||||
// Create resource with default textfile only.
|
||||
$resourcegenerator = $this->generator->get_plugin_generator('mod_resource');
|
||||
$record = array('course' => $this->course->id,
|
||||
'name' => get_string('smallfiles', 'tool_generator'));
|
||||
$options = array('section' => 0);
|
||||
$resource = $resourcegenerator->create_instance($record, $options);
|
||||
|
||||
// Add files.
|
||||
$fs = get_file_storage();
|
||||
$context = context_module::instance($resource->cmid);
|
||||
$filerecord = array('component' => 'mod_resource', 'filearea' => 'content',
|
||||
'contextid' => $context->id, 'itemid' => 0, 'filepath' => '/');
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$filerecord['filename'] = 'smallfile' . $i . '.dat';
|
||||
|
||||
// Generate random binary data (different for each file so it
|
||||
// doesn't compress unrealistically).
|
||||
$data = self::get_random_binary(self::$paramsmallfilesize[$this->size]);
|
||||
|
||||
$fs->create_file_from_string($filerecord, $data);
|
||||
$this->dot($i, $count);
|
||||
}
|
||||
|
||||
$this->end_log();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string of random binary data. The start of the string includes
|
||||
* the current time, in an attempt to avoid large-scale repetition.
|
||||
*
|
||||
* @param int $length Number of bytes
|
||||
* @return Random data
|
||||
*/
|
||||
private static function get_random_binary($length) {
|
||||
$data = microtime(true);
|
||||
if (strlen($data) > $length) {
|
||||
// Use last digits of data.
|
||||
return substr($data, -$length);
|
||||
}
|
||||
$length -= strlen($data);
|
||||
for ($j=0; $j < $length; $j++) {
|
||||
$data .= chr(rand(1, 255));
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a number of resource activities with one big file each.
|
||||
*/
|
||||
private function create_big_files() {
|
||||
global $CFG;
|
||||
|
||||
// Work out how many files and how many blocks to use (up to 64KB).
|
||||
$count = self::$parambigfilecount[$this->size];
|
||||
$blocks = ceil(self::$parambigfilesize[$this->size] / 65536);
|
||||
$blocksize = floor(self::$parambigfilesize[$this->size] / $blocks);
|
||||
|
||||
$this->log('createbigfiles', $count, true);
|
||||
|
||||
// Prepare temp area.
|
||||
$tempfolder = make_temp_directory('tool_generator');
|
||||
$tempfile = $tempfolder . '/' . rand();
|
||||
|
||||
// Create resources and files.
|
||||
$fs = get_file_storage();
|
||||
$resourcegenerator = $this->generator->get_plugin_generator('mod_resource');
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
// Create resource.
|
||||
$record = array('course' => $this->course->id,
|
||||
'name' => get_string('bigfile', 'tool_generator', $i));
|
||||
$options = array('section' => $this->get_target_section());
|
||||
$resource = $resourcegenerator->create_instance($record, $options);
|
||||
|
||||
// Write file.
|
||||
$handle = fopen($tempfile, 'w');
|
||||
if (!$handle) {
|
||||
throw new coding_exception('Failed to open temporary file');
|
||||
}
|
||||
for ($j = 0; $j < $blocks; $j++) {
|
||||
$data = self::get_random_binary($blocksize);
|
||||
fwrite($handle, $data);
|
||||
$this->dot($i * $blocks + $j, $count * $blocks);
|
||||
}
|
||||
fclose($handle);
|
||||
|
||||
// Add file.
|
||||
$context = context_module::instance($resource->cmid);
|
||||
$filerecord = array('component' => 'mod_resource', 'filearea' => 'content',
|
||||
'contextid' => $context->id, 'itemid' => 0, 'filepath' => '/',
|
||||
'filename' => 'bigfile' . $i . '.dat');
|
||||
$fs->create_file_from_pathname($filerecord, $tempfile);
|
||||
}
|
||||
|
||||
unlink($tempfile);
|
||||
$this->end_log();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates one forum activity with a bunch of posts.
|
||||
*/
|
||||
private function create_forum() {
|
||||
global $DB;
|
||||
|
||||
$discussions = self::$paramforumdiscussions[$this->size];
|
||||
$posts = self::$paramforumposts[$this->size];
|
||||
$totalposts = $discussions * $posts;
|
||||
|
||||
$this->log('createforum', $totalposts, true);
|
||||
|
||||
// Create empty forum.
|
||||
$forumgenerator = $this->generator->get_plugin_generator('mod_forum');
|
||||
$record = array('course' => $this->course->id,
|
||||
'name' => get_string('pluginname', 'forum'));
|
||||
$options = array('section' => 0);
|
||||
$forum = $forumgenerator->create_instance($record, $options);
|
||||
|
||||
// Add discussions and posts.
|
||||
$sofar = 0;
|
||||
for ($i=0; $i < $discussions; $i++) {
|
||||
$record = array('forum' => $forum->id, 'course' => $this->course->id,
|
||||
'userid' => $this->get_target_user());
|
||||
$discussion = $forumgenerator->create_discussion($record);
|
||||
$parentid = $DB->get_field('forum_posts', 'id', array('discussion' => $discussion->id), MUST_EXIST);
|
||||
$sofar++;
|
||||
for ($j=0; $j < $posts - 1; $j++, $sofar++) {
|
||||
$record = array('discussion' => $discussion->id,
|
||||
'userid' => $this->get_target_user(), 'parent' => $parentid);
|
||||
$forumgenerator->create_post($record);
|
||||
$this->dot($sofar, $totalposts);
|
||||
}
|
||||
}
|
||||
|
||||
$this->end_log();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a section number.
|
||||
*
|
||||
* Depends on $this->fixeddataset.
|
||||
*
|
||||
* @return int A section number from 1 to the number of sections
|
||||
*/
|
||||
private function get_target_section() {
|
||||
|
||||
if (!$this->fixeddataset) {
|
||||
$key = rand(1, self::$paramsections[$this->size]);
|
||||
} else {
|
||||
// Using section 1.
|
||||
$key = 1;
|
||||
}
|
||||
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a user id.
|
||||
*
|
||||
* Depends on $this->fixeddataset.
|
||||
*
|
||||
* @return int A user id for a random created user
|
||||
*/
|
||||
private function get_target_user() {
|
||||
|
||||
if (!$this->fixeddataset) {
|
||||
$userid = $this->userids[rand(1, self::$paramusers[$this->size])];
|
||||
} else if ($userid = current($this->userids)) {
|
||||
// Moving pointer to the next user.
|
||||
next($this->userids);
|
||||
} else {
|
||||
// Returning to the beginning if we reached the end.
|
||||
$userid = reset($this->userids);
|
||||
}
|
||||
|
||||
return $userid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays information as part of progress.
|
||||
* @param string $langstring Part of langstring (after progress_)
|
||||
* @param mixed $a Optional lang string parameters
|
||||
* @param bool $leaveopen If true, doesn't close LI tag (ready for dots)
|
||||
*/
|
||||
private function log($langstring, $a = null, $leaveopen = false) {
|
||||
protected function log($langstring, $a = null, $leaveopen = false) {
|
||||
if (!$this->progress) {
|
||||
return;
|
||||
}
|
||||
@@ -597,7 +149,7 @@ class tool_generator_backend {
|
||||
* @param int $number Number of completed items
|
||||
* @param int $total Total number of items to complete
|
||||
*/
|
||||
private function dot($number, $total) {
|
||||
protected function dot($number, $total) {
|
||||
if (!$this->progress) {
|
||||
return;
|
||||
}
|
||||
@@ -625,7 +177,7 @@ class tool_generator_backend {
|
||||
/**
|
||||
* Ends a log string that was started using log function with $leaveopen.
|
||||
*/
|
||||
private function end_log() {
|
||||
protected function end_log() {
|
||||
if (!$this->progress) {
|
||||
return;
|
||||
}
|
||||
@@ -636,4 +188,6 @@ class tool_generator_backend {
|
||||
echo html_writer::end_tag('li');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,507 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* tool_generator course backend code.
|
||||
*
|
||||
* @package tool_generator
|
||||
* @copyright 2013 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Backend code for the 'make large course' tool.
|
||||
*
|
||||
* @package tool_generator
|
||||
* @copyright 2013 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class tool_generator_course_backend extends tool_generator_backend {
|
||||
/**
|
||||
* @var array Number of sections in course
|
||||
*/
|
||||
private static $paramsections = array(1, 10, 100, 500, 1000, 2000);
|
||||
/**
|
||||
* @var array Number of Page activities in course
|
||||
*/
|
||||
private static $parampages = array(1, 50, 200, 1000, 5000, 10000);
|
||||
/**
|
||||
* @var array Number of students enrolled in course
|
||||
*/
|
||||
private static $paramusers = array(1, 100, 1000, 10000, 50000, 100000);
|
||||
/**
|
||||
* Total size of small files: 1KB, 1MB, 10MB, 100MB, 1GB, 2GB.
|
||||
*
|
||||
* @var array Number of small files created in a single file activity
|
||||
*/
|
||||
private static $paramsmallfilecount = array(1, 64, 128, 1024, 16384, 32768);
|
||||
/**
|
||||
* @var array Size of small files (to make the totals into nice numbers)
|
||||
*/
|
||||
private static $paramsmallfilesize = array(1024, 16384, 81920, 102400, 65536, 65536);
|
||||
/**
|
||||
* Total size of big files: 8KB, 8MB, 80MB, 800MB, 8GB, 16GB.
|
||||
*
|
||||
* @var array Number of big files created as individual file activities
|
||||
*/
|
||||
private static $parambigfilecount = array(1, 2, 5, 10, 10, 10);
|
||||
/**
|
||||
* @var array Size of each large file
|
||||
*/
|
||||
private static $parambigfilesize = array(8192, 4194304, 16777216, 83886080,
|
||||
858993459, 1717986918);
|
||||
/**
|
||||
* @var array Number of forum discussions
|
||||
*/
|
||||
private static $paramforumdiscussions = array(1, 10, 100, 500, 1000, 2000);
|
||||
/**
|
||||
* @var array Number of forum posts per discussion
|
||||
*/
|
||||
private static $paramforumposts = array(2, 2, 5, 10, 10, 10);
|
||||
|
||||
/**
|
||||
* @var string Course shortname
|
||||
*/
|
||||
private $shortname;
|
||||
|
||||
/**
|
||||
* @var testing_data_generator Data generator
|
||||
*/
|
||||
protected $generator;
|
||||
|
||||
/**
|
||||
* @var stdClass Course object
|
||||
*/
|
||||
private $course;
|
||||
|
||||
/**
|
||||
* @var array Array from test user number (1...N) to userid in database
|
||||
*/
|
||||
private $userids;
|
||||
|
||||
/**
|
||||
* Constructs object ready to create course.
|
||||
*
|
||||
* @param string $shortname Course shortname
|
||||
* @param int $size Size as numeric index
|
||||
* @param bool $fixeddataset To use fixed or random data
|
||||
* @param bool $progress True if progress information should be displayed
|
||||
* @return int Course id
|
||||
*/
|
||||
public function __construct($shortname, $size, $fixeddataset = false, $progress = true) {
|
||||
|
||||
// Set parameters.
|
||||
$this->shortname = $shortname;
|
||||
|
||||
parent::__construct($size, $fixeddataset, $progress);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of size choices supported by this backend.
|
||||
*
|
||||
* @return array List of size (int) => text description for display
|
||||
*/
|
||||
public static function get_size_choices() {
|
||||
$options = array();
|
||||
for ($size = self::MIN_SIZE; $size <= self::MAX_SIZE; $size++) {
|
||||
$options[$size] = get_string('coursesize_' . $size, 'tool_generator');
|
||||
}
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that a shortname is available (unused).
|
||||
*
|
||||
* @param string $shortname Proposed course shortname
|
||||
* @return string An error message if the name is unavailable or '' if OK
|
||||
*/
|
||||
public static function check_shortname_available($shortname) {
|
||||
global $DB;
|
||||
$fullname = $DB->get_field('course', 'fullname',
|
||||
array('shortname' => $shortname), IGNORE_MISSING);
|
||||
if ($fullname !== false) {
|
||||
// I wanted to throw an exception here but it is not possible to
|
||||
// use strings from moodle.php in exceptions, and I didn't want
|
||||
// to duplicate the string in tool_generator, so I changed this to
|
||||
// not use exceptions.
|
||||
return get_string('shortnametaken', 'moodle', $fullname);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the entire 'make' process.
|
||||
*
|
||||
* @return int Course id
|
||||
*/
|
||||
public function make() {
|
||||
global $DB, $CFG;
|
||||
require_once($CFG->dirroot . '/lib/phpunit/classes/util.php');
|
||||
|
||||
raise_memory_limit(MEMORY_EXTRA);
|
||||
|
||||
if ($this->progress && !CLI_SCRIPT) {
|
||||
echo html_writer::start_tag('ul');
|
||||
}
|
||||
|
||||
$entirestart = microtime(true);
|
||||
|
||||
// Start transaction.
|
||||
$transaction = $DB->start_delegated_transaction();
|
||||
|
||||
// Get generator.
|
||||
$this->generator = phpunit_util::get_data_generator();
|
||||
|
||||
// Make course.
|
||||
$this->course = $this->create_course();
|
||||
$this->create_users();
|
||||
$this->create_pages();
|
||||
$this->create_small_files();
|
||||
$this->create_big_files();
|
||||
$this->create_forum();
|
||||
|
||||
// Log total time.
|
||||
$this->log('coursecompleted', round(microtime(true) - $entirestart, 1));
|
||||
|
||||
if ($this->progress && !CLI_SCRIPT) {
|
||||
echo html_writer::end_tag('ul');
|
||||
}
|
||||
|
||||
// Commit transaction and finish.
|
||||
$transaction->allow_commit();
|
||||
return $this->course->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the actual course.
|
||||
*
|
||||
* @return stdClass Course record
|
||||
*/
|
||||
private function create_course() {
|
||||
$this->log('createcourse', $this->shortname);
|
||||
$courserecord = array('shortname' => $this->shortname,
|
||||
'fullname' => get_string('fullname', 'tool_generator',
|
||||
array('size' => get_string('shortsize_' . $this->size, 'tool_generator'))),
|
||||
'numsections' => self::$paramsections[$this->size]);
|
||||
return $this->generator->create_course($courserecord, array('createsections' => true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a number of user accounts and enrols them on the course.
|
||||
* Note: Existing user accounts that were created by this system are
|
||||
* reused if available.
|
||||
*/
|
||||
private function create_users() {
|
||||
global $DB;
|
||||
|
||||
// Work out total number of users.
|
||||
$count = self::$paramusers[$this->size];
|
||||
|
||||
// Get existing users in order. We will 'fill up holes' in this up to
|
||||
// the required number.
|
||||
$this->log('checkaccounts', $count);
|
||||
$nextnumber = 1;
|
||||
$rs = $DB->get_recordset_select('user', $DB->sql_like('username', '?'),
|
||||
array('tool_generator_%'), 'username', 'id, username');
|
||||
foreach ($rs as $rec) {
|
||||
// Extract number from username.
|
||||
$matches = array();
|
||||
if (!preg_match('~^tool_generator_([0-9]{6})$~', $rec->username, $matches)) {
|
||||
continue;
|
||||
}
|
||||
$number = (int)$matches[1];
|
||||
|
||||
// Create missing users in range up to this.
|
||||
if ($number != $nextnumber) {
|
||||
$this->create_user_accounts($nextnumber, min($number - 1, $count));
|
||||
} else {
|
||||
$this->userids[$number] = (int)$rec->id;
|
||||
}
|
||||
|
||||
// Stop if we've got enough users.
|
||||
$nextnumber = $number + 1;
|
||||
if ($number >= $count) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
$rs->close();
|
||||
|
||||
// Create users from end of existing range.
|
||||
if ($nextnumber <= $count) {
|
||||
$this->create_user_accounts($nextnumber, $count);
|
||||
}
|
||||
|
||||
// Assign all users to course.
|
||||
$this->log('enrol', $count, true);
|
||||
|
||||
$enrolplugin = enrol_get_plugin('manual');
|
||||
$instances = enrol_get_instances($this->course->id, true);
|
||||
foreach ($instances as $instance) {
|
||||
if ($instance->enrol === 'manual') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($instance->enrol !== 'manual') {
|
||||
throw new coding_exception('No manual enrol plugin in course');
|
||||
}
|
||||
$role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
|
||||
|
||||
for ($number = 1; $number <= $count; $number++) {
|
||||
// Enrol user.
|
||||
$enrolplugin->enrol_user($instance, $this->userids[$number], $role->id);
|
||||
$this->dot($number, $count);
|
||||
}
|
||||
|
||||
// Sets the pointer at the beginning to be aware of the users we use.
|
||||
reset($this->userids);
|
||||
|
||||
$this->end_log();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates user accounts with a numeric range.
|
||||
*
|
||||
* @param int $first Number of first user
|
||||
* @param int $last Number of last user
|
||||
*/
|
||||
private function create_user_accounts($first, $last) {
|
||||
$this->log('createaccounts', (object)array('from' => $first, 'to' => $last), true);
|
||||
$count = $last - $first + 1;
|
||||
$done = 0;
|
||||
for ($number = $first; $number <= $last; $number++, $done++) {
|
||||
// Work out username with 6-digit number.
|
||||
$textnumber = (string)$number;
|
||||
while (strlen($textnumber) < 6) {
|
||||
$textnumber = '0' . $textnumber;
|
||||
}
|
||||
$username = 'tool_generator_' . $textnumber;
|
||||
|
||||
// Create user account.
|
||||
$record = array('firstname' => get_string('firstname', 'tool_generator'),
|
||||
'lastname' => $number, 'username' => $username);
|
||||
$user = $this->generator->create_user($record);
|
||||
$this->userids[$number] = (int)$user->id;
|
||||
$this->dot($done, $count);
|
||||
}
|
||||
$this->end_log();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a number of Page activities.
|
||||
*/
|
||||
private function create_pages() {
|
||||
// Set up generator.
|
||||
$pagegenerator = $this->generator->get_plugin_generator('mod_page');
|
||||
|
||||
// Create pages.
|
||||
$number = self::$parampages[$this->size];
|
||||
$this->log('createpages', $number, true);
|
||||
for ($i=0; $i<$number; $i++) {
|
||||
$record = array('course' => $this->course->id);
|
||||
$options = array('section' => $this->get_target_section());
|
||||
$pagegenerator->create_instance($record, $options);
|
||||
$this->dot($i, $number);
|
||||
}
|
||||
|
||||
$this->end_log();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates one resource activity with a lot of small files.
|
||||
*/
|
||||
private function create_small_files() {
|
||||
$count = self::$paramsmallfilecount[$this->size];
|
||||
$this->log('createsmallfiles', $count, true);
|
||||
|
||||
// Create resource with default textfile only.
|
||||
$resourcegenerator = $this->generator->get_plugin_generator('mod_resource');
|
||||
$record = array('course' => $this->course->id,
|
||||
'name' => get_string('smallfiles', 'tool_generator'));
|
||||
$options = array('section' => 0);
|
||||
$resource = $resourcegenerator->create_instance($record, $options);
|
||||
|
||||
// Add files.
|
||||
$fs = get_file_storage();
|
||||
$context = context_module::instance($resource->cmid);
|
||||
$filerecord = array('component' => 'mod_resource', 'filearea' => 'content',
|
||||
'contextid' => $context->id, 'itemid' => 0, 'filepath' => '/');
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$filerecord['filename'] = 'smallfile' . $i . '.dat';
|
||||
|
||||
// Generate random binary data (different for each file so it
|
||||
// doesn't compress unrealistically).
|
||||
$data = self::get_random_binary(self::$paramsmallfilesize[$this->size]);
|
||||
|
||||
$fs->create_file_from_string($filerecord, $data);
|
||||
$this->dot($i, $count);
|
||||
}
|
||||
|
||||
$this->end_log();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string of random binary data. The start of the string includes
|
||||
* the current time, in an attempt to avoid large-scale repetition.
|
||||
*
|
||||
* @param int $length Number of bytes
|
||||
* @return Random data
|
||||
*/
|
||||
private static function get_random_binary($length) {
|
||||
$data = microtime(true);
|
||||
if (strlen($data) > $length) {
|
||||
// Use last digits of data.
|
||||
return substr($data, -$length);
|
||||
}
|
||||
$length -= strlen($data);
|
||||
for ($j=0; $j < $length; $j++) {
|
||||
$data .= chr(rand(1, 255));
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a number of resource activities with one big file each.
|
||||
*/
|
||||
private function create_big_files() {
|
||||
global $CFG;
|
||||
|
||||
// Work out how many files and how many blocks to use (up to 64KB).
|
||||
$count = self::$parambigfilecount[$this->size];
|
||||
$blocks = ceil(self::$parambigfilesize[$this->size] / 65536);
|
||||
$blocksize = floor(self::$parambigfilesize[$this->size] / $blocks);
|
||||
|
||||
$this->log('createbigfiles', $count, true);
|
||||
|
||||
// Prepare temp area.
|
||||
$tempfolder = make_temp_directory('tool_generator');
|
||||
$tempfile = $tempfolder . '/' . rand();
|
||||
|
||||
// Create resources and files.
|
||||
$fs = get_file_storage();
|
||||
$resourcegenerator = $this->generator->get_plugin_generator('mod_resource');
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
// Create resource.
|
||||
$record = array('course' => $this->course->id,
|
||||
'name' => get_string('bigfile', 'tool_generator', $i));
|
||||
$options = array('section' => $this->get_target_section());
|
||||
$resource = $resourcegenerator->create_instance($record, $options);
|
||||
|
||||
// Write file.
|
||||
$handle = fopen($tempfile, 'w');
|
||||
if (!$handle) {
|
||||
throw new coding_exception('Failed to open temporary file');
|
||||
}
|
||||
for ($j = 0; $j < $blocks; $j++) {
|
||||
$data = self::get_random_binary($blocksize);
|
||||
fwrite($handle, $data);
|
||||
$this->dot($i * $blocks + $j, $count * $blocks);
|
||||
}
|
||||
fclose($handle);
|
||||
|
||||
// Add file.
|
||||
$context = context_module::instance($resource->cmid);
|
||||
$filerecord = array('component' => 'mod_resource', 'filearea' => 'content',
|
||||
'contextid' => $context->id, 'itemid' => 0, 'filepath' => '/',
|
||||
'filename' => 'bigfile' . $i . '.dat');
|
||||
$fs->create_file_from_pathname($filerecord, $tempfile);
|
||||
}
|
||||
|
||||
unlink($tempfile);
|
||||
$this->end_log();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates one forum activity with a bunch of posts.
|
||||
*/
|
||||
private function create_forum() {
|
||||
global $DB;
|
||||
|
||||
$discussions = self::$paramforumdiscussions[$this->size];
|
||||
$posts = self::$paramforumposts[$this->size];
|
||||
$totalposts = $discussions * $posts;
|
||||
|
||||
$this->log('createforum', $totalposts, true);
|
||||
|
||||
// Create empty forum.
|
||||
$forumgenerator = $this->generator->get_plugin_generator('mod_forum');
|
||||
$record = array('course' => $this->course->id,
|
||||
'name' => get_string('pluginname', 'forum'));
|
||||
$options = array('section' => 0);
|
||||
$forum = $forumgenerator->create_instance($record, $options);
|
||||
|
||||
// Add discussions and posts.
|
||||
$sofar = 0;
|
||||
for ($i=0; $i < $discussions; $i++) {
|
||||
$record = array('forum' => $forum->id, 'course' => $this->course->id,
|
||||
'userid' => $this->get_target_user());
|
||||
$discussion = $forumgenerator->create_discussion($record);
|
||||
$parentid = $DB->get_field('forum_posts', 'id', array('discussion' => $discussion->id), MUST_EXIST);
|
||||
$sofar++;
|
||||
for ($j=0; $j < $posts - 1; $j++, $sofar++) {
|
||||
$record = array('discussion' => $discussion->id,
|
||||
'userid' => $this->get_target_user(), 'parent' => $parentid);
|
||||
$forumgenerator->create_post($record);
|
||||
$this->dot($sofar, $totalposts);
|
||||
}
|
||||
}
|
||||
|
||||
$this->end_log();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a section number.
|
||||
*
|
||||
* Depends on $this->fixeddataset.
|
||||
*
|
||||
* @return int A section number from 1 to the number of sections
|
||||
*/
|
||||
private function get_target_section() {
|
||||
|
||||
if (!$this->fixeddataset) {
|
||||
$key = rand(1, self::$paramsections[$this->size]);
|
||||
} else {
|
||||
// Using section 1.
|
||||
$key = 1;
|
||||
}
|
||||
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a user id.
|
||||
*
|
||||
* Depends on $this->fixeddataset.
|
||||
*
|
||||
* @return int A user id for a random created user
|
||||
*/
|
||||
private function get_target_user() {
|
||||
|
||||
if (!$this->fixeddataset) {
|
||||
$userid = $this->userids[rand(1, self::$paramusers[$this->size])];
|
||||
} else if ($userid = current($this->userids)) {
|
||||
// Moving pointer to the next user.
|
||||
next($this->userids);
|
||||
} else {
|
||||
// Returning to the beginning if we reached the end.
|
||||
$userid = reset($this->userids);
|
||||
}
|
||||
|
||||
return $userid;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -31,8 +31,8 @@ class tool_generator_make_form extends moodleform {
|
||||
$mform = $this->_form;
|
||||
|
||||
$mform->addElement('select', 'size', get_string('size', 'tool_generator'),
|
||||
tool_generator_backend::get_size_choices());
|
||||
$mform->setDefault('size', tool_generator_backend::DEFAULT_SIZE);
|
||||
tool_generator_course_backend::get_size_choices());
|
||||
$mform->setDefault('size', tool_generator_course_backend::DEFAULT_SIZE);
|
||||
|
||||
$mform->addElement('text', 'shortname', get_string('shortnamecourse'));
|
||||
$mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
|
||||
@@ -48,7 +48,7 @@ class tool_generator_make_form extends moodleform {
|
||||
// Check course doesn't already exist.
|
||||
if (!empty($data['shortname'])) {
|
||||
// Check shortname.
|
||||
$error = tool_generator_backend::check_shortname_available($data['shortname']);
|
||||
$error = tool_generator_course_backend::check_shortname_available($data['shortname']);
|
||||
if ($error) {
|
||||
$errors['shortname'] = $error;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,203 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* tool_generator site backend.
|
||||
*
|
||||
* @package tool_generator
|
||||
* @copyright 2013 David Monllaó
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Backend code for the site generator.
|
||||
*
|
||||
* @package tool_generator
|
||||
* @copyright 2013 David Monllaó
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class tool_generator_site_backend extends tool_generator_backend {
|
||||
|
||||
/**
|
||||
* @var string The course's shortname prefix.
|
||||
*/
|
||||
const SHORTNAMEPREFIX = 'testcourse_';
|
||||
|
||||
/**
|
||||
* @var bool If the debugging level checking was skipped.
|
||||
*/
|
||||
protected $bypasscheck;
|
||||
|
||||
/**
|
||||
* @var array Multidimensional array where the first level is the course size and the second the site size.
|
||||
*/
|
||||
protected static $sitecourses = array(
|
||||
array(2, 8, 64, 256, 1024, 4096),
|
||||
array(1, 4, 8, 16, 32, 64),
|
||||
array(0, 0, 1, 4, 8, 16),
|
||||
array(0, 0, 0, 1, 0, 0),
|
||||
array(0, 0, 0, 0, 1, 0),
|
||||
array(0, 0, 0, 0, 0, 1)
|
||||
);
|
||||
|
||||
/**
|
||||
* Constructs object ready to make the site.
|
||||
*
|
||||
* @param int $size Size as numeric index
|
||||
* @param bool $bypasscheck If debugging level checking was skipped.
|
||||
* @param bool $fixeddataset To use fixed or random data
|
||||
* @param bool $progress True if progress information should be displayed
|
||||
* @return int Course id
|
||||
*/
|
||||
public function __construct($size, $bypasscheck, $fixeddataset = false, $progress = true) {
|
||||
|
||||
// Set parameters.
|
||||
$this->bypasscheck = $bypasscheck;
|
||||
|
||||
parent::__construct($size, $fixeddataset, $progress);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of size choices supported by this backend.
|
||||
*
|
||||
* @return array List of size (int) => text description for display
|
||||
*/
|
||||
public static function get_size_choices() {
|
||||
$options = array();
|
||||
for ($size = self::MIN_SIZE; $size <= self::MAX_SIZE; $size++) {
|
||||
$options[$size] = get_string('sitesize_' . $size, 'tool_generator');
|
||||
}
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the entire 'make' process.
|
||||
*
|
||||
* @return int Course id
|
||||
*/
|
||||
public function make() {
|
||||
global $DB, $CFG;
|
||||
|
||||
raise_memory_limit(MEMORY_EXTRA);
|
||||
|
||||
if ($this->progress && !CLI_SCRIPT) {
|
||||
echo html_writer::start_tag('ul');
|
||||
}
|
||||
|
||||
$entirestart = microtime(true);
|
||||
|
||||
// Create courses.
|
||||
$prevchdir = getcwd();
|
||||
chdir($CFG->dirroot);
|
||||
$ncourse = $this->get_last_testcourse_id();
|
||||
foreach (self::$sitecourses as $coursesize => $ncourses) {
|
||||
for ($i = 1; $i <= $ncourses[$this->size]; $i++) {
|
||||
// Non language-dependant shortname.
|
||||
$ncourse++;
|
||||
$this->run_create_course(self::SHORTNAMEPREFIX . $ncourse, $coursesize);
|
||||
}
|
||||
}
|
||||
chdir($prevchdir);
|
||||
|
||||
// Store last course id to return it (will be the bigger one).
|
||||
$lastcourseid = $DB->get_field('course', 'id', array('shortname' => self::SHORTNAMEPREFIX . $ncourse));
|
||||
|
||||
// Log total time.
|
||||
$this->log('sitecompleted', round(microtime(true) - $entirestart, 1));
|
||||
|
||||
if ($this->progress && !CLI_SCRIPT) {
|
||||
echo html_writer::end_tag('ul');
|
||||
}
|
||||
|
||||
return $lastcourseid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a course with the specified shortname, coursesize and the provided maketestsite options.
|
||||
*
|
||||
* @param string $shortname The course shortname
|
||||
* @param int $coursesize One of the possible course sizes.
|
||||
* @return void
|
||||
*/
|
||||
protected function run_create_course($shortname, $coursesize) {
|
||||
|
||||
// We are in $CFG->dirroot.
|
||||
$command = 'php admin/tool/generator/cli/maketestcourse.php';
|
||||
|
||||
$options = array(
|
||||
'--shortname="' . $shortname . '"',
|
||||
'--size="' . get_string('shortsize_' . $coursesize, 'tool_generator') . '"'
|
||||
);
|
||||
|
||||
if (!$this->progress) {
|
||||
$options[] = '--quiet';
|
||||
}
|
||||
|
||||
// Extend options.
|
||||
$optionstoextend = array(
|
||||
'fixeddataset' => 'fixeddataset',
|
||||
'bypasscheck' => 'bypasscheck',
|
||||
);
|
||||
|
||||
// Getting an options string.
|
||||
foreach ($optionstoextend as $attribute => $option) {
|
||||
if (!empty($this->{$attribute})) {
|
||||
$options[] = '--' . $option;
|
||||
}
|
||||
}
|
||||
$options = implode(' ', $options);
|
||||
if ($this->progress) {
|
||||
system($command . ' ' . $options, $exitcode);
|
||||
} else {
|
||||
passthru($command . ' ' . $options, $exitcode);
|
||||
}
|
||||
|
||||
if ($exitcode != 0) {
|
||||
exit($exitcode);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains the last unique sufix (numeric) using the test course prefix.
|
||||
*
|
||||
* @return int The last generated numeric value.
|
||||
*/
|
||||
protected function get_last_testcourse_id() {
|
||||
global $DB;
|
||||
|
||||
$params = array();
|
||||
$params['shortnameprefix'] = $DB->sql_like_escape(self::SHORTNAMEPREFIX) . '%';
|
||||
$like = $DB->sql_like('shortname', ':shortnameprefix');
|
||||
|
||||
if (!$testcourses = $DB->get_records_select('course', $like, $params, 'shortname DESC')) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// They come ordered by shortname DESC, so non-numeric values will be the first ones.
|
||||
foreach ($testcourses as $testcourse) {
|
||||
$sufix = substr($testcourse->shortname, strlen(self::SHORTNAMEPREFIX));
|
||||
if (is_numeric($sufix)) {
|
||||
return $sufix;
|
||||
}
|
||||
}
|
||||
|
||||
// If all sufixes are not numeric this is the fist make test site run.
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -79,13 +79,13 @@ $fixeddataset = $options['fixeddataset'];
|
||||
|
||||
// Check size.
|
||||
try {
|
||||
$size = tool_generator_backend::size_for_name($sizename);
|
||||
$size = tool_generator_course_backend::size_for_name($sizename);
|
||||
} catch (coding_exception $e) {
|
||||
cli_error("Invalid size ($sizename). Use --help for help.");
|
||||
}
|
||||
|
||||
// Check shortname.
|
||||
if ($error = tool_generator_backend::check_shortname_available($shortname)) {
|
||||
if ($error = tool_generator_course_backend::check_shortname_available($shortname)) {
|
||||
cli_error($error);
|
||||
}
|
||||
|
||||
@@ -93,5 +93,5 @@ if ($error = tool_generator_backend::check_shortname_available($shortname)) {
|
||||
session_set_user(get_admin());
|
||||
|
||||
// Do backend code to generate course.
|
||||
$backend = new tool_generator_backend($shortname, $size, $fixeddataset, empty($options['quiet']));
|
||||
$backend = new tool_generator_course_backend($shortname, $size, $fixeddataset, empty($options['quiet']));
|
||||
$id = $backend->make();
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* CLI interface for creating a test site.
|
||||
*
|
||||
* @package tool_generator
|
||||
* @copyright 2013 David Monllaó
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
define('CLI_SCRIPT', true);
|
||||
define('NO_OUTPUT_BUFFERING', true);
|
||||
|
||||
require(__DIR__ . '/../../../../config.php');
|
||||
require_once($CFG->libdir. '/clilib.php');
|
||||
|
||||
// CLI options.
|
||||
list($options, $unrecognized) = cli_get_params(
|
||||
array(
|
||||
'help' => false,
|
||||
'size' => false,
|
||||
'fixeddataset' => false,
|
||||
'bypasscheck' => false,
|
||||
'quiet' => false
|
||||
),
|
||||
array(
|
||||
'h' => 'help'
|
||||
)
|
||||
);
|
||||
|
||||
$sitesizes = '* ' . implode(PHP_EOL . '* ', tool_generator_site_backend::get_size_choices());
|
||||
|
||||
// Display help.
|
||||
if (!empty($options['help']) || empty($options['size'])) {
|
||||
echo "
|
||||
Utility to generate a standard test site data set.
|
||||
|
||||
Not for use on live sites; only normally works if debugging is set to DEVELOPER
|
||||
level.
|
||||
|
||||
Consider that, depending on the size you select, this CLI tool can really generate a lot of data, aproximated sizes:
|
||||
|
||||
$sitesizes
|
||||
|
||||
Options:
|
||||
--size Size of the generated site, this value affects the number of courses and their size. Accepted values: XS, S, M, L, XL, or XXL (required)
|
||||
--fixeddataset Use a fixed data set instead of randomly generated data
|
||||
--bypasscheck Bypasses the developer-mode check (be careful!)
|
||||
--quiet Do not show any output
|
||||
|
||||
-h, --help Print out this help
|
||||
|
||||
Example from Moodle root directory:
|
||||
\$ php admin/tool/generator/cli/maketestsite.php --size=S
|
||||
";
|
||||
// Exit with error unless we're showing this because they asked for it.
|
||||
exit(empty($options['help']) ? 1 : 0);
|
||||
}
|
||||
|
||||
// Check debugging is set to developer level.
|
||||
if (empty($options['bypasscheck']) && !$CFG->debugdeveloper) {
|
||||
cli_error(get_string('error_notdebugging', 'tool_generator'));
|
||||
}
|
||||
|
||||
// Get options.
|
||||
$sizename = $options['size'];
|
||||
$fixeddataset = $options['fixeddataset'];
|
||||
|
||||
// Check size.
|
||||
try {
|
||||
$size = tool_generator_site_backend::size_for_name($sizename);
|
||||
} catch (coding_exception $e) {
|
||||
cli_error("Invalid size ($sizename). Use --help for help.");
|
||||
}
|
||||
|
||||
// Switch to admin user account.
|
||||
session_set_user(get_admin());
|
||||
|
||||
// Do backend code to generate site.
|
||||
$backend = new tool_generator_site_backend($size, $options['bypasscheck'], $fixeddataset, empty($options['quiet']));
|
||||
$backend->make();
|
||||
@@ -23,6 +23,12 @@
|
||||
*/
|
||||
|
||||
$string['bigfile'] = 'Big file {$a}';
|
||||
$string['coursesize_0'] = 'XS (~10KB; create in ~1 second)';
|
||||
$string['coursesize_1'] = 'S (~10MB; create in ~30 seconds)';
|
||||
$string['coursesize_2'] = 'M (~100MB; create in ~5 minutes)';
|
||||
$string['coursesize_3'] = 'L (~1GB; create in ~1 hour)';
|
||||
$string['coursesize_4'] = 'XL (~10GB; create in ~4 hours)';
|
||||
$string['coursesize_5'] = 'XXL (~20GB; create in ~8 hours)';
|
||||
$string['createcourse'] = 'Create course';
|
||||
$string['creating'] = 'Creating course';
|
||||
$string['done'] = 'done ({$a}s)';
|
||||
@@ -51,24 +57,25 @@ $string['maketestcourse'] = 'Make test course';
|
||||
$string['pluginname'] = 'Random course generator';
|
||||
$string['progress_createcourse'] = 'Creating course {$a}';
|
||||
$string['progress_checkaccounts'] = 'Checking user accounts ({$a})';
|
||||
$string['progress_coursecompleted'] = 'Course completed ({$a}s)';
|
||||
$string['progress_createaccounts'] = 'Creating user accounts ({$a->from} - {$a->to})';
|
||||
$string['progress_createbigfiles'] = 'Creating big files ({$a})';
|
||||
$string['progress_createforum'] = 'Creating forum ({$a} posts)';
|
||||
$string['progress_createpages'] = 'Creating pages ({$a})';
|
||||
$string['progress_createsmallfiles'] = 'Creating small files ({$a})';
|
||||
$string['progress_enrol'] = 'Enrolling users into course ({$a})';
|
||||
$string['progress_complete'] = 'Complete ({$a}s)';
|
||||
$string['progress_sitecompleted'] = 'Site completed ({$a}s)';
|
||||
$string['shortsize_0'] = 'XS';
|
||||
$string['shortsize_1'] = 'S';
|
||||
$string['shortsize_2'] = 'M';
|
||||
$string['shortsize_3'] = 'L';
|
||||
$string['shortsize_4'] = 'XL';
|
||||
$string['shortsize_5'] = 'XXL';
|
||||
$string['sitesize_0'] = 'XS (~10MB; 3 courses, created in ~30 seconds)';
|
||||
$string['sitesize_1'] = 'S (~50MB; 8 courses, created in ~2 minutes)';
|
||||
$string['sitesize_2'] = 'M (~200MB; 73 courses, created in ~10 minutes)';
|
||||
$string['sitesize_3'] = 'L (~1\'5GB; 277 courses, created in ~1\'5 hours)';
|
||||
$string['sitesize_4'] = 'XL (~10GB; 1065 courses, created in ~5 hours)';
|
||||
$string['sitesize_5'] = 'XXL (~20GB; 4177 courses, created in ~10 hours)';
|
||||
$string['size'] = 'Size of course';
|
||||
$string['size_0'] = 'XS (~10KB; create in ~1 second)';
|
||||
$string['size_1'] = 'S (~10MB; create in ~30 seconds)';
|
||||
$string['size_2'] = 'M (~100MB; create in ~5 minutes)';
|
||||
$string['size_3'] = 'L (~1GB; create in ~1 hour)';
|
||||
$string['size_4'] = 'XL (~10GB; create in ~4 hours)';
|
||||
$string['size_5'] = 'XXL (~20GB; create in ~8 hours)';
|
||||
$string['smallfiles'] = 'Small files';
|
||||
|
||||
@@ -55,7 +55,7 @@ $mform = new tool_generator_make_form('maketestcourse.php');
|
||||
if ($data = $mform->get_data()) {
|
||||
// Do actual work.
|
||||
echo $OUTPUT->heading(get_string('creating', 'tool_generator'));
|
||||
$backend = new tool_generator_backend($data->shortname, $data->size);
|
||||
$backend = new tool_generator_course_backend($data->shortname, $data->size);
|
||||
$id = $backend->make();
|
||||
|
||||
echo html_writer::div(
|
||||
|
||||
@@ -35,7 +35,7 @@ class tool_generator_maketestcourse_testcase extends advanced_testcase {
|
||||
$this->setAdminUser();
|
||||
|
||||
// Create the XS course.
|
||||
$backend = new tool_generator_backend('TOOL_MAKELARGECOURSE_XS', 0, false, false);
|
||||
$backend = new tool_generator_course_backend('TOOL_MAKELARGECOURSE_XS', 0, false, false);
|
||||
$courseid = $backend->make();
|
||||
|
||||
// Get course details.
|
||||
@@ -118,7 +118,7 @@ class tool_generator_maketestcourse_testcase extends advanced_testcase {
|
||||
$this->setAdminUser();
|
||||
|
||||
// Create the S course (more sections and activities than XS).
|
||||
$backend = new tool_generator_backend('TOOL_S_COURSE_1', 1, true, false);
|
||||
$backend = new tool_generator_course_backend('TOOL_S_COURSE_1', 1, true, false);
|
||||
$courseid = $backend->make();
|
||||
|
||||
// Get course details.
|
||||
|
||||
Reference in New Issue
Block a user