Completed
Push — master ( 1a5e2c...34133a )
by Julito
08:10
created

ch_selectivedisplay::getJs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
class ch_selectivedisplay extends ch_yesno
6
{
7
    /**
8
     * @param array $questionData
9
     * @param array $answers
10
     */
11
    public function render(FormValidator $form, $questionData = [], $answers = null)
12
    {
13
        if (is_array($questionData['options'])) {
14
            $class = 'radio-inline';
15
            $labelClass = 'radio-inline';
16
            if ($questionData['display'] === 'vertical') {
17
                $class = 'radio-vertical';
18
            }
19
20
            $name = 'question'.$questionData['question_id'];
21
            $radioAttributes = [
22
                'radio-class' => $class,
23
                'label-class' => $labelClass,
24
                'class' => 'survey_selective_input',
25
            ];
26
27
            if (!empty($questionData['is_required'])) {
28
                $radioAttributes['required'] = 'required';
29
            }
30
31
            $form->addRadio(
32
                $name,
33
                null,
34
                $questionData['options'],
35
                $radioAttributes
36
            );
37
38
            if (!empty($answers)) {
39
                $form->setDefaults([$name => is_array($answers) ? current($answers) : $answers]);
40
            }
41
        }
42
    }
43
44
    public static function getJs()
45
    {
46
        return '<script>
47
            $(function() {
48
                var hideQuestion = false;
49
                $(".survey_question").each(function() {
50
                    var questionClass = $(this).attr("class").trim();
51
                    if (hideQuestion) {
52
                        $(this).hide();
53
                        if (questionClass === "survey_question ch_selectivedisplay") {
54
                            $(this).show();
55
                        }
56
                    }
57
                    if (questionClass === "survey_question ch_selectivedisplay") {
58
                        hideQuestion = true;
59
                    }
60
                });
61
62
                $(".survey_selective_input").on("click", function() {
63
                   var parent = $(this).parent().parent().parent().parent();
64
                   var next = parent.nextAll();
65
                   var visible = $(this).attr("data-order") == 1;
66
67
                   next.each(function() {
68
                        if ($(this).attr("class") === "survey_question ch_selectivedisplay") {
69
                            return false;
70
                        }
71
                        if ($(this).attr("class") === "start-survey") {
72
                            return false;
73
                        }
74
                       if (visible) {
75
                           $(this).show();
76
                       } else {
77
                           $(this).hide();
78
                       }
79
                    });
80
                });
81
            });
82
83
            </script>';
84
    }
85
}
86