diff --git a/question/bank/editquestion/question.php b/question/bank/editquestion/question.php index dfade996968..0d5cb179670 100644 --- a/question/bank/editquestion/question.php +++ b/question/bank/editquestion/question.php @@ -310,6 +310,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); } @@ -331,6 +339,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 39dc3ab889b..4dee50d7e3a 100644 --- a/question/classes/local/bank/view.php +++ b/question/classes/local/bank/view.php @@ -1128,6 +1128,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 1afe2567611..18098e764f2 100644 --- a/question/tests/behat/filter_questions_combined_conditions.feature +++ b/question/tests/behat/filter_questions_combined_conditions.feature @@ -6,27 +6,28 @@ 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 "activities" exist: - | activity | name | intro | course | idnumber | - | qbank | Qbank 1 | Question bank 1 | C1 | qbank1 | + | activity | name | intro | course | idnumber | + | qbank | Qbank 1 | Question bank 1 | C1 | qbank1 | And the following "question categories" exist: | contextlevel | reference | name | | Activity module | qbank1 | Test questions 1 | | Activity module | qbank1 | Test questions 2 | + | Activity module | qbank1 | Test questions 3 | 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 | @@ -46,8 +47,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 | @@ -68,3 +69,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 "Test questions 3" + 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 | Test questions 3 | + And I set the following fields to these values: + | Category | Test questions 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"