From d1557ccd986f617615b6f5cd2cd9fc1b79805aaf Mon Sep 17 00:00:00 2001 From: Jack Date: Wed, 4 Dec 2024 12:22:04 +0000 Subject: [PATCH] Delete old events before creating new ones --- lib.php | 18 ++++++++++++++++++ turnitintooltwo_assignment.class.php | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/lib.php b/lib.php index fb42a45f..451f2260 100755 --- a/lib.php +++ b/lib.php @@ -1790,3 +1790,21 @@ function turnitintooltwo_update_event($turnitintooltwo, $part, $courseparam = fa turnitintooltwo_comms::handle_exceptions($e, 'turnitintooltwoupdateerror', false); } } + + +/** + * Delete a Moodle event based on passed in details. + * + * @param object $turnitintooltwo The turnitintooltwo assignment object. + * @param object $part The name of the part we are deleting. + */ +function turnitintooltwo_delete_event($turnitintooltwo, $part) { + global $DB, $USER; + + try { + $DB->delete_records_select("event", "modulename = ? AND userid = ? AND name = ?", + [ "turnitintooltwo", $USER->id, $turnitintooltwo->name." - ".$part->partname ]); + } catch (Exception $e) { + turnitintooltwo_comms::handle_exceptions($e, 'turnitintooltwoupdateerror', false); + } +} diff --git a/turnitintooltwo_assignment.class.php b/turnitintooltwo_assignment.class.php index 26563eb6..eece2416 100755 --- a/turnitintooltwo_assignment.class.php +++ b/turnitintooltwo_assignment.class.php @@ -1174,6 +1174,11 @@ public function edit_part_field($partid, $fieldname, $fieldvalue) { $partdetails = $this->get_part_details($partid); $return["partid"] = $partid; + // Delete existing events for this assignment part if title or due date changed. + if ($fieldname == "partname" || $fieldname == "dtdue") { + turnitintooltwo_delete_event($this->turnitintooltwo, $partdetails); + } + // Update Turnitin Assignment. $assignment = new TiiAssignment(); $assignment->setAssignmentId($partdetails->tiiassignid);