170 lines
3.6 KiB
PHP
170 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace api\module\ue;
|
|
|
|
|
|
use database\core\Repo;
|
|
use database\repo\tp;
|
|
use error\core\Error;
|
|
use error\core\Err;
|
|
|
|
class tpController{
|
|
|
|
|
|
/* (1) Create a new TP
|
|
*
|
|
* @code<String> UE code
|
|
*
|
|
* @return groups<array> The list of groups for this UE
|
|
*
|
|
---------------------------------------------------------*/
|
|
public static function post($args){
|
|
$code = "";
|
|
$idProf = null;
|
|
$volume = 0;
|
|
$formations = [];
|
|
extract($args);
|
|
|
|
/* Get the tp repo */
|
|
/** @var tp $tp_repo */
|
|
$tp_repo = Repo::getRepo('tp');
|
|
|
|
/* (1) Try to create tp */
|
|
$created_id = $tp_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<String> UE code
|
|
*
|
|
* @return groups<array> The list of groups for this UE
|
|
*
|
|
---------------------------------------------------------*/
|
|
public static function get($args){
|
|
$code = "";
|
|
extract($args);
|
|
|
|
/* Get the tp repo */
|
|
/** @var tp $tp_repo */
|
|
$tp_repo = Repo::getRepo('tp');
|
|
|
|
/* (1) Try to fetch data */
|
|
$fetched = $tp_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 TP
|
|
*
|
|
* @idTP<int> Id of the TP to update
|
|
* @idProf<int> [OPT] The new professor ID (-1 to unset)
|
|
* @volume<int> [OPT] The new number of hours for the TP
|
|
* @add_form<array> List of ids of formations to add
|
|
* @rem_form<array> List of ids of formations to remove
|
|
*
|
|
* @return updated<bool> Whether the TP have been updated
|
|
*
|
|
---------------------------------------------------------*/
|
|
public static function put($args){
|
|
$idTP = -1;
|
|
$idProf = null;
|
|
$volume = null;
|
|
$add_form = [];
|
|
$rem_form = [];
|
|
extract($args);
|
|
|
|
/* Get the tp repo */
|
|
/** @var tp $tp_repo */
|
|
$tp_repo = Repo::getRepo('tp');
|
|
|
|
/* (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 = $tp_repo->updateProf($idTP, $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 = $tp_repo->updateVolume($idTP, $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)
|
|
$tp_repo->linkFormation($id_form, $idTP);
|
|
|
|
}
|
|
|
|
/* (5) If have to remove formations -> remove them */
|
|
if( count($rem_form) > 0 ){
|
|
|
|
foreach($rem_form as $id_form)
|
|
$tp_repo->unlinkFormation($id_form, $idTP);
|
|
|
|
}
|
|
|
|
|
|
return ['updated' => true];
|
|
|
|
}
|
|
|
|
|
|
/* (4) Deletes an existing TP
|
|
*
|
|
* @idTP<int> Id of the TP
|
|
*
|
|
* @return deleted<bool> Whether it has been deleted
|
|
*
|
|
---------------------------------------------------------*/
|
|
public static function delete($args){
|
|
$idTP = 0;
|
|
extract($args);
|
|
|
|
/* Get the tp repo */
|
|
/** @var tp $tp_repo */
|
|
$tp_repo = Repo::getRepo('tp');
|
|
|
|
/* (1) Dispatch response from repo */
|
|
return ['deleted' => $tp_repo->delete($idTP)];
|
|
|
|
}
|
|
|
|
|
|
} |