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

191 lines
4.3 KiB
PHP
Raw Normal View History

<?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 repos */
/** @var tp $tp_repo */
$tp_repo = Repo::getRepo('tp');
/** @var ue $ue_repo */
$ue_repo = Repo::getRepo('ue');
/* (1) Fetch default formation from UE
---------------------------------------------------------*/
/* (1) Try to fetch the TP' UE */
$fetched_ue = $ue_repo->get($code);
/* (2) Manage error */
if( !is_array($fetched_ue) || count($fetched_ue) < 1 )
return ['error' => new Error(Err::RepoError)];
$defaultForm = intval($fetched_ue[0]['idForm']);
/* (3) Add to formation list if got a valid default formation */
if( is_int($defaultForm) && $defaultForm >= 0 && !in_array($defaultForm, $formations) )
$formations[] = $defaultForm;
/* (2) Create the 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, 'formations' => $formations];
}
/* (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)];
}
}