MDL-41861 tool_generator: Adding fixeddataset option

This commit is contained in:
David Monllao
2013-09-25 10:12:33 +08:00
parent c65e322ff0
commit b0e19c4ca6
3 changed files with 49 additions and 13 deletions
+44 -11
View File
@@ -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 @@ class tool_generator_backend {
// Set parameters.
$this->shortname = $shortname;
$this->size = $size;
$this->fixeddataset = $fixeddataset;
$this->progress = $progress;
}
@@ -317,6 +324,9 @@ class tool_generator_backend {
$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 @@ class tool_generator_backend {
$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 @@ class tool_generator_backend {
// 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 @@ class tool_generator_backend {
$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 @@ class tool_generator_backend {
}
/**
* 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;
}
/**
+4 -1
View File
@@ -35,6 +35,7 @@ list($options, $unrecognized) = cli_get_params(
'help' => false,
'shortname' => false,
'size' => false,
'fixeddataset' => false,
'bypasscheck' => false,
'quiet' => false
),
@@ -54,6 +55,7 @@ level.
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
@@ -74,6 +76,7 @@ if (empty($options['bypasscheck']) && !debugging('', DEBUG_DEVELOPER)) {
// Get options.
$shortname = $options['shortname'];
$sizename = $options['size'];
$fixeddataset = $options['fixeddataset'];
// Check size.
try {
@@ -91,5 +94,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, empty($options['quiet']));
$backend = new tool_generator_backend($shortname, $size, $fixeddataset, empty($options['quiet']));
$id = $backend->make();
@@ -38,7 +38,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);
$backend = new tool_generator_backend('TOOL_MAKELARGECOURSE_XS', 0, false, false);
$courseid = $backend->make();
// Get course details.