Skip to content

Commit

Permalink
MDL-41399 tool_generator: Adding fixeddataset option
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllao authored and dmonllao committed Aug 30, 2013
1 parent ee78814 commit 219cae0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
55 changes: 44 additions & 11 deletions admin/tool/generator/classes/backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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');
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion admin/tool/generator/cli/maketestcourse.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
'help' => false,
'shortname' => false,
'size' => false,
'fixeddataset' => false,
'bypasscheck' => false,
'quiet' => false
),
Expand All @@ -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
Expand All @@ -73,6 +75,7 @@
// Get options.
$shortname = $options['shortname'];
$sizename = $options['size'];
$fixeddataset = $options['fixeddataset'];

// Check size.
try {
Expand All @@ -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();
2 changes: 1 addition & 1 deletion admin/tool/generator/tests/maketestcourse_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 219cae0

Please sign in to comment.