diff --git a/build/api/module/ueController.php b/build/api/module/ueController.php new file mode 100644 index 0000000..8cb9846 --- /dev/null +++ b/build/api/module/ueController.php @@ -0,0 +1,45 @@ + [OPT] The UE code + * + * @return ues The UE(s) data + * + ---------------------------------------------------------*/ + public static function get($args){ + $code = null; + extract($args); + + /* Get the ue repo */ + /** @var ue $ue_repo */ + $ue_repo = Repo::getRepo('ue'); + + + /* (1) Get All ues or 1 by its code (if set) */ + $fetched = $ue_repo->get($code); + + /* (3) Return data */ + return ['ues' => $fetched]; + + } + + +} \ No newline at end of file diff --git a/build/database/repo/ue.php b/build/database/repo/ue.php index 5998716..8e89c6f 100644 --- a/build/database/repo/ue.php +++ b/build/database/repo/ue.php @@ -13,53 +13,179 @@ use database\core\Repo_i; class ue extends Repo_i { - public function create(string $code, string $label, bool $required, float $volumeCours, float $volumeTD, float $volumeTP, bool $disabled = false, ?int $defaultFormation = null) : int { - $st = $this->pdo->prepare("INSERT INTO UE(code, label, required, volumeCours, volumeTD, volumeTP, disabled, Formation_idFormation) + + /* (1) Create a new UE + * + * @code The code of the UE + * @label The UE label (name) + * @volumeCours The UE required volume of COURSES + * @volumeTD The UE required volume of TD + * @volumeTP The UE required volume of TP + * @disabled If it is disabled + * @defaultFormation If there is a foreign key for a default formation (if only one formation) + * + * @return created_code Code of the created UE (NULL on error) + * + ---------------------------------------------------------*/ + public function create(string $code, string $label, bool $required, float $volumeCours, float $volumeTD, float $volumeTP, bool $disabled = false, ?int $defaultFormation = null) : ?int { + + /* (1) Prepare request */ + $st = $this->pdo->prepare("INSERT INTO UE(`code`, `label`, `required`, `volumeCours`, `volumeTD`, `volumeTP`, `disabled`, `Formation_idFormation`) VALUE(:code, :label, :required, :volCours, :volTD, :volTP, :disabled, :idFormation) "); - $st->execute([ - "code" => $code, - "label" => $label, - "required" => $required ? 1 : 0, - "volCours" => $volumeCours, - "volTD" => $volumeTD, - "volTP" => $volumeTP, - "disabled" => $disabled ? 1 : 0, + /* (2) Manage statement error */ + if( $st === false ) + return null; + + /* (3) Bind params and execute request */ + $success = $st->execute([ + "code" => $code, + "label" => $label, + "required" => $required ? 1 : 0, + "volCours" => $volumeCours, + "volTD" => $volumeTD, + "volTP" => $volumeTP, + "disabled" => $disabled ? 1 : 0, "idFormation" => $defaultFormation ]); + /* (4) Manage execution error */ + if( !$success ) + return null; + + /* (5) Return insert id */ return $this->pdo->lastInsertId(); + } - public function update(string $code, array $data) : bool{ - $updSt = ""; - foreach ($data as $key => $field){ - $updSt .= "$key = :$key,"; - } + /* (2) Updates an UE data + * + * @code The UE code + * @label [OPT] The UE's new label + * @required [OPT] Whether the UE is required + * @volumeCours [OPT] The UE's new volume of COURSES + * @volumeTD [OPT] The UE's new volume of TD + * @volumeTP [OPT] The UE's new volume of TP + * @disabled [OPT] Whether the UE is disabled + * @defaultFormation [OPT] The default formation foreign key + * + * @return updated Whether the update have been successful + * + ---------------------------------------------------------*/ + public function update(string $code, ?String $label, ?bool $required, ?float $volumeCours, ?float $volumeTD, ?float $volumeTP, ?bool $disabled, ?int $defaultFormation) : bool{ - $updSt = rtrim($updSt,","); + /* (1) Build request */ + $build_rq = []; + $bind_param = [ ':code' => $code ]; - $st = $this->pdo->prepare("UPDATE UE SET $updSt WHERE code = :code"); + if( !is_null($label) ){ $build_rq[] = '`label` = :label'; $bind_param[':label'] = $label; } + if( !is_null($required) ){ $build_rq[] = '`required` = :required'; $bind_param[':required'] = $required; } + if( !is_null($volumeCours) ){ $build_rq[] = '`volumeCours` = :volumeCours'; $bind_param[':volumeCours'] = $volumeCours; } + if( !is_null($volumeTD) ){ $build_rq[] = '`volumeTD` = :volumeTD'; $bind_param[':volumeTD'] = $volumeTD; } + if( !is_null($volumeTP) ){ $build_rq[] = '`volumeTP` = :volumeTP'; $bind_param[':volumeTP'] = $volumeTP; } + if( !is_null($disabled) ){ $build_rq[] = '`disabled` = :disabled'; $bind_param[':disabled'] = $disabled; } + if( !is_null($defaultFormation) ){ $build_rq[] = '`Formation_idFormation` = :defaultFormation'; $bind_param[':defaultFormation'] = $defaultFormation; } + + /* (2) ERROR if no updated field */ + if( count($build_rq) <= 0 || count($bind_param) <= 1 ) + return FALSE; + + /* (3) Build request */ + $sql_rq = "UPDATE `UE` SET ".implode(', ', $build_rq)." WHERE `code` = :code"; + + /* (4) Prepare statement */ + $st = $this->pdo->prepare($sql_rq); + + /* (5) Return execution success */ + return $st->execute($bind_param); - return $st->execute(array_merge($data,[ - "code" => $code - ])); } + + /* (3) Deletes an UE + * + * @code The UE code + * + * @return deleted Whether the UE have been deleeted successfully + * + ---------------------------------------------------------*/ public function delete(string $code) : bool { - $st = $this->pdo->prepare("DELETE FROM UE WHERE code = :code"); - return $st->execute([ - "code" => $code - ]); + /* (1) Prepare statement */ + $st = $this->pdo->prepare('DELETE FROM UE WHERE code = :code'); + + /* (2) Manage error */ + if( $st === false ) + return false; + + /* (3) Bind params and execute request */ + return $st->execute([ ':code' => $code ]); + } + + + /* (4) Disables an UE + * + * @code The UE code + * + * @return disabled Whether the UE have been disabled successfully + * + ---------------------------------------------------------*/ public function disable(string $code) : bool { - return $this->update($code, ["disabled" => 1]); + return $this->update($code, null, null, null, null, null, true, null); } + + + /* (5) Enables an UE + * + * @code The UE code + * + * @return enabled Whether the UE have been enabled successfully + * + ---------------------------------------------------------*/ public function enable(string $code) : bool { - return $this->update($code, ["disabled" => 0]); + return $this->update($code, null, null, null, null, null, false, null); + } + + + + + /* (6) Gets a UE by its code || getAll + * + * @code [OPT] The UE code, if not set, getAll() + * + * @return ues The UEs matching code (NULL on error) + * + ---------------------------------------------------------*/ + public function get(?String $code=null) : ?array{ + + /* (1) Manage if no id given */ + $cond = is_null($code) ? '' : ' WHERE `code` = :code'; + $parm = is_null($code) ? [] : [':code' => $code]; + + /* (2) Prepare Statement */ + $st = $this->pdo->prepare("SELECT * FROM `UE`$cond GROUP BY `label` ASC"); + + /* (3) Bind params and execute statement */ + if( is_bool($st) ) return []; + $success = $st->execute($parm); + + /* (4) Manage error */ + if( !$success ) + return []; + + /* (5) Get data */ + $fetched = $st->fetchAll(); + + /* (6) Return [] on no result */ + if( $fetched === false ) + return []; + + /* (7) Return data */ + return $fetched; + } } \ No newline at end of file diff --git a/config/modules.json b/config/modules.json index 6e1e102..3ab0843 100644 --- a/config/modules.json +++ b/config/modules.json @@ -131,6 +131,40 @@ } + }, + + "ue":{ + + "POST": { + "des": "Creates a new UE", + "per": [], + "par": { + "code": { "des": "UE code.", "typ": "varchar(2,30,alphanumeric)" }, + "label": { "des": "UE label", "typ": "varchar(2,30,alphanumeric)" }, + "required": { "des": "If UE is required", "typ": "bool" }, + "volumeCours": { "des": "Number of course hours for UE", "typ": "float" }, + "volumeTD": { "des": "Number of TD hours for UE", "typ": "float" }, + "volumeTP": { "des": "Number of TP hours for UE", "typ": "float" }, + "disabled": { "des": "Whether UE is disabled", "typ": "boolean" }, + "defaultFormation": { "des": "UID for optional default formation", "typ": "id", "opt": true } + }, + "out": { + "created_code": { "des": "Created UE code", "typ": "varchar(2,30,alphanumeric)" } + } + }, + + "GET": { + "des": "Get one or all UE", + "per": [], + "par": { + "URL0": { "des": "Optional UE code.", "typ": "varchar(2,30,alphanumeric)", "ren": "code", "opt": true } + }, + "out": { + "ues": { "des": "UE list", "typ": "array" } + } + } + + }, "formation": {