diff --git a/classes/sdapi.php b/classes/sdapi.php index b0d84d3efe7a04b5790ae412335a2a8dfeb534cb..34a616e0dfa8f5b80816950876ff6ef7b507a86a 100644 --- a/classes/sdapi.php +++ b/classes/sdapi.php @@ -740,7 +740,7 @@ class sdapi { $newcourseappointments = []; $obsoletecourseappointments = []; - $ret += 1 * $this->diff_course_appointments($trace, $newcourseappointments, $obsoletecourseappointments); + $ret += 1 * $this->diff_course_appointments($trace, $newcourseappointments, $obsoletecourseappointments); $newgroupappointments = []; $obsoletegroupappointments = []; $ret += 10 * $this->diff_group_appointments($trace, $newgroupappointments, $obsoletegroupappointments); @@ -835,7 +835,7 @@ class sdapi { // Find all existing appointments with eventsource 'rwthonline'. $existingappointments = array(); - $sql = "SELECT e.id, e.name, e.description, e.timestart, e.timeduration, c.$localcoursefield, sa.sitedirectory_eventid + $sql = "SELECT e.id, e.name, e.description, e.timestart, e.timeduration, e.location, e.description, c.$localcoursefield, sa.sitedirectory_eventid FROM {event} e JOIN {rwth_sd_sync_appointment} sa ON (sa.eventsource = 'rwthonline' AND e.id = sa.moodle_eventid) JOIN {course} c ON c.id = e.courseid @@ -848,8 +848,42 @@ class sdapi { $rs->close(); $obsoleteappointments = array_diff_key($existingappointments, $externalappointments); + $commonappointments = array_intersect_key($existingappointments, $externalappointments); + foreach ($commonappointments as $ronid => $appointment) { + $existingrecord = $existingappointments[$ronid]; + $possiblenewrecord = $externalappointments[$ronid]; + // Check for termin name change. + $existingname = $existingrecord->name; + $possiblenewname = $possiblenewrecord['keyword']; + // Check for startdate change. + $existingstartdate = $existingrecord->timestart; + $possiblenewstartdate = $possiblenewrecord['start']; + // Check for enddate change. + $existingenddate = $existingrecord->timestart + $existingrecord->timeduration; + $possiblenewenddate = $possiblenewrecord['end']; + // Check for room or location change. + $roomlocation = explode(']', $existingrecord->location); + $existingroom = trim(explode('[', $roomlocation[0])[1]); + $existinglocation = trim($roomlocation[1]); + $possiblenewroom = $possiblenewrecord['room']; + $possiblenewlocation = $possiblenewrecord['location']; + // Check for subject change. + $existingsubject = $existingrecord->description; + $possiblenewsubject = $possiblenewrecord['subject']; + + + if ($existingname !== $possiblenewname || + $existingstartdate !== $possiblenewstartdate || + $existingenddate !== $possiblenewenddate || + $existingroom !== $possiblenewroom || + $existinglocation !== $possiblenewlocation || + $existingsubject !== $possiblenewsubject) { + $obsoleteappointments += [$ronid => $existingappointments[$ronid]]; + $newappointments += [$ronid => $externalappointments[$ronid]]; + } + } - $newappointments = array_diff_key($externalappointments, $existingappointments); + $newappointments += array_diff_key($externalappointments, $existingappointments); foreach ($newappointments as $ronid => $newappointment) { $courseid = $DB->get_field('course', 'id', array($localcoursefield => $newappointment[$coursefield_l])); if (!$courseid) { @@ -963,7 +997,7 @@ class sdapi { // Find all existing group appointments with eventsource 'rwthonline'. $existingappointments = []; - $sql = "SELECT e.id, e.name, e.description, e.timestart, e.timeduration, sg.sitedirectory_groupid, sa.sitedirectory_eventid + $sql = "SELECT e.id, e.name, e.description, e.timestart, e.timeduration, e.location, e.description, sg.sitedirectory_groupid, sa.sitedirectory_eventid FROM {event} e JOIN {rwth_sd_sync_appointment} sa ON (sa.eventsource = 'rwthonline' AND e.id = sa.moodle_eventid) JOIN {groups} g ON g.id = e.groupid @@ -974,10 +1008,44 @@ class sdapi { $existingappointments[$ronid] = $existingevent; } $rs->close(); - + $obsoleteappointments = array_diff_key($existingappointments, $externalappointments); + $commonappointments = array_intersect_key($existingappointments, $externalappointments); + + foreach ($commonappointments as $ronid => $appointment) { + $existingrecord = $existingappointments[$ronid]; + $possiblenewrecord = $externalappointments[$ronid]; + // Check for termin name change. + $existingname = $existingrecord->name; + $possiblenewname = $possiblenewrecord['keyword']; + // Check for startdate change. + $existingstartdate = $existingrecord->timestart; + $possiblenewstartdate = $possiblenewrecord['start']; + // Check for enddate change. + $existingenddate = $existingrecord->timestart + $existingrecord->timeduration; + $possiblenewenddate = $possiblenewrecord['end']; + // Check for room or location change. + $roomlocation = explode(']', $existingrecord->location); + $existingroom = trim(explode('[', $roomlocation[0])[1]); + $existinglocation = trim($roomlocation[1]); + $possiblenewroom = $possiblenewrecord['room']; + $possiblenewlocation = $possiblenewrecord['location']; + // Check for subject change. + $existingsubject = $existingrecord->description; + $possiblenewsubject = $possiblenewrecord['subject']; + + if ($existingname !== $possiblenewname || + $existingstartdate !== $possiblenewstartdate || + $existingenddate !== $possiblenewenddate || + $existingroom !== $possiblenewroom || + $existinglocation !== $possiblenewlocation || + $existingsubject !== $possiblenewsubject) { + $obsoleteappointments += [$ronid => $existingappointments[$ronid]]; + $newappointments += [$ronid => $externalappointments[$ronid]]; + } + } - $newappointments = array_diff_key($externalappointments, $existingappointments); + $newappointments += array_diff_key($externalappointments, $existingappointments); foreach ($newappointments as $ronid => $newappointment) { $moodlegroup = $DB->get_record_sql("SELECT g.* FROM {groups} g @@ -1077,10 +1145,10 @@ class sdapi { date_default_timezone_set("Europe/Berlin"); $starttime = strtotime($newappointment[$startfield_l]); - $duration = strtotime($newappointment[$endfield_l]) - strtotime($newappointment[$startfield_l]); + $duration = strtotime($newappointment[$endfield_l]) - strtotime($newappointment[$startfield_l]); - $newappointment[$roomfield_l] = htmlentities($newappointment[$roomfield_l]); - $newappointment[$locationfield_l] = htmlentities($newappointment[$locationfield_l]); + $newappointment[$roomfield_l] = $newappointment[$roomfield_l]; + $newappointment[$locationfield_l] = $newappointment[$locationfield_l]; $newappointment[$subjectfield_l] = htmlentities($newappointment[$subjectfield_l]); $newappointment[$keywordfield_l] = htmlentities($newappointment[$keywordfield_l]); $newappointment[$notesfield_l] = htmlentities($newappointment[$notesfield_l]); @@ -1191,8 +1259,8 @@ class sdapi { $starttime = strtotime($newappointment[$startfield_l]); $duration = strtotime($newappointment[$endfield_l]) - strtotime($newappointment[$startfield_l]); - $newappointment[$roomfield_l] = htmlentities($newappointment[$roomfield_l]); - $newappointment[$locationfield_l] = htmlentities($newappointment[$locationfield_l]); + $newappointment[$roomfield_l] = $newappointment[$roomfield_l]; + $newappointment[$locationfield_l] = $newappointment[$locationfield_l]; $newappointment[$subjectfield_l] = htmlentities($newappointment[$subjectfield_l]); $newappointment[$keywordfield_l] = htmlentities($newappointment[$keywordfield_l]); $newappointment[$notesfield_l] = htmlentities($newappointment[$notesfield_l]);