Passed
Pull Request — 1.11.x (#4360)
by Angel Fernando Quiroz
08:44
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.png';
8
    public $explanationLangVar = 'MultipleSelectDropdown';
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('Enter a list of answers (one answer by line)')],
28
            ['rows' => 8]
29
        );
30
        $form->addFile(
31
            'list_file',
32
            ['', get_lang('Or select a CSV file with a list of answers')],
33
            ['accept' => 'text/csv']
34
        );
35
36
        $buttonGroup = [];
37
38
        if ($objExe->edit_exercise_in_lp == true ||
39
            (empty($this->exerciseList) && empty($obj_ex->iid))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $obj_ex seems to be never defined.
Loading history...
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
56
    public function createAnswersForm($form)
57
    {
58
    }
59
60
    public function processCreation($form, $exercise)
61
    {
62
        $listFile = $form->getSubmitValue('list_file');
63
        $listText = $form->getSubmitValue('list_text');
64
65
        parent::processCreation($form, $exercise);
66
67
        $lines = [];
68
69
        if (UPLOAD_ERR_OK === (int) $listFile['error']) {
70
            $csvData = Import::csvToArray($listFile['tmp_name']);
71
            $lines = array_map(
72
                function (array $row) {
73
                    return array_values($row)[0];
74
                },
75
                $csvData
76
            );
77
        } elseif (!empty($listText)) {
78
            $lines = array_map(
79
                function ($line) {
80
                    return trim($line);
81
                },
82
                explode("\n", $listText)
83
            );
84
        }
85
86
        $lines = array_filter($lines);
87
        $courseId = api_get_course_int_id();
88
89
        $i = 1;
90
91
        foreach ($lines as $line) {
92
            Question::saveQuestionOption($this->iid, $line, $courseId, $i++);
93
        }
94
    }
95
96
    /**
97
     * @param FormValidator $form
98
     * @param Exercise      $exercise
99
     *
100
     * @return void
101
     */
102
    public function processAnswersCreation($form, $exercise)
103
    {
104
    }
105
106
    public function return_header(Exercise $exercise, $counter = null, $score = [])
107
    {
108
        $header = parent::return_header($exercise, $counter, $score);
109
110
        $header .= '<table class="'.$this->question_table_class.'"><thead><tr>';
111
112
        $header .= '<th class="text-center">'.get_lang('Choice').'</th>';
113
114
        if ($exercise->showExpectedChoiceColumn()) {
115
            $header .= '<th class="text-center">'.get_lang('ExpectedChoice').'</th>';
116
        }
117
118
        $header .= '<th>'.get_lang('Answer').'</th>';
119
        if ($exercise->showExpectedChoice()) {
120
            $header .= '<th class="text-center">'.get_lang('Status').'</th>';
121
        }
122
123
        $header .= '</tr></thead>';
124
125
        return $header;
126
    }
127
}
128