display_multiple_answer_true_false()   F
last analyzed

Complexity

Conditions 26
Paths 5041

Size

Total Lines 119
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 26
eloc 70
nc 5041
nop 12
dl 0
loc 119
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/* See license terms in /license.txt */
3
4
use Chamilo\CoreBundle\Entity\TrackEExercise;
5
use Chamilo\CoreBundle\Enums\StateIcon;
6
use Chamilo\CoreBundle\Framework\Container;
7
8
class ExerciseShowFunctions
9
{
10
    /**
11
     * Shows the answer to a fill-in-the-blanks question, as HTML.
12
     *
13
     * @param Exercise $exercise
14
     * @param int      $feedbackType
15
     * @param string   $answer
16
     * @param int      $id                           Exercise ID
17
     * @param int      $questionId                   Question ID
18
     * @param int      $resultsDisabled
19
     * @param bool     $showTotalScoreAndUserChoices
20
     * @param string   $originalStudentAnswer
21
     */
22
    public static function display_fill_in_blanks_answer(
23
        $exercise,
24
        $feedbackType,
25
        $answer,
26
        $id,
27
        $questionId,
28
        $resultsDisabled,
29
        $showTotalScoreAndUserChoices,
30
        $originalStudentAnswer = ''
31
    ) {
32
        $answerHTML = FillBlanks::getHtmlDisplayForAnswer(
33
            $answer,
34
            $feedbackType,
35
            $resultsDisabled,
36
            $showTotalScoreAndUserChoices
37
        );
38
39
        if (empty($id)) {
40
            echo '<tr><td>';
41
            echo Security::remove_XSS($answerHTML, COURSEMANAGERLOWSECURITY);
42
            echo '</td></tr>';
43
        } else {
44
            echo '<tr><td>';
45
            echo Security::remove_XSS($answerHTML, COURSEMANAGERLOWSECURITY);
46
            echo '</td>';
47
            echo '</tr>';
48
        }
49
    }
50
51
    /**
52
     * Shows the answer to a calculated question, as HTML.
53
     *
54
     *  @param Exercise $exercise
55
     * @param string    Answer text
56
     * @param int       Exercise ID
57
     * @param int       Question ID
58
     */
59
    public static function display_calculated_answer(
60
        $exercise,
61
        $feedback_type,
62
        $answer,
63
        $id,
64
        $questionId,
65
        $resultsDisabled,
66
        $showTotalScoreAndUserChoices,
67
        $expectedChoice = '',
68
        $choice = '',
69
        $status = ''
70
    ) {
71
        if ($exercise->showExpectedChoice()) {
72
            if (empty($id)) {
73
                echo '<tr><td>'.Security::remove_XSS($answer).'</td>';
74
                echo '<td>'.Security::remove_XSS($choice).'</td>';
75
                if ($exercise->showExpectedChoiceColumn()) {
76
                    echo '<td>'.Security::remove_XSS($expectedChoice).'</td>';
77
                }
78
79
                echo '<td>'.Security::remove_XSS($status).'</td>';
80
                echo '</tr>';
81
            } else {
82
                echo '<tr><td>';
83
                echo Security::remove_XSS($answer);
84
                echo '</td><td>';
85
                echo Security::remove_XSS($choice);
86
                echo '</td>';
87
                if ($exercise->showExpectedChoiceColumn()) {
88
                    echo '<td>';
89
                    echo Security::remove_XSS($expectedChoice);
90
                    echo '</td>';
91
                }
92
                echo '<td>';
93
                echo Security::remove_XSS($status);
94
                echo '</td>';
95
                echo '</tr>';
96
            }
97
        } else {
98
            if (empty($id)) {
99
                echo '<tr><td>'.Security::remove_XSS($answer).'</td></tr>';
100
            } else {
101
                echo '<tr><td>';
102
                echo Security::remove_XSS($answer);
103
                echo '</tr>';
104
            }
105
        }
106
    }
107
108
    /**
109
     * Shows the answer to a free-answer question, as HTML.
110
     *
111
     * @param string    Answer text
112
     * @param int       Exercise ID
113
     * @param int       Question ID
114
     */
115
    public static function display_free_answer(
116
        $feedback_type,
117
        $answer,
118
        $exe_id,
119
        $questionId,
120
        $questionScore = null,
121
        $resultsDisabled = 0
122
    ) {
123
        $comments = Event::get_comments($exe_id, $questionId);
0 ignored issues
show
Bug introduced by
The method get_comments() does not exist on Event. ( Ignorable by Annotation )

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

123
        /** @scrutinizer ignore-call */ 
124
        $comments = Event::get_comments($exe_id, $questionId);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
124
125
        if (!empty($answer)) {
126
            echo '<tr><td>';
127
            echo Security::remove_XSS($answer);
128
            echo '</td></tr>';
129
        }
130
131
        if (EXERCISE_FEEDBACK_TYPE_EXAM != $feedback_type) {
132
            if ($questionScore > 0 || !empty($comments)) {
133
            } else {
134
                echo '<tr>';
135
                echo Display::tag('td', ExerciseLib::getNotCorrectedYetText());
136
                echo '</tr>';
137
            }
138
        }
139
    }
140
141
    /**
142
     * @param $feedback_type
143
     * @param $answer
144
     * @param $trackExerciseId
145
     * @param $questionId
146
     * @param int $resultsDisabled
147
     * @param int $questionScore
148
     */
149
    public static function display_oral_expression_answer(
150
        $feedback_type,
151
        $answer,
152
        $trackExerciseId,
153
        $questionId,
154
        $resultsDisabled = 0,
155
        $questionScore = 0,
156
        $showAlertIfNotCorrected = false
157
    ) {
158
        /** @var TrackEExercise $trackExercise */
159
        $trackExercise = Container::getTrackEExerciseRepository()->find($trackExerciseId);
160
161
        if (null === $trackExerciseId) {
162
            return;
163
        }
164
165
        $questionAttempt = $trackExercise->getAttemptByQuestionId($questionId);
166
167
        if (null === $questionAttempt) {
168
            return;
169
        }
170
171
        $assetRepo = Container::getAssetRepository();
172
173
        foreach ($questionAttempt->getAttemptFiles() as $attemptFile) {
174
            echo Display::tag(
175
                'audio',
176
                '',
177
                [
178
                    'src' => $assetRepo->getAssetUrl($attemptFile->getAsset()),
179
                    'controls' => '',
180
                ]
181
            );
182
        }
183
184
        if (!empty($answer)) {
185
            echo Display::tag('p', Security::remove_XSS($answer));
186
        }
187
188
        $comment = Event::get_comments($trackExerciseId, $questionId);
189
        $teacherAudio = ExerciseLib::getOralFeedbackAudio(
190
                        $trackExerciseId,
191
                        $questionId
192
                    );
193
194
        if ($showAlertIfNotCorrected && !$questionScore && EXERCISE_FEEDBACK_TYPE_EXAM != $feedback_type && empty($comment) && empty($teacherAudio)) {
195
            echo Display::tag('p', ExerciseLib::getNotCorrectedYetText());
196
        }
197
    }
198
199
    /**
200
     * Displays the answer to a hotspot question.
201
     *
202
     * @param int    $feedback_type
203
     * @param int    $answerId
204
     * @param string $answer
205
     * @param string $studentChoice
206
     * @param string $answerComment
207
     * @param int    $resultsDisabled
208
     * @param int    $orderColor
209
     * @param bool   $showTotalScoreAndUserChoices
210
     */
211
    public static function display_hotspot_answer(
212
        $exercise,
213
        $feedback_type,
214
        $answerId,
215
        $answer,
216
        $studentChoice,
217
        $answerComment,
218
        $resultsDisabled,
219
        $orderColor,
220
        $showTotalScoreAndUserChoices
221
    ) {
222
        $hide_expected_answer = false;
223
        switch ($resultsDisabled) {
224
            case RESULT_DISABLE_SHOW_SCORE_ONLY:
225
                if (0 == $feedback_type) {
226
                    $hide_expected_answer = true;
227
                }
228
                break;
229
            case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
230
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
231
                $hide_expected_answer = true;
232
                if ($showTotalScoreAndUserChoices) {
233
                    $hide_expected_answer = false;
234
                }
235
                break;
236
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT_NO_FEEDBACK:
237
                $hide_expected_answer = true;
238
                if ($showTotalScoreAndUserChoices) {
239
                    $hide_expected_answer = false;
240
                }
241
                if (false === $showTotalScoreAndUserChoices && empty($studentChoice)) {
242
                    return '';
243
                }
244
                break;
245
        }
246
247
        if (!$hide_expected_answer
248
            && !$studentChoice
249
            && in_array($resultsDisabled, [RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER])
250
        ) {
251
            return;
252
        }
253
254
        $hotspotColors = [
255
            '', // $i starts from 1 on next loop (ugly fix)
256
            '#4271B5',
257
            '#FE8E16',
258
            '#45C7F0',
259
            '#BCD631',
260
            '#D63173',
261
            '#D7D7D7',
262
            '#90AFDD',
263
            '#AF8640',
264
            '#4F9242',
265
            '#F4EB24',
266
            '#ED2024',
267
            '#3B3B3B',
268
            '#F7BDE2',
269
        ];
270
271
        $content = '<tr>';
272
        $content .= '<td class="text-center" width="5%">';
273
        $content .= '<span class="fa fa-square fa-fw fa-2x" aria-hidden="true" style="color:'.
274
            $hotspotColors[$orderColor].'"></span>';
275
        $content .= '</td>';
276
        $content .= '<td class="text-left" width="25%">';
277
        $content .= "$answerId - $answer";
278
        $content .= '</td>';
279
        if (false === $exercise->hideComment) {
280
            $content .= '<td class="text-left" width="10%">';
281
            if (!$hide_expected_answer) {
282
                $status = Display::label(get_lang('Incorrect'), 'danger');
283
                if ($studentChoice) {
284
                    $status = Display::label(get_lang('Correct'), 'success');
285
                }
286
                $content .= $status;
287
            } else {
288
                $content .= '&nbsp;';
289
            }
290
            $content .= '</td>';
291
            if (EXERCISE_FEEDBACK_TYPE_EXAM != $feedback_type) {
292
                $content .= '<td class="text-left" width="60%">';
293
                if ($studentChoice) {
294
                    $content .= '<span style="font-weight: bold; color: #008000;">'.nl2br($answerComment).'</span>';
295
                } else {
296
                    $content .= '&nbsp;';
297
                }
298
                $content .= '</td>';
299
            } else {
300
                $content .= '<td class="text-left" width="60%">&nbsp;</td>';
301
            }
302
        }
303
        $content .= '</tr>';
304
305
        echo $content;
306
    }
307
308
    /**
309
     * Display the answers to a multiple choice question.
310
     *
311
     * @param Exercise $exercise
312
     * @param int      $feedbackType                 Feedback type
313
     * @param int      $answerType                   Answer type
314
     * @param int      $studentChoice                Student choice
315
     * @param string   $answer                       Textual answer
316
     * @param string   $answerComment                Comment on answer
317
     * @param string   $answerCorrect                Correct answer comment
318
     * @param int      $id                           Exercise ID
319
     * @param int      $questionId                   Question ID
320
     * @param bool     $ans                          Whether to show the answer comment or not
321
     * @param bool     $resultsDisabled
322
     * @param bool     $showTotalScoreAndUserChoices
323
     * @param bool     $export
324
     */
325
    public static function display_unique_or_multiple_answer(
326
        $exercise,
327
        $feedbackType,
328
        $answerType,
329
        $studentChoice,
330
        $answer,
331
        $answerComment,
332
        $answerCorrect,
333
        $id,
334
        $questionId,
335
        $ans,
336
        $resultsDisabled,
337
        $showTotalScoreAndUserChoices,
338
        $export = false
339
    ) {
340
        if (true === $exercise->hideNoAnswer && empty($studentChoice)) {
341
            return '';
342
        }
343
        if ($export) {
344
            $answer = strip_tags_blacklist($answer, ['title', 'head']);
345
            // Fix answers that contains this tags
346
            $tags = [
347
                '<html>',
348
                '</html>',
349
                '<body>',
350
                '</body>',
351
            ];
352
            $answer = str_replace($tags, '', $answer);
353
        }
354
355
        $studentChoiceInt = (int) $studentChoice;
356
        $answerCorrectChoice = (int) $answerCorrect;
357
        $hide_expected_answer = false;
358
        $showComment = false;
359
        switch ($resultsDisabled) {
360
            case RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER:
361
                $hide_expected_answer = true;
362
                $showComment = true;
363
                if (!$answerCorrect && empty($studentChoice)) {
364
                    return '';
365
                }
366
                break;
367
            case RESULT_DISABLE_SHOW_SCORE_ONLY:
368
                if (0 == $feedbackType) {
369
                    $hide_expected_answer = true;
370
                }
371
                break;
372
            case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
373
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
374
                $hide_expected_answer = true;
375
                if ($showTotalScoreAndUserChoices) {
376
                    $hide_expected_answer = false;
377
                }
378
                break;
379
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT_NO_FEEDBACK:
380
                if (false === $showTotalScoreAndUserChoices && empty($studentChoiceInt)) {
381
                    return '';
382
                }
383
                break;
384
        }
385
386
        if (in_array($answerType, [UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION])) {
387
            if ($studentChoice) {
388
                $icon = StateIcon::RADIOBOX_MARKED;
389
            } else {
390
                $icon = StateIcon::RADIOBOX_BLANK;
391
            }
392
        } else {
393
            if ($studentChoice) {
394
                $icon = StateIcon::CHECKBOX_MARKED;
395
            } else {
396
                $icon = StateIcon::CHECKBOX_BLANK;
397
            }
398
        }
399
        if (in_array($answerType, [UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION])) {
400
            if ($answerCorrect) {
401
                $iconAnswer = StateIcon::RADIOBOX_MARKED;
402
            } else {
403
                $iconAnswer = StateIcon::RADIOBOX_BLANK;
404
            }
405
        } else {
406
            if ($answerCorrect) {
407
                $iconAnswer = StateIcon::CHECKBOX_MARKED;
408
            } else {
409
                $iconAnswer = StateIcon::CHECKBOX_BLANK;
410
            }
411
412
        }
413
414
        $studentChoiceClass = '';
415
        if (in_array(
416
            $resultsDisabled,
417
            [
418
                RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
419
                RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
420
            ]
421
        )
422
        ) {
423
            if ($answerCorrect) {
424
                $studentChoiceClass = 'success';
425
            }
426
        }
427
428
        echo '<tr class="'.$studentChoiceClass.'">';
429
430
        echo '<td style="width:5%">';
431
        echo Display::getMdiIcon($icon, 'ch-tool-icon', null, ICON_SIZE_TINY);
432
        echo '</td>';
433
        if ($exercise->showExpectedChoiceColumn()) {
434
            if (false === $hide_expected_answer) {
435
                echo '<td style="width:5%">';
436
                echo Display::getMdiIcon($iconAnswer, 'ch-tool-icon', null, ICON_SIZE_TINY);
437
                echo '</td>';
438
            } else {
439
                echo '<td style="width:5%">';
440
                echo '-';
441
                echo '</td>';
442
            }
443
        }
444
445
        echo '<td style="width:40%">';
446
        echo $answer;
447
        echo '</td>';
448
449
        if ($exercise->showExpectedChoice()) {
450
            $status = Display::label(get_lang('Incorrect'), 'danger');
451
            if ($answerCorrect || ($answerCorrect && $studentChoiceInt === $answerCorrectChoice)) {
452
                $status = Display::label(get_lang('Correct'), 'success');
453
            }
454
            echo '<td class="text-center">';
455
            // Show only status for the selected student answer BT#16256
456
            if ($studentChoice) {
457
                echo $status;
458
            }
459
460
            echo '</td>';
461
        }
462
463
        if (EXERCISE_FEEDBACK_TYPE_EXAM != $feedbackType) {
464
            $showComment = true;
465
        }
466
467
        if (false === $exercise->hideComment) {
468
            if ($showComment) {
469
                echo '<td style="width:20%">';
470
                $color = 'black';
471
                if ($answerCorrect) {
472
                    $color = 'green';
473
                }
474
                if ($hide_expected_answer) {
475
                    $color = '';
476
                }
477
                $comment = '<span style="font-weight: bold; color: '.$color.';">'.
478
                Security::remove_XSS($answerComment).
479
                '</span>';
480
                echo $comment;
481
                echo '</td>';
482
            } else {
483
                echo '<td>&nbsp;</td>';
484
            }
485
        }
486
487
        echo '</tr>';
488
    }
489
490
    /**
491
     * Display the answers to a multiple choice question.
492
     *
493
     * @param Exercise $exercise
494
     * @param int $feedbackType Feedback type
495
     * @param int $answerType Answer type
496
     * @param int $studentChoice Student choice
497
     * @param string  $answer Textual answer
498
     * @param string  $answerComment Comment on answer
499
     * @param string  $answerCorrect Correct answer comment
500
     * @param int $id Exercise ID
501
     * @param int $questionId Question ID
502
     * @param bool $ans Whether to show the answer comment or not
503
     * @param int $resultsDisabled
504
     * @param bool $showTotalScoreAndUserChoices
505
     */
506
    public static function display_multiple_answer_true_false(
507
        $exercise,
508
        $feedbackType,
509
        $answerType,
510
        $studentChoice,
511
        $answer,
512
        $answerComment,
513
        $answerCorrect,
514
        $id,
515
        $questionId,
516
        $ans,
517
        $resultsDisabled,
518
        $showTotalScoreAndUserChoices
519
    ) {
520
        $hide_expected_answer = false;
521
        $hideStudentChoice = false;
522
        switch ($resultsDisabled) {
523
            //case RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING:
524
            case RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER:
525
                $hideStudentChoice = false;
526
                $hide_expected_answer = true;
527
                break;
528
            case RESULT_DISABLE_SHOW_SCORE_ONLY:
529
                if (0 == $feedbackType) {
530
                    $hide_expected_answer = true;
531
                }
532
                break;
533
            case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
534
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
535
                $hide_expected_answer = true;
536
                if ($showTotalScoreAndUserChoices) {
537
                    $hide_expected_answer = false;
538
                }
539
                break;
540
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT_NO_FEEDBACK:
541
                if (false === $showTotalScoreAndUserChoices && empty($studentChoice)) {
542
                    return '';
543
                }
544
                break;
545
        }
546
547
        $content = '<tr>';
548
        if (false === $hideStudentChoice) {
549
            $content .= '<td width="5%">';
550
            $course_id = api_get_course_int_id();
551
            $new_options = [];
552
            $originOptions = Question::readQuestionOption($questionId);
553
554
            if (!empty($originOptions)) {
555
                foreach ($originOptions as $item) {
556
                    $new_options[$item['iid']] = $item;
557
                }
558
            }
559
560
            // Your choice
561
            if (isset($new_options[$studentChoice])) {
562
                $content .= get_lang($new_options[$studentChoice]['title']);
563
            } else {
564
                $content .= '-';
565
            }
566
            $content .= '</td>';
567
        }
568
569
        // Expected choice
570
        if ($exercise->showExpectedChoiceColumn()) {
571
            if (!$hide_expected_answer) {
572
                $content .= '<td width="5%">';
573
                if (isset($new_options[$answerCorrect])) {
574
                    $content .= get_lang($new_options[$answerCorrect]['title']);
575
                } else {
576
                    $content .= '-';
577
                }
578
                $content .= '</td>';
579
            }
580
        }
581
582
        $content .= '<td width="40%">';
583
        $content .= $answer;
584
        $content .= '</td>';
585
586
        if ($exercise->showExpectedChoice()) {
587
            $status = Display::label(get_lang('Incorrect'), 'danger');
588
            if (isset($new_options[$studentChoice])) {
589
                if ($studentChoice == $answerCorrect) {
590
                    $status = Display::label(get_lang('Correct'), 'success');
591
                }
592
            }
593
            $content .= '<td class="text-center">';
594
            $content .= $status;
595
            $content .= '</td>';
596
        }
597
598
        if (false === $exercise->hideComment) {
599
            if (EXERCISE_FEEDBACK_TYPE_EXAM != $feedbackType) {
600
                $content .= '<td width="20%">';
601
                $color = 'black';
602
                if (isset($new_options[$studentChoice]) || in_array(
603
                    $exercise->results_disabled,
604
                    [
605
                        RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
606
                        RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
607
                    ]
608
                )
609
            ) {
610
                    if ($studentChoice == $answerCorrect) {
611
                        $color = 'green';
612
                    }
613
614
                    if ($hide_expected_answer) {
615
                        $color = '';
616
                    }
617
                    $content .= '<span style="font-weight: bold; color: '.$color.';">'.nl2br($answerComment).'</span>';
618
                }
619
                $content .= '</td>';
620
            }
621
        }
622
        $content .= '</tr>';
623
624
        echo $content;
625
    }
626
627
    /**
628
     * Display the answers to a multiple choice question.
629
     *
630
     * @param Exercise $exercise
631
     * @param int      $feedbackType
632
     * @param int      $studentChoice
633
     * @param int      $studentChoiceDegree
634
     * @param string   $answer
635
     * @param string   $answerComment
636
     * @param int      $answerCorrect
637
     * @param int      $questionId
638
     * @param bool     $inResultsDisabled
639
     */
640
    public static function displayMultipleAnswerTrueFalseDegreeCertainty(
641
        $exercise,
642
        $feedbackType,
643
        $studentChoice,
644
        $studentChoiceDegree,
645
        $answer,
646
        $answerComment,
647
        $answerCorrect,
648
        $questionId,
649
        $inResultsDisabled
650
    ) {
651
        $hideExpectedAnswer = false;
652
        if (0 == $feedbackType && 2 == $inResultsDisabled) {
653
            $hideExpectedAnswer = true;
654
        }
655
656
        echo '<tr><td width="5%">';
657
        $question = new MultipleAnswerTrueFalseDegreeCertainty();
658
        $courseId = api_get_course_int_id();
659
        $newOptions = Question::readQuestionOption($questionId, $courseId);
660
661
        // Your choice
662
        if (isset($newOptions[$studentChoice])) {
663
            echo get_lang($newOptions[$studentChoice]['name']);
664
        } else {
665
            echo '-';
666
        }
667
        echo '</td>';
668
669
        // Expected choice
670
        if ($exercise->showExpectedChoiceColumn()) {
671
            echo '<td width="5%">';
672
            if (!$hideExpectedAnswer) {
673
                if (isset($newOptions[$answerCorrect])) {
674
                    echo get_lang($newOptions[$answerCorrect]['name']);
675
                } else {
676
                    echo '-';
677
                }
678
            } else {
679
                echo '-';
680
            }
681
            echo '</td>';
682
        }
683
684
        echo '<td width="20%">';
685
        echo $answer;
686
        echo '</td><td width="5%" style="text-align:center;">';
687
        if (isset($newOptions[$studentChoiceDegree])) {
688
            echo $newOptions[$studentChoiceDegree]['name'];
689
        }
690
        echo '</td>';
691
692
        $position = isset($newOptions[$studentChoiceDegree]) ? $newOptions[$studentChoiceDegree]['position'] : '';
693
        $degreeInfo = $question->getResponseDegreeInfo(
694
            $studentChoice,
695
            $answerCorrect,
696
            $position
697
        );
698
699
        $degreeInfo['color'] = isset($degreeInfo['color']) ? $degreeInfo['color'] : '';
700
        $degreeInfo['background-color'] = isset($degreeInfo['background-color']) ? $degreeInfo['background-color'] : '';
701
        $degreeInfo['description'] = isset($degreeInfo['description']) ? $degreeInfo['description'] : '';
702
        $degreeInfo['label'] = isset($degreeInfo['label']) ? $degreeInfo['label'] : '';
703
704
        echo '
705
            <td width="15%">
706
                <div style="text-align:center;color: '.$degreeInfo['color'].';
707
                    background-color: '.$degreeInfo['background-color'].';
708
                    line-height:30px;height:30px;width: 100%;margin:auto;"
709
                    title="'.$degreeInfo['description'].'">'.
710
                    nl2br($degreeInfo['label']).
711
                '</div>
712
            </td>';
713
714
        if (false === $exercise->hideComment) {
715
            if (EXERCISE_FEEDBACK_TYPE_EXAM != $feedbackType) {
716
                echo '<td width="20%">';
717
                if (isset($newOptions[$studentChoice])) {
718
                    echo '<span style="font-weight: bold; color: black;">'.nl2br($answerComment).'</span>';
719
                }
720
                echo '</td>';
721
            } else {
722
                echo '<td>&nbsp;</td>';
723
            }
724
        }
725
        echo '</tr>';
726
    }
727
728
    /**
729
     * Display the answers to a multiple choice question.
730
     *
731
     * @param Exercise $exercise
732
     * @param int Answer type
733
     * @param int Student choice
734
     * @param string  Textual answer
735
     * @param string  Comment on answer
736
     * @param string  Correct answer comment
737
     * @param int Exercise ID
738
     * @param int Question ID
739
     * @param bool Whether to show the answer comment or not
740
     */
741
    public static function display_multiple_answer_combination_true_false(
742
        $exercise,
743
        $feedbackType,
744
        $answerType,
745
        $studentChoice,
746
        $answer,
747
        $answerComment,
748
        $answerCorrect,
749
        $id,
750
        $questionId,
751
        $ans,
752
        $resultsDisabled,
753
        $showTotalScoreAndUserChoices
754
    ) {
755
        $hide_expected_answer = false;
756
        $hideStudentChoice = false;
757
        switch ($resultsDisabled) {
758
            case RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING:
759
            case RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER:
760
                $hideStudentChoice = true;
761
                $hide_expected_answer = true;
762
                break;
763
            case RESULT_DISABLE_SHOW_SCORE_ONLY:
764
                if (0 == $feedbackType) {
765
                    $hide_expected_answer = true;
766
                }
767
                break;
768
            case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
769
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
770
                $hide_expected_answer = true;
771
                if ($showTotalScoreAndUserChoices) {
772
                    $hide_expected_answer = false;
773
                }
774
                break;
775
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT_NO_FEEDBACK:
776
                if (false === $showTotalScoreAndUserChoices && empty($studentChoice)) {
777
                    return '';
778
                }
779
                break;
780
        }
781
782
        echo '<tr>';
783
784
        if (false === $hideStudentChoice) {
785
            echo '<td width="5%">';
786
            // Your choice
787
            $question = new MultipleAnswerCombinationTrueFalse();
788
            if (isset($question->options[$studentChoice])) {
789
                echo $question->options[$studentChoice];
790
            } else {
791
                echo $question->options[2];
792
            }
793
            echo '</td>';
794
        }
795
796
        // Expected choice
797
        if ($exercise->showExpectedChoiceColumn()) {
798
            if (!$hide_expected_answer) {
799
                echo '<td width="5%">';
800
                if (isset($question->options[$answerCorrect])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $question does not seem to be defined for all execution paths leading up to this point.
Loading history...
801
                    echo $question->options[$answerCorrect];
802
                } else {
803
                    echo $question->options[2];
804
                }
805
                echo '</td>';
806
            }
807
        }
808
809
        echo '<td width="40%">';
810
        echo $answer;
811
        echo '</td>';
812
813
        if ($exercise->showExpectedChoice()) {
814
            $status = '';
815
            if (isset($studentChoice)) {
816
                $status = Display::label(get_lang('Incorrect'), 'danger');
817
                if ($studentChoice == $answerCorrect) {
818
                    $status = Display::label(get_lang('Correct'), 'success');
819
                }
820
            }
821
            echo '<td class="text-center">';
822
            echo $status;
823
            echo '</td>';
824
        }
825
826
        if (false === $exercise->hideComment) {
827
            if (EXERCISE_FEEDBACK_TYPE_EXAM != $feedbackType) {
828
                echo '<td width="20%">';
829
                //@todo replace this harcoded value
830
                if ($studentChoice || in_array(
831
                        $resultsDisabled,
832
                        [
833
                RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
834
                RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
835
                        ]
836
                    )
837
            ) {
838
                    $color = 'black';
839
                    if ($studentChoice == $answerCorrect) {
840
                        $color = 'green';
841
                    }
842
                    if ($hide_expected_answer) {
843
                        $color = '';
844
                    }
845
                    echo '<span style="font-weight: bold; color: '.$color.';">'.nl2br($answerComment).'</span>';
846
                }
847
                echo '</td>';
848
            } else {
849
                echo '<td>&nbsp;</td>';
850
            }
851
        }
852
        echo '</tr>';
853
    }
854
855
    /**
856
     * @param int  $feedbackType
857
     * @param int  $exeId
858
     * @param int  $questionId
859
     * @param null $questionScore
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $questionScore is correct as it would always require null to be passed?
Loading history...
860
     * @param int  $resultsDisabled
861
     */
862
    public static function displayAnnotationAnswer(
863
        $feedbackType,
864
        $exeId,
865
        $questionId,
866
        $questionScore = null,
867
        $resultsDisabled = 0
868
    ) {
869
        $comments = Event::get_comments($exeId, $questionId);
870
        if (EXERCISE_FEEDBACK_TYPE_EXAM != $feedbackType) {
871
            if ($questionScore <= 0 && empty($comments)) {
872
                echo '<br />'.ExerciseLib::getNotCorrectedYetText();
873
            }
874
        }
875
    }
876
}
877