diff --git a/mod/quiz/tests/behat/editing_switch_bank.feature b/mod/quiz/tests/behat/editing_switch_bank.feature index f9a53fdf5a0..cb6c047386c 100644 --- a/mod/quiz/tests/behat/editing_switch_bank.feature +++ b/mod/quiz/tests/behat/editing_switch_bank.feature @@ -54,10 +54,10 @@ Feature: Switching question bank when adding questions to a quiz And I should see "Test questions 3" Scenario: Viewing question banks not in the current course show as recently accessed - Given I am on the "qbank1" "Activity" page - And I am on the "qbank2" "Activity" page - And I am on the "qbank3" "Activity" page - And I am on the "Quiz 1" "mod_quiz > Edit" page + Given "teacher" has recently viewed the "qbank1" "qbank" question bank + And "teacher" has recently viewed the "qbank2" "qbank" question bank + And "teacher" has recently viewed the "qbank3" "qbank" question bank + And "teacher" has recently viewed the "Quiz 1" "quiz" question bank When I open the "last" add to quiz menu And I follow "from question bank" And I click on "Switch bank" "button" diff --git a/question/bank/history/tests/question_history_view_test.php b/question/bank/history/tests/question_history_view_test.php index cfc4b7a1694..a554e57d816 100644 --- a/question/bank/history/tests/question_history_view_test.php +++ b/question/bank/history/tests/question_history_view_test.php @@ -101,7 +101,7 @@ final class question_history_view_test extends \advanced_testcase { $course = $generator->create_course(); $qbank = $generator->create_module('qbank', ['course' => $course->id]); $cm = get_coursemodule_from_id('qbank', $qbank->cmid); - $context = \context_course::instance($course->id); + $context = \context_module::instance($cm->id); // Create a question in the default category. $contexts = new \core_question\local\bank\question_edit_contexts($context); diff --git a/question/classes/local/bank/view.php b/question/classes/local/bank/view.php index 437267c9d63..f23c680351b 100644 --- a/question/classes/local/bank/view.php +++ b/question/classes/local/bank/view.php @@ -267,6 +267,9 @@ class view { $this->init_question_actions(); $this->init_sort(); $this->init_bulk_actions(); + + // Record that this question bank has been used. + question_bank_helper::add_bank_context_to_recently_viewed($contexts->lowest()); } /** diff --git a/question/edit.php b/question/edit.php index 14909b1d18b..5ea2f91ac0e 100644 --- a/question/edit.php +++ b/question/edit.php @@ -95,6 +95,5 @@ $category->id = $categoryid; $catcontext = context::instance_by_id($contextid); $event = question_category_viewed::create_from_question_category_instance($category, $catcontext); $event->trigger(); -\core_question\local\bank\question_bank_helper::add_bank_context_to_recently_viewed($catcontext); echo $OUTPUT->footer(); diff --git a/question/tests/behat/behat_core_question.php b/question/tests/behat/behat_core_question.php index 1131437f83c..e69a09054b0 100644 --- a/question/tests/behat/behat_core_question.php +++ b/question/tests/behat/behat_core_question.php @@ -408,4 +408,36 @@ class behat_core_question extends behat_question_base { // Apply filters. $this->execute("behat_forms::press_button", [get_string('applyfilters')]); } + + /** + * Record that a user has recently accessed the question bank related to a particular activity. + * + * @Given :user has recently viewed the :activityname :activitytype question bank + * @param string $useridentifier The user's username or email. + * @param string $activityname name of an activity. + * @param string $activitytype type of an activity, e.g. 'quiz' or 'qbank'. + */ + public function user_has_recently_viewed_question_bank( + string $useridentifier, + string $activityname, + string $activitytype, + ): void { + global $USER; + $originaluser = $USER; + + if (!plugin_supports('mod', $activitytype, FEATURE_USES_QUESTIONS)) { + throw new Exception($activitytype . ' do not have a question bank.'); + } + + $user = $this->get_user_by_identifier($useridentifier); + if (!$user) { + throw new Exception('Unknow user ' . $useridentifier . '.'); + } + $USER = $user; + + $cm = $this->get_cm_by_activity_name($activitytype, $activityname); + \core_question\local\bank\question_bank_helper::add_bank_context_to_recently_viewed($cm->context); + + $USER = $originaluser; + } } diff --git a/question/tests/question_bank_column_test.php b/question/tests/question_bank_column_test.php index 860712efd85..96feb0dfabd 100644 --- a/question/tests/question_bank_column_test.php +++ b/question/tests/question_bank_column_test.php @@ -45,7 +45,7 @@ final class question_bank_column_test extends \advanced_testcase { $qbank = $this->getDataGenerator()->create_module('qbank', ['course' => $course->id]); $cm = get_coursemodule_from_id('qbank', $qbank->cmid); $questionbank = new view( - new question_edit_contexts(\context_course::instance($course->id)), + new question_edit_contexts(\context_module::instance($cm->id)), new \moodle_url('/'), $course, $cm @@ -82,7 +82,7 @@ final class question_bank_column_test extends \advanced_testcase { $qbank = $this->getDataGenerator()->create_module('qbank', ['course' => $course->id]); $cm = get_coursemodule_from_id('qbank', $qbank->cmid); $questionbank = new view( - new question_edit_contexts(\context_course::instance($course->id)), + new question_edit_contexts(\context_module::instance($cm->id)), new \moodle_url('/'), $course, $cm