diff --git a/question/bank/editquestion/question.php b/question/bank/editquestion/question.php index c4b7479dfc5..b1a1853563a 100644 --- a/question/bank/editquestion/question.php +++ b/question/bank/editquestion/question.php @@ -328,6 +328,14 @@ if ($mform->is_cancelled()) { $returnurl->param('sesskey', sesskey()); $returnurl->param('cmid', $cmid); } + // Update the filter param to the updated category if the return have any. + if (!empty($returnurl->param('filter'))) { + $filter = json_decode($returnurl->param('filter'), true); + if (isset($filter['category']['values'])) { + $filter['category']['values'][0] = $question->category; + $returnurl->param('filter', json_encode($filter)); + } + } redirect($returnurl); } @@ -349,6 +357,14 @@ if ($mform->is_cancelled()) { } else { $nexturl->param('courseid', $COURSE->id); } + // Update the filter param to the updated category if the return url have any. + if (!empty($nexturl->param('filter'))) { + $filter = json_decode($nexturl->param('filter'), true); + if (isset($filter['category']['values'])) { + $filter['category']['values'][0] = $question->category; + $nexturl->param('filter', json_encode($filter)); + } + } redirect($nexturl); } diff --git a/question/classes/local/bank/view.php b/question/classes/local/bank/view.php index 507e95a030e..7ddf1739b21 100644 --- a/question/classes/local/bank/view.php +++ b/question/classes/local/bank/view.php @@ -1145,6 +1145,16 @@ class view { [$categoryid, $contextid] = category_condition::validate_category_param($this->pagevars['cat']); $catcontext = \context::instance_by_id($contextid); + // Update the question in the list with correct category context when we have selected category filter. + if (isset($this->pagevars['filter']['category']['values'])) { + $categoryid = $this->pagevars['filter']['category']['values'][0]; + foreach ($this->contexts->all() as $context) { + if ((int) $context->instanceid === (int) $categoryid) { + $catcontext = $context; + break; + } + } + } echo \html_writer::start_tag( 'div', diff --git a/question/tests/behat/filter_questions_combined_conditions.feature b/question/tests/behat/filter_questions_combined_conditions.feature index 046d2550d8c..2a8c2316463 100644 --- a/question/tests/behat/filter_questions_combined_conditions.feature +++ b/question/tests/behat/filter_questions_combined_conditions.feature @@ -6,24 +6,27 @@ Feature: The questions in the question bank can be filtered by combine various c Background: Given the following "users" exist: - | username | firstname | lastname | email | - | teacher1 | Teacher | 1 | teacher1@example.com | + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | teacher1@example.com | And the following "courses" exist: | fullname | shortname | format | - | Course 1 | C1 | weeks | + | Course 1 | C1 | weeks | And the following "course enrolments" exist: - | user | course | role | - | teacher1 | C1 | editingteacher | + | user | course | role | + | teacher1 | C1 | editingteacher | And the following "question categories" exist: - | contextlevel | reference | name | - | Course | C1 | Test questions 1| - | Course | C1 | Test questions 2| + | contextlevel | reference | name | + | Course | C1 | Test questions 1 | + | Course | C1 | Test questions 2 | + And the following "question categories" exist: + | contextlevel | reference | name | questioncategory | + | Course | C1 | Subcategory | Test questions 1 | And the following "questions" exist: - | questioncategory | qtype | name | user | questiontext | - | Test questions 1 | essay | question 1 name | teacher1 | Question 1 text | - | Test questions 1 | essay | question 2 name | teacher1 | Question 2 text | - | Test questions 2 | essay | question 3 name | teacher1 | Question 3 text | - | Test questions 2 | essay | question 4 name | teacher1 | Question 4 text | + | questioncategory | qtype | name | user | questiontext | + | Test questions 1 | essay | question 1 name | teacher1 | Question 1 text | + | Test questions 1 | essay | question 2 name | teacher1 | Question 2 text | + | Test questions 2 | essay | question 3 name | teacher1 | Question 3 text | + | Test questions 2 | essay | question 4 name | teacher1 | Question 4 text | And the following "core_question > Tags" exist: | question | tag | | question 1 name | foo | @@ -42,8 +45,8 @@ Feature: The questions in the question bank can be filtered by combine various c @javascript Scenario: Filters persist when the page is reloaded Given the following "questions" exist: - | questioncategory | qtype | name | user | questiontext | status | - | Test questions 1 | essay | hidden question name | teacher1 | Hidden text | hidden | + | questioncategory | qtype | name | user | questiontext | status | + | Test questions 1 | essay | hidden question name | teacher1 | Hidden text | hidden | And the following "core_question > Tags" exist: | question | tag | | hidden question name | foo | @@ -64,3 +67,21 @@ Feature: The questions in the question bank can be filtered by combine various c And I should not see "question 2 name" in the "categoryquestions" "table" And I should not see "question 3 name" in the "categoryquestions" "table" And I should not see "question 4 name" in the "categoryquestions" "table" + + @javascript + Scenario: Filtered category should be kept when we create new question. + Given I apply question bank filter "Category" with value "Subcategory" + And I should not see "question 1 name" + And I should not see "question 2 name" + And I click on "Create a new question" "button" + And I click on "True/False" "text" + And I click on "submitbutton" "button" + And the following fields match these values: + | Category |    Subcategory | + And I set the following fields to these values: + | Category | Test questions 2 (2) | + | Question name | Question 3 | + | Question text | T/F question text | + When I press "id_submitbutton" + Then I should see "Question 3" + And I should see "question 3 name"