diff --git a/question.php b/question.php index 1bb5717..cd13208 100644 --- a/question.php +++ b/question.php @@ -168,6 +168,20 @@ public function update_attempt_state_data_for_new_version( return $startdata; } + + /** + * Check whether question has any specific feedback. + * + * @return bool + */ + public function has_specific_feedback(): bool { + foreach ($this->rows as $row) { + if (trim($row->feedback) !== '') { + return true; + } + } + return false; + } } /** diff --git a/renderer.php b/renderer.php index ef3bc1b..56e5756 100644 --- a/renderer.php +++ b/renderer.php @@ -149,8 +149,8 @@ public function matrix_table(question_attempt $qa, question_display_options $opt ['scope' => 'col', 'class' => 'align-middle text-center']); $index += 1; } - // Add feedback header. - if ($options->feedback) { + // Add feedback header only when specific feedback is set to be displayed and provided at least for one row. + if ($options->feedback && $question->has_specific_feedback()) { $table .= html_writer::tag('th', html_writer::span(get_string('feedback', 'question'), 'answer_col', ['id' => 'col' . $index]), ['scope' => 'col', 'class' => 'rowfeedback align-middle']); } @@ -212,7 +212,7 @@ public function matrix_table(question_attempt $qa, question_display_options $opt $table .= html_writer::tag('td', $answered, ['class' => "$class matrixanswer align-middle text-center"]); } - if ($options->feedback) { + if ($options->feedback && $question->has_specific_feedback()) { $table .= html_writer::tag('td', $feedback); } $table .= html_writer::end_tag('tr'); diff --git a/tests/question_single_test.php b/tests/question_single_test.php index b9d4097..1b8325d 100644 --- a/tests/question_single_test.php +++ b/tests/question_single_test.php @@ -280,4 +280,27 @@ public function test_update_attempt_state_date_from_old_version_ok(): void { $this->assertEquals(['_roworder' => '22,23,21,24'], $newq->update_attempt_state_data_for_new_version($oldstep, $q)); } + + public function test_has_specific_feedback(): void { + $q = \test_question_maker::make_question('oumatrix', 'animals_single'); + + // This question has speific feedback for all 4 rows. + $this->assertTrue($q->has_specific_feedback()); + + // First row does not have feedback text. + $q->rows[11]->feedback = ''; + $this->assertTrue($q->has_specific_feedback()); + + // First and second rows do not have feedback text. + $q->rows[12]->feedback = ''; + $this->assertTrue($q->has_specific_feedback()); + + // First, second and third rows do not have feedback text. + $q->rows[13]->feedback = ''; + $this->assertTrue($q->has_specific_feedback()); + + // All rows do not have feedback text. + $q->rows[14]->feedback = ''; + $this->assertFalse($q->has_specific_feedback()); + } }