Skip to content

Commit

Permalink
Calendar: Fix edit reminders already sent - refs BT#19392
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Dec 23, 2021
1 parent 76a91ed commit 67df1a3
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 17 deletions.
1 change: 0 additions & 1 deletion main/calendar/agenda.php
Expand Up @@ -106,7 +106,6 @@ function add_image_form() {
\'<option value="i">'.get_lang('Minutes').'</option>\' +
\'<option value="h">'.get_lang('Hours').'</option>\' +
\'<option value="d">'.get_lang('Days').'</option>\' +
\'<option value="w">'.get_lang('Weeks').'</option>\' +
\'</select>\' +
\'</div>\' +
\'<div class="col-sm-2"><p class="form-control-static">'.get_lang('Before').'</p></div>\' +
Expand Down
99 changes: 84 additions & 15 deletions main/inc/lib/agenda.lib.php
Expand Up @@ -478,9 +478,6 @@ public function addReminder($eventId, $count, $period)
case 'd':
$dateInterval = DateInterval::createFromDateString("$count days");
break;
case 'w':
$dateInterval = DateInterval::createFromDateString("$count weeks");
break;
default:
return null;
}
Expand All @@ -497,16 +494,65 @@ public function addReminder($eventId, $count, $period)
$em->flush();
}

public function removeReminders(int $eventId)
public function removeReminders(int $eventId, int $count, string $period)
{
switch ($period) {
case 'i':
$dateInterval = DateInterval::createFromDateString("$count minutes");
break;
case 'h':
$dateInterval = DateInterval::createFromDateString("$count hours");
break;
case 'd':
$dateInterval = DateInterval::createFromDateString("$count days");
break;
default:
return null;
}

Database::getManager()
->createQuery(
'DELETE FROM ChamiloCoreBundle:AgendaReminder ar WHERE ar.eventId = :eventId AND ar.type = :type'
'DELETE FROM ChamiloCoreBundle:AgendaReminder ar
WHERE ar.eventId = :eventId AND ar.type = :type AND ar.dateInterval = :dateInterval'
)
->setParameters(
[
'eventId' => $eventId,
'type' => $this->type,
'dateInterval' => $dateInterval,
]
)
->setParameters(['eventId' => $eventId, 'type' => $this->type])
->execute();
}

public function getReminder(int $eventId, int $count, string $period)
{
switch ($period) {
case 'i':
$dateInterval = DateInterval::createFromDateString("$count minutes");
break;
case 'h':
$dateInterval = DateInterval::createFromDateString("$count hours");
break;
case 'd':
$dateInterval = DateInterval::createFromDateString("$count days");
break;
default:
return null;
}

$em = Database::getManager();
$remindersRepo = $em->getRepository('ChamiloCoreBundle:AgendaReminder');

return $remindersRepo->findOneBy(
[
'type' => $this->type,
'dateInterval' => $dateInterval,
'eventId' => $eventId,
]
);
}

/**
* @param int $eventId
* @param int $courseId
Expand Down Expand Up @@ -810,7 +856,7 @@ public function editEvent(
$authorId = 0,
array $inviteesList = [],
bool $isCollective = false,
array $reminders = []
array $remindersList = []
) {
$id = (int) $id;
$start = api_get_utc_datetime($start);
Expand Down Expand Up @@ -1129,13 +1175,7 @@ public function editEvent(
break;
}

if (api_get_configuration_value('agenda_reminders')) {
$this->removeReminders($id);

foreach ($reminders as $reminder) {
$this->addReminder($id, $reminder[0], $reminder[1]);
}
}
$this->editReminders($id, $remindersList);
}

/**
Expand Down Expand Up @@ -2512,6 +2552,12 @@ public function parseSendToArray($to)
return $sendTo;
}

/**
* @param int $eventId
* @param string $type
*
* @return array<int, AgendaReminder>
*/
public function getEventReminders($eventId, $type = null): array
{
$em = Database::getManager();
Expand Down Expand Up @@ -2860,7 +2906,6 @@ public function addFieldsForRemindersToForm(int $eventId, FormValidator $form)
'i' => get_lang('Minutes'),
'h' => get_lang('Hours'),
'd' => get_lang('Days'),
'w' => get_lang('Weeks'),
]
)
->setValue($reminderInfo['date_interval'][1])
Expand Down Expand Up @@ -4505,6 +4550,30 @@ public static function saveCollectiveProperties(array $inviteeUserList, bool $is
$em->flush();
}

private function editReminders(int $eventId, array $reminderList = [])
{
if (false === api_get_configuration_value('agenda_reminders')) {
return;
}

$eventReminders = $this->parseEventReminders(
$this->getEventReminders($eventId)
);
$eventIntervalList = array_column($eventReminders, 'date_interval');

foreach ($eventIntervalList as $eventIntervalInfo) {
if (!in_array($eventIntervalInfo, $reminderList)) {
$this->removeReminders($eventId, $eventIntervalInfo[0], $eventIntervalInfo[1]);
}
}

foreach ($reminderList as $reminderInfo) {
if (!in_array($reminderInfo, $eventIntervalList)) {
$this->addReminder($eventId, $reminderInfo[0], $reminderInfo[1]);
}
}
}

private static function isUserInvitedInEvent(int $id, int $userId): bool
{
$user = api_get_user_entity($userId);
Expand Down
1 change: 0 additions & 1 deletion main/template/default/agenda/month.tpl
Expand Up @@ -937,7 +937,6 @@ $(function() {
'<option value="i">{{ 'Minutes'|get_lang }}</option>' +
'<option value="h">{{ 'Hours'|get_lang }}</option>' +
'<option value="d">{{ 'Days'|get_lang }}</option>' +
'<option value="w">{{ 'Weeks'|get_lang }}</option>' +
'</select>' +
'</div>' +
'<div class="col-sm-2"><p class="form-control-static">{{ 'Before'|get_lang }}</p></div>' +
Expand Down

0 comments on commit 67df1a3

Please sign in to comment.