From 6b7b39fe3ab01f05dca7b3df4a0224d2eb5e3ab7 Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Tue, 7 Mar 2017 16:50:52 +0800 Subject: [PATCH] MDL-58048 calendar: Show itemcount conditionally Part of MDL-55611 epic. --- .../external/event_action_exporter.php | 42 ++++++++++++++++++- calendar/classes/external/event_exporter.php | 7 +++- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/calendar/classes/external/event_action_exporter.php b/calendar/classes/external/event_action_exporter.php index fc3e2914be6..a33ed5914fb 100644 --- a/calendar/classes/external/event_action_exporter.php +++ b/calendar/classes/external/event_action_exporter.php @@ -26,8 +26,10 @@ namespace core_calendar\external; defined('MOODLE_INTERNAL') || die(); -use \core\external\exporter; -use \core_calendar\local\interfaces\action_interface; +use core\external\exporter; +use core_calendar\local\event\core_container; +use core_calendar\local\interfaces\action_interface; +use renderer_base; /** * Class for displaying a calendar event's action. @@ -67,6 +69,41 @@ class event_action_exporter extends exporter { ]; } + /** + * Return the list of additional properties. + * + * @return array + */ + protected static function define_other_properties() { + return [ + 'showitemcount' => ['type' => PARAM_BOOL, 'default' => false] + ]; + } + + /** + * Get the additional values to inject while exporting. + * + * @param renderer_base $output The renderer. + * @return array Keys are the property names, values are their values. + */ + protected function get_other_values(renderer_base $output) { + $event = $this->related['event']; + + $modulename = $event->get_course_module()->get('modname'); + $component = 'mod_' . $modulename; + $showitemcountcallback = 'core_calendar_event_action_shows_item_count'; + $mapper = core_container::get_event_mapper(); + $calevent = $mapper->from_event_to_legacy_event($event); + $params = [$calevent, $this->data->itemcount]; + $showitemcount = component_callback($component, $showitemcountcallback, $params, false); + + // Prepare other values data. + $data = [ + 'showitemcount' => $showitemcount + ]; + return $data; + } + /** * Returns a list of objects that are related. * @@ -75,6 +112,7 @@ class event_action_exporter extends exporter { protected static function define_related() { return [ 'context' => 'context', + 'event' => '\\core_calendar\\local\\interfaces\\event_interface' ]; } } diff --git a/calendar/classes/external/event_exporter.php b/calendar/classes/external/event_exporter.php index 67213e8a680..10530e4ed74 100644 --- a/calendar/classes/external/event_exporter.php +++ b/calendar/classes/external/event_exporter.php @@ -189,8 +189,11 @@ class event_exporter extends exporter { $values['icon'] = $iconexporter->export($output); if ($event instanceof action_event_interface) { - $actionexporter = new event_action_exporter($event->get_action(), - ['context' => $context]); + $actionrelated = [ + 'context' => $context, + 'event' => $event + ]; + $actionexporter = new event_action_exporter($event->get_action(), $actionrelated); $values['action'] = $actionexporter->export($output); }