refactor stv Pruefung Tab and API endpoint to use same logic for insert and update, use luxon lib to initialize datepicker with iso date and send iso date format to api endpoint

This commit is contained in:
Harald Bamberger
2025-11-24 16:26:11 +01:00
parent cdc1933ce4
commit 6b4f5778c2
4 changed files with 208 additions and 245 deletions
@@ -178,175 +178,7 @@ class Pruefung extends FHCAPI_Controller
*/
public function insertPruefung()
{
$authUID = getAuthUID();
$this->load->library('form_validation');
$this->form_validation->set_rules('lehreinheit_id', $this->p->t('lehre', 'lehreinheit'), 'required', [
'required' => $this->p->t('ui', 'error_fieldRequired', ['field' => $this->p->t('lehre', 'lehreinheit')]),
]);
$this->form_validation->set_rules('pruefungstyp_kurzbz', $this->p->t('lehre', 'pruefung'), 'required', [
'required' => $this->p->t('ui', 'error_fieldRequired', ['field' => $this->p->t('global', 'typ')]),
]);
$this->form_validation->set_rules(
'datum',
$this->p->t('global', 'datum'),
['is_valid_date']
);
if ($this->form_validation->run() == false)
{
$this->terminateWithValidationErrors($this->form_validation->error_array());
}
$this->load->model('education/Zeugnisnote_model', 'ZeugnisnoteModel');
$this->PruefungModel->db->trans_start();
if ($this->input->post('pruefungstyp_kurzbz') == "Termin2")
{
//Wenn ein 2. Termin angelegt wird, und kein 1. Termin vorhanden ist,
//dann wird auch ein 1. Termin angelegt mit der derzeitigen Zeugnisnote
$resultP = $this->PruefungModel->loadWhere(array(
'lehreinheit_id' => $this->input->post('lehreinheit_id'),
'student_uid' => $this->input->post('student_uid'),
'pruefungstyp_kurzbz' => 'Termin1'));
$termin1 = $this->getDataOrTerminateWithError($resultP);
if (!$termin1)
{
//check if existing Zeugnisnote
$this->ZeugnisnoteModel->addJoin('lehre.tbl_lehreinheit', 'lehrveranstaltung_id');
$this->ZeugnisnoteModel->db->where(
'lehre.tbl_zeugnisnote.studiensemester_kurzbz',
'lehre.tbl_lehreinheit.studiensemester_kurzbz',
false
);
$resultP = $this->ZeugnisnoteModel->loadWhere(array(
'lehrveranstaltung_id' => $this->input->post('lehrveranstaltung_id'),
'student_uid' => $this->input->post('student_uid')
));
$zeugnisnoten = $this->getDataOrTerminateWithError($resultP);
if ($zeugnisnoten)
{
$zeugnisnote = current($zeugnisnoten);
$resultN = $this->PruefungModel->insert([
'lehreinheit_id' => $this->input->post('lehreinheit_id'),
'student_uid' => $this->input->post('student_uid'),
'mitarbeiter_uid' => $this->input->post('mitarbeiter_uid'),
'datum' => $zeugnisnote->benotungsdatum,
'pruefungstyp_kurzbz' => 'Termin1',
'note' => $zeugnisnote->note,
'punkte' => $zeugnisnote->punkte,
'anmerkung' => 'automatisiert aus Zeugnisnote erstellt',
'insertamum' => date('c'),
'insertvon' => $authUID,
]);
$this->getDataOrTerminateWithError($resultN);
}
//Wenn keine Zeugnisnote vorhanden ist, dann wird kein
//1.Termin angelegt
}
}
$result = $this->PruefungModel->insert([
'lehreinheit_id' => $this->input->post('lehreinheit_id'),
'student_uid' => $this->input->post('student_uid'),
'mitarbeiter_uid' => $this->input->post('mitarbeiter_uid'),
'datum' => $this->input->post('datum'),
'pruefungstyp_kurzbz' => $this->input->post('pruefungstyp_kurzbz'),
'note' => $this->input->post('note'),
'anmerkung' => $this->input->post('anmerkung'),
'insertamum' => date('c'),
'insertvon' => $authUID,
'punkte' => $this->input->post('punkte') ? str_replace(',', '.', $this->input->post('punkte')) : null
]);
$this->getDataOrTerminateWithError($result);
//get studiensemester_kurzbz and lehreveranstaltung_id from lehreinheit
$this->load->model('education/Lehreinheit_model', 'LehreinheitModel');
$result = $this->LehreinheitModel->load($this->input->post('lehreinheit_id'));
$lehreinheiten = $this->getDataOrTerminateWithError($result);
if (!$lehreinheiten) {
$this->terminateWithValidationErrors([
'lehreinheit_id' => $this->p->t('ui', 'error_fieldNotFound', [
'field' => $this->p->t('lehre', 'lehreinheit')
])
]);
}
$lehreinheit = current($lehreinheiten);
$studiensemester_kurzbz = $lehreinheit->studiensemester_kurzbz;
$lehrveranstaltung_id = $lehreinheit->lehrveranstaltung_id;
//check if existing zeugnisnote
$result = $this->ZeugnisnoteModel->loadWhere(array(
'lehrveranstaltung_id' => $lehrveranstaltung_id,
'student_uid' => $this->input->post('student_uid'),
'studiensemester_kurzbz' => $studiensemester_kurzbz
));
$zeugnisnoten = $this->getDataOrTerminateWithError($result);
if (!$zeugnisnoten)
{
//insert zeugnisnote, if not existing
$result = $this->ZeugnisnoteModel->insert(array(
'lehrveranstaltung_id' => $lehrveranstaltung_id,
'student_uid' => $this->input->post('student_uid'),
'studiensemester_kurzbz' => $studiensemester_kurzbz,
'note' => $this->input->post('note'),
'uebernahmedatum' => date('c'),
'benotungsdatum' => $this->input->post('datum'),
'insertamum' => date('c'),
'insertvon' => $authUID,
'punkte' => $this->input->post('punkte') ? str_replace(',', '.', $this->input->post('punkte')) : null
));
$this->getDataOrTerminateWithError($result);
$this->PruefungModel->db->trans_complete();
$this->terminateWithSuccess();
}
$note = current($zeugnisnoten);
$uebernahmedatum = new DateTime($note->uebernahmedatum);
$benotungsdatum = new DateTime($note->benotungsdatum);
$pruefungsdatum = new DateTime($this->input->post('datum'));
$checkDate = $uebernahmedatum === '' || $benotungsdatum > $uebernahmedatum
? $benotungsdatum
: $uebernahmedatum;
if ($checkDate >= $pruefungsdatum && $this->input->post('note') !== $note->note)
{
$this->PruefungModel->db->trans_complete();
$this->terminateWithSuccess($this->p->t('exam', 'hinweis_changeAfterExamDate'));
}
//update zeugnisnote, if existing and valid datum
$result = $this->ZeugnisnoteModel->update([
'lehrveranstaltung_id' => $lehrveranstaltung_id,
'student_uid' => $this->input->post('student_uid'),
'studiensemester_kurzbz' => $studiensemester_kurzbz
], [
'note' => $this->input->post('note'),
'uebernahmedatum' => date('c'),
'benotungsdatum' => $this->input->post('datum'),
'updateamum' => date('c'),
'updatevon' => $authUID,
'punkte' => $this->input->post('punkte') ? str_replace(',', '.', $this->input->post('punkte')) : null
]);
$this->PruefungModel->db->trans_complete();
$this->terminateWithSuccess();
$this->insertOrUpdatePruefung();
}
/**
@@ -355,8 +187,6 @@ class Pruefung extends FHCAPI_Controller
* @param pruefung_id
*
* @return success or error
*
* no impact on lehre.tbl_zeugnisnote
*/
public function updatePruefung($pruefung_id)
{
@@ -366,48 +196,7 @@ class Pruefung extends FHCAPI_Controller
if (!$oldpruefung)
show_404(); // Pruefung that should be updated does not exist
$authUID = getAuthUID();
$this->load->library('form_validation');
$this->form_validation->set_rules('lehrveranstaltung_id', $this->p->t('lehre', 'lehrveranstaltung'), 'required', [
'required' => $this->p->t('ui', 'error_fieldRequired', ['field' => $this->p->t('lehre', 'lehrveranstaltung')]),
]);
$this->form_validation->set_rules('lehreinheit_id', $this->p->t('lehre', 'lehreinheit'), 'required', [
'required' => $this->p->t('ui', 'error_fieldRequired', ['field' => $this->p->t('lehre', 'lehreinheit')]),
]);
$this->form_validation->set_rules('pruefungstyp_kurzbz', $this->p->t('lehre', 'pruefung'), 'required', [
'required' => $this->p->t('ui', 'error_fieldRequired', ['field' => $this->p->t('global', 'typ')]),
]);
$this->form_validation->set_rules(
'datum',
$this->p->t('global', 'datum'),
['is_valid_date']
);
if ($this->form_validation->run() == false)
{
$this->terminateWithValidationErrors($this->form_validation->error_array());
}
$result = $this->PruefungModel->update(
[
'pruefung_id' => $pruefung_id
],
[ 'lehreinheit_id' => $this->input->post('lehreinheit_id'),
'student_uid' => $this->input->post('student_uid'),
'mitarbeiter_uid' => $this->input->post('mitarbeiter_uid'),
'note' => $this->input->post('note'),
'pruefungstyp_kurzbz' => $this->input->post('pruefungstyp_kurzbz'),
'datum' => $this->input->post('datum'),
'anmerkung' => $this->input->post('anmerkung'),
'updatevon' => $authUID,
'updateamum' => date('c'),
]
);
$this->getDataOrTerminateWithError($result);
return $this->outputJsonSuccess(true);
$this->insertOrUpdatePruefung($pruefung_id);
}
/**
@@ -581,4 +370,198 @@ class Pruefung extends FHCAPI_Controller
return $this->terminateWithSuccess($data);
}
protected function insertOrUpdatePruefung($pruefung_id=null)
{
$authUID = getAuthUID();
$this->load->library('form_validation');
$this->form_validation->set_rules('lehreinheit_id', $this->p->t('lehre', 'lehreinheit'), 'required', [
'required' => $this->p->t('ui', 'error_fieldRequired', ['field' => $this->p->t('lehre', 'lehreinheit')]),
]);
$this->form_validation->set_rules('pruefungstyp_kurzbz', $this->p->t('lehre', 'pruefung'), 'required', [
'required' => $this->p->t('ui', 'error_fieldRequired', ['field' => $this->p->t('global', 'typ')]),
]);
$this->form_validation->set_rules(
'datum',
$this->p->t('global', 'datum'),
['is_valid_date']
);
if ($this->form_validation->run() == false)
{
$this->terminateWithValidationErrors($this->form_validation->error_array());
}
$this->load->model('education/Zeugnisnote_model', 'ZeugnisnoteModel');
$this->PruefungModel->db->trans_start();
if ($this->input->post('pruefungstyp_kurzbz') == "Termin2")
{
//Wenn ein 2. Termin angelegt wird, und kein 1. Termin vorhanden ist,
//dann wird auch ein 1. Termin angelegt mit der derzeitigen Zeugnisnote
$resultP = $this->PruefungModel->loadWhere(array(
'lehreinheit_id' => $this->input->post('lehreinheit_id'),
'student_uid' => $this->input->post('student_uid'),
'pruefungstyp_kurzbz' => 'Termin1'));
$termin1 = $this->getDataOrTerminateWithError($resultP);
if (!$termin1)
{
//check if existing Zeugnisnote
$this->ZeugnisnoteModel->addJoin('lehre.tbl_lehreinheit', 'lehrveranstaltung_id');
$this->ZeugnisnoteModel->db->where(
'lehre.tbl_zeugnisnote.studiensemester_kurzbz',
'lehre.tbl_lehreinheit.studiensemester_kurzbz',
false
);
$resultP = $this->ZeugnisnoteModel->loadWhere(array(
'lehrveranstaltung_id' => $this->input->post('lehrveranstaltung_id'),
'student_uid' => $this->input->post('student_uid')
));
$zeugnisnoten = $this->getDataOrTerminateWithError($resultP);
if ($zeugnisnoten)
{
$zeugnisnote = current($zeugnisnoten);
$resultN = $this->PruefungModel->insert([
'lehreinheit_id' => $this->input->post('lehreinheit_id'),
'student_uid' => $this->input->post('student_uid'),
'mitarbeiter_uid' => $this->input->post('mitarbeiter_uid'),
'datum' => $zeugnisnote->benotungsdatum,
'pruefungstyp_kurzbz' => 'Termin1',
'note' => $zeugnisnote->note,
'punkte' => $zeugnisnote->punkte,
'anmerkung' => 'automatisiert aus Zeugnisnote erstellt',
'insertamum' => date('c'),
'insertvon' => $authUID,
]);
$this->getDataOrTerminateWithError($resultN);
}
//Wenn keine Zeugnisnote vorhanden ist, dann wird kein
//1.Termin angelegt
}
}
if(intval($pruefung_id) > 0)
{
$result = $this->PruefungModel->update(
[
'pruefung_id' => $pruefung_id
],
[ 'lehreinheit_id' => $this->input->post('lehreinheit_id'),
'student_uid' => $this->input->post('student_uid'),
'mitarbeiter_uid' => $this->input->post('mitarbeiter_uid'),
'note' => $this->input->post('note'),
'pruefungstyp_kurzbz' => $this->input->post('pruefungstyp_kurzbz'),
'datum' => $this->input->post('datum'),
'anmerkung' => $this->input->post('anmerkung'),
'updatevon' => $authUID,
'updateamum' => date('c'),
]
);
}
else
{
$result = $this->PruefungModel->insert([
'lehreinheit_id' => $this->input->post('lehreinheit_id'),
'student_uid' => $this->input->post('student_uid'),
'mitarbeiter_uid' => $this->input->post('mitarbeiter_uid'),
'datum' => $this->input->post('datum'),
'pruefungstyp_kurzbz' => $this->input->post('pruefungstyp_kurzbz'),
'note' => $this->input->post('note'),
'anmerkung' => $this->input->post('anmerkung'),
'insertamum' => date('c'),
'insertvon' => $authUID,
'punkte' => $this->input->post('punkte') ? str_replace(',', '.', $this->input->post('punkte')) : null
]);
}
$this->getDataOrTerminateWithError($result);
//get studiensemester_kurzbz and lehreveranstaltung_id from lehreinheit
$this->load->model('education/Lehreinheit_model', 'LehreinheitModel');
$result = $this->LehreinheitModel->load($this->input->post('lehreinheit_id'));
$lehreinheiten = $this->getDataOrTerminateWithError($result);
if (!$lehreinheiten) {
$this->terminateWithValidationErrors([
'lehreinheit_id' => $this->p->t('ui', 'error_fieldNotFound', [
'field' => $this->p->t('lehre', 'lehreinheit')
])
]);
}
$lehreinheit = current($lehreinheiten);
$studiensemester_kurzbz = $lehreinheit->studiensemester_kurzbz;
$lehrveranstaltung_id = $lehreinheit->lehrveranstaltung_id;
//check if existing zeugnisnote
$result = $this->ZeugnisnoteModel->loadWhere(array(
'lehrveranstaltung_id' => $lehrveranstaltung_id,
'student_uid' => $this->input->post('student_uid'),
'studiensemester_kurzbz' => $studiensemester_kurzbz
));
$zeugnisnoten = $this->getDataOrTerminateWithError($result);
if (!$zeugnisnoten)
{
//insert zeugnisnote, if not existing
$result = $this->ZeugnisnoteModel->insert(array(
'lehrveranstaltung_id' => $lehrveranstaltung_id,
'student_uid' => $this->input->post('student_uid'),
'studiensemester_kurzbz' => $studiensemester_kurzbz,
'note' => $this->input->post('note'),
'uebernahmedatum' => date('c'),
'benotungsdatum' => $this->input->post('datum'),
'insertamum' => date('c'),
'insertvon' => $authUID,
'punkte' => $this->input->post('punkte') ? str_replace(',', '.', $this->input->post('punkte')) : null
));
$this->getDataOrTerminateWithError($result);
$this->PruefungModel->db->trans_complete();
$this->terminateWithSuccess();
}
$note = current($zeugnisnoten);
$uebernahmedatum = new DateTime($note->uebernahmedatum);
$benotungsdatum = new DateTime($note->benotungsdatum);
$pruefungsdatum = new DateTime($this->input->post('datum'));
$checkDate = $note->uebernahmedatum === '' || $benotungsdatum > $uebernahmedatum
? $benotungsdatum
: $uebernahmedatum;
if ($checkDate > $pruefungsdatum && $this->input->post('note') !== $note->note)
{
$this->PruefungModel->db->trans_complete();
$this->terminateWithSuccess($this->p->t('exam', 'hinweis_changeAfterExamDate'));
}
//update zeugnisnote, if existing and valid datum
$result = $this->ZeugnisnoteModel->update([
'lehrveranstaltung_id' => $lehrveranstaltung_id,
'student_uid' => $this->input->post('student_uid'),
'studiensemester_kurzbz' => $studiensemester_kurzbz
], [
'note' => $this->input->post('note'),
'uebernahmedatum' => date('c'),
'benotungsdatum' => $this->input->post('datum'),
'updateamum' => date('c'),
'updatevon' => $authUID,
'punkte' => $this->input->post('punkte') ? str_replace(',', '.', $this->input->post('punkte')) : null
]);
$this->PruefungModel->db->trans_complete();
$this->terminateWithSuccess();
}
}