Skip to content

Commit

Permalink
StudentQuiz: Questions from a StudentQuiz cannot be reused in other S…
Browse files Browse the repository at this point in the history
…tudentQuizzes #814019
  • Loading branch information
danghieu1407 committed Nov 20, 2024
1 parent 7ae0f90 commit 5380ec0
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 10 deletions.
46 changes: 36 additions & 10 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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);
}
}
}

Expand Down
58 changes: 58 additions & 0 deletions tests/studentquiz_question_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 5380ec0

Please sign in to comment.