MatchingDraggable::createAnswersForm()   F
last analyzed

Complexity

Conditions 21
Paths 4032

Size

Total Lines 191
Code Lines 122

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
eloc 122
nc 4032
nop 1
dl 0
loc 191
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
/**
6
 * MatchingDraggable.
7
 *
8
 * @author Angel Fernando Quiroz Campos <[email protected]>
9
 */
10
class MatchingDraggable extends Question
11
{
12
    public $typePicture = 'matchingdrag.png';
13
    public $explanationLangVar = 'Match by dragging';
14
15
    public function __construct()
16
    {
17
        parent::__construct();
18
        $this->type = MATCHING_DRAGGABLE;
19
        $this->isContent = $this->getIsContent();
20
    }
21
22
    public function createAnswersForm($form)
23
    {
24
        $defaults = [];
25
        $nb_matches = $nb_options = 2;
26
        $matches = [];
27
        $answer = null;
28
        $counter = 1;
29
        if (isset($this->id)) {
30
            $answer = new Answer($this->id);
31
            $answer->read();
32
33
            if ($answer->nbrAnswers > 0) {
34
                for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
35
                    $correct = $answer->isCorrect($i);
36
                    if (empty($correct)) {
37
                        $matches[$answer->selectAutoId($i)] = chr(64 + $counter);
38
                        $counter++;
39
                    }
40
                }
41
            }
42
        }
43
44
        if ($form->isSubmitted()) {
45
            $nb_matches = $form->getSubmitValue('nb_matches');
46
            $nb_options = $form->getSubmitValue('nb_options');
47
            if (isset($_POST['lessOptions'])) {
48
                $nb_matches--;
49
                $nb_options--;
50
            }
51
            if (isset($_POST['moreOptions'])) {
52
                $nb_matches++;
53
                $nb_options++;
54
            }
55
        } elseif (!empty($this->id)) {
56
            if ($answer->nbrAnswers > 0) {
57
                $nb_matches = $nb_options = 0;
58
                for ($i = 1; $i <= $answer->nbrAnswers; $i++) {
59
                    if ($answer->isCorrect($i)) {
60
                        $nb_matches++;
61
                        $defaults['answer['.$nb_matches.']'] = $answer->selectAnswer($i);
62
                        $defaults['weighting['.$nb_matches.']'] = float_format($answer->selectWeighting($i), 1);
63
                        $defaults['matches['.$nb_matches.']'] = $answer->correct[$i];
64
                    } else {
65
                        $nb_options++;
66
                        $defaults['option['.$nb_options.']'] = $answer->selectAnswer($i);
67
                    }
68
                }
69
            }
70
        } else {
71
            $defaults['answer[1]'] = get_lang('First step');
72
            $defaults['answer[2]'] = get_lang('Second step');
73
            $defaults['matches[2]'] = '2';
74
            $defaults['option[1]'] = get_lang('Note down the address');
75
            $defaults['option[2]'] = get_lang('Contact the emergency services');
76
        }
77
78
        if (empty($matches)) {
79
            for ($i = 1; $i <= $nb_options; $i++) {
80
                // fill the array with A, B, C.....
81
                $matches[$i] = chr(64 + $i);
82
            }
83
        } else {
84
            for ($i = $counter; $i <= $nb_options; $i++) {
85
                // fill the array with A, B, C.....
86
                $matches[$i] = chr(64 + $i);
87
            }
88
        }
89
90
        $form->addElement('hidden', 'nb_matches', $nb_matches);
91
        $form->addElement('hidden', 'nb_options', $nb_options);
92
93
        // DISPLAY MATCHES
94
        $html = '<table class="table table-striped table-hover">
95
            <thead>
96
                <tr>
97
                    <th width="10">'.get_lang('N°').'</th>
98
                    <th width="85%">'.get_lang('Answer').'</th>
99
                    <th width="15%">'.get_lang('Matches To').'</th>
100
                    <th width="10">'.get_lang('Score').'</th>
101
                </tr>
102
            </thead>
103
            <tbody>';
104
105
        $form->addHeader(get_lang('Match them'));
106
        $form->addHtml($html);
107
108
        if ($nb_matches < 1) {
109
            $nb_matches = 1;
110
            echo Display::return_message(get_lang('You have to create at least one answer'), 'normal');
111
        }
112
113
        $editorConfig = [
114
            'ToolbarSet' => 'TestMatching',
115
            'Width' => '100%',
116
            'Height' => '125',
117
        ];
118
119
        for ($i = 1; $i <= $nb_matches; $i++) {
120
            $renderer = &$form->defaultRenderer();
121
            $renderer->setElementTemplate(
122
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
123
                "answer[$i]"
124
            );
125
126
            $renderer->setElementTemplate(
127
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
128
                "matches[$i]"
129
            );
130
131
            $renderer->setElementTemplate(
132
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
133
                "weighting[$i]"
134
            );
135
136
            $form->addHtml('<tr>');
137
            $form->addHtml("<td>$i</td>");
138
139
            //$form->addText("answer[$i]", null);
140
            $form->addHtmlEditor(
141
                "answer[$i]",
142
                null,
143
                null,
144
                false,
145
                $editorConfig
146
            );
147
148
            $form->addSelect("matches[$i]", null, $matches);
149
            $form->addText("weighting[$i]", null, true, ['style' => 'width: 60px;', 'value' => 10]);
150
            $form->addHtml('</tr>');
151
        }
152
153
        $form->addHtml('</tbody></table>');
154
155
        // DISPLAY OPTIONS
156
        $html = '<table class="table table-striped table-hover">
157
            <thead>
158
                <tr>
159
                    <th width="15%">'.get_lang('N°').'</th>
160
                    <th width="85%">'.get_lang('Answer').'</th>
161
                </tr>
162
            </thead>
163
            <tbody>';
164
165
        $form->addHtml($html);
166
167
        if ($nb_options < 1) {
168
            $nb_options = 1;
169
            echo Display::return_message(get_lang('You have to create at least one answer'), 'normal');
170
        }
171
172
        for ($i = 1; $i <= $nb_options; $i++) {
173
            $renderer = &$form->defaultRenderer();
174
175
            $renderer->setElementTemplate(
176
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error -->{element}</td>',
177
                "option[$i]"
178
            );
179
180
            $form->addHtml('<tr>');
181
            $form->addHtml('<td>'.chr(64 + $i).'</td>');
182
            $form->addHtmlEditor(
183
                "option[$i]",
184
                null,
185
                null,
186
                false,
187
                $editorConfig
188
            );
189
            $form->addHtml('</tr>');
190
        }
191
192
        $form->addHtml('</table>');
193
        global $text;
194
        $group = [];
195
        // setting the save button here and not in the question class.php
196
        $group[] = $form->addButtonDelete(get_lang('Remove element'), 'lessOptions', true);
197
        $group[] = $form->addButtonCreate(get_lang('Add element'), 'moreOptions', true);
198
        $group[] = $form->addButtonSave($text, 'submitQuestion', true);
199
        $form->addGroup($group);
200
201
        if (!empty($this->id)) {
202
            $form->setDefaults($defaults);
203
        } else {
204
            if (1 == $this->isContent) {
205
                $form->setDefaults($defaults);
206
            }
207
        }
208
209
        $form->setConstants(
210
            [
211
                'nb_matches' => $nb_matches,
212
                'nb_options' => $nb_options,
213
            ]
214
        );
215
    }
216
217
    public function processAnswersCreation($form, $exercise)
218
    {
219
        $nb_matches = $form->getSubmitValue('nb_matches');
220
        $nb_options = $form->getSubmitValue('nb_options');
221
        $this->weighting = 0;
222
        $position = 0;
223
        $objAnswer = new Answer($this->id);
224
225
        // Insert the options
226
        for ($i = 1; $i <= $nb_options; $i++) {
227
            $position++;
228
            $option = $form->getSubmitValue("option[$i]");
229
            $objAnswer->createAnswer($option, 0, '', 0, $position);
230
        }
231
232
        // Insert the answers
233
        for ($i = 1; $i <= $nb_matches; $i++) {
234
            $position++;
235
            $answer = $form->getSubmitValue("answer[$i]");
236
            $matches = $form->getSubmitValue("matches[$i]");
237
            $weighting = $form->getSubmitValue("weighting[$i]");
238
            $this->weighting += $weighting;
239
240
            $objAnswer->createAnswer(
241
                $answer,
242
                $matches,
243
                '',
244
                $weighting,
245
                $position
246
            );
247
        }
248
        $objAnswer->save();
249
        $this->save($exercise);
250
    }
251
252
    public function return_header(Exercise $exercise, $counter = null, $score = [])
253
    {
254
        $header = parent::return_header($exercise, $counter, $score);
255
        $header .= '<table class="matching '.$this->questionTableClass.'"><tr>';
256
        $header .= '<th>'.get_lang('Elements list').'</th>';
257
        if (!in_array($exercise->results_disabled, [
258
            RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
259
            //RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
260
        ])
261
        ) {
262
            $header .= '<th>'.get_lang('Your choice').'</th>';
263
            if ($exercise->showExpectedChoiceColumn()) {
264
                $header .= '<th>'.get_lang('Expected choice').'</th>';
265
            }
266
        }
267
268
        if ($exercise->showExpectedChoice()) {
269
            $header .= '<th class="text-center">'.get_lang('Status').'</th>';
270
        }
271
        $header .= '</tr>';
272
273
        return $header;
274
    }
275
}
276