From ae8981e9b3de449ab66696f07978ee4db723a565 Mon Sep 17 00:00:00 2001 From: DamienLyon Date: Tue, 6 Jun 2023 10:11:58 +0200 Subject: [PATCH 1/2] Update agenda.lib.php Add function deleteEventIfAlreadyExit Prevent duplicate event to the course calendar when import large outlook calendar --- main/inc/lib/agenda.lib.php | 40 ++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/main/inc/lib/agenda.lib.php b/main/inc/lib/agenda.lib.php index e34d4b0ff1..b19c69c106 100644 --- a/main/inc/lib/agenda.lib.php +++ b/main/inc/lib/agenda.lib.php @@ -326,7 +326,7 @@ class Agenda if (!empty($parentEventId)) { $attributes['parent_event_id'] = $parentEventId; } - + $this->deleteEventIfAlreadyExit($start, $end, $allDay, $title); $senderId = $this->getSenderId(); $sessionId = $this->getSessionId(); @@ -497,6 +497,44 @@ class Agenda return $id; } + + /** + * @param string $start datetime format: 2012-06-14 09:00:00 in local time + * @param string $end datetime format: 2012-06-14 09:00:00 in local time + * @param string $allDay (true, false) + * @param string $title + * + * Prevent duplicate event to the course calendar + * + * @return bool + */ + public function deleteEventIfAlreadyExit( + $start, + $end, + $allDay, + $title + ) { + $courseId = $this->course['real_id']; + $start = Database::escape_string($start); + $end = Database::escape_string($end); + $allDay = (int) $allDay; + $title = Database::escape_string($title); + $sql = "SELECT id FROM ".$this->tbl_course_agenda." + WHERE c_id = $courseId + AND start_date = '$start' + AND end_date = '$end' + AND all_day = $allDay + AND title = '$title'"; + $res = Database::query($sql); + if (Database::num_rows($res) > 0) { + $row = Database::fetch_array($res, 'ASSOC'); + $id = $row['id']; + $this->deleteEvent($id); + return true; + } + + return false; + } /** * @throws Exception From 78fdb229efce3f7e03cd4c50d75ec4f3503c0201 Mon Sep 17 00:00:00 2001 From: Yannick Warnier Date: Fri, 21 Jul 2023 10:08:16 +0200 Subject: [PATCH 2/2] Calendar: Add session management to deleteEventIfAlreadyExists() (and rename method to fix English typo). Fix code conventions - refs #4737 --- main/inc/lib/agenda.lib.php | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/main/inc/lib/agenda.lib.php b/main/inc/lib/agenda.lib.php index b19c69c106..cc0fae2d3e 100644 --- a/main/inc/lib/agenda.lib.php +++ b/main/inc/lib/agenda.lib.php @@ -326,7 +326,7 @@ class Agenda if (!empty($parentEventId)) { $attributes['parent_event_id'] = $parentEventId; } - $this->deleteEventIfAlreadyExit($start, $end, $allDay, $title); + $this->deleteEventIfAlreadyExists($start, $end, $allDay, $title); $senderId = $this->getSenderId(); $sessionId = $this->getSessionId(); @@ -497,30 +497,30 @@ class Agenda return $id; } - - /** - * @param string $start datetime format: 2012-06-14 09:00:00 in local time - * @param string $end datetime format: 2012-06-14 09:00:00 in local time - * @param string $allDay (true, false) - * @param string $title - * - * Prevent duplicate event to the course calendar + + /** + * Checks if an event exists and delete it (right before inserting a modified version in addEvent()) + * @param string $start datetime format: 2012-06-14 09:00:00 in local time + * @param string $end datetime format: 2012-06-14 09:00:00 in local time + * @param int $allDay (true = 1, false = 0) + * @param string $title * * @return bool + * @throws Exception */ - public function deleteEventIfAlreadyExit( - $start, - $end, - $allDay, - $title - ) { + public function deleteEventIfAlreadyExists( + string $start, + string $end, + int $allDay, + string $title + ): bool { $courseId = $this->course['real_id']; $start = Database::escape_string($start); $end = Database::escape_string($end); - $allDay = (int) $allDay; $title = Database::escape_string($title); $sql = "SELECT id FROM ".$this->tbl_course_agenda." WHERE c_id = $courseId + AND session_id = ".$this->sessionId." AND start_date = '$start' AND end_date = '$end' AND all_day = $allDay @@ -530,6 +530,7 @@ class Agenda $row = Database::fetch_array($res, 'ASSOC'); $id = $row['id']; $this->deleteEvent($id); + return true; }