UniqueAnswerNoOption::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use ChamiloSession as Session;
6
7
/**
8
 * Class UniqueAnswerNoOption
9
 * Allows to instantiate an object of type UNIQUE_ANSWER (MULTIPLE CHOICE, UNIQUE ANSWER),
10
 * extending the class question.
11
 *
12
 * @author Eric Marguin
13
 * @author Julio Montoya
14
 */
15
class UniqueAnswerNoOption extends Question
16
{
17
    public $typePicture = 'mcuao.png';
18
    public $explanationLangVar = 'Unique answer with unknown';
19
20
    public function __construct()
21
    {
22
        parent::__construct();
23
        $this->type = UNIQUE_ANSWER_NO_OPTION;
24
        $this->isContent = $this->getIsContent();
25
    }
26
27
    public function createAnswersForm($form)
28
    {
29
        // getting the exercise list
30
        $obj_ex = Session::read('objExercise');
31
32
        $editor_config = [
33
            'ToolbarSet' => 'TestProposedAnswer',
34
            'Width' => '100%',
35
            'Height' => '125',
36
        ];
37
        // This line define how many question by default appear when creating a choice question
38
        // The previous default value was 2. See task #1759.
39
        $nb_answers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 3;
40
        $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
41
42
        /*
43
          Types of Feedback
44
          $feedback_option[0]=get_lang('Feedback');
45
          $feedback_option[1]=get_lang('Adaptative test with immediate feedback');
46
          $feedback_option[2]=get_lang('Exam (no feedback)');
47
         */
48
49
        $feedback_title = '';
50
        if (1 == $obj_ex->getFeedbackType()) {
51
            $editor_config['Width'] = '250';
52
            $editor_config['Height'] = '110';
53
            $comment_title = '<th width="50%" >'.get_lang('Comment').'</th>';
54
            $feedback_title = '<th width="50%" >'.get_lang('Scenario').'</th>';
55
        } else {
56
            $comment_title = '<th width="50%">'.get_lang('Comment').'</th>';
57
        }
58
59
        $html = '<table class="table table-striped table-hover">';
60
        $html .= '<thead>';
61
        $html .= '<tr>';
62
        $html .= '<th>'.get_lang('N°').'</th>';
63
        $html .= '<th>'.get_lang('True').'</th>';
64
        $html .= '<th width="50%">'.get_lang('Answer').'</th>';
65
        $html .= $comment_title.$feedback_title;
66
        $html .= '<th>'.get_lang('Score').'</th>';
67
        $html .= '</tr>';
68
        $html .= '</thead>';
69
        $html .= '<tbody>';
70
71
        $form->addHeader(get_lang('Answers'));
72
        $form->addHtml($html);
73
74
        $defaults = [];
75
        $correct = 0;
76
        $answer = false;
77
        if (!empty($this->id)) {
78
            $answer = new Answer($this->id);
79
            $answer->read();
80
            if ($answer->nbrAnswers > 0 && !$form->isSubmitted()) {
81
                $nb_answers = $answer->nbrAnswers;
82
            }
83
        }
84
85
        $temp_scenario = [];
86
        if ($nb_answers < 1) {
87
            $nb_answers = 1;
88
            echo Display::return_message(get_lang('You have to create at least one answer'));
89
        }
90
        $editQuestion = isset($_GET['editQuestion']) ? $_GET['editQuestion'] : false;
91
        if ($editQuestion) {
92
            //fixing $nb_answers
93
            $new_list = [];
94
            $count = 1;
95
            if (isset($_POST['lessAnswers'])) {
96
                if (!isset($_SESSION['less_answer'])) {
97
                    $_SESSION['less_answer'] = $this->id;
98
                    $nb_answers--;
99
                }
100
            }
101
            for ($k = 1; $k <= $nb_answers; $k++) {
102
                if ('666' != $answer->position[$k]) {
103
                    $new_list[$count] = $count;
104
                    $count++;
105
                }
106
            }
107
        } else {
108
            for ($k = 1; $k <= $nb_answers; $k++) {
109
                $new_list[$k] = $k;
110
            }
111
        }
112
113
        foreach ($new_list as $key) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $new_list does not seem to be defined for all execution paths leading up to this point.
Loading history...
114
            $i = $key;
115
            $form->addHtml('<tr>');
116
            if (is_object($answer)) {
117
                if (666 == $answer->position[$i]) {
118
                    //we set nothing
119
                } else {
120
                    if ($answer->correct[$i]) {
121
                        $correct = $i;
122
                    }
123
                    $answer_result = $answer->answer[$i];
124
                    $weight_result = float_format($answer->weighting[$i], 1);
125
                    if ($nb_answers == $i) {
126
                        $weight_result = '0';
127
                    }
128
129
                    $defaults['answer['.$i.']'] = $answer_result;
130
                    $defaults['comment['.$i.']'] = $answer->comment[$i];
131
                    $defaults['weighting['.$i.']'] = $weight_result;
132
133
                    $item_list = explode('@@', $answer->destination[$i]);
134
135
                    $try = $item_list[0];
136
                    $lp = $item_list[1];
137
                    $list_dest = $item_list[2];
138
                    $url = $item_list[3];
139
140
                    if (0 == $try) {
141
                        $try_result = 0;
142
                    } else {
143
                        $try_result = 1;
144
                    }
145
146
                    if (0 == $url) {
147
                        $url_result = '';
148
                    } else {
149
                        $url_result = $url;
150
                    }
151
152
                    $temp_scenario['url'.$i] = $url_result;
153
                    $temp_scenario['try'.$i] = $try_result;
154
                    $temp_scenario['lp'.$i] = $lp;
155
                    $temp_scenario['destination'.$i] = $list_dest;
156
                }
157
            }
158
159
            $defaults['scenario'] = $temp_scenario;
160
            $renderer = &$form->defaultRenderer();
161
162
            $renderer->setElementTemplate(
163
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
164
                'correct'
165
            );
166
            $renderer->setElementTemplate(
167
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
168
                'counter['.$i.']'
169
            );
170
            $renderer->setElementTemplate(
171
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
172
                'answer['.$i.']'
173
            );
174
            $renderer->setElementTemplate(
175
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
176
                'comment['.$i.']'
177
            );
178
            $renderer->setElementTemplate(
179
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
180
                'weighting['.$i.']'
181
            );
182
183
            $answerNumber = $form->addText('counter['.$i.']', null, false, ['value' => $i]);
184
            $answerNumber->freeze();
185
186
            $form->addElement(
187
                'radio',
188
                'correct',
189
                null,
190
                null,
191
                $i,
192
                ['class' => 'checkbox', 'style' => 'margin-left: 0em;']
193
            );
194
            $form->addHtmlEditor('answer['.$i.']', null, [], $editor_config);
195
            $form->addHtmlEditor('comment['.$i.']', null, [], $editor_config);
196
            $form->addText('weighting['.$i.']', null, false, ['style' => 'width: 60px;', 'value' => '0']);
197
            $form->addHtml('</tr>');
198
            $i++;
199
        }
200
201
        if (empty($this->id)) {
202
            $form->addHidden('new_question', 1);
203
        }
204
205
        //Adding the "I don't know" question answer
206
        //if (empty($this -> id)) {
207
        $i = 666;
208
        $form->addHtml('<tr>');
209
210
        $defaults["counter[$i]"] = '-';
211
        $defaults['answer['.$i.']'] = get_lang('Don\'t know');
212
        $defaults['weighting['.$i.']'] = '0';
213
        $defaults['scenario'] = $temp_scenario;
214
        $renderer = &$form->defaultRenderer();
215
216
        $renderer->setElementTemplate(
217
            '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
218
            'correct'
219
        );
220
        $renderer->setElementTemplate(
221
            '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
222
            'counter['.$i.']'
223
        );
224
        $renderer->setElementTemplate(
225
            '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
226
            'answer['.$i.']'
227
        );
228
        $renderer->setElementTemplate(
229
            '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
230
            'comment['.$i.']'
231
        );
232
        $renderer->setElementTemplate(
233
            '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
234
            'weighting['.$i.']'
235
        );
236
237
        $form
238
            ->addElement('text', 'counter['.$i.']', null)
239
            ->freeze();
240
241
        $form->addHidden('position['.$i.']', '666');
242
        $form->addElement('radio', 'correct', null, null, $i, ['class' => 'checkbox', 'disabled' => true]);
243
        $form->addHtmlEditor('answer['.$i.']', null, true, [], $editor_config);
244
245
        $form->addRule('answer['.$i.']', get_lang('Required field'), 'required');
246
        $form->applyFilter("answer[$i]", 'attr_on_filter');
247
        $form->addHtmlEditor('comment['.$i.']', null, true, [], $editor_config);
248
        $form->addElement('text', "weighting[$i]", null)->freeze();
249
250
        $form->addHTml('</tr>');
251
        $form->addHtml('</tbody></table>');
252
253
        $buttonGroup = [];
254
255
        global $text;
256
        //ie6 fix
257
        if (true == $obj_ex->edit_exercise_in_lp ||
258
            (empty($this->exerciseList) && empty($obj_ex->id))
259
        ) {
260
            //setting the save button here and not in the question class.php
261
            $buttonGroup[] = $form->addButtonDelete(get_lang('Remove answer option'), 'lessAnswers', true);
262
            $buttonGroup[] = $form->addButtonCreate(get_lang('Add answer option'), 'moreAnswers', true);
263
            $buttonGroup[] = $form->addButtonSave($text, 'submitQuestion', true);
264
265
            $form->addGroup($buttonGroup);
266
        }
267
268
        //We check the first radio button to be sure a radio button will be check
269
        if (0 == $correct) {
270
            $correct = 1;
271
        }
272
        $defaults['correct'] = $correct;
273
274
        if (!empty($this->id)) {
275
            $form->setDefaults($defaults);
276
        } else {
277
            $form->setDefaults($defaults);
278
        }
279
280
        $form->addElement('hidden', 'nb_answers');
281
        $form->setConstants(['nb_answers' => $nb_answers]);
282
    }
283
284
    public function processAnswersCreation($form, $exercise)
285
    {
286
        $questionWeighting = $nbrGoodAnswers = 0;
287
        $correct = $form->getSubmitValue('correct');
288
        $objAnswer = new Answer($this->id);
289
        $nb_answers = $form->getSubmitValue('nb_answers');
290
        $minus = 1;
291
        if ($form->getSubmitValue('new_question')) {
292
            $minus = 0;
293
        }
294
295
        for ($i = 1; $i <= $nb_answers - $minus; $i++) {
296
            $position = trim($form->getSubmitValue('position['.$i.']'));
297
            $answer = trim($form->getSubmitValue('answer['.$i.']'));
298
            $comment = trim($form->getSubmitValue('comment['.$i.']'));
299
            $weighting = trim($form->getSubmitValue('weighting['.$i.']'));
300
            $scenario = $form->getSubmitValue('scenario');
301
            $try = $scenario['try'.$i] ?? null;
302
            $lp = $scenario['lp'.$i] ?? null;
303
            $destination = $scenario['destination'.$i] ?? null;
304
            $url = trim($scenario['url'.$i] ?? null);
305
306
            /*
307
            How we are going to parse the destination value
308
309
            here we parse the destination value which is a string
310
            1@@3@@2;4;4;@@http://www.chamilo.org
311
312
            where: try_again@@lp_id@@selected_questions@@url
313
314
            try_again = is 1 || 0
315
            lp_id = id of a learning path (0 if dont select)
316
            selected_questions= ids of questions
317
            url= an url
318
            */
319
            /*
320
            $destination_str='';
321
            foreach ($list_destination as $destination_id)
322
            {
323
                $destination_str.=$destination_id.';';
324
            }*/
325
            $goodAnswer = $correct == $i ? true : false;
326
327
            if ($goodAnswer) {
328
                $nbrGoodAnswers++;
329
                $weighting = abs($weighting);
330
                if ($weighting > 0) {
331
                    $questionWeighting += $weighting;
332
                }
333
            }
334
335
            if (empty($try)) {
336
                $try = 0;
337
            }
338
339
            if (empty($lp)) {
340
                $lp = 0;
341
            }
342
343
            if (empty($destination)) {
344
                $destination = 0;
345
            }
346
347
            if ('' == $url) {
348
                $url = 0;
349
            }
350
351
            //1@@1;2;@@2;4;4;@@http://www.chamilo.org
352
            $dest = $try.'@@'.$lp.'@@'.$destination.'@@'.$url;
353
            $objAnswer->createAnswer(
354
                $answer,
355
                $goodAnswer,
356
                $comment,
357
                $weighting,
358
                $i,
359
                null,
360
                null,
361
                $dest
362
            );
363
        }
364
365
        //Create 666 answer
366
        $i = 666;
367
        $answer = trim($form->getSubmitValue('answer['.$i.']'));
368
        $comment = trim($form->getSubmitValue('comment['.$i.']'));
369
        $weighting = trim($form->getSubmitValue('weighting['.$i.']'));
370
        $goodAnswer = $correct == $i ? true : false;
371
        $dest = '';
372
373
        $objAnswer->createAnswer(
374
            $answer,
375
            $goodAnswer,
376
            $comment,
377
            $weighting,
378
            $i,
379
            null,
380
            null,
381
            $dest
382
        );
383
384
        // saves the answers into the data base
385
        $objAnswer->save();
386
387
        // sets the total weighting of the question
388
        $this->updateWeighting($questionWeighting);
389
        $this->save($exercise);
390
    }
391
392
    public function return_header(Exercise $exercise, $counter = null, $score = [])
393
    {
394
        $header = parent::return_header($exercise, $counter, $score);
395
        $header .= '<table class="'.$this->questionTableClass.'"><tr>';
396
397
        if (!in_array($exercise->results_disabled, [
398
            RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
399
        ])
400
        ) {
401
            $header .= '<th>'.get_lang('Your choice').'</th>';
402
            if ($exercise->showExpectedChoiceColumn()) {
403
                $header .= '<th>'.get_lang('Expected choice').'</th>';
404
            }
405
        }
406
        $header .= '<th>'.get_lang('Answer').'</th>';
407
        if ($exercise->showExpectedChoice()) {
408
            $header .= '<th class="text-center">'.get_lang('Status').'</th>';
409
        }
410
        if (false === $exercise->hideComment) {
411
            $header .= '<th>'.get_lang('Comment').'</th>';
412
        }
413
        $header .= '</tr>';
414
415
        return $header;
416
    }
417
}
418