ptut-vhost/build/api/module/ue/tdController.php

170 lines
3.6 KiB
PHP

<?php
namespace api\module\ue;
use database\core\Repo;
use database\repo\td;
use error\core\Error;
use error\core\Err;
class tdController{
/* (1) Create a new TD
*
* @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 td repo */
/** @var td $td_repo */
$td_repo = Repo::getRepo('td');
/* (1) Try to create td */
$created_id = $td_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 td repo */
/** @var td $td_repo */
$td_repo = Repo::getRepo('td');
/* (1) Try to fetch data */
$fetched = $td_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 TD
*
* @idTD<int> Id of the TD to update
* @idProf<int> [OPT] The new professor ID (-1 to unset)
* @volume<int> [OPT] The new number of hours for the TD
* @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 TD have been updated
*
---------------------------------------------------------*/
public static function put($args){
$idTD = -1;
$idProf = null;
$volume = null;
$add_form = [];
$rem_form = [];
extract($args);
/* Get the td repo */
/** @var td $td_repo */
$td_repo = Repo::getRepo('td');
/* (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 = $td_repo->updateProf($idTD, $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 = $td_repo->updateVolume($idTD, $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)
$td_repo->linkFormation($id_form, $idTD);
}
/* (5) If have to remove formations -> remove them */
if( count($rem_form) > 0 ){
foreach($rem_form as $id_form)
$td_repo->unlinkFormation($id_form, $idTD);
}
return ['updated' => true];
}
/* (4) Deletes an existing TD
*
* @idTD<int> Id of the TD
*
* @return deleted<bool> Whether it has been deleted
*
---------------------------------------------------------*/
public static function delete($args){
$idTD = 0;
extract($args);
/* Get the td repo */
/** @var td $td_repo */
$td_repo = Repo::getRepo('td');
/* (1) Dispatch response from repo */
return ['deleted' => $td_repo->delete($idTD)];
}
}