diff --git a/application/controllers/jobs/ESIJob.php b/application/controllers/jobs/ESIJob.php new file mode 100644 index 000000000..74c5a925d --- /dev/null +++ b/application/controllers/jobs/ESIJob.php @@ -0,0 +1,169 @@ +load->model('Person_model', 'PersonModel'); + $this->load->model('Kennzeichen_model', 'KennzeichenModel'); + + // load libraries + //$this->load->library('extensions/FHC-Core-DVUH/DVUHIssueLib'); + } + + //------------------------------------------------------------------------------------------------------------------ + // Public methods + + /** + * Initialises generateESI job, handles job queue, logs infos/errors + */ + public function generateESI() + { + //$jobType = 'DVUHSendPruefungsaktivitaeten'; + $this->logInfo(ESIScheduler::JOB_TYPE_GENERATE_ESI.' job start'); + + // Gets the latest jobs + $lastJobs = $this->getLastJobs(ESIScheduler::JOB_TYPE_GENERATE_ESI); + if (isError($lastJobs)) + { + $this->logError(getCode($lastJobs).': '.getError($lastJobs), ESIScheduler::JOB_TYPE_GENERATE_ESI); + } + else + { + $this->updateJobs( + getData($lastJobs), // Jobs to be updated + array(JobsQueueLib::PROPERTY_START_TIME), // Job properties to be updated + array(date('Y-m-d H:i:s')) // Job properties new values + ); + + $person_arr = $this->_getInputObjArray(getData($lastJobs)); + + foreach ($person_arr as $persobj) + { + if (!isset($persobj->person_id)) + $this->logError("Error when generating ESI: invalid parameters"); + else + { + $person_id = $persobj->person_id; + + // check if there already is an active ESI + $this->KennzeichenModel->addSelect('1'); + $activeKennzeichenRes = $this->KennzeichenModel->loadWhere( + array('person_id' => $person_id, 'kennzeichentyp_kurzbz' => ESIScheduler::KENNZEICHENTYP_KURZBZ, 'aktiv' => true) + ); + + if (hasData($activeKennzeichenRes)) + { + $this->logError("Active ESI for person Id $prestudent_id already exists"); + continue; + } + + // get Matrikelnr for person for which ESI should be generated + $this->PersonModel->addSelect('matr_nr'); + $personRes = $this->PersonModel->load($person_id); + + if (!hasData($personRes)) + { + $this->logError("Person with Id $person_id not found"); + continue; + } + + $matr_nr = getData($personRes)[0]->matr_nr; + + if (isEmptyString($matr_nr)) + { + $this->logError("Matrikelnummer for person with Id $person_id is empty"); + continue; + } + + $esi = self::ESI_PREFIX.$matrikelnr; + + // check if ESI was already used + $this->KennzeichenModel->addSelect('1'); + $existingKennzeichenRes = $this->KennzeichenModel->loadWhere( + array('person_id' => $person_id, 'kennzeichentyp_kurzbz' => ESIScheduler::KENNZEICHENTYP_KURZBZ, 'inhalt' => $esi) + ); + + if (hasData($existingKennzeichenRes)) + { + $this->logError("ESI $esi for person Id $prestudent_id already exists"); + continue; + } + + // if everything ok, save the esi for the person + $saveEsiResult = $this->Kennzeichen_model->insert( + array( + 'person_id' => $person_id, + 'kennzeichentyp_kurzbz' => ESIScheduler::KENNZEICHENTYP_KURZBZ, + 'inhalt' => $esi, + 'aktiv' => true + ) + ); + + if (isError($saveEsiResult)) + { + $this->logError("Error when sending ESI, person Id $person_id ".getError($saveEsiResult)); + } + elseif (hasData($saveEsiResult)) + { + // TODO everything ok + } + } + } + + // Update jobs properties values + $this->updateJobs( + getData($lastJobs), // Jobs to be updated + array(JobsQueueLib::PROPERTY_STATUS, JobsQueueLib::PROPERTY_END_TIME), // Job properties to be updated + array(JobsQueueLib::STATUS_DONE, date('Y-m-d H:i:s')) // Job properties new values + ); + + if (hasData($lastJobs)) $this->updateJobsQueue($jobType, getData($lastJobs)); + } + + $this->logInfo(ESIScheduler::JOB_TYPE_GENERATE_ESI.' job stop'); + } + + // -------------------------------------------------------------------------------------------- + // Private methods + + /** + * Extracts input data from jobs. + * @param $jobs + * @return array with jobinput + */ + private function _getInputObjArray($jobs) + { + $mergedUsersArray = array(); + + if (count($jobs) == 0) return $mergedUsersArray; + + foreach ($jobs as $job) + { + $decodedInput = json_decode($job->input); + if ($decodedInput != null) + { + foreach ($decodedInput as $el) + { + $mergedUsersArray[] = $el; + } + } + } + return $mergedUsersArray; + } +} diff --git a/application/controllers/jobs/schedulers/ESIScheduler.php b/application/controllers/jobs/schedulers/ESIScheduler.php new file mode 100644 index 000000000..3ab858937 --- /dev/null +++ b/application/controllers/jobs/schedulers/ESIScheduler.php @@ -0,0 +1,108 @@ +load->model('organisation/Studiensemester_model', 'StudiensemesterModel'); + } + + //------------------------------------------------------------------------------------------------------------------ + // Public methods + + /** + * Creates jobs queue entries for generateESI job. + * @param string $studiensemester_kurzbz semester for which ESIs should be generated + */ + public function generateESI($studiensemester_kurzbz = null) + { + // if no semester given, get current studiensemester + if (!isset($studiensemester_kurzbz)) + { + $semRes = $this->StudiensemesterModel->getAkt(); + + if (hasData($semRes)) + { + $studiensemester_kurzbz = getData($semRes)[0]->studiensemester_kurzbz; + } + } + + if (isset($studiensemester_kurzbz)) + { + $this->logInfo('Start job queue scheduler '.self::JOB_TYPE_GENERATE_ESI); + + $qry = " + SELECT + DISTINCT person_id + FROM + public.tbl_person pers + JOIN public.tbl_prestudent ps USING (person_id) + JOIN public.tbl_prestudentstatus pss USING (prestudent_id) + WHERE + pss.studiensemester_kurzbz = ? + AND pers.matr_nr IS NOT NULL + AND pss.status_kurzbz IN ? + AND NOT EXISTS ( -- has no ESI yet + SELECT 1 + FROM + public.tbl_kennzeichen + WHERE + person_id = pers.person_id + AND kennzeichentyp_kurzbz = ? + AND aktiv + ) + AND NOT EXISTS ( -- making sure it's not an incoming + SELECT 1 + FROM + public.tbl_prestudentstatus + WHERE + prestudent_id = ps.prestudent_id + AND status_kurzbz = 'Incoming' + )"; + + $db = new DB_Model(); + $jobInputResult = $db->execReadOnlyQuery($qry, array($studiensemester_kurzbz, $this->_active_status_kurzbz, self::KENNZEICHENTYP_KURZBZ)); + + // If an error occured then log it + if (isError($jobInputResult)) + { + $this->logError(getError($jobInputResult)); + } + elseif (hasData($jobInputResult)) // if persons found + { + // Add the new job to the jobs queue + $addNewJobResult = $this->addNewJobsToQueue( + self::JOB_TYPE_GENERATE_ESI, // job type + $this->generateJobs( // gnerate the structure of the new job + JobsQueueLib::STATUS_NEW, + json_encode(getData($jobInputResult)) + ) + ); + + // If error occurred return it + if (isError($addNewJobResult)) $this->logError(getError($addNewJobResult)); + } + } + else + { + $this->logError('Error when getting Studiensemester'); + } + + $this->logInfo('End job queue scheduler '.self::JOB_TYPE_GENERATE_ESI); + } +} diff --git a/application/models/person/Kennzeichen_model.php b/application/models/person/Kennzeichen_model.php new file mode 100644 index 000000000..7d00a68b4 --- /dev/null +++ b/application/models/person/Kennzeichen_model.php @@ -0,0 +1,36 @@ +dbTable = 'public.tbl_kennzeichen'; + $this->pk = 'kennzeichen_id'; + } + + /** + * + * @param + * @return object success or error + */ + public function _() + { + + } + + /** + * Get Zustelladress of given person. + * @param string $person_id + * @param string $select + * @return array + */ + //~ public function getZustellAdresse($person_id, $select = '*') + //~ { + //~ $this->addSelect($select); + //~ return $this->loadWhere(array('person_id' => $person_id, 'zustelladresse'=> true)); + //~ } +} diff --git a/system/dbupdate_3.4/21620_neues_feld_zum_erfassen_des_ESI.php b/system/dbupdate_3.4/21620_neues_feld_zum_erfassen_des_ESI.php index a7d415b68..5b6b22918 100644 --- a/system/dbupdate_3.4/21620_neues_feld_zum_erfassen_des_ESI.php +++ b/system/dbupdate_3.4/21620_neues_feld_zum_erfassen_des_ESI.php @@ -89,8 +89,10 @@ if (!$result = @$db->db_query('SELECT 0 FROM public.tbl_kennzeichen WHERE 0 = 1' ALTER TABLE public.tbl_kennzeichen ADD CONSTRAINT pk_tbl_kennzeichen PRIMARY KEY (kennzeichen_id); - ALTER TABLE public.tbl_kennzeichen ADD CONSTRAINT fk_kennzeichen_person FOREIGN KEY (person_id) REFERENCES public.tbl_person(person_id) ON UPDATE CASCADE ON DELETE RESTRICT; - ALTER TABLE public.tbl_kennzeichen ADD CONSTRAINT fk_kennzeichen_kennzeichentyp_kurzbz FOREIGN KEY (kennzeichentyp_kurzbz) REFERENCES public.tbl_kennzeichentyp(kennzeichentyp_kurzbz) ON UPDATE CASCADE ON DELETE RESTRICT; + ALTER TABLE public.tbl_kennzeichen ADD CONSTRAINT fk_kennzeichen_person FOREIGN KEY (person_id) + REFERENCES public.tbl_person(person_id) ON UPDATE CASCADE ON DELETE RESTRICT; + ALTER TABLE public.tbl_kennzeichen ADD CONSTRAINT fk_kennzeichen_kennzeichentyp_kurzbz FOREIGN KEY (kennzeichentyp_kurzbz) + REFERENCES public.tbl_kennzeichentyp(kennzeichentyp_kurzbz) ON UPDATE CASCADE ON DELETE RESTRICT; -- create unique constraint, no person can have the same kennzeichen twice ALTER TABLE public.tbl_kennzeichen ADD CONSTRAINT uk_kennzeichen_person_id_inhalt UNIQUE (person_id, kennzeichentyp_kurzbz, inhalt); @@ -114,3 +116,31 @@ if (!$result = @$db->db_query('SELECT 0 FROM public.tbl_kennzeichen WHERE 0 = 1' else echo '
Granted privileges to vilesci on public.tbl_kennzeichen'; } + +// public.tbl_kennzeichentyp: add type esi +if ($result = $db->db_query("SELECT 1 FROM public.tbl_kennzeichentyp WHERE kennzeichentyp_kurzbz='esi'")) +{ + if($db->db_num_rows($result)==0) + { + $qry = "INSERT INTO public.tbl_kennzeichentyp(kennzeichentyp_kurzbz, bezeichnung) VALUES('esi', 'European Student Identifier');"; + + if(!$db->db_query($qry)) + echo 'Kennzeichentyp: '.$db->db_last_error().'
'; + else + echo '
Neuer Kennzeichentyp esi in public.tbl_kennzeichentyp hinzugefügt'; + } +} + +// system.tbl_jobtypes: add type esi +if ($result = $db->db_query("SELECT 1 FROM system.tbl_jobtypes WHERE type='generateESI'")) +{ + if($db->db_num_rows($result)==0) + { + $qry = "INSERT INTO system.tbl_jobtypes(type, description) VALUES('generateESI', 'Generate and save European Student Identifier');"; + + if(!$db->db_query($qry)) + echo 'Jobtyp: '.$db->db_last_error().'
'; + else + echo '
Neuer Jobtyp generateESI in system.tbl_jobtypes hinzugefügt'; + } +}