2018-03-03 21:18:36 +00:00
/ * ( 1 ) L o a d p r o f e s s o r s
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
/* (1) Initialize list */
gstore . add ( 'professors' , [ ] ) ;
/* (2) Get professors */
2018-03-04 10:59:10 +00:00
api . call ( 'GET professor/1/' , { vh : true } , function ( rs ) {
2018-03-03 21:18:36 +00:00
// {1} If error -> abort //
2018-03-05 10:46:01 +00:00
if ( rs . error !== 0 ) return console . log ( 'No professor found, error: ' + rs . error ) ;
console . log ( rs ) ;
2018-03-03 21:18:36 +00:00
// {2} Store professors //
gstore . get . professors = rs . professors ;
2018-03-04 17:15:48 +00:00
} ) ;
2018-03-06 15:20:31 +00:00
/ * ( 2 ) L o a d c a t e g o r i e s ( f o r c r e a t i n g a p r o f e s s o r )
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
/* (1) Initialize list */
gstore . add ( 'categories' , [ ] ) ;
/* (2) Get categories */
api . call ( 'GET category' , { vh : true } , function ( rs ) {
// {1} If error -> abort //
if ( rs . error !== 0 ) return console . log ( 'No categorie found, error: ' + rs . error ) ;
console . log ( rs ) ;
// {2} Store categories //
gstore . get . categories = rs . categories ;
} ) ;
2018-03-05 17:20:40 +00:00
/ * ( 2 ) D e f i n e f i l t e r s ' c a l l b a c k
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
/* (1) Define global callback */
gstore . add ( 'filter_handler' , function ( ) {
var request = { } ;
/* (1) Get all filter types */
var filter _type = Object . keys ( gstore . get . filters ) ;
/* (2) Get all formated filter data */
for ( ftype of filter _type ) {
// 1. Initialize result filter
request [ ftype ] = [ ] ;
var fl = gstore . get . filters [ ftype ] . length ;
// 2. Add only @code for @active entries
for ( var f = 1 ; f < fl ; f ++ ) {
// 2.1. Store filter locally (for easier access)
var filter = gstore . get . filters [ ftype ] [ f ] ;
// 2.2. If active -> add to list
( filter . code != null && filter . active ) && request [ ftype ] . push ( filter . code ) ;
}
}
/* (3) Get professor id for matching filter */
api . call ( 'POST professor/filter' , request , function ( rs ) {
2018-03-06 13:49:48 +00:00
// 1. Display all on error
if ( rs . error !== 0 )
rs . matches = gstore . get . professors . map ( function ( prof , i ) { return i ; } ) ;
2018-03-05 17:20:40 +00:00
console . log ( rs ) ;
// 2. Fetch professor elements
var local _ptr = gstore . get . professors ;
var l = gstore . get . professors . length ;
// 3. For each element
for ( var e = 0 ; e < l ; e ++ ) {
// 3.1. Show by default
var element = document . querySelector ( 'section[data-id=\'' + local _ptr [ e ] . idProfesseur + '\']' ) ;
if ( ! element ) continue ;
element . remClass ( 'filter-hidden' ) ;
// 3.2. Only hide if does not match filter
2018-03-05 18:51:28 +00:00
if ( rs . matches . indexOf ( local _ptr [ e ] . idProfesseur ) <= - 1 )
2018-03-05 17:20:40 +00:00
element . addClass ( 'filter-hidden' ) ;
}
} ) ;
} ) ;
/ * ( 3 ) G e t F i l t e r s
2018-03-05 10:46:01 +00:00
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
2018-03-08 17:13:07 +00:00
/* (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 */
2018-03-08 17:00:39 +00:00
gstore . add ( 'show_fgroup' , function ( gname ) {
var opened = gstore . get . filters [ gname ] != null && gstore . get . filters [ gname ] [ 0 ] . visible ;
// {1} hide all by default//
for ( var f in gstore . get . filters )
gstore . get . filters [ f ] [ 0 ] . visible = false ;
// {2} If wrong @gname -> abort //
if ( gstore . get . filters [ gname ] == null )
return ;
// {3} Show selected filter //
gstore . get . filters [ gname ] [ 0 ] . visible = ! opened ;
} ) ;
2018-03-08 17:13:07 +00:00
/* (3) Define filter item toggle */
2018-03-08 17:00:39 +00:00
gstore . add ( 'toggle_filter' , function ( gname , i ) {
// {1} If wrong @gname -> abort //
if ( gstore . get . filters [ gname ] == null )
return ;
// {2} If wrong @i -> abort //
if ( gstore . get . filters [ gname ] [ i ] == null )
return ;
// {3} Toggle filter activation //
gstore . get . filters [ gname ] [ i ] . active = ! gstore . get . filters [ gname ] [ i ] . active ;
// {4} Update active table //
gstore . get . filters [ gname ] [ 0 ] . active . splice ( 0 ) ;
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 ) ;
2018-03-05 10:46:01 +00:00
} ) ;
2018-03-08 17:00:39 +00:00
/* (4) Get Formations */
2018-03-05 10:46:01 +00:00
api . call ( 'GET formation' , { } , function ( rs ) {
// {1} If error -> abort //
if ( rs . error !== 0 ) return console . log ( 'No formation found, error: ' + rs . error ) ;
console . log ( rs ) ;
// {2} Format UE filters //
for ( var i = 0 ; i < rs . formations . length ; i ++ )
gstore . get . filters . formations . push ( {
2018-03-05 17:20:40 +00:00
code : rs . formations [ i ] . idForm ,
2018-03-05 10:46:01 +00:00
name : rs . formations [ i ] . labelForm ,
active : false
} ) ;
} ) ;
2018-03-08 17:00:39 +00:00
/* (5) Get UEs */
2018-03-05 10:46:01 +00:00
api . call ( 'GET ue' , { } , function ( rs ) {
// {1} If error -> abort //
if ( rs . error !== 0 ) return console . log ( 'No UE found, error: ' + rs . error ) ;
console . log ( rs ) ;
// {2} Format UE filters //
for ( var i = 0 ; i < rs . ues . length ; i ++ )
gstore . get . filters . ues . push ( {
code : rs . ues [ i ] . code ,
name : rs . ues [ i ] . label ,
active : false
} ) ;
} ) ;
2018-03-08 17:13:07 +00:00
/* (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
} ) ;
} ) ;
2018-03-05 10:46:01 +00:00
2018-03-05 17:20:40 +00:00
/ * ( 4 ) M a n a g e I n s t a n t S e a r c h ( I S )
2018-03-04 17:15:48 +00:00
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
/* (1) Define global timeout index */
gstore . add ( 'is_to' , null ) ;
/* (2) Define search value buffer */
gstore . add ( 'is_buf' , null ) ;
/* (3) Define instant search function */
gstore . add ( 'is_handler' , function ( e ) {
/* (1) Remove last timeout */
if ( gstore . get . is _to != null )
clearTimeout ( gstore . get . is _to ) ;
/* (2) Store value in buffer */
gstore . get . is _buf = e . target . value . trim ( ) . toLowerCase ( ) ;
/* (3) Launch timeout (wait 1s) before filtering */
gstore . get . is _to = setTimeout ( function ( ) {
// 1. Fetch elements
var local _ptr = gstore . get . professors ;
var l = gstore . get . professors . length ;
// 2. For each element
for ( var e = 0 ; e < l ; e ++ ) {
2018-03-05 17:20:40 +00:00
// 2.1. Show by default
2018-03-04 17:15:48 +00:00
var element = document . querySelector ( 'section[data-id=\'' + local _ptr [ e ] . idProfesseur + '\']' ) ;
if ( ! element ) continue ;
2018-03-05 17:20:40 +00:00
element . remClass ( 'search-hidden' ) ;
2018-03-04 17:15:48 +00:00
// 2.2. Extract name components
2018-03-05 17:20:40 +00:00
var fname = local _ptr [ e ] . firstName . trim ( ) . toLowerCase ( ) ;
var lname = local _ptr [ e ] . lastName . trim ( ) . toLowerCase ( ) ;
var fullname = fname + ' ' + lname ;
// 2.3. Hide if does not match
var match _offset = gstore . get . is _buf . length == 0 || fname . search ( gstore . get . is _buf ) + lname . search ( gstore . get . is _buf ) + fullname . search ( gstore . get . is _buf ) ;
2018-03-04 17:15:48 +00:00
2018-03-05 17:20:40 +00:00
if ( match _offset <= - 3 )
element . addClass ( 'search-hidden' ) ;
2018-03-04 17:15:48 +00:00
}
2018-03-04 17:25:07 +00:00
} , 250 ) ;
2018-03-03 21:18:36 +00:00
} ) ;
2018-03-04 17:15:48 +00:00
2018-03-07 12:13:31 +00:00
2018-03-07 13:34:44 +00:00
/ * ( 5 ) M a n a g e i n s t a n t c r e a t e
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
2018-03-07 17:49:28 +00:00
/* (1) Initialize toggle show */
gstore . add ( 'create_card' , false ) ;
/* (2) Initialize inputs */
2018-03-07 13:34:44 +00:00
gstore . add ( 'create_cat' , '-' ) ;
gstore . add ( 'create_name' , '' ) ;
gstore . add ( 'create_cas' , '' ) ;
gstore . add ( 'create_h' , '' ) ;
2018-03-07 17:49:28 +00:00
/* (3) Initialize error message */
2018-03-07 13:43:04 +00:00
gstore . add ( 'create_err_valid' , false ) ;
2018-03-07 13:34:44 +00:00
gstore . add ( 'create_err' , '' ) ;
2018-03-30 15:32:12 +00:00
/* (4) Define reset handler */
gstore . add ( 'ic_reset' , function ( ) {
gstore . add ( 'create_cat' , '-' ) ;
gstore . add ( 'create_name' , '' ) ;
gstore . add ( 'create_cas' , '' ) ;
gstore . add ( 'create_h' , '' ) ;
gstore . add ( 'create_err_valid' , false ) ;
gstore . add ( 'create_err' , '' ) ;
} ) ;
/* (5) Define create handler */
2018-03-07 13:34:44 +00:00
gstore . add ( 'ic_handler' , function ( prof _id ) {
2018-03-07 17:49:28 +00:00
/* (4.1) Trim text input */
2018-03-07 13:34:44 +00:00
gstore . get . create _name = gstore . get . create _name . trim ( ) ;
gstore . get . create _cas = gstore . get . create _cas . trim ( ) . toLowerCase ( ) ;
gstore . get . create _h = gstore . get . create _h . trim ( ) ;
2018-03-07 17:49:28 +00:00
/* (4.2) Store values locally */
2018-03-07 13:34:44 +00:00
var cat = gstore . get . create _cat ;
var name = gstore . get . create _name . split ( ' ' ) ;
var cas = gstore . get . create _cas ;
var hour = gstore . get . create _h ;
2018-03-07 17:49:28 +00:00
/* (4.3) Init client-side check */
2018-03-07 13:34:44 +00:00
var errors = [ ] ;
2018-03-07 17:49:28 +00:00
/* (4.3.1) Check category */
2018-03-07 13:34:44 +00:00
if ( isNaN ( cat ) ) errors . push ( 'La catégorie de l\'enseignant est manquante' ) ;
2018-03-07 17:49:28 +00:00
/* (4.3.2) Check name */
2018-03-07 13:34:44 +00:00
if ( name . length !== 2 || name [ 0 ] . length < 2 || name [ 1 ] . length < 2 )
errors . push ( 'Le nom doit suivre le format "Prénom Nom"' ) ;
2018-03-07 17:49:28 +00:00
/* (4.3.3) Check CAS login */
2018-03-07 13:34:44 +00:00
if ( ! /^([a-z]{4,16})?$/ . test ( cas ) )
errors . push ( 'L\'identifiant doit être vide ou comprendre de 4 à 16 lettres' ) ;
2018-03-07 17:49:28 +00:00
/* (4.3.4) Check hours */
2018-03-07 13:34:44 +00:00
if ( hour === '' || isNaN ( hour ) || hour < 0 )
errors . push ( 'Le nombre d\'heures doit être un entier positif.' ) ;
2018-03-07 17:49:28 +00:00
/* (4.4) Show first error only (for 2s) */
2018-03-07 13:34:44 +00:00
if ( errors . length > 0 ) {
gstore . get . create _err = errors [ 0 ] ;
return setTimeout ( ( ) => gstore . add ( 'create_err' , '' ) , 2000 ) ;
}
2018-03-07 17:49:28 +00:00
/* (4.5.1) Création de la requête */
2018-03-07 13:34:44 +00:00
var rq = {
2018-03-07 13:43:04 +00:00
firstName : name [ 0 ] ,
lastName : name [ 1 ] ,
2018-03-07 13:34:44 +00:00
category : cat ,
2018-03-07 13:43:04 +00:00
initials : name [ 0 ] . substr ( 0 , 2 ) + name [ 1 ] . substr ( 0 , 2 ) ,
2018-03-07 13:34:44 +00:00
hoursToDo : hour ,
isAdmin : false
} ;
// optional cas_login
if ( cas . length > 0 ) rq . casLogin = cas ;
2018-03-07 17:49:28 +00:00
/* (4.5.2) Send request */
2018-03-07 13:34:44 +00:00
api . call ( 'POST professor' , rq , function ( rs ) {
console . log ( rs ) ;
2018-03-07 17:49:28 +00:00
/* (4.5.2.1) Manage 'already exist' error */
2018-03-07 13:34:44 +00:00
if ( rs . error == 29 ) {
2018-03-13 10:26:08 +00:00
gstore . get . create _err = 'Le couple Nom-Prénom ou l\'identifiant sont déja utilisés.' ;
2018-03-07 13:34:44 +00:00
return setTimeout ( ( ) => gstore . add ( 'create_err' , '' ) , 2000 ) ;
}
2018-03-07 17:49:28 +00:00
/* (4.5.2.2) Manage other errors */
2018-03-07 13:34:44 +00:00
if ( rs . error !== 0 ) {
gstore . get . create _err = 'erreur (' + rs . error + ') Impossible de créer l\'enseignant' ;
return setTimeout ( ( ) => gstore . add ( 'create_err' , '' ) , 2000 ) ;
}
2018-03-07 17:49:28 +00:00
/* (4.5.2.3) Show that user is created */
2018-03-07 13:43:04 +00:00
// display all is ok
gstore . add ( 'create_err_valid' , true ) ;
gstore . add ( 'create_err' , 'L\'enseignant a été créé, il s\'affichera au prochain rechargement' ) ;
// empty fields
gstore . get . create _cat = '-' ;
gstore . get . create _name = '' ;
gstore . get . create _cas = '' ;
gstore . get . create _h = '' ;
return setTimeout ( ( ) => {
gstore . add ( 'create_err' , '' ) ;
gstore . add ( 'create_err_valid' , false ) ;
} , 2000 ) ;
2018-03-07 13:34:44 +00:00
} ) ;
} ) ;
/ * ( 6 ) M a n a g e i n s t a n t r e m o v e
2018-03-07 12:13:31 +00:00
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
/* (1) Define remove handler */
gstore . add ( 'ir_handler' , function ( prof _id ) {
/* (1) Abort if wrong prof_id */
if ( prof _id == null || isNaN ( prof _id ) )
return ;
2018-03-08 11:45:09 +00:00
/* (2.1) Find index in gstore */
var gi = gstore . get . professors . map ( ( data , i ) => { return ( data . idProfesseur && data . idProfesseur == prof _id ) ? i : '' ; } ) . join ( '' ) ;
/* (2.2) Exit if not found */
if ( isNaN ( gi ) ) return ;
2018-03-08 12:58:30 +00:00
var local = gstore . get . professors [ gi ] ;
2018-03-08 11:45:09 +00:00
/* (3) Show popup */
2018-03-07 12:13:31 +00:00
( new Promise ( ( resolve , reject ) => {
popup . ask ( {
title : 'Confirmation de suppression' ,
2018-03-08 11:45:09 +00:00
content : "La suppression de l'enseignant <b>" + local . firstName + " " + local . lastName + "</b> est irréversible.<br><br>Voulez-vous le supprimer définitivement ?" ,
2018-03-07 12:13:31 +00:00
action : 'Supprimer' ,
type : 'invalid'
} , ( popup _rs ) => { popup _rs && resolve ( ) } ) ;
2018-03-08 11:45:09 +00:00
/* (4) On pop-up confirmation */
2018-03-07 12:13:31 +00:00
} ) ) . then ( function ( ) {
return new Promise ( ( resolve , reject ) => {
2018-03-08 11:45:09 +00:00
/* (4.1) Delete professor */
2018-03-07 12:13:31 +00:00
api . call ( 'DELETE professor/' + prof _id , { } , function ( rs ) {
2018-03-08 11:45:09 +00:00
/* (4.1.1) Abort on error */
2018-03-07 12:13:31 +00:00
if ( rs . error !== 0 || rs . deleted !== true )
return reject ( rs . error ) ;
2018-03-08 11:45:09 +00:00
/* (4.1.2) Success */
2018-03-07 12:13:31 +00:00
resolve ( ) ;
} ) ;
} ) ;
2018-03-08 11:45:09 +00:00
/* (5) On success */
2018-03-07 12:13:31 +00:00
} ) . then ( function ( ) {
2018-03-08 11:45:09 +00:00
/* remove from visible */
2018-03-07 12:13:31 +00:00
gstore . get . professors . splice ( gi , 1 ) ;
2018-03-08 11:45:09 +00:00
/* (6) On error */
2018-03-07 12:13:31 +00:00
} ) . catch ( function ( err _code ) {
popup . ask ( {
title : 'Error (' + err _code + ')' ,
2018-03-07 12:20:02 +00:00
content : 'La suppression a échouée. Veuillez réessayer ultérieurement.' ,
2018-03-07 12:13:31 +00:00
action : 'OK' ,
type : 'neutral'
} , ( ) => { } ) ;
} ) ;
2018-03-07 12:20:02 +00:00
} ) ;
2018-03-07 12:13:31 +00:00
2018-03-07 13:34:44 +00:00
/ * ( 7 ) M a n a g e i n s t a n t e d i t
2018-03-07 12:20:02 +00:00
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
2018-03-08 12:58:30 +00:00
/* (1) Init edit_mode */
gstore . add ( 'edit_i' , - 1 ) ;
2018-03-07 12:20:02 +00:00
2018-03-08 12:58:30 +00:00
/* (2) Initialize inputs */
gstore . add ( 'edit_cat' , '-' ) ;
gstore . add ( 'edit_name' , '' ) ;
gstore . add ( 'edit_cas' , '' ) ;
gstore . add ( 'edit_h' , '' ) ;
/* (3) Initialize error message */
gstore . add ( 'edit_err_valid' , false ) ;
gstore . add ( 'edit_err' , '' ) ;
/* (4) Define toggle view */
gstore . add ( 'ie_toggle' , function ( prof _i ) {
/* (4.1) Abort if wrong prof_i */
if ( prof _i == null || isNaN ( prof _i ) || gstore . get . professors [ prof _i ] == null )
return gstore . add ( 'edit_i' , - 1 ) ;
/* (4.2) Toggle current value */
var prof = gstore . get . professors [ prof _i ] ;
/* (4.3) Pre-fill edit values */
gstore . get . edit _cat = prof . idCat ;
gstore . get . edit _cas = prof . casLogin ;
gstore . get . edit _name = prof . firstName + ' ' + prof . lastName ;
gstore . get . edit _h = prof . hoursToDo . toString ( ) ;
/* (4.4) Set card to edit mode */
gstore . get . edit _i = prof _i ;
} ) ;
/* (5) Confirm update */
gstore . add ( 'ie_handler' , function ( prof _i ) {
/* (5.1) Abort if wrong prof_i */
if ( prof _i == null || isNaN ( prof _i ) || gstore . get . professors [ prof _i ] == null )
2018-03-07 12:20:02 +00:00
return ;
2018-03-08 12:58:30 +00:00
/* (5.2) Toggle current value */
var prof = gstore . get . professors [ prof _i ] ;
2018-03-08 11:45:09 +00:00
2018-03-08 12:58:30 +00:00
/* (5.3) Trim text input */
gstore . get . edit _name = gstore . get . edit _name . trim ( ) ;
gstore . get . edit _cas = gstore . get . edit _cas . trim ( ) . toLowerCase ( ) ;
gstore . get . edit _h = gstore . get . edit _h . trim ( ) ;
2018-03-08 11:45:09 +00:00
2018-03-08 12:58:30 +00:00
/* (5.4) Store values locally */
var cat = gstore . get . edit _cat ;
var name = gstore . get . edit _name . split ( ' ' ) ;
var cas = gstore . get . edit _cas ;
var hour = gstore . get . edit _h ;
/* (5.5) Init client-side check */
var errors = [ ] ;
/* (5.5.1) Check category */
if ( isNaN ( cat ) ) errors . push ( 'La catégorie de l\'enseignant est manquante' ) ;
/* (5.5.2) Check name */
if ( name . length !== 2 || name [ 0 ] . length < 2 || name [ 1 ] . length < 2 )
errors . push ( 'Le nom doit suivre le format "Prénom Nom"' ) ;
/* (5.5.3) Check CAS login */
if ( ! /^([a-z]{4,16})?$/ . test ( cas ) )
errors . push ( 'L\'identifiant doit être vide ou comprendre de 4 à 16 lettres' ) ;
/* (5.5.4) Check hours */
if ( hour === '' || isNaN ( hour ) || hour < 0 )
errors . push ( 'Le nombre d\'heures doit être un entier positif.' ) ;
/* (5.6) Show first error only (for 2s) */
if ( errors . length > 0 ) {
gstore . get . edit _err = errors [ 0 ] ;
return setTimeout ( ( ) => gstore . add ( 'edit_err' , '' ) , 2000 ) ;
}
/* (5.7) Création de la requête */
var rq = { } ;
2018-03-13 10:13:03 +00:00
2018-03-08 12:58:30 +00:00
( name [ 0 ] != prof . firstName ) && ( rq . firstName = name [ 0 ] ) ;
( name [ 1 ] != prof . lastName ) && ( rq . lastName = name [ 1 ] ) ;
( cat != prof . idCat ) && ( rq . category = cat ) ;
( hour != prof . hoursToDo ) && ( rq . hoursToDo = hour ) ;
2018-03-13 10:13:03 +00:00
// if empty cas -> request to remove cas login
( cas != prof . casLogin ) && ( ( cas . length > 0 ) && ( rq . casLogin = cas ) || ( rq . remCas = true ) ) ;
2018-03-08 12:58:30 +00:00
// update initials whatever have been modified (to avoid API error when no field given)
rq . initials = name [ 0 ] . substr ( 0 , 2 ) + name [ 1 ] . substr ( 0 , 2 ) ;
2018-03-08 11:45:09 +00:00
2018-03-07 12:20:02 +00:00
( new Promise ( ( resolve , reject ) => {
popup . ask ( {
title : 'Confirmation de modification' ,
2018-03-08 12:58:30 +00:00
content : "La modification de l'enseignant <b>" + prof . firstName + " " + prof . lastName + "</b> est irréversible.<br><br>Voulez-vous le modifier ?" ,
2018-03-07 12:20:02 +00:00
action : 'Modifier' ,
type : 'search'
} , ( popup _rs ) => { popup _rs && resolve ( ) } ) ;
2018-03-08 12:58:30 +00:00
/* (5.8) On pop-up confirmation */
2018-03-07 12:20:02 +00:00
} ) ) . then ( function ( ) {
return new Promise ( ( resolve , reject ) => {
2018-03-08 12:58:30 +00:00
/* (5.8.1) Delete professor */
api . call ( 'PUT professor/' + prof . idProfesseur , rq , function ( rs ) {
2018-03-07 12:20:02 +00:00
2018-03-13 10:26:08 +00:00
/* (5.8.1.1) Manage 'already exist' error */
if ( rs . error == 29 ) {
gstore . get . edit _err = 'Le couple Nom-Prénom ou l\'identifiant sont déja utilisés.' ;
return setTimeout ( ( ) => gstore . add ( 'edit_err' , '' ) , 2000 ) ;
}
/* (5.8.1.2) Abort on error */
2018-03-07 12:20:02 +00:00
if ( rs . error !== 0 || rs . updated !== true )
return reject ( rs . error ) ;
2018-03-13 10:26:08 +00:00
/* (5.8.1.3) Success */
2018-03-07 12:20:02 +00:00
resolve ( ) ;
} ) ;
} ) ;
2018-03-08 12:58:30 +00:00
/* (5.9) On success */
2018-03-07 12:20:02 +00:00
} ) . then ( function ( ) {
2018-03-08 12:58:30 +00:00
/* (5.9.1) update VueJS element */
gstore . get . professors [ prof _i ] . idCat = cat ;
gstore . get . professors [ prof _i ] . firstName = name [ 0 ] ;
gstore . get . professors [ prof _i ] . lastName = name [ 1 ] ;
gstore . get . professors [ prof _i ] . casLogin = cas ;
gstore . get . professors [ prof _i ] . hoursToDo = hour ;
2018-03-07 12:20:02 +00:00
2018-03-08 17:34:25 +00:00
/* (5.9.2) Try to set the category label */
var ci = gstore . get . filters . categories . map ( ( data , i ) => { return ( data . code && data . code == cat ) ? i : '' ; } ) . join ( '' ) ;
/* (5.9.3) Exit if not found */
if ( isNaN ( ci ) ) return gstore . add ( 'edit_i' , - 1 ) ;
/* (5.9.4) If found -> set category label */
gstore . get . professors [ prof _i ] . categorie = gstore . get . filters . categories [ ci ] . name ;
/* (5.9.5) Remove edit mode */
2018-03-08 12:58:30 +00:00
gstore . add ( 'edit_i' , - 1 ) ;
2018-03-07 12:20:02 +00:00
2018-03-08 12:58:30 +00:00
/* (5.10) On error */
2018-03-07 12:20:02 +00:00
} ) . catch ( function ( err _code ) {
popup . ask ( {
title : 'Error (' + err _code + ')' ,
content : 'La modification a échouée. Veuillez réessayer ultérieurement.' ,
action : 'OK' ,
type : 'neutral'
} , ( ) => { } ) ;
} ) ;
2018-03-07 12:13:31 +00:00
2018-03-07 13:58:01 +00:00
} ) ;
/ * ( 8 ) M a n a g e i n s t a n t a d m i n
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
/* (1) Define admin handler */
gstore . add ( 'ia_handler' , function ( prof _i ) {
/* (1) Abort if wrong prof_i */
if ( prof _i == null || isNaN ( prof _i ) || gstore . get . professors [ prof _i ] == null )
return ;
/* (2) Toggle current value */
var local = gstore . get . professors [ prof _i ] ;
var is _admin = local . admin == '1' || local . admin === true ;
var new _state = ! is _admin ;
/* (3.1) Update in database */
api . call ( 'PUT professor/' + local . idProfesseur , { isAdmin : new _state } , function ( rs ) {
/* (3.1.1) Abort on error */
if ( rs . error !== 0 || rs . updated !== true )
return console . log ( 'Impossible de changer le status \'admin\', erreur ' + rs . error ) ;
/* (3.1.2) Success */
gstore . get . professors [ prof _i ] . admin = new _state ? 1 : 0 ;
} ) ;
2018-03-26 11:23:32 +00:00
} ) ;
/ * ( 9 ) M a n a g e i n s t a n t d o w n l o a d f i c h e
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
/* (1) Define download handler */
gstore . add ( 'id_handler' , function ( prof _id ) {
/* (1) Abort if wrong prof_id */
if ( prof _id == null || isNaN ( prof _id ) )
return ;
/* (2.1) Find index in gstore */
var gi = gstore . get . professors . map ( ( data , i ) => { return ( data . idProfesseur && data . idProfesseur == prof _id ) ? i : '' ; } ) . join ( '' ) ;
/* (2.2) Exit if not found */
if ( isNaN ( gi ) ) return ;
var local = gstore . get . professors [ gi ] ;
/* (3.1) Update in database */
api . call ( ` GET professor/pdf/ ${ local . idProfesseur } ` , { } , function ( rs ) {
/* (3.1.1) Abort on error */
if ( rs . error !== 0 || rs . link == null )
return console . log ( 'Impossible de télécharger la fiche, erreur ' + rs . error ) ;
/* (3.1.2) Success */
document . location = rs . link ;
} ) ;
2018-03-07 12:20:02 +00:00
} ) ;