From c3245f6f52014cfbe35597c2d03e178c89b3acd1 Mon Sep 17 00:00:00 2001 From: Safat Shahin Date: Mon, 15 Aug 2022 12:33:27 +1000 Subject: [PATCH 1/2] MDL-75126 core_question: Allow multiple bulk action from qbank plugins This commit implements the bulk action api to allow multiple bulk actions from the qbank plugins instead of one. Any qbank plugin wants to implement bulk action can now define an array of bulk actions as a plugin feature. --- .../bulkmove/classes/bulk_move_action.php | 2 +- .../bank/bulkmove/classes/plugin_feature.php | 6 ++-- .../classes/bulk_delete_action.php | 2 +- .../deletequestion/classes/plugin_feature.php | 6 ++-- .../classes/local/bank/bulk_action_base.php | 22 ++++++++++++- .../local/bank/plugin_features_base.php | 6 ++-- question/classes/local/bank/view.php | 31 +++++++++++-------- question/upgrade.txt | 5 +++ 8 files changed, 57 insertions(+), 23 deletions(-) diff --git a/question/bank/bulkmove/classes/bulk_move_action.php b/question/bank/bulkmove/classes/bulk_move_action.php index 60bbf1065d4..ec244a32674 100644 --- a/question/bank/bulkmove/classes/bulk_move_action.php +++ b/question/bank/bulkmove/classes/bulk_move_action.php @@ -30,7 +30,7 @@ class bulk_move_action extends \core_question\local\bank\bulk_action_base { return get_string('movetobulkaction', 'qbank_bulkmove'); } - public function get_bulk_action_key(): string { + public function get_key(): string { return 'move'; } diff --git a/question/bank/bulkmove/classes/plugin_feature.php b/question/bank/bulkmove/classes/plugin_feature.php index 4c9eda185f3..5b324b55dd5 100644 --- a/question/bank/bulkmove/classes/plugin_feature.php +++ b/question/bank/bulkmove/classes/plugin_feature.php @@ -28,7 +28,9 @@ use core_question\local\bank\plugin_features_base; * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class plugin_feature extends plugin_features_base { - public function get_bulk_actions(): ?bulk_action_base { - return new bulk_move_action(); + public function get_bulk_actions(): array { + return [ + new bulk_move_action(), + ]; } } diff --git a/question/bank/deletequestion/classes/bulk_delete_action.php b/question/bank/deletequestion/classes/bulk_delete_action.php index f2fc6931064..303e61e82da 100644 --- a/question/bank/deletequestion/classes/bulk_delete_action.php +++ b/question/bank/deletequestion/classes/bulk_delete_action.php @@ -30,7 +30,7 @@ class bulk_delete_action extends \core_question\local\bank\bulk_action_base { return get_string('delete'); } - public function get_bulk_action_key(): string { + public function get_key(): string { return 'deleteselected'; } diff --git a/question/bank/deletequestion/classes/plugin_feature.php b/question/bank/deletequestion/classes/plugin_feature.php index c11fae01d21..e159313af5e 100644 --- a/question/bank/deletequestion/classes/plugin_feature.php +++ b/question/bank/deletequestion/classes/plugin_feature.php @@ -43,7 +43,9 @@ class plugin_feature extends plugin_features_base { ]; } - public function get_bulk_actions(): ?bulk_action_base { - return new bulk_delete_action(); + public function get_bulk_actions(): array { + return [ + new bulk_delete_action(), + ]; } } diff --git a/question/classes/local/bank/bulk_action_base.php b/question/classes/local/bank/bulk_action_base.php index 775c5a3cd5a..e0fb2599063 100644 --- a/question/classes/local/bank/bulk_action_base.php +++ b/question/classes/local/bank/bulk_action_base.php @@ -44,7 +44,9 @@ abstract class bulk_action_base { * * @return string */ - abstract public function get_bulk_action_key(): string; + public function get_bulk_action_key(): string { + return ''; + } /** * URL of the bulk action redirect page. @@ -68,4 +70,22 @@ abstract class bulk_action_base { public function get_bulk_action_capabilities(): ?array { return null; } + + + /** + * A unique key for the bulk action, this will be used in the api to identify the action data. + * Every bulk must have a unique key to perform the action as a part of the form post in the base view. + * When questions are selected, it will post according to the key its selected from the dropdown. + * + * Note: This method is the first towards moving from get_bulk_action_key() to get_key(). + * + * @return string + */ + public function get_key(): string { + if (!empty($this->get_bulk_action_key())) { + return $this->get_bulk_action_key(); + } + throw new \coding_exception('Bulk actions must implement the get_key() or get_bulk_action_key() method. + In Moodle 4.1, get_bulk_action_key() is being deprecated and replaced by get_key().'); + } } diff --git a/question/classes/local/bank/plugin_features_base.php b/question/classes/local/bank/plugin_features_base.php index de06126dd74..f12729b9dcb 100644 --- a/question/classes/local/bank/plugin_features_base.php +++ b/question/classes/local/bank/plugin_features_base.php @@ -59,10 +59,10 @@ class plugin_features_base { /** * This method will return the array objects for the bulk actions ui. * - * @return null|bulk_action_base + * @return bulk_action_base[] */ - public function get_bulk_actions(): ?bulk_action_base { - return null; + public function get_bulk_actions() { + return []; } } diff --git a/question/classes/local/bank/view.php b/question/classes/local/bank/view.php index ac163272e18..f8895ba4bd6 100644 --- a/question/classes/local/bank/view.php +++ b/question/classes/local/bank/view.php @@ -202,22 +202,27 @@ class view { protected function init_bulk_actions(): void { $plugins = \core_component::get_plugin_list_with_class('qbank', 'plugin_feature', 'plugin_feature.php'); foreach ($plugins as $componentname => $plugin) { - $pluginentrypoint = new $plugin(); - $pluginentrypointobject = $pluginentrypoint->get_bulk_actions(); - // Don't need the plugins without bulk actions. - if ($pluginentrypointobject === null) { - unset($plugins[$componentname]); - continue; - } if (!\core\plugininfo\qbank::is_plugin_enabled($componentname)) { - unset($plugins[$componentname]); continue; } - $this->bulkactions[$pluginentrypointobject->get_bulk_action_key()] = [ - 'title' => $pluginentrypointobject->get_bulk_action_title(), - 'url' => $pluginentrypointobject->get_bulk_action_url(), - 'capabilities' => $pluginentrypointobject->get_bulk_action_capabilities() - ]; + + $pluginentrypoint = new $plugin(); + $bulkactions = $pluginentrypoint->get_bulk_actions(); + if (!is_array($bulkactions)) { + debugging("The method {$componentname}::get_bulk_actions() must return an array of bulk actions instead of + a single bulk action. Please update your implementation of get_bulk_actions() to return an array. Check out the + qbank_bulkmove plugin for a working example.", DEBUG_DEVELOPER); + $bulkactions = [$bulkactions]; + } + + foreach ($bulkactions as $bulkactionobject) { + $this->bulkactions[$bulkactionobject->get_key()] = [ + 'title' => $bulkactionobject->get_bulk_action_title(), + 'url' => $bulkactionobject->get_bulk_action_url(), + 'capabilities' => $bulkactionobject->get_bulk_action_capabilities() + ]; + } + } } diff --git a/question/upgrade.txt b/question/upgrade.txt index 887c93c8e91..61090bed1e2 100644 --- a/question/upgrade.txt +++ b/question/upgrade.txt @@ -1,5 +1,10 @@ This files describes API changes for code that uses the question API. +=== 4.0.5 === + +1) Question bank plugins can now define more than one bulk action. Therefore, plugin_features_base::get_bulk_actions has been + changed to return an array, rather than a single bulk action class. Please update the plugin_features class in your plugin if necessary. + === 4.0 === Moodle 4.0 included the results of a major project to re-work the question bank. From cf7d6131adffe462e71ac86887845c51e3a45615 Mon Sep 17 00:00:00 2001 From: Safat Shahin Date: Thu, 10 Nov 2022 00:33:44 +1100 Subject: [PATCH 2/2] MDL-75126 core_question: Deprecate get_bulk_action_key This commit will deprecate get_bulk_action_key and move the implementation in the get_key method entirely by making this an absract method. --- .../classes/local/bank/bulk_action_base.php | 20 ++++++++----------- question/classes/local/bank/view.php | 7 ++++--- question/upgrade.txt | 4 ++++ 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/question/classes/local/bank/bulk_action_base.php b/question/classes/local/bank/bulk_action_base.php index e0fb2599063..67bc4c0d3f5 100644 --- a/question/classes/local/bank/bulk_action_base.php +++ b/question/classes/local/bank/bulk_action_base.php @@ -44,9 +44,7 @@ abstract class bulk_action_base { * * @return string */ - public function get_bulk_action_key(): string { - return ''; - } + abstract function get_key(): string; /** * URL of the bulk action redirect page. @@ -71,21 +69,19 @@ abstract class bulk_action_base { return null; } - /** * A unique key for the bulk action, this will be used in the api to identify the action data. * Every bulk must have a unique key to perform the action as a part of the form post in the base view. * When questions are selected, it will post according to the key its selected from the dropdown. * - * Note: This method is the first towards moving from get_bulk_action_key() to get_key(). - * * @return string + * @deprecated since Moodle 4.1 + * @see get_key() + * @todo Final deprecation on Moodle 4.5 MDL-72438 */ - public function get_key(): string { - if (!empty($this->get_bulk_action_key())) { - return $this->get_bulk_action_key(); - } - throw new \coding_exception('Bulk actions must implement the get_key() or get_bulk_action_key() method. - In Moodle 4.1, get_bulk_action_key() is being deprecated and replaced by get_key().'); + public function get_bulk_action_key() { + debugging("The method get_bulk_action_key() in bulk_action_base class is deprecated. + Please use the abstract method get_key() instead.", DEBUG_DEVELOPER); + return $this->get_key(); } } diff --git a/question/classes/local/bank/view.php b/question/classes/local/bank/view.php index f8895ba4bd6..08b2df0814a 100644 --- a/question/classes/local/bank/view.php +++ b/question/classes/local/bank/view.php @@ -209,9 +209,10 @@ class view { $pluginentrypoint = new $plugin(); $bulkactions = $pluginentrypoint->get_bulk_actions(); if (!is_array($bulkactions)) { - debugging("The method {$componentname}::get_bulk_actions() must return an array of bulk actions instead of - a single bulk action. Please update your implementation of get_bulk_actions() to return an array. Check out the - qbank_bulkmove plugin for a working example.", DEBUG_DEVELOPER); + debugging("The method {$componentname}::get_bulk_actions() must return an " . + "array of bulk actions instead of a single bulk action. " . + "Please update your implementation of get_bulk_actions() to return an array. " . + "Check out the qbank_bulkmove plugin for a working example.", DEBUG_DEVELOPER); $bulkactions = [$bulkactions]; } diff --git a/question/upgrade.txt b/question/upgrade.txt index 61090bed1e2..d477f1a797c 100644 --- a/question/upgrade.txt +++ b/question/upgrade.txt @@ -1,5 +1,9 @@ This files describes API changes for code that uses the question API. +=== 4.1 === + +1) get_bulk_action_key() in core_question\local\bank\bulk_action_base class is deprecated and renamed to get_key(). + === 4.0.5 === 1) Question bank plugins can now define more than one bulk action. Therefore, plugin_features_base::get_bulk_actions has been