MDL-81627 quiz restore: fix restore of grade item for each slot

This commit is contained in:
Tim Hunt 2024-04-23 10:53:52 +01:00
parent 7b9a4eeca5
commit 8e7bd88ce3
2 changed files with 30 additions and 17 deletions

View File

@ -443,7 +443,7 @@ class restore_quiz_activity_structure_step extends restore_questions_activity_st
}
if (isset($data->quizgradeitemid)) {
$data->quizgradeitemid = $this->get_mappingid('quiz_grade_items', $data->quizgradeitemid);
$data->quizgradeitemid = $this->get_mappingid('quiz_grade_item', $data->quizgradeitemid);
}
$data->quizid = $this->get_new_parentid('quiz');

View File

@ -16,6 +16,7 @@
namespace mod_quiz;
use core_question_generator;
use mod_quiz_generator;
use restore_date_testcase;
@ -39,39 +40,51 @@ final class restore_quiz_grade_items_test extends restore_date_testcase {
global $DB;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$generator = $this->getDataGenerator();
/** @var mod_quiz_generator $quizgen */
$quizgen = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
/** @var core_question_generator $questiongenerator */
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
// Create a quiz with two grade items.
$course = $generator->create_course();
$quiz = $quizgen->create_instance(['course' => $course->id]);
$listeninggrade = $quizgen->create_grade_item(['quizid' => $quiz->id, 'name' => 'Listening']);
$readinggrade = $quizgen->create_grade_item(['quizid' => $quiz->id, 'name' => 'Reading']);
$quizgen->create_grade_item(['quizid' => $quiz->id, 'name' => 'Listening']);
$quizgen->create_grade_item(['quizid' => $quiz->id, 'name' => 'Reading']);
// Add two questions to the quiz.
$cat = $questiongenerator->create_question_category();
$saq1 = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
$saq2 = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
quiz_add_quiz_question($saq1->id, $quiz, 0, 1);
quiz_add_quiz_question($saq2->id, $quiz, 0, 1);
// Back up and restore including group info and user info.
// Set one of the question to use a grade item.
$quizobj = quiz_settings::create($quiz->id);
$structure = $quizobj->get_structure();
$structure->update_slot_grade_item($structure->get_slot_by_number(2), $readinggrade->id);
$quizobj->get_grade_calculator()->recompute_quiz_sumgrades();
// Back up and restore the course.
$newcourseid = $this->backup_and_restore($course);
// Verify the grade items were copied over.
$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'],
(object) ['id' => reset($quizgradeitems)->id, 'quizid' => $newquiz->id, 'sortorder' => 1, 'name' => 'Listening'],
(object) ['id' => end($quizgradeitems)->id, 'quizid' => $newquiz->id, 'sortorder' => 2, 'name' => 'Reading'],
],
$quizgradeitems
array_values($quizgradeitems),
);
// Verify that each slot uses the right grade item.
$this->assertNull($structure->get_slot_by_number(1)->quizgradeitemid);
$this->assertEquals(end($quizgradeitems)->id, $structure->get_slot_by_number(2)->quizgradeitemid);
}
}