Skip to content

Commit

Permalink
MDL-22151 & MDL-22138 - Quiz activity backup & questions banks too!
Browse files Browse the repository at this point in the history
CV S: ----------------------------------------------------------------------
  • Loading branch information
stronk7 committed Sep 21, 2010
1 parent 1f3fec6 commit 767cb7f
Show file tree
Hide file tree
Showing 18 changed files with 804 additions and 24 deletions.
4 changes: 2 additions & 2 deletions backup/backup.class.php
Expand Up @@ -106,8 +106,8 @@ abstract class backup implements checksumable {
const OPERATION_RESTORE ='restore';// We are performing one restore

// Version (to keep CFG->backup_version (and release) updated automatically)
const VERSION = 2010072300;
const RELEASE = '2.0 Preview 5';
const VERSION = 2010092100;
const RELEASE = '2.0 RC1';
}

/*
Expand Down
3 changes: 3 additions & 0 deletions backup/moodle2/backup_course_task.class.php
Expand Up @@ -78,6 +78,9 @@ public function build() {
// Annotate the groups used in already annotated groupings
$this->add_step(new backup_annotate_groups_from_groupings('annotate_groups'));

// Annotate the question_categories belonging to the course context
$this->add_step(new backup_calculate_question_categories('course_question_categories'));

// Generate the roles file (optionally role assignments and always role overrides)
$this->add_step(new backup_roles_structure_step('course_roles', 'roles.xml'));

Expand Down
6 changes: 6 additions & 0 deletions backup/moodle2/backup_custom_fields.php
Expand Up @@ -102,6 +102,12 @@ public function fill_values($values) {
}
}

/**
* Implementation of backup_optigroup_element to be used by plugins stuff.
* Split just for better separation and future specialisation
*/
class backup_plugin_element extends backup_optigroup_element { }

/**
* Implementation of backup_optigroup_element to be used by subplugins stuff.
* Split just for better separation and future specialisation
Expand Down
10 changes: 10 additions & 0 deletions backup/moodle2/backup_final_task.class.php
Expand Up @@ -48,6 +48,16 @@ public function build() {
// including membership based on setting
$this->add_step(new backup_groups_structure_step('groups', 'groups.xml'));

// Annotate all the question files for the already annotated question
// categories (this is performed here and not in the structure step because
// it involves multiple contexts and as far as we are always backup-ing
// complete question banks we don't need to restrict at all and can be
// done in a single pass
$this->add_step(new backup_annotate_all_question_files('question_files'));

// Generate the questions file with the final annotated question_categories
$this->add_step(new backup_questions_structure_step('questions', 'questions.xml'));

// Annotate all the user files (conditionally) (private, profile and icon files)
// Because each user has its own context, we need a separate/specialised step here
// This step also ensures that the contexts for all the users exist, so next
Expand Down
2 changes: 2 additions & 0 deletions backup/moodle2/backup_plan_builder.class.php
Expand Up @@ -32,6 +32,8 @@
require_once($CFG->dirroot . '/backup/moodle2/backup_block_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_default_block_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_xml_transformer.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_subplugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_settingslib.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_stepslib.php');
Expand Down
76 changes: 76 additions & 0 deletions backup/moodle2/backup_plugin.class.php
@@ -0,0 +1,76 @@
<?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/>.

/**
* @package moodlecore
* @subpackage backup-moodle2
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* Class implementing the plugins support for moodle2 backups
*
* TODO: Finish phpdocs
*/
abstract class backup_plugin {

protected $plugintype;
protected $pluginname;
protected $connectionpoint;
protected $optigroup; // Optigroup, parent of all optigroup elements

public function __construct($plugintype, $pluginname, $optigroup) {
$this->plugintype = $plugintype;
$this->pluginname = $pluginname;
$this->optigroup = $optigroup;
$this->connectionpoint = '';
}

public function define_plugin_structure($connectionpoint) {

$this->connectionpoint = $connectionpoint;

$methodname = 'define_' . $connectionpoint . '_plugin_structure';

if (method_exists($this, $methodname)) {
$this->$methodname();
}
}

/**
* Factory method that will return one backup_plugin_element (backup_optigroup_element)
* with its name automatically calculated, based one the plugin being handled (type, name)
*/
protected function get_plugin_element($final_elements = null, $conditionparam = null, $conditionvalue = null) {
// Something exclusive for this backup_plugin_element (backup_optigroup_element)
// because it hasn't XML representation
$name = 'optigroup_' . $this->plugintype . '_' . $this->pluginname . '_' . $this->connectionpoint;
$optigroup_element = new backup_plugin_element($name, $final_elements, $conditionparam, $conditionvalue);
$this->optigroup->add_child($optigroup_element); // Add optigroup_element to stay connected since beginning
return $optigroup_element;
}

/**
* Simple helper function that suggests one name for the main nested element in plugins
* It's not mandatory to use it but recommended ;-)
*/
protected function get_recommended_name() {
return 'plugin_' . $this->plugintype . '_' . $this->pluginname . '_' . $this->connectionpoint;
}

}
158 changes: 158 additions & 0 deletions backup/moodle2/backup_qtype_plugin.class.php
@@ -0,0 +1,158 @@
<?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/>.

/**
* @package moodlecore
* @subpackage backup-moodle2
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* Class extending standard backup_plugin in order to implement some
* helper methods related with the questions (qtype plugin)
*
* TODO: Finish phpdocs
*/
abstract class backup_qtype_plugin extends backup_plugin {

/**
* Attach to $element (usually questions) the needed backup structures
* for question_answers for a given question
* Used by various qtypes (calculated, essay, multianswer,
* multichoice, numerical, shortanswer, truefalse)
*/
protected function add_question_question_answers($element) {
// Check $element is one nested_backup_element
if (! $element instanceof backup_nested_element) {
throw new backup_step_exception('question_answers_bad_parent_element', $element);
}

// Define the elements
$answers = new backup_nested_element('answers');
$answer = new backup_nested_element('answer', array('id'), array(
'answertext', 'answerformat', 'fraction', 'feedback',
'feedbackformat'));

// Build the tree
$element->add_child($answers);
$answers->add_child($answer);

// Set the sources
$answer->set_source_table('question_answers', array('question' => backup::VAR_PARENTID));

// Aliases
$answer->set_source_alias('answer', 'answertext');

// don't need to annotate ids nor files
}

/**
* Attach to $element (usually questions) the needed backup structures
* for question_numerical_units for a given question
* Used both by calculated and numerical qtypes
*/
protected function add_question_numerical_units($element) {
// Check $element is one nested_backup_element
if (! $element instanceof backup_nested_element) {
throw new backup_step_exception('question_numerical_units_bad_parent_element', $element);
}

// Define the elements
$units = new backup_nested_element('numerical_units');
$unit = new backup_nested_element('numerical_unit', array('id'), array(
'multiplier', 'unit'));

// Build the tree
$element->add_child($units);
$units->add_child($unit);

// Set the sources
$unit->set_source_table('question_numerical_units', array('question' => backup::VAR_PARENTID));

// don't need to annotate ids nor files
}

/**
* Attach to $element (usually questions) the needed backup structures
* for question_numerical_options for a given question
* Used both by calculated and numerical qtypes
*/
protected function add_question_numerical_options($element) {
// Check $element is one nested_backup_element
if (! $element instanceof backup_nested_element) {
throw new backup_step_exception('question_numerical_options_bad_parent_element', $element);
}

// Define the elements
$options = new backup_nested_element('numerical_options');
$option = new backup_nested_element('numerical_option', array('id'), array(
'instructions', 'instructionsformat', 'showunits', 'unitsleft',
'unitgradingtype', 'unitpenalty'));

// Build the tree
$element->add_child($options);
$options->add_child($option);

// Set the sources
$option->set_source_table('question_numerical_options', array('question' => backup::VAR_PARENTID));

// don't need to annotate ids nor files
}

/**
* Attach to $element (usually questions) the needed backup structures
* for question_datasets for a given question
* Used by calculated qtypes
*/
protected function add_question_datasets($element) {
// Check $element is one nested_backup_element
if (! $element instanceof backup_nested_element) {
throw new backup_step_exception('question_datasets_bad_parent_element', $element);
}

// Define the elements
$definitions = new backup_nested_element('dataset_definitions');
$definition = new backup_nested_element('dataset_definition', array('id'), array(
'category', 'name', 'type', 'options',
'itemcount'));

$items = new backup_nested_element('dataset_items');
$item = new backup_nested_element('dataset_item', array('id'), array(
'number', 'value'));

// Build the tree
$element->add_child($definitions);
$definitions->add_child($definition);

$definition->add_child($items);
$items->add_child($item);

// Set the sources
$definition->set_source_sql('SELECT *
FROM {question_dataset_definitions} qdd
JOIN {question_datasets} qd ON qd.datasetdefinition = qdd.id
WHERE qd.question = ?', array(backup::VAR_PARENTID));

$item->set_source_table('question_dataset_items', array('definition' => backup::VAR_PARENTID));

// Aliases
$item->set_source_alias('itemnumber', 'number');

// don't need to annotate ids nor files
}
}

0 comments on commit 767cb7f

Please sign in to comment.