MDL-20636 Backup and restore of Opaque questions.

This commit is contained in:
Tim Hunt 2011-03-02 15:48:43 +00:00
parent 515e6b97b5
commit cd4b6c1932
2 changed files with 197 additions and 0 deletions

View File

@ -0,0 +1,87 @@
<?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 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Provides the information to backup ddwtos questions
*
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class backup_qtype_opaque_plugin extends backup_qtype_plugin {
/**
* Returns the qtype information to attach to question element
*/
protected function define_question_plugin_structure() {
// Define the virtual plugin element with the condition to fulfill
$plugin = $this->get_plugin_element(null, '../../qtype', 'opaque');
// Create one standard named plugin element (the visible container)
$pluginwrapper = new backup_nested_element($this->get_recommended_name());
// connect the visible container ASAP
$plugin->add_child($pluginwrapper);
// Now create the qtype own structures
$opaque = new backup_nested_element('opaque', array('id'), array(
'remoteid', 'remoteversion'));
$engine = new backup_nested_element('engine', array('id'), array('name', 'passkey'));
$server = new backup_nested_element('server', array('type'), array('url'));
// Now the own qtype tree
$pluginwrapper->add_child($opaque);
$opaque->add_child($engine);
$engine->add_child($server);
// set source to populate the data
$opaque->set_source_table('question_opaque', array('questionid' => backup::VAR_PARENTID));
$engine->set_source_sql('
SELECT e.*
FROM {question_opaque_engines} e
JOIN {question_opaque} qo ON qo.engineid = e.id
WHERE qo.id = ?',
array(backup::VAR_PARENTID));
$server->set_source_table('question_opaque_servers', array('engineid' => backup::VAR_PARENTID));
return $plugin;
}
/**
* Returns one array with filearea => mappingname elements for the qtype
*
* Used by {@link get_components_and_fileareas} to know about all the qtype
* files to be processed both in backup and restore.
*/
public static function get_qtype_fileareas() {
return array(
'correctfeedback' => 'question_created',
'partiallycorrectfeedback' => 'question_created',
'incorrectfeedback' => 'question_created');
}
}

View File

@ -0,0 +1,110 @@
<?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 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/question/type/opaque/locallib.php');
/**
* restore plugin class that provides the necessary information
* needed to restore one ddwtos qtype plugin
*
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class restore_qtype_opaque_plugin extends restore_qtype_plugin {
protected $enginemanager = null;
/* Overridden. See parent class for docs. */
protected function define_question_plugin_structure() {
return array(
new restore_path_element('opaque', $this->get_pathfor('/opaque'), true),
new restore_path_element('engine', $this->get_pathfor('/opaque/engine')),
new restore_path_element('server', $this->get_pathfor('/opaque/engine/server')),
);
}
/**
* Process the qtype/opaque element
*/
public function process_opaque($data) {
global $DB;
$engine = (object) $data['engine'][0];
$engine->questionengines = array();
$engine->questionbanks = array();
foreach ($data['engine'][0]['server'] as $server) {
if ($server['type'] == 'qe') {
$engine->questionengines[] = $server['url'];
} else if ($server['type'] == 'qb') {
$engine->questionbanks[] = $server['url'];
}
}
if (empty($engine->questionengines)) {
throw new coding_exception('Missing question engine URLs in an Opaque question backup.');
}
// Detect if the question is created or mapped
$oldquestionid = $this->get_old_parentid('question');
$newquestionid = $this->get_new_parentid('question');
$questioncreated = $this->get_mappingid('question_created', $oldquestionid) ? true : false;
// If the question has been created by restore, we need to create its question_ddwtos too
if (!$questioncreated) {
// New question, insert.
$question = (object) $data;
$question->engineid = $this->engine_manager()->find_or_create_engineid($engine);
$question->questionid = $newquestionid;
$DB->insert_record('question_opaque', $question);
}
}
/**
* Process the qtype/opaque/server element
*/
public function process_engine($data) {
// Do nothing. All the data is processed in process_opaque.
}
/**
* Process the qtype/opaque/server element
*/
public function process_server($data) {
// Do nothing. All the data is processed in process_opaque.
}
/**
* @return qtype_opaque_engine_manager an engine manager.
*/
protected function engine_manager() {
if (is_null($this->enginemanager)) {
$this->enginemanager = new qtype_opaque_engine_manager();
}
return $this->enginemanager;
}
}