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

ch_multiplechoiceother::render()   B

Complexity

Conditions 9
Paths 54

Size

Total Lines 53
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 35
nc 54
nop 3
dl 0
loc 53
rs 8.0555
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
class ch_multiplechoiceother extends survey_question
6
{
7
    /**
8
     * @param array $survey_data
9
     * @param array $formData
10
     *
11
     * @return FormValidator
12
     */
13
    public function createForm($survey_data, $formData)
14
    {
15
        parent::createForm($survey_data, $formData);
16
17
        $options = [
18
            'horizontal' => get_lang('Horizontal'),
19
            'vertical' => get_lang('Vertical'),
20
        ];
21
        $this->getForm()->addRadio('horizontalvertical', get_lang('DisplayAnswersHorVert'), $options);
22
23
        $formData['horizontalvertical'] = isset($formData['horizontalvertical']) ? $formData['horizontalvertical'] : 'horizontal';
24
        $this->getForm()->setDefaults($formData);
25
26
        $config = ['ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120'];
27
        $total = count($formData['answers']);
28
29
        if (is_array($formData['answers'])) {
30
            foreach ($formData['answers'] as $key => $value) {
31
                if ($value === 'other') {
32
                    continue;
33
                }
34
                $this->getForm()->addHtmlEditor('answers['.$key.']', null, false, false, $config);
35
                if ($total > 2) {
36
                    $this->getForm()->addButton("delete_answer[$key]", get_lang('Delete'), 'trash', 'danger');
37
                }
38
            }
39
        }
40
41
        if (isset($formData['answersid']) && !empty($formData['answersid'])) {
42
            $counter = 1;
43
            $total = count($formData['answersid']);
44
            foreach ($formData['answersid'] as $value) {
45
                if ($counter === $total) {
46
                    break;
47
                }
48
                $this->getForm()->addHidden('answersid[]', $value);
49
                $counter++;
50
            }
51
        }
52
53
        parent::addRemoveButtons($formData);
54
    }
55
56
    /**
57
     * @param array $questionData
58
     * @param array $answers
59
     */
60
    public function render(FormValidator $form, $questionData = [], $answers = [])
61
    {
62
        $question = new ch_yesno();
63
        $otherId = 0;
64
        foreach ($questionData['options'] as $key => $option) {
65
            if ('other' === $option) {
66
                $otherId = $key;
67
            }
68
        }
69
70
        foreach ($questionData['options'] as &$option) {
71
            if ($option === 'other') {
72
                $option = '<p>'.get_lang('SurveyOtherAnswerSpecify').'</p>';
73
            }
74
        }
75
        $questionId = $questionData['question_id'];
76
        $question->render($form, $questionData, $answers);
77
        $form->addHtml(
78
            '<script>
79
            $(function() {
80
                $("input:radio[name=\"question'.$questionId.'\"]").change(function() {
81
                    if ($(this).val() == "'.$otherId.'") {
82
                        $("#other_div_'.$questionId.'").show();
83
                    } else {
84
                        $("#other_div_'.$questionId.'").hide();
85
                        $("#other_question'.$questionId.'").val("");
86
                    }
87
                });
88
            });
89
            </script>'
90
        );
91
92
        $display = 'display:none';
93
        $defaultOtherData = '';
94
        if (!empty($answers)) {
95
            $answers = self::decodeOptionValue($answers);
96
            if (isset($answers[1])) {
97
                $display = '';
98
                $defaultOtherData = $answers[1];
99
            }
100
        }
101
        $form->addHtml('<div id="other_div_'.$questionId.'" class="multiple_choice_other" style="'.$display.'">');
102
        $element = $form->addText(
103
            'other_question'.$questionId,
104
            get_lang('SurveyOtherAnswer'),
105
            false,
106
            ['id' => 'other_question'.$questionId]
107
        );
108
        $form->addHtml('</div>');
109
110
        if (!empty($answers) && !empty($defaultOtherData)) {
111
            $element->setValue($defaultOtherData);
112
            $element->freeze();
113
        }
114
    }
115
116
    public static function decodeOptionValue($value)
117
    {
118
        return explode('@:@', $value);
119
    }
120
}
121