From 5380ec0fe29e983f7d8fc3fe40cec43132498a5e Mon Sep 17 00:00:00 2001 From: danghieu1407 Date: Wed, 13 Nov 2024 13:45:25 +0700 Subject: [PATCH] StudentQuiz: Questions from a StudentQuiz cannot be reused in other StudentQuizzes #814019 --- locallib.php | 46 ++++++++++++++++++----- tests/studentquiz_question_test.php | 58 +++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 10 deletions(-) diff --git a/locallib.php b/locallib.php index 3ab832cc..b9e997c7 100644 --- a/locallib.php +++ b/locallib.php @@ -29,6 +29,7 @@ use mod_studentquiz\utils; use core_question\local\bank\question_version_status; use mod_studentquiz\local\studentquiz_progress; +use mod_studentquiz\local\studentquiz_question; defined('MOODLE_INTERNAL') || die(); @@ -721,11 +722,13 @@ function mod_studentquiz_get_roles() { */ function mod_studentquiz_ensure_studentquiz_question_record($id, $cmid, $honorpublish = true, $hidden = true) { global $DB, $USER; - $params = [ - 'questionid' => $id, - ]; - // Check if record exist. - $sql = "SELECT COUNT(*) + $studentquiz = $DB->get_record('studentquiz', ['coursemodule' => $cmid]); + $cm = get_coursemodule_from_id('studentquiz', $cmid); + $contextid = context_module::instance($cmid)->id; + $question = question_bank::load_question($id); + + $params = ['questionid' => $id]; + $sql = "SELECT qr.itemid, qr.usingcontextid, qr.questionbankentryid FROM {studentquiz} sq -- Get this StudentQuiz question. JOIN {studentquiz_question} sqq ON sqq.studentquizid = sq.id @@ -742,9 +745,9 @@ function mod_studentquiz_ensure_studentquiz_question_record($id, $cmid, $honorpu JOIN {question} q ON q.id = qv.questionid WHERE q.id = :questionid "; - if (!$DB->count_records_sql($sql, $params)) { - $studentquiz = $DB->get_record('studentquiz', ['coursemodule' => $cmid]); - $cm = get_coursemodule_from_id('studentquiz', $cmid); + + // Check if record exist. + if (!$DB->record_exists_sql($sql, $params)) { $groupid = groups_get_activity_group($cm, true); $params = [ 'studentquizid' => $studentquiz->id, @@ -766,8 +769,6 @@ function mod_studentquiz_ensure_studentquiz_question_record($id, $cmid, $honorpu utils::question_save_action($record, null, studentquiz_helper::STATE_SHOW); } // Load question to create a question references. - $question = question_bank::load_question($id); - $contextid = context_module::instance($cmid)->id; $referenceparams = [ 'usingcontextid' => $contextid, 'itemid' => $record, @@ -777,6 +778,31 @@ function mod_studentquiz_ensure_studentquiz_question_record($id, $cmid, $honorpu 'version' => null ]; $DB->insert_record('question_references', (object) $referenceparams); + + } else { + $existedqr = $DB->get_record_sql($sql, $params); + $existedqrrecord = $DB->get_record('question_references', [ + 'itemid' => $existedqr->itemid, + 'component' => 'mod_studentquiz', + 'questionarea' => 'studentquiz_question', + 'usingcontextid' => $existedqr->usingcontextid, + 'questionbankentryid' => $existedqr->questionbankentryid, + 'version' => null, + ]); + + $newsqq = studentquiz_question::get_studentquiz_question_from_question($question, null, + $cm, context_module::instance($cmid)); + $newsqqrecord = $DB->get_record('studentquiz_question', ['id' => $newsqq->get_id()]); + + if ($newsqqrecord) { + $newsqqrecord->studentquizid = $studentquiz->id; + $DB->update_record('studentquiz_question', $newsqqrecord); + } + + if ($existedqrrecord) { + $existedqrrecord->usingcontextid = $contextid; + $DB->update_record('question_references', $existedqrrecord); + } } } diff --git a/tests/studentquiz_question_test.php b/tests/studentquiz_question_test.php index 13b53848..3a97ecf3 100644 --- a/tests/studentquiz_question_test.php +++ b/tests/studentquiz_question_test.php @@ -136,4 +136,62 @@ private function get_data_properties_on_studentquiz_question(): object { $reflector->setAccessible(true); return $reflector->getValue($this->studentquizquestion); } + + /** + * Test move question in studentquiz to another studentquiz. + */ + public function test_move_studentquiz_question(): void { + global $DB; + + $generator = $this->getDataGenerator(); + $course = $generator->create_course(); + $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); + + // Create two StudentQuiz activity. + $studentquizdata = [ + 'course' => $course->id, + 'anonymrank' => true, + 'questionquantifier' => 10, + 'approvedquantifier' => 5, + 'ratequantifier' => 3, + 'correctanswerquantifier' => 2, + 'incorrectanswerquantifier' => -1, + ]; + + $cmid1 = $generator->create_module('studentquiz', $studentquizdata)->cmid; + $cmid2 = $generator->create_module('studentquiz', $studentquizdata)->cmid; + + $context1 = \context_module::instance($cmid1); + $context2 = \context_module::instance($cmid2); + $studentquiz = [ + mod_studentquiz_load_studentquiz($cmid1, $context1->id), + mod_studentquiz_load_studentquiz($cmid2, $context2->id), + ]; + + $question1 = $questiongenerator->create_question('truefalse', null, + ['name' => 'Student quiz 1 Question', 'category' => $studentquiz[0]->categoryid]); + + // Move question to Studentquiz 2 category. + question_move_questions_to_category([$question1->id], $studentquiz[1]->categoryid); + $question1 = \question_bank::load_question($question1->id); + // Ensure it question 1 is in the Studentquiz 2 category. + $this->assertEquals($question1->category, $studentquiz[1]->categoryid); + $this->assertNotEquals($question1->category, $studentquiz[0]->categoryid); + + // Ensure the contextid is correct. + $this->assertEquals($question1->contextid, $context2->id); + + $questionrefrecord = $DB->get_records('question_references', [ + 'usingcontextid' => $context2->id, + 'questionbankentryid' => $question1->questionbankentryid, + 'component' => 'mod_studentquiz', + 'questionarea' => 'studentquiz_question', + ]); + + // Ensure the question_references record is updated. + $this->assertCount(1, $questionrefrecord); + $questionrefrecord = reset($questionrefrecord); + $this->assertEquals($questionrefrecord->usingcontextid, $context2->id); + $this->assertEquals($questionrefrecord->questionbankentryid, $question1->questionbankentryid); + } }