Passed
Pull Request — 1.11.x (#4360)
by Angel Fernando Quiroz
08:20 queued 16s
created

MultipleAnswerDropdown::return_header()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 20
rs 9.9332
cc 3
nop 3
nc 4
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
class MultipleAnswerDropdown extends Question
6
{
7
    public $typePicture = 'mcma_dropdown.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)) {
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
            $lines = Import::csvColumnToArray($listFile['tmp_name']);
83
        } elseif (!empty($listText)) {
84
            $lines = explode("\n", $listText);
85
        }
86
87
        $lines = array_map('trim', $lines);
88
        $lines = array_filter($lines);
89
90
        $objAnswer = new Answer($this->iid);
91
92
        $i = 1;
93
94
        foreach ($lines as $line) {
95
            $isCorrect = 0;
96
97
            if (isset($objAnswer->correct[$i])) {
98
                $isCorrect = (int) $objAnswer->correct[$i];
99
            }
100
101
            $objAnswer->createAnswer($line, $isCorrect, '', 0, $i++);
102
        }
103
104
        $objAnswer->save();
105
    }
106
107
    /**
108
     * @param FormValidator $form
109
     * @param Exercise      $exercise
110
     *
111
     * @return void
112
     */
113
    public function processAnswersCreation($form, $exercise)
114
    {
115
    }
116
117
    public function return_header(Exercise $exercise, $counter = null, $score = [])
118
    {
119
        $header = parent::return_header($exercise, $counter, $score);
120
121
        $header .= '<table class="'.$this->question_table_class.'"><thead><tr>';
122
123
        $header .= '<th class="text-center">'.get_lang('Choice').'</th>';
124
125
        if ($exercise->showExpectedChoiceColumn()) {
126
            $header .= '<th class="text-center">'.get_lang('ExpectedChoice').'</th>';
127
        }
128
129
        $header .= '<th>'.get_lang('Answer').'</th>';
130
        if ($exercise->showExpectedChoice()) {
131
            $header .= '<th class="text-center">'.get_lang('Status').'</th>';
132
        }
133
134
        $header .= '</tr></thead>';
135
136
        return $header;
137
    }
138
}
139