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 + // . AS _ + $select .= $tables[$t] . "." . $fields[$f] . " AS " . $tables[$t] . "_" . $fields[$f]; + if ($f < count($fields) - 1) $select .= ", "; + } + + if ($t < count($tables) - 1) $select .= ", "; + + $tableColumnsCountArray[$t] = count($fields); + } + + // Adds the select clause + $this->addSelect($select); + + // Execute the query + $resultDB = $this->db->get_where($this->dbTable, $where); + + // If everything went ok... + if ($resultDB) + { + // Converts the object that contains data, from the returned CI's object to an array + $resultArray = $resultDB->result(); + // Array that will contain all the mainTable records, and to each record the linked data + // of a side table + $returnArray = array(); + $returnArrayCounter = 0; // Array counter + $prevPK = null; // Previous primary key + + // Iterates the array that contains data from DB + for ($i = 0; $i < count($resultArray); $i++) + { + // Converts an object properties to an associative array + $objectVars = get_object_vars($resultArray[$i]); + // Temporary array that will contain a representation of every records returned from DB + // every element is an associative array that contains all the data of each table + $objTmpArray = array(); + + // Gets all the data of a single table from the returned record, and creates an object filled with these data + for ($f = 0; $f < count($tableColumnsCountArray); $f++) + { + $objTmpArray[$f] = new stdClass(); // Object that will represent a data set of a table + foreach (array_slice($objectVars, $f == 0 ? 0 : $tableColumnsCountArray[$f - 1], $tableColumnsCountArray[$f]) as $key => $value) + { + $objTmpArray[$f]->{str_replace($tables[$f] . "_", "", $key)} = $value; + } + } + + // Object that represents data of the main table + $mainTableObj = $objTmpArray[0]; + // Fill $returnArray with all data from mainTable, and for each element will link the data from the side tables + for ($t = 1; $t < count($tables); $t++) + { + // Object that represents data of the side table + $sideTableObj = $objTmpArray[$t]; + + $sideTableProperty = $tables[$t]; + if (is_array($sideTablesAliases)) + { + $sideTableProperty = $sideTablesAliases[$t -1]; + } + + if ($prevPK == $mainTableObj->{$this->pk}) + { + array_push($returnArray[$returnArrayCounter - 1]->{$sideTableProperty}, $sideTableObj); + } + else + { + $mainTableObj->{$sideTableProperty} = array($sideTableObj); + $returnArray[$returnArrayCounter++] = $mainTableObj; + } + + $prevPK = $mainTableObj->{$this->pk}; + } + } + + // Sets result with the standard success object that contains all the studiengang + $result = $this->_success($returnArray); + } + else + { + $result = $this->_error($resultDB); + } + + return $result; + } /** --------------------------------------------------------------- * Add a table to join with @@ -203,14 +320,13 @@ class DB_Model extends FHC_Model // Check rights for joined table // 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($joinTable), 's')) - return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->getBerechtigungKurzbz($joinTable), FHC_MODEL_ERROR); +// if (substr(get_called_class(), -6) == '_model') +// if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz($joinTable), 's')) +// return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->getBerechtigungKurzbz($joinTable), FHC_MODEL_ERROR); $this->db->join($joinTable, $cond, $type); - return $this->_success(TRUE); + return $this->_success(true); } /** --------------------------------------------------------------- @@ -226,7 +342,7 @@ class DB_Model extends FHC_Model $this->db->order_by($field, $type); - return $this->_success(TRUE); + return $this->_success(true); } /** --------------------------------------------------------------- @@ -234,15 +350,15 @@ class DB_Model extends FHC_Model * * @return void */ - public function addSelect($select) + public function addSelect($select, $escape = true) { // Check Class-Attributes and parameters if (is_null($select) || $select == '') return $this->_error(lang('fhc_'.FHC_NODBTABLE), FHC_MODEL_ERROR); - $this->db->select($select); + $this->db->select($select, $escape); - return $this->_success(TRUE); + return $this->_success(true); } /** --------------------------------------------------------------- @@ -275,7 +391,7 @@ class DB_Model extends FHC_Model $this->db->limit($start); } - return $this->_success(TRUE); + return $this->_success(true); } /** --------------------------------------------------------------- @@ -293,7 +409,6 @@ class DB_Model extends FHC_Model return $this->_error(lang('fhc_'.FHC_NOPK), FHC_MODEL_ERROR); // 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), 'd')) return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->getBerechtigungKurzbz($this->dbTable), FHC_MODEL_ERROR); @@ -392,7 +507,7 @@ class DB_Model extends FHC_Model else if (!$string && ($ch=='"' || $ch=="'")) { - $string = TRUE; + $string = true; $quote = $ch; } else diff --git a/application/models/organisation/Studiengang_model.php b/application/models/organisation/Studiengang_model.php index e32c33ed9..894ca0e5e 100644 --- a/application/models/organisation/Studiengang_model.php +++ b/application/models/organisation/Studiengang_model.php @@ -100,4 +100,39 @@ class Studiengang_model extends DB_Model return $this->_success($result->result()); } + + /** + * + */ + public function getStudienplan($studiensemester_kurzbz, $ausbildungssemester, $aktiv, $onlinebewerbung) + { + // Join table public.tbl_studiengang with table lehre.tbl_studienordnung on column studiengang_kz + $this->addJoin("lehre.tbl_studienordnung", "studiengang_kz"); + // Then join with table lehre.tbl_studienplan on column studienordnung_id + $this->addJoin("lehre.tbl_studienplan", "studienordnung_id"); + // Then join with table lehre.tbl_studienplan_semester on column studienplan_id + $this->addJoin("lehre.tbl_studienplan_semester", "studienplan_id"); + + // Ordering by studiengang_kz and studienplan_id + $this->addOrder("public.tbl_studiengang.studiengang_kz"); + $this->addOrder("lehre.tbl_studienplan.studienplan_id"); + + $result = $this->loadList( + "tbl_studiengang", + array( + "tbl_studienplan" + ), + 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 + ), + array( + "studienplaene" + ) + ); + + return $result; + } } \ No newline at end of file