From 0c07c1e92e66ad1858e712703409181af5e8a711 Mon Sep 17 00:00:00 2001 From: Julio Date: Fri, 13 Jul 2018 11:00:31 +0200 Subject: [PATCH] Add personal events option in plugins see BT#14608 In order to load plugin calendar events as personal events, the plugin has to impement the getPersonalEvents() function and set a the variable $hasPersonalEvents to true in the implementation of the "Plugin" class. --- main/inc/lib/agenda.lib.php | 57 ++++++------ main/inc/lib/plugin.class.php | 1 + main/template/default/agenda/month.tpl | 1 - plugin/lp_calendar/LpCalendarPlugin.php | 116 ++++++++++++++++++++++++ 4 files changed, 148 insertions(+), 27 deletions(-) diff --git a/main/inc/lib/agenda.lib.php b/main/inc/lib/agenda.lib.php index 3fb6a5885a..1db03e2bbb 100644 --- a/main/inc/lib/agenda.lib.php +++ b/main/inc/lib/agenda.lib.php @@ -165,7 +165,7 @@ class Agenda */ public function setSessionId($id) { - $this->sessionId = intval($id); + $this->sessionId = (int) $id; } /** @@ -1514,18 +1514,16 @@ class Agenda */ public function getPersonalEvents($start, $end) { - $start = intval($start); - $end = intval($end); + $start = (int) $start; + $end = (int) $end; $startCondition = ''; $endCondition = ''; if ($start !== 0) { - $start = api_get_utc_datetime($start); - $startCondition = "AND date >= '".$start."'"; + $startCondition = "AND date >= '".api_get_utc_datetime($start)."'"; } if ($start !== 0) { - $end = api_get_utc_datetime($end); - $endCondition = "AND (enddate <= '".$end."' OR enddate IS NULL)"; + $endCondition = "AND (enddate <= '".api_get_utc_datetime($end)."' OR enddate IS NULL)"; } $user_id = api_get_user_id(); @@ -1547,16 +1545,12 @@ class Agenda if (!empty($row['date'])) { $event['start'] = $this->formatEventDate($row['date']); - $event['start_date_localtime'] = api_get_local_time( - $row['date'] - ); + $event['start_date_localtime'] = api_get_local_time($row['date']); } if (!empty($row['enddate'])) { $event['end'] = $this->formatEventDate($row['enddate']); - $event['end_date_localtime'] = api_get_local_time( - $row['enddate'] - ); + $event['end_date_localtime'] = api_get_local_time($row['enddate']); } $event['description'] = $row['text']; @@ -1569,6 +1563,21 @@ class Agenda } } + // Add plugin personal events + + $this->plugin = new AppPlugin(); + $plugins = $this->plugin->getInstalledPluginListObject(); + /** @var Plugin $plugin */ + foreach ($plugins as $plugin) { + if ($plugin->hasPersonalEvents && method_exists($plugin, 'getPersonalEvents')) { + $pluginEvents = $plugin->getPersonalEvents($this, $start, $end); + + if (!empty($pluginEvents)) { + $this->events = array_merge($this->events, $pluginEvents); + } + } + } + return $my_events; } @@ -1973,18 +1982,12 @@ class Agenda } if (!empty($row['start_date'])) { - $event['start'] = $this->formatEventDate( - $row['start_date'] - ); - $event['start_date_localtime'] = api_get_local_time( - $row['start_date'] - ); + $event['start'] = $this->formatEventDate($row['start_date']); + $event['start_date_localtime'] = api_get_local_time($row['start_date']); } if (!empty($row['end_date'])) { $event['end'] = $this->formatEventDate($row['end_date']); - $event['end_date_localtime'] = api_get_local_time( - $row['end_date'] - ); + $event['end_date_localtime'] = api_get_local_time($row['end_date']); } $event['sent_to'] = ''; @@ -2099,7 +2102,7 @@ class Agenda $event['editable'] = false; $event['type'] = 'admin'; - if (api_is_platform_admin() && $this->type == 'admin') { + if (api_is_platform_admin() && $this->type === 'admin') { $event['editable'] = true; } @@ -3508,7 +3511,8 @@ class Agenda } /** - * This function retrieves all the personal agenda items and add them to the agenda items found by the other functions. + * This function retrieves all the personal agenda items and add them to the agenda items found by the other + * functions. */ public static function get_personal_agenda_items( $user_id, @@ -3631,7 +3635,8 @@ class Agenda * @param array Agendaitems * @param int Month number * @param int Year number - * @param array Array of strings containing long week day names (deprecated, you can send an empty array instead) + * @param array Array of strings containing long week day names (deprecated, you can send an empty array + * instead) * @param string The month name */ public static function display_mymonthcalendar( @@ -4109,7 +4114,7 @@ class Agenda * * @return bool|string */ - private function formatEventDate($utcTime) + public function formatEventDate($utcTime) { $utcTimeZone = new DateTimeZone('UTC'); $platformTimeZone = new DateTimeZone(api_get_timezone()); diff --git a/main/inc/lib/plugin.class.php b/main/inc/lib/plugin.class.php index 6b993865ae..d968051cb6 100755 --- a/main/inc/lib/plugin.class.php +++ b/main/inc/lib/plugin.class.php @@ -25,6 +25,7 @@ class Plugin public $isMailPlugin = false; // Adds icon in the course home public $addCourseTool = true; + public $hasPersonalEvents = false; /** * When creating a new course, these settings are added to the course, in diff --git a/main/template/default/agenda/month.tpl b/main/template/default/agenda/month.tpl index d0faac5301..c14d8e97c6 100755 --- a/main/template/default/agenda/month.tpl +++ b/main/template/default/agenda/month.tpl @@ -395,7 +395,6 @@ $(document).ready(function() { } var onHoverInfo = ''; - {% if on_hover_info.description %} if (event.description) { onHoverInfo = event.description; diff --git a/plugin/lp_calendar/LpCalendarPlugin.php b/plugin/lp_calendar/LpCalendarPlugin.php index ecce3fc91b..8b21027d26 100644 --- a/plugin/lp_calendar/LpCalendarPlugin.php +++ b/plugin/lp_calendar/LpCalendarPlugin.php @@ -13,6 +13,7 @@ class LpCalendarPlugin extends Plugin { const EVENT_TYPE_TAKEN = 1; const EVENT_TYPE_EXAM = 2; + public $hasPersonalEvents = true; /** * Class constructor @@ -24,6 +25,9 @@ class LpCalendarPlugin extends Plugin parent::__construct($version, $author, ['enabled' => 'boolean']); } + /** + * @return array + */ public static function getEventTypeList() { return [ @@ -203,7 +207,48 @@ class LpCalendarPlugin extends Plugin } return $list; + } + + /** + * @param array $calendarInfo + * @param int $start + * @param int $end + * + * @return array + */ + public static function getCalendarsEventsByDate($calendarInfo, $start, $end) + { + if (empty($calendarInfo)) { + return []; + } + $calendarId = (int) $calendarInfo['id']; + $start = (int) $start; + $end = (int) $end; + + $startCondition = ''; + $endCondition = ''; + + if ($start !== 0) { + $start = api_get_utc_datetime($start); + $startCondition = "AND start_date >= '".$start."'"; + } + if ($start !== 0) { + $end = api_get_utc_datetime($end); + $endCondition = "AND (end_date <= '".$end."' OR end_date IS NULL)"; + } + + $sql = "SELECT * FROM learning_calendar_events + WHERE calendar_id = $calendarId $startCondition $endCondition "; + + $result = Database::query($sql); + $list = []; + $link = api_get_path(WEB_PLUGIN_PATH).'lp_calendar/start.php'; + while ($row = Database::fetch_array($result, 'ASSOC')) { + $list[] = $row; + } + + return ['calendar' => $calendarInfo, 'events' => $list]; } /** @@ -291,6 +336,25 @@ class LpCalendarPlugin extends Plugin return $item; } + /** + * @param int $userId + * @param int $start + * @param int $end + * + * @return array + */ + public static function getUserEvents($userId, $start, $end) + { + $calendarRelUser = self::getUserCalendar($userId); + if (!empty($calendarRelUser)) { + $calendar = self::getCalendar($calendarRelUser['calendar_id']); + + return self::getCalendarsEventsByDate($calendar, $start, $end); + } + + return []; + } + /** * @param int $userId * @@ -390,4 +454,56 @@ class LpCalendarPlugin extends Plugin $form->addText('minutes_per_day', get_lang('MinutesPerDay')); $form->addHtmlEditor('description', get_lang('Description'), false); } + + /** + * @param int $start + * @param int $end + * + * @return array + */ + public function getPersonalEvents($calendar, $start, $end) + { + $userId = api_get_user_id(); + $events = self::getUserEvents($userId, $start, $end); + + if (empty($events)) { + return []; + } + + $calendarInfo = $events['calendar']; + $events = $events['events']; + + $list = []; + $typeList = self::getEventTypeList(); + foreach ($events as $row) { + $event['id'] = 'personal_'.$row['id']; + $event['title'] = $calendarInfo['title']; + $event['className'] = 'personal'; + $color = isset($typeList[$row['type']]) ? $typeList[$row['type']] : 'green'; + $event['borderColor'] = $color; + $event['backgroundColor'] = $color; + + $event['editable'] = false; + $event['sent_to'] = get_lang('Me'); + $event['type'] = 'personal'; + + if (!empty($row['start_date'])) { + $event['start'] = $calendar->formatEventDate($row['start_date']); + $event['start_date_localtime'] = api_get_local_time($row['start_date']); + } + + if (!empty($row['end_date'])) { + $event['end'] = $calendar->formatEventDate($row['end_date']); + $event['end_date_localtime'] = api_get_local_time($row['end_date']); + } + + $event['description'] = 'plugin'; + $event['allDay'] = 1; + $event['parent_event_id'] = 0; + $event['has_children'] = 0; + $list[] = $event; + } + + return $list; + } }