Skip to content

Commit

Permalink
Questionnaire: All branching shown in View your response
Browse files Browse the repository at this point in the history
  • Loading branch information
toanlamt committed Nov 1, 2024
1 parent f2ffa46 commit d036bf2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
25 changes: 25 additions & 0 deletions questionnaire.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand Down Expand Up @@ -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;
}
}
18 changes: 18 additions & 0 deletions tests/behat/dependency_question.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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?"

0 comments on commit d036bf2

Please sign in to comment.