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

95 lines
1.6 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: lucas
* Date: 15/03/18
* Time: 11:47
*/
namespace api\module;
use database\core\Repo;
use database\repo\professor;
class departmentController
{
/* (1) Get 1 | all departments
*
* @id_dep<int> [OPT] Department id
*
* @return departments<array> Matching departments
*
---------------------------------------------------------*/
public function get($args){
$id_dep = null;
extract($args);
/** @var professor $prof_repo */
$prof_repo = Repo::getRepo('professor');
/* (1) Get the list of linked departments for this @cas_login */
$departments = $prof_repo->getLinkedDepartments($_SESSION['CAS']['login']);
/* (2) If no @id_dep -> return all */
if( is_null($id_dep) )
return ['departments' => $departments];
/* (3) If @id_dep -> Only add elements that are @id_dep */
$filtered = [];
foreach($departments as $dep){
if( $dep['idDep'] == $id_dep ){
$filtered[] = $dep;
break;
}
}
/* (4) Return filtered departments */
return ['departments' => $filtered];
}
public function put($args){
$department = 0;
extract($args);
/** @var professor $prof_repo */
$prof_repo = Repo::getRepo('professor');
$deps = $prof_repo->getLinkedDepartments($_SESSION['CAS']['login']);
if( count($deps) > 0 ){
foreach($deps as $dep){
if( $dep['idDep'] == $department){
$_SESSION['AvailableDepartments'] = $deps;
$_SESSION['CurrentDatabase'] = $dep['dbName'];
$_SESSION['CurrentDepartmentId'] = $dep['idDep'];
return ['switched' => true];
}
}
}
return ['switched' => false];
}
}