From d4db49e09e36e33072c0799c0d25c09e3ca0cc71 Mon Sep 17 00:00:00 2001 From: cgfhtw Date: Fri, 26 Jul 2024 12:04:20 +0200 Subject: [PATCH] Status->isLastStatus corrected --- .../api/frontend/v1/stv/Status.php | 15 ++--- .../models/crm/Prestudentstatus_model.php | 64 +++++++++---------- 2 files changed, 34 insertions(+), 45 deletions(-) diff --git a/application/controllers/api/frontend/v1/stv/Status.php b/application/controllers/api/frontend/v1/stv/Status.php index 11db9f73d..158ad8a22 100644 --- a/application/controllers/api/frontend/v1/stv/Status.php +++ b/application/controllers/api/frontend/v1/stv/Status.php @@ -85,15 +85,8 @@ class Status extends FHCAPI_Controller { $result = $this->PrestudentstatusModel->checkIfLastStatusEntry($prestudent_id); + $result = $this->getDataOrTerminateWithError($result); - if (isError($result)) - { - return $this->terminateWithError($result, self::ERROR_TYPE_GENERAL); - } - if($result->retval == "1") - { - $this->terminateWithError($this->p->t('lehre','error_lastRole'), self::ERROR_TYPE_GENERAL); - } return $this->terminateWithSuccess($result); } @@ -963,7 +956,7 @@ class Status extends FHCAPI_Controller { return $this->terminateWithError($result, self::ERROR_TYPE_GENERAL); } - if($result->retval == "1") + if($result->retval) { //Berechtigungen nach Check prüfen! if(!$isBerechtigtAdmin && !$isBerechtigtNoStudstatusCheck) @@ -1098,13 +1091,13 @@ class Status extends FHCAPI_Controller $logId = $result->retval; //Delete Studentlehrverband if no Status left - $result = $this->PrestudentstatusModel->checkIfLastStatusEntry($prestudent_id, true); + $result = $this->PrestudentstatusModel->checkIfLastStudentStatusEntry($prestudent_id); if (isError($result)) { return $this->terminateWithError(getError($result), self::ERROR_TYPE_GENERAL); } - if ($result->retval == "1") + if ($result->retval) { //get student_uid $this->load->model('crm/Student_model', 'StudentModel'); diff --git a/application/models/crm/Prestudentstatus_model.php b/application/models/crm/Prestudentstatus_model.php index bb88c0f8c..f99a8e73f 100644 --- a/application/models/crm/Prestudentstatus_model.php +++ b/application/models/crm/Prestudentstatus_model.php @@ -484,48 +484,44 @@ class Prestudentstatus_model extends DB_Model /** * Check if there is only one prestudentstatus left - * @param integer $prestudent_id - * @return success("1") if last prestudentstatusentry, else success("0") + * + * @param integer $prestudent_id + * + * @return stdClass */ - public function checkIfLastStatusEntry($prestudent_id, $isStudent=false ) + public function checkIfLastStatusEntry($prestudent_id) { - $qry = "SELECT - COUNT(*) as anzahl - FROM - public.tbl_prestudentstatus - WHERE - prestudent_id = ? - "; + $this->addSelect('COUNT(*) AS anzahl', false); - if($isStudent) - { - $qry .= "AND status_kurzbz = 'Student'"; - } - - $result = $this->execQuery($qry, array($prestudent_id)); + $result = $this->loadWhere([ + 'prestudent_id' => $prestudent_id + ]); if (isError($result)) - { - return error($result); - } + return $result; - $resultObject = current(getData($result)); + $resultObject = current($result->retval); - if (property_exists($resultObject, 'anzahl')) - { - $anzahl = (int) $resultObject->anzahl; - if ($anzahl <= 1 ) - { - return success("1", $this->p->t('lehre','error_lastRole')); - } - else - return success("0", $this->p->t('lehre','anzahl_existingRoles', ['anzahl' => $anzahl])); + $anzahl = (int)$resultObject->anzahl; - } - else - { - return error($result); - } + if ($anzahl <= 1) + return success(true, $this->p->t('lehre', 'error_lastRole')); + + return success(false, $this->p->t('lehre', 'anzahl_existingRoles', ['anzahl' => $anzahl])); + } + + /** + * Check if there is only one "Student" prestudentstatus left + * + * @param integer $prestudent_id + * + * @return stdClass + */ + public function checkIfLastStudentStatusEntry($prestudent_id) + { + $this->db->where('status_kurzbz', self::STATUS_STUDENT); + + return $this->checkIfLastStatusEntry($prestudent_id); } public function getAllPrestudentstatiWithStudiensemester($prestudent_id)