Skip to content

Commit 8c32816

Browse files
hieuvuAnupamaSarjoshi
hieuvu
authored andcommitted
Embedded Questions: Update db query to fix the error thrown by 'Previous attempts' link #883644
1 parent 40153e7 commit 8c32816

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

classes/output/renderer.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,18 @@ public function embedded_question(\question_usage_by_activity $quba, int $slot,
7373
$output = $this->add_fill_with_correct_link($output);
7474
}
7575

76-
// We want to display the Previous attempts link only if there is atleast one completed attempt.
77-
if (class_exists(report_embedquestion\attempt_summary_table::class) && $quba->question_count() > 1) {
78-
$output = $this->add_embedded_question_report_link($quba, $slot, $output);
76+
if (class_exists(report_embedquestion\attempt_summary_table::class)) {
77+
// We want to display the Previous attempts link only if there is at least one completed attempt.
78+
$hasattempts = false;
79+
foreach ($quba->get_attempt_iterator() as $qa) {
80+
if ($qa->get_state() !== \question_state::$todo) {
81+
$hasattempts = true;
82+
break;
83+
}
84+
}
85+
if ($hasattempts) {
86+
$output = $this->add_embedded_question_report_link($quba, $slot, $output);
87+
}
7988
}
8089

8190
return $output;

tests/attempt_test.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,14 @@ public function test_question_rendering(): void {
317317
'</div>~';
318318
$this->assertMatchesRegularExpression($expectedregex, $html);
319319

320-
// Verify that the Previous attempts link is displayed for the second attempt.
320+
/** @var \core_question_generator $questiongenerator */
321+
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
321322
$attempt->start_new_attempt_at_question();
323+
$postdata = $questiongenerator->get_simulated_post_data_for_questions_in_usage($attempt->get_question_usage(),
324+
[1 => 'Sample answer'], true);
325+
$attempt->process_submitted_actions($postdata);
326+
327+
// Verify that the Previous attempts link is displayed for the second attempt.
322328
$html = $attempt->render_question($renderer);
323329
$expectedregex = '~<div class="info"><h3 class="no">Question <span class="qno">[^<]+</span>' .
324330
'</h3><div class="state">Not complete</div><div class="grade">Marked out of 1.00</div>' .

0 commit comments

Comments
 (0)