Passed
Push — 1.11.x ( 1065dc...86bcc1 )
by Julito
12:14
created

UniqueAnswer::__construct()   A

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 Chamilo\CourseBundle\Entity\CQuizAnswer;
6
use ChamiloSession as Session;
7
8
/**
9
 * Class UniqueAnswer.
10
 *
11
 * This class allows to instantiate an object of type UNIQUE_ANSWER
12
 * (MULTIPLE CHOICE, UNIQUE ANSWER),
13
 * extending the class question
14
 *
15
 * @author Eric Marguin
16
 * @author Julio Montoya
17
 */
18
class UniqueAnswer extends Question
19
{
20
    public $typePicture = 'mcua.png';
21
    public $explanationLangVar = 'UniqueSelect';
22
23
    /**
24
     * Constructor.
25
     */
26
    public function __construct()
27
    {
28
        parent::__construct();
29
        $this->type = UNIQUE_ANSWER;
30
        $this->isContent = $this->getIsContent();
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function createAnswersForm($form)
37
    {
38
        // Getting the exercise list
39
        /** @var Exercise $obj_ex */
40
        $obj_ex = Session::read('objExercise');
41
42
        $editor_config = [
43
            'ToolbarSet' => 'TestProposedAnswer',
44
            'Width' => '100%',
45
            'Height' => '125',
46
        ];
47
48
        //this line defines how many questions by default appear when creating a choice question
49
        // The previous default value was 2. See task #1759.
50
        $nb_answers = isset($_POST['nb_answers']) ? (int) $_POST['nb_answers'] : 4;
51
        $nb_answers += (isset($_POST['lessAnswers']) ? -1 : (isset($_POST['moreAnswers']) ? 1 : 0));
52
53
        $feedback_title = '';
54
        switch ($obj_ex->getFeedbackType()) {
55
            case EXERCISE_FEEDBACK_TYPE_DIRECT:
56
                // Scenario
57
                $comment_title = '<th width="20%">'.get_lang('Comment').'</th>';
58
                $feedback_title = '<th width="20%">'.get_lang('Scenario').'</th>';
59
                break;
60
            case EXERCISE_FEEDBACK_TYPE_POPUP:
61
                $comment_title = '<th width="20%">'.get_lang('Comment').'</th>';
62
                break;
63
            default:
64
                $comment_title = '<th width="40%">'.get_lang('Comment').'</th>';
65
                break;
66
        }
67
68
        $html = '<table class="table table-striped table-hover">
69
            <thead>
70
                <tr style="text-align: center;">
71
                    <th width="5%">'.get_lang('Number').'</th>
72
                    <th width="5%"> '.get_lang('True').'</th>
73
                    <th width="40%">'.get_lang('Answer').'</th>
74
                        '.$comment_title.'
75
                        '.$feedback_title.'
76
                    <th width="10%">'.get_lang('Weighting').'</th>
77
                </tr>
78
            </thead>
79
            <tbody>';
80
81
        $form->addHeader(get_lang('Answers'));
82
        $form->addHtml($html);
83
84
        $defaults = [];
85
86
        $correct = 0;
87
        if (!empty($this->id)) {
88
            $answer = new Answer($this->id);
89
            $answer->read();
90
            if ($answer->nbrAnswers > 0 && !$form->isSubmitted()) {
91
                $nb_answers = $answer->nbrAnswers;
92
            }
93
        }
94
        $form->addElement('hidden', 'nb_answers');
95
96
        $obj_ex->setQuestionList(true);
97
        $question_list = $obj_ex->getQuestionList();
98
        $select_question = [];
99
        $select_question[0] = get_lang('SelectTargetQuestion');
100
        if (is_array($question_list)) {
101
            foreach ($question_list as $key => $questionid) {
102
                //To avoid warning messages
103
                if (!is_numeric($questionid)) {
104
                    continue;
105
                }
106
                $question = Question::read($questionid);
107
                $questionTitle = strip_tags($question->selectTitle());
108
                $select_question[$questionid] = "Q$key: $questionTitle";
109
            }
110
        }
111
        $select_question[-1] = get_lang('ExitTest');
112
113
        $list = new LearnpathList(api_get_user_id());
114
        $flat_list = $list->get_flat_list();
115
        $select_lp_id = [];
116
        $select_lp_id[0] = get_lang('SelectTargetLP');
117
118
        foreach ($flat_list as $id => $details) {
119
            $select_lp_id[$id] = cut($details['lp_name'], 20);
120
        }
121
122
        $temp_scenario = [];
123
        if ($nb_answers < 1) {
124
            $nb_answers = 1;
125
            echo Display::return_message(
126
                get_lang('YouHaveToCreateAtLeastOneAnswer')
127
            );
128
        }
129
130
        for ($i = 1; $i <= $nb_answers; $i++) {
131
            $form->addHtml('<tr>');
132
            if (isset($answer) && is_object($answer)) {
133
                if (isset($answer->correct[$i]) && $answer->correct[$i]) {
134
                    $correct = $i;
135
                }
136
                $defaults['answer['.$i.']'] = isset($answer->answer[$i]) ? $answer->answer[$i] : '';
137
                $defaults['comment['.$i.']'] = isset($answer->comment[$i]) ? $answer->comment[$i] : '';
138
                $defaults['weighting['.$i.']'] = isset($answer->weighting[$i]) ? float_format($answer->weighting[$i], 1) : 0;
139
                $item_list = [];
140
                if (isset($answer->destination[$i])) {
141
                    $item_list = explode('@@', $answer->destination[$i]);
142
                }
143
                $try = isset($item_list[0]) ? $item_list[0] : '';
144
                $lp = isset($item_list[1]) ? $item_list[1] : '';
145
                $list_dest = isset($item_list[2]) ? $item_list[2] : '';
146
                $url = isset($item_list[3]) ? $item_list[3] : '';
147
148
                if ($try == 0) {
149
                    $try_result = 0;
150
                } else {
151
                    $try_result = 1;
152
                }
153
                if ($url == 0) {
154
                    $url_result = '';
155
                } else {
156
                    $url_result = $url;
157
                }
158
159
                $temp_scenario['url'.$i] = $url_result;
160
                $temp_scenario['try'.$i] = $try_result;
161
                $temp_scenario['lp'.$i] = $lp;
162
                $temp_scenario['destination'.$i] = $list_dest;
163
            } else {
164
                $defaults['answer[1]'] = get_lang('DefaultUniqueAnswer1');
165
                $defaults['weighting[1]'] = 10;
166
                $defaults['answer[2]'] = get_lang('DefaultUniqueAnswer2');
167
                $defaults['weighting[2]'] = 0;
168
                $temp_scenario['destination'.$i] = ['0'];
169
                $temp_scenario['lp'.$i] = ['0'];
170
            }
171
            $defaults['scenario'] = $temp_scenario;
172
173
            $renderer = $form->defaultRenderer();
174
175
            $renderer->setElementTemplate(
176
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
177
                'correct'
178
            );
179
            $renderer->setElementTemplate(
180
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
181
                'counter['.$i.']'
182
            );
183
            $renderer->setElementTemplate(
184
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
185
                'answer['.$i.']'
186
            );
187
            $renderer->setElementTemplate(
188
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
189
                'comment['.$i.']'
190
            );
191
            $renderer->setElementTemplate(
192
                '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}</td>',
193
                'weighting['.$i.']'
194
            );
195
196
            $answer_number = $form->addElement(
197
                'text',
198
                'counter['.$i.']',
199
                null,
200
                ' value = "'.$i.'"'
201
            );
202
            $answer_number->freeze();
203
            $form->addElement(
204
                'radio',
205
                'correct',
206
                null,
207
                null,
208
                $i,
209
                'class="checkbox"'
210
            );
211
212
            $form->addHtmlEditor('answer['.$i.']', null, null, false, $editor_config);
213
214
            $form->addRule(
215
                'answer['.$i.']',
216
                get_lang('ThisFieldIsRequired'),
217
                'required'
218
            );
219
220
            switch ($obj_ex->getFeedbackType()) {
221
                case EXERCISE_FEEDBACK_TYPE_DIRECT:
222
                    $this->setDirectOptions($i, $form, $renderer, $select_lp_id, $select_question);
223
                    break;
224
                case EXERCISE_FEEDBACK_TYPE_POPUP:
225
                default:
226
                    $form->addHtmlEditor('comment['.$i.']', null, null, false, $editor_config);
227
                    break;
228
            }
229
            $form->addText('weighting['.$i.']', null, null, ['value' => '0']);
230
            $form->addHtml('</tr>');
231
        }
232
233
        $form->addHtml('</tbody>');
234
        $form->addHtml('</table>');
235
236
        global $text;
237
        $buttonGroup = [];
238
239
        if ($obj_ex->edit_exercise_in_lp == true ||
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
240
            (empty($this->exerciseList) && empty($obj_ex->id))
241
        ) {
242
            //setting the save button here and not in the question class.php
243
            $buttonGroup[] = $form->addButtonDelete(get_lang('LessAnswer'), 'lessAnswers', true);
244
            $buttonGroup[] = $form->addButtonCreate(get_lang('PlusAnswer'), 'moreAnswers', true);
245
            $buttonGroup[] = $form->addButton(
246
                'submitQuestion',
247
                $text,
248
                'check',
249
                'primary',
250
                'default',
251
                null,
252
                ['id' => 'submit-question'],
253
                true
254
            );
255
            $form->addGroup($buttonGroup);
256
        }
257
258
        // We check the first radio button to be sure a radio button will be check
259
        if ($correct == 0) {
260
            $correct = 1;
261
        }
262
263
        if (isset($_POST) && isset($_POST['correct'])) {
264
            $correct = (int) $_POST['correct'];
265
        }
266
267
        $defaults['correct'] = $correct;
268
269
        if (!empty($this->id)) {
270
            $form->setDefaults($defaults);
271
        } else {
272
            if ($this->isContent == 1) {
273
                // Default sample content.
274
                $form->setDefaults($defaults);
275
            } else {
276
                $correct = 1;
277
                if (isset($_POST) && isset($_POST['correct'])) {
278
                    $correct = (int) $_POST['correct'];
279
                }
280
281
                $form->setDefaults(['correct' => $correct]);
282
            }
283
        }
284
        $form->setConstants(['nb_answers' => $nb_answers]);
285
    }
286
287
    public function setDirectOptions($i, FormValidator $form, $renderer, $select_lp_id, $select_question)
288
    {
289
        $editor_config = [
290
            'ToolbarSet' => 'TestProposedAnswer',
291
            'Width' => '100%',
292
            'Height' => '125',
293
        ];
294
295
        $form->addHtmlEditor(
296
            'comment['.$i.']',
297
            null,
298
            null,
299
            false,
300
            $editor_config
301
        );
302
        // Direct feedback
303
        //Adding extra feedback fields
304
        $group = [];
305
        $group['try'.$i] = $form->createElement(
306
            'checkbox',
307
            'try'.$i,
308
            null,
309
            get_lang('TryAgain')
310
        );
311
        $group['lp'.$i] = $form->createElement(
312
            'select',
313
            'lp'.$i,
314
            get_lang('SeeTheory').': ',
315
            $select_lp_id
316
        );
317
        $group['destination'.$i] = $form->createElement(
318
            'select',
319
            'destination'.$i,
320
            get_lang('GoToQuestion').': ',
321
            $select_question
322
        );
323
        $group['url'.$i] = $form->createElement(
324
            'text',
325
            'url'.$i,
326
            get_lang('Other').': ',
327
            [
328
                'class' => 'col-md-2',
329
                'placeholder' => get_lang('Other'),
330
            ]
331
        );
332
        $form->addGroup($group, 'scenario');
333
334
        $renderer->setElementTemplate(
335
            '<td><!-- BEGIN error --><span class="form_error">{error}</span><!-- END error --><br/>{element}',
336
            'scenario'
337
        );
338
    }
339
340
    /**
341
     * {@inheritdoc}
342
     */
343
    public function processAnswersCreation($form, $exercise)
344
    {
345
        $questionWeighting = $nbrGoodAnswers = 0;
346
        $correct = $form->getSubmitValue('correct');
347
        $objAnswer = new Answer($this->id);
348
        $nb_answers = $form->getSubmitValue('nb_answers');
349
350
        for ($i = 1; $i <= $nb_answers; $i++) {
351
            $answer = trim($form->getSubmitValue('answer['.$i.']'));
352
            $comment = trim($form->getSubmitValue('comment['.$i.']'));
353
            $weighting = trim($form->getSubmitValue('weighting['.$i.']'));
354
            $scenario = $form->getSubmitValue('scenario');
355
356
            $try = null;
357
            $lp = null;
358
            $destination = null;
359
            $url = null;
360
            if (isset($scenario['try'.$i])) {
361
                $try = !empty($scenario['try'.$i]);
362
            }
363
364
            if (isset($scenario['lp'.$i])) {
365
                $lp = $scenario['lp'.$i];
366
            }
367
368
            if (isset($scenario['destination'.$i])) {
369
                $destination = $scenario['destination'.$i];
370
            }
371
372
            if (isset($scenario['url'.$i])) {
373
                $url = trim($scenario['url'.$i]);
374
            }
375
376
            /*
377
            How we are going to parse the destination value
378
379
           here we parse the destination value which is a string
380
            1@@3@@2;4;4;@@http://www.chamilo.org
381
382
            where: try_again@@lp_id@@selected_questions@@url
383
384
           try_again = is 1 || 0
385
           lp_id = id of a learning path (0 if dont select)
386
           selected_questions= ids of questions
387
           url= an url
388
389
            $destination_str='';
390
            foreach ($list_destination as $destination_id)
391
            {
392
                $destination_str.=$destination_id.';';
393
            }*/
394
395
            $goodAnswer = $correct == $i ? true : false;
396
397
            if ($goodAnswer) {
398
                $nbrGoodAnswers++;
399
                $weighting = abs($weighting);
400
                if ($weighting > 0) {
401
                    $questionWeighting += $weighting;
402
                }
403
            }
404
405
            if (empty($try)) {
406
                $try = 0;
407
            }
408
409
            if (empty($lp)) {
410
                $lp = 0;
411
            }
412
413
            if (empty($destination)) {
414
                $destination = 0;
415
            }
416
417
            if ($url == '') {
418
                $url = 0;
419
            }
420
421
            //1@@1;2;@@2;4;4;@@http://www.chamilo.org
422
            $dest = $try.'@@'.$lp.'@@'.$destination.'@@'.$url;
423
            $objAnswer->createAnswer(
424
                $answer,
425
                $goodAnswer,
426
                $comment,
427
                $weighting,
0 ignored issues
show
Bug introduced by
It seems like $weighting can also be of type double and string; however, parameter $weighting of Answer::createAnswer() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

427
                /** @scrutinizer ignore-type */ $weighting,
Loading history...
428
                $i,
429
                null,
430
                null,
431
                $dest
432
            );
433
        }
434
435
        // saves the answers into the data base
436
        $objAnswer->save();
437
438
        // sets the total weighting of the question
439
        $this->updateWeighting($questionWeighting);
0 ignored issues
show
Bug introduced by
It seems like $questionWeighting can also be of type double; however, parameter $weighting of Question::updateWeighting() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

439
        $this->updateWeighting(/** @scrutinizer ignore-type */ $questionWeighting);
Loading history...
440
        $this->save($exercise);
441
    }
442
443
    /**
444
     * {@inheritdoc}
445
     */
446
    public function return_header(Exercise $exercise, $counter = null, $score = [])
447
    {
448
        $header = parent::return_header($exercise, $counter, $score);
449
        $header .= '<table class="'.$this->question_table_class.'"><tr>';
450
451
        $header .= '<th>'.get_lang('Choice').'</th>';
452
        if ($exercise->showExpectedChoiceColumn()) {
453
            $header .= '<th>'.get_lang('ExpectedChoice').'</th>';
454
        }
455
456
        $header .= '<th>'.get_lang('Answer').'</th>';
457
        if ($exercise->showExpectedChoice()) {
458
            $header .= '<th>'.get_lang('Status').'</th>';
459
        }
460
        $header .= '<th>'.get_lang('Comment').'</th>';
461
        $header .= '</tr>';
462
463
        return $header;
464
    }
465
466
    /**
467
     * Saves one answer to the database.
468
     *
469
     * @param int    $id          The ID of the answer (has to be calculated for this course)
470
     * @param int    $question_id The question ID (to which the answer is attached)
471
     * @param string $title       The text of the answer
472
     * @param string $comment     The feedback for the answer
473
     * @param float  $score       The score you get when picking this answer
474
     * @param int    $correct     Whether this answer is considered *the* correct one (this is the unique answer type)
475
     */
476
    public function addAnswer(
477
        $id,
478
        $question_id,
479
        $title,
480
        $comment,
481
        $score = 0.0,
482
        $correct = 0
483
    ) {
484
        $em = Database::getManager();
485
        $tbl_quiz_answer = Database::get_course_table(TABLE_QUIZ_ANSWER);
486
        $tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
487
        $course_id = api_get_course_int_id();
488
        $question_id = intval($question_id);
489
        $score = floatval($score);
490
        $correct = intval($correct);
491
        $title = Database::escape_string($title);
492
        $comment = Database::escape_string($comment);
493
        // Get the max position.
494
        $sql = "SELECT max(position) as max_position
495
                FROM $tbl_quiz_answer
496
                WHERE
497
                    c_id = $course_id AND
498
                    question_id = $question_id";
499
        $rs_max = Database::query($sql);
500
        $row_max = Database::fetch_object($rs_max);
501
        $position = $row_max->max_position + 1;
502
503
        // Insert a new answer
504
        $quizAnswer = new CQuizAnswer();
505
        $quizAnswer
506
            ->setCId($course_id)
507
            ->setId($id)
508
            ->setQuestionId($question_id)
509
            ->setAnswer($title)
510
            ->setCorrect($correct)
511
            ->setComment($comment)
512
            ->setPonderation($score)
513
            ->setPosition($position)
514
            ->setDestination('0@@0@@0@@0');
515
516
        $em->persist($quizAnswer);
517
        $em->flush();
518
519
        $id = $quizAnswer->getIid();
520
521
        if ($id) {
522
            $quizAnswer
523
                ->setId($id);
524
525
            $em->merge($quizAnswer);
526
            $em->flush();
527
        }
528
529
        if ($correct) {
530
            $sql = "UPDATE $tbl_quiz_question
531
                    SET ponderation = (ponderation + $score)
532
                    WHERE c_id = $course_id AND id = ".$question_id;
533
            Database::query($sql);
534
        }
535
    }
536
}
537