UE code * * @return groups The list of groups for this UE * ---------------------------------------------------------*/ public static function post($args){ $code = ""; $idProf = null; $volume = 0; $formations = []; extract($args); /* Get the cours repo */ /** @var cours $cours_repo */ $cours_repo = Repo::getRepo('cours'); /* (1) Try to create cours */ $created_id = $cours_repo->create($code, $idProf, $volume, $formations); /* (2) Manage error */ if( is_null($created_id) || !is_int($created_id) ) return ['error' => new Error(Err::RepoError)]; return ['created_id' => $created_id]; } /* (2) Get groups for a specific UE * * @code UE code * * @return groups The list of groups for this UE * ---------------------------------------------------------*/ public static function get($args){ $code = ""; extract($args); /* Get the cours repo */ /** @var cours $cours_repo */ $cours_repo = Repo::getRepo('cours'); /* (1) Try to fetch data */ $fetched = $cours_repo->getGroups($code); /* (2) Manage error */ if( is_null($fetched) || !is_array($fetched) ) return ['error' => new Error(Err::RepoError)]; /* (3) Parse JSON list */ foreach($fetched as $f=>$v) $fetched[$f]['formations'] = json_decode($v['formations']); return ['groups' => $fetched]; } /* (3) Updates an existing Cours * * @idCours Id of the Cours to update * @idProf [OPT] The new professor ID (-1 to unset) * @volume [OPT] The new number of hours for the Cours * @add_form List of ids of formations to add * @rem_form List of ids of formations to remove * * @return updated Whether the Cours have been updated * ---------------------------------------------------------*/ public static function put($args){ $idCours = -1; $idProf = null; $volume = null; $add_form = []; $rem_form = []; extract($args); /* Get the cours repo */ /** @var cours $cours_repo */ $cours_repo = Repo::getRepo('cours'); /* (1) If nothing to do -> error */ if( is_null($idProf) && is_null($volume) && count($add_form) + count($rem_form) == 0) return ['error' => new Error(Err::MissingParam)]; /* (2) If have to update @idProf */ if( !is_null($idProf) ){ // call repo $updated = $cours_repo->updateProf($idCours, $idProf < 0 ? NULL : $idProf ); // if error -> dispatch if( !$updated ) return ['error' => new Error(Err::RepoError)]; } /* (3) If have to update @volume */ if( !is_null($volume) ){ // call repo $updated = $cours_repo->updateVolume($idCours, $volume); // if error -> dispatch if( !$updated ) return ['error' => new Error(Err::RepoError)]; } /* (4) If have to add formations -> add them */ if( count($add_form) > 0 ){ foreach($add_form as $id_form) $cours_repo->linkFormation($id_form, $idCours); } /* (5) If have to remove formations -> remove them */ if( count($rem_form) > 0 ){ foreach($rem_form as $id_form) $cours_repo->unlinkFormation($id_form, $idCours); } return ['updated' => true]; } /* (4) Deletes an existing Cours * * @idCours Id of the Cours * * @return deleted Whether it has been deleted * ---------------------------------------------------------*/ public static function delete($args){ $idCours = 0; extract($args); /* Get the cours repo */ /** @var cours $cours_repo */ $cours_repo = Repo::getRepo('cours'); /* (1) Dispatch response from repo */ return ['deleted' => $cours_repo->delete($idCours)]; } }