Proyectos de Subversion LeadersLinked - Backend

Rev

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

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