Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Questionnaire: All branching shown in View your response #616

Open
wants to merge 1 commit into
base: MOODLE_404_STABLE
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moodle code style requires functions to be all lowercase, and separated by underscores.

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?"