From a930def22bea7d57df6ac609e3c9a17b61d4cbce Mon Sep 17 00:00:00 2001 From: Carlos Castillo Date: Mon, 8 Aug 2022 10:55:11 -0500 Subject: [PATCH] MDL-74913 core_reportbuilder: Add divider to the report action menu --- reportbuilder/classes/system_report.php | 16 ++++++++-- .../classes/table/system_report_table.php | 31 +++++++++++++++++-- reportbuilder/upgrade.txt | 2 +- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/reportbuilder/classes/system_report.php b/reportbuilder/classes/system_report.php index 7b61948eabc..80cae63640d 100644 --- a/reportbuilder/classes/system_report.php +++ b/reportbuilder/classes/system_report.php @@ -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; diff --git a/reportbuilder/classes/table/system_report_table.php b/reportbuilder/classes/table/system_report_table.php index 0e35ac8b19b..d1ad21811e1 100644 --- a/reportbuilder/classes/table/system_report_table.php +++ b/reportbuilder/classes/table/system_report_table.php @@ -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); } diff --git a/reportbuilder/upgrade.txt b/reportbuilder/upgrade.txt index f16934e7564..9b311f4aa5c 100644 --- a/reportbuilder/upgrade.txt +++ b/reportbuilder/upgrade.txt @@ -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