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

164 lines
3.7 KiB
PHP
Raw Permalink Normal View History

2018-03-15 11:02:28 +00:00
<?php
/**
* Created by PhpStorm.
* User: lucas
* Date: 15/03/18
* Time: 11:47
*/
namespace api\module;
use database\core\Repo;
2018-05-09 15:44:40 +00:00
use database\repo\database;
use database\repo\department;
use database\repo\meta;
2018-05-09 15:44:40 +00:00
use database\repo\professor;
2018-03-15 11:02:28 +00:00
class departmentController
2018-03-15 11:02:28 +00:00
{
2018-03-20 10:47:54 +00:00
/* (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 meta $meta_repo */
$meta_repo = Repo::getRepo('meta');
2018-03-20 10:47:54 +00:00
/* (1) Get the list of linked departments for this @cas_login */
$departments = $meta_repo->get_prof_departments($_SESSION['CAS']['login']);
2018-03-20 10:47:54 +00:00
/* (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];
}
2018-03-15 11:02:28 +00:00
public function put($args){
$department = 0;
extract($args);
/** @var meta $meta_repo */
$meta_repo = Repo::getRepo('meta');
2018-03-15 11:02:28 +00:00
$deps = $meta_repo->get_prof_departments($_SESSION['CAS']['login']);
2018-03-15 11:02:28 +00:00
if( count($deps) > 0 ){
foreach($deps as $dep){
if( $dep['idDep'] == $department ){
$_SESSION['AvailableDepartments'] = $deps;
$_SESSION['CurrentDepartmentId'] = $dep['idDep'];
$_SESSION['VERSION'] = [
'list' => $dep['versions'],
'current' => null
];
// select version with default = 1
foreach($_SESSION['VERSION']['list'] as $v){
if( $v['default'] == 1 ){
$_SESSION['VERSION']['current'] = intval($v['iddatabase']);
$_SESSION['CurrentDatabase'] = $v['dbName'];
break;
}
}
// if no default -> select first
if( !is_int($_SESSION['VERSION']) ){
$_SESSION['VERSION']['current'] = intval($_SESSION['VERSION']['list'][0]['iddatabase']);
$_SESSION['CurrentDatabase'] = $_SESSION['VERSION']['list'][0]['dbName'];
}
2018-03-15 11:02:28 +00:00
return ['switched' => true];
2018-03-15 11:02:28 +00:00
}
2018-03-15 11:02:28 +00:00
}
}
return ['switched' => false];
2018-03-15 11:02:28 +00:00
}
2018-05-09 15:44:40 +00:00
public function post($args){
$name = null;
extract($args);
/** @var meta $metaRepo */
$metaRepo = Repo::getRepo("meta");
/** @var professor $profRep */
$profRep = Repo::getRepo("professor");
/** @var database $dbRepo */
$dbRepo = Repo::getRepo("database");
//create the department in the meta database
$depId = $metaRepo->createDepartment($name);
//link the current user to that department
$metaRepo->link($_SESSION['CAS']['login'],$depId);
//create the database and init the structure
$dbName = $dbRepo->init($depId);
//link the new database to the department
$metaRepo->createVersion("Version 1",$dbName,$depId,true);
//get the current user data from current department
$user = $profRep->get($_SESSION['CAS']['id'])[0];
//switch our connexion to that database
Repo::switchDatabase($dbName);
//create the default admin in the new database (categories are common to all department)
$profRep->create($user["lastName"],
$user["firstName"],
$user["Categorie_idCategorie"],
$user["hoursToDo"],
$user["abreviation"],
true,
$user["casLogin"]);
//update user session
$departments = $metaRepo->get_prof_departments($user["casLogin"]);
$_SESSION['AvailableDepartments'] = $departments;
//we are good now
return ["success" => true];
}
2018-03-15 11:02:28 +00:00
}