diff --git a/application/controllers/api/v1/organisation/Studiengang2.php b/application/controllers/api/v1/organisation/Studiengang2.php index cb6606585..18e28ad26 100644 --- a/application/controllers/api/v1/organisation/Studiengang2.php +++ b/application/controllers/api/v1/organisation/Studiengang2.php @@ -62,121 +62,11 @@ class Studiengang2 extends APIv1_Controller // If $studiensemester_kurzbz and $ausbildungssemester are present if (isset($studiensemester_kurzbz) && isset($ausbildungssemester)) { - $result = null; // return variable - // Check & set if (!isset($aktiv)) $aktiv = "TRUE"; if (!isset($onlinebewerbung)) $onlinebewerbung = "TRUE"; - // Join table public.tbl_studiengang with table lehre.tbl_studienordnung on column studiengang_kz - $result = $this->StudiengangModel->addJoin("lehre.tbl_studienordnung", "studiengang_kz"); - if ($result->error == EXIT_SUCCESS) // If the API caller has the rights - { - // Then join with table lehre.tbl_studienplan on column studienordnung_id - $result = $this->StudiengangModel->addJoin("lehre.tbl_studienplan", "studienordnung_id"); - if ($result->error == EXIT_SUCCESS) // If the API caller has the rights - { - // Then join with table lehre.tbl_studienplan_semester on column studienplan_id - $result = $this->StudiengangModel->addJoin("lehre.tbl_studienplan_semester", "studienplan_id"); - if ($result->error == EXIT_SUCCESS) // If the API caller has the rights - { - // Select all fields from table public.tbl_studiengang and table lehre.tbl_studienplan - // The separator is used to keep data separated between table public.tbl_studiengang - // and table lehre.tbl_studienplan (keep 'em separated!) - $this->StudiengangModel->addSelect( - "public.tbl_studiengang.*, - '' as " . Studiengang2::$PROPERTIES_SEPARATOR . ", - lehre.tbl_studienplan.studienplan_id as tbl_sp_studienplan_id, - lehre.tbl_studienplan.studienordnung_id as tbl_sp_studienordnung_id, - lehre.tbl_studienplan.orgform_kurzbz as tbl_sp_orgform_kurzbz, - lehre.tbl_studienplan.version as tbl_sp_version, - lehre.tbl_studienplan.bezeichnung as tbl_sp_bezeichnung, - lehre.tbl_studienplan.regelstudiendauer as tbl_sp_regelstudiendauer, - lehre.tbl_studienplan.sprache as tbl_sp_sprache, - lehre.tbl_studienplan.aktiv as tbl_sp_aktiv, - lehre.tbl_studienplan.semesterwochen as tbl_sp_semesterwochen, - lehre.tbl_studienplan.testtool_sprachwahl as tbl_sp_testtool_sprachwahl, - lehre.tbl_studienplan.insertamum as tbl_sp_insertamum, - lehre.tbl_studienplan.insertvon as tbl_sp_insertvon, - lehre.tbl_studienplan.updateamum as tbl_sp_updateamum, - lehre.tbl_studienplan.updatevon as tbl_sp_updatevon, - lehre.tbl_studienplan.ext_id as tbl_sp_ext_id, - lehre.tbl_studienplan.ects_stpl as tbl_sp_ects_stpl, - lehre.tbl_studienplan.pflicht_sws as tbl_sp_pflicht_sws, - lehre.tbl_studienplan.pflicht_lvs as tbl_sp_pflicht_lvs" - ); - - // Ordering by studiengang_kz and studienplan_id - $this->StudiengangModel->addOrder("public.tbl_studiengang.studiengang_kz"); - $this->StudiengangModel->addOrder("lehre.tbl_studienplan.studienplan_id"); - - // Execute the query - $result = $this->StudiengangModel->loadWhere(array( - "lehre.tbl_studienplan_semester.studiensemester_kurzbz" => $studiensemester_kurzbz, - "lehre.tbl_studienplan_semester.semester" => $ausbildungssemester, - "public.tbl_studiengang.aktiv" => $aktiv, - "public.tbl_studiengang.onlinebewerbung" => $onlinebewerbung - )); - } - } - } - - // If everything went ok... - if (is_object($result) && $result->error == EXIT_SUCCESS && - is_array($result->retval) && count($result->retval) > 0) - { - $studiengangArray = array(); // Array that will contain all the studiengang - $countReturnArray = 0; // Array counter - $prevStudiengang_kz = null; // Previous studiengang key - - // Iterates the array that contains data from database - for ($i = 0; $i < count($result->retval); $i++) - { - $objStudiengang = new stdClass(); // New object that represent a studiengang - $objStudienplan = new stdClass(); // New object that represent a studienplan - $separator = false; - - // Getting all the properties as an array, of an element of the array that - // represents a single studiengang with its own studienplan - foreach (get_object_vars($result->retval[$i]) as $key => $value) - { - // If the current element is the separator then ignore it! - if ($key != Studiengang2::$PROPERTIES_SEPARATOR) - { - // Before the separator: studiengang - if (!$separator) - { - $objStudiengang->{$key} = $value; - } - else // After the separator: studienplan - { - $objStudienplan->{str_replace("tbl_sp_", "", $key)} = $value; - } - } - else - { - $separator = true; - } - } - - // If the current studiengang is the same as before, adds to it the studienplan - if ($prevStudiengang_kz == $objStudiengang->studiengang_kz) - { - array_push($studiengangArray[$countReturnArray - 1]->studienplaene, $objStudienplan); - } - // Otherwise creates the property studienplaene and adds the first studienplan, - // then adds the new studiengang to studiengangArray and sets prevStudiengang_kz - else - { - $objStudiengang->studienplaene = array($objStudienplan); - $studiengangArray[$countReturnArray++] = $objStudiengang; - $prevStudiengang_kz = $objStudiengang->studiengang_kz; - } - } - - // Sets result with the standard success object that contains all the studiengang - $result = $this->_success($studiengangArray); - } + $result = $this->StudiengangModel->getStudienplan($studiensemester_kurzbz, $ausbildungssemester, $aktiv, $onlinebewerbung); $this->response($result, REST_Controller::HTTP_OK); } diff --git a/application/core/DB_Model.php b/application/core/DB_Model.php index 954336898..6f4561d89 100644 --- a/application/core/DB_Model.php +++ b/application/core/DB_Model.php @@ -136,7 +136,6 @@ class DB_Model extends FHC_Model // Check rights only if this method is called from a model - //var_dump(get_called_class()); if (substr(get_called_class(), -6) == '_model') if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz($this->dbTable), 's')) return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->getBerechtigungKurzbz($this->dbTable), FHC_MODEL_ERROR); @@ -174,21 +173,139 @@ class DB_Model extends FHC_Model // Check rights // Check rights only if this method is called from a model - //var_dump(get_called_class()); if (substr(get_called_class(), -6) == '_model') if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz($this->dbTable), 's')) return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->getBerechtigungKurzbz($this->dbTable), FHC_MODEL_ERROR); - // DB-SELECT - if (is_null($where)) - $result = $this->db->get($this->dbTable); - else - $result = $this->db->get_where($this->dbTable, $where); + // Execute query + $result = $this->db->get_where($this->dbTable, $where); + if ($result) return $this->_success($result->result()); else return $this->_error($this->db->error(), FHC_DB_ERROR); } + + /** --------------------------------------------------------------- + * Load data and convert a record into a list of data from the main table, + * and linked to every element, the data from the side tables + * + * TODO: + * - Adding support for composed primary key + * - Adding support for cascading side tables + * + * @return array + */ + public function loadList($mainTable, $sideTables, $where = null, $sideTablesAliases = null) + { + // Check Class-Attributes + if (is_null($this->dbTable)) + return $this->_error(lang('fhc_'.FHC_NODBTABLE), FHC_MODEL_ERROR); + + // Check rights + // Check rights only if this method is called from a model + if (substr(get_called_class(), -6) == '_model') + if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz($this->dbTable), 's')) + return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->getBerechtigungKurzbz($this->dbTable), FHC_MODEL_ERROR); + + // List of tables on which it will work + $tables = array_merge(array($mainTable), $sideTables); + // Array that will contain the number of columns of each table + $tableColumnsCountArray = array(); + + // Generates the select clause based on the columns of each table + $select = ""; + for ($t = 0; $t < count($tables); $t++) + { + $fields = $this->db->list_fields($tables[$t]); // list of the columns of the current table + for ($f = 0; $f < count($fields); $f++) + { + // To avoid overwriting of the properties within the object returned by CI + // will be given an alias to every column, that will be composed with the following schema + //