Calendar: Adapt event colors from settings and fix event updates

pull/5294/head
christianbeeznst 2 years ago
parent be08774a2b
commit 5efd8e4070
  1. 24
      assets/vue/views/ccalendarevent/CCalendarEventList.vue
  2. 2
      src/CoreBundle/ApiResource/CalendarEvent.php
  3. 30
      src/CoreBundle/DataTransformer/CalendarEventTransformer.php
  4. 1
      src/CourseBundle/Entity/CCalendarEvent.php

@ -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)

@ -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;

@ -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'];
}
}

@ -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;

Loading…
Cancel
Save