Skip to content

Commit 3278146

Browse files
toanlamttai.letan
authored andcommitted
Questionnaire: All branching shown in View your response
1 parent 666310d commit 3278146

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

questionnaire.class.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,9 @@ public function view_response($rid, $referer= '', $resps = '', $compare = false,
423423
}
424424
$pdf = ($outputtarget == 'pdf') ? true : false;
425425
foreach ($this->questions as $question) {
426+
if (self::isDependentOnChoices($question, $this->responses[$rid]->answers)) {
427+
continue;
428+
}
426429
if ($question->type_id < QUESPAGEBREAK) {
427430
$i++;
428431
}
@@ -4189,4 +4192,26 @@ public static function get_user_identity_fields($context, $userid) {
41894192
$row = $DB->get_record_sql($sql, array_merge($params, [$userid]));
41904193
return $row;
41914194
}
4195+
4196+
/**
4197+
* Determines if a question is dependent on another question's choice.
4198+
*
4199+
* Checks if the provided question has dependencies on other questions
4200+
* and if any required answer choices have not been selected.
4201+
*
4202+
* @param object $question The question object containing dependencies
4203+
* @param array $answers An associative array of answers with question IDs as keys
4204+
*
4205+
* @return bool True if the question has an unmet dependency; otherwise, false
4206+
*/
4207+
public static function isDependentOnChoices(object $question, array $answers): bool {
4208+
if (!empty($question->dependencies)) {
4209+
foreach ($question->dependencies as $dependency) {
4210+
if (empty($answers[$dependency->dependquestionid][$dependency->dependchoiceid])) {
4211+
return true;
4212+
}
4213+
}
4214+
}
4215+
return false;
4216+
}
41924217
}

tests/behat/dependency_question.feature

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,21 @@ Feature: Questions can be defined to be dependent on answers to multiple previou
106106
And I should see "Do you own a dog?"
107107
And I should see "Parent Question : position 1 (Q1->Dog) set"
108108
And I log out
109+
110+
@javascript
111+
Scenario: Students can only view answers to questions asked on the individual responses page.
112+
Given I log in as "student1"
113+
And I am on "Course 1" course homepage
114+
And I follow "Test questionnaire"
115+
When I navigate to "Answer the questions..." in current page administration
116+
And I should see "Do you own a car?"
117+
And I click on "Yes" "radio"
118+
And I press "Next Page >>"
119+
And I should see "What colour is the car?"
120+
And I set the field "What colour is the car?" to "Black"
121+
And I press "Next Page >>"
122+
And I press "Submit questionnaire"
123+
And I navigate to "View your response(s)" in current page administration
124+
And I should see "Do you own a car?"
125+
And I should see "What colour is the car?"
126+
Then I should not see "Will you buy a car this year?"

0 commit comments

Comments
 (0)