$DB->set_debug(true);
}
if (!empty($CFG->showcrondebugging)) {
- $CFG->debug = DEBUG_DEVELOPER;
- $CFG->debugdisplay = true;
+ set_debugging(DEBUG_DEVELOPER, true);
}
$starttime = microtime();
$CFG->early_install_lang = true;
$CFG->ostype = (stristr(PHP_OS, 'win') && !stristr(PHP_OS, 'darwin')) ? 'WINDOWS' : 'UNIX';
$CFG->dboptions = array();
+$CFG->debug = (E_ALL | E_STRICT);
+$CFG->debugdisplay = true;
+$CFG->debugdeveloper = true;
$parts = explode('/', str_replace('\\', '/', dirname(dirname(__FILE__))));
$CFG->admin = array_pop($parts);
if (function_exists('opcache_reset')) {
opcache_reset();
}
+ $cache = 0;
+
+} else {
+ $cache = 1;
}
require('../config.php');
$agreelicense = optional_param('agreelicense', 0, PARAM_BOOL);
$fetchupdates = optional_param('fetchupdates', 0, PARAM_BOOL);
$newaddonreq = optional_param('installaddonrequest', null, PARAM_RAW);
-$cache = optional_param('cache', 0, PARAM_BOOL);
// Set up PAGE.
$url = new moodle_url('/admin/index.php');
-if (!is_null($newaddonreq)) {
- // We need to set the eventual add-on installation request in the $PAGE's URL
- // so that it is stored in $SESSION->wantsurl and the admin is redirected
- // correctly once they are logged-in.
- $url->param('installaddonrequest', $newaddonreq);
-}
if ($cache) {
- $url->param('cache', $cache);
+ $url->param('cache', 1);
}
$PAGE->set_url($url);
unset($url);
+// Are we returning from an add-on installation request at moodle.org/plugins?
+if ($newaddonreq and !$cache and empty($CFG->disableonclickaddoninstall)) {
+ $target = new moodle_url('/admin/tool/installaddon/index.php', array(
+ 'installaddonrequest' => $newaddonreq,
+ 'confirm' => 0));
+ if (!isloggedin() or isguestuser()) {
+ // Login and go the the add-on tool page.
+ $SESSION->wantsurl = $target->out();
+ redirect(get_login_url());
+ }
+ redirect($target);
+}
+
$PAGE->set_pagelayout('admin'); // Set a default pagelayout
$documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
$PAGE->set_heading($strinstallation);
$PAGE->set_cacheable(false);
+ /** @var core_admin_renderer $output */
$output = $PAGE->get_renderer('core', 'admin');
echo $output->install_licence_page();
die();
$PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
$PAGE->set_cacheable(false);
+ /** @var core_admin_renderer $output */
$output = $PAGE->get_renderer('core', 'admin');
echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
die();
}
// Detect config cache inconsistency, this happens when you switch branches on dev servers.
-if ($cache) {
- if ($CFG->version != $DB->get_field('config', 'value', array('name'=>'version'))) {
- purge_all_caches();
- redirect(new moodle_url('/admin/index.php'), 'Config cache inconsistency detected, resetting caches...');
- }
+if ($CFG->version != $DB->get_field('config', 'value', array('name'=>'version'))) {
+ purge_all_caches();
+ redirect(new moodle_url('/admin/index.php'), 'Config cache inconsistency detected, resetting caches...');
}
-if ($version > $CFG->version) { // upgrade
+if (!$cache and $version > $CFG->version) { // upgrade
// We purge all of MUC's caches here.
// Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
// This ensures a real config object is loaded and the stores will be purged.
$PAGE->set_title($stradministration);
$PAGE->set_cacheable(false);
+ /** @var core_admin_renderer $output */
$output = $PAGE->get_renderer('core', 'admin');
echo $output->upgrade_stale_php_files_page();
die();
$PAGE->set_heading($strdatabasechecking);
$PAGE->set_cacheable(false);
+ /** @var core_admin_renderer $output */
$output = $PAGE->get_renderer('core', 'admin');
echo $output->upgrade_confirm_page($a->newversion, $maturity);
die();
$PAGE->set_heading($strcurrentrelease);
$PAGE->set_cacheable(false);
+ /** @var core_admin_renderer $output */
$output = $PAGE->get_renderer('core', 'admin');
echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
die();
$reloadurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1));
+ /** @var core_admin_renderer $output */
+ $output = $PAGE->get_renderer('core', 'admin');
+
// check plugin dependencies first
$failed = array();
if (!plugin_manager::instance()->all_plugins_ok($version, $failed)) {
- $output = $PAGE->get_renderer('core', 'admin');
echo $output->unsatisfied_dependencies_page($version, $failed, $reloadurl);
die();
}
redirect($reloadurl);
}
- $output = $PAGE->get_renderer('core', 'admin');
-
$deployer = available_update_deployer::instance();
if ($deployer->enabled()) {
$deployer->initialize($reloadurl, $reloadurl);
}
// Updated human-readable release version if necessary
-if ($release <> $CFG->release) { // Update the release version
+if (!$cache and $release <> $CFG->release) { // Update the release version
set_config('release', $release);
}
-if ($branch <> $CFG->branch) { // Update the branch
+if (!$cache and $branch <> $CFG->branch) { // Update the branch
set_config('branch', $branch);
}
-if (moodle_needs_upgrading()) {
+if (!$cache and moodle_needs_upgrading()) {
if (!$PAGE->headerprinted) {
// means core upgrade or installation was not already done
if (!$confirmplugins) {
// Now we can be sure everything was upgraded and caches work fine,
// redirect if necessary to make sure caching is enabled.
-if (!$cache and !optional_param('sesskey', '', PARAM_RAW)) {
- redirect(new moodle_url($PAGE->url, array('cache' => 1)));
+if (!$cache) {
+ redirect(new moodle_url('/admin/index.php', array('cache' => 1)));
}
// Check for valid admin user - no guest autologin
set_config('registered', time());
}
-// Check if we are returning from an add-on installation request at moodle.org/plugins
-if (!is_null($newaddonreq)) {
- if (!empty($CFG->disableonclickaddoninstall)) {
- // The feature is disabled in config.php, ignore the request.
- } else {
- redirect(new moodle_url('/admin/tool/installaddon/index.php', array(
- 'installaddonrequest' => $newaddonreq,
- 'confirm' => 0)));
- }
-}
-
// setup critical warnings before printing admin tree block
$insecuredataroot = is_dataroot_insecure(true);
$SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
require_once($CFG->libdir . '/pluginlib.php');
require_once($CFG->libdir . '/filelib.php');
-admin_externalpage_setup('pluginsoverview');
-require_capability('moodle/site:config', context_system::instance());
-
$fetchremote = optional_param('fetchremote', false, PARAM_BOOL);
$updatesonly = optional_param('updatesonly', false, PARAM_BOOL);
$contribonly = optional_param('contribonly', false, PARAM_BOOL);
$delete = optional_param('delete', '', PARAM_COMPONENT);
$confirmed = optional_param('confirm', false, PARAM_BOOL);
-$output = $PAGE->get_renderer('core', 'admin');
+// NOTE: do not use admin_externalpage_setup() here because it loads
+// full admin tree which is not possible during uninstallation.
+
+require_login();
+$syscontext = context_system::instance();
+require_capability('moodle/site:config', $syscontext);
$pluginman = plugin_manager::instance();
if ($uninstall) {
require_sesskey();
+
+ if (!$confirmed) {
+ admin_externalpage_setup('pluginsoverview');
+ } else {
+ $PAGE->set_url('/admin/plugins.php');
+ $PAGE->set_context($syscontext);
+ $PAGE->set_pagelayout('maintenance');
+ $PAGE->set_popup_notification_allowed(false);
+ }
+
+ /** @var core_admin_renderer $output */
+ $output = $PAGE->get_renderer('core', 'admin');
+
$pluginfo = $pluginman->get_plugin_info($uninstall);
// Make sure we know the plugin.
if ($delete and $confirmed) {
require_sesskey();
+
+ $PAGE->set_url('/admin/plugins.php');
+ $PAGE->set_context($syscontext);
+ $PAGE->set_pagelayout('maintenance');
+ $PAGE->set_popup_notification_allowed(false);
+
+ /** @var core_admin_renderer $output */
+ $output = $PAGE->get_renderer('core', 'admin');
+
$pluginfo = $pluginman->get_plugin_info($delete);
// Make sure we know the plugin.
if (function_exists('opcache_reset')) {
opcache_reset();
}
- redirect($PAGE->url);
+ // We need to execute upgrade to make sure everything including caches is up to date.
+ redirect(new moodle_url('/admin/index.php'));
}
+admin_externalpage_setup('pluginsoverview');
+
+/** @var core_admin_renderer $output */
+$output = $PAGE->get_renderer('core', 'admin');
+
$checker = available_update_checker::instance();
// Filtering options.
print_error('cannotdeletemissingbehaviour', 'question', $thispageurl);
}
- if (!isset($behaviours[$delete])) {
+ if (!isset($behaviours[$delete]) && !get_config('qbehaviour_' . $delete, 'version')) {
print_error('unknownbehaviour', 'question', $thispageurl, $delete);
}
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('deletingbehaviour', 'question', $behaviourname));
- // Delete any configuration records.
- if (!unset_all_config_for_plugin('qbehaviour_' . $delete)) {
- echo $OUTPUT->notification(get_string('errordeletingconfig', 'admin', 'qbehaviour_' . $delete));
- }
+ // Remove this behaviour from configurations where it might appear.
if (($key = array_search($delete, $disabledbehaviours)) !== false) {
unset($disabledbehaviours[$key]);
set_config('disabledbehaviours', implode(',', $disabledbehaviours), 'question');
set_config('behavioursortorder', implode(',', $behaviourorder), 'question');
}
- // Then the tables themselves
- drop_plugin_tables($delete, core_component::get_plugin_directory('qbehaviour', $delete) . '/db/install.xml', false);
-
- // Remove event handlers and dequeue pending events
- events_uninstall('qbehaviour_' . $delete);
+ // Then uninstall the plugin.
+ uninstall_plugin('qbehaviour', $delete);
+ // Display a message.
$a = new stdClass();
$a->behaviour = $behaviourname;
$a->directory = core_component::get_plugin_directory('qbehaviour', $delete);
print_error('cannotdeletemissingqtype', 'question', $thispageurl);
}
- if (!isset($qtypes[$delete])) {
+ if (!isset($qtypes[$delete]) && !get_config('qtype_' . $delete, 'version')) {
print_error('unknownquestiontype', 'question', $thispageurl, $delete);
}
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('deletingqtype', 'question', $qtypename));
- // Delete any configuration records.
- if (!unset_all_config_for_plugin('qtype_' . $delete)) {
- echo $OUTPUT->notification(get_string('errordeletingconfig', 'admin', 'qtype_' . $delete));
- }
+ // Delete any questoin configuration records mentioning this plugin.
unset_config($delete . '_disabled', 'question');
unset_config($delete . '_sortorder', 'question');
- // Then the tables themselves
- drop_plugin_tables($delete, $qtypes[$delete]->plugin_dir() . '/db/install.xml', false);
-
- // Remove event handlers and dequeue pending events
- events_uninstall('qtype_' . $delete);
+ // Then uninstall the plugin.
+ uninstall_plugin('qtype', $delete);
$a = new stdClass();
$a->qtype = $qtypename;
public function upgrade_confirm_page($strnewversion, $maturity) {
$output = '';
- $continueurl = new moodle_url('index.php', array('confirmupgrade' => 1));
- $cancelurl = new moodle_url('index.php');
+ $continueurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1));
+ $continue = new single_button($continueurl, get_string('continue'), 'get');
+ $cancelurl = new moodle_url('/admin/index.php');
$output .= $this->header();
$output .= $this->maturity_warning($maturity);
- $output .= $this->confirm(get_string('upgradesure', 'admin', $strnewversion), $continueurl, $cancelurl);
+ $output .= $this->confirm(get_string('upgradesure', 'admin', $strnewversion), $continue, $cancelurl);
$output .= $this->footer();
return $output;
$pluginname = $pluginman->plugin_name($pluginfo->component);
+ // Do not show navigation here, they must click one of the buttons.
+ $this->page->set_pagelayout('maintenance');
+ $this->page->set_cacheable(false);
+
$output .= $this->output->header();
$output .= $this->output->heading(get_string('uninstalling', 'core_plugin', array('name' => $pluginname)));
'uninstalldeleteconfirmexternal');
}
- $output .= $this->output->confirm($confirm, $continueurl, $this->page->url);
+ // After any uninstall we must execute full upgrade to finish the cleanup!
+ $output .= $this->output->confirm($confirm, $continueurl, new moodle_url('/admin/index.php'));
$output .= $this->output->footer();
return $output;
public function plugin_uninstall_results_page(plugin_manager $pluginman, plugininfo_base $pluginfo, progress_trace_buffer $progress) {
$output = '';
- $pluginname = $pluginman->plugin_name($pluginfo->component);
+ $pluginname = $pluginfo->component;
$output .= $this->output->header();
$output .= $this->output->heading(get_string('uninstalling', 'core_plugin', array('name' => $pluginname)));
$output .= $this->output->box(get_string('uninstalldelete', 'core_plugin',
array('name' => $pluginname, 'rootdir' => $pluginfo->rootdir)), 'generalbox uninstalldelete');
- $output .= $this->output->continue_button($this->page->url);
+ $output .= $this->output->continue_button(new moodle_url('/admin/index.php'));
$output .= $this->output->footer();
return $output;
if (!$registered) {
- $registerbutton = $this->single_button(new moodle_url('registration/register.php',
+ $registerbutton = $this->single_button(new moodle_url('/admin/registration/register.php',
array('huburl' => HUB_MOODLEORGHUBURL, 'hubname' => 'Moodle.org')),
get_string('register', 'admin'));
$success = $repositorytype->update_options($settings);
} else {
$type = new repository_type($plugin, (array)$fromform, $visible);
- $type->create();
$success = true;
+ if (!$repoid = $type->create()) {
+ $success = false;
+ }
$data = data_submitted();
}
if ($success) {
$this->displaypermissions = $this->allpermissions;
$this->strperms[$this->allpermissions[CAP_INHERIT]] = get_string('notset', 'core_role');
- $this->allcontextlevels = array(
- CONTEXT_SYSTEM => get_string('coresystem'),
- CONTEXT_USER => get_string('user'),
- CONTEXT_COURSECAT => get_string('category'),
- CONTEXT_COURSE => get_string('course'),
- CONTEXT_MODULE => get_string('activitymodule'),
- CONTEXT_BLOCK => get_string('block')
- );
+ $this->allcontextlevels = array();
+ $levels = context_helper::get_all_levels();
+ foreach ($levels as $level => $classname) {
+ $this->allcontextlevels[$level] = context_helper::get_level_name($level);
+ }
}
protected function load_current_permissions() {
--- /dev/null
+<?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/>.
+
+/**
+ * Backend generic 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 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
+ */
+ const MIN_SIZE = 0;
+ /**
+ * @var int Highest (largest) size index
+ */
+ const MAX_SIZE = 5;
+ /**
+ * @var int Default size index
+ */
+ const DEFAULT_SIZE = 3;
+
+ /**
+ * @var bool True if we want a fixed dataset or false to generate random data
+ */
+ protected $fixeddataset;
+
+ /**
+ * @var bool True if displaying progress
+ */
+ protected $progress;
+
+ /**
+ * @var int Epoch time at which last dot was displayed
+ */
+ protected $lastdot;
+
+ /**
+ * @var int Epoch time at which last percentage was displayed
+ */
+ protected $lastpercentage;
+
+ /**
+ * @var int Epoch time at which current step (current set of dots) started
+ */
+ protected $starttime;
+
+ /**
+ * @var int Size code (index in the above arrays)
+ */
+ protected $size;
+
+ /**
+ * Generic generator class
+ *
+ * @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
+ * @throws coding_exception If parameters are invalid
+ */
+ 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->size = $size;
+ $this->fixeddataset = $fixeddataset;
+ $this->progress = $progress;
+ }
+
+ /**
+ * Converts a size name into the numeric constant.
+ *
+ * @param string $sizename Size name e.g. 'L'
+ * @return int Numeric version
+ * @throws coding_exception If the size name is not known
+ */
+ public static function size_for_name($sizename) {
+ for ($size = self::MIN_SIZE; $size <= self::MAX_SIZE; $size++) {
+ if ($sizename == get_string('shortsize_' . $size, 'tool_generator')) {
+ return $size;
+ }
+ }
+ throw new coding_exception("Unknown size name '$sizename'");
+ }
+
+ /**
+ * 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)
+ */
+ protected function log($langstring, $a = null, $leaveopen = false) {
+ if (!$this->progress) {
+ return;
+ }
+ if (CLI_SCRIPT) {
+ echo '* ';
+ } else {
+ echo html_writer::start_tag('li');
+ }
+ echo get_string('progress_' . $langstring, 'tool_generator', $a);
+ if (!$leaveopen) {
+ if (CLI_SCRIPT) {
+ echo "\n";
+ } else {
+ echo html_writer::end_tag('li');
+ }
+ } else {
+ echo ': ';
+ $this->lastdot = time();
+ $this->lastpercentage = $this->lastdot;
+ $this->starttime = microtime(true);
+ }
+ }
+
+ /**
+ * Outputs dots. There is up to one dot per second. Once a minute, it
+ * displays a percentage.
+ * @param int $number Number of completed items
+ * @param int $total Total number of items to complete
+ */
+ protected function dot($number, $total) {
+ if (!$this->progress) {
+ return;
+ }
+ $now = time();
+ if ($now == $this->lastdot) {
+ return;
+ }
+ $this->lastdot = $now;
+ if (CLI_SCRIPT) {
+ echo '.';
+ } else {
+ echo ' . ';
+ }
+ if ($now - $this->lastpercentage >= 30) {
+ echo round(100.0 * $number / $total, 1) . '%';
+ $this->lastpercentage = $now;
+ }
+
+ // Update time limit so PHP doesn't time out.
+ if (!CLI_SCRIPT) {
+ set_time_limit(120);
+ }
+ }
+
+ /**
+ * Ends a log string that was started using log function with $leaveopen.
+ */
+ protected function end_log() {
+ if (!$this->progress) {
+ return;
+ }
+ echo get_string('done', 'tool_generator', round(microtime(true) - $this->starttime, 1));
+ if (CLI_SCRIPT) {
+ echo "\n";
+ } else {
+ echo html_writer::end_tag('li');
+ }
+ }
+
+}
--- /dev/null
+<?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;
+ }
+
+}
--- /dev/null
+<?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/>.
+
+defined('MOODLE_INTERNAL') || die();
+
+require_once($CFG->libdir . '/formslib.php');
+
+/**
+ * Form with options for creating large course.
+ *
+ * @package tool_generator
+ * @copyright 2013 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class tool_generator_make_form extends moodleform {
+
+ public function definition() {
+ $mform = $this->_form;
+
+ $mform->addElement('select', 'size', get_string('size', 'tool_generator'),
+ 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');
+ $mform->setType('shortname', PARAM_TEXT);
+
+ $mform->addElement('submit', 'submit', get_string('createcourse', 'tool_generator'));
+ }
+
+ public function validation($data, $files) {
+ global $DB;
+ $errors = array();
+
+ // Check course doesn't already exist.
+ if (!empty($data['shortname'])) {
+ // Check shortname.
+ $error = tool_generator_course_backend::check_shortname_available($data['shortname']);
+ if ($error) {
+ $errors['shortname'] = $error;
+ }
+ }
+
+ return $errors;
+ }
+}
--- /dev/null
+<?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;
+ }
+
+}
require(dirname(__FILE__) . '/../../../../config.php');
require_once(dirname(__FILE__) . '/../locallib.php');
-if (!debugging('', DEBUG_DEVELOPER)) {
+if (!$CFG->debugdeveloper) {
echo("This script is for developers only!!!\n");
exit(1);
}
--- /dev/null
+<?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 course.
+ *
+ * @package tool_generator
+ * @copyright 2013 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+define('CLI_SCRIPT', true);
+define('NO_OUTPUT_BUFFERING', true);
+
+require(dirname(__FILE__) . '/../../../../config.php');
+require_once($CFG->libdir. '/clilib.php');
+
+// CLI options.
+list($options, $unrecognized) = cli_get_params(
+ array(
+ 'help' => false,
+ 'shortname' => false,
+ 'size' => false,
+ 'fixeddataset' => false,
+ 'bypasscheck' => false,
+ 'quiet' => false
+ ),
+ array(
+ 'h' => 'help'
+ )
+);
+
+// Display help.
+if (!empty($options['help']) || empty($options['shortname']) || empty($options['size'])) {
+ echo "
+Utility to create standard test course. (Also available in GUI interface.)
+
+Not for use on live sites; only normally works if debugging is set to DEVELOPER
+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
+
+-h, --help Print out this help
+
+Example from Moodle root directory:
+\$ php admin/tool/generator/cli/maketestcourse.php --shortname=SIZE_S --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']) && !debugging('', DEBUG_DEVELOPER)) {
+ cli_error(get_string('error_notdebugging', 'tool_generator'));
+}
+
+// Get options.
+$shortname = $options['shortname'];
+$sizename = $options['size'];
+$fixeddataset = $options['fixeddataset'];
+
+// Check size.
+try {
+ $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_course_backend::check_shortname_available($shortname)) {
+ cli_error($error);
+}
+
+// Switch to admin user account.
+session_set_user(get_admin());
+
+// Do backend code to generate course.
+$backend = new tool_generator_course_backend($shortname, $size, $fixeddataset, empty($options['quiet']));
+$id = $backend->make();
--- /dev/null
+<?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();
error('Only for admins');
}
-if (!debugging('', DEBUG_DEVELOPER)) {
+if (!$CFG->debugdeveloper) {
error('This script is for developers only!!!');
}
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
- * Strings for component 'tool_generator', language 'en', branch 'MOODLE_22_STABLE'
+ * Language strings.
*
- * @package tool
- * @subpackage generator
- * @copyright 2011 Petr Skoda
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package tool_generator
+ * @copyright 2013 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-$string['pluginname'] = 'Random course generator';
+$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)';
+$string['explanation'] = 'This tool creates standard test courses that include many
+sections, activities, and files.
+
+This is intended to provide a standardised measure for checking the reliability
+and performance of various system components (such as backup and restore).
+
+This test is important because there have been many cases previously where,
+faced with real-life use cases (e.g. a course with 1,000 activities), the system
+does not work.
+
+Courses created using this feature can occupy a large amount of database and
+filesystem space (tens of gigabytes). You will need to delete the courses
+(and wait for various cleanup runs) to release this space again.
+
+**Do not use this feature on a live system**. Use only on a developer server.
+(To avoid accidental use, this feature is disabled unless you have also selected
+DEVELOPER debugging level.)';
+
+$string['error_notdebugging'] = 'Not available on this server because debugging is not set to DEVELOPER';
+$string['firstname'] = 'Test course user';
+$string['fullname'] = 'Test course: {$a->size}';
+$string['maketestcourse'] = 'Make test course';
+$string['pluginname'] = 'Development data 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_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['smallfiles'] = 'Small files';
--- /dev/null
+<?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/>.
+
+/**
+ * Script creates a standardised large course for testing reliability and
+ * performance.
+ *
+ * @package tool_generator
+ * @copyright 2013 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+// Disable buffering so that the progress output displays gradually without
+// needing to call flush().
+define('NO_OUTPUT_BUFFERING', true);
+
+require('../../../config.php');
+
+require_once($CFG->libdir . '/adminlib.php');
+
+// Initialise page and check permissions.
+admin_externalpage_setup('toolgenerator');
+
+// Start page.
+echo $OUTPUT->header();
+echo $OUTPUT->heading(get_string('maketestcourse', 'tool_generator'));
+
+// Information message.
+$context = context_system::instance();
+echo $OUTPUT->box(format_text(get_string('explanation', 'tool_generator'),
+ FORMAT_MARKDOWN, array('context' => $context)));
+
+// Check debugging is set to DEVELOPER.
+if (!debugging('', DEBUG_DEVELOPER)) {
+ echo $OUTPUT->notification(get_string('error_notdebugging', 'tool_generator'));
+ echo $OUTPUT->footer();
+ exit;
+}
+
+// Set up the form.
+$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_course_backend($data->shortname, $data->size);
+ $id = $backend->make();
+
+ echo html_writer::div(
+ html_writer::link(new moodle_url('/course/view.php', array('id' => $id)),
+ get_string('continue')));
+} else {
+ // Display form.
+ $mform->display();
+}
+
+// Finish page.
+echo $OUTPUT->footer();
--- /dev/null
+<?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/>.
+
+/**
+ * Admin settings.
+ *
+ * @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;
+
+if ($hassiteconfig) {
+ $ADMIN->add('development', new admin_externalpage('toolgenerator',
+ get_string('maketestcourse', 'tool_generator'),
+ $CFG->wwwroot . '/' . $CFG->admin . '/tool/generator/maketestcourse.php'));
+}
+
--- /dev/null
+<?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/>.
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Automated unit testing. This tests the 'make large course' backend,
+ * using the 'XS' option so that it completes quickly.
+ *
+ * @package tool_generator
+ * @copyright 2013 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class tool_generator_maketestcourse_testcase extends advanced_testcase {
+ /**
+ * Creates a small test course and checks all the components have been put in place.
+ */
+ public function test_make_xs_course() {
+ global $DB;
+
+ $this->resetAfterTest();
+ $this->setAdminUser();
+
+ // Create the XS course.
+ $backend = new tool_generator_course_backend('TOOL_MAKELARGECOURSE_XS', 0, false, false);
+ $courseid = $backend->make();
+
+ // Get course details.
+ $course = get_course($courseid);
+ $context = context_course::instance($courseid);
+ $modinfo = get_fast_modinfo($course);
+
+ // Check sections (just section 0 plus one other).
+ $this->assertEquals(2, count($modinfo->get_section_info_all()));
+
+ // Check user is enrolled.
+ $users = get_enrolled_users($context);
+ $this->assertEquals(1, count($users));
+ $this->assertEquals('tool_generator_000001', reset($users)->username);
+
+ // Check there's a page on the course.
+ $pages = $modinfo->get_instances_of('page');
+ $this->assertEquals(1, count($pages));
+
+ // Check there are small files.
+ $resources = $modinfo->get_instances_of('resource');
+ $ok = false;
+ foreach ($resources as $resource) {
+ if ($resource->sectionnum == 0) {
+ // The one in section 0 is the 'small files' resource.
+ $ok = true;
+ break;
+ }
+ }
+ $this->assertTrue($ok);
+
+ // Check it contains 2 files (the default txt and a dat file).
+ $fs = get_file_storage();
+ $resourcecontext = context_module::instance($resource->id);
+ $files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false);
+ $files = array_values($files);
+ $this->assertEquals(2, count($files));
+ $this->assertEquals('resource1.txt', $files[0]->get_filename());
+ $this->assertEquals('smallfile0.dat', $files[1]->get_filename());
+
+ // Check there's a single 'big' file (it's actually only 8KB).
+ $ok = false;
+ foreach ($resources as $resource) {
+ if ($resource->sectionnum == 1) {
+ $ok = true;
+ break;
+ }
+ }
+ $this->assertTrue($ok);
+
+ // Check it contains 2 files.
+ $resourcecontext = context_module::instance($resource->id);
+ $files = $fs->get_area_files($resourcecontext->id, 'mod_resource', 'content', false, 'filename', false);
+ $files = array_values($files);
+ $this->assertEquals(2, count($files));
+ $this->assertEquals('bigfile0.dat', $files[0]->get_filename());
+ $this->assertEquals('resource2.txt', $files[1]->get_filename());
+
+ // Get forum and count the number of posts on it.
+ $forums = $modinfo->get_instances_of('forum');
+ $forum = reset($forums);
+ $posts = $DB->count_records_sql("
+ SELECT
+ COUNT(1)
+ FROM
+ {forum_posts} fp
+ JOIN {forum_discussions} fd ON fd.id = fp.discussion
+ WHERE
+ fd.forum = ?", array($forum->instance));
+ $this->assertEquals(2, $posts);
+ }
+
+ /**
+ * Creates an small test course with fixed data set and checks the used sections and users.
+ */
+ public function test_fixed_data_set() {
+ global $DB;
+
+ $this->resetAfterTest();
+ $this->setAdminUser();
+
+ // Create the S course (more sections and activities than XS).
+ $backend = new tool_generator_course_backend('TOOL_S_COURSE_1', 1, true, false);
+ $courseid = $backend->make();
+
+ // Get course details.
+ $course = get_course($courseid);
+ $modinfo = get_fast_modinfo($course);
+
+ // Check module instances belongs to section 1.
+ $instances = $modinfo->get_instances_of('page');
+ $npageinstances = count($instances);
+ foreach ($instances as $instance) {
+ $this->assertEquals(1, $instance->sectionnum);
+ }
+
+ // Users that started discussions are the same.
+ $forums = $modinfo->get_instances_of('forum');
+ $nforuminstances = count($forums);
+ $discussions = forum_get_discussions(reset($forums), 'd.timemodified ASC');
+ $lastusernumber = 0;
+ $discussionstarters = array();
+ foreach ($discussions as $discussion) {
+ $usernumber = intval($discussion->lastname);
+
+ // Checks that the users are odd numbers.
+ $this->assertEquals(1, $usernumber % 2);
+
+ // Checks that the users follows an increasing order.
+ $this->assertGreaterThan($lastusernumber, $usernumber);
+ $lastusernumber = $usernumber;
+ $discussionstarters[$discussion->userid] = $discussion->subject;
+ }
+
+ }
+}
/**
* Version details.
*
- * @package tool
- * @subpackage generator
- * @copyright 2009 Nicolas Connault
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @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();
-$plugin->version = 2013050100; // The current plugin version (Date: YYYYMMDDXX)
-$plugin->requires = 2013050100; // Requires this Moodle version
-$plugin->component = 'tool_generator'; // Full name of the plugin (used for diagnostics)
-
-$plugin->maturity = MATURITY_ALPHA; // this version's maturity level
+$plugin->version = 2013090200;
+$plugin->requires = 2013090200;
+$plugin->component = 'tool_generator';
navigation_node::override_active_url(new moodle_url('/admin/tool/phpunit/index.php'));
admin_externalpage_setup('toolphpunitwebrunner');
-if (!debugging('', DEBUG_DEVELOPER)) {
+if (!$CFG->debugdeveloper) {
error('Not available on production sites, sorry.');
}
if ($code != 0) {
tool_phpunit_problem('Can not initialize database');
}
- $CFG->debug = 0; // no pesky redirect warning, we really want to redirect
+ set_debugging(DEBUG_NONE, false); // Hack: no redirect warning, we really want to redirect.
redirect(new moodle_url($PAGE->url, array('execute'=>1, 'tespath'=>$testpath, 'testclass'=>$testclass, 'sesskey'=>sesskey())), 'Reloading page');
echo $OUTPUT->footer();
die();
if ($code != 0) {
tool_phpunit_problem('Can not initialize database');
}
- $CFG->debug = 0; // no pesky redirect warning, we really want to redirect
+ set_debugging(DEBUG_NONE, false); // Hack: no redirect warning, we really want to redirect.
redirect(new moodle_url($PAGE->url, array('execute'=>1, 'tespath'=>$testpath, 'testclass'=>$testclass, 'sesskey'=>sesskey())), 'Reloading page');
die();
*/
class tool_uploadcourse_helper {
- /**
- * Remove the restore content from disk and cache.
- *
- * @return void
- */
- public static function clean_restore_content() {
- global $CFG;
-
- // There are some sloppy unclosed file handles in backup/restore code,
- // let's hope somebody unset all controllers before calling this
- // and destroy magic will close all remaining open file handles,
- // otherwise Windows will fail deleting the directory.
- gc_collect_cycles();
-
- if (!empty($CFG->keeptempdirectoriesonbackup)) {
- $cache = cache::make('tool_uploadcourse', 'helper');
- $backupids = (array) $cache->get('backupids');
- foreach ($backupids as $cachekey => $backupid) {
- $cache->delete($cachekey);
- fulldelete("$CFG->tempdir/backup/$backupid/");
- }
- $cache->delete('backupids');
- }
- }
-
/**
* Generate a shortname based on a template.
*
* Get the restore content tempdir.
*
* The tempdir is the sub directory in which the backup has been extracted.
- * This caches the result for better performance.
+ *
+ * This caches the result for better performance, but $CFG->keeptempdirectoriesonbackup
+ * needs to be enabled, otherwise the cache is ignored.
*
* @param string $backupfile path to a backup file.
* @param string $shortname shortname of a course.
$cachekey = null;
if (!empty($backupfile)) {
$backupfile = realpath($backupfile);
+ if (empty($backupfile) || !is_readable($backupfile)) {
+ $errors['cannotreadbackupfile'] = new lang_string('cannotreadbackupfile', 'tool_uploadcourse');
+ return false;
+ }
$cachekey = 'backup_path:' . $backupfile;
} else if (!empty($shortname) || is_numeric($shortname)) {
$cachekey = 'backup_sn:' . $shortname;
return false;
}
- $cache = cache::make('tool_uploadcourse', 'helper');
- if (($backupid = $cache->get($cachekey)) === false) {
- // Use false instead of null because it would consider that the cache
- // key has not been set.
- $backupid = false;
+ // If $CFG->keeptempdirectoriesonbackup is not set to true, any restore happening would
+ // automatically delete the backup directory... causing the cache to return an unexisting directory.
+ $usecache = !empty($CFG->keeptempdirectoriesonbackup);
+ if ($usecache) {
+ $cache = cache::make('tool_uploadcourse', 'helper');
+ }
+
+ // If we don't use the cache, or if we do and not set, or the directory doesn't exist any more.
+ if (!$usecache || (($backupid = $cache->get($cachekey)) === false || !is_dir("$CFG->tempdir/backup/$backupid"))) {
+
+ // Use null instead of false because it would consider that the cache key has not been set.
+ $backupid = null;
+
if (!empty($backupfile)) {
- if (!is_readable($backupfile)) {
- $errors['cannotreadbackupfile'] = new lang_string('cannotreadbackupfile', 'tool_uploadcourse');
- } else {
- // Extracting the backup file.
- $packer = get_file_packer('application/vnd.moodle.backup');
- $backupid = restore_controller::get_tempdir_name(SITEID, $USER->id);
- $path = "$CFG->tempdir/backup/$backupid/";
- $result = $packer->extract_to_pathname($backupfile, $path);
- if (!$result) {
- $errors['invalidbackupfile'] = new lang_string('invalidbackupfile', 'tool_uploadcourse');
- }
+ // Extracting the backup file.
+ $packer = get_file_packer('application/vnd.moodle.backup');
+ $backupid = restore_controller::get_tempdir_name(SITEID, $USER->id);
+ $path = "$CFG->tempdir/backup/$backupid/";
+ $result = $packer->extract_to_pathname($backupfile, $path);
+ if (!$result) {
+ $errors['invalidbackupfile'] = new lang_string('invalidbackupfile', 'tool_uploadcourse');
}
} else if (!empty($shortname) || is_numeric($shortname)) {
// Creating restore from an existing course.
new lang_string('coursetorestorefromdoesnotexist', 'tool_uploadcourse');
}
}
- $cache->set($cachekey, $backupid);
- // Store all the directories to be able to remove them in self::clean_restore_content().
- $backupids = (array) $cache->get('backupids');
- $backupids[$cachekey] = $backupid;
- $cache->set('backupids', $backupids);
+ if ($usecache) {
+ $cache->set($cachekey, $backupid);
+ }
}
+ if ($backupid === null) {
+ $backupid = false;
+ }
return $backupid;
}
$tracker->finish();
$tracker->results($total, $created, $updated, $deleted, $errors);
-
- $this->remove_restore_content();
}
/**
}
$tracker->finish();
- $this->remove_restore_content();
return $preview;
}
- /**
- * Delete the restore object.
- *
- * @return void
- */
- protected function remove_restore_content() {
- tool_uploadcourse_helper::clean_restore_content();
- }
-
/**
* Reset the current process.
*
$mform->disabledIf('options[shortnametemplate]', 'options[mode]', 'eq', tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE);
$mform->disabledIf('options[shortnametemplate]', 'options[mode]', 'eq', tool_uploadcourse_processor::MODE_UPDATE_ONLY);
+ // Restore file is not in the array options on purpose, because formslib can't handle it!
$contextid = $this->_customdata['contextid'];
$mform->addElement('hidden', 'contextid', $contextid);
$mform->setType('contextid', PARAM_INT);
- $mform->addElement('filepicker', 'options[restorefile]', get_string('templatefile', 'tool_uploadcourse'));
- $mform->addHelpButton('options[restorefile]', 'templatefile', 'tool_uploadcourse');
+ $mform->addElement('filepicker', 'restorefile', get_string('templatefile', 'tool_uploadcourse'));
+ $mform->addHelpButton('restorefile', 'templatefile', 'tool_uploadcourse');
$mform->addElement('text', 'options[templatecourse]', get_string('coursetemplatename', 'tool_uploadcourse'));
$mform->setType('options[templatecourse]', PARAM_TEXT);
$mform->closeHeaderBefore('buttonar');
}
- /**
- * Server side validation.
- * @param array $data - form data
- * @param object $files - form files
- * @return array $errors - form errors
- */
- public function validation($data, $files) {
- $errors = parent::validation($data, $files);
- $columns = $this->_customdata['columns'];
- $optype = $data['options']['mode'];
-
- // Look for other required data.
- if ($optype != tool_uploadcourse_processor::MODE_UPDATE_ONLY) {
- if (!in_array('fullname', $columns)) {
- if (isset($errors['mode'])) {
- $errors['mode'] .= ' ';
- }
- $errors['mode'] .= get_string('missingfield', 'error', 'fullname');
- }
- if (!in_array('summary', $columns)) {
- if (isset($errors['mode'])) {
- $errors['mode'] .= ' ';
- }
- $errors['mode'] .= get_string('missingfield', 'error', 'summary');
- }
- }
-
- return $errors;
- }
}
$options = (array) $form2data->options;
$defaults = (array) $form2data->defaults;
+
+ // Restorefile deserves its own logic because formslib does not really appreciate
+ // when the name of a filepicker is an array...
+ $options['restorefile'] = '';
+ if (!empty($form2data->restorefile)) {
+ $options['restorefile'] = $mform2->save_temp_file('restorefile');
+ }
$processor = new tool_uploadcourse_processor($cir, $options, $defaults);
echo $OUTPUT->header();
echo $OUTPUT->continue_button($returnurl);
}
+ // Deleting the file after processing or preview.
+ if (!empty($options['restorefile'])) {
+ @unlink($options['restorefile']);
+ }
+
} else {
$processor = new tool_uploadcourse_processor($cir, $form1data->options, array());
echo $OUTPUT->header();
}
$this->assertTrue($found);
+ // Restoring twice from the same course should work.
+ $data = array('shortname' => 'B1', 'templatecourse' => $c1->shortname, 'summary' => 'B', 'category' => 1,
+ 'fullname' => 'B1');
+ $co = new tool_uploadcourse_course($mode, $updatemode, $data);
+ $this->assertTrue($co->prepare());
+ $co->proceed();
+ $course = $DB->get_record('course', array('shortname' => 'B1'));
+ $modinfo = get_fast_modinfo($course);
+ $found = false;
+ foreach ($modinfo->get_cms() as $cmid => $cm) {
+ if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
+ $found = true;
+ break;
+ }
+ }
+ $this->assertTrue($found);
+
// Restore the time limit to prevent warning.
set_time_limit(0);
}
}
$this->assertTrue($found);
+ // Restoring twice from the same file should work.
+ $data = array('shortname' => 'B1', 'backupfile' => __DIR__ . '/fixtures/backup.mbz',
+ 'summary' => 'B', 'category' => 1, 'fullname' => 'B1');
+ $co = new tool_uploadcourse_course($mode, $updatemode, $data);
+ $this->assertTrue($co->prepare());
+ $co->proceed();
+ $course = $DB->get_record('course', array('shortname' => 'B1'));
+ $modinfo = get_fast_modinfo($course);
+ $found = false;
+ foreach ($modinfo->get_cms() as $cmid => $cm) {
+ if ($cm->modname == 'glossary' && $cm->name == 'Imported Glossary') {
+ $found = true;
+ } else if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
+ // We should not find this!
+ $this->assertTrue(false);
+ }
+ }
+ $this->assertTrue($found);
+
// Restore the time limit to prevent warning.
set_time_limit(0);
}
$bc->destroy();
unset($bc); // File logging is a mess, we can only try to rely on gc to close handles.
+ $oldcfg = isset($CFG->keeptempdirectoriesonbackup) ? $CFG->keeptempdirectoriesonbackup : false;
+ $CFG->keeptempdirectoriesonbackup = true;
+
// Checking restore dir.
$dir = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
$bcinfo = backup_general_helper::get_backup_information($dir);
$this->assertFalse($dir);
$this->assertArrayHasKey('coursetorestorefromdoesnotexist', $errors);
- // Cleaning content directories.
- $oldcfg = isset($CFG->keeptempdirectoriesonbackup) ? $CFG->keeptempdirectoriesonbackup : false;
- $dir = "$CFG->tempdir/backup/$dir";
- $this->assertTrue(file_exists($dir));
-
+ // Trying again without caching. $CFG->keeptempdirectoriesonbackup is required for caching.
$CFG->keeptempdirectoriesonbackup = false;
- tool_uploadcourse_helper::clean_restore_content();
- $this->assertTrue(file_exists($dir));
- $CFG->keeptempdirectoriesonbackup = true;
- tool_uploadcourse_helper::clean_restore_content();
- $this->assertFalse(file_exists($dir));
+ // Checking restore dir.
+ $dir = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
+ $dir2 = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
+ $this->assertNotEquals($dir, $dir2);
+
+ // Checking with a shortname.
+ $dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
+ $dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
+ $this->assertNotEquals($dir, $dir2);
+
+ // Get a course that does not exist.
+ $errors = array();
+ $dir = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors);
+ $this->assertFalse($dir);
+ $this->assertArrayHasKey('coursetorestorefromdoesnotexist', $errors);
+ $dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors);
+ $this->assertEquals($dir, $dir2);
$CFG->keeptempdirectoriesonbackup = $oldcfg;
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/csvlib.class.php');
require_once($CFG->dirroot.'/user/profile/lib.php');
+require_once($CFG->dirroot.'/user/lib.php');
require_once($CFG->dirroot.'/group/lib.php');
require_once($CFG->dirroot.'/cohort/lib.php');
require_once('locallib.php');
}
if ($doupdate or $existinguser->password !== $oldpw) {
- // we want only users that were really updated
-
- $DB->update_record('user', $existinguser);
+ // We want only users that were really updated.
+ user_update_user($existinguser, false);
$upt->track('status', $struserupdated);
$usersupdated++;
profile_save_data($existinguser);
}
- events_trigger('user_updated', $existinguser);
-
if ($bulk == UU_BULK_UPDATED or $bulk == UU_BULK_ALL) {
if (!in_array($user->id, $SESSION->bulk_users)) {
$SESSION->bulk_users[] = $user->id;
$upt->track('password', '-', 'normal', false);
}
- // create user - insert_record ignores any extra properties
- $user->id = $DB->insert_record('user', $user);
+ $user->id = user_create_user($user, false);
$upt->track('username', html_writer::link(new moodle_url('/user/profile.php', array('id'=>$user->id)), s($user->username)), 'normal', false);
// pre-process custom profile menu fields data from csv file
// make sure user context exists
context_user::instance($user->id);
- events_trigger('user_created', $user);
-
if ($bulk == UU_BULK_NEW or $bulk == UU_BULK_ALL) {
if (!in_array($user->id, $SESSION->bulk_users)) {
$SESSION->bulk_users[] = $user->id;
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/authlib.php');
require_once($CFG->dirroot.'/user/filters/lib.php');
+ require_once($CFG->dirroot.'/user/lib.php');
$delete = optional_param('delete', 0, PARAM_INT);
$confirm = optional_param('confirm', '', PARAM_ALPHANUM); //md5 confirmation hash
if ($user = $DB->get_record('user', array('id'=>$suspend, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
if (!is_siteadmin($user) and $USER->id != $user->id and $user->suspended != 1) {
$user->suspended = 1;
- $user->timemodified = time();
- $DB->set_field('user', 'suspended', $user->suspended, array('id'=>$user->id));
- $DB->set_field('user', 'timemodified', $user->timemodified, array('id'=>$user->id));
- // force logout
+ // Force logout.
session_kill_user($user->id);
- events_trigger('user_updated', $user);
+ user_update_user($user, false);
}
}
redirect($returnurl);
if ($user = $DB->get_record('user', array('id'=>$unsuspend, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
if ($user->suspended != 0) {
$user->suspended = 0;
- $user->timemodified = time();
- $DB->set_field('user', 'suspended', $user->suspended, array('id'=>$user->id));
- $DB->set_field('user', 'timemodified', $user->timemodified, array('id'=>$user->id));
- events_trigger('user_updated', $user);
+ user_update_user($user, false);
}
}
redirect($returnurl);
require_once($CFG->dirroot.'/course/lib.php');
// Ensure errors are well explained
-$CFG->debug = DEBUG_NORMAL;
+set_debugging(DEBUG_DEVELOPER, true);
if (!is_enabled_auth('cas')) {
error_log('[AUTH CAS] '.get_string('pluginnotenabled', 'auth_ldap'));
$remove_users = $DB->get_records_sql($sql, $params);
if (!empty($remove_users)) {
+ require_once($CFG->dirroot.'/user/lib.php');
$trace->output(get_string('auth_dbuserstoremove','auth_db', count($remove_users)));
foreach ($remove_users as $user) {
$updateuser = new stdClass();
$updateuser->id = $user->id;
$updateuser->suspended = 1;
- $updateuser->timemodified = time();
- $DB->update_record('user', $updateuser);
+ user_update_user($updateuser, false);
$trace->output(get_string('auth_dbsuspenduser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)), 1);
}
}
function user_signup($user, $notify=true) {
global $CFG, $DB;
require_once($CFG->dirroot.'/user/profile/lib.php');
+ require_once($CFG->dirroot.'/user/lib.php');
$user->password = hash_internal_user_password($user->password);
- $user->id = $DB->insert_record('user', $user);
+ $user->id = user_create_user($user, false);
- /// Save any custom profile field information
+ // Save any custom profile field information.
profile_save_data($user);
- $user = $DB->get_record('user', array('id'=>$user->id));
- events_trigger('user_created', $user);
-
if (! send_confirmation_email($user)) {
print_error('auth_emailnoemail','auth_email');
}
require_once($CFG->libdir.'/authlib.php');
require_once($CFG->libdir.'/ldaplib.php');
+require_once($CFG->dirroot.'/user/lib.php');
/**
* LDAP authentication plugin.
print_error('auth_ldap_create_error', 'auth_ldap');
}
- $user->id = $DB->insert_record('user', $user);
+ $user->id = user_create_user($user, false);
// Save any custom profile field information
profile_save_data($user);
update_internal_user_password($user, $plainslashedpassword);
$user = $DB->get_record('user', array('id'=>$user->id));
- events_trigger('user_created', $user);
if (! send_confirmation_email($user)) {
print_error('noemail', 'auth_ldap');
if (!$this->user_activate($username)) {
return AUTH_CONFIRM_FAIL;
}
- $DB->set_field('user', 'confirmed', 1, array('id'=>$user->id));
+ $user->confirmed = 1;
if ($user->firstaccess == 0) {
- $DB->set_field('user', 'firstaccess', time(), array('id'=>$user->id));
+ $user->firstaccess = time();
}
- $euser = $DB->get_record('user', array('id' => $user->id));
- events_trigger('user_updated', $euser);
+ require_once($CFG->dirroot.'/user/lib.php');
+ user_update_user($user, false);
return AUTH_CONFIRM_OK;
}
} else {
$updateuser = new stdClass();
$updateuser->id = $user->id;
$updateuser->suspended = 1;
- $DB->update_record('user', $updateuser);
+ user_update_user($updateuser, false);
echo "\t"; print_string('auth_dbsuspenduser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)); echo "\n";
- $euser = $DB->get_record('user', array('id' => $user->id));
- events_trigger('user_updated', $euser);
session_kill_user($user->id);
}
} else {
$updateuser->id = $user->id;
$updateuser->auth = $this->authtype;
$updateuser->suspended = 0;
- $DB->update_record('user', $updateuser);
+ user_update_user($updateuser, false);
echo "\t"; print_string('auth_dbreviveduser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)); echo "\n";
- $euser = $DB->get_record('user', array('id' => $user->id));
- events_trigger('user_updated', $euser);
}
} else {
print_string('nouserentriestorevive', 'auth_ldap');
$user->lang = $CFG->lang;
}
- $id = $DB->insert_record('user', $user);
+ $id = user_create_user($user, false);
echo "\t"; print_string('auth_dbinsertuser', 'auth_db', array('name'=>$user->username, 'id'=>$id)); echo "\n";
$euser = $DB->get_record('user', array('id' => $id));
- events_trigger('user_created', $euser);
+
if (!empty($this->config->forcechangepassword)) {
set_user_preference('auth_forcepasswordchange', 1, $id);
}
$updatekeys = array_keys($newinfo);
}
- foreach ($updatekeys as $key) {
- if (isset($newinfo[$key])) {
- $value = $newinfo[$key];
- } else {
- $value = '';
- }
+ if (!empty($updatekeys)) {
+ $newuser = new stdClass();
+ $newuser->id = $userid;
- if (!empty($this->config->{'field_updatelocal_' . $key})) {
- if ($user->{$key} != $value) { // only update if it's changed
- $DB->set_field('user', $key, $value, array('id'=>$userid));
+ foreach ($updatekeys as $key) {
+ if (isset($newinfo[$key])) {
+ $value = $newinfo[$key];
+ } else {
+ $value = '';
+ }
+
+ if (!empty($this->config->{'field_updatelocal_' . $key})) {
+ // Only update if it's changed.
+ if ($user->{$key} != $value) {
+ $newuser->$key = $value;
+ }
}
}
- }
- if (!empty($updatekeys)) {
- $euser = $DB->get_record('user', array('id' => $userid));
- events_trigger('user_updated', $euser);
+ user_update_user($newuser, false);
}
} else {
return false;
// Now start the whole NTLM machinery.
if($this->config->ntlmsso_ie_fastpath == AUTH_NTLM_FASTPATH_YESATTEMPT ||
$this->config->ntlmsso_ie_fastpath == AUTH_NTLM_FASTPATH_YESFORM) {
-
- if(check_browser_version('MSIE')) {
+ if (core_useragent::check_ie_version()) {
$sesskey = sesskey();
redirect($CFG->wwwroot.'/auth/ldap/ntlmsso_magic.php?sesskey='.$sesskey);
} else if ($this->config->ntlmsso_ie_fastpath == AUTH_NTLM_FASTPATH_YESFORM) {
require_once($CFG->dirroot.'/course/lib.php');
// Ensure errors are well explained
-$CFG->debug = DEBUG_NORMAL;
+set_debugging(DEBUG_DEVELOPER, true);
if (!is_enabled_auth('ldap')) {
error_log('[AUTH LDAP] '.get_string('pluginnotenabled', 'auth_ldap'));
if ($authplugin->ntlmsso_magic($sesskey) && file_exists($file)) {
if (!empty($authplugin->config->ntlmsso_ie_fastpath)) {
- if (check_browser_version('MSIE')) {
+ if (core_useragent::check_ie_version()) {
// $PAGE->https_required() up above takes care of what $CFG->httpswwwroot should be.
redirect($CFG->httpswwwroot.'/auth/ldap/ntlmsso_finish.php');
}
global $CFG, $DB;
require_once $CFG->dirroot . '/mnet/xmlrpc/client.php';
require_once $CFG->libdir . '/gdlib.php';
+ require_once($CFG->dirroot.'/user/lib.php');
// verify the remote host is configured locally before attempting RPC call
if (! $remotehost = $DB->get_record('mnet_host', array('wwwroot' => $remotepeer->wwwroot, 'deleted' => 0))) {
if (empty($localuser->firstaccess)) { // Now firstaccess, grab it here
$localuser->firstaccess = time();
}
-
- $DB->update_record('user', $localuser);
+ user_update_user($localuser, false);
if (!$firsttime) {
// repeat customer! let the IDP know about enrolments
}
$backup = new backup_ui($bc);
$backup->process();
-if ($backup->get_stage() == backup_ui::STAGE_FINAL) {
- $backup->execute();
-} else {
- $backup->save_controller();
-}
$PAGE->set_title($heading.': '.$backup->get_stage_name());
$PAGE->set_heading($heading);
if ($backup->enforce_changed_dependencies()) {
debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER);
}
+
+if ($backup->get_stage() == backup_ui::STAGE_FINAL) {
+ // Display an extra progress bar so that we can show the progress first.
+ echo html_writer::start_div('', array('id' => 'executionprogress'));
+ echo $renderer->progress_bar($backup->get_progress_bar());
+ $backup->get_controller()->set_progress(new core_backup_display_progress());
+ $backup->execute();
+ echo html_writer::end_div();
+ echo html_writer::script('document.getElementById("executionprogress").style.display = "none";');
+} else {
+ $backup->save_controller();
+}
+
echo $renderer->progress_bar($backup->get_progress_bar());
echo $backup->display($renderer);
$backup->destroy();
protected $destination; // Destination chain object (fs_moodle, fs_os, db, email...)
protected $logger; // Logging chain object (moodle, inline, fs, db, syslog)
+ /**
+ * @var core_backup_progress Progress reporting object.
+ */
+ protected $progress;
+
protected $checksum; // Cache @checksumable results for lighter @is_checksum_correct() uses
/**
// Default logger chain (based on interactive/execution)
$this->logger = backup_factory::get_logger_chain($this->interactive, $this->execution, $this->backupid);
+ // By default there is no progress reporter. Interfaces that wish to
+ // display progress must set it.
+ $this->progress = new core_backup_null_progress();
+
// Instantiate the output_controller singleton and active it if interactive and inmediate
$oc = output_controller::get_instance();
if ($this->interactive == backup::INTERACTIVE_YES && $this->execution == backup::EXECUTION_INMEDIATE) {
return $this->logger;
}
+ /**
+ * Gets the progress reporter, which can be used to report progress within
+ * the backup or restore process.
+ *
+ * @return core_backup_progress Progress reporting object
+ */
+ public function get_progress() {
+ return $this->progress;
+ }
+
+ /**
+ * Sets the progress reporter.
+ *
+ * @param core_backup_progress $progress Progress reporting object
+ */
+ public function set_progress(core_backup_progress $progress) {
+ $this->progress = $progress;
+ }
+
/**
* Executes the backup
* @return void Throws and exception of completes
protected $logger; // Logging chain object (moodle, inline, fs, db, syslog)
+ /**
+ * @var core_backup_progress Progress reporting object.
+ */
+ protected $progress;
+
protected $checksum; // Cache @checksumable results for lighter @is_checksum_correct() uses
/**
// Default logger chain (based on interactive/execution)
$this->logger = backup_factory::get_logger_chain($this->interactive, $this->execution, $this->restoreid);
+ // By default there is no progress reporter. Interfaces that wish to
+ // display progress must set it.
+ $this->progress = new core_backup_null_progress();
+
// Instantiate the output_controller singleton and active it if interactive and inmediate
$oc = output_controller::get_instance();
if ($this->interactive == backup::INTERACTIVE_YES && $this->execution == backup::EXECUTION_INMEDIATE) {
return $this->logger;
}
+ /**
+ * Gets the progress reporter, which can be used to report progress within
+ * the backup or restore process.
+ *
+ * @return core_backup_progress Progress reporting object
+ */
+ public function get_progress() {
+ return $this->progress;
+ }
+
+ /**
+ * Sets the progress reporter.
+ *
+ * @param core_backup_progress $progress Progress reporting object
+ */
+ public function set_progress(core_backup_progress $progress) {
+ $this->progress = $progress;
+ }
+
public function execute_plan() {
// Basic/initial prevention against time/memory limits
set_time_limit(1 * 60 * 60); // 1 hour for 1 course initially granted
// If it's the final stage process the import
if ($backup->get_stage() == backup_ui::STAGE_FINAL) {
+ echo $OUTPUT->header();
+
+ // Display an extra progress bar so that we can show the current stage.
+ echo html_writer::start_div('', array('id' => 'executionprogress'));
+ echo $renderer->progress_bar($backup->get_progress_bar());
+
+ // Start the progress display - we split into 2 chunks for backup and restore.
+ $progress = new core_backup_display_progress();
+ $progress->start_progress('', 2);
+ $backup->get_controller()->set_progress($progress);
+
// First execute the backup
$backup->execute();
$backup->destroy();
unset($backup);
+ // Note that we've done that progress.
+ $progress->progress(1);
+
// Check whether the backup directory still exists. If missing, something
// went really wrong in backup, throw error. Note that backup::MODE_IMPORT
// backups don't store resulting files ever
// Prepare the restore controller. We don't need a UI here as we will just use what
// ever the restore has (the user has just chosen).
$rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_YES, backup::MODE_IMPORT, $USER->id, $restoretarget);
+ $rc->set_progress($progress);
// Convert the backup if required.... it should NEVER happed
if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
$rc->convert();
// Mark the UI finished.
$rc->finish_ui();
// Execute prechecks
+ $warnings = false;
if (!$rc->execute_precheck()) {
$precheckresults = $rc->get_precheck_results();
- if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
- fulldelete($tempdestination);
-
- echo $OUTPUT->header();
- echo $renderer->precheck_notices($precheckresults);
- echo $OUTPUT->continue_button(new moodle_url('/course/view.php', array('id'=>$course->id)));
- echo $OUTPUT->footer();
- die();
- }
- } else {
- if ($restoretarget == backup::TARGET_CURRENT_DELETING || $restoretarget == backup::TARGET_EXISTING_DELETING) {
- restore_dbops::delete_course_content($course->id);
+ if (is_array($precheckresults)) {
+ if (!empty($precheckresults['errors'])) { // If errors are found, terminate the import.
+ fulldelete($tempdestination);
+
+ echo $OUTPUT->header();
+ echo $renderer->precheck_notices($precheckresults);
+ echo $OUTPUT->continue_button(new moodle_url('/course/view.php', array('id'=>$course->id)));
+ echo $OUTPUT->footer();
+ die();
+ }
+ if (!empty($precheckresults['warnings'])) { // If warnings are found, go ahead but display warnings later.
+ $warnings = $precheckresults['warnings'];
+ }
}
- // Execute the restore
- $rc->execute_plan();
}
+ if ($restoretarget == backup::TARGET_CURRENT_DELETING || $restoretarget == backup::TARGET_EXISTING_DELETING) {
+ restore_dbops::delete_course_content($course->id);
+ }
+ // Execute the restore.
+ $rc->execute_plan();
// Delete the temp directory now
fulldelete($tempdestination);
+ // All progress complete. Hide progress area.
+ $progress->end_progress();
+ echo html_writer::end_div();
+ echo html_writer::script('document.getElementById("executionprogress").style.display = "none";');
+
// Display a notification and a continue button
- echo $OUTPUT->header();
- echo $OUTPUT->notification(get_string('importsuccess', 'backup'),'notifysuccess');
+ if ($warnings) {
+ echo $OUTPUT->box_start();
+ echo $OUTPUT->notification(get_string('warning'), 'notifywarning');
+ echo html_writer::start_tag('ul', array('class'=>'list'));
+ foreach ($warnings as $warning) {
+ echo html_writer::tag('li', $warning);
+ }
+ echo html_writer::end_tag('ul');
+ echo $OUTPUT->box_end();
+ }
+ echo $OUTPUT->notification(get_string('importsuccess', 'backup'), 'notifysuccess');
echo $OUTPUT->continue_button(new moodle_url('/course/view.php', array('id'=>$course->id)));
echo $OUTPUT->footer();
$this->built = true;
}
+ public function get_weight() {
+ // The final task takes ages, so give it 20 times the weight of a normal task.
+ return 20;
+ }
+
// Protected API starts here
/**
*/
abstract class restore_qtype_plugin extends restore_plugin {
+ /*
+ * A simple answer to id cache for a single questions answers.
+ * @var array
+ */
+ private $questionanswercache = array();
+
+ /*
+ * The id of the current question in the questionanswercache.
+ * @var int
+ */
+ private $questionanswercacheid = null;
+
/**
* Add to $paths the restore_path_elements needed
* to handle question_answers for a given question
// The question existed, we need to map the existing question_answers
} else {
- // Look in question_answers by answertext matching
- $sql = 'SELECT id
- FROM {question_answers}
- WHERE question = ?
- AND ' . $DB->sql_compare_text('answer', 255) . ' = ' . $DB->sql_compare_text('?', 255);
- $params = array($newquestionid, $data->answertext);
- $newitemid = $DB->get_field_sql($sql, $params);
-
- // Not able to find the answer, let's try cleaning the answertext
- // of all the question answers in DB as slower fallback. MDL-30018.
- if (!$newitemid) {
+ // Have we cached the current question?
+ if ($this->questionanswercacheid !== $newquestionid) {
+ // The question changed, purge and start again!
+ $this->questionanswercache = array();
$params = array('question' => $newquestionid);
$answers = $DB->get_records('question_answers', $params, '', 'id, answer');
+ $this->questionanswercacheid = $newquestionid;
+ // Cache all cleaned answers for a simple text match.
foreach ($answers as $answer) {
- // Clean in the same way than {@link xml_writer::xml_safe_utf8()}.
+ // MDL-30018: Clean in the same way as {@link xml_writer::xml_safe_utf8()}.
$clean = preg_replace('/[\x-\x8\xb-\xc\xe-\x1f\x7f]/is','', $answer->answer); // Clean CTRL chars.
$clean = preg_replace("/\r\n|\r/", "\n", $clean); // Normalize line ending.
- if ($clean === $data->answertext) {
- $newitemid = $data->id;
- }
+ $this->questionanswercache[$clean] = $answer->id;
}
}
- // If we haven't found the newitemid, something has gone really wrong, question in DB
- // is missing answers, exception
- if (!$newitemid) {
+ if (!isset($this->questionanswercache[$data->answertext])) {
+ // If we haven't found the matching answer, something has gone really wrong, the question in the DB
+ // is missing answers, throw an exception.
$info = new stdClass();
$info->filequestionid = $oldquestionid;
$info->dbquestionid = $newquestionid;
$info->answer = $data->answertext;
throw new restore_step_exception('error_question_answers_missing_in_db', $info);
}
+ $newitemid = $this->questionanswercache[$data->answertext];
}
// Create mapping (we'll use this intensively when restoring question_states. And also answerfeedback files)
$this->set_mapping('question_answer', $oldid, $newitemid);
$data->theme = '';
}
+ // Check if this is an old SCORM course format.
+ if ($data->format == 'scorm') {
+ $data->format = 'singleactivity';
+ $data->activitytype = 'scorm';
+ }
+
// Course record ready, update it
$DB->update_record('course', $data);
// Iterate over aliases in the queue.
foreach ($rs as $record) {
- $info = restore_dbops::decode_backup_temp_info($record->info);
+ $info = backup_controller_dbops::decode_backup_temp_info($record->info);
// Try to pick a repository instance that should serve the alias.
$repository = $this->choose_repository($info);
}
$outcome = $restore->process();
+$heading = $course->fullname;
+
+$PAGE->set_title($heading.': '.$restore->get_stage_name());
+$PAGE->set_heading($heading);
+$PAGE->navbar->add($restore->get_stage_name());
+
+$renderer = $PAGE->get_renderer('core','backup');
+echo $OUTPUT->header();
+if (!$restore->is_independent() && $restore->enforce_changed_dependencies()) {
+ debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER);
+}
+
if (!$restore->is_independent()) {
if ($restore->get_stage() == restore_ui::STAGE_PROCESS && !$restore->requires_substage()) {
try {
+ // Display an extra progress bar so that we can show the progress first.
+ echo html_writer::start_div('', array('id' => 'executionprogress'));
+ echo $renderer->progress_bar($restore->get_progress_bar());
+ $restore->get_controller()->set_progress(new core_backup_display_progress());
$restore->execute();
+ echo html_writer::end_div();
+ echo html_writer::script('document.getElementById("executionprogress").style.display = "none";');
} catch(Exception $e) {
$restore->cleanup();
throw $e;
$restore->save_controller();
}
}
-$heading = $course->fullname;
-
-$PAGE->set_title($heading.': '.$restore->get_stage_name());
-$PAGE->set_heading($heading);
-$PAGE->navbar->add($restore->get_stage_name());
-$renderer = $PAGE->get_renderer('core','backup');
-echo $OUTPUT->header();
-if (!$restore->is_independent() && $restore->enforce_changed_dependencies()) {
- debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER);
-}
echo $renderer->progress_bar($restore->get_progress_bar());
echo $restore->display($renderer);
$restore->destroy();
global $CFG;
$dfltloglevel = backup::LOG_WARNING; // Default logging level
- if (debugging('', DEBUG_DEVELOPER)) { // Debug developer raises default logging level
+ if ($CFG->debugdeveloper) { // Debug developer raises default logging level
$dfltloglevel = backup::LOG_DEBUG;
}
// Instantiate with debugging enabled and $CFG->backup_error_log_logger_level not set
$CFG->debugdisplay = true;
- $CFG->debug = DEBUG_DEVELOPER;
unset($CFG->backup_error_log_logger_level);
$logger1 = backup_factory::get_logger_chain(backup::INTERACTIVE_YES, backup::EXECUTION_INMEDIATE, 'test');
$this->assertTrue($logger1 instanceof error_log_logger); // 1st logger is error_log_logger
require_once($CFG->dirroot . '/backup/util/loggers/file_logger.class.php');
require_once($CFG->dirroot . '/backup/util/loggers/database_logger.class.php');
require_once($CFG->dirroot . '/backup/util/loggers/output_indented_logger.class.php');
+require_once($CFG->dirroot . '/backup/util/progress/core_backup_progress.class.php');
+require_once($CFG->dirroot . '/backup/util/progress/core_backup_null_progress.class.php');
+require_once($CFG->dirroot . '/backup/util/progress/core_backup_display_progress.class.php');
require_once($CFG->dirroot . '/backup/util/settings/setting_dependency.class.php');
require_once($CFG->dirroot . '/backup/util/settings/base_setting.class.php');
require_once($CFG->dirroot . '/backup/util/settings/backup_setting.class.php');
require_once($CFG->dirroot . '/backup/util/loggers/file_logger.class.php');
require_once($CFG->dirroot . '/backup/util/loggers/database_logger.class.php');
require_once($CFG->dirroot . '/backup/util/loggers/output_indented_logger.class.php');
+require_once($CFG->dirroot . '/backup/util/progress/core_backup_progress.class.php');
+require_once($CFG->dirroot . '/backup/util/progress/core_backup_null_progress.class.php');
+require_once($CFG->dirroot . '/backup/util/progress/core_backup_display_progress.class.php');
require_once($CFG->dirroot . '/backup/util/factories/backup_factory.class.php');
require_once($CFG->dirroot . '/backup/util/factories/restore_factory.class.php');
require_once($CFG->dirroot . '/backup/util/helper/backup_helper.class.php');
if ($this->levelcol) {
$columns[$this->levelcol] = $level;
}
- $columns[$this->messagecol] = $message; //TODO: should this be cleaned?
+ $columns[$this->messagecol] = clean_param($message, PARAM_NOTAGS);
return $this->insert_log_record($this->logtable, $columns);
}
// to preserve DB logs if the whole backup/restore transaction is
// rollback
global $DB;
- return $DB->insert_record($this->logtable, $columns, false); // Don't return inserted id
+ return $DB->insert_record($table, $columns, false); // Don't return inserted id
}
}
return $this->controller->get_logger();
}
+ /**
+ * Gets the progress reporter, which can be used to report progress within
+ * the backup or restore process.
+ *
+ * @return core_backup_progress Progress reporting object
+ */
+ public function get_progress() {
+ return $this->controller->get_progress();
+ }
+
public function is_excluding_activities() {
return $this->excludingdactivities;
}
if (!$this->built) {
throw new base_plan_exception('base_plan_not_built');
}
+
+ // Calculate the total weight of all tasks and start progress tracking.
+ $progress = $this->get_progress();
+ $totalweight = 0;
+ foreach ($this->tasks as $task) {
+ $totalweight += $task->get_weight();
+ }
+ $progress->start_progress($this->get_name(), $totalweight);
+
+ // Build and execute all tasks.
foreach ($this->tasks as $task) {
$task->build();
$task->execute();
}
+
+ // Finish progress tracking.
+ $progress->end_progress();
}
+ /**
+ * Gets the progress reporter, which can be used to report progress within
+ * the backup or restore process.
+ *
+ * @return core_backup_progress Progress reporting object
+ */
+ public abstract function get_progress();
+
/**
* Destroy all circular references. It helps PHP 5.2 a lot!
*/
return $this->settings;
}
+ /**
+ * Returns the weight of this task, an approximation of the amount of time
+ * it will take. By default this value is 1. It can be increased for longer
+ * tasks.
+ *
+ * @return int Weight
+ */
+ public function get_weight() {
+ return 1;
+ }
+
public function get_setting($name) {
// First look in task settings
$result = null;
return $this->plan->get_logger();
}
+ /**
+ * Gets the progress reporter, which can be used to report progress within
+ * the backup or restore process.
+ *
+ * @return core_backup_progress Progress reporting object
+ */
+ public function get_progress() {
+ return $this->plan->get_progress();
+ }
+
public function log($message, $level, $a = null, $depth = null, $display = false) {
backup_helper::log($message, $level, $a, $depth, $display, $this->get_logger());
}
if ($this->executed) {
throw new base_task_exception('base_task_already_executed', $this->name);
}
+
+ // Starts progress based on the weight of this task and number of steps.
+ $progress = $this->get_progress();
+ $progress->start_progress($this->get_name(), count($this->steps), $this->get_weight());
+ $done = 0;
+
+ // Execute all steps.
foreach ($this->steps as $step) {
$result = $step->execute();
// If step returns array, it will be forwarded to plan
if (is_array($result) and !empty($result)) {
$this->add_result($result);
}
+ $done++;
+ $progress->progress($done);
}
// Mark as executed if any step has been executed
if (!empty($this->steps)) {
$this->executed = true;
}
+
+ // Finish progress for this task.
+ $progress->end_progress();
}
/**
return $this->controller->get_logger();
}
+ /**
+ * Gets the progress reporter, which can be used to report progress within
+ * the backup or restore process.
+ *
+ * @return core_backup_progress Progress reporting object
+ */
+ public function get_progress() {
+ return $this->controller->get_progress();
+ }
+
public function get_info() {
return $this->controller->get_info();
}
parent::execute();
$this->controller->set_status(backup::STATUS_FINISHED_OK);
- events_trigger('course_restored', (object) array(
- 'courseid' => $this->get_courseid(), // The new course
- 'userid' => $this->get_userid(), // User doing the restore
- 'type' => $this->controller->get_type(), // backup::TYPE_* constant
- 'target' => $this->controller->get_target(), // backup::TARGET_* constant
- 'mode' => $this->controller->get_mode(), // backup::MODE_* constant
- 'operation' => $this->controller->get_operation(), // backup::OPERATION_* constant
- 'samesite' => $this->controller->is_samesite(),
+ // Trigger a course restored event.
+ $event = \core\event\course_restored::create(array(
+ 'objectid' => $this->get_courseid(),
+ 'userid' => $this->get_userid(),
+ 'context' => context_course::instance($this->get_courseid()),
+ 'other' => array('type' => $this->controller->get_type(),
+ 'target' => $this->controller->get_target(),
+ 'mode' => $this->controller->get_mode(),
+ 'operation' => $this->controller->get_operation(),
+ 'samesite' => $this->controller->is_samesite())
));
+ $event->trigger();
}
/**
class mock_base_plan extends base_plan {
public function build() {
}
+
+ public function get_progress() {
+ return null;
+ }
}
/**
--- /dev/null
+<?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/>.
+
+/**
+ * Progress handler that uses a standard Moodle progress bar to display
+ * progress. The Moodle progress bar cannot show indeterminate progress,
+ * so we do extra output in addition to the bar.
+ *
+ * @package core_backup
+ * @copyright 2013 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class core_backup_display_progress extends core_backup_progress {
+ /**
+ * @var int Number of wibble states (state0...stateN-1 classes in CSS)
+ */
+ const WIBBLE_STATES = 13;
+
+ /**
+ * @var progress_bar Current progress bar.
+ */
+ private $bar;
+
+ private $lastwibble, $currentstate = 0, $direction = 1;
+
+ /**
+ * @var bool True to display names
+ */
+ protected $displaynames = false;
+
+ /**
+ * Constructs the progress reporter. This will output HTML code for the
+ * progress bar, and an indeterminate wibbler below it.
+ *
+ * @param bool $startnow If true, outputs HTML immediately.
+ */
+ public function __construct($startnow = true) {
+ if ($startnow) {
+ $this->start_html();
+ }
+ }
+
+ /**
+ * By default, the progress section names do not display because (in backup)
+ * these are usually untranslated and incomprehensible. To make them
+ * display, call this method.
+ *
+ * @param bool $displaynames True to display names
+ */
+ public function set_display_names($displaynames = true) {
+ $this->displaynames = $displaynames;
+ }
+
+ /**
+ * Starts to output progress.
+ *
+ * Called in constructor and in update_progress if required.
+ *
+ * @throws coding_exception If already started
+ */
+ public function start_html() {
+ if ($this->bar) {
+ throw new coding_exception('Already started');
+ }
+ $this->bar = new progress_bar();
+ $this->bar->create();
+ echo html_writer::start_div('wibbler');
+ }
+
+ /**
+ * Finishes output. (Progress can begin again later if there are more
+ * calls to update_progress.)
+ *
+ * Automatically called from update_progress when progress finishes.
+ */
+ public function end_html() {
+ // Finish progress bar.
+ $this->bar->update_full(100, '');
+ $this->bar = null;
+
+ // End wibbler div.
+ echo html_writer::end_div();
+ }
+
+ public function update_progress() {
+ // If finished...
+ if (!$this->is_in_progress_section()) {
+ if ($this->bar) {
+ $this->end_html();
+ }
+ } else {
+ if (!$this->bar) {
+ $this->start_html();
+ }
+ // In case of indeterminate or small progress, update the wibbler
+ // (up to once per second).
+ if (time() != $this->lastwibble) {
+ $this->lastwibble = time();
+ echo html_writer::div('', 'wibble state' . $this->currentstate);
+
+ // Go on to next colour.
+ $this->currentstate += $this->direction;
+ if ($this->currentstate < 0 || $this->currentstate >= self::WIBBLE_STATES) {
+ $this->direction = -$this->direction;
+ $this->currentstate += 2 * $this->direction;
+ }
+ }
+
+ // Get progress.
+ list ($min, $max) = $this->get_progress_proportion_range();
+
+ // Update progress bar.
+ $message = '';
+ if ($this->displaynames) {
+ $message = $this->get_current_description();
+ }
+ $this->bar->update_full($min * 100, $message);
+
+ // Flush output.
+ flush();
+ }
+ }
+}
--- /dev/null
+<?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/>.
+
+/**
+ * Progress handler that ignores progress entirely.
+ *
+ * @package core_backup
+ * @copyright 2013 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class core_backup_null_progress extends core_backup_progress {
+ public function update_progress() {
+ // Do nothing.
+ }
+}
--- /dev/null
+<?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/>.
+
+/**
+ * Base class for handling progress information during a backup and restore.
+ *
+ * Subclasses should generally override the current_progress function which
+ * summarises all progress information.
+ *
+ * @package core_backup
+ * @copyright 2013 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+abstract class core_backup_progress {
+ /**
+ * @var int Constant indicating that the number of progress calls is unknown.
+ */
+ const INDETERMINATE = -1;
+
+ /**
+ * @var int The number of seconds that can pass without progress() calls.
+ */
+ const TIME_LIMIT_WITHOUT_PROGRESS = 120;
+
+ /**
+ * @var int Time of last progress call.
+ */
+ protected $lastprogresstime;
+
+ /**
+ * @var int Number of progress calls (restricted to ~ 1/second).
+ */
+ protected $count;
+
+ /**
+ * @var array Array of progress descriptions for each stack level.
+ */
+ protected $descriptions = array();
+
+ /**
+ * @var array Array of maximum progress values for each stack level.
+ */
+ protected $maxes = array();
+
+ /**
+ * @var array Array of current progress values.
+ */
+ protected $currents = array();
+
+ /**
+ * @var int Array of counts within parent progress entry (ignored for first)
+ */
+ protected $parentcounts = array();
+
+ /**
+ * Marks the start of an operation that will display progress.
+ *
+ * This can be called multiple times for nested progress sections. It must
+ * be paired with calls to end_progress.
+ *
+ * The progress maximum may be INDETERMINATE if the current operation has
+ * an unknown number of steps. (This is default.)
+ *
+ * Calling this function will always result in a new display, so this
+ * should not be called exceedingly frequently.
+ *
+ * When it is complete by calling end_progress, each start_progress section
+ * automatically adds progress to its parent, as defined by $parentcount.
+ *
+ * @param string $description Description to display
+ * @param int $max Maximum value of progress for this section
+ * @param int $parentcount How many progress points this section counts for
+ * @throws coding_exception If max is invalid
+ */
+ public function start_progress($description, $max = self::INDETERMINATE,
+ $parentcount = 1) {
+ if ($max != self::INDETERMINATE && $max <= 0) {
+ throw new coding_exception(
+ 'start_progress() max value cannot be zero or negative');
+ }
+ if ($parentcount < 1) {
+ throw new coding_exception(
+ 'start_progress() parent progress count must be at least 1');
+ }
+ if (!empty($this->descriptions)) {
+ $prevmax = end($this->maxes);
+ if ($prevmax !== self::INDETERMINATE) {
+ $prevcurrent = end($this->currents);
+ if ($prevcurrent + $parentcount > $prevmax) {
+ throw new coding_exception(
+ 'start_progress() parent progress would exceed max');
+ }
+ }
+ } else {
+ if ($parentcount != 1) {
+ throw new coding_exception(
+ 'start_progress() progress count must be 1 when no parent');
+ }
+ }
+ $this->descriptions[] = $description;
+ $this->maxes[] = $max;
+ $this->currents[] = 0;
+ $this->parentcounts[] = $parentcount;
+ $this->update_progress();
+ $lastprogresstime = $this->get_time();
+ }
+
+ /**
+ * Marks the end of an operation that will display progress.
+ *
+ * This must be paired with each start_progress call.
+ *
+ * If there is a parent progress section, its progress will be increased
+ * automatically to reflect the end of the child section.
+ *
+ * @throws coding_exception If progress hasn't been started
+ */
+ public function end_progress() {
+ if (!count($this->descriptions)) {
+ throw new coding_exception('end_progress() without start_progress()');
+ }
+ array_pop($this->descriptions);
+ array_pop($this->maxes);
+ array_pop($this->currents);
+ $parentcount = array_pop($this->parentcounts);
+ if (!empty($this->descriptions)) {
+ $lastmax = end($this->maxes);
+ if ($lastmax != self::INDETERMINATE) {
+ $lastvalue = end($this->currents);
+ $this->currents[key($this->currents)] = $lastvalue + $parentcount;
+ }
+ }
+ $this->update_progress();
+ }
+
+ /**
+ * Indicates that progress has occurred.
+ *
+ * The progress value should indicate the total progress so far, from 0
+ * to the value supplied for $max (inclusive) in start_progress.
+ *
+ * You do not need to call this function for every value. It is OK to skip
+ * values. It is also OK to call this function as often as desired; it
+ * doesn't do anything if called more than once per second.
+ *
+ * It must be INDETERMINATE if start_progress was called with $max set to
+ * INDETERMINATE. Otherwise it must not be indeterminate.
+ *
+ * @param int $progress Progress so far
+ * @throws coding_exception If progress value is invalid
+ */
+ public function progress($progress = self::INDETERMINATE) {
+ // Ignore too-frequent progress calls (more than once per second).
+ $now = $this->get_time();
+ if ($now === $this->lastprogresstime) {
+ return;
+ }
+
+ // Check we are inside a progress section.
+ $max = end($this->maxes);
+ if ($max === false) {
+ throw new coding_exception(
+ 'progress() without start_progress');
+ }
+
+ // Check and apply new progress.
+ if ($progress === self::INDETERMINATE) {
+ // Indeterminate progress.
+ if ($max !== self::INDETERMINATE) {
+ throw new coding_exception(
+ 'progress() INDETERMINATE, expecting value');
+ }
+ } else {
+ // Determinate progress.
+ $current = end($this->currents);
+ if ($max === self::INDETERMINATE) {
+ throw new coding_exception(
+ 'progress() with value, expecting INDETERMINATE');
+ } else if ($progress < 0 || $progress > $max) {
+ throw new coding_exception(
+ 'progress() value out of range');
+ } else if ($progress < $current) {
+ throw new coding_Exception(
+ 'progress() value may not go backwards');
+ }
+ $this->currents[key($this->currents)] = $progress;
+ }
+
+ // Update progress.
+ $this->count++;
+ $this->lastprogresstime = $now;
+ set_time_limit(self::TIME_LIMIT_WITHOUT_PROGRESS);
+ $this->update_progress();
+ }
+
+ /**
+ * Gets time (this is provided so that unit tests can override it).
+ *
+ * @return int Current system time
+ */
+ protected function get_time() {
+ return time();
+ }
+
+ /**
+ * Called whenever new progress should be displayed.
+ */
+ protected abstract function update_progress();
+
+ /**
+ * @return bool True if currently inside a progress section
+ */
+ public function is_in_progress_section() {
+ return !empty($this->descriptions);
+ }
+
+ /**
+ * @return string Current progress section description
+ */
+ public function get_current_description() {
+ $description = end($this->descriptions);
+ if ($description === false) {
+ throw new coding_exception('Not inside progress section');
+ }
+ return $description;
+ }
+
+ /**
+ * Obtains current progress in a way suitable for drawing a progress bar.
+ *
+ * Progress is returned as a minimum and maximum value. If there is no
+ * indeterminate progress, these values will be identical. If there is
+ * intermediate progress, these values can be different. (For example, if
+ * the top level progress sections is indeterminate, then the values will
+ * always be 0.0 and 1.0.)
+ *
+ * @return array Minimum and maximum possible progress proportions
+ */
+ public function get_progress_proportion_range() {
+ // If there is no progress underway, we must have finished.
+ if (empty($this->currents)) {
+ return array(1.0, 1.0);
+ }
+ $count = count($this->currents);
+ $min = 0.0;
+ $max = 1.0;
+ for ($i = 0; $i < $count; $i++) {
+ // Get max value at that section - if it's indeterminate we can tell
+ // no more.
+ $sectionmax = $this->maxes[$i];
+ if ($sectionmax === self::INDETERMINATE) {
+ return array($min, $max);
+ }
+
+ // Special case if current value is max (this should only happen
+ // just before ending a section).
+ $sectioncurrent = $this->currents[$i];
+ if ($sectioncurrent === $sectionmax) {
+ return array($max, $max);
+ }
+
+ // Using the current value at that section, we know we are somewhere
+ // between 'current' and the next 'current' value which depends on
+ // the parentcount of the nested section (if any).
+ $newmin = ($sectioncurrent / $sectionmax) * ($max - $min) + $min;
+ $nextcurrent = $sectioncurrent + 1;
+ if ($i + 1 < $count) {
+ $weight = $this->parentcounts[$i + 1];
+ $nextcurrent = $sectioncurrent + $weight;
+ }
+ $newmax = ($nextcurrent / $sectionmax) * ($max - $min) + $min;
+ $min = $newmin;
+ $max = $newmax;
+ }
+
+ // If there was nothing indeterminate, we use the min value as current.
+ return array($min, $min);
+ }
+
+ /**
+ * Obtains current indeterminate progress in a way suitable for adding to
+ * the progress display.
+ *
+ * This returns the number of indeterminate calls (at any level) during the
+ * lifetime of this progress reporter, whether or not there is a current
+ * indeterminate step. (The number will not be ridiculously high because
+ * progress calls are limited to one per second.)
+ *
+ * @return int Number of indeterminate progress calls
+ */
+ public function get_progress_count() {
+ return $this->count;
+ }
+}
--- /dev/null
+<?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/>.
+
+/**
+ * Unit tests for the progress classes.
+ *
+ * @package core_backup
+ * @category phpunit
+ * @copyright 2013 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+// Include all the needed stuff.
+global $CFG;
+require_once($CFG->dirroot . '/backup/util/progress/core_backup_progress.class.php');
+
+/**
+ * Progress tests.
+ */
+class backup_progress_testcase extends basic_testcase {
+
+ /**
+ * Tests for basic use with simple numeric progress.
+ */
+ public function test_basic() {
+ $progress = new core_backup_mock_progress();
+
+ // Check values of empty progress things.
+ $this->assertFalse($progress->is_in_progress_section());
+
+ // Start progress counting, check basic values and check that update
+ // gets called.
+ $progress->start_progress('hello', 10);
+ $this->assertTrue($progress->was_update_called());
+ $this->assertTrue($progress->is_in_progress_section());
+ $this->assertEquals('hello', $progress->get_current_description());
+
+ // Check numeric position and indeterminate count.
+ $this->assert_min_max(0.0, 0.0, $progress);
+ $this->assertEquals(0, $progress->get_progress_count());
+
+ // Make some progress and check that the time limit gets added.
+ $progress->step_time();
+ $progress->progress(2);
+ $this->assertTrue($progress->was_update_called());
+ $this->assertEquals(120, ini_get('max_execution_time'));
+
+ // Check the new value.
+ $this->assert_min_max(0.2, 0.2, $progress);
+
+ // Do another progress run at same time, it should be ignored.
+ $progress->progress(3);
+ $this->assertFalse($progress->was_update_called());
+ $this->assert_min_max(0.2, 0.2, $progress);
+
+ // End the section. This should cause an update.
+ $progress->end_progress();
+ $this->assertTrue($progress->was_update_called());
+
+ // Because there are no sections left open, it thinks we finished.
+ $this->assert_min_max(1.0, 1.0, $progress);
+
+ // There was 1 progress call.
+ $this->assertEquals(1, $progress->get_progress_count());
+
+ // Clear the time limit, otherwise phpunit complains.
+ set_time_limit(0);
+ }
+
+ /**
+ * Tests progress that is nested and/or indeterminate.
+ */
+ public function test_nested() {
+ // Outer progress goes from 0 to 10.
+ $progress = new core_backup_mock_progress();
+ $progress->start_progress('hello', 10);
+
+ // Get up to 4, check position.
+ $progress->step_time();
+ $progress->progress(4);
+ $this->assert_min_max(0.4, 0.4, $progress);
+ $this->assertEquals('hello', $progress->get_current_description());
+
+ // Now start indeterminate progress.
+ $progress->start_progress('world');
+ $this->assert_min_max(0.4, 0.5, $progress);
+ $this->assertEquals('world', $progress->get_current_description());
+
+ // Do some indeterminate progress and count it (once per second).
+ $progress->step_time();
+ $progress->progress();
+ $this->assertEquals(2, $progress->get_progress_count());
+ $progress->progress();
+ $this->assertEquals(2, $progress->get_progress_count());
+ $progress->step_time();
+ $progress->progress();
+ $this->assertEquals(3, $progress->get_progress_count());
+ $this->assert_min_max(0.4, 0.5, $progress);
+
+ // Exit the indeterminate section.
+ $progress->end_progress();
+ $this->assert_min_max(0.5, 0.5, $progress);
+
+ $progress->step_time();
+ $progress->progress(7);
+ $this->assert_min_max(0.7, 0.7, $progress);
+
+ // Enter a numbered section (this time with a range of 5).
+ $progress->start_progress('frogs', 5);
+ $this->assert_min_max(0.7, 0.7, $progress);
+ $progress->step_time();
+ $progress->progress(1);
+ $this->assert_min_max(0.72, 0.72, $progress);
+ $progress->step_time();
+ $progress->progress(3);
+ $this->assert_min_max(0.76, 0.76, $progress);
+
+ // Now enter another indeterminate section.
+ $progress->start_progress('and');
+ $this->assert_min_max(0.76, 0.78, $progress);
+
+ // Make some progress, should increment indeterminate count.
+ $progress->step_time();
+ $progress->progress();
+ $this->assertEquals(7, $progress->get_progress_count());
+
+ // Enter numbered section, won't make any difference to values.
+ $progress->start_progress('zombies', 2);
+ $progress->step_time();
+ $progress->progress(1);
+ $this->assert_min_max(0.76, 0.78, $progress);
+ $this->assertEquals(8, $progress->get_progress_count());
+
+ // Leaving it will make no difference too.
+ $progress->end_progress();
+
+ // Leaving the indeterminate section will though.
+ $progress->end_progress();
+ $this->assert_min_max(0.78, 0.78, $progress);
+
+ // Leave the two numbered sections.
+ $progress->end_progress();
+ $this->assert_min_max(0.8, 0.8, $progress);
+ $progress->end_progress();
+ $this->assertFalse($progress->is_in_progress_section());
+
+ set_time_limit(0);
+ }
+
+ /**
+ * Tests&n