From cff71ec829eb043552e6585f2cca500089a92219 Mon Sep 17 00:00:00 2001 From: Johann Hoffmann Date: Thu, 13 Nov 2025 17:16:51 +0100 Subject: [PATCH] AbgabetoolJob notifyStudentMail function to send Emails to students about their changed abgabetermine inside the configurable threshold; --- application/config/abgabe.php | 2 + .../controllers/api/frontend/v1/Abgabe.php | 94 ++++++++++- .../controllers/jobs/AbgabetoolJob.php | 151 ++++++++++++++++++ application/models/crm/Student_model.php | 8 + .../models/education/Paabgabe_model.php | 18 ++- system/phrasesupdate.php | 20 +++ 6 files changed, 290 insertions(+), 3 deletions(-) create mode 100644 application/controllers/jobs/AbgabetoolJob.php diff --git a/application/config/abgabe.php b/application/config/abgabe.php index e2092a367..beacb2b87 100644 --- a/application/config/abgabe.php +++ b/application/config/abgabe.php @@ -7,3 +7,5 @@ $config['turnitin_link'] = 'https://technikum-wien.turnitin.com/sso/sp/redwood/s $config['old_abgabe_beurteilung_link'] = 'https://moodle.technikum-wien.at/mod/page/view.php?id=1005052'; +//$config['PAABGABE_EMAIL_THRESHOLD_MS'] = 86400000; // 1 day +$config['PAABGABE_EMAIL_THRESHOLD_MS'] = 864000000; // 10 day \ No newline at end of file diff --git a/application/controllers/api/frontend/v1/Abgabe.php b/application/controllers/api/frontend/v1/Abgabe.php index 690c2a768..c3dd3f06d 100644 --- a/application/controllers/api/frontend/v1/Abgabe.php +++ b/application/controllers/api/frontend/v1/Abgabe.php @@ -42,7 +42,8 @@ class Abgabe extends FHCAPI_Controller 'getProjektarbeitenForStudiengang' =>array('basis/abgabe_assistenz:rw'), 'getStudiengaenge' => array('basis/abgabe_assistenz:rw'), 'getStudentProjektarbeitAbgabeFile' => array('basis/abgabe_student:rw', 'basis/abgabe_lektor:rw', 'basis/abgabe_assistenz:rw'), - ]); + 'notifyStudentMail' => self::PERM_LOGGED + ]); $this->load->library('PhrasesLib'); $this->load->library('SignatureLib'); @@ -68,7 +69,7 @@ class Abgabe extends FHCAPI_Controller 'abgabetool' ) ); - + $this->load->config('abgabe'); $this->load->helper('hlp_sancho_helper'); } @@ -997,6 +998,95 @@ class Abgabe extends FHCAPI_Controller } } + + + // test method for abgabe nightly email job + public function notifyStudentMail() + { + // send all new projektarbeit abgabe since the last job run to the related student + +// $this->logInfo('Start job queue scheduler FHC-Core->notifyBetreuerMail'); + $this->load->model('education/Projektarbeit_model', 'ProjektarbeitModel'); + $this->load->model('education/Paabgabe_model', 'PaabgabeModel'); + $this->load->model('crm/Student_model', 'StudentModel'); + + $milliseconds = $this->config->item('PAABGABE_EMAIL_THRESHOLD_MS'); + + $result = $this->PaabgabeModel->findAbgabenNewOrUpdatedSince($milliseconds); + $retval = getData($result); + + // group results per projektarbeit/student_uid + $student_uids = []; + forEach($retval as $paabgabe) { + if(!isset($student_uids[$paabgabe->student_uid])) { + $student_uids[$paabgabe->student_uid] = []; + } + + $student_uids[$paabgabe->student_uid][] = $paabgabe; + } + + foreach ($student_uids as $uid => $abgaben) { + // $uid is the student's UID + $result = $this->StudentModel->getEmailAnredeForStudentUID($uid); + $data = getData($result)[0]; + + // $abgabe is the array of paabgabe objects + $anredeFillString = $data->anrede=="Herr"?"r":""; + $fullFormattedNameString = trim($data->titelpre." ".$data->vorname." ".$data->vornamen." ".$data->nachname." ".$data->titelpost); + + // https://www.php.net/manual/en/migration70.new-features.php#migration70.new-features.spaceship-op + // php has spaceships 🚀🚀🚀🚀🚀 + usort($abgaben, function($a, $b) { + return strtotime($a->datum) <=> strtotime($b->datum); + }); + + $projektarbeit_titel = $abgaben[0]->titel; + $abgabenString = '

'; + forEach($abgaben as $abgabe) { + $datetime = new DateTime($abgabe->datum); + $dateEmailFormatted = $datetime->format('d.m.Y'); + + $abgabenString .= $dateEmailFormatted.' '.$abgabe->bezeichnung.' '.$abgabe->kurzbz.'
'; + } + + // Link to Entschuldigungsmanagement + if(defined('CIS4') && CIS4) { + $ci3BootstrapFilePath = "cis.php"; + } else { + $ci3BootstrapFilePath = "index.ci.php"; + } + $url = APP_ROOT.$ci3BootstrapFilePath.'/Cis/Abgabetool/Student'; + +// $linkAbgabetool = ''; + $body_fields = array( + 'anrede' => $data->anrede, + 'anredeFillString' => $anredeFillString, + 'fullFormattedNameString' => $fullFormattedNameString, + 'paTitel' => $projektarbeit_titel, + 'abgabenString' => $abgabenString, + 'linkAbgabetool' => $url + ); + + // send email with bundled info + sendSanchoMail( + 'paabgabeUpdatesSammelmail', + $body_fields, + $uid.'@'.DOMAIN, + $this->p->t('abgabetool', 'changedAbgabeterminev2') + ); + + } + + $this->terminateWithSuccess( + array( + 'student_uids' => $student_uids, + 'result' => $result, + 'body_fields' => $body_fields + ) + ); + + } + private function checkAbgabeSignatur($abgabe, $projektarbeit) { if($abgabe->paabgabetyp_kurzbz != 'end') { return; diff --git a/application/controllers/jobs/AbgabetoolJob.php b/application/controllers/jobs/AbgabetoolJob.php new file mode 100644 index 000000000..24c32db11 --- /dev/null +++ b/application/controllers/jobs/AbgabetoolJob.php @@ -0,0 +1,151 @@ +_ci =& get_instance(); + + $this->_ci->load->helper('hlp_sancho_helper'); + + $this->_ci->load->model('education/Projektarbeit_model', 'ProjektarbeitModel'); + $this->_ci->load->model('education/Paabgabe_model', 'PaabgabeModel'); + + // signatur fehlt mail + $this->_ci->load->model('crm/Student_model', 'StudentModel'); + $this->_ci->load->model('organisation/Studiengang_model', 'StudiengangModel'); + + + // 2. begutachter mail + $this->_ci->load->model('education/Projektbetreuer_model', 'ProjektbetreuerModel'); + + $this->loadPhrases([ + 'abgabetool' + ]); + +// $this->_ci->load->model('extensions/FHC-Core-Anwesenheiten/QR_model', 'QRModel'); +// $this->_ci->load->config('extensions/FHC-Core-Anwesenheiten/qrsettings'); + } + + + + public function notifyBetreuerMail() + { + // send all new projektarbeit abgabe since the lat job run to the related betreuer + +// $this->logInfo('Start job queue scheduler FHC-Core->notifyBetreuerMail'); + + $milliseconds = $this->_ci->config->item('PAABGABE_UPDATEAMUM_EMAIL_THRESHOLD_MILLISECONDS'); + + $result = $this->PaabgabeModel->findAbgabenNewOrUpdatedSince($milliseconds); + $retval = getData($result); + + // group results per projektarbeit/student_uid + $student_uids = []; + forEach($retval as $paabgabe) { + if(!in_array($paabgabe->student_uid, $student_uids, true)) { + $student_uids[$paabgabe->student_uid] = [$paabgabe]; + } else { + $student_uids[$paabgabe->student_uid][] = $paabgabe; + } + } + + + + // send emails with bundled info + +// $rows_affected = $this->QRModel->db->affected_rows(); +// +// if (isError($result)) +// { +// $this->logError(getError($result), $milliseconds); +// } else { +// $this->logInfo($rows_affected." QR Codes deleted."); +// } +// +// $this->logInfo('End job queue scheduler FHC-Core->notifyBetreuerMail'); + } + + public function notifyStudentMail() + { + // send all new projektarbeit abgabe since the last job run to the related student + + $this->logInfo('Start job queue scheduler FHC-Core->notifyStudentMail'); + + $milliseconds = $this->_ci->config->item('PAABGABE_EMAIL_THRESHOLD_MS'); + + $result = $this->_ci->PaabgabeModel->findAbgabenNewOrUpdatedSince($milliseconds); + $retval = getData($result); + + // group results per projektarbeit/student_uid + $student_uids = []; + forEach($retval as $paabgabe) { + if(!isset($student_uids[$paabgabe->student_uid])) { + $student_uids[$paabgabe->student_uid] = []; + } + + $student_uids[$paabgabe->student_uid][] = $paabgabe; + } + + foreach ($student_uids as $uid => $abgaben) { + // $uid is the student's UID + $result = $this->_ci->StudentModel->getEmailAnredeForStudentUID($uid); + $data = getData($result)[0]; + + // $abgabe is the array of paabgabe objects + $anredeFillString = $data->anrede=="Herr"?"r":""; + $fullFormattedNameString = trim($data->titelpre." ".$data->vorname." ".$data->vornamen." ".$data->nachname." ".$data->titelpost); + + // https://www.php.net/manual/en/migration70.new-features.php#migration70.new-features.spaceship-op + // php has spaceships 🚀🚀🚀🚀🚀 + usort($abgaben, function($a, $b) { + return strtotime($a->datum) <=> strtotime($b->datum); + }); + + $projektarbeit_titel = $abgaben[0]->titel; + $abgabenString = '

'; + forEach($abgaben as $abgabe) { + $datetime = new DateTime($abgabe->datum); + $dateEmailFormatted = $datetime->format('d.m.Y'); + + $abgabenString .= $dateEmailFormatted.' '.$abgabe->bezeichnung.' '.$abgabe->kurzbz.'
'; + } + + // TODO: check this config flag in job context + // Link to Entschuldigungsmanagement + if(defined('CIS4') && CIS4) { + $ci3BootstrapFilePath = "cis.php"; + } else { + $ci3BootstrapFilePath = "index.ci.php"; + } + $url = APP_ROOT.$ci3BootstrapFilePath.'/Cis/Abgabetool/Student'; + + $body_fields = array( + 'anrede' => $data->anrede, + 'anredeFillString' => $anredeFillString, + 'fullFormattedNameString' => $fullFormattedNameString, + 'paTitel' => $projektarbeit_titel, + 'abgabenString' => $abgabenString, + 'linkAbgabetool' => $url + ); + + // send email with bundled info + sendSanchoMail( + 'paabgabeUpdatesSammelmail', + $body_fields, + $uid.'@'.DOMAIN, + $this->p->t('abgabetool', 'changedAbgabeterminev2') + ); + } + + $this->logInfo('End job queue scheduler FHC-Core->notifyStudentMail'); + } +} \ No newline at end of file diff --git a/application/models/crm/Student_model.php b/application/models/crm/Student_model.php index 539c3cf56..19e57b6dc 100644 --- a/application/models/crm/Student_model.php +++ b/application/models/crm/Student_model.php @@ -279,4 +279,12 @@ class Student_model extends DB_Model { return $student_uid . '@' . DOMAIN; } + + public function getEmailAnredeForStudentUID($student_uid) { + $qry = "SELECT anrede, titelpre, vorname, vornamen, nachname, titelpost + FROM campus.vw_student + WHERE uid = ?"; + + return $this->execReadOnlyQuery($qry, array($student_uid)); + } } diff --git a/application/models/education/Paabgabe_model.php b/application/models/education/Paabgabe_model.php index 343a86706..e2ccfaaca 100644 --- a/application/models/education/Paabgabe_model.php +++ b/application/models/education/Paabgabe_model.php @@ -60,5 +60,21 @@ class Paabgabe_model extends DB_Model return $this->execReadOnlyQuery($qry, array($person_id)); } - + + public function findAbgabenNewOrUpdatedSince($milliseconds) + { + $interval = $milliseconds . ' milliseconds'; + $query = "SELECT projektarbeit_id, paabgabe_id, paabgabetyp_kurzbz, fixtermin, datum, kurzbz, campus.tbl_paabgabetyp.bezeichnung, campus.tbl_paabgabe.abgabedatum, + campus.tbl_paabgabe.insertvon, campus.tbl_paabgabe.insertamum, campus.tbl_paabgabe.updatevon, campus.tbl_paabgabe.updateamum, + campus.tbl_paabgabe.note, upload_allowed, beurteilungsnotiz, student_uid, tbl_projektarbeit.note, lehre.tbl_projektarbeit.titel + FROM campus.tbl_paabgabe + JOIN campus.tbl_paabgabetyp USING (paabgabetyp_kurzbz) + JOIN lehre.tbl_projektarbeit USING (projektarbeit_id) + + WHERE campus.tbl_paabgabe.insertamum >= NOW() - INTERVAL ? + OR campus.tbl_paabgabe.updateamum >= NOW() - INTERVAL ? + "; + + return $this->execQuery($query, [$interval, $interval]); + } } diff --git a/system/phrasesupdate.php b/system/phrasesupdate.php index b395636b7..cf4262333 100644 --- a/system/phrasesupdate.php +++ b/system/phrasesupdate.php @@ -44048,6 +44048,26 @@ array( ) ) ), + array( + 'app' => 'core', + 'category' => 'abgabetool', + 'phrase' => 'changedAbgabeterminev2', + 'insertvon' => 'system', + 'phrases' => array( + array( + 'sprache' => 'German', + 'text' => 'Updates: Abgabetermine Bachelor-/Masterarbeiten', + 'description' => '', + 'insertvon' => 'system' + ), + array( + 'sprache' => 'English', + 'text' => "Updates: Deadlines Bachelor's-/Master's Thesis", + 'description' => '', + 'insertvon' => 'system' + ) + ) + ), // ABGABETOOL PHRASEN END array( 'app' => 'core',