From 5efd8e407041979e2b0614f915af082829427b60 Mon Sep 17 00:00:00 2001 From: christianbeeznst Date: Mon, 25 Mar 2024 20:07:56 -0500 Subject: [PATCH] Calendar: Adapt event colors from settings and fix event updates --- .../ccalendarevent/CCalendarEventList.vue | 24 +++++---------- src/CoreBundle/ApiResource/CalendarEvent.php | 2 ++ .../CalendarEventTransformer.php | 30 ++++++++++++++++++- src/CourseBundle/Entity/CCalendarEvent.php | 1 + 4 files changed, 40 insertions(+), 17 deletions(-) diff --git a/assets/vue/views/ccalendarevent/CCalendarEventList.vue b/assets/vue/views/ccalendarevent/CCalendarEventList.vue index d64a900f9f..9424beac1d 100644 --- a/assets/vue/views/ccalendarevent/CCalendarEventList.vue +++ b/assets/vue/views/ccalendarevent/CCalendarEventList.vue @@ -213,14 +213,7 @@ async function getCalendarEvents({ startStr, endStr }) { const calendarEvents = await cCalendarEventService.findAll({ params }).then((response) => response.json()) return calendarEvents["hydra:member"].map((event) => { - let color = '#007BFF' - if (event.type === 'global') { - color = '#FF0000' - } else if (event.type === 'course') { - color = '#28a745' - } else if (event.type === 'session') { - color = '#800080' - } + let color = event.color || '#007BFF' return { ...event, @@ -267,6 +260,7 @@ const calendarOptions = ref({ selectable: true, eventClick(eventClickInfo) { eventClickInfo.jsEvent.preventDefault() + currentEvent = eventClickInfo.event let event = eventClickInfo.event.toPlainObject() @@ -277,8 +271,6 @@ const calendarOptions = ref({ return } - currentEvent = event - item.value = { ...event.extendedProps } item.value["title"] = event.title @@ -320,7 +312,7 @@ const currentContext = computed(() => { } else { return 'personal' } -}); +}) const allowAction = (eventType) => { const contextRules = { @@ -328,13 +320,13 @@ const allowAction = (eventType) => { course: ['course'], session: ['session'], personal: ['personal'] - }; + } - return contextRules[currentContext.value].includes(eventType); -}; + return contextRules[currentContext.value].includes(eventType) +} -const showEditButton = computed(() => allowAction(item.value.type)); -const showDeleteButton = computed(() => allowAction(item.value.type)); +const showEditButton = computed(() => allowAction(item.value.type)) +const showDeleteButton = computed(() => allowAction(item.value.type)) const cal = ref(null) diff --git a/src/CoreBundle/ApiResource/CalendarEvent.php b/src/CoreBundle/ApiResource/CalendarEvent.php index 189e5f2ad6..74a189cb72 100644 --- a/src/CoreBundle/ApiResource/CalendarEvent.php +++ b/src/CoreBundle/ApiResource/CalendarEvent.php @@ -44,6 +44,8 @@ class CalendarEvent extends AbstractResource public ?ResourceNode $resourceNode = null, ?array $resourceLinkListFromEntity = null, #[Groups(['calendar_event:read'])] + public ?string $color = null, + #[Groups(['calendar_event:read'])] public ?string $type = null, ) { $this->resourceLinkListFromEntity = $resourceLinkListFromEntity; diff --git a/src/CoreBundle/DataTransformer/CalendarEventTransformer.php b/src/CoreBundle/DataTransformer/CalendarEventTransformer.php index 2daf88edfe..8871d5e551 100644 --- a/src/CoreBundle/DataTransformer/CalendarEventTransformer.php +++ b/src/CoreBundle/DataTransformer/CalendarEventTransformer.php @@ -11,6 +11,7 @@ use Chamilo\CoreBundle\ApiResource\CalendarEvent; use Chamilo\CoreBundle\Entity\Session; use Chamilo\CoreBundle\Entity\SessionRelCourse; use Chamilo\CoreBundle\Repository\Node\UsergroupRepository; +use Chamilo\CoreBundle\Settings\SettingsManager; use Chamilo\CourseBundle\Entity\CCalendarEvent; use Chamilo\CourseBundle\Repository\CCalendarEventRepository; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; @@ -21,7 +22,8 @@ class CalendarEventTransformer implements DataTransformerInterface public function __construct( private readonly RouterInterface $router, private readonly UsergroupRepository $usergroupRepository, - private readonly CCalendarEventRepository $calendarEventRepository + private readonly CCalendarEventRepository $calendarEventRepository, + private readonly SettingsManager $settingsManager ) {} public function transform($object, string $to, array $context = []): object @@ -51,6 +53,8 @@ class CalendarEventTransformer implements DataTransformerInterface } $eventType = $this->calendarEventRepository->determineEventType($object); + $color = $this->determineEventColor($eventType); + $calendarEvent = new CalendarEvent( 'calendar_event_'.$object->getIid(), $object->getTitle(), @@ -67,6 +71,7 @@ class CalendarEventTransformer implements DataTransformerInterface $object->getMaxAttendees(), $object->getResourceNode(), $object->getResourceLinkListFromEntity(), + $color ); $calendarEvent->setType($eventType); @@ -99,4 +104,27 @@ class CalendarEventTransformer implements DataTransformerInterface $sessionUrl, ); } + + private function determineEventColor(string $eventType): string + { + $agendaColors = [ + 'platform' => 'red', + 'course' => '#458B00', + 'session' => '#00496D', + 'personal' => 'steel blue', + ]; + + $settingAgendaColors = $this->settingsManager->getSetting('agenda.agenda_colors'); + if (is_array($settingAgendaColors)) { + $agendaColors = array_merge($agendaColors, $settingAgendaColors); + } + + $colorKeyMap = [ + 'global' => 'platform', + ]; + + $colorKey = $colorKeyMap[$eventType] ?? $eventType; + + return $agendaColors[$colorKey] ?? $agendaColors['personal']; + } } diff --git a/src/CourseBundle/Entity/CCalendarEvent.php b/src/CourseBundle/Entity/CCalendarEvent.php index 0e58e21922..1de8928128 100644 --- a/src/CourseBundle/Entity/CCalendarEvent.php +++ b/src/CourseBundle/Entity/CCalendarEvent.php @@ -132,6 +132,7 @@ class CCalendarEvent extends AbstractResource implements ResourceInterface, Stri #[ORM\Column(name: 'comment', type: 'text', nullable: true)] protected ?string $comment = null; + #[Groups(['calendar_event:write', 'calendar_event:read'])] #[ORM\Column(name: 'color', type: 'string', length: 20, nullable: true)] protected ?string $color = null;