ptut-vhost/build/api/module/professorController.php

240 lines
5.9 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: lucas
* Date: 27/02/18
* Time: 16:19
*/
namespace api\module;
use database\core\Repo;
use database\repo\professor;
use error\core\Error;
use error\core\Err;
class professorController{
/* (1) Returns 1 or all professors
*
* @prof_id<int> [OPT] The professor UID
*
* @return professors<array> The professor(s) data
*
---------------------------------------------------------*/
public static function get($args){
$prof_id = null;
$with_vh = 0;
extract($args);
/* Get the professor repo */
/** @var professor $prof_repo */
$prof_repo = Repo::getRepo('professor');
/* (1) If with VH data
---------------------------------------------------------*/
if( is_int($with_vh) && $with_vh === 1 ){
/* (1) Get All professors or 1 by its id (if set) */
$fetched = $prof_repo->getWithVH($prof_id);
/* (2) Return data */
return ['professors' => $fetched];
}
/* (2) If without VH data (only Professor.*)
---------------------------------------------------------*/
/* (1) Get All professors or 1 by its id (if set) */
$fetched = $prof_repo->get($prof_id);
/* (3) Return data */
return ['professors' => $fetched];
}
/* (2) Creates a new professor
*
* @lastName<String> The professor's lastName
* @firstName<String> The professor's firstName
* @category<int> The professor's category ID
* @hoursToDo<int> The professor's number of hours to do
* @initials<int> The professor's initials
* @isAdmin<bool> Whether the professor is an admin
* @casLogin<String> The professor's CAS username
*
* @return prof_id<int> The created professor UID (if no error)
*
---------------------------------------------------------*/
public static function post($args){
$lastName = "";
$firstName = "";
$category = 0;
$hoursToDo = 0;
$initials = "";
$isAdmin = false;
$casLogin = null;
extract($args);
/* Get the professor repo */
/** @var professor $prof_repo */
$prof_repo = Repo::getRepo('professor');
/* (1) Check for unique field to be unique
---------------------------------------------------------*/
/* (1) Check @firstName + @lastName */
$exists = $prof_repo->exists($lastName, $firstName, $casLogin);
/* (2) If found -> already exists */
if( !is_null($exists) )
return ['error' => new Error(Err::AlreadyExists)];
/* (3) if @casLogin -> check unique */
if( !is_null($casLogin) ){
/* (1) Check if @casLogin already exists */
$exists = $prof_repo->getByLogin($casLogin);
/* (2) If found -> already exists */
if( !is_null($exists) )
return ['error' => new Error(Err::AlreadyExists)];
}
/* (3) Else try to create */
$repo_rtn = $prof_repo->create(
$lastName,
$firstName,
$category,
$hoursToDo,
$initials,
$isAdmin,
is_null($casLogin) ? "" : $casLogin
);
/* (4) If repo error -> return it */
if( is_null($repo_rtn) )
return ['error' => new Error(Err::RepoError)];
/* (5) Else return UID */
return ['created_uid' => $repo_rtn];
}
/* (3) Deletes an existing professor
*
* @prof_id<int> The professor UID
*
* @return deleted<bool> Whether it has been removed
*
---------------------------------------------------------*/
public static function delete($args){
$prof_id = 0;
extract($args);
/* Get the professor repo */
/** @var professor $prof_repo */
$prof_repo = Repo::getRepo('professor');
/* (1) Try to delete */
return ['deleted' => $prof_repo->delete($prof_id)];
}
/* (4) Edits an existing professor
*
* @prof_id<int> The professor UID
* @lastName<String> [OPT] The professor's lastName
* @firstName<String> [OPT] The professor's firstName
* @category<int> [OPT] The professor's category ID
* @hoursToDo<int> [OPT] The professor's number of hours to do
* @initials<int> [OPT] The professor's initials
* @isAdmin<bool> [OPT] Whether the professor is an admin
* @casLogin<String> [OPT] The professor's CAS username
* @remCas<bool> [OPT] Whether to remove the CAS login (only works if @casLogin is NULL)
*
* @return updated<bool> Whether it has been updated
*
---------------------------------------------------------*/
public static function put($args){
$prof_id = 0;
$lastName = null;
$firstName = null;
$category = null;
$hoursToDo = null;
$initials = null;
$isAdmin = null;
$casLogin = null;
$remCas = true;
extract($args);
/* Get the professor repo */
/** @var professor $prof_repo */
$prof_repo = Repo::getRepo('professor');
/* (1) If @casLogin -> check unique
---------------------------------------------------------*/
if( !is_null($casLogin) ){
/* (1) Check if @casLogin already exists */
$exists_cas = $prof_repo->getByLogin($casLogin);
/* (2) If found -> already exists */
if( !is_null($exists_cas) )
return ['error' => new Error(Err::AlreadyExists)];
}
/* (2) If @lastName or @firstName -> check unique
---------------------------------------------------------*/
if( !is_null($firstName) || !is_null($lastName) ){
/* (1) Try to fetch @prof_id data */
$fetched = $prof_repo->get($prof_id);
/* (2) Error if cannot find */
if( count($fetched) < 1 )
return ['error' => new Error(Err::NoMatchFound)];
/* (3) Extract @firstName + @lastName */
$names = [
'lastName' => is_null($lastName) ? $fetched[0]['lastName'] : $lastName,
'firstName' => is_null($firstName) ? $fetched[0]['firstName'] : $firstName
];
/* (4) Check if exists */
$exists = $prof_repo->exists($names['lastName'], $names['firstName']);
/* (2) If found -> already exists */
if( !is_null($exists) )
return ['error' => new Error(Err::AlreadyExists)];
}
/* (3) Try to update */
return ['updated' => $prof_repo->update(
$prof_id,
$lastName,
$firstName,
$category,
$hoursToDo,
$initials,
$isAdmin,
$remCas === true && is_null($casLogin) ? '' : $casLogin
)];
}
}