From ecc10431fe839874adc9edb0766270c0a76bef5e Mon Sep 17 00:00:00 2001 From: bison-paolo Date: Fri, 13 Jan 2017 17:59:58 +0100 Subject: [PATCH] Added method getLastStatuses to Prestudent model and controller --- .../controllers/api/v1/crm/Prestudent.php | 27 +++++++++++ application/models/crm/Prestudent_model.php | 47 +++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/application/controllers/api/v1/crm/Prestudent.php b/application/controllers/api/v1/crm/Prestudent.php index 98dd61aef..6ee24463a 100644 --- a/application/controllers/api/v1/crm/Prestudent.php +++ b/application/controllers/api/v1/crm/Prestudent.php @@ -89,6 +89,33 @@ class Prestudent extends APIv1_Controller } } + /** + * @return void + */ + public function getLastStatuses() + { + $person_id = $this->get('person_id'); + $studiensemester_kurzbz = $this->get('studiensemester_kurzbz'); + $studiengang_kz = $this->get('studiengang_kz'); + $status_kurzbz = $this->get('status_kurzbz'); + + if (isset($person_id)) + { + $result = $this->PrestudentModel->getLastStatuses( + $person_id, + $studiensemester_kurzbz, + $studiengang_kz, + $status_kurzbz + ); + + $this->response($result, REST_Controller::HTTP_OK); + } + else + { + $this->response(); + } + } + /** * @return void */ diff --git a/application/models/crm/Prestudent_model.php b/application/models/crm/Prestudent_model.php index 9353444cd..07fccde28 100644 --- a/application/models/crm/Prestudent_model.php +++ b/application/models/crm/Prestudent_model.php @@ -11,4 +11,51 @@ class Prestudent_model extends DB_Model $this->dbTable = 'public.tbl_prestudent'; $this->pk = 'prestudent_id'; } + + /** + * @return void + */ + public function getLastStatuses($person_id, $studiensemester_kurzbz = null, $studiengang_kz = null, $status_kurzbz = null) + { + // Checks if the operation is permitted by the API caller + if (($isEntitled = $this->isEntitled('public.tbl_prestudent', PermissionLib::SELECT_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) + return $isEntitled; + if (($isEntitled = $this->isEntitled('public.tbl_prestudentstatus', PermissionLib::SELECT_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) + return $isEntitled; + if (($isEntitled = $this->isEntitled('public.tbl_status', PermissionLib::SELECT_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR)) !== true) + return $isEntitled; + + $query = 'SELECT * + FROM public.tbl_prestudent p + JOIN ( + SELECT DISTINCT ON(prestudent_id) * + FROM public.tbl_prestudentstatus + WHERE prestudent_id IN (SELECT prestudent_id FROM public.tbl_prestudent WHERE person_id = ?) + ORDER BY prestudent_id, datum desc, insertamum desc + ) ps USING(prestudent_id) + JOIN public.tbl_status USING(status_kurzbz) + WHERE ps.ausbildungssemester = 1'; + + $parametersArray = array($person_id); + + if ($studiensemester_kurzbz != '') + { + array_push($parametersArray, $studiensemester_kurzbz); + $query .= ' AND ps.studiensemester_kurzbz = ?'; + } + + if (isset($studiengang_kz)) + { + array_push($parametersArray, $studiengang_kz); + $query .= ' AND p.studiengang_kz = ?'; + } + + if ($status_kurzbz != '') + { + array_push($parametersArray, $status_kurzbz); + $query .= ' AND ps.status_kurzbz = ?'; + } + + return $this->execQuery($query, $parametersArray); + } } \ No newline at end of file