Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 348 | Rev 351 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 348 Rev 349
Línea 327... Línea 327...
327
            'accept': 'application/json',
327
            'accept': 'application/json',
328
            'method': 'get',
328
            'method': 'get',
329
            'url': action,
329
            'url': action,
330
        }).done(function(response) {
330
        }).done(function(response) {
331
            if (response['success']) {
331
            if (response['success']) {
332
                validatorForm.resetForm();
-
 
333
                $('#form-main').attr('action', action);
332
                $('#form-main').attr('action', action);
334
                $('#form-main #form-id').val(response['data']['id']),
333
                $('#form-main #form-id').val(response['data']['id']),
335
                    $('#name').html(response['data']['name']);
334
                $('#name').html(response['data']['name']);
336
                $('#text').html(response['data']['text']);
335
                $('#text').html(response['data']['text']);
337
                $('#user').html(response['data']['user']);
336
                $('#user').html(response['data']['user']);
338
                /*-------------Render Sections -------*/
337
                /*-------------Render Sections -------*/
339
                objFormGenerator.clear();
338
                objFormGenerator.clear();
340
                objFormGenerator.sections = response['data']['content'] || [];
339
                objFormGenerator.sections = response['data']['content'] || [];
Línea 349... Línea 348...
349
            $.fn.showError(textStatus);
348
            $.fn.showError(textStatus);
350
        });
349
        });
351
    });
350
    });
352
    //IMPORTANT: update CKEDITOR textarea with actual content before submit
351
    //IMPORTANT: update CKEDITOR textarea with actual content before submit
353
    $("#form-main").on('submit', function() {
352
    $("#form-main").on('submit', function() {
354
        for (var instanceName in CKEDITOR.instances) {
-
 
355
            CKEDITOR.instances[instanceName].updateElement();
-
 
356
        }
-
 
357
    })
-
 
358
    /**
-
 
359
     * Validate rules form 
-
 
360
     */
-
 
361
    var validatorForm = $("#form-main").validate({
-
 
362
        ignore: [],
-
 
363
        errorClass: 'help-block',
-
 
364
        errorElement: 'span',
353
        e.preventDefault();
365
        rules: {
-
 
366
            'form-name': {
-
 
367
                required: true,
-
 
368
                minlength: 2,
-
 
369
                maxlength: 50
-
 
370
            },
-
 
371
            'form-description': {
-
 
372
                required: true,
-
 
373
            },
-
 
374
            'form-text': {
-
 
375
                required: true,
-
 
376
            },
-
 
377
            'form-language': {
-
 
378
                required: true,
-
 
379
            },
-
 
380
            'forrm-status': {
-
 
381
                required: true,
-
 
382
            },
-
 
383
        },
-
 
384
        highlight: function(element) {
-
 
385
            $(element).closest('.form-group').addClass('has-error');
-
 
386
        },
-
 
387
        unhighlight: function(element) {
-
 
388
            $(element).closest('.form-group').removeClass('has-error');
-
 
389
        },
-
 
390
        errorPlacement: function(error, element) {
-
 
391
            if (element.attr("data-error-container")) {
-
 
392
                error.appendTo(element.attr("data-error-container"));
-
 
393
            } else {
-
 
394
                error.insertAfter(element);
-
 
395
            }
-
 
396
        },
-
 
397
        invalidHandler: function(form, validator) {
-
 
398
            if (!validator.numberOfInvalids())
-
 
399
                return;
-
 
400
            $('html, body').animate({
-
 
401
                scrollTop: $(validator.errorList[0].element).offset().top - 100
-
 
402
            }, 1000);
-
 
403
        },
-
 
404
        submitHandler: function(form) {
-
 
405
            var error = false;
-
 
406
            if (objFormGenerator.sections.length == 0) {
-
 
407
                $.fn.showError('ERROR_SECCTIONS');
-
 
408
                return false;
-
 
409
            } else {
-
 
410
                for (i = 0; i < objFormGenerator.sections.length; i++) {
-
 
411
                    if (objFormGenerator.sections[i].questions.length == 0) {
-
 
412
                        $.fn.showError('ERROR_QUESTIONS'.replace('%s', objFormGenerator.sections[i].name));
-
 
413
                        return false;
-
 
414
                    }
-
 
415
                    var valueSection = parseInt(objFormGenerator.sections[i].value);
-
 
416
                    var totalValueQuestion = 0;
-
 
417
                    for (j = 0; j < objFormGenerator.sections[i].questions.length; j++) {
-
 
418
                        valueQuestion = parseInt(objFormGenerator.sections[i].questions[j].value);
-
 
419
                        totalValueQuestion = totalValueQuestion + valueQuestion;
-
 
420
                        if (objFormGenerator.sections[i].questions[j].type == 'simple' ||
-
 
421
                            objFormGenerator.sections[i].questions[j].type == 'multiple' ||
-
 
422
                            objFormGenerator.sections[i].questions[j].type == 'rating-open') {
-
 
423
                            var questionNumber = j + 1;
-
 
424
                            var numberCorrect = 0;
-
 
425
                            if (objFormGenerator.sections[i].questions[j].options.length == 0) {
-
 
426
                                $.fn.showError('ERROR_OPTIONS'.replace('%s', objFormGenerator.sections[i].name).replace('%n', questionNumber));
-
 
427
                                return false;
-
 
428
                            }
-
 
429
                            var totalOption = 0;
-
 
430
                            var maxOption = 0;
-
 
431
                            for (k = 0; k < objFormGenerator.sections[i].questions[j].options.length; k++) {
-
 
432
                                if (objFormGenerator.sections[i].questions[j].type == 'simple' || objFormGenerator.sections[i].questions[j].type == 'multiple') {
-
 
433
                                    if (objFormGenerator.sections[i].questions[j].options[k].correct == 1) {
-
 
434
                                        numberCorrect++;
-
 
435
                                    }
-
 
436
                                }
-
 
437
                                if (objFormGenerator.sections[i].questions[j].type == 'multiple' && objFormGenerator.sections[i].questions[j].options[k].correct == 1) {
-
 
438
                                    totalOption = totalOption + parseInt(objFormGenerator.sections[i].questions[j].options[k].value);
-
 
439
                                }
-
 
440
                                if (objFormGenerator.sections[i].questions[j].type == 'rating-open') {
-
 
441
                                    if (parseInt(objFormGenerator.sections[i].questions[j].options[k].value) > maxOption) {
-
 
442
                                        maxOption = parseInt(objFormGenerator.sections[i].questions[j].options[k].value);
-
 
443
                                    }
-
 
444
                                }
-
 
445
                            }
-
 
446
                            if (objFormGenerator.sections[i].questions[j].type == 'simple' || objFormGenerator.sections[i].questions[j].type == 'multiple') {
-
 
447
                                if (numberCorrect == 0) {
-
 
448
                                    $.fn.showError('ERROR_OPTIONS_CORRECT'.replace('%s', objFormGenerator.sections[i].name).replace('%n', questionNumber));
-
 
449
                                    return false;
-
 
450
                                }
-
 
451
                                if (
-
 
452
                                    objFormGenerator.sections[i].questions[j].type == 'simple' && numberCorrect > 1) {
-
 
453
                                    $.fn.showError('ERROR_OPTIONS_DUPLICATE_CORRECT'.replace('%s', objFormGenerator.sections[i].name).replace('%n', questionNumber));
-
 
454
                                    return false;
-
 
455
                                }
-
 
456
                                if (objFormGenerator.sections[i].questions[j].type == 'multiple' && numberCorrect == 1) {
-
 
457
                                    $.fn.showError('ERROR_OPTIONS_ONE_CORRECT'.replace('%s', objFormGenerator.sections[i].name).replace('%n', questionNumber));
-
 
458
                                    return false;
-
 
459
                                }
-
 
460
                            }
-
 
461
                            if (objFormGenerator.sections[i].questions[j].type == 'multiple' && totalOption != valueQuestion) {
-
 
462
                                $.fn.showError('ERROR_OPTIONS_SUM_VALUES'.replace('%s', objFormGenerator.sections[i].name).replace('%n', questionNumber));
-
 
463
                                return false;
-
 
464
                            }
-
 
465
                            if (objFormGenerator.sections[i].questions[j].type == 'rating-open' && maxOption > valueQuestion) {
-
 
466
                                $.fn.showError('ERROR_OPTIONS_MAX_OPTION'.replace('%s', objFormGenerator.sections[i].name).replace('%n', questionNumber));
-
 
467
                                return false;
-
 
468
                            }
-
 
469
                        }
-
 
470
                    }
-
 
471
                    if (valueSection != totalValueQuestion) {
-
 
472
                        $.fn.showError('ERROR_VALUE_SECTIONS'.replace('%s', objFormGenerator.sections[i].name));
-
 
473
                        return false;
-
 
474
                    }
-
 
475
                }
-
 
476
                var formId = parseInt($('#form-main #form-id').val());
354
        var formId = parseInt($('#form-main #form-id').val());
477
                var formContinue = parseInt($('#form-main #form-continue').val());
355
                var formContinue = parseInt($('#form-main #form-continue').val());
478
                var data = {
356
                var data = {
479
                    'id': formId,
357
                    'id': formId,
480
                    'name': $('#form-main #form-name').val(),
-
 
481
                    'description': $('#form-main #form-description').val(),
-
 
482
                    'text': $('#form-main #form-text').val(),
-
 
483
                    'language': $('#form-main #form-language').val(),
-
 
484
                    'status': $('#form-main #form-status').val(),
358
                    'status': $('#form-main #form-status').val(),
485
                    'content': JSON.stringify(objFormGenerator.sections)
359
                    'content': JSON.stringify(objFormGenerator.sections)
486
                }
360
                }
487
                $.ajax({
361
                $.ajax({
488
                    'dataType': 'json',
362
                    'dataType': 'json',
Línea 490... Línea 364...
490
                    'url': $('#form-main').attr('action'),
364
                    'url': $('#form-main').attr('action'),
491
                    'data': data,
365
                    'data': data,
492
                }).done(function(response) {
366
                }).done(function(response) {
493
                    if (response['success']) {
367
                    if (response['success']) {
494
                        $.fn.showSuccess(response['data']);
368
                        $.fn.showSuccess(response['data']);
495
                        if (formContinue == 1) {
-
 
496
                            $('#form-main').attr('action', response['action_edit']);
-
 
497
                            $('#form-main #form-id').val(response['id']);
-
 
498
                            $('#form-main #form-continue').val(0);
-
 
499
                        } else {
-
 
500
                            $('#row-edit').hide();
369
                            $('#row-edit').hide();
501
                            $('#row-forms').show();
370
                            $('#row-forms').show();
502
                            /*---------- Reset Form -------- */
371
                            /*---------- Reset Form -------- */
503
                            $('#form-main')[0].reset();
372
                            $('#form-main')[0].reset();
504
                            /*--------Reset Sections ----------*/
373
                            /*--------Reset Sections ----------*/
505
                            clearSectionData();
374
                            clearSectionData();
506
                            /* ------- Refresh Table -----------*/
375
                            /* ------- Refresh Table -----------*/
507
                        }
-
 
508
                        tableForm.fnDraw();
376
                        tableForm.fnDraw();
509
                    } else {
377
                    } else {
510
                        $.fn.showError(response['message'] || 'ERROR_UNKNOWN');
378
                        $.fn.showError(response['message'] || 'ERROR_UNKNOWN');
511
                    }
379
                    }
512
                }).fail(function(jqXHR, textStatus, errorThrown) {
380
                }).fail(function(jqXHR, textStatus, errorThrown) {
513
                    $.fn.showError(textStatus);
381
                    $.fn.showError(textStatus);
514
                });
382
                });
515
                return false;
383
                return false;
516
            }
-
 
517
        }
-
 
518
    });
384
    })
-
 
385
   
519
    /**
386
    /**
520
     * Clicked cancel new/edit Form
387
     * Clicked cancel new/edit Form
521
     */
388
     */
522
    $('button.btn-edit-cancel').click(function(e) {
389
    $('button.btn-edit-cancel').click(function(e) {
523
        e.preventDefault();
390
        e.preventDefault();
524
        $('#row-edit').hide();
391
        $('#row-edit').hide();
525
        $('#row-forms').show();
392
        $('#row-forms').show();
526
    });
393
    });
527
    /**
394
    /**
528
     * Clicked save and continue new Form
-
 
529
     */
-
 
530
    $('button.btn-form-save-continue').click(function(e) {
-
 
531
        e.preventDefault();
-
 
532
        $('#form-main #form-continue').val('1')
-
 
533
        $('#form-main').submit();
-
 
534
    });
-
 
535
    /**
-
 
536
     * Clicked save and close new/edit Form
395
     * Clicked save and close new/edit Form
537
     */
396
     */
538
    $('button.btn-form-save-close').click(function(e) {
397
    $('button.btn-form-save-close').click(function(e) {
539
        e.preventDefault();
398
        e.preventDefault();
540
        $('#form-main #form-continue').val('0')
399
        $('#form-main #form-continue').val('0')