Completed
Push — 1.10.x ( 748658...a788c0 )
by
unknown
63:31
created

ExerciseShowFunctions::display_calculated_answer()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 31
Code Lines 26

Duplication

Lines 20
Ratio 64.52 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 26
c 1
b 0
f 0
nc 3
nop 6
dl 20
loc 31
rs 8.5806
1
<?php
2
/* See license terms in /license.txt */
3
/**
4
* EVENTS LIBRARY
5
*
6
* This is the events library for Chamilo.
7
* Functions of this library are used to record information when some kind
8
* of event occur.
9
* use its own function.
10
*
11
* @package chamilo.library
12
* @todo convert queries to use Database API
13
*/
14
/**
15
 * Class
16
 * @package chamilo.library
17
 */
18
class ExerciseShowFunctions
19
{
20
	/**
21
	 * Shows the answer to a fill-in-the-blanks question, as HTML
22
     * @param int $feedbackType
23
	 * @param string    $answer
24
	 * @param int $id       Exercise ID
25
	 * @param int $questionId      Question ID
26
     * @param int $resultsDisabled
27
     * @param string $originalStudentAnswer
28
 *
29
	 * @return void
30
	 */
31
	public static function display_fill_in_blanks_answer(
32
	    $feedbackType,
33
	    $answer,
34
	    $id,
35
	    $questionId,
36
	    $resultsDisabled,
37
	    $originalStudentAnswer = '',
38
	    $showTotalScoreAndUserChoices
39
    ) {
40
        $answerHTML = FillBlanks::getHtmlDisplayForAnswer($answer, $resultsDisabled, $showTotalScoreAndUserChoices);
41
        if (strpos($originalStudentAnswer, 'font color') !== false) {
42
            $answerHTML = $originalStudentAnswer;
43
        }
44
45
        if (empty($id)) {
46
            echo '<tr><td>';
47
            echo Security::remove_XSS($answerHTML, COURSEMANAGERLOWSECURITY);
48
            echo '</td></tr>';
49 View Code Duplication
        } else {
50
            ?>
51
            <tr>
52
                <td>
53
                    <?php echo nl2br(Security::remove_XSS($answerHTML, COURSEMANAGERLOWSECURITY)); ?>
54
                </td>
55
56
                <?php
57
                if (!api_is_allowed_to_edit(null,true) && $feedbackType != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
58
                    <td>
59
                    <?php
60
                    $comm = Event::get_comments($id, $questionId);
61
                    ?>
62
                    </td>
63
                <?php } ?>
64
            </tr>
65
        <?php
66
        }
67
	}
68
69
    /**
70
     * Shows the answer to a calculated question, as HTML
71
     * @param string    Answer text
72
     * @param int       Exercise ID
73
     * @param int       Question ID
74
     * @return void
75
     */
76
    public static function display_calculated_answer(
77
        $feedback_type,
78
        $answer,
79
        $id,
80
        $questionId,
81
        $results_disabled,
82
        $showTotalScoreAndUserChoices
83
    ) {
84
        if (empty($id)) {
85
            echo '<tr><td>'. Security::remove_XSS($answer).'</td></tr>';
86 View Code Duplication
        } else {
87
        ?>
88
            <tr>
89
                <td>
90
                    <?php
91
                    echo Security::remove_XSS($answer);
92
                    ?>
93
                </td>
94
95
            <?php
96
            if (!api_is_allowed_to_edit(null,true) && $feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
97
                <td>
98
                    <?php
99
                    $comm = Event::get_comments($id,$questionId);
100
                    ?>
101
                </td>
102
            <?php } ?>
103
            </tr>
104
        <?php
105
        }
106
    }
107
108
	/**
109
	 * Shows the answer to a free-answer question, as HTML
110
	 * @param string    Answer text
111
	 * @param int       Exercise ID
112
	 * @param int       Question ID
113
	 * @return void
114
	 */
115
	public static function display_free_answer(
116
	    $feedback_type,
117
	    $answer,
118
	    $exe_id,
119
	    $questionId,
120
	    $questionScore = null,
121
	    $results_disabled = 0
122
    ) {
123
        $comments = Event::get_comments($exe_id, $questionId);
124
125
        if (!empty($answer)) {
126
            echo '<tr><td>';
127
            echo nl2br(Security::remove_XSS($answer));
128
            echo '</td></tr>';
129
        }
130
131
        if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
132
            if ($questionScore > 0 || !empty($comments)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
133
            } else {
134
                echo '<tr>';
135
                echo Display::tag('td', Display::return_message(get_lang('notCorrectedYet')), array());
136
                echo '</tr>';
137
            }
138
        }
139
	}
140
141
    /**
142
    * @param $feedback_type
143
    * @param $answer
144
    * @param $id
145
    * @param $questionId
146
    * @param null $nano
147
    * @param int $results_disabled
148
     */
149
	public static function display_oral_expression_answer(
150
	    $feedback_type,
151
	    $answer,
152
	    $id,
153
	    $questionId,
154
        $nano = null,
155
        $results_disabled = 0
156
    ) {
157
        if (isset($nano)) {
158
            echo $nano->show_audio_file();
159
        }
160
161
        if (empty($id)) {
162
            echo '<tr>';
163
            echo Display::tag('td', nl2br(Security::remove_XSS($answer)), array('width'=>'55%'));
164
            echo '</tr>';
165
            if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
166
                echo '<tr>';
167
                echo Display::tag('td',get_lang('notCorrectedYet'), array('width'=>'45%'));
168
                echo '</tr>';
169
            } else {
170
                echo '<tr><td>&nbsp;</td></tr>';
171
            }
172
        } else {
173
            echo '<tr>';
174
            echo '<td>';
175
            if (!empty($answer)) {
176
                echo nl2br(Security::remove_XSS($answer));
177
            }
178
            echo '</td>';
179
180
            if (!api_is_allowed_to_edit(null,true) && $feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
181
                echo '<td>';
182
                $comm = Event::get_comments($id,$questionId);
183
                echo '</td>';
184
            }
185
            echo '</tr>';
186
        }
187
    }
188
189
	/**
190
	 * Displays the answer to a hotspot question
191
     * @param int $feedback_type
192
     * @param int $answerId
193
     * @param string $answer
194
     * @param string $studentChoice
195
     * @param string $answerComment
196
     * @param int $resultsDisabled
197
     * @param int $orderColor
198
     */
199
	public static function display_hotspot_answer(
200
        $feedback_type,
201
        $answerId,
202
        $answer,
203
        $studentChoice,
204
        $answerComment,
205
        $resultsDisabled,
206
        $orderColor,
207
        $showTotalScoreAndUserChoices
208
    ) {
209
        $hide_expected_answer = false;
210
        if ($feedback_type == 0 && $resultsDisabled == 2) {
211
            $hide_expected_answer = true;
212
        }
213
214 View Code Duplication
        if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
215
            if ($showTotalScoreAndUserChoices) {
216
                $hide_expected_answer = true;
217
            } else {
218
                $hide_expected_answer = false;
219
            }
220
        }
221
222
		$hotspot_colors = array(
223
            "", // $i starts from 1 on next loop (ugly fix)
224
            "#4271B5",
225
            "#FE8E16",
226
            "#45C7F0",
227
            "#BCD631",
228
            "#D63173",
229
            "#D7D7D7",
230
            "#90AFDD",
231
            "#AF8640",
232
            "#4F9242",
233
            "#F4EB24",
234
            "#ED2024",
235
            "#3B3B3B",
236
            "#F7BDE2"
237
        );
238
239
		?>
240
		<table class="data_table">
241
		<tr>
242
            <td class="text-center" width="5%">
243
                <span class="fa fa-square fa-fw fa-2x" aria-hidden="true" style="color: <?php echo $hotspot_colors[$orderColor]; ?>"></span>
244
            </td>
245
			<td class="text-left" width="25%">
246
                <?php echo "$answerId - $answer"; ?>
247
			</td>
248
			<td class="text-left" width="10%">
249
				<?php
250
                if (!$hide_expected_answer) {
251
    				$my_choice = $studentChoice ? get_lang('Correct') : get_lang('Fault');
252
    				echo $my_choice;
253
                }
254
				?>
255
			</td>
256
			<?php if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
257
			<td class="text-left" width="60%">
258
				<?php
259
                if ($studentChoice) {
260
                    echo '<span style="font-weight: bold; color: #008000;">'.nl2br($answerComment).'</span>';
261
                }
262
				?>
263
			</td>
264
			<?php } else { ?>
265
				<td class="text-left" width="60%">&nbsp;</td>
266
			<?php } ?>
267
		</tr>
268
		<?php
269
	}
270
271
	/**
272
	 * Display the answers to a multiple choice question
273
	 * @param int $feedback_type Feedback type
274
	 * @param integer Answer type
275
	 * @param integer Student choice
276
	 * @param string  Textual answer
277
	 * @param string  Comment on answer
278
	 * @param string  Correct answer comment
279
	 * @param integer Exercise ID
280
	 * @param integer Question ID
281
	 * @param boolean Whether to show the answer comment or not
282
	 * @return void
283
	 */
284
	public static function display_unique_or_multiple_answer(
285
        $feedback_type,
286
        $answerType,
287
        $studentChoice,
288
        $answer,
289
        $answerComment,
290
        $answerCorrect,
291
        $id,
292
        $questionId,
293
        $ans,
294
        $resultsDisabled,
295
        $showTotalScoreAndUserChoices
296
    ) {
297
298
        $hide_expected_answer = false;
299
        if ($feedback_type == 0 && ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ONLY)) {
300
            $hide_expected_answer = true;
301
        }
302
303 View Code Duplication
        if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
304
            if ($showTotalScoreAndUserChoices) {
305
                $hide_expected_answer = true;
306
            } else {
307
                $hide_expected_answer = false;
308
            }
309
        }
310
311
        $icon = in_array($answerType, array(UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION)) ? 'radio':'checkbox';
312
		$icon .= $studentChoice?'_on':'_off';
313
		$icon .= '.gif';
314
315
		$iconAnswer = in_array($answerType, array(UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION)) ? 'radio':'checkbox';
316
		$iconAnswer .= $answerCorrect ? '_on' : '_off';
317
		$iconAnswer .= '.gif';
318
319
		?>
320
		<tr>
321
		<td width="5%">
322
			<?php echo Display::return_icon($icon); ?>
323
		</td>
324
		<td width="5%">
325
            <?php if (!$hide_expected_answer) {
326
                echo Display::return_icon($iconAnswer);
327
            } else {
328
                echo "-";
329
            } ?>
330
		</td>
331
		<td width="40%">
332
			<?php
333
			echo $answer;
334
			?>
335
		</td>
336
337 View Code Duplication
		<?php if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
338
		<td width="20%">
339
			<?php
340
            if ($studentChoice) {
341
				if ($answerCorrect) {
342
                    $color = 'green';
343
					//echo '<span style="font-weight: bold; color: #008000;">'.nl2br($answerComment).'</span>';
344
				} else {
345
                    $color = 'black';
346
                    //echo '<span style="font-weight: bold; color: #FF0000;">'.nl2br($answerComment).'</span>';
347
				}
348
				if ($hide_expected_answer) {
349
				    $color = '';
350
				}
351
                echo '<span style="font-weight: bold; color: '.$color.';">'.nl2br($answerComment).'</span>';
352
			}
353
			?>
354
		</td>
355
			<?php
356
		    if ($ans==1) {
357
		        $comm = Event::get_comments($id,$questionId);
358
			}
359
		    ?>
360
		 <?php } else { ?>
361
			<td>&nbsp;</td>
362
		<?php } ?>
363
		</tr>
364
		<?php
365
	}
366
367
    /**
368
     * Display the answers to a multiple choice question
369
     *
370
     * @param integer Answer type
371
     * @param integer Student choice
372
     * @param string  Textual answer
373
     * @param string  Comment on answer
374
     * @param string  Correct answer comment
375
     * @param integer Exercise ID
376
     * @param integer Question ID
377
     * @param boolean Whether to show the answer comment or not
378
     * @return void
379
     */
380
    public static function display_multiple_answer_true_false(
381
        $feedback_type,
382
        $answerType,
383
        $studentChoice,
384
        $answer,
385
        $answerComment,
386
        $answerCorrect,
387
        $id,
388
        $questionId,
389
        $ans,
390
        $resultsDisabled,
391
        $showTotalScoreAndUserChoices
392
    ) {
393
        $hide_expected_answer = false;
394
395
        if ($feedback_type == 0 && ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ONLY)) {
396
            $hide_expected_answer = true;
397
        }
398
399 View Code Duplication
        if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
400
            if ($showTotalScoreAndUserChoices) {
401
                $hide_expected_answer = true;
402
            } else {
403
                $hide_expected_answer = false;
404
            }
405
        }
406
407
        ?>
408
        <tr>
409
        <td width="5%">
410
        <?php
411
        $course_id = api_get_course_int_id();
412
        $new_options = Question::readQuestionOption($questionId, $course_id);
413
414
        //Your choice
415 View Code Duplication
        if (isset($new_options[$studentChoice])) {
416
            echo get_lang($new_options[$studentChoice]['name']);
417
        } else {
418
        	echo '-';
419
        }
420
421
        ?>
422
        </td>
423
        <td width="5%">
424
        <?php
425
426
		// Expected choice
427
        if (!$hide_expected_answer) {
428 View Code Duplication
            if (isset($new_options[$answerCorrect])) {
429
                echo get_lang($new_options[$answerCorrect]['name']);
430
            } else {
431
                echo '-';
432
            }
433
        } else {
434
            echo '-';
435
        }
436
        ?>
437
        </td>
438
        <td width="40%">
439
			<?php echo $answer; ?>
440
        </td>
441
442 View Code Duplication
        <?php if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
443
        <td width="20%">
444
            <?php
445
            $color = "black";
446
            if (isset($new_options[$studentChoice])) {
447
                if ($studentChoice == $answerCorrect) {
448
                    $color = "green";
449
                }
450
451
                if ($hide_expected_answer) {
452
				    $color = '';
453
				}
454
455
                echo '<span style="font-weight: bold; color: '.$color.';">'.nl2br($answerComment).'</span>';
456
            }
457
            ?>
458
        </td>
459
            <?php
460
            if ($ans==1) {
461
                $comm = Event::get_comments($id, $questionId);
462
            }
463
            ?>
464
         <?php } else { ?>
465
            <td>&nbsp;</td>
466
        <?php } ?>
467
        </tr>
468
        <?php
469
    }
470
471
     /**
472
     * Display the answers to a multiple choice question
473
     *
474
     * @param integer Answer type
475
     * @param integer Student choice
476
     * @param string  Textual answer
477
     * @param string  Comment on answer
478
     * @param string  Correct answer comment
479
     * @param integer Exercise ID
480
     * @param integer Question ID
481
     * @param boolean Whether to show the answer comment or not
482
     * @return void
483
     */
484
    public static function display_multiple_answer_combination_true_false(
485
        $feedback_type,
486
        $answerType,
487
        $studentChoice,
488
        $answer,
489
        $answerComment,
490
        $answerCorrect,
491
        $id,
492
        $questionId,
493
        $ans,
494
        $resultsDisabled,
495
        $showTotalScoreAndUserChoices
496
    ) {
497
        $hide_expected_answer = false;
498
        if ($feedback_type == 0 && ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ONLY)) {
499
            $hide_expected_answer = true;
500
        }
501
502 View Code Duplication
        if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
503
            if ($showTotalScoreAndUserChoices) {
504
                $hide_expected_answer = true;
505
            } else {
506
                $hide_expected_answer = false;
507
            }
508
        }
509
510
        ?>
511
        <tr>
512
        <td width="5%">
513
        <?php
514
		//Your choice
515
        $question = new MultipleAnswerCombinationTrueFalse();
516
        if (isset($question->options[$studentChoice])) {
517
            echo $question->options[$studentChoice];
518
        } else {
519
            echo $question->options[2];
520
        }
521
        ?>
522
        </td>
523
        <td width="5%">
524
        <?php
525
		// Expected choice
526
        if (!$hide_expected_answer) {
527
            if (isset($question->options[$answerCorrect])) {
528
                echo $question->options[$answerCorrect];
529
            } else {
530
                echo $question->options[2];
531
            }
532
        }
533
        else {
534
            echo '-';
535
        }
536
        ?>
537
        </td>
538
        <td width="40%">
539
            <?php
540
            //my answer
541
            echo $answer;
542
            ?>
543
        </td>
544
545 View Code Duplication
        <?php if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
546
        <td width="20%">
547
            <?php
548
            //@todo replace this harcoded value
549
            if ($studentChoice) {
550
                 $color = "black";
551
                if ($studentChoice == $answerCorrect) {
552
                    $color = "green";
553
                }
554
555
                if ($hide_expected_answer) {
556
                    $color = '';
557
                }
558
                echo '<span style="font-weight: bold; color: '.$color.';">'.nl2br($answerComment).'</span>';
559
            }
560
            ?>
561
        </td>
562
            <?php
563
            if ($ans==1) {
564
                $comm = Event::get_comments($id,$questionId);
565
            }
566
            ?>
567
         <?php } else { ?>
568
            <td>&nbsp;</td>
569
        <?php } ?>
570
        </tr>
571
        <?php
572
    }
573
}
574