Skip to content

Instantly share code, notes, and snippets.

@marinaglancy
Created October 22, 2013 12:38
Show Gist options
  • Select an option

  • Save marinaglancy/7099938 to your computer and use it in GitHub Desktop.

Select an option

Save marinaglancy/7099938 to your computer and use it in GitHub Desktop.
Example script to generate data on the development site
<?php
//define('CLI_SCRIPT', true);
include "config.php";
require_once($CFG->dirroot . '/lib/testing/classes/util.php');
$PAGE->set_context(context_system::instance());
$generator = testing_util::get_data_generator();
// Create users.
$users = array();
for ($i=0; $i<10; $i++) {
$user = $generator->create_user();
echo "Created user ".html_writer::link(new moodle_url('/user/profile.php', array('id' => $user->id)),
'User '.$user->firstname.' '.$user->lastname)."<br>";
$users[] = $user;
}
// Create course.
$course = $generator->create_course(array(
'numsections' => 10,
'format' => 'topics',
'shortname' => 'Course'.rand(1,100000))
);
echo "Created course ".html_writer::link(new moodle_url('/course/view.php', array('id' => $course->id)),
'Course '.$course->shortname)."<br>";
// Enrol users.
$roles = $DB->get_records_menu('role', array(), 'shortname', 'shortname,id');
foreach ($users as $user) {
$generator->enrol_user($user->id, $course->id, $roles['student'], 'manual');
}
// Find all existing module generators.
$modulegenerators = array();
$modules = core_component::get_plugin_list('mod');
foreach ($modules as $modname => $modpath) {
try {
$modulegenerators[$modname] = $generator->get_plugin_generator('mod_'.$modname);
} catch (coding_exception $e) { }
}
// Generate one instance of each module in the course.
foreach ($modulegenerators as $modname => $modgenerator) {
$modgenerator->create_instance(array('course' => $course->id), array('section' => 0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment