aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJan-Hendrik Willms <tleilax+studip@gmail.com>2024-12-04 08:30:41 +0000
committerJan-Hendrik Willms <tleilax+studip@gmail.com>2024-12-04 08:30:41 +0000
commit0bb23f9dae4bfdfd4619e73c3bab51cc84e8ebee (patch)
tree184b040a2b8e859d284ed6ad688ad20d963f2433 /lib
parent24bbb999223d2c96e79e864dc8004c7153d60f2b (diff)
restore logging of institute changes and insert home institut back into seminar_inst, fixes #4751
Closes #4751 Merge request studip/studip!3546
Diffstat (limited to 'lib')
-rw-r--r--lib/models/Course.php86
1 files changed, 86 insertions, 0 deletions
diff --git a/lib/models/Course.php b/lib/models/Course.php
index c8138e1..690fbc2 100644
--- a/lib/models/Course.php
+++ b/lib/models/Course.php
@@ -89,6 +89,11 @@ class Course extends SimpleORMap implements Range, PrivacyObject, StudipItem, Fe
*/
protected $initial_end_semester;
+ /**
+ * @var array|null Currently assigned institutes, used for tracking changes
+ */
+ protected $currently_assigned_institutes = null;
+
protected static function configure($config = [])
{
$config['db_table'] = 'seminare';
@@ -305,6 +310,7 @@ class Course extends SimpleORMap implements Range, PrivacyObject, StudipItem, Fe
$config['i18n_fields']['ort'] = true;
$config['registered_callbacks']['before_store'][] = 'logStore';
+ $config['registered_callbacks']['before_store'][] = 'handleInstitutes';
$config['registered_callbacks']['after_create'][] = 'setDefaultTools';
$config['registered_callbacks']['after_delete'][] = function (Course $course) {
CourseAvatar::getAvatar($course->id)->reset();
@@ -377,6 +383,12 @@ class Course extends SimpleORMap implements Range, PrivacyObject, StudipItem, Fe
parent::initRelation($relation);
$this->initial_start_semester = $this->getStartSemester();
$this->initial_end_semester = $this->getEndSemester();
+ } elseif ($relation === 'institutes' && $this->currently_assigned_institutes === null) {
+ parent::initRelation($relation);
+ $this->currently_assigned_institutes = array_filter(
+ $this->relations['institutes']->pluck('id'),
+ fn($inst_id) => $inst_id !== $this->getPristineValue('institut_id')
+ );
}
parent::initRelation($relation);
}
@@ -392,6 +404,7 @@ class Course extends SimpleORMap implements Range, PrivacyObject, StudipItem, Fe
//Reset the flags for the start and end semester:
$this->initial_start_semester = null;
$this->initial_end_semester = null;
+ $this->currently_assigned_institutes = null;
}
/**
@@ -2141,6 +2154,79 @@ class Course extends SimpleORMap implements Range, PrivacyObject, StudipItem, Fe
][$this->completion] ?? _('undefiniert');
}
+ public function setValue($field, $value)
+ {
+ if (strtolower($field) === 'institut_id') {
+ $this->institutes = $this->institutes->filter(function (Institute $institute) {
+ return $institute->id !== $this->institut_id;
+ });
+ } elseif (strtolower($field) === 'institutes') {
+ $this->initRelation($field);
+ }
+
+ return parent::setValue($field, $value);
+ }
+
+ /**
+ * Handle all things related to storing the institutes
+ */
+ protected function handleInstitutes(): void
+ {
+ if ($this->isFieldDirty('institut_id')) {
+ StudipLog::log(
+ 'CHANGE_INSTITUTE_DATA',
+ $this->id,
+ $this->institut_id,
+ "Die Heimateinrichtung wurde zu \"{$this->home_institut->name}\" geändert."
+ );
+ }
+
+ if ($this->currently_assigned_institutes !== null) {
+ $assigned_ids = $this->institutes->pluck('id');
+
+ // Deleted
+ $deleted_ids = array_diff($this->currently_assigned_institutes, $assigned_ids);
+ Institute::findEachMany(
+ function (Institute $institute) {
+ StudipLog::log(
+ 'CHANGE_INSTITUTE_DATA',
+ $this->id,
+ $institute->id,
+ "Die beteiligte Einrichtung \"{$institute->name}\" wurde gelöscht."
+ );
+ NotificationCenter::postNotification('SeminarInstitutionDidDelete', $institute->id, $this->id);
+ },
+ $deleted_ids
+ );
+
+ // Added
+ $added_ids = array_diff($assigned_ids, $this->currently_assigned_institutes);
+ Institute::findEachMany(
+ function (Institute $institute) {
+ StudipLog::log(
+ 'CHANGE_INSTITUTE_DATA',
+ $this->id,
+ $institute->id,
+ "Die beteiligte Einrichtung \"{$institute->name}\" wurde hinzugefügt."
+ );
+ NotificationCenter::postNotification('SeminarInstitutionDidCreate', $institute->id, $this->id);
+ },
+ $added_ids
+ );
+
+ if (count($deleted_ids) > 0 || count($added_ids) > 0) {
+ NotificationCenter::postNotification('CourseDidChangeInstitutes', $this);
+ }
+ }
+
+ if (
+ $this->institut_id
+ && !$this->institutes->find($this->institut_id)
+ ) {
+ $this->institutes[] = $this->home_institut;
+ }
+ }
+
/**
* Generates a general log entry if the course were changed.
* Furthermore, this method emits notifications when the