mirror of
https://github.com/moodle/moodle.git
synced 2025-08-09 10:56:56 +02:00
MDL-57730 core_calendar: Allow modules to control event visibility
By implementing the mod_{modname}_core_calendar_is_event_visible callback a module can decide whether or not a user should see an event. Part of MDL-55611 epic.
This commit is contained in:
parent
5df117d438
commit
2a5cce61ba
5 changed files with 185 additions and 45 deletions
|
@ -74,14 +74,13 @@ class core_container {
|
|||
*/
|
||||
private static function init() {
|
||||
if (empty(self::$eventfactory)) {
|
||||
$identity = function(event_interface $event) {
|
||||
return $event;
|
||||
};
|
||||
self::$actionfactory = new action_factory();
|
||||
self::$actioneventfactory = new action_event_factory();
|
||||
self::$eventmapper = new event_mapper(
|
||||
new event_factory(
|
||||
function(event_interface $event) {
|
||||
return $event;
|
||||
}
|
||||
)
|
||||
new event_factory($identity, $identity)
|
||||
);
|
||||
self::$eventfactory = new event_factory(
|
||||
function(event_interface $event) {
|
||||
|
@ -96,6 +95,23 @@ class core_container {
|
|||
);
|
||||
|
||||
return $action ? self::$actioneventfactory->create_instance($event, $action) : $event;
|
||||
},
|
||||
function(event_interface $event) {
|
||||
$mapper = self::$eventmapper;
|
||||
$eventvisible = component_callback(
|
||||
'mod_' . $event->get_course_module()->get('modname'),
|
||||
'core_calendar_is_event_visible',
|
||||
[
|
||||
$mapper->from_event_to_legacy_event($event)
|
||||
]
|
||||
);
|
||||
|
||||
// Module does not implement the callback, event should be visible.
|
||||
if (is_null($eventvisible)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $eventvisible ? true : false;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -43,9 +43,14 @@ use core_calendar\local\interfaces\event_interface;
|
|||
*/
|
||||
abstract class event_abstract_factory implements event_factory_interface {
|
||||
/**
|
||||
* @var callable $callbackapplier Function to apply component callbacks.
|
||||
* @var callable $actioncallbackapplier Function to apply component action callbacks.
|
||||
*/
|
||||
protected $callbackapplier;
|
||||
protected $actioncallbackapplier;
|
||||
|
||||
/**
|
||||
* @var callable $visibilitycallbackapplier Function to apply component visibility callbacks.
|
||||
*/
|
||||
protected $visibilitycallbackapplier;
|
||||
|
||||
/**
|
||||
* Applies component actions to the event.
|
||||
|
@ -55,13 +60,23 @@ abstract class event_abstract_factory implements event_factory_interface {
|
|||
*/
|
||||
protected abstract function apply_component_action(event_interface $event);
|
||||
|
||||
/**
|
||||
* Exposes the event (or not)
|
||||
*
|
||||
* @param event_interface $event The event to potentially expose.
|
||||
* @return event_interface|null The exposed event or null.
|
||||
*/
|
||||
protected abstract function expose_event(event_interface $event);
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param callable $callbackapplier Function to apply component callbacks.
|
||||
* @param callable $actioncallbackapplier Function to apply component action callbacks.
|
||||
* @param callable $visibilitycallbackapplier Function to apply component visibility callbacks.
|
||||
*/
|
||||
public function __construct(callable $callbackapplier) {
|
||||
$this->callbackapplier = $callbackapplier;
|
||||
public function __construct(callable $actioncallbackapplier, callable $visibilitycallbackapplier) {
|
||||
$this->actioncallbackapplier = $actioncallbackapplier;
|
||||
$this->visibilitycallbackapplier = $visibilitycallbackapplier;
|
||||
}
|
||||
|
||||
public function create_instance(\stdClass $dbrow) {
|
||||
|
@ -108,25 +123,27 @@ abstract class event_abstract_factory implements event_factory_interface {
|
|||
});
|
||||
}
|
||||
|
||||
return $this->apply_component_action(
|
||||
new event(
|
||||
$dbrow->id,
|
||||
$dbrow->name,
|
||||
new event_description($dbrow->description, $dbrow->format),
|
||||
$course,
|
||||
$group,
|
||||
$user,
|
||||
new repeat_event_collection($dbrow->id, $this),
|
||||
$module,
|
||||
$dbrow->eventtype,
|
||||
new event_times(
|
||||
(new \DateTimeImmutable())->setTimestamp($dbrow->timestart),
|
||||
(new \DateTimeImmutable())->setTimestamp($dbrow->timestart + $dbrow->timeduration),
|
||||
(new \DateTimeImmutable())->setTimestamp($dbrow->timesort ? $dbrow->timesort : $dbrow->timestart),
|
||||
(new \DateTimeImmutable())->setTimestamp($dbrow->timemodified)
|
||||
),
|
||||
!empty($dbrow->visible),
|
||||
$subscription
|
||||
return $this->expose_event(
|
||||
$this->apply_component_action(
|
||||
new event(
|
||||
$dbrow->id,
|
||||
$dbrow->name,
|
||||
new event_description($dbrow->description, $dbrow->format),
|
||||
$course,
|
||||
$group,
|
||||
$user,
|
||||
new repeat_event_collection($dbrow->id, $this),
|
||||
$module,
|
||||
$dbrow->eventtype,
|
||||
new event_times(
|
||||
(new \DateTimeImmutable())->setTimestamp($dbrow->timestart),
|
||||
(new \DateTimeImmutable())->setTimestamp($dbrow->timestart + $dbrow->timeduration),
|
||||
(new \DateTimeImmutable())->setTimestamp($dbrow->timesort ? $dbrow->timesort : $dbrow->timestart),
|
||||
(new \DateTimeImmutable())->setTimestamp($dbrow->timemodified)
|
||||
),
|
||||
!empty($dbrow->visible),
|
||||
$subscription
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
|
@ -38,17 +38,25 @@ use core_calendar\local\interfaces\event_interface;
|
|||
*/
|
||||
class event_factory extends event_abstract_factory {
|
||||
protected function apply_component_action(event_interface $event) {
|
||||
if (!$event->get_course_module()) {
|
||||
return $event;
|
||||
}
|
||||
|
||||
$callbackapplier = $this->callbackapplier;
|
||||
$callbackapplier = $this->actioncallbackapplier;
|
||||
$callbackresult = $callbackapplier($event);
|
||||
|
||||
if (!$callbackresult instanceof event_interface) {
|
||||
throw new invalid_callback_exception('Event factory callback applier must return an instance of action_interface');
|
||||
throw new invalid_callback_exception(
|
||||
'Event factory action callback applier must return an instance of event_interface');
|
||||
}
|
||||
|
||||
return $callbackresult;
|
||||
}
|
||||
|
||||
protected function expose_event(event_interface $event) {
|
||||
$callbackapplier = $this->visibilitycallbackapplier;
|
||||
$callbackresult = $callbackapplier($event);
|
||||
|
||||
if (!is_bool($callbackresult)) {
|
||||
throw new invalid_callback_exception('Event factory visibility callback applier must return true or false');
|
||||
}
|
||||
|
||||
return $callbackresult === true ? $event : null;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue