Passed
Pull Request — 1.11.x (#4360)
by Angel Fernando Quiroz
09:48
created

MultipleAnswerDropdown::createForm()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 47
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 47
rs 8.8017
cc 6
nc 4
nop 2
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
class MultipleAnswerDropdown extends Question
6
{
7
    public $typePicture = 'mcma.png';
8
    public $explanationLangVar = 'MultipleAnswerDropdown';
9
10
    public function __construct()
11
    {
12
        parent::__construct();
13
14
        $this->type = MULTIPLE_ANSWER_DROPDOWN;
15
    }
16
17
    public function createForm(&$form, $exercise)
18
    {
19
        global $text;
20
21
        parent::createForm($form, $exercise);
22
23
        $objExe = ChamiloSession::read('objExercise');
24
25
        $form->addTextarea(
26
            'list_text',
27
            [get_lang('AnswerList'), get_lang('EnterListOfAnswersOneAnswerByLine)')],
28
            ['rows' => 8]
29
        );
30
        $form->addFile(
31
            'list_file',
32
            ['', get_lang('OrSelectCsvFileWithListOfAnswers')],
33
            ['accept' => 'text/csv']
34
        );
35
36
        $buttonGroup = [];
37
38
        if ($objExe->edit_exercise_in_lp == true ||
39
            (empty($this->exerciseList) && empty($objExe->iid))
40
        ) {
41
            $buttonGroup[] = $form->addButton(
42
                'submitQuestion',
43
                $text,
44
                'check',
45
                'primary',
46
                'default',
47
                null,
48
                ['id' => 'submit-question'],
49
                true
50
            );
51
        }
52
53
        $form->addGroup($buttonGroup);
54
55
        if (!empty($this->iid) && !$form->isSubmitted()) {
56
            $objAnswer = new Answer($this->iid, 0, $exercise, false);
57
            $optionData = array_column(
58
                $objAnswer->getAnswers(),
59
                'answer'
60
            );
61
62
            $form->setDefaults(
63
                ['list_text' => implode(PHP_EOL, $optionData)]
64
            );
65
        }
66
    }
67
68
    public function createAnswersForm($form)
69
    {
70
    }
71
72
    public function processCreation($form, $exercise)
73
    {
74
        $listFile = $form->getSubmitValue('list_file');
75
        $listText = $form->getSubmitValue('list_text');
76
77
        parent::processCreation($form, $exercise);
78
79
        $lines = [];
80
81
        if (UPLOAD_ERR_OK === (int) $listFile['error']) {
82
            $csvData = Import::csvToArray($listFile['tmp_name']);
83
            $lines = array_map(
84
                function (array $row) {
85
                    return array_values($row)[0];
86
                },
87
                $csvData
88
            );
89
        } elseif (!empty($listText)) {
90
            $lines = array_map(
91
                function ($line) {
92
                    return trim($line);
93
                },
94
                explode("\n", $listText)
95
            );
96
        }
97
98
        $lines = array_filter(
99
            $lines,
100
            function (string $line): bool {
101
                $line = trim($line);
102
103
                return !empty($line);
104
            }
105
        );
106
107
        $objAnswer = new Answer($this->iid);
108
109
        $i = 1;
110
111
        foreach ($lines as $line) {
112
            $isCorrect = 0;
113
114
            if (isset($objAnswer->correct[$i])) {
115
                $isCorrect = (int) $objAnswer->correct[$i];
116
            }
117
118
            $objAnswer->createAnswer($line, $isCorrect, '', 0, $i++);
119
        }
120
121
        $objAnswer->save();
122
    }
123
124
    /**
125
     * @param FormValidator $form
126
     * @param Exercise      $exercise
127
     *
128
     * @return void
129
     */
130
    public function processAnswersCreation($form, $exercise)
131
    {
132
    }
133
134
    public function return_header(Exercise $exercise, $counter = null, $score = [])
135
    {
136
        $header = parent::return_header($exercise, $counter, $score);
137
138
        $header .= '<table class="'.$this->question_table_class.'"><thead><tr>';
139
140
        $header .= '<th class="text-center">'.get_lang('Choice').'</th>';
141
142
        if ($exercise->showExpectedChoiceColumn()) {
143
            $header .= '<th class="text-center">'.get_lang('ExpectedChoice').'</th>';
144
        }
145
146
        $header .= '<th>'.get_lang('Answer').'</th>';
147
        if ($exercise->showExpectedChoice()) {
148
            $header .= '<th class="text-center">'.get_lang('Status').'</th>';
149
        }
150
151
        $header .= '</tr></thead>';
152
153
        return $header;
154
    }
155
}
156