Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

ExerciseShowFunctions::display_calculated_answer()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 47
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 31
c 1
b 0
f 0
nc 6
nop 10
dl 0
loc 47
rs 8.8017

How to fix   Many Parameters   

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

122
            /** @scrutinizer ignore-call */ 
123
            $exeInfo = Event::get_exercise_results_by_attempt($exeId);

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...
123
            if (empty($exeInfo)) {
124
                global $exercise_stat_info;
125
                $userId = $exercise_stat_info['exe_user_id'];
126
            } else {
127
                $userId = $exeInfo[$exeId]['exe_user_id'];
128
            }
129
            $userWebpath = UserManager::getUserPathById($userId, 'web').'my_files'.'/upload_answer/'.$exeId.'/'.$questionId.'/';
130
            $filesNames = explode('|', $answer);
131
            echo '<tr><td>';
132
            foreach ($filesNames as $filename) {
133
                $filename = Security::remove_XSS($filename);
134
                echo '<p><a href="'.$userWebpath.$filename.'" target="_blank">'.$filename.'</a></p>';
135
            }
136
            echo '</td></tr>';
137
        }
138
139
        if (EXERCISE_FEEDBACK_TYPE_EXAM != $feedbackType) {
140
            $comments = Event::get_comments($exeId, $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

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