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

ch_multipleresponse::createForm()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 8
nop 2
dl 0
loc 32
rs 8.6666
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Class ch_multipleresponse.
6
 */
7
class ch_multipleresponse extends survey_question
8
{
9
    /**
10
     * @param array $surveyData
11
     * @param array $formData
12
     */
13
    public function createForm($surveyData, $formData)
14
    {
15
        parent::createForm($surveyData, $formData);
16
        $options = [
17
            'horizontal' => get_lang('Horizontal'),
18
            'vertical' => get_lang('Vertical'),
19
        ];
20
        $this->getForm()->addRadio('horizontalvertical', get_lang('DisplayAnswersHorVert'), $options);
21
22
        $formData['horizontalvertical'] = isset($formData['horizontalvertical']) ? $formData['horizontalvertical'] : 'horizontal';
23
        $this->getForm()->setDefaults($formData);
24
25
        $config = ['ToolbarSet' => 'Survey', 'Width' => '100%', 'Height' => '120'];
26
        if (is_array($formData['answers'])) {
27
            foreach ($formData['answers'] as $key => $value) {
28
                $this->getForm()->addHtmlEditor(
29
                    'answers['.$key.']',
30
                    null,
31
                    false,
32
                    false,
33
                    $config
34
                );
35
            }
36
        }
37
38
        if (isset($formData['answersid']) && !empty($formData['answersid'])) {
39
            foreach ($formData['answersid'] as $value) {
40
                $this->getForm()->addHidden('answersid[]', $value);
41
            }
42
        }
43
44
        parent::addRemoveButtons($formData);
45
    }
46
47
    /**
48
     * @param array $questionData
49
     * @param array $answers
50
     */
51
    public function render(
52
        FormValidator $form,
53
        $questionData = [],
54
        $answers = []
55
    ) {
56
        $class = 'checkbox-inline';
57
        $labelClass = 'checkbox-inline';
58
        if ('vertical' == $questionData['display']) {
59
            $class = 'checkbox-vertical';
60
        }
61
62
        $name = 'question'.$questionData['question_id'];
63
        $form->addCheckBoxGroup(
64
            $name,
65
            null,
66
            $questionData['options'],
67
            ['checkbox-class' => $class, 'label-class' => $labelClass]
68
        );
69
70
        $defaults = [];
71
        if (!empty($answers)) {
72
            foreach ($answers as $answer) {
73
                $defaults[$name.'['.$answer.']'] = true;
74
            }
75
        }
76
77
        $form->setDefaults($defaults);
78
    }
79
}
80