MatchingDraggable::createAnswersForm()   F
last analyzed

Complexity

Conditions 25
Paths > 20000

Size

Total Lines 209
Code Lines 132

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 132
c 0
b 0
f 0
dl 0
loc 209
rs 0
cc 25
nc 36288
nop 1

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