MDL-74913 core_reportbuilder: Add divider to the report action menu

This commit is contained in:
Carlos Castillo
2022-10-03 09:48:43 +02:00
parent 24f97edd91
commit a930def22b
3 changed files with 43 additions and 6 deletions
+14 -2
View File
@@ -18,6 +18,7 @@ declare(strict_types=1);
namespace core_reportbuilder;
use action_menu_filler;
use coding_exception;
use stdClass;
use core_reportbuilder\local\models\report;
@@ -43,7 +44,7 @@ abstract class system_report extends base {
/** @var bool $filterformdefault Whether to use the default filters form */
private $filterformdefault = true;
/** @var action[] $actions */
/** @var action|action_menu_filler[] $actions */
private $actions = [];
/** @var column $initialsortcolumn */
@@ -150,6 +151,17 @@ abstract class system_report extends base {
$this->actions[] = $action;
}
/**
* Adds action divider to the report
*
*/
final public function add_action_divider(): void {
$divider = new action_menu_filler();
// We need to set as not primary action because we just need add an action divider, not a new action item.
$divider->primary = false;
$this->actions[] = $divider;
}
/**
* Whether report has any actions
*
@@ -162,7 +174,7 @@ abstract class system_report extends base {
/**
* Return report actions
*
* @return action[]
* @return action|action_menu_filler[]
*/
final public function get_actions(): array {
return $this->actions;
@@ -19,6 +19,7 @@ declare(strict_types=1);
namespace core_reportbuilder\table;
use action_menu;
use action_menu_filler;
use core_table\local\filter\filterset;
use html_writer;
use moodle_exception;
@@ -223,9 +224,33 @@ class system_report_table extends base_report_table {
$menu = new action_menu();
$menu->set_menu_trigger($OUTPUT->pix_icon('a/setting', get_string('actions', 'core_reportbuilder')));
foreach ($this->report->get_actions() as $action) {
// Ensure the action link can be displayed for the current row.
$actionlink = $action->get_action_link($row);
$actions = array_filter($this->report->get_actions(), function($action) use ($row) {
// Only return dividers and action items who can be displayed for current users.
return $action instanceof action_menu_filler || $action->get_action_link($row);
});
$totalactions = count($actions);
$actionvalues = array_values($actions);
foreach ($actionvalues as $position => $action) {
if ($action instanceof action_menu_filler) {
$ispreviousdivider = array_key_exists($position - 1, $actionvalues) &&
($actionvalues[$position - 1] instanceof action_menu_filler);
$isnextdivider = array_key_exists($position + 1, $actionvalues) &&
($actionvalues[$position + 1] instanceof action_menu_filler);
$isfirstdivider = ($position === 0);
$islastdivider = ($position === $totalactions - 1);
// Avoid add divider at last/first position and having multiple fillers in a row.
if ($ispreviousdivider || $isnextdivider || $isfirstdivider || $islastdivider) {
continue;
}
$actionlink = $action;
} else {
// Ensure the action link can be displayed for the current row.
$actionlink = $action->get_action_link($row);
}
if ($actionlink) {
$menu->add($actionlink);
}
+1 -1
View File
@@ -2,7 +2,7 @@ This file describes API changes in /reportbuilder/*
Information provided here is intended especially for developers.
=== 4.1 ===
* New method `add_action_divider()` in base system report class, to allow adding a divider to the action menu.
* New external method `core_reportbuilder_set_filters` for setting report filter values (plus `setFilters` AJAX repository
export for calling from Javascript modules)
* New method `set_filter_form_default` in base system report class, to override whether the default filters form