[module.professor.filter] can now filter by 'categories' + [repo.category] implemented getProfessors() + [webpack.teacher.view] instant-filter for 'categories' -> will propably help to have categories to update category label when prof edited)

This commit is contained in:
xdrm-brackets 2018-03-08 18:13:07 +01:00
parent 158b9d2cb0
commit 8a3d83503d
4 changed files with 97 additions and 16 deletions

View File

@ -10,6 +10,7 @@ namespace api\module\professor;
use database\core\Repo;
use database\repo\category;
use database\repo\formation;
use database\repo\ue;
use error\core\Error;
@ -20,6 +21,7 @@ class filterController{
/* (1) Get professor ID(s) matching a specific filter
*
* @categories<array> [OPT] Array of categories IDS
* @formations<array> [OPT] Array of formation IDS
* @ues<array> [OPT] Array of UE codes
*
@ -28,6 +30,7 @@ class filterController{
---------------------------------------------------------*/
public static function post($args){
$categories = [];
$formations = [];
$ues = [];
extract($args);
@ -39,11 +42,12 @@ class filterController{
/* (1) If no filter -> return error
---------------------------------------------------------*/
/* (1) Pre-process conditions */
$no_category = !is_array($categories) || count($categories) < 1;
$no_ue = !is_array($ues) || count($ues) < 1;
$no_formation = !is_array($formations) || count($formations) < 1;
/* (2) Exit if no filter */
if( $no_ue && $no_formation )
if( $no_category && $no_ue && $no_formation )
return ['error' => new Error(Err::MissingParam, 'You must give at least 1 parameter')];
/* (3) Init. result array (only keys used for unicity) */
@ -51,7 +55,35 @@ class filterController{
/* (2) Filter by formation
/* (2) Filter by categories
---------------------------------------------------------*/
if( is_array($categories) ){
/** @var category $cat_repo */
$cat_repo = Repo::getRepo('category');
/* (1) For each formation -> get request */
foreach($categories as $cat_id){
// 1. Ignore if wrong format
if( !is_numeric($cat_id) || !is_int($cat_id) )
continue;
// 2. Get from repo
$fetched_ids = $cat_repo->getProfessors( intval($cat_id) );
// 3. Add in unique set
foreach($fetched_ids as $prof_id)
$matches_uniq[ intval($prof_id['idProfesseur']) ] = null;
}
}
/* (3) Filter by formation
---------------------------------------------------------*/
if( is_array($formations) ){
@ -79,7 +111,7 @@ class filterController{
/* (3) Filter by ue
/* (4) Filter by ue
---------------------------------------------------------*/
if( is_array($ues) ){

View File

@ -105,4 +105,37 @@ class category extends Repo_i{
}
/* (5) Gets all professors who teaches a category by ids (array)
*
* @cat_id<int> The category id
*
* @return professors<array> The professors' UID matching the @cat_id category
*
---------------------------------------------------------*/
public function getProfessors(int $cat_id) : array{
/* (1) Prepare statement */
$st = $this->pdo->prepare("SELECT idProfesseur FROM Professeur WHERE Categorie_idCategorie = :cat_id;");
/* (2) Bind params and execute statement */
if( is_bool($st) ) return [];
$success = $st->execute([ ':cat_id' => $cat_id ]);
/* (3) Manage error */
if( !$success )
return [];
/* (4) Get data */
$fetched = $st->fetchAll();
/* (5) Return [] on no result */
if( $fetched === false )
return [];
/* (6) Return data */
return $fetched;
}
}

View File

@ -158,6 +158,7 @@
"des": "Get matching professors (only ids) for a given filter",
"per": [],
"par": {
"categories": { "des": "Optional category ID list", "typ": "array<id>", "opt": true },
"formations": { "des": "Optional formation ID list", "typ": "array<id>", "opt": true },
"ues": { "des": "Optional UE code list", "typ": "array<varchar(2,30,alphanumeric)>", "opt": true }
},

View File

@ -106,7 +106,14 @@ gstore.add('filter_handler', function(){
/* (3) Get Filters
---------------------------------------------------------*/
/* (1) Define filter group show/hide */
/* (1) Define global filter */
gstore.add('filters', {
categories: [{ visible: false, active: [] }],
formations: [{ visible: false, active: [] }],
ues: [{ visible: false, active: [] }]
});
/* (2) Define filter group show/hide */
gstore.add('show_fgroup', function(gname){
var opened = gstore.get.filters[gname] != null && gstore.get.filters[gname][0].visible;
@ -124,7 +131,7 @@ gstore.add('show_fgroup', function(gname){
});
/* (2) Define filter item toggle */
/* (3) Define filter item toggle */
gstore.add('toggle_filter', function(gname, i){
// {1} If wrong @gname -> abort //
@ -140,20 +147,11 @@ gstore.add('toggle_filter', function(gname, i){
// {4} Update active table //
gstore.get.filters[gname][0].active.splice(0);
console.log(gstore.get.filters[gname][0].active);
for( var f = 1 ; f < gstore.get.filters[gname].length ; f++ )
if( gstore.get.filters[gname][f].active )
gstore.get.filters[gname][0].active.push(f);
console.log(gstore.get.filters[gname][0].active);
});
/* (3) Define global filter */
gstore.add('filters', {
formations: [{ visible: false, active: [] }],
ues: [{ visible: false, active: [] }]
});
/* (4) Get Formations */
@ -190,6 +188,23 @@ api.call('GET ue', {}, function(rs){
});
/* (6) Get professor categories */
api.call('GET category', {}, function(rs){
// {1} If error -> abort //
if( rs.error !== 0 ) return console.log('No category found, error: '+rs.error);
console.log(rs);
// {2} Format category filters //
for( var i = 0 ; i < rs.categories.length ; i++ )
gstore.get.filters.categories.push({
code: rs.categories[i].idCategorie,
name: rs.categories[i].labelCategorie,
active: false
});
});