From c9491626ab774071c764e2e5acf38f48a43793c5 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Wed, 6 Nov 2019 19:32:15 +0000 Subject: [PATCH 1/1] MDL-67153 question bank: let qtype add actions to the edit menu --- question/classes/bank/edit_menu_column.php | 10 ++++++++++ question/type/questiontypebase.php | 21 +++++++++++++++++++++ question/type/upgrade.txt | 10 ++++++++++ 3 files changed, 41 insertions(+) diff --git a/question/classes/bank/edit_menu_column.php b/question/classes/bank/edit_menu_column.php index 8ee79bbccd8..9cbec704a0d 100644 --- a/question/classes/bank/edit_menu_column.php +++ b/question/classes/bank/edit_menu_column.php @@ -86,6 +86,16 @@ class edit_menu_column extends column_base { } } + $qtypeactions = \question_bank::get_qtype($question->qtype) + ->get_extra_question_bank_actions($question); + foreach ($qtypeactions as $action) { + $menu->add($action); + } + echo $OUTPUT->render($menu); } + + public function get_required_fields() { + return ['q.qtype']; + } } diff --git a/question/type/questiontypebase.php b/question/type/questiontypebase.php index 7af20957460..9b54465d055 100644 --- a/question/type/questiontypebase.php +++ b/question/type/questiontypebase.php @@ -233,6 +233,27 @@ class question_type { return $CFG->wwwroot . '/question/type/' . $this->name(); } + /** + * Get extra actions for a question of this type to add to the question bank edit menu. + * + * This method is called if the {@link edit_menu_column} is being used in the + * question bank, which it is by default since Moodle 3.8. If applicable for + * your question type, you can return arn array of {@link action_menu_link}s. + * These will be added at the end of the Edit menu for this question. + * + * The $question object passed in will have a hard-to-predict set of fields, + * because the fields present depend on which columns are included in the + * question bank view. However, you can rely on 'id', 'createdby', + * 'contextid', 'hidden' and 'category' (id) being present, and so you + * can call question_has_capability_on without causing performance problems. + * + * @param stdClass $question the available information about the particular question the action is for. + * @return action_menu_link[] any actions you want to add to the Edit menu for this question. + */ + public function get_extra_question_bank_actions(stdClass $question): array { + return []; + } + /** * This method should be overriden if you want to include a special heading or some other * html on a question editing page besides the question editing form. diff --git a/question/type/upgrade.txt b/question/type/upgrade.txt index dd5545f3723..48cd5e8cd23 100644 --- a/question/type/upgrade.txt +++ b/question/type/upgrade.txt @@ -1,5 +1,15 @@ This files describes API changes for question type plugins. +=== 3.8 === + +* There is a new method for question types get_extra_question_bank_actions. + Assuming the question bank display is using the new 'edit_menu_column' + (which it will be by default) this method lets you add question-type-specific + actions to the menu. The question_type base class has extensive PHPdoc comments + on the method to explain what you should do, and there is an example of how to + use it in a question type at + https://github.com/moodleou/moodle-qtype_pmatch/commit/2aefa8b5dcc7bab768f4707a4ffb7befcf4c2540. + === 3.8, 3.7.3, 3.6.7 === * Coming up in Moodle 3.8 are some changes to the question bank UI. These will break any -- 2.43.0