first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,155 @@
<?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\backup;
use advanced_testcase;
use backup_controller;
use restore_controller;
use quiz_question_helper_test_trait;
use backup;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
require_once($CFG->dirroot . '/question/engine/lib.php');
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
require_once($CFG->dirroot . '/course/lib.php');
require_once($CFG->dirroot . '/mod/quiz/tests/quiz_question_helper_test_trait.php');
/**
* Test repeatedly restoring a quiz into another course.
*
* @package mod_quiz
* @category test
* @copyright Julien Rädler
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \restore_questions_parser_processor
* @covers \restore_create_categories_and_questions
*/
final class repeated_restore_test extends advanced_testcase {
use quiz_question_helper_test_trait;
/**
* Restore a quiz twice into the same target course, and verify the quiz uses the restored questions both times.
*/
public function test_restore_quiz_into_other_course_twice(): void {
global $USER;
$this->resetAfterTest();
$this->setAdminUser();
// Step 1: Create two courses and a user with editing teacher capabilities.
$generator = $this->getDataGenerator();
$course1 = $generator->create_course();
$course2 = $generator->create_course();
$teacher = $USER;
$generator->enrol_user($teacher->id, $course1->id, 'editingteacher');
$generator->enrol_user($teacher->id, $course2->id, 'editingteacher');
// Create a quiz with questions in the first course.
$quiz = $this->create_test_quiz($course1);
$coursecontext = \context_course::instance($course1->id);
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
// Create a question category.
$cat = $questiongenerator->create_question_category(['contextid' => $coursecontext->id]);
// Create a short answer question.
$saq = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
// Update the question to simulate editing.
$questiongenerator->update_question($saq);
// Add question to quiz.
quiz_add_quiz_question($saq->id, $quiz);
// Create a numerical question.
$numq = $questiongenerator->create_question('numerical', null, ['category' => $cat->id]);
// Update the question to simulate multiple versions.
$questiongenerator->update_question($numq);
$questiongenerator->update_question($numq);
// Add question to quiz.
quiz_add_quiz_question($numq->id, $quiz);
// Create a true false question.
$tfq = $questiongenerator->create_question('truefalse', null, ['category' => $cat->id]);
// Update the question to simulate multiple versions.
$questiongenerator->update_question($tfq);
$questiongenerator->update_question($tfq);
// Add question to quiz.
quiz_add_quiz_question($tfq->id, $quiz);
// Capture original question IDs for verification after import.
$modules1 = get_fast_modinfo($course1->id)->get_instances_of('quiz');
$module1 = reset($modules1);
$questionscourse1 = \mod_quiz\question\bank\qbank_helper::get_question_structure(
$module1->instance, $module1->context);
$originalquestionids = [];
foreach ($questionscourse1 as $slot) {
array_push($originalquestionids, intval($slot->questionid));
}
// Step 2: Backup the first course.
$bc = new backup_controller(backup::TYPE_1COURSE, $course1->id, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_IMPORT, $teacher->id);
$backupid = $bc->get_backupid();
$bc->execute_plan();
$bc->destroy();
// Step 3: Import the backup into the second course.
$rc = new restore_controller($backupid, $course2->id, backup::INTERACTIVE_NO, backup::MODE_IMPORT,
$teacher->id, backup::TARGET_CURRENT_ADDING);
$rc->execute_precheck();
$rc->execute_plan();
$rc->destroy();
// Verify the question ids from the quiz in the original course are different
// from the question ids in the duplicated quiz in the second course.
$modules2 = get_fast_modinfo($course2->id)->get_instances_of('quiz');
$module2 = reset($modules2);
$questionscourse2firstimport = \mod_quiz\question\bank\qbank_helper::get_question_structure(
$module2->instance, $module2->context);
foreach ($questionscourse2firstimport as $slot) {
$this->assertNotContains(intval($slot->questionid), $originalquestionids,
"Question ID $slot->questionid should not be in the original course's question IDs.");
}
// Repeat the backup and import process to simulate a second import.
$bc = new backup_controller(backup::TYPE_1COURSE, $course1->id, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_IMPORT, $teacher->id);
$backupid = $bc->get_backupid();
$bc->execute_plan();
$bc->destroy();
$rc = new restore_controller($backupid, $course2->id, backup::INTERACTIVE_NO, backup::MODE_IMPORT,
$teacher->id, backup::TARGET_CURRENT_ADDING);
$rc->execute_precheck();
$rc->execute_plan();
$rc->destroy();
// Verify that the second restore has used the same new questions that were created by the first restore.
$modules3 = get_fast_modinfo($course2->id)->get_instances_of('quiz');
$module3 = end($modules3);
$questionscourse2secondimport = \mod_quiz\question\bank\qbank_helper::get_question_structure(
$module3->instance, $module3->context);
foreach ($questionscourse2secondimport as $slot) {
$this->assertEquals($questionscourse2firstimport[$slot->slot]->questionid, $slot->questionid);
}
}
}
+83
View File
@@ -0,0 +1,83 @@
<?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\backup;
use advanced_testcase;
use backup;
use restore_controller;
/**
* Test restoring 3.9 backups including random questions.
*
* @package mod_quiz
* @copyright 2024 Tomo Tsuyuki <tomotsuyuki@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \restore_move_module_questions_categories
*/
final class restore_39_test extends advanced_testcase {
public function test_restore_random_question_39(): void {
global $DB, $CFG, $USER;
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
$this->resetAfterTest();
$this->setAdminUser();
// The example Moodle 3.9 backup file used in this test is an activity-level backup of a quiz.
// So, the backup contains just the quiz-level question bank which contains:
// | - Question category: Top
// | - Question category: Default for Test MDL-78902 quiz
// | - Question: Test MDL-78902 T/F question
// | - Question: Random (Default for Test MDL-78902 quiz)
// The quiz itself contains 1 question, the random question.
// So, during the restore, the quiz_slot needs to be updated to use a question_set_reference.
$backupfile = 'moodle_39_quiz_with_random_question_from_mod_context';
// Extract backup file.
$backupid = $backupfile;
$backuppath = make_backup_temp_directory($backupid);
get_file_packer('application/vnd.moodle.backup')->extract_to_pathname(
__DIR__ . "/../fixtures/$backupfile.mbz", $backuppath);
// Restore the quiz activity in the backup from Moodle 3.9 to a new course.
$coursecat = self::getDataGenerator()->create_category();
$course = self::getDataGenerator()->create_course(['category' => $coursecat->id]);
$rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_NO,
backup::MODE_GENERAL, $USER->id, backup::TARGET_EXISTING_ADDING);
$this->assertTrue($rc->execute_precheck());
$rc->execute_plan();
$rc->destroy();
// Get information about the quiz activity and confirm the references are correct.
$modinfo = get_fast_modinfo($course->id);
$quizzes = array_values($modinfo->get_instances_of('quiz'));
// Get contextid for the restored quiz activity.
$contextid = $quizzes[0]->context->id;
$qcats = $DB->get_records('question_categories', ['contextid' => $contextid], 'parent');
// Confirm there are 2 question categories for the restored quiz activity.
$this->assertEquals(['top', 'Default for Test MDL-78902 quiz'], array_column($qcats, 'name'));
// Get question_set_references records for the restored quiz activity.
$references = $DB->get_records('question_set_references', ['usingcontextid' => $contextid]);
foreach ($references as $reference) {
$filtercondition = json_decode($reference->filtercondition);
// Confirm the questionscontextid is set correctly, which is from filter question category id.
$this->assertEquals($reference->questionscontextid,
$qcats[$filtercondition->questioncategoryid]->contextid);
}
}
}
+109
View File
@@ -0,0 +1,109 @@
<?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\backup;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . "/phpunit/classes/restore_date_testcase.php");
/**
* Restore date tests.
*
* @package mod_quiz
* @copyright 2017 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class restore_date_test extends \restore_date_testcase {
/**
* Test restore dates.
*/
public function test_restore_dates(): void {
global $DB, $USER;
// Create quiz data.
$record = ['timeopen' => 100, 'timeclose' => 100, 'timemodified' => 100, 'tiemcreated' => 100, 'questionsperpage' => 0,
'grade' => 100.0, 'sumgrades' => 2];
list($course, $quiz) = $this->create_course_and_module('quiz', $record);
// Create questions.
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $questiongenerator->create_question_category();
$saq = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
// Add to the quiz.
quiz_add_quiz_question($saq->id, $quiz);
// Create an attempt.
$timestamp = 100;
$quizobj = \mod_quiz\quiz_settings::create($quiz->id);
$attempt = quiz_create_attempt($quizobj, 1, false, $timestamp, false);
$quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timestamp);
quiz_attempt_save_started($quizobj, $quba, $attempt);
// Quiz grade.
$grade = new \stdClass();
$grade->quiz = $quiz->id;
$grade->userid = $USER->id;
$grade->grade = 8.9;
$grade->timemodified = $timestamp;
$grade->id = $DB->insert_record('quiz_grades', $grade);
// User override.
$override = (object)[
'quiz' => $quiz->id,
'groupid' => 0,
'userid' => $USER->id,
'sortorder' => 1,
'timeopen' => 100,
'timeclose' => 200
];
$DB->insert_record('quiz_overrides', $override);
// Set time fields to a constant for easy validation.
$DB->set_field('quiz_attempts', 'timefinish', $timestamp);
// Do backup and restore.
$newcourseid = $this->backup_and_restore($course);
$newquiz = $DB->get_record('quiz', ['course' => $newcourseid]);
$this->assertFieldsNotRolledForward($quiz, $newquiz, ['timecreated', 'timemodified']);
$props = ['timeclose', 'timeopen'];
$this->assertFieldsRolledForward($quiz, $newquiz, $props);
$newattempt = $DB->get_record('quiz_attempts', ['quiz' => $newquiz->id]);
$newoverride = $DB->get_record('quiz_overrides', ['quiz' => $newquiz->id]);
$newgrade = $DB->get_record('quiz_grades', ['quiz' => $newquiz->id]);
// Attempt time checks.
$diff = $this->get_diff();
$this->assertEquals($timestamp, $newattempt->timemodified);
$this->assertEquals($timestamp, $newattempt->timefinish);
$this->assertEquals($timestamp, $newattempt->timestart);
$this->assertEquals($timestamp + $diff, $newattempt->timecheckstate); // Should this be rolled?
// Quiz override time checks.
$diff = $this->get_diff();
$this->assertEquals($override->timeopen + $diff, $newoverride->timeopen);
$this->assertEquals($override->timeclose + $diff, $newoverride->timeclose);
// Quiz grade time checks.
$this->assertEquals($grade->timemodified, $newgrade->timemodified);
}
}
@@ -0,0 +1,90 @@
<?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 core_question_generator;
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();
$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']);
// 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);
// 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());
// Check the grade items are right in the restored quiz.
$this->assertEquals(
[
(object) ['id' => reset($quizgradeitems)->id, 'quizid' => $newquiz->id, 'sortorder' => 1, 'name' => 'Listening'],
(object) ['id' => end($quizgradeitems)->id, 'quizid' => $newquiz->id, 'sortorder' => 2, 'name' => 'Reading'],
],
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);
}
}