MDL-81227 question bank: improve the order of the action menu

This commit is contained in:
Tim Hunt 2024-03-13 18:05:55 +00:00
parent 757be30c39
commit f17d5783e1
10 changed files with 76 additions and 0 deletions

View File

@ -75,6 +75,10 @@ class delete_action extends question_action_base {
}
}
public function get_menu_position(): int {
return 400;
}
protected function get_url_icon_and_label(\stdClass $question): array {
if (!question_has_capability_on($question, 'edit')) {
return [null, null, null];

View File

@ -57,6 +57,10 @@ class copy_action extends question_action_base {
}
}
public function get_menu_position(): int {
return 250;
}
/**
* Get the URL for duplicating a question as a moodle_url.
*

View File

@ -67,6 +67,10 @@ class edit_action extends question_action_base {
}
}
public function get_menu_position(): int {
return 200;
}
/**
* Get the URL for editing a question as a link.
*

View File

@ -36,6 +36,10 @@ class export_xml_action extends question_action_base {
$this->strexportasxml = get_string('exportasxml', 'question');
}
public function get_menu_position(): int {
return 600;
}
protected function get_url_icon_and_label(\stdClass $question): array {
if (!\question_bank::is_qtype_installed($question->qtype)) {
// It sometimes happens that people end up with junk questions

View File

@ -36,6 +36,10 @@ class history_action extends question_action_base {
$this->strpreview = get_string('history_action', 'qbank_history');
}
public function get_menu_position(): int {
return 500;
}
protected function get_url_icon_and_label(\stdClass $question): array {
if (!\question_bank::is_qtype_installed($question->qtype)) {
// It sometimes happens that people end up with junk questions

View File

@ -38,6 +38,10 @@ class preview_action extends question_action_base {
$this->strpreview = get_string('preview');
}
public function get_menu_position(): int {
return 100;
}
protected function get_url_icon_and_label(\stdClass $question): array {
if (!\question_bank::is_qtype_installed($question->qtype)) {
// It sometimes happens that people end up with junk questions

View File

@ -48,6 +48,10 @@ class tags_action extends question_action_base {
$this->managetags = get_string('managetags', 'tag');
}
public function get_menu_position(): int {
return 300;
}
protected function check_tags_status(): void {
global $CFG;
if (!$CFG->usetags) {

View File

@ -1,6 +1,13 @@
This file describes core qbank plugin changes in /question/bank/*,
information provided here is intended especially for developers.
=== 4.4 ===
* Question bank actions (anything that subclasses question_action_base) should implement the
method get_menu_position() to control what position in the action menu it appears it.
See the comment on the base class method for more details. If you don't do this, you will
get a debugging warning.
=== 4.3 ===
* The helper class in qbank_statistics has had several of its methods deprecated.

View File

@ -375,8 +375,22 @@ class view {
$menuactions = $plugin->get_question_actions($this);
foreach ($menuactions as $menuaction) {
$this->questionactions[$menuaction::class] = $menuaction;
if ($menuaction->get_menu_position() === question_action_base::MENU_POSITION_NOT_SET) {
debugging('Question bank actions must define the get_menu_position method. ' .
$menuaction::class . ' does not.', DEBUG_DEVELOPER);
}
}
}
// Sort according to each action's desired position.
// Note, we are relying on the sort to be stable for
// equal values of get_menu_position.
uasort(
$this->questionactions,
function (question_action_base $a, question_action_base $b) {
return $a->get_menu_position() <=> $b->get_menu_position();
},
);
}
/**

View File

@ -25,6 +25,8 @@ namespace core_question\local\bank;
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class view_component {
/** @var int value we return from get_menu_position here. Subclasses should override this. */
const MENU_POSITION_NOT_SET = 6666;
/** @var view Question bank view. */
protected $qbank;
@ -45,6 +47,31 @@ abstract class view_component {
protected function init(): void {
}
/**
* Return an integer to indicate the desired position in the menu for this link, smaller at the top.
*
* The standard menu items in Moodle core return these numbers:
* 100 preview_action
* 200 edit_action
* 250 copy_action
* 300 tags_action
* 400 delete_action
* 500 history_action
* 600 export_xml_action
* (So, if you want your action at a particular place in the order, there should be space.)
*
* If two actions get the same order number, then the tie-break on the sort
* is plugin name, then the order returned by get_question_actions for that plugin.
*
* @return int desired position. Smallest at the top.
*/
public function get_menu_position(): int {
// We return a big number by default, which is after all the standard core links,
// so they go first. This should be overridden by all plugins, and not overriding will
// generate a debugging warning from {@see \core_question\local\bank\view::init_question_actions()}.
return self::MENU_POSITION_NOT_SET;
}
/**
* Return an array 'table_alias' => 'JOIN clause' to bring in any data that
* this feature requires.