This commit is contained in:
Huong Nguyen
2025-04-09 15:35:11 +07:00
10 changed files with 63 additions and 26 deletions
@@ -316,12 +316,11 @@ class custom_view extends \core_question\local\bank\view {
* @return void
*/
public function display(): void {
$editcontexts = $this->contexts->having_one_edit_tab_cap('questions');
echo \html_writer::start_div('questionbankwindow boxwidthwide boxaligncenter', [
'data-component' => 'core_question',
'data-callback' => 'display_question_bank',
'data-contextid' => $editcontexts[array_key_last($editcontexts)]->id,
'data-contextid' => $this->contexts->lowest()->id,
]);
// Show the 'switch question bank' button.
+3 -1
View File
@@ -2344,7 +2344,9 @@ function mod_quiz_output_fragment_quiz_question_bank($args): string {
build_required_parameters_for_custom_view($params, $extraparams);
$course = get_course($cm->course);
require_capability('mod/quiz:manage', $contexts->lowest());
if (!has_capability('moodle/question:useall', $contexts->lowest())) {
require_capability('moodle/question:usemine', $contexts->lowest());
}
// Custom View.
$questionbank = new $viewclass($contexts, $thispageurl, $course, $cm, $pagevars, $extraparams);
@@ -6,17 +6,20 @@ Feature: Switching question bank when adding questions to a quiz
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| username |
| teacher |
And the following "role capability" exists:
| role | teacher |
| moodle/question:useall | allow |
And the following "courses" exist:
| fullname | shortname | format |
| Course 1 | C1 | weeks |
| Course 2 | C2 | weeks |
| Course 3 | C3 | weeks |
| fullname | shortname |
| Course 1 | C1 |
| Course 2 | C2 |
| Course 3 | C3 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| teacher1 | C2 | editingteacher |
| user | course | role |
| teacher | C1 | editingteacher |
| teacher | C2 | teacher |
And the following "activities" exist:
| activity | name | intro | course | idnumber |
| quiz | Quiz 1 | Quiz 1 for testing the Add menu | C1 | quiz1 |
@@ -31,8 +34,7 @@ Feature: Switching question bank when adding questions to a quiz
| Activity module | qbank3 | Test questions 3 |
| Activity module | qbank4 | Test questions 4 |
| Activity module | quiz1 | Test questions 5 |
And I log in as "teacher1"
And I am on the "Quiz 1" "mod_quiz > Edit" page
And I am on the "Quiz 1" "mod_quiz > Edit" page logged in as "teacher"
Scenario: Switching to another bank shows the expected banks
When I open the "last" add to quiz menu
@@ -55,10 +57,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"
@@ -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);
+1 -1
View File
@@ -76,7 +76,7 @@ class search_shared_banks extends external_api {
$sharedbanks = question_bank_helper::get_activity_instances_with_shareable_questions(
notincourseids: [$courseid],
havingcap: ['moodle/question:managecategory'],
havingcap: ['moodle/question:useall', 'moodle/question:usemine'],
filtercontext: $modulecontext,
search: $search,
limit: self::MAX_RESULTS + 1, // Return up to 1 extra result, so we know there are more.
@@ -130,7 +130,7 @@ class question_bank_helper {
*
* @param array $incourseids array of course ids where you want instances included. Leave empty if you want from all courses.
* @param array $notincourseids array of course ids where you do not want instances included.
* @param array $havingcap current user must have these capabilities on each bank context.
* @param array $havingcap current user must have at least one of these capabilities on each bank context.
* @param bool $getcategories optionally return the categories belonging to these banks.
* @param int $currentbankid optionally include the bank id you want included as the first result from the method return.
* it will only be included if the other parameters allow it.
@@ -167,7 +167,7 @@ class question_bank_helper {
*
* @param array $incourseids array of course ids where you want instances included. Leave empty if you want from all courses.
* @param array $notincourseids array of course ids where you do not want instances included.
* @param array $havingcap current user must have these capabilities on each bank context.
* @param array $havingcap current user must have at least one of these capabilities on each bank context.
* @param bool $getcategories optionally return the categories belonging to these banks.
* @param int $currentbankid optionally include the bank id you want included as the first result from the method return.
* it will only be included if the other parameters allow it.
@@ -204,7 +204,7 @@ class question_bank_helper {
* @param bool $getcategories optionally return the categories belonging to these banks.
* @param int $currentbankid optionally include the bank id you want included as the first result from the method return.
* it will only be included if the other parameters allow it.
* @param array $havingcap current user must have these capabilities on each bank context.
* @param array $havingcap current user must have at least one of these capabilities on each bank context.
* @param ?context $filtercontext Optional context to use for all string filtering, useful for performance when calling with
* parameters that will get banks across multiple contexts.
* @param string $search Optional term to search question bank instances by name
+3
View File
@@ -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());
}
/**
-1
View File
@@ -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();
@@ -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;
}
}
+2 -2
View File
@@ -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