diff --git a/application/controllers/lehre/anrechnung/ApproveAnrechnungUebersicht.php b/application/controllers/lehre/anrechnung/ApproveAnrechnungUebersicht.php index 207c4de9b..7b66a728c 100644 --- a/application/controllers/lehre/anrechnung/ApproveAnrechnungUebersicht.php +++ b/application/controllers/lehre/anrechnung/ApproveAnrechnungUebersicht.php @@ -6,6 +6,8 @@ class approveAnrechnungUebersicht extends Auth_Controller { const BERECHTIGUNG_ANRECHNUNG_GENEHMIGEN = 'lehre/anrechnung_genehmigen'; + const REVIEW_ANRECHNUNG_URI = '/lehre/anrechnung/ReviewAnrechnungUebersicht'; + const ANRECHNUNGSTATUS_PROGRESSED_BY_STGL = 'inProgressDP'; const ANRECHNUNGSTATUS_PROGRESSED_BY_KF = 'inProgressKF'; const ANRECHNUNGSTATUS_PROGRESSED_BY_LEKTOR = 'inProgressLektor'; @@ -70,7 +72,7 @@ class approveAnrechnungUebersicht extends Auth_Controller if (!is_string($studiensemester_kurzbz)) { - $studiensemester = $this->StudiensemesterModel->getNearest(); // TODO check + $studiensemester = $this->StudiensemesterModel->getNearest(); if (hasData($studiensemester)) { $studiensemester_kurzbz = $studiensemester->retval[0]->studiensemester_kurzbz; @@ -221,6 +223,16 @@ class approveAnrechnungUebersicht extends Auth_Controller // Output json to ajax if (isset($json) && !isEmptyArray($json)) { + /** + * Send mails to lectors + * NOTE: mails are sent at the end to ensure sending only ONE mail to each LV-Leitung or lector + * even if they are required for more recommendations + * */ + if (!$this->_sendSanchoMailToLectors($json)) + { + show_error('Failed sending emails'); + } + return $this->outputJsonSuccess($json); } else @@ -283,4 +295,118 @@ class approveAnrechnungUebersicht extends Auth_Controller return true; } + + /** + * Send mail to lectors asking for recommendation. (first to LV-Leitung, if not present to all lectors of lv) + * @param $mail_params + * @return bool + */ + private function _sendSanchoMailToLectors($mail_params) + { + // Get Lehrveranstaltungen + $anrechnung_arr = array(); + + foreach ($mail_params as $item) + { + $this->AnrechnungModel->addSelect('lehrveranstaltung_id, studiensemester_kurzbz'); + $anrechnung_arr[]= array( + 'lehrveranstaltung_id' => $this->AnrechnungModel->load($item['anrechnung_id'])->retval[0]->lehrveranstaltung_id, + 'studiensemester_kurzbz' => $this->AnrechnungModel->load($item['anrechnung_id'])->retval[0]->studiensemester_kurzbz + ); + } + + $anrechnung_arr = array_unique($anrechnung_arr, SORT_REGULAR); + + + /** + * Get lectors (prio for LV-Leitung, if not present to all lectors of LV. + * Anyway this function will receive a unique array to avoid sending more mails to one and the same lector. + * **/ + $lector_arr = $this->_getLectors($anrechnung_arr); + + // Send mail to lectors + foreach ($lector_arr as $lector) + { + $to = $lector->uid; + $vorname = $lector->vorname; + + // Get full name of stgl + $this->load->model('person/Person_model', 'PersonModel'); + if (!$stgl_name = getData($this->PersonModel->getFullName($this->_uid))) + { + show_error ('Failed retrieving person'); + } + + // Link to Antrag genehmigen + $url = site_url(self::REVIEW_ANRECHNUNG_URI); + + // Prepare mail content + $body_fields = array( + 'vorname' => $vorname, + 'stgl_name' => $stgl_name, + 'link' => anchor($url, 'Anrechnungsanträge Übersicht') + ); + + sendSanchoMail( + 'AnrechnungEmpfehlungAnfordern', + $body_fields, + $to, + 'Neue LV-Anrechnungsanträge benötigen Deine Empfehlung' + ); + } + return true; + } + + /** + * Get lectors (prio for LV-Leitung, if not present to all lectors of LV. + * Anyway this function will receive a unique array to avoid sending more mails to one and the same lector. + * @param $anrechnung_arr + * @return array + */ + private function _getLectors($anrechnung_arr) + { + $lector_arr = array(); + + // Get lectors + foreach($anrechnung_arr as $anrechnung) + { + $this->load->model('education/Lehrveranstaltung_model', 'LehrveranstaltungModel'); + $result = $this->LehrveranstaltungModel->getLecturersByLv($anrechnung['studiensemester_kurzbz'], $anrechnung['lehrveranstaltung_id']); + + if (!$result = getData($result)) + { + show_error('Failed retrieving lectors of Lehrveranstaltung'); + } + + // Check if lv has LV-Leitung + $key = array_search(true, array_column($result, 'lvleiter')); + + // If lv has LV-Leitung, keep only the one + if ($key !== false) + { + $lector_arr[]= $result[$key]; + } + // ...otherwise keep all lectors + else + { + $lector_arr = array_merge($lector_arr, $result); + } + } + + /** + * NOTE: This step is only done to make the array unique by uid, vorname and nachname in the following step + * (e.g. if same lector is ones LV-Leitung and another time not, then array_unique would leave both. + * But we wish to send only one email by to that one person) + * **/ + foreach ($lector_arr as $lector) + { + unset($lector->lvleiter); + } + + // Now make the lector array aka mail receivers unique + $lector_arr = array_unique($lector_arr, SORT_REGULAR); + + return $lector_arr; + + } } \ No newline at end of file