diff --git a/admin/tool/generator/classes/backend.php b/admin/tool/generator/classes/backend.php index beed42112e138..813e6429c4368 100644 --- a/admin/tool/generator/classes/backend.php +++ b/admin/tool/generator/classes/backend.php @@ -89,6 +89,11 @@ class tool_generator_backend { */ private $size; + /** + * @var bool True if we want a fixed dataset or false to generate random data + */ + private $fixeddataset; + /** * @var bool True if displaying progress */ @@ -129,11 +134,12 @@ class tool_generator_backend { * * @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, $progress = true) { + public function __construct($shortname, $size, $fixeddataset = false, $progress = true) { // Check parameter. if ($size < self::MIN_SIZE || $size > self::MAX_SIZE) { throw new coding_exception('Invalid size'); @@ -142,6 +148,7 @@ public function __construct($shortname, $size, $progress = true) { // Set parameters. $this->shortname = $shortname; $this->size = $size; + $this->fixeddataset = $fixeddataset; $this->progress = $progress; } @@ -317,6 +324,9 @@ private function create_users() { $this->dot($number, $count); } + // Sets the pointer at the beginning to be aware of the users we use. + reset($this->userids); + $this->end_log(); } @@ -360,7 +370,7 @@ private function create_pages() { $this->log('createpages', $number, true); for ($i=0; $i<$number; $i++) { $record = array('course' => $this->course->id); - $options = array('section' => $this->get_random_section()); + $options = array('section' => $this->get_target_section()); $pagegenerator->create_instance($record, $options); $this->dot($i, $number); } @@ -445,7 +455,7 @@ private function create_big_files() { // Create resource. $record = array('course' => $this->course->id, 'name' => get_string('bigfile', 'tool_generator', $i)); - $options = array('section' => $this->get_random_section()); + $options = array('section' => $this->get_target_section()); $resource = $resourcegenerator->create_instance($record, $options); // Write file. @@ -495,13 +505,13 @@ private function create_forum() { $sofar = 0; for ($i=0; $i < $discussions; $i++) { $record = array('forum' => $forum->id, 'course' => $this->course->id, - 'userid' => $this->get_random_user()); + '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_random_user(), 'parent' => $parentid); + 'userid' => $this->get_target_user(), 'parent' => $parentid); $forumgenerator->create_post($record); $this->dot($sofar, $totalposts); } @@ -511,21 +521,44 @@ private function create_forum() { } /** - * Gets a random section number. + * Gets a section number. + * + * Depends on $this->fixeddataset. * * @return int A section number from 1 to the number of sections */ - private function get_random_section() { - return rand(1, self::$paramsections[$this->size]); + 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 random user id. + * Gets a user id. + * + * Depends on $this->fixeddataset. * * @return int A user id for a random created user */ - private function get_random_user() { - return $this->userids[rand(1, self::$paramusers[$this->size])]; + 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; } /** diff --git a/admin/tool/generator/cli/maketestcourse.php b/admin/tool/generator/cli/maketestcourse.php index b646e2f94afc6..a0a4df8573f1d 100644 --- a/admin/tool/generator/cli/maketestcourse.php +++ b/admin/tool/generator/cli/maketestcourse.php @@ -34,6 +34,7 @@ 'help' => false, 'shortname' => false, 'size' => false, + 'fixeddataset' => false, 'bypasscheck' => false, 'quiet' => false ), @@ -53,6 +54,7 @@ Options: --shortname Shortname of course to create (required) --size Size of course to create 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 @@ -73,6 +75,7 @@ // Get options. $shortname = $options['shortname']; $sizename = $options['size']; +$fixeddataset = $options['fixeddataset']; // Check size. try { @@ -90,5 +93,5 @@ session_set_user(get_admin()); // Do backend code to generate course. -$backend = new tool_generator_backend($shortname, $size, empty($options['quiet'])); +$backend = new tool_generator_backend($shortname, $size, $fixeddataset, empty($options['quiet'])); $id = $backend->make(); diff --git a/admin/tool/generator/tests/maketestcourse_test.php b/admin/tool/generator/tests/maketestcourse_test.php index 3b2244d284b91..8b9a2607e8d5c 100644 --- a/admin/tool/generator/tests/maketestcourse_test.php +++ b/admin/tool/generator/tests/maketestcourse_test.php @@ -35,7 +35,7 @@ public function test_make_xs_course() { $this->setAdminUser(); // Create the XS course. - $backend = new tool_generator_backend('TOOL_MAKELARGECOURSE_XS', 0, false); + $backend = new tool_generator_backend('TOOL_MAKELARGECOURSE_XS', 0, false, false); $courseid = $backend->make(); // Get course details.