Skip to content

Commit

Permalink
incomplete fix error with multiple references to the same question.
Browse files Browse the repository at this point in the history
Test and additional validation is not yet implemented
  • Loading branch information
Glutamat42 committed Apr 30, 2024
1 parent fcfb919 commit 7eb19f3
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions classes/local/db/adleradaptivity_question_repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace mod_adleradaptivity\local\db;

use context_module;
use dml_exception;
use moodle_database;
use stdClass;

class adleradaptivity_question_repository extends base_repository {
Expand All @@ -14,14 +14,30 @@ class adleradaptivity_question_repository extends base_repository {
* @return stdClass question object
* @throws dml_exception
*/
public function get_adleradaptivity_question_by_question_bank_entries_id(int $question_bank_entries_id) {
public function get_adleradaptivity_question_by_question_bank_entries_id(int $question_bank_entries_id, context_module $module_context) {
$sql = "
SELECT aq.*
FROM {adleradaptivity_questions} aq
JOIN {question_references} qr ON qr.itemid = aq.id
WHERE qr.questionbankentryid = ?
WHERE qr.questionbankentryid = :questionbankentryid
AND qr.usingcontextid = :usingcontextid
";

return $this->db->get_record_sql($sql, ['questionbankentryid' => $question_bank_entries_id], MUST_EXIST);
// TODO: this fails if there are multiple references to the same question (e.g. cloning a module)
// Multiple records found, only one record expected.
// Error code: multiplerecordsfound

// questions are loaded from question_usage. This questions are then tried to be matched to adleradaptivity_questions.
// This expects that there is only one question_reference per question.
//
// -> 1) verify question is only referenced once in the current module instance
// -> 2) additional condition in sql query to only get the question_reference for the current module instance
// -> 2b) load all questions via the modules database (instance -> tasks -> questions -> moodle questions) and verify it is in the question_usage (cross check both directions)

// Update: done 2
// TODO: test that tests this new behavior (for testing: remove `AND qr.usingcontextid = ?` and `, 'usingcontextid' => $module_context->id`
// TODO: 1

return $this->db->get_record_sql($sql, ['questionbankentryid' => $question_bank_entries_id, 'usingcontextid' => $module_context->id], MUST_EXIST);
}
}

0 comments on commit 7eb19f3

Please sign in to comment.