[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); /* (2) Parse 'formations' from json array */ foreach($fetched as $f=>$v) $fetched[$f]['formations'] = \json_decode($v['formations']); /* (3) Return data */ return ['ues' => $fetched]; } /* (2) Creates a new UE * * @code The code of the UE * @label The UE label (name) * @required If the UE is required * @volumeCours The UE required volume of COURSES * @volumeTD The UE required volume of TD * @volumeTP The UE required volume of TP * @disabled [OPT] If it is disabled * @defaultFormation [OPT] If there is a foreign key for a default formation (if only one formation) * * @return created_code The created UE code (if no error) * ---------------------------------------------------------*/ public static function post($args){ $code = ""; $label = ""; $required = false; $volumeCours = 0; $volumeTD = 0; $volumeTP = 0; $disabled = true; $defaultFormation = -1; extract($args); /* Get the ue repo */ /** @var ue $ue_repo */ $ue_repo = Repo::getRepo('ue'); /* (1) Check if ue code already exists */ $exists = $ue_repo->get($code); /* (2) If found -> already exists */ if( count($exists) > 0 ) return ['error' => new Error(Err::AlreadyExists)]; /* (3) Else try to create */ $repo_rtn = $ue_repo->create( $code, $label, $required, $volumeCours, $volumeTD, $volumeTP, $disabled, is_int($defaultFormation) && $defaultFormation < 0 ? null : $defaultFormation ); /* (4) If repo error -> return it */ if( is_null($repo_rtn) ) return ['error' => new Error(Err::RepoError)]; /* (5) Else return UID */ return ['created_code' => $repo_rtn]; } /* (3) Deletes an existing UE * * @code The UE code * * @return deleted Whether it has been removed * ---------------------------------------------------------*/ public static function delete($args){ $code = ''; extract($args); /* Get the ue repo */ /** @var ue $ue_repo */ $ue_repo = Repo::getRepo('ue'); /* (1) Try to delete */ return ['deleted' => $ue_repo->delete($code)]; } /* (4) Edits an existing UE * * @code The code of the UE * @new_code [OPT] The new code of the UE * @label [OPT] The UE label (name) * @required [OPT] If the UE is required * @volumeCours [OPT] The UE required volume of COURSES * @volumeTD [OPT] The UE required volume of TD * @volumeTP [OPT] The UE required volume of TP * @disabled [OPT] If it is disabled * @defaultFormation [OPT] default formation for this UE (-1 to unset, NULL to ignore) * * @return updated Whether it has been updated * ---------------------------------------------------------*/ public static function put($args){ $code = ""; $new_code = ""; $label = ""; $required = false; $volumeCours = 0; $volumeTD = 0; $volumeTP = 0; $disabled = true; $defaultFormation = null; extract($args); /* Get the ue repo */ /** @var ue $ue_repo */ $ue_repo = Repo::getRepo('ue'); /* (1) Check for @new_code to be unique (not already used) ---------------------------------------------------------*/ if( !is_null($new_code) ){ /* (1) Check @new_code */ $exists = $ue_repo->get($new_code); /* (2) If found -> already exists */ if( count($exists) > 0 ) return ['error' => new Error(Err::AlreadyExists)]; } /* (2) Try to update ---------------------------------------------------------*/ return ['updated' => $ue_repo->update( $code, $new_code, $label, $required, $volumeCours, $volumeTD, $volumeTP, $disabled, $defaultFormation )]; } }