MDL-76717 qbank_usage: Fix usage count query for random questions
This commit is contained in:
@@ -128,9 +128,10 @@ class helper {
|
||||
JOIN {quiz_attempts} qa ON qa.quiz = qz.id
|
||||
JOIN {question_usages} qu ON qu.id = qa.uniqueid
|
||||
JOIN {question_attempts} qatt ON qatt.questionusageid = qu.id
|
||||
JOIN {question} q ON q.id = qatt.questionid
|
||||
WHERE qa.preview = 0
|
||||
AND q.id = ?";
|
||||
JOIN {question_versions} qv ON qv.questionid = qatt.questionid
|
||||
JOIN {question_versions} qv2 ON qv.questionbankentryid = qv2.questionbankentryid
|
||||
WHERE qa.preview = 0
|
||||
AND qv2.questionid = ?";
|
||||
return $sql;
|
||||
}
|
||||
|
||||
|
||||
@@ -74,6 +74,7 @@ class question_usage_table extends table_sql {
|
||||
$this->define_columns($columns);
|
||||
$this->define_headers($headers);
|
||||
$this->define_baseurl($PAGE->url);
|
||||
$this->set_attribute('id', 'question_usage_table');
|
||||
}
|
||||
|
||||
public function query_db($pagesize, $useinitialsbar = true) {
|
||||
|
||||
@@ -38,3 +38,15 @@ Feature: Use the qbank plugin manager page for question usage
|
||||
Then I should see "Version 1"
|
||||
And I click on "Close" "button" in the ".modal-dialog" "css_element"
|
||||
And I should see "0" on the usage column
|
||||
|
||||
@javascript
|
||||
Scenario: Question usage modal should work with usage data
|
||||
Given quiz "Test quiz" contains the following questions:
|
||||
| question | page |
|
||||
| First question | 1 |
|
||||
And I am on the "Test quiz" "mod_quiz > question bank" page logged in as "admin"
|
||||
And I set the field "Select a category" to "Test questions"
|
||||
And I should see "Test questions"
|
||||
And I should see "1" on the usage column
|
||||
When I click "1" on the usage column
|
||||
Then "Test quiz" "table_row" should exist in the "question-usage_table" "region"
|
||||
|
||||
@@ -34,6 +34,16 @@ class helper_test extends \advanced_testcase {
|
||||
*/
|
||||
protected $quiz;
|
||||
|
||||
/**
|
||||
* @var \stdClass $user
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* @var \core_question_generator $questiongenerator
|
||||
*/
|
||||
protected $questiongenerator;
|
||||
|
||||
/**
|
||||
* @var array $questions
|
||||
*/
|
||||
@@ -46,20 +56,15 @@ class helper_test extends \advanced_testcase {
|
||||
$this->resetAfterTest();
|
||||
$layout = '1,2,0';
|
||||
// Make a user to do the quiz.
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$this->user = $this->getDataGenerator()->create_user();
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
// Make a quiz.
|
||||
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
||||
$this->quiz = $quizgenerator->create_instance(['course' => $course->id,
|
||||
'grade' => 100.0, 'sumgrades' => 2, 'layout' => $layout]);
|
||||
'grade' => 100.0, 'sumgrades' => 2, 'layout' => $layout]);
|
||||
|
||||
$quizobj = \mod_quiz\quiz_settings::create($this->quiz->id, $user->id);
|
||||
|
||||
$quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
||||
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
||||
|
||||
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
||||
$cat = $questiongenerator->create_question_category();
|
||||
$this->questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
||||
$cat = $this->questiongenerator->create_question_category();
|
||||
|
||||
$page = 1;
|
||||
foreach (explode(',', $layout) as $slot) {
|
||||
@@ -68,13 +73,19 @@ class helper_test extends \advanced_testcase {
|
||||
continue;
|
||||
}
|
||||
|
||||
$question = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
|
||||
$question = $this->questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
|
||||
quiz_add_quiz_question($question->id, $this->quiz, $page);
|
||||
$this->questions [] = $question;
|
||||
}
|
||||
}
|
||||
|
||||
protected function attempt_quiz() {
|
||||
$quizobj = \mod_quiz\quiz_settings::create($this->quiz->id, $this->user->id);
|
||||
|
||||
$quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
||||
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
||||
$timenow = time();
|
||||
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $user->id);
|
||||
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $this->user->id);
|
||||
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
|
||||
quiz_attempt_save_started($quizobj, $quba, $attempt);
|
||||
quiz_attempt::create($attempt->id);
|
||||
@@ -86,6 +97,7 @@ class helper_test extends \advanced_testcase {
|
||||
* @covers ::get_question_attempts_count_in_quiz
|
||||
*/
|
||||
public function test_get_question_attempts_count_in_quiz() {
|
||||
$this->attempt_quiz();
|
||||
foreach ($this->questions as $question) {
|
||||
$questionattemptcount = helper::get_question_attempts_count_in_quiz($question->id, $this->quiz->id);
|
||||
// Test the attempt count matches the usage count, each question should have one count.
|
||||
@@ -105,4 +117,25 @@ class helper_test extends \advanced_testcase {
|
||||
$this->assertEquals(1, $count);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If a question has been included via a random question attempt, this should be counted as a usage.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_get_random_question_attempts_usage_count() {
|
||||
$this->setAdminUser();
|
||||
$cat = $this->questiongenerator->create_question_category();
|
||||
$question = $this->questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
|
||||
quiz_add_random_questions($this->quiz, 1, $cat->id, 1, false);
|
||||
|
||||
$qdef = \question_bank::load_question($question->id);
|
||||
$count = helper::get_question_entry_usage_count($qdef);
|
||||
$this->assertEquals(0, $count);
|
||||
|
||||
$this->attempt_quiz();
|
||||
|
||||
$count = helper::get_question_entry_usage_count($qdef);
|
||||
$this->assertEquals(1, $count);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user