[repo.ue] UPDATE manage 'new_code' [module.ue] PUT manages optional argument 'new_code' [webpack.data.ue] display input for 'code' and send 'new_code' to API if set

This commit is contained in:
xdrm-brackets 2018-03-19 23:32:09 +01:00
parent 228bdddd2d
commit 38600994c0
5 changed files with 44 additions and 14 deletions

View File

@ -130,6 +130,7 @@ class ueController{
/* (4) Edits an existing UE
*
* @code<String> The code of the UE
* @new_code<String> [OPT] The new code of the UE
* @label<String> [OPT] The UE label (name)
* @required<bool> [OPT] If the UE is required
* @volumeCours<float> [OPT] The UE required volume of COURSES
@ -143,6 +144,7 @@ class ueController{
---------------------------------------------------------*/
public static function put($args){
$code = "";
$new_code = "";
$label = "";
$required = false;
$volumeCours = 0;
@ -156,9 +158,29 @@ class ueController{
/** @var ue $ue_repo */
$ue_repo = Repo::getRepo('ue');
/* (1) Try to update */
/* (1) Check for @new_code to be unique (not already used)
---------------------------------------------------------*/
if( !is_null($new_code) ){
/* (1) Check @new_code */
$exists = $ue_repo->get($new_code);
/* (2) If found -> already exists */
if( count($exists) > 0 )
return ['error' => new Error(Err::AlreadyExists)];
}
/* (2) Try to update
---------------------------------------------------------*/
return ['updated' => $ue_repo->update(
$code,
$new_code,
$label,
$required,
$volumeCours,

View File

@ -63,6 +63,7 @@ class ue extends Repo_i {
/* (2) Updates an UE data
*
* @code<String> The UE code
* @new_code<String> [OPT] The new UE code
* @label<String> [OPT] The UE's new label
* @required<bool> [OPT] Whether the UE is required
* @volumeCours<float> [OPT] The UE's new volume of COURSES
@ -74,12 +75,13 @@ class ue extends Repo_i {
* @return updated<bool> Whether the update have been successful
*
---------------------------------------------------------*/
public function update(string $code, ?String $label, ?bool $required, ?float $volumeCours, ?float $volumeTD, ?float $volumeTP, ?bool $disabled, ?int $defaultFormation) : bool{
public function update(string $code, ?String $new_code, ?String $label, ?bool $required, ?float $volumeCours, ?float $volumeTD, ?float $volumeTP, ?bool $disabled, ?int $defaultFormation) : bool{
/* (1) Build request */
$build_rq = [];
$bind_param = [ ':code' => $code ];
if( !is_null($new_code) ){ $build_rq[] = '`code` = :new_code'; $bind_param[':new_code'] = $new_code; }
if( !is_null($label) ){ $build_rq[] = '`label` = :label'; $bind_param[':label'] = $label; }
if( !is_null($required) ){ $build_rq[] = '`required` = :required'; $bind_param[':required'] = $required?1:0; }
if( !is_null($volumeCours) ){ $build_rq[] = '`volumeCours` = :volumeCours'; $bind_param[':volumeCours'] = $volumeCours; }

View File

@ -270,6 +270,7 @@
"per": [],
"par": {
"URL0": { "des": "UE code.", "typ": "varchar(4,20,alphanumeric)", "ren": "code" },
"new_code": { "des": "UE new code", "typ": "varchar(4,20,alphanumeric)", "opt": true },
"label": { "des": "UE label", "typ": "varchar(4,30,alphanumeric)", "opt": true },
"required": { "des": "If UE is required", "typ": "bool", "opt": true },
"volumeCours": { "des": "Number of course hours for UE", "typ": "float", "opt": true },

View File

@ -65,8 +65,8 @@
<!-- if EDIT MODE -->
<h1 v-show='gstore.edit_i==pi' :class="ue.required ? 'warning' : ''">
<input type='text' placeholder='Libellé' v-model='gstore.edit_label'>
<span :data-visible='1'>({{ ue.code }})</span>
<!-- <span data-visible='1'>(<input type='text' placeholder='code' v-model='gstore.edit_code'>)</span> -->
<!-- <span :data-visible='1'>({{ ue.code }})</span> -->
<span data-visible='1'>(<input type='text' placeholder='code' v-model='gstore.edit_code'>)</span>
</h1>
<!-- endif -->

View File

@ -329,7 +329,7 @@ gstore.add('edit_i', -1);
/* (2) Initialize inputs */
gstore.add('edit_form', '-');
gstore.add('edit_label', '');
// gstore.add('edit_code', '');
gstore.add('edit_code', '');
gstore.add('edit_vol', { c: '', td: '', tp: ''});
/* (3) Initialize error message */
@ -372,7 +372,7 @@ gstore.add('ie_handler', function(ue_i){
/* (5.3) Trim text input */
gstore.get.edit_label = gstore.get.edit_label.trim();
// gstore.get.edit_code = gstore.get.edit_code.toString().trim().toUpperCase();
gstore.get.edit_code = gstore.get.edit_code.toString().trim().toUpperCase();
gstore.get.edit_vol.c = gstore.get.edit_vol.c.toString().trim();
gstore.get.edit_vol.td = gstore.get.edit_vol.td.toString().trim();
gstore.get.edit_vol.tp = gstore.get.edit_vol.tp.toString().trim();
@ -380,7 +380,7 @@ gstore.add('ie_handler', function(ue_i){
/* (5.4) Store values locally */
var form = gstore.get.edit_form;
var label = gstore.get.edit_label;
// var code = gstore.get.edit_code;
var code = gstore.get.edit_code;
var vco = gstore.get.edit_vol.c;
var vtd = gstore.get.edit_vol.td;
var vtp = gstore.get.edit_vol.tp;
@ -396,8 +396,8 @@ gstore.add('ie_handler', function(ue_i){
errors.push('Le label doit comprendre faire au moins 4 caractères');
/* (5.5.3) Check code */
// if( !/^[A-Z0-9]{4,20}$/.test(code) )
// errors.push('Le code doit comprendre de 4 à 20 lettres/chiffres');
if( !/^[A-Z0-9]{4,20}$/.test(code) )
errors.push('Le code doit comprendre de 4 à 20 lettres/chiffres');
/* (5.5.4) Check volumes */
if( vco === '' || isNaN(vco) || vco < 0 )
@ -416,7 +416,7 @@ gstore.add('ie_handler', function(ue_i){
/* (5.7) Création de la requête */
var rq = {};
( label != ue.label ) && ( rq.label = label );
// ( code != ue.code ) && ( rq.code = code );
( code != ue.code ) && ( rq.new_code = code );
( vco != ue.volumeCours ) && ( rq.volumeCours = parseInt(vco) );
( vtd != ue.volumeTD ) && ( rq.volumeTD = parseInt(vtd) );
( vtp != ue.volumeTP ) && ( rq.volumeTP = parseInt(vtp) );
@ -439,12 +439,17 @@ gstore.add('ie_handler', function(ue_i){
/* (5.8.1) Update UE */
api.call(`PUT ue/${ue.code}`, rq, function(rs){
/* (5.8.1.1) Abort on error */
console.log(rq, rs.error != 0, rs.updated != true);
/* (5.8.1.1) Manage 'already exist' error */
if( rs.error == 29 ){
gstore.get.edit_err = 'Le nouveau code est déja utilisé par une autre UE.';
return setTimeout(() => gstore.add('edit_err', ''), 2000);
}
/* (5.8.1.2) Abort on error */
if( rs.error != 0 || rs.updated != true )
return reject(rs.error);
/* (5.8.1.2) Success */
/* (5.8.1.3) Success */
resolve();
});
@ -456,7 +461,7 @@ gstore.add('ie_handler', function(ue_i){
/* (5.9.1) update VueJS element */
gstore.get.ues[ue_i].label = label;
// gstore.get.ues[ue_i].code = code;
gstore.get.ues[ue_i].code = code;
gstore.get.ues[ue_i].idForm = parseInt(form);
gstore.get.ues[ue_i].volumeCours = parseInt(vco);
gstore.get.ues[ue_i].volumeTD = parseInt(vtd);