Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 255 | Rev 257 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
251 geraldo 1
<?php
2
$acl = $this->viewModel()->getRoot()->getVariable('acl');
3
$currentUser = $this->currentUserHelper();
4
 
5
$roleName = $currentUser->getUserTypeId();
6
 
253 geraldo 7
$routeDatatable = $this->url('self-evaluation/reviews');
251 geraldo 8
$routeDashboard = $this->url('dashboard');
9
 
253 geraldo 10
$allowEdit = $acl->isAllowed($roleName, 'self-evaluation/reviews/edit') ? 1 : 0;
251 geraldo 11
 
12
 
13
$this->inlineScript()->appendFile($this->basePath('plugins/ckeditor/ckeditor.js'));
14
 
15
 
16
$this->headLink()->appendStylesheet($this->basePath('plugins/nprogress/nprogress.css'));
17
$this->inlineScript()->appendFile($this->basePath('plugins/nprogress/nprogress.js'));
18
 
19
 
20
$this->inlineScript()->appendFile($this->basePath('plugins/jquery-validation/jquery.validate.js'));
21
$this->inlineScript()->appendFile($this->basePath('plugins/jquery-validation/additional-methods.js'));
22
$this->inlineScript()->appendFile($this->basePath('plugins/jquery-validation/localization/messages_es.js'));
23
 
24
$this->headLink()->appendStylesheet($this->basePath('plugins/datatables-bs4/css/dataTables.bootstrap4.min.css'));
25
$this->headLink()->appendStylesheet($this->basePath('plugins/datatables-responsive/css/responsive.bootstrap4.min.css'));
26
 
27
$this->inlineScript()->appendFile($this->basePath('plugins/datatables/jquery.dataTables.min.js'));
28
$this->inlineScript()->appendFile($this->basePath('plugins/datatables-bs4/js/dataTables.bootstrap4.min.js'));
29
$this->inlineScript()->appendFile($this->basePath('plugins/datatables-responsive/js/dataTables.responsive.min.js'));
30
$this->inlineScript()->appendFile($this->basePath('plugins/datatables-responsive/js/responsive.bootstrap4.min.js'));
31
 
32
 
33
$this->headLink()->appendStylesheet($this->basePath('plugins/select2/css/select2.min.css'));
34
$this->headLink()->appendStylesheet($this->basePath('plugins/select2-bootstrap4-theme/select2-bootstrap4.min.css'));
35
 
36
$this->inlineScript()->appendFile($this->basePath('plugins/select2/js/select2.full.min.js'));
37
 
38
$this->inlineScript()->appendFile($this->basePath('plugins/moment/moment-with-locales.min.js'));
39
$this->headLink()->appendStylesheet($this->basePath('plugins/bootstrap-datetimepicker/css/bootstrap-datetimepicker.css'));
40
$this->inlineScript()->appendFile($this->basePath('plugins/bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js'));
41
 
42
 
43
$this->headLink()->appendStylesheet($this->basePath('plugins/bootstrap4-toggle/css/bootstrap4-toggle.min.css'));
44
$this->inlineScript()->appendFile($this->basePath('plugins/bootstrap4-toggle/js/bootstrap4-toggle.min.js'));
45
 
46
$this->inlineScript()->appendFile($this->basePath('plugins/bootstrap-confirmation/dist/bootstrap-confirmation.js'));
47
$this->headLink()->appendStylesheet($this->basePath('plugins/bootstrap-checkbox/awesome-bootstrap-checkbox.css'));
48
$this->inlineScript()->appendFile($this->basePath('plugins/jquery-input-number/input-number-format.jquery.js'));
49
// bootbox Alert //
50
$this->inlineScript()->appendFile($this->basePath('plugins/bootbox/bootbox.min.js'));
51
// JsRender //
52
$this->inlineScript()->appendFile($this->basePath('plugins/jsrender/jsrender.min.js'));
53
 
54
 
55
$this->inlineScript()->captureStart();
56
echo <<<JS
57
    const classFormGenerator = function() {
58
    this.id = 0,
59
        this.table = '',
60
        this.name = '',
61
        this.text = '',
62
        this.status = 'a',
63
        this.sections = [],
64
        this.clear = function() {
65
            this.sections = [];
66
            this.render();
67
        },
68
        /**
69
         * Render array sections
70
         */
71
        this.renderSection = function(slug_section) {
72
            var s = '';
73
            for (i = 0; i < this.sections.length; i++) {
74
                if (slug_section != this.sections[i].slug_section) {
75
                    continue;
76
                }
77
                for (j = 0; j < this.sections[i].questions.length; j++) {
78
                    this.sections[i].questions[j].position = j;
79
                    if (this.sections[i].questions[j].type == 'simple' || this.sections[i].questions[j].type == 'multiple' || this.sections[i].questions[j].type == 'rating-open') {
80
                        this.sections[i].questions[j].options.sort(function(a, b) {
81
                            if (a.position > b.position) {
82
                                return 1;
83
                            }
84
                            if (a.position < b.position) {
85
                                return -1;
86
                            }
87
                            return 0;
88
                        });
89
                        for (k = 0; k < this.sections[i].questions[j].options.length; k++) {
90
                            this.sections[i].questions[j].options[k].position = j;
91
                        }
92
                    }
93
                }
94
            }
95
            $('[data-toggle="tooltip"]').tooltip();
96
        },
97
        this.render = function() {
98
            this.sections.sort(function(a, b) {
99
                if (a.position > b.position) {
100
                    return 1;
101
                }
102
                if (a.position < b.position) {
103
                    return -1;
104
                }
105
                return 0;
106
            });
107
            var s = '';
108
            for (i = 0; i < this.sections.length; i++) {
109
                this.sections[i].position = i;
110
                this.sections[i].questions.sort(function(a, b) {
111
                    if (a.position > b.position) {
112
                        return 1;
113
                    }
114
                    if (a.position < b.position) {
115
                        return -1;
116
                    }
117
                    return 0;
118
                });
119
                for (j = 0; j < this.sections[i].questions.length; j++) {
120
                    this.sections[i].questions[j].position = j;
121
                    if (this.sections[i].questions[j].type == 'simple' || this.sections[i].questions[j].type == 'multiple' || this.sections[i].questions[j].type == 'rating-open') {
122
                        this.sections[i].questions[j].options.sort(function(a, b) {
123
                            if (a.position > b.position) {
124
                                return 1;
125
                            }
126
                            if (a.position < b.position) {
127
                                return -1;
128
                            }
129
                            return 0;
130
                        });
131
                        for (k = 0; k < this.sections[i].questions[j].options.length; k++) {
132
                            this.sections[i].questions[j].options[k].position = j;
133
                        }
134
                    }
135
                }
136
            }
137
            $('[data-toggle="tooltip"]').tooltip();
138
        },
254 geraldo 139
        /**
140
         * Add element to section array
141
         */
142
        this.addSection = function(name, text, value) {
143
            var d = new Date();
144
            var slug = 'section' + d.getTime();
145
            var position = 0;
146
            $.each(this.sections, function(index, section) {
147
                if (position < section.position) {
148
                    position = section.position;
149
                }
150
            });
151
            position++;
152
            var section = {
153
                'slug_section': slug,
154
                'name': name,
155
                'text': text,
156
                'value': value,
157
                'position': position,
158
                'questions': [],
159
                'status': 0
160
            }
161
            this.sections.push(section);
162
        },
251 geraldo 163
         /**
254 geraldo 164
         * Edit element to section array
165
         */
166
        this.editSection = function(slug, name, text, value) {
167
            var renderTable = false;
168
            for (i = 0; i < this.sections.length; i++) {
169
                if (slug == this.sections[i].slug_section) {
170
                    this.sections[i].name = name;
171
                    this.sections[i].text = text;
172
                    this.sections[i].value = value;
173
                    renderTable = true;
174
                    break;
175
                }
176
            }
177
            if (renderTable) {
178
                this.renderSection(slug);
179
            }
180
        },
181
         /**
182
         * Remove element to section array
183
         */
184
        this.deleteSection = function(slug) {
185
            var renderTable = false;
186
            for (i = 0; i < this.sections.length; i++) {
187
                if (slug == this.sections[i].slug_section) {
188
                    this.sections.splice(i, 1);
189
                    renderTable = true;
190
                    break;
191
                }
192
            }
193
            if (renderTable) {
194
                $('#panel' + slug).remove();
195
            }
196
        },
197
         /**
251 geraldo 198
         * Add element to question array
199
         */
254 geraldo 200
        this.addQuestion = function(slug_section, text, value, type, maxlength, multiline, range) {
201
            var d = new Date();
202
            var slug_question = 'question' + d.getTime();
203
            var position = 0;
204
            var renderTable = false;
205
            for (i = 0; i < this.sections.length; i++) {
206
                if (slug_section == this.sections[i].slug_section) {
207
                    $.each(this.sections[i].questions, function(index, question) {
208
                        if (position < question.position) {
209
                            position = question.position;
210
                        }
211
                    });
212
                    position++;
213
                    var question = {
214
                        'slug_section': slug_section,
215
                        'slug_question': slug_question,
216
                        'text': text,
217
                        'value': value,
218
                        'type': type,
219
                        'position': position,
220
                        'maxlength': maxlength,
221
                        'multiline': multiline,
222
                        'range': range,
223
                        'options': [],
224
                        'answer': type=='multiple' ? [] : ''
225
                    }
226
                    this.sections[i].questions.push(question);
227
                    renderTable = true;
228
                    break;
229
                }
230
            }
231
            if (renderTable) {
232
                this.renderSection(slug_section);
233
            }
234
        },
235
         /**
236
         * Add element to question array
237
         */
251 geraldo 238
        this.editQuestion = function(slug_section, slug_question, text, value, type, maxlength, multiline, range) {
239
            var renderTable = false;
240
            for (i = 0; i < this.sections.length; i++) {
241
                if (slug_section == this.sections[i].slug_section) {
242
                    for (j = 0; j < this.sections[i].questions.length; j++) {
243
                        if (slug_question == this.sections[i].questions[j].slug_question) {
244
                            this.sections[i].questions[j].text = text,
245
                                this.sections[i].questions[j].value = value,
246
                                this.sections[i].questions[j].type = type;
247
                            if (type == 'open') {
248
                                this.sections[i].questions[j].maxlength = maxlength;
249
                                this.sections[i].questions[j].multiline = multiline;
250
                                this.sections[i].questions[j].options = [];
251
                            } else {
252
                                this.sections[i].questions[j].maxlength = 0;
253
                                this.sections[i].questions[j].multiline = 0;
254
                            }
255
                            if (type == 'rating-range') {
256
                                this.sections[i].questions[j].range = range;
257
                            } else {
258
                                this.sections[i].questions[j].range = 0;
259
                            }
260
 
261
                            if(type=='multiple'){
262
                                this.sections[i].questions[j].answer = [];
263
                            }else{
264
                                this.sections[i].questions[j].answer = '';
265
                            }
266
 
267
                            renderTable = true;
268
                            break;
269
                        }
270
                    }
271
                }
272
                if (renderTable) {
273
                    break;
274
                }
275
            }
276
            if (renderTable) {
277
                this.renderSection(slug_section);
278
            }
254 geraldo 279
        },
280
        /**
281
         * Remove element to question array
282
         */
283
        this.deleteQuestion = function(slug_section, slug_question) {
284
            var renderTable = false;
285
            for (i = 0; i < this.sections.length; i++) {
286
                if (slug_section == this.sections[i].slug_section) {
287
                    for (j = 0; j < this.sections[i].questions.length; j++) {
288
                        if (slug_question == this.sections[i].questions[j].slug_question) {
289
                            this.sections[i].questions.splice(j, 1);
290
                            renderTable = true;
291
                            break;
292
                        }
293
                    }
294
                }
295
                if (renderTable) {
296
                    break;
297
                }
298
            }
299
            if (renderTable) {
300
                this.renderSection(slug_section);
301
            }
302
        },
303
        /**
304
         * Add element to option array
305
         */
306
        this.addOption = function(slug_section, slug_question, text, correct, value) {
307
            var d = new Date();
308
            var slug_option = 'option' + d.getTime();
309
            var position = 0;
310
            var renderTable = false;
311
            for (i = 0; i < this.sections.length; i++) {
312
                if (slug_section == this.sections[i].slug_section) {
313
                    for (j = 0; j < this.sections[i].questions.length; j++) {
314
                        if (slug_question == this.sections[i].questions[j].slug_question) {
315
                            $.each(this.sections[i].questions[j].options, function(index, option) {
316
                                if (position < option.position) {
317
                                    position = option.position;
318
                                }
319
                            });
320
                            position++;
321
                            var option = {
322
                                'slug_section': slug_section,
323
                                'slug_question': slug_question,
324
                                'slug_option': slug_option,
325
                                'text': text,
326
                                'correct': correct,
327
                                'value': value,
328
                                'checked': false
329
                            }
330
                            this.sections[i].questions[j].options.push(option);
331
                            renderTable = true;
332
                            break;
333
                        }
334
                        if (renderTable) {
335
                            break;
336
                        }
337
                    }
338
                }
339
            }
340
            if (renderTable) {
341
                this.renderSection(slug_section);
342
            }
343
        },
344
        /**
345
         * Edit element to option array
346
         */
347
        this.editOption = function(slug_section, slug_question, option_slug, text, correct, value) {
348
            var renderTable = false;
349
            for (i = 0; i < this.sections.length; i++) {
350
                if (slug_section == this.sections[i].slug_section) {
351
                    for (j = 0; j < this.sections[i].questions.length; j++) {
352
                        if (slug_question == this.sections[i].questions[j].slug_question) {
353
                            for (k = 0; k < this.sections[i].questions[j].options.length; k++) {
354
                                if (option_slug == this.sections[i].questions[j].options[k].slug_option) {
355
                                    this.sections[i].questions[j].options[k].text = text;
356
                                    this.sections[i].questions[j].options[k].correct = correct;
357
                                    this.sections[i].questions[j].options[k].value = value;
358
                                    renderTable = true;
359
                                    break;
360
                                }
361
                            }
362
                        }
363
                        if (renderTable) {
364
                            break;
365
                        }
366
                    }
367
                }
368
                if (renderTable) {
369
                    break;
370
                }
371
            }
372
            if (renderTable) {
373
                this.renderSection(slug_section);
374
            }
375
        },
376
        /**
377
         * Remove element to option array
378
         */
379
        this.deleteOption = function(slug_section, slug_question, option_slug) {
380
            var renderTable = false;
381
            for (i = 0; i < this.sections.length; i++) {
382
                if (slug_section == this.sections[i].slug_section) {
383
                    for (j = 0; j < this.sections[i].questions.length; j++) {
384
                        if (slug_question == this.sections[i].questions[j].slug_question) {
385
                            for (k = 0; k < this.sections[i].questions[j].options.length; k++) {
386
                                if (option_slug == this.sections[i].questions[j].options[k].slug_option) {
387
                                    this.sections[i].questions[j].options.splice(k, 1);
388
                                    renderTable = true;
389
                                    break;
390
                                }
391
                            }
392
                        }
393
                        if (renderTable) {
394
                            break;
395
                        }
396
                    }
397
                }
398
                if (renderTable) {
399
                    break;
400
                }
401
            }
402
            if (renderTable) {
403
                this.renderSection(slug_section);
404
            }
251 geraldo 405
        }
406
}
407
 
408
function htmlEntities(str) {
409
    return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
410
}
411
jQuery(document).ready(function($) {
412
    var objFormGenerator = new classFormGenerator();
413
    objFormGenerator.render();
414
    var allowEdit = $allowEdit;
415
    var allowDelete = $allowDelete;
416
    /**
417
     * Get rows and set data table
418
     */
419
    var tableForm = $('#gridTable').dataTable({
420
        'processing': true,
421
        'serverSide': true,
422
        'searching': true,
423
        'order': [
424
            [0, 'asc']
425
        ],
426
        'ordering': true,
427
        'ordenable': true,
428
        'responsive': true,
429
        'select': false,
430
        'paging': true,
431
        'pagingType': 'simple_numbers',
432
        'ajax': {
433
            'url': '$routeDatatable',
434
            'type': 'get',
435
            'beforeSend': function(request) {
436
                NProgress.start();
437
            },
438
            'dataFilter': function(response) {
439
                var response = jQuery.parseJSON(response);
440
                var json = {};
441
                json.recordsTotal = 0;
442
                json.recordsFiltered = 0;
443
                json.data = [];
444
                if (response.success) {
445
                    json.recordsTotal = response.data.total;
446
                    json.recordsFiltered = response.data.total;
447
                    json.data = response.data.items;
448
                } else {
449
                    $.fn.showError(response.data)
450
                }
451
                return JSON.stringify(json);
452
            }
453
        },
454
        'language': {
455
            'sProcessing': 'LABEL_DATATABLE_SPROCESSING',
456
            'sLengthMenu': 'LABEL_DATATABLE_SLENGTHMENU',
457
            'sZeroRecords': 'LABEL_DATATABLE_SZERORECORDS',
458
            'sEmptyTable': 'LABEL_DATATABLE_SEMPTYTABLE',
459
            'sInfo': 'LABEL_DATATABLE_SINFO',
460
            'sInfoEmpty': 'LABEL_DATATABLE_SINFOEMPTY',
461
            'sInfoFiltered': 'LABEL_DATATABLE_SINFOFILTERED',
462
            'sInfoPostFix': '',
463
            'sSearch': 'LABEL_DATATABLE_SSEARCH',
464
            'sUrl': '',
465
            'sInfoThousands': ',',
466
            'sLoadingRecords': 'LABEL_DATATABLE_SLOADINGRECORDS',
467
            'oPaginate': {
468
                'sFirst': 'LABEL_DATATABLE_SFIRST',
469
                'sLast': 'LABEL_DATATABLE_SLAST',
470
                'sNext': 'LABEL_DATATABLE_SNEXT',
471
                'sPrevious': 'LABEL_DATATABLE_SPREVIOUS'
472
            },
473
            'oAria': {
474
                'sSortAscending': ': LABEL_DATATABLE_SSORTASCENDING',
475
                'sSortDescending': ':LABEL_DATATABLE_SSORTDESCENDING'
476
            },
477
        },
478
        'drawCallback': function(settings) {
479
            NProgress.done();
480
            $('button.btn-delete').confirmation({
481
                rootSelector: 'button.btn-delete',
482
                title: 'LABEL_ARE_YOU_SURE',
483
                singleton: true,
484
                btnOkLabel: 'LABEL_YES',
485
                btnCancelLabel: 'LABEL_NO',
486
                onConfirm: function(value) {
487
                    action = $(this).data('href');
488
                    NProgress.start();
489
                    $.ajax({
490
                        'dataType': 'json',
491
                        'accept': 'application/json',
492
                        'method': 'post',
493
                        'url': action,
494
                    }).done(function(response) {
495
                        if (response['success']) {
496
                            $.fn.showSuccess(response['data']);
497
                            tableForm.fnDraw();
498
                        } else {
499
                            $.fn.showError(response['data']);
500
                        }
501
                    }).fail(function(jqXHR, textStatus, errorThrown) {
502
                        $.fn.showError(textStatus);
503
                    }).always(function() {
504
                        NProgress.done();
505
                    });
506
                },
507
            });
508
        },
509
        'aoColumns': [{
510
                'mDataProp': 'name'
511
            },
512
            {
513
                'mDataProp': 'language'
514
            },
515
            {
516
                'mDataProp': 'status'
517
            },
518
            {
519
                'mDataProp': 'actions'
520
            },
521
        ],
522
        'columnDefs': [{
523
                'targets': 0,
524
                'className': 'text-vertical-middle',
525
            },
526
            {
527
                'targets': 1,
528
                'className': 'text-vertical-middle',
529
            },
530
            {
531
                'targets': -2,
532
                'orderable': false,
533
                'className': 'text-center',
534
                'render': function(data, type, row) {
535
                    checked = data == 'a' ? ' checked="checked" ' : '';
536
                    return '<div class="checkbox checkbox-success">' +
537
                        '<input class="styled" type="checkbox" ' + checked + ' disabled="disabled">' +
538
                        '<label ></label></div>';
539
                }
540
            },
541
            {
542
                'targets': -1,
543
                'orderable': false,
544
                'render': function(data, type, row) {
545
                    s = '';
546
                    if (allowEdit) {
547
                        s = s + '<button class="btn btn-primary btn-edit-form" data-href="' + data['link_edit'] + '" data-toggle="tooltip" title="LABEL_EDIT"><i class="fa fa-pencil"></i> LABEL_EDIT </button>&nbsp;';
548
                    }
549
                    if (allowDelete) {
550
                        s = s + '<button class="btn btn-danger btn-delete" data-href="' + data['link_delete'] + '" data-toggle="tooltip" title="LABEL_DELETE"><i class="fa fa-trash"></i> LABEL_DELETE </button>&nbsp;';
551
                    }
552
                    return s;
553
                }
554
            }
555
        ],
556
    });
557
    /**
558
     * Clicked on edit form
559
     */
560
    $('body').on('click', 'button.btn-edit-form', function(e) {
561
        e.preventDefault();
562
        form_id = $(this).data('id')
563
        var action = $(this).data('href');
564
        $.ajax({
565
            'dataType': 'json',
566
            'accept': 'application/json',
567
            'method': 'get',
568
            'url': action,
569
        }).done(function(response) {
570
            if (response['success']) {
571
                validatorForm.resetForm();
572
                $('#form-main').attr('action', action);
573
                $('#form-main #form-id').val(response['data']['id']),
574
                $('#form-main #form-continue').val('0');
575
                $('#form-main #form-name').val(response['data']['name']),
576
 
577
                /*----------Set Ckeditor ------------*/
578
                CKEDITOR.instances['form-description'].setData(response['data']['description']);
579
                CKEDITOR.instances['form-text'].setData(response['data']['text']);
580
 
581
                $('#form-main #form-language').val(response['data']['language']),
582
                $('#form-main #form-status').val(response['data']['status']);
583
 
584
                /*-------------Render Sections -------*/
585
 
586
                objFormGenerator.clear();
587
                objFormGenerator.sections = response['data']['content'] || [];
588
                objFormGenerator.render();
589
                renderSectionData(objFormGenerator.sections);
590
 
254 geraldo 591
                $('#row-forms').hide();
251 geraldo 592
                $('#row-edit').show();
593
                $('#form-main #form-name').focus();
594
 
595
            } else {
596
                $.fn.showError(response['message'] || 'ERROR_UNKNOWN');
597
            }
598
        }).fail(function(jqXHR, textStatus, errorThrown) {
599
            $.fn.showError(textStatus);
600
        });
601
    });
602
    $(document).on('click', '[data-type="select_all"]', function() {
603
        if ($('input[name="select_all"]:checked').val() == 'all') {
604
            $('[data-type="select"]').prop('checked', true);
605
            $('[data-action="delete"]').removeClass('hide');
606
        } else {
607
            $('[data-type="select"]').prop('checked', false);
608
            $('[data-action="delete"]').addClass('hide');
609
        }
610
    });
611
 
612
    function getLength() {
613
        return $('[data-type="select"]').length;
614
    }
615
 
616
    function currentSelected() {
617
        var selected = [];
618
        $.each($("input[name='select[]']:checked"), function() {
619
            selected.push($(this).val());
620
        });
621
        return selected;
622
    }
623
    $(document).on('change', "input[name='select[]']", function() {
624
        var c = currentSelected();
625
        var cc = getLength();
626
        if (c.length == cc) {
627
            if ($('[data-action="delete"]').hasClass('hide')) {
628
                $('[data-action="delete"]').removeClass('hide');
629
            }
630
            return true;
631
        } else {
632
            $('input[name="select_all"]').prop('checked', false);
633
        }
634
        if (c.length > 0) {
635
            if ($('[data-action="delete"]').hasClass('hide')) {
636
                $('[data-action="delete"]').removeClass('hide');
637
            }
638
        } else {
639
            if (!$('[data-action="delete"]').hasClass('hide')) {
640
                $('[data-action="delete"]').addClass('hide');
641
            }
642
        }
643
    });
644
    var form3 = $('#form_sample_3');
645
    var error3 = $('.alert-danger', form3);
646
    var success3 = $('.alert-success', form3);
647
    //IMPORTANT: update CKEDITOR textarea with actual content before submit
648
    $("#form-main").on('submit', function() {
649
        for (var instanceName in CKEDITOR.instances) {
650
            CKEDITOR.instances[instanceName].updateElement();
651
        }
652
    })
653
    /**
654
     * Validate rules form
655
     */
656
    var validatorForm = $("#form-main").validate({
657
        ignore: [],
658
        errorClass: 'help-block',
659
        errorElement: 'span',
660
        rules: {
661
            'form-name': {
662
                required: true,
663
                minlength: 2,
664
                maxlength: 50
665
            },
666
            'form-description': {
667
                required: true,
668
            },
669
            'form-text': {
670
                required: true,
671
            },
672
            'form-language': {
673
                required: true,
674
            },
675
            'forrm-status': {
676
                required: true,
677
            },
678
        },
679
        highlight: function(element) {
680
            $(element).closest('.form-group').addClass('has-error');
681
        },
682
        unhighlight: function(element) {
683
            $(element).closest('.form-group').removeClass('has-error');
684
        },
685
        errorPlacement: function(error, element) {
686
            if (element.attr("data-error-container")) {
687
                error.appendTo(element.attr("data-error-container"));
688
            } else {
689
                error.insertAfter(element);
690
            }
691
        },
692
        invalidHandler: function(form, validator) {
693
            if (!validator.numberOfInvalids())
694
                return;
695
            $('html, body').animate({
696
                scrollTop: $(validator.errorList[0].element).offset().top - 100
697
            }, 1000);
698
        },
699
        submitHandler: function(form) {
700
            var error = false;
701
            if (objFormGenerator.sections.length == 0) {
702
                $.fn.showError('ERROR_SECCTIONS');
703
                return false;
704
            } else {
705
                for (i = 0; i < objFormGenerator.sections.length; i++) {
706
                    if (objFormGenerator.sections[i].questions.length == 0) {
707
                        $.fn.showError('ERROR_QUESTIONS'.replace('%s', objFormGenerator.sections[i].name));
708
                        return false;
709
                    }
710
                    var valueSection = parseInt(objFormGenerator.sections[i].value);
711
                    var totalValueQuestion = 0;
712
                    for (j = 0; j < objFormGenerator.sections[i].questions.length; j++) {
713
                        valueQuestion = parseInt(objFormGenerator.sections[i].questions[j].value);
714
                        totalValueQuestion = totalValueQuestion + valueQuestion;
715
                        if (objFormGenerator.sections[i].questions[j].type == 'simple' ||
716
                            objFormGenerator.sections[i].questions[j].type == 'multiple' ||
717
                            objFormGenerator.sections[i].questions[j].type == 'rating-open') {
718
                            var questionNumber = j + 1;
719
                            var numberCorrect = 0;
720
                            if (objFormGenerator.sections[i].questions[j].options.length == 0) {
721
                                $.fn.showError('ERROR_OPTIONS'.replace('%s', objFormGenerator.sections[i].name).replace('%n', questionNumber));
722
                                return false;
723
                            }
724
                            var totalOption = 0;
725
                            var maxOption = 0;
726
                            for (k = 0; k < objFormGenerator.sections[i].questions[j].options.length; k++) {
727
                                if (objFormGenerator.sections[i].questions[j].type == 'simple' || objFormGenerator.sections[i].questions[j].type == 'multiple') {
728
                                    if (objFormGenerator.sections[i].questions[j].options[k].correct == 1) {
729
                                        numberCorrect++;
730
                                    }
731
                                }
732
                                if (objFormGenerator.sections[i].questions[j].type == 'multiple' && objFormGenerator.sections[i].questions[j].options[k].correct == 1) {
733
                                    totalOption = totalOption + parseInt(objFormGenerator.sections[i].questions[j].options[k].value);
734
                                }
735
                                if (objFormGenerator.sections[i].questions[j].type == 'rating-open') {
736
                                    if (parseInt(objFormGenerator.sections[i].questions[j].options[k].value) > maxOption) {
737
                                        maxOption = parseInt(objFormGenerator.sections[i].questions[j].options[k].value);
738
                                    }
739
                                }
740
                            }
741
                            if (objFormGenerator.sections[i].questions[j].type == 'simple' || objFormGenerator.sections[i].questions[j].type == 'multiple') {
742
                                if (numberCorrect == 0) {
743
                                    $.fn.showError('ERROR_OPTIONS_CORRECT'.replace('%s', objFormGenerator.sections[i].name).replace('%n', questionNumber));
744
                                    return false;
745
                                }
746
                                if (
747
                                    objFormGenerator.sections[i].questions[j].type == 'simple' && numberCorrect > 1) {
748
                                    $.fn.showError('ERROR_OPTIONS_DUPLICATE_CORRECT'.replace('%s', objFormGenerator.sections[i].name).replace('%n', questionNumber));
749
                                    return false;
750
                                }
751
                                if (objFormGenerator.sections[i].questions[j].type == 'multiple' && numberCorrect == 1) {
752
                                    $.fn.showError('ERROR_OPTIONS_ONE_CORRECT'.replace('%s', objFormGenerator.sections[i].name).replace('%n', questionNumber));
753
                                    return false;
754
                                }
755
                            }
756
                            if (objFormGenerator.sections[i].questions[j].type == 'multiple' && totalOption != valueQuestion) {
757
                                $.fn.showError('ERROR_OPTIONS_SUM_VALUES'.replace('%s', objFormGenerator.sections[i].name).replace('%n', questionNumber));
758
                                return false;
759
                            }
760
                            if (objFormGenerator.sections[i].questions[j].type == 'rating-open' && maxOption > valueQuestion) {
761
                                $.fn.showError('ERROR_OPTIONS_MAX_OPTION'.replace('%s', objFormGenerator.sections[i].name).replace('%n', questionNumber));
762
                                return false;
763
                            }
764
                        }
765
                    }
766
                    if (valueSection != totalValueQuestion) {
767
                        $.fn.showError('ERROR_VALUE_SECTIONS'.replace('%s', objFormGenerator.sections[i].name));
768
                        return false;
769
                    }
770
                }
771
                var formId = parseInt($('#form-main #form-id').val());
772
                var formContinue = parseInt($('#form-main #form-continue').val());
773
                var data = {
774
                    'id': formId,
775
                    'name': $('#form-main #form-name').val(),
776
                    'description': $('#form-main #form-description').val(),
777
                    'text': $('#form-main #form-text').val(),
778
                    'language': $('#form-main #form-language').val(),
779
                    'status': $('#form-main #form-status').val(),
780
                    'content': JSON.stringify(objFormGenerator.sections)
781
                }
782
                $.ajax({
783
                    'dataType': 'json',
784
                    'method': 'post',
785
                    'url': $('#form-main').attr('action'),
786
                    'data': data,
787
                }).done(function(response) {
788
                    if (response['success']) {
789
                        $.fn.showSuccess(response['data']);
790
                        if (formContinue == 1) {
791
                            $('#form-main').attr('action',response['action_edit']);
792
                            $('#form-main #form-id').val(response['id']);
793
                            $('#form-main #form-continue').val(0);
794
                        } else {
795
                            $('#row-edit').hide();
254 geraldo 796
                            $('#row-forms').show();
251 geraldo 797
                        /*---------- Reset Form -------- */
798
                        $('#form-main')[0].reset();
799
                        /*--------Reset Ckeditor ----------*/
800
                        CKEDITOR.instances['form-text'].setData('');
801
                        CKEDITOR.instances['form-description'].setData('');
802
                        /*--------Reset Sections ----------*/
803
                        clearSectionData();
804
                        /* ------- Refresh Table -----------*/
805
 
806
                        }
807
                        tableForm.fnDraw();
808
                    } else {
809
                        $.fn.showError(response['message'] || 'ERROR_UNKNOWN');
810
                    }
811
                }).fail(function(jqXHR, textStatus, errorThrown) {
812
                    $.fn.showError(textStatus);
813
                });
814
                return false;
815
            }
816
        }
817
    });
254 geraldo 818
    //IMPORTANT: update CKEDITOR textarea with actual content before submit
819
    $("#form-section").on('submit', function() {
820
        for (var instanceName in CKEDITOR.instances) {
821
            CKEDITOR.instances[instanceName].updateElement();
822
        }
823
    })
251 geraldo 824
    /**
254 geraldo 825
     * Validate rules form section
826
     */
827
    var validatorFormSection = $("#form-section").validate({
828
        ignore: [],
829
        errorClass: 'help-block',
830
        errorElement: 'span',
831
        rules: {
832
            'section-name': {
833
                required: true,
834
                minlength: 2,
835
                maxlength: 50
836
            },
837
            'section-text': {
838
                required: false,
839
            },
840
            'section-value': {
841
                required: true,
842
                number: true,
843
                min: 1
844
            },
845
        },
846
        highlight: function(element) {
847
            $(element).closest('.form-group').addClass('has-error');
848
        },
849
        unhighlight: function(element) {
850
            $(element).closest('.form-group').removeClass('has-error');
851
        },
852
        errorPlacement: function(error, element) {
853
            if (element.attr("data-error-container")) {
854
                error.appendTo(element.attr("data-error-container"));
855
            } else {
856
                error.insertAfter(element);
857
            }
858
        },
859
        invalidHandler: function(form, validator) {
860
            if (!validator.numberOfInvalids())
861
                return;
862
            $('html, body').animate({
863
                scrollTop: $(validator.errorList[0].element).offset().top - 100
864
            }, 1000);
865
        },
866
        submitHandler: function(form) {
867
            // do other things for a valid form
868
            //form.submit();
869
            var slug = $('#form-section #section-slug').val();
870
            if (slug) {
871
                objFormGenerator.editSection(
872
                    $('#form-section #section-slug').val(),
873
                    $('#form-section #section-name').val(),
874
                    $('#form-section #section-text').val(),
875
                    $('#form-section #section-value').val()
876
                );
877
            } else {
878
                objFormGenerator.addSection(
879
                    $('#form-section #section-name').val(),
880
                    $('#form-section #section-text').val(),
881
                    $('#form-section #section-value').val()
882
                );
883
            }
884
            renderSectionData(objFormGenerator.sections);
885
            $('#modal-section').modal('hide');
886
            return false;
887
        }
888
    });
889
    //IMPORTANT: update CKEDITOR textarea with actual content before submit
890
    $("#form-question").on('submit', function() {
891
        for (var instanceName in CKEDITOR.instances) {
892
            CKEDITOR.instances[instanceName].updateElement();
893
        }
894
    })
895
    /**
896
     * Validate rules form Questions
897
     */
898
    var validatorFormQuestion = $("#form-question").validate({
899
        ignore: [],
900
        errorClass: 'help-block',
901
        errorElement: 'span',
902
        rules: {
903
            'question-text': {
904
                required: true,
905
            },
906
            'question-value': {
907
                required: true,
908
                number: true,
909
                min: 1
910
            },
911
            'question-type': {
912
                required: true,
913
            },
914
            'question-max-length': {
915
                required: true,
916
                digits: true,
917
                min: 0
918
            },
919
            'question-range': {
920
                required: true,
921
                number: true,
922
                min: 1
923
            },
924
        },
925
        highlight: function(element) {
926
            $(element).closest('.form-group').addClass('has-error');
927
        },
928
        unhighlight: function(element) {
929
            $(element).closest('.form-group').removeClass('has-error');
930
        },
931
        errorPlacement: function(error, element) {
932
            if (element.attr("data-error-container")) {
933
                error.appendTo(element.attr("data-error-container"));
934
            } else {
935
                error.insertAfter(element);
936
            }
937
        },
938
        invalidHandler: function(form, validator) {
939
            if (!validator.numberOfInvalids())
940
                return;
941
            $('html, body').animate({
942
                scrollTop: $(validator.errorList[0].element).offset().top - 100
943
            }, 1000);
944
        },
945
        submitHandler: function(form) {
946
            if ($('#form-question #question-slug').val()) {
947
                objFormGenerator.editQuestion(
948
                    $('#form-question #question-section').val(),
949
                    $('#form-question #question-slug').val(),
950
                    $('#form-question #question-text').val(),
951
                    $('#form-question #question-value').val(),
952
                    $('#form-question #question-type').val(),
953
                    $('#form-question #question-max-length').val(),
954
                    $('#form-question #question-multiline').val(),
955
                    $('#form-question #question-range').val()
956
                );
957
            } else {
958
                objFormGenerator.addQuestion(
959
                    $('#form-question #question-section').val(),
960
                    $('#form-question #question-text').val(),
961
                    $('#form-question #question-value').val(),
962
                    $('#form-question #question-type').val(),
963
                    $('#form-question #question-max-length').val(),
964
                    $('#form-question #question-multiline').val(),
965
                    $('#form-question #question-range').val()
966
                );
967
            }
968
            renderSectionData(objFormGenerator.sections);
969
            $('#modal-question').modal('hide');
970
            return false;
971
        }
972
    });
973
    //IMPORTANT: update CKEDITOR textarea with actual content before submit
974
    $("#form-option").on('submit', function() {
975
        for (var instanceName in CKEDITOR.instances) {
976
            CKEDITOR.instances[instanceName].updateElement();
977
        }
978
    })
979
    /**
980
     * Validate rules form options
981
     */
982
    var validatorFormOption = $("#form-option").validate({
983
        ignore: [],
984
        errorClass: 'help-block',
985
        errorElement: 'span',
986
        rules: {
987
            'option-text': {
988
                required: true,
989
            },
990
            'option-value': {
991
                required: true,
992
                number: true,
993
                min: 1
994
            }
995
        },
996
        highlight: function(element) {
997
            $(element).closest('.form-group').addClass('has-error');
998
        },
999
        unhighlight: function(element) {
1000
            $(element).closest('.form-group').removeClass('has-error');
1001
        },
1002
        errorPlacement: function(error, element) {
1003
            if (element.attr("data-error-container")) {
1004
                error.appendTo(element.attr("data-error-container"));
1005
            } else {
1006
                error.insertAfter(element);
1007
            }
1008
        },
1009
        invalidHandler: function(form, validator) {
1010
            if (!validator.numberOfInvalids())
1011
                return;
1012
            $('html, body').animate({
1013
                scrollTop: $(validator.errorList[0].element).offset().top - 100
1014
            }, 1000);
1015
        },
1016
        submitHandler: function(form) {
1017
            if ($('#form-option #option-slug').val()) {
1018
                objFormGenerator.editOption(
1019
                    $('#form-option #option-section').val(),
1020
                    $('#form-option #option-question').val(),
1021
                    $('#form-option #option-slug').val(),
1022
                    $('#form-option #option-text').val(),
1023
                    $('#form-option #option-correct').val(),
1024
                    $('#form-option #option-value').val()
1025
                );
1026
            } else {
1027
                objFormGenerator.addOption(
1028
                    $('#form-option #option-section').val(),
1029
                    $('#form-option #option-question').val(),
1030
                    $('#form-option #option-text').val(),
1031
                    $('#form-option #option-correct').val(),
1032
                    $('#form-option #option-value').val()
1033
                );
1034
            }
1035
            renderSectionData(objFormGenerator.sections);
1036
            $('#modal-option').modal('hide');
1037
            return false;
1038
        }
1039
    });
1040
    /**
1041
     * Clicked on add new section
1042
     */
1043
    $('body').on('click', 'button[id="btn-add-section"]', function(e) {
1044
        e.preventDefault();
1045
        validatorFormSection.resetForm();
1046
        $('#form-section #section-slug').val('');
1047
        $('#form-section #section-name').val('');
1048
        CKEDITOR.instances['section-text'].setData('');
1049
        $('#form-section #section-value').val('0');
1050
        $('#modal-section h4[class="modal-title"]').html('LABEL_ADD LABEL_SECTION');
1051
        $('#modal-section').modal('show');
1052
    });
1053
    /**
1054
     * Clicked on edit section
1055
     */
1056
    $('body').on('click', 'button.btn-edit-section', function(e) {
1057
        e.preventDefault();
1058
        var slug = $(this).data('section');
1059
        var section;
1060
        var showForm = false;
1061
        for (i = 0; i < objFormGenerator.sections.length; i++) {
1062
            section = objFormGenerator.sections[i];
1063
            if (slug == section.slug_section) {
1064
                validatorFormSection.resetForm();
1065
                $('#form-section #section-slug').val(section.slug_section);
1066
                $('#form-section #section-name').val(section.name);
1067
                CKEDITOR.instances['section-text'].setData(section.text);
1068
                $('#form-section #section-value').val(section.value);
1069
                showForm = true;
1070
                break;
1071
            }
1072
        }
1073
        if (showForm) {
1074
            $('#modal-section h4[class="modal-title"]').html('LABEL_EDIT LABEL_SECTION');
1075
            $('#modal-section').modal('show');
1076
        }
1077
    });
1078
    /**
1079
     * Clicked on remove section
1080
     */
1081
    $('body').on('click', 'button.btn-delete-section', function(e) {
1082
        e.preventDefault();
1083
        var slug = $(this).data('section');
1084
        bootbox.confirm({
1085
            title: "LABEL_DELETE LABEL_SECTION",
1086
            message: "LABEL_QUESTION_DELETE",
1087
            buttons: {
1088
                cancel: {
1089
                    label: '<i class="fa fa-times"></i> LABEL_CANCEL'
1090
                },
1091
                confirm: {
1092
                    label: '<i class="fa fa-check"></i> LABEL_ACCEPT'
1093
                }
1094
            },
1095
            callback: function(result) {
1096
                if (result) {
1097
                    objFormGenerator.deleteSection(slug);
1098
                    renderSectionData(objFormGenerator.sections);
1099
                }
1100
            }
1101
        });
1102
    });
1103
    /**
1104
     * Clicked add new question
1105
     */
1106
    $('body').on('click', 'button.btn-add-question', function(e) {
1107
        e.preventDefault();
1108
        validatorFormQuestion.resetForm();
1109
        var slug = $(this).data('section');
1110
        $('#form-question #question-section').val(slug);
1111
        $('#form-question #question-slug').val('');
1112
        CKEDITOR.instances['question-text'].setData('');
1113
        $('#form-question #question-value').val('0');
1114
        $('#form-question #question-type').val($('#form-question #question-type option:first').val());
1115
        $('#form-question #question-max-length').val('0');
1116
        $('#form-question #question-max-length').parent().show();
1117
        $('#form-question #question-multiline').val('0');
1118
        $('#form-question #question-multiline').parent().show();
1119
        $('#form-question #question-range').val('10');
1120
        $('#form-question #question-range').parent().hide();
1121
        $('#modal-question h4[class="modal-title"]').html('LABEL_ADD LABEL_QUESTION');
1122
        $('#modal-question').modal('show');
1123
    });
1124
    /**
1125
     * Clicked edit question
1126
     */
1127
    $('body').on('click', 'button.btn-edit-question', function(e) {
1128
        e.preventDefault();
1129
        var slug_section = $(this).data('section');
1130
        var slug = $(this).data('question');
1131
        var showForm = false;
1132
        for (i = 0; i < objFormGenerator.sections.length; i++) {
1133
            if (slug_section == objFormGenerator.sections[i].slug_section) {
1134
                for (j = 0; j < objFormGenerator.sections[i].questions.length; j++) {
1135
                    if (slug == objFormGenerator.sections[i].questions[j].slug_question) {
1136
                        validatorFormQuestion.resetForm();
1137
                        $('#form-question #question-section').val(objFormGenerator.sections[i].slug_section);
1138
                        $('#form-question #question-slug').val(objFormGenerator.sections[i].questions[j].slug_question);
1139
                        CKEDITOR.instances['question-text'].setData(objFormGenerator.sections[i].questions[j].text);
1140
                        $('#form-question #question-value').val(objFormGenerator.sections[i].questions[j].value);
1141
                        $('#form-question #question-type').val(objFormGenerator.sections[i].questions[j].type);
1142
                        if (objFormGenerator.sections[i].questions[j].type == 'open') {
1143
                            $('#form-question #question-max-length').val(objFormGenerator.sections[i].questions[j].maxlength);
1144
                            $('#form-question #question-max-length').parent().show();
1145
                            $('#form-question #question-multiline').val(objFormGenerator.sections[i].questions[j].multiline);
1146
                            $('#form-question #question-multiline').parent().show();
1147
                        } else {
1148
                            $('#form-question #question-max-length').val('0');
1149
                            $('#form-question #question-max-length').parent().hide();
1150
                            $('#form-question #question-multiline').val('0');
1151
                            $('#form-question #question-multiline').parent().hide();
1152
                        }
1153
                        if (objFormGenerator.sections[i].questions[j].type == 'rating-range') {
1154
                            $('#form-question #question-range').val(objFormGenerator.sections[i].questions[j].range);
1155
                            $('#form-question #question-range').parent().show();
1156
                        } else {
1157
                            $('#form-question #question-range').val('10');
1158
                            $('#form-question #question-range').parent().hide();
1159
                        }
1160
                        showForm = true;
1161
                        break;
1162
                    }
1163
                }
1164
                break;
1165
            }
1166
        }
1167
        if (showForm) {
1168
            $('#modal-question h4[class="modal-title"]').html('LABEL_EDIT LABEL_QUESTION');
1169
            $('#modal-question').modal('show');
1170
        }
1171
    });
1172
    /**
1173
     * Clicked remove question
1174
     */
1175
    $('body').on('click', 'button.btn-delete-question', function(e) {
1176
        e.preventDefault();
1177
        var slug_section = $(this).data('section');
1178
        var slug = $(this).data('question');
1179
        bootbox.confirm({
1180
            title: "LABEL_DELETE LABEL_QUESTION",
1181
            message: "LABEL_QUESTION_DELETE",
1182
            buttons: {
1183
                cancel: {
1184
                    label: '<i class="fa fa-times"></i> LABEL_CANCEL'
1185
                },
1186
                confirm: {
1187
                    label: '<i class="fa fa-check"></i> LABEL_ACCEPT'
1188
                }
1189
            },
1190
            callback: function(result) {
1191
                if (result) {
1192
                    objFormGenerator.deleteQuestion(slug_section, slug);
1193
                    renderSectionData(objFormGenerator.sections);
1194
                }
1195
            }
1196
        });
1197
    });
1198
    /**
1199
     * Clicked add new Option
1200
     */
1201
    $('body').on('click', 'button.btn-add-option', function(e) {
1202
        e.preventDefault();
1203
        var slug_section = $(this).data('section');
1204
        var slug_question = $(this).data('question');
1205
        var showForm = false;
1206
        for (i = 0; i < objFormGenerator.sections.length; i++) {
1207
            if (slug_section == objFormGenerator.sections[i].slug_section) {
1208
                for (j = 0; j < objFormGenerator.sections[i].questions.length; j++) {
1209
                    if (slug_question == objFormGenerator.sections[i].questions[j].slug_question) {
1210
                        validatorFormOption.resetForm();
1211
                        $('#form-option #option-section').val(slug_section);
1212
                        $('#form-option #option-question').val(slug_question);
1213
                        $('#form-option #option-slug').val('');
1214
                        CKEDITOR.instances['option-text'].setData('', function() {
1215
                            editor.focus();
1216
                        });
1217
                        $('#form-option #option-correct').val('0');
1218
                        if (objFormGenerator.sections[i].questions[j].type == 'rating-open') {
1219
                            $('#form-option #option-correct').parent().hide();
1220
                        } else {
1221
                            $('#form-option #option-correct').parent().show();
1222
                        }
1223
                        if (objFormGenerator.sections[i].questions[j].type == 'multiple' || objFormGenerator.sections[i].questions[j].type == 'rating-open') {
1224
                            $('#form-option #option-value').val('0');
1225
                            $('#form-option #option-value').parent().show();
1226
                        } else {
1227
                            $('#form-option #option-value').val('1');
1228
                            $('#form-option #option-value').parent().hide();
1229
                        }
1230
                        renderSectionData(objFormGenerator.sections);
1231
                        $('#modal-option h4[class="modal-title"]').html('LABEL_ADD LABEL_OPTION');
1232
                        $('#modal-option').modal('show');
1233
                        return true;
1234
                    }
1235
                }
1236
            }
1237
        }
1238
    });
1239
    /**
1240
     * Clicked edit option
1241
     */
1242
    $('body').on('click', 'button.btn-edit-option', function(e) {
1243
        e.preventDefault();
1244
        var slug_section = $(this).data('section');
1245
        var slug_question = $(this).data('question');
1246
        var slug = $(this).data('slug');
1247
        var showForm = false;
1248
        for (i = 0; i < objFormGenerator.sections.length; i++) {
1249
            if (slug_section == objFormGenerator.sections[i].slug_section) {
1250
                for (j = 0; j < objFormGenerator.sections[i].questions.length; j++) {
1251
                    if (slug_question == objFormGenerator.sections[i].questions[j].slug_question) {
1252
                        for (k = 0; k < objFormGenerator.sections[i].questions[j].options.length; k++) {
1253
                            if (slug == objFormGenerator.sections[i].questions[j].options[k].slug_option) {
1254
                                validatorFormOption.resetForm();
1255
                                $('#form-option #option-section').val(objFormGenerator.sections[i].slug_section);
1256
                                $('#form-option #option-question').val(objFormGenerator.sections[i].questions[j].slug_question);
1257
                                $('#form-option #option-slug').val(objFormGenerator.sections[i].questions[j].options[k].slug_option);
1258
                                CKEDITOR.instances['option-text'].setData(objFormGenerator.sections[i].questions[j].options[k].text,
1259
                                    function() {
1260
                                        editor.focus();
1261
                                    });
1262
                                $('#form-option #option-correct').val(objFormGenerator.sections[i].questions[j].options[k].correct);
1263
                                if (objFormGenerator.sections[i].questions[j].type == 'multiple' || objFormGenerator.sections[i].questions[j].type == 'simple') {
1264
                                    $('#form-option #option-correct').parent().show();
1265
                                } else {
1266
                                    $('#form-option #option-correct').parent().hide();
1267
                                }
1268
                                $('#form-option #option-value').val(objFormGenerator.sections[i].questions[j].options[k].value);
1269
                                if (objFormGenerator.sections[i].questions[j].type == 'multiple' || objFormGenerator.sections[i].questions[j].type == 'rating-open') {
1270
                                    $('#form-option #option-value').parent().show();
1271
                                } else {
1272
                                    $('#form-option #option-value').parent().hide();
1273
                                }
1274
                                showForm = true;
1275
                                break;
1276
                            }
1277
                        }
1278
                    }
1279
                    if (showForm) {
1280
                        break;
1281
                    }
1282
                }
1283
            }
1284
            if (showForm) {
1285
                break;
1286
            }
1287
        }
1288
        if (showForm) {
1289
            $('#modal-option h4[class="modal-title"]').html('LABEL_EDIT LABEL_OPTION');
1290
            $('#modal-option').modal('show');
1291
        }
1292
    });
1293
    /**
1294
     * Clicked remove option
1295
     */
1296
    $('body').on('click', 'button.btn-delete-option', function(e) {
1297
        e.preventDefault();
1298
        var slug_section = $(this).data('section');
1299
        var slug_question = $(this).data('question');
1300
        var slug = $(this).data('slug');
1301
        bootbox.confirm({
1302
            title: "LABEL_DELETE LABEL_OPTION",
1303
            message: "LABEL_QUESTION_DELETE",
1304
            buttons: {
1305
                cancel: {
1306
                    label: '<i class="fa fa-times"></i> LABEL_CANCEL'
1307
                },
1308
                confirm: {
1309
                    label: '<i class="fa fa-check"></i> LABEL_ACCEPT'
1310
                }
1311
            },
1312
            callback: function(result) {
1313
                if (result) {
1314
                    objFormGenerator.deleteOption(slug_section, slug_question, slug);
1315
                    renderSectionData(objFormGenerator.sections);
1316
                }
1317
            }
1318
        });
1319
    })
1320
    /**
1321
     * Format input number (Form Section)
1322
     */
1323
    $('#form-section #section-value').inputNumberFormat({
1324
        'decimal': 2
1325
    });
1326
    /**
1327
     * Format input number (Form Question)
1328
     */
1329
    $('#form-question #question-value').inputNumberFormat({
1330
        'decimal': 2
1331
    });
1332
    /**
1333
     * Format input number (Form Question)
1334
     */
1335
    $('#form-question #question-max-length').inputNumberFormat({
1336
        'decimal': 0
1337
    });
1338
    /**
1339
     * Format input number (Form Option)
1340
     */
1341
    $('#form-option #option-value').inputNumberFormat({
1342
        'decimal': 2
1343
    });
1344
    /**
251 geraldo 1345
     * Detect when updating question status and hide/Show options
1346
     */
1347
    $('#form-question #question-type').change(function(e) {
1348
        e.preventDefault();
1349
        if ($('#form-question #question-type').val() == 'open') {
1350
            $('#form-question #question-max-length').parent().show();
1351
            $('#form-question #question-multiline').parent().show();
1352
        } else {
1353
            $('#form-question #question-max-length').val('0');
1354
            $('#form-question #question-max-length').parent().hide();
1355
            $('#form-question #question-multiline').val('0');
1356
            $('#form-question #question-multiline').parent().hide();
1357
        }
1358
        $('#form-question #question-range').val('10');
1359
        if ($('#form-question #question-type').val() == 'rating-range') {
1360
            $('#form-question #question-range').parent().show();
1361
        } else {
1362
            $('#form-question #question-range').parent().hide();
1363
        }
1364
    });
1365
    /**
1366
     * Clicked cancel new/edit Form
1367
     */
1368
    $('button.btn-edit-cancel').click(function(e) {
1369
        e.preventDefault();
1370
        $('#row-edit').hide();
254 geraldo 1371
        $('#row-forms').show();
251 geraldo 1372
    });
1373
    /**
1374
     * Clicked save and continue new Form
1375
     */
1376
    $('button.btn-form-save-continue').click(function(e) {
1377
        e.preventDefault();
1378
        $('#form-main #form-continue').val('1')
1379
        $('#form-main').submit();
1380
    });
1381
    /**
1382
     * Clicked save and close new/edit Form
1383
     */
1384
    $('button.btn-form-save-close').click(function(e) {
1385
        e.preventDefault();
1386
        $('#form-main #form-continue').val('0')
1387
        $('#form-main').submit();
1388
    });
1389
    /**
1390
     * Modal Settings
1391
     */
1392
    $('#modal-section, #modal-question, #modal-option').modal({
1393
        backdrop: 'static',
1394
        keyboard: false,
1395
        show: false
1396
    });
1397
    /**
1398
     * Get Question type
1399
     */
1400
    const getQuestionTypeBySlug = (slug_section, slug_question) => {
1401
        for (i = 0; i < objFormGenerator.sections.length; i++) {
1402
            if (slug_section == objFormGenerator.sections[i].slug_section) {
1403
                for (j = 0; j < objFormGenerator.sections[i].questions.length; j++) {
1404
                    if (slug_question == objFormGenerator.sections[i].questions[j].slug_question) {
1405
                        return objFormGenerator.sections[i].questions[j].type;
1406
                    }
1407
                }
1408
            }
1409
        }
1410
    }
1411
    /**
1412
     * Remove Html Tags
1413
     */
1414
    const removeTags = (str) => str.toString().replace( /(<([^>]+)>)/ig, '')
1415
    /**
1416
     * Render Sections data
1417
     */
1418
    const renderSectionData = (data) => $("#rows").html($("#sectionTemplate").render(data, {
1419
        getType: getQuestionTypeBySlug,
1420
        removeTags: removeTags
1421
    }));
1422
 
1423
 
1424
    /**
1425
     * Clear Div Section data
1426
     */
1427
    const clearSectionData = () => $("#rows").html('');
1428
    /**
1429
     * Clicked refresh button
1430
     */
1431
    $('button.btn-refresh').click(function(e) {
1432
        tableForm.fnDraw();
1433
    });
1434
 
1435
});
1436
JS;
1437
$this->inlineScript()->captureEnd();
1438
?>
1439
 
1440
<!-- Content Header (Page header) -->
1441
<section class="content-header">
1442
    <div class="container-fluid">
1443
        <div class="row mb-2">
1444
            <div class="col-sm-12">
254 geraldo 1445
                <h1>LABEL_SELF_EVALUATION_FORMS</h1>
251 geraldo 1446
            </div>
1447
        </div>
1448
    </div>
1449
    <!-- /.container-fluid -->
1450
</section>
1451
<section class="content">
254 geraldo 1452
    <div class="container-fluid" id="row-forms">
251 geraldo 1453
        <div class="row">
1454
            <div class="col-12">
1455
                <div class="card">
1456
                    <div class="card-body">
1457
                        <table id="gridTable" class="table   table-hover">
1458
                            <thead>
1459
                                <tr>
1460
                                    <th>LABEL_NAME</th>
1461
                                    <th>LABEL_LANGUAGE</th>
254 geraldo 1462
                                    <th>LABEL_ACTIVE</th>
251 geraldo 1463
                                    <th>LABEL_ACTIONS</th>
1464
                                </tr>
1465
                            </thead>
1466
                            <tbody></tbody>
1467
                        </table>
1468
                    </div>
1469
                    <div class="card-footer clearfix">
1470
                        <div style="float:right;">
1471
                            <button type="button" class="btn btn-info btn-refresh"><i class="fa fa-refresh"></i> LABEL_REFRESH </button>
1472
                        </div>
1473
                    </div>
1474
                </div>
1475
            </div>
1476
        </div>
1477
    </div>
1478
 
1479
    <!-- Create/Edit Form -->
1480
 
1481
    <div class="row" id="row-edit" style="display: none">
1482
        <div class="col-xs-12 col-md-12">
1483
            <form action="#" name="form-main" id="form-main">
256 geraldo 1484
 
251 geraldo 1485
                <div class="form-group">
1486
                    <button type="submit" form="form-main" class="btn btn-info btn-form-save-continue">LABEL_SAVE & LABEL_CONTINUE</button>
1487
                    <button type="button" class="btn btn-primary btn-form-save-close">LABEL_SAVE & LABEL_CLOSE</button>
1488
                    <button type="button" class="btn btn-secondary btn-edit-cancel">LABEL_CANCEL</button>
1489
                </div>
1490
            </form>
1491
        </div>
1492
    </div>
1493
 
256 geraldo 1494
 
251 geraldo 1495
 
1496
    <!---Template Sections --->
1497
    <script id="sectionTemplate" type="text/x-jsrender">
1498
    <div class="panel panel-default" id="panel-{{:slug_section}}">
1499
        <div class="panel-heading">
1500
            <h4 class="panel-title">
1501
                <a class="accordion-toggle" data-toggle="collapse" aria-expanded="true" data-parent="#panel-{{:slug_section}}" href="#collapse-{{:slug_section}}">
1502
                    <span class="section-name{{:slug_section}}">
1503
                        {{:name}}
1504
                    </span>
1505
                </a>
1506
            </h4>
1507
        </div>
1508
        <div id="collapse-{{:slug_section}}" class="panel-collapse in collapse show">
1509
            <div class="panel-body">
1510
                <div class="table-responsive">
1511
                    <table class="table table-bordered">
1512
                        <thead>
1513
                            <tr>
1514
                                <th style="width: 10%;">LABEL_ELEMENT</th>
1515
                                <th style="width: 50%;">LABEL_TEXT</th>
1516
                                <th style="width: 10%;">LABEL_VALUE</th>
1517
                                <th style="width: 10%;">LABEL_TYPE</th>
1518
                                <th style="width: 20%;">LABEL_ACTIONS</th>
1519
                            </tr>
1520
                        </thead>
1521
                        <tbody>
1522
                            <tr class="tr-section">
1523
                                <td class="text-left">LABEL_SECTION</td>
1524
                                <td class="text-left">{{:name}}</td>
1525
                                <td>{{:value}}</td>
1526
                                <td></td>
1527
                                <td>
1528
                                    <button class="btn btn-default btn-edit-section" data-section="{{:slug_section}}" data-toggle="tooltip"  data-original-title="LABEL_EDIT LABEL_SECTION"><i class="fa fa-edit" aria-hidden="true"></i> LABEL_EDIT LABEL_SECTION </button>
1529
                                    <button class="btn btn-default btn-delete-section" data-section="{{:slug_section}}" data-toggle="tooltip"  data-original-title="LABEL_DELETE LABEL_SECTION"><i class="fa fa-ban" aria-hidden="true"></i> LABEL_DELETE LABEL_SECTION </button>
1530
                                    <button class="btn btn-default btn-add-question" data-section="{{:slug_section}}" data-toggle="tooltip"  data-original-title="LABEL_ADD  LABEL_QUESTION"><i class="fa fa-plus" aria-hidden="true"></i> LABEL_ADD  LABEL_QUESTION </button>
1531
                                </td>
1532
                            </tr>
1533
                            {{for questions}}
1534
                            <tr class="tr-question">
1535
                                <td class="text-left">--LABEL_QUESTION</td>
1536
                                <td class="text-left">
1537
                                    {{:~removeTags(text)}}
1538
                                </td>
1539
                                <td><font color="red">{{:value}}</font></td>
1540
                                <td class="text-capitalize">
1541
                                    {{if type == 'open'}} LABEL_OPEN {{/if}}
1542
                                    {{if type == 'simple'}} Simple {{/if}}
1543
                                    {{if type == 'multiple'}} Multiple {{/if}}
1544
                                    {{if type == 'rating-open'}} LABEL_RATING_OPEN {{/if}}
1545
                                    {{if type == 'rating-range'}} LABEL_RATING_RANGE {{/if}}
1546
                                </td>
1547
                                <td>
1548
                                    <button class="btn btn-default btn-edit-question" data-section="{{:slug_section}}" data-question="{{:slug_question}}" data-toggle="tooltip"  data-original-title="LABEL_EDIT LABEL_QUESTION"><i class="fa fa-edit" aria-hidden="true"></i> LABEL_EDIT LABEL_QUESTION</button>
1549
                                    <button class="btn btn-default btn-delete-question" data-section="{{:slug_section}}" data-question="{{:slug_question}}" data-toggle="tooltip"  data-original-title="LABEL_DELETE LABEL_QUESTION"><i class="fa fa-ban" aria-hidden="true"></i> LABEL_DELETE LABEL_QUESTION</button>
1550
 
1551
                                    {{if type == 'simple' || type == 'rating-open' || type=='multiple' }}
1552
                                    <button class="btn btn-default btn-add-option" data-section="{{:slug_section}}" data-question="{{:slug_question}}" data-toggle="tooltip"  data-original-title="LABEL_ADD  LABEL_OPTION"><i class="fa fa-plus" aria-hidden="true"></i> LABEL_ADD  LABEL_OPTION</button>
1553
                                    {{/if}}
1554
 
1555
                                </td>
1556
                            </tr>
1557
                            {{for options}}
1558
                            <tr class="tr-option">
1559
                                <td class="text-left">---LABEL_OPTION</td>
1560
                                <td class="text-left">
1561
                                    {{:~removeTags(text)}}
1562
                                </td>
1563
                                <td>
1564
 
1565
                                    {{if ~getType( slug_section, slug_question) == 'multiple' || ~getType( slug_section, slug_question) == 'rating-open'  }}
1566
                                    {{:value}}
1567
                                    {{/if}}
1568
                                </td>
1569
                                <td class="text-left">
1570
                                    {{if ~getType( slug_section, slug_question) != 'rating-open'}}
1571
                                    {{if correct == 1}}
1572
                                    <font color="green">LABEL_CORRECT</font>
1573
                                    {{/if}}
1574
                                    {{if correct == 0}}
1575
                                    <font color="red">LABEL_FAIL</font>
1576
                                    {{/if}}
1577
                                    {{/if}}
1578
                                </td>
1579
                                <td>
1580
                                    <button class="btn btn-default btn-edit-option" data-section="{{:slug_section}}" data-question="{{:slug_question}}" data-slug="{{:slug_option}}" data-toggle="tooltip"  data-original-title="LABEL_EDIT LABEL_OPTION"><i class="fa fa-edit" aria-hidden="true"></i> LABEL_EDIT LABEL_OPTION</button>
1581
                                    <button class="btn btn-default btn-delete-option" data-section="{{:slug_section}}" data-question="{{:slug_question}}" data-slug="{{:slug_option}}" data-toggle="tooltip"  data-original-title="LABEL_DELETE LABEL_OPTION"><i class="fa fa-ban" aria-hidden="true"></i> LABEL_DELETE LABEL_OPTION</button>
1582
                                </td>
1583
                            </tr>
1584
                            {{/for}}
1585
                            {{/for}}
1586
                        </tbody>
1587
                    </table>
1588
                </div>
1589
            </div>
1590
        </div>
1591
    </div>
1592
    </script>
1593
 
1594
    <!-- End Template Sections-->
1595
 
1596
</section>