Proyectos de Subversion LeadersLinked - Backend

Rev

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

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