diff --git a/questionnaire.class.php b/questionnaire.class.php index 004d4609..818ab800 100644 --- a/questionnaire.class.php +++ b/questionnaire.class.php @@ -423,6 +423,9 @@ public function view_response($rid, $referer= '', $resps = '', $compare = false, } $pdf = ($outputtarget == 'pdf') ? true : false; foreach ($this->questions as $question) { + if (self::isDependentOnChoices($question, $this->responses[$rid]->answers)) { + continue; + } if ($question->type_id < QUESPAGEBREAK) { $i++; } @@ -4176,4 +4179,26 @@ public static function get_user_identity_fields($context, $userid) { $row = $DB->get_record_sql($sql, array_merge($params, [$userid])); return $row; } + + /** + * Determines if a question is dependent on another question's choice. + * + * Checks if the provided question has dependencies on other questions + * and if any required answer choices have not been selected. + * + * @param object $question The question object containing dependencies + * @param array $answers An associative array of answers with question IDs as keys + * + * @return bool True if the question has an unmet dependency; otherwise, false + */ + public static function isDependentOnChoices(object $question, array $answers): bool { + if (!empty($question->dependencies)) { + foreach ($question->dependencies as $dependency) { + if (empty($answers[$dependency->dependquestionid][$dependency->dependchoiceid])) { + return true; + } + } + } + return false; + } } diff --git a/tests/behat/dependency_question.feature b/tests/behat/dependency_question.feature index 8a9a8ccb..a3521389 100644 --- a/tests/behat/dependency_question.feature +++ b/tests/behat/dependency_question.feature @@ -106,3 +106,21 @@ Feature: Questions can be defined to be dependent on answers to multiple previou And I should see "Do you own a dog?" And I should see "Parent Question : position 1 (Q1->Dog) set" And I log out + + @javascript + Scenario: Students can only view answers to questions asked on the individual responses page. + Given I log in as "student1" + And I am on "Course 1" course homepage + And I follow "Test questionnaire" + When I navigate to "Answer the questions..." in current page administration + And I should see "Do you own a car?" + And I click on "Yes" "radio" + And I press "Next Page >>" + And I should see "What colour is the car?" + And I set the field "What colour is the car?" to "Black" + And I press "Next Page >>" + And I press "Submit questionnaire" + And I navigate to "View your response(s)" in current page administration + And I should see "Do you own a car?" + And I should see "What colour is the car?" + Then I should not see "Will you buy a car this year?"