MDL-74610 quiz multiple grades: backup and restore of the new data

This commit is contained in:
Tim Hunt 2023-08-08 16:03:57 +01:00
parent 857681092a
commit e499098838
3 changed files with 108 additions and 2 deletions

View File

@ -46,10 +46,14 @@ class backup_quiz_activity_structure_step extends backup_questions_activity_stru
// Define elements for access rule subplugin settings.
$this->add_subplugin_structure('quizaccess', $quiz, true);
$quizgradeitems = new backup_nested_element('quiz_grade_items');
$quizgradeitem = new backup_nested_element('quiz_grade_item', ['id'], ['sortorder', 'name']);
$qinstances = new backup_nested_element('question_instances');
$qinstance = new backup_nested_element('question_instance', ['id'],
['quizid', 'slot', 'page', 'displaynumber', 'requireprevious', 'maxmark']);
['quizid', 'slot', 'page', 'displaynumber', 'requireprevious', 'maxmark', 'quizgradeitemid']);
$this->add_question_references($qinstance, 'mod_quiz', 'slot');
@ -88,6 +92,9 @@ class backup_quiz_activity_structure_step extends backup_questions_activity_stru
$this->add_subplugin_structure('quizaccess', $attempt, true);
// Build the tree.
$quiz->add_child($quizgradeitems);
$quizgradeitems->add_child($quizgradeitem);
$quiz->add_child($qinstances);
$qinstances->add_child($qinstance);
@ -109,6 +116,8 @@ class backup_quiz_activity_structure_step extends backup_questions_activity_stru
// Define sources.
$quiz->set_source_table('quiz', ['id' => backup::VAR_ACTIVITYID]);
$quizgradeitem->set_source_table('quiz_grade_items', ['quizid' => backup::VAR_PARENTID]);
$qinstance->set_source_table('quiz_slots', ['quizid' => backup::VAR_PARENTID]);
$section->set_source_table('quiz_sections', ['quizid' => backup::VAR_PARENTID]);

View File

@ -61,6 +61,7 @@ class restore_quiz_activity_structure_step extends restore_questions_activity_st
// A chance for access subplugings to set up their quiz data.
$this->add_subplugin_structure('quizaccess', $quiz);
$paths[] = new restore_path_element('quiz_grade_item', '/activity/quiz/quiz_grade_items/quiz_grade_item');
$quizquestioninstance = new restore_path_element('quiz_question_instance',
'/activity/quiz/question_instances/question_instance');
$paths[] = $quizquestioninstance;
@ -324,6 +325,21 @@ class restore_quiz_activity_structure_step extends restore_questions_activity_st
}
}
/**
* Process a quiz grade items.
*
* @param stdClass|array $data
*/
protected function process_quiz_grade_item($data): void {
global $DB;
$data = (object) $data;
$data->quizid = $this->get_new_parentid('quiz');
$oldid = $data->id;
$newitemid = $DB->insert_record('quiz_grade_items', $data);
$this->set_mapping('quiz_grade_item', $oldid, $newitemid, true);
}
/**
* Process the data for pre 4.0 quiz data where the question_references and question_set_references table introduced.
*
@ -382,7 +398,7 @@ class restore_quiz_activity_structure_step extends restore_questions_activity_st
* @param stdClass|array $data
*/
protected function process_quiz_question_instance($data) {
global $CFG, $DB;
global $DB;
$data = (object)$data;
$oldid = $data->id;
@ -421,6 +437,10 @@ class restore_quiz_activity_structure_step extends restore_questions_activity_st
return;
}
if (isset($data->quizgradeitemid)) {
$data->quizgradeitemid = $this->get_mappingid('quiz_grade_items', $data->quizgradeitemid);
}
$data->quizid = $this->get_new_parentid('quiz');
$newitemid = $DB->insert_record('quiz_slots', $data);

View File

@ -0,0 +1,77 @@
<?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/>.
namespace mod_quiz;
use mod_quiz_generator;
use restore_date_testcase;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . "/phpunit/classes/restore_date_testcase.php");
/**
* Test of backup and restore of quiz grade items.
*
* @package mod_quiz
* @copyright 2023 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \backup_quiz_activity_structure_step
* @covers \restore_quiz_activity_structure_step
*/
final class restore_quiz_grade_items_test extends restore_date_testcase {
public function test_restore_quiz_grade_items(): void {
global $DB;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
/** @var mod_quiz_generator $quizgen */
$quizgen = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
$quiz = $quizgen->create_instance(['course' => $course->id]);
$quizgen->create_grade_item(['quizid' => $quiz->id, 'name' => 'Listening']);
$quizgen->create_grade_item(['quizid' => $quiz->id, 'name' => 'Reading']);
// Back up and restore including group info and user info.
$newcourseid = $this->backup_and_restore($course);
$newquiz = $DB->get_record('quiz', ['course' => $newcourseid]);
$quizobj = quiz_settings::create($newquiz->id);
$structure = $quizobj->get_structure();
$quizgradeitems = array_values($structure->get_grade_items());
// Strip out the ids, since we don't care what those will be.
$quizgradeitems = array_map(
function ($quizgradeitem) {
unset($quizgradeitem->id);
return $quizgradeitem;
},
$quizgradeitems
);
// Check the grade items are right in the restored quiz.
$this->assertEquals(
[
(object) ['quizid' => $newquiz->id, 'sortorder' => 1, 'name' => 'Listening'],
(object) ['quizid' => $newquiz->id, 'sortorder' => 2, 'name' => 'Reading'],
],
$quizgradeitems
);
}
}