exercise_show_functions.lib.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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 informations when some kind
  8. * of event occur. Each event has his own types of informations then each event
  9. * use its own function.
  10. *
  11. * @package chamilo.library
  12. *
  13. * @todo convert queries to use Database API
  14. */
  15. /**
  16. * Class.
  17. *
  18. * @package chamilo.library
  19. */
  20. class ExerciseShowFunctions
  21. {
  22. /**
  23. * Shows the answer to a fill-in-the-blanks question, as HTML.
  24. *
  25. * @param Exercise $exercise
  26. * @param int $feedbackType
  27. * @param string $answer
  28. * @param int $id Exercise ID
  29. * @param int $questionId Question ID
  30. * @param int $resultsDisabled
  31. * @param string $originalStudentAnswer
  32. * @param bool $showTotalScoreAndUserChoices
  33. */
  34. public static function display_fill_in_blanks_answer(
  35. $exercise,
  36. $feedbackType,
  37. $answer,
  38. $id,
  39. $questionId,
  40. $resultsDisabled,
  41. $originalStudentAnswer = '',
  42. $showTotalScoreAndUserChoices
  43. ) {
  44. $answerHTML = FillBlanks::getHtmlDisplayForAnswer(
  45. $answer,
  46. $feedbackType,
  47. $resultsDisabled,
  48. $showTotalScoreAndUserChoices
  49. );
  50. if (empty($id)) {
  51. echo '<tr><td>';
  52. echo Security::remove_XSS($answerHTML, COURSEMANAGERLOWSECURITY);
  53. echo '</td></tr>';
  54. } else {
  55. echo '<tr><td>';
  56. echo Security::remove_XSS($answerHTML, COURSEMANAGERLOWSECURITY);
  57. echo '</td>';
  58. echo '</tr>';
  59. }
  60. }
  61. /**
  62. * Shows the answer to a calculated question, as HTML.
  63. *
  64. * @param Exercise $exercise
  65. * @param string Answer text
  66. * @param int Exercise ID
  67. * @param int Question ID
  68. */
  69. public static function display_calculated_answer(
  70. $exercise,
  71. $feedback_type,
  72. $answer,
  73. $id,
  74. $questionId,
  75. $resultsDisabled,
  76. $showTotalScoreAndUserChoices,
  77. $expectedChoice = '',
  78. $choice = '',
  79. $status = ''
  80. ) {
  81. if ($exercise->showExpectedChoice()) {
  82. if (empty($id)) {
  83. echo '<tr><td>'.Security::remove_XSS($answer).'</td>';
  84. echo '<td>'.Security::remove_XSS($choice).'</td>';
  85. if ($exercise->showExpectedChoiceColumn()) {
  86. echo '<td>'.Security::remove_XSS($expectedChoice).'</td>';
  87. }
  88. echo '<td>'.Security::remove_XSS($status).'</td>';
  89. echo '</tr>';
  90. } else {
  91. echo '<tr><td>';
  92. echo Security::remove_XSS($answer);
  93. echo '</td><td>';
  94. echo Security::remove_XSS($choice);
  95. echo '</td>';
  96. if ($exercise->showExpectedChoiceColumn()) {
  97. echo '<td>';
  98. echo Security::remove_XSS($expectedChoice);
  99. echo '</td>';
  100. }
  101. echo '<td>';
  102. echo Security::remove_XSS($status);
  103. echo '</td>';
  104. echo '</tr>';
  105. }
  106. } else {
  107. if (empty($id)) {
  108. echo '<tr><td>'.Security::remove_XSS($answer).'</td></tr>';
  109. } else {
  110. echo '<tr><td>';
  111. echo Security::remove_XSS($answer);
  112. echo '</tr>';
  113. }
  114. }
  115. }
  116. /**
  117. * Shows the answer to a free-answer question, as HTML.
  118. *
  119. * @param string Answer text
  120. * @param int Exercise ID
  121. * @param int Question ID
  122. */
  123. public static function display_free_answer(
  124. $feedback_type,
  125. $answer,
  126. $exe_id,
  127. $questionId,
  128. $questionScore = null,
  129. $resultsDisabled = 0
  130. ) {
  131. $comments = Event::get_comments($exe_id, $questionId);
  132. if (!empty($answer)) {
  133. echo '<tr><td>';
  134. echo Security::remove_XSS($answer);
  135. echo '</td></tr>';
  136. }
  137. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  138. if ($questionScore > 0 || !empty($comments)) {
  139. } else {
  140. echo '<tr>';
  141. echo Display::tag('td', ExerciseLib::getNotCorrectedYetText());
  142. echo '</tr>';
  143. }
  144. }
  145. }
  146. /**
  147. * @param $feedback_type
  148. * @param $answer
  149. * @param $id
  150. * @param $questionId
  151. * @param null $fileUrl
  152. * @param int $resultsDisabled
  153. * @param int $questionScore
  154. */
  155. public static function display_oral_expression_answer(
  156. $feedback_type,
  157. $answer,
  158. $id,
  159. $questionId,
  160. $fileUrl = null,
  161. $resultsDisabled = 0,
  162. $questionScore = 0
  163. ) {
  164. if (isset($fileUrl)) {
  165. echo '
  166. <tr>
  167. <td><audio src="'.$fileUrl.'" controls></audio></td>
  168. </tr>
  169. ';
  170. }
  171. if (empty($id)) {
  172. echo '<tr>';
  173. if (!empty($answer)) {
  174. echo Display::tag('td', Security::remove_XSS($answer), ['width' => '55%']);
  175. }
  176. echo '</tr>';
  177. if (!$questionScore && $feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  178. echo '<tr>';
  179. echo Display::tag('td', ExerciseLib::getNotCorrectedYetText(), ['width' => '45%']);
  180. echo '</tr>';
  181. } else {
  182. echo '<tr><td>&nbsp;</td></tr>';
  183. }
  184. } else {
  185. echo '<tr>';
  186. echo '<td>';
  187. if (!empty($answer)) {
  188. echo Security::remove_XSS($answer);
  189. }
  190. echo '</td>';
  191. echo '</tr>';
  192. }
  193. }
  194. /**
  195. * Displays the answer to a hotspot question.
  196. *
  197. * @param int $feedback_type
  198. * @param int $answerId
  199. * @param string $answer
  200. * @param string $studentChoice
  201. * @param string $answerComment
  202. * @param int $resultsDisabled
  203. * @param int $orderColor
  204. * @param bool $showTotalScoreAndUserChoices
  205. */
  206. public static function display_hotspot_answer(
  207. $feedback_type,
  208. $answerId,
  209. $answer,
  210. $studentChoice,
  211. $answerComment,
  212. $resultsDisabled,
  213. $orderColor,
  214. $showTotalScoreAndUserChoices
  215. ) {
  216. $hide_expected_answer = false;
  217. switch ($resultsDisabled) {
  218. case RESULT_DISABLE_SHOW_SCORE_ONLY:
  219. if ($feedback_type == 0) {
  220. $hide_expected_answer = true;
  221. }
  222. break;
  223. case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
  224. case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
  225. $hide_expected_answer = true;
  226. if ($showTotalScoreAndUserChoices) {
  227. $hide_expected_answer = false;
  228. }
  229. break;
  230. }
  231. $hotspot_colors = [
  232. '', // $i starts from 1 on next loop (ugly fix)
  233. '#4271B5',
  234. '#FE8E16',
  235. '#45C7F0',
  236. '#BCD631',
  237. '#D63173',
  238. '#D7D7D7',
  239. '#90AFDD',
  240. '#AF8640',
  241. '#4F9242',
  242. '#F4EB24',
  243. '#ED2024',
  244. '#3B3B3B',
  245. '#F7BDE2',
  246. ];
  247. $content = '<table class="data_table"><tr>';
  248. $content .= '<td class="text-center" width="5%">';
  249. $content .= '<span class="fa fa-square fa-fw fa-2x" aria-hidden="true" style="color:'.
  250. $hotspot_colors[$orderColor].'"></span>';
  251. $content .= '</td>';
  252. $content .= '<td class="text-left" width="25%">';
  253. $content .= "$answerId - $answer";
  254. $content .= '</td>';
  255. $content .= '<td class="text-left" width="10%">';
  256. if (!$hide_expected_answer) {
  257. $status = Display::label(get_lang('Incorrect'), 'danger');
  258. if ($studentChoice) {
  259. $status = Display::label(get_lang('Correct'), 'success');
  260. } else {
  261. if (in_array($resultsDisabled, [
  262. RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
  263. ])
  264. ) {
  265. return '';
  266. }
  267. }
  268. $content .= $status;
  269. }
  270. $content .= '</td>';
  271. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  272. $content .= '<td class="text-left" width="60%">';
  273. if ($studentChoice) {
  274. $content .= '<span style="font-weight: bold; color: #008000;">'.nl2br($answerComment).'</span>';
  275. }
  276. $content .= '</td>';
  277. } else {
  278. $content .= '<td class="text-left" width="60%">&nbsp;</td>';
  279. }
  280. $content .= '</tr>';
  281. echo $content;
  282. }
  283. /**
  284. * Display the answers to a multiple choice question.
  285. *
  286. * @param Exercise $exercise
  287. * @param int $feedbackType Feedback type
  288. * @param int $answerType Answer type
  289. * @param int $studentChoice Student choice
  290. * @param string $answer Textual answer
  291. * @param string $answerComment Comment on answer
  292. * @param string $answerCorrect Correct answer comment
  293. * @param int $id Exercise ID
  294. * @param int $questionId Question ID
  295. * @param bool $ans Whether to show the answer comment or not
  296. * @param bool $resultsDisabled
  297. * @param bool $showTotalScoreAndUserChoices
  298. * @param bool $export
  299. */
  300. public static function display_unique_or_multiple_answer(
  301. $exercise,
  302. $feedbackType,
  303. $answerType,
  304. $studentChoice,
  305. $answer,
  306. $answerComment,
  307. $answerCorrect,
  308. $id,
  309. $questionId,
  310. $ans,
  311. $resultsDisabled,
  312. $showTotalScoreAndUserChoices,
  313. $export = false
  314. ) {
  315. if ($export) {
  316. $answer = strip_tags_blacklist($answer, ['title', 'head']);
  317. // Fix answers that contains this tags
  318. $tags = [
  319. '<html>',
  320. '</html>',
  321. '<body>',
  322. '</body>',
  323. ];
  324. $answer = str_replace($tags, '', $answer);
  325. }
  326. $studentChoiceInt = (int) $studentChoice;
  327. $answerCorrectChoice = (int) $answerCorrect;
  328. $hide_expected_answer = false;
  329. $showComment = false;
  330. switch ($resultsDisabled) {
  331. case RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER:
  332. $hide_expected_answer = true;
  333. $showComment = true;
  334. if (!$answerCorrect && empty($studentChoice)) {
  335. return '';
  336. }
  337. break;
  338. case RESULT_DISABLE_SHOW_SCORE_ONLY:
  339. if ($feedbackType == 0) {
  340. $hide_expected_answer = true;
  341. }
  342. break;
  343. case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
  344. case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
  345. $hide_expected_answer = true;
  346. if ($showTotalScoreAndUserChoices) {
  347. $hide_expected_answer = false;
  348. }
  349. break;
  350. }
  351. $icon = in_array($answerType, [UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION]) ? 'radio' : 'checkbox';
  352. $icon .= $studentChoice ? '_on' : '_off';
  353. $icon .= '.png';
  354. $iconAnswer = in_array($answerType, [UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION]) ? 'radio' : 'checkbox';
  355. $iconAnswer .= $answerCorrect ? '_on' : '_off';
  356. $iconAnswer .= '.png';
  357. $studentChoiceClass = '';
  358. if (in_array(
  359. $resultsDisabled,
  360. [
  361. RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
  362. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
  363. ]
  364. )
  365. ) {
  366. if ($answerCorrect) {
  367. $studentChoiceClass = 'success';
  368. }
  369. }
  370. echo '<tr class="'.$studentChoiceClass.'">';
  371. echo '<td width="5%">';
  372. echo Display::return_icon($icon, null, null, ICON_SIZE_TINY);
  373. echo '</td>';
  374. if ($exercise->showExpectedChoiceColumn()) {
  375. if ($hide_expected_answer === false) {
  376. echo '<td width="5%">';
  377. echo Display::return_icon($iconAnswer, null, null, ICON_SIZE_TINY);
  378. echo '</td>';
  379. } else {
  380. echo '<td width="5%">';
  381. echo '-';
  382. echo '</td>';
  383. }
  384. }
  385. echo '<td width="40%">';
  386. echo $answer;
  387. echo '</td>';
  388. if ($exercise->showExpectedChoice()) {
  389. $status = Display::label(get_lang('Incorrect'), 'danger');
  390. if ($answerCorrect || ($answerCorrect && $studentChoiceInt === $answerCorrectChoice)) {
  391. $status = Display::label(get_lang('Correct'), 'success');
  392. }
  393. echo '<td width="20%">';
  394. echo $status;
  395. echo '</td>';
  396. }
  397. if ($feedbackType != EXERCISE_FEEDBACK_TYPE_EXAM) {
  398. $showComment = true;
  399. }
  400. if ($showComment) {
  401. echo '<td width="20%">';
  402. $color = 'black';
  403. if ($answerCorrect) {
  404. $color = 'green';
  405. }
  406. if ($hide_expected_answer) {
  407. $color = '';
  408. }
  409. $comment = '<span style="font-weight: bold; color: '.$color.';">'.
  410. Security::remove_XSS($answerComment).
  411. '</span>';
  412. echo $comment;
  413. echo '</td>';
  414. } else {
  415. echo '<td>&nbsp;</td>';
  416. }
  417. echo '</tr>';
  418. }
  419. /**
  420. * Display the answers to a multiple choice question.
  421. *
  422. * @param Exercise $exercise
  423. * @param int Answer type
  424. * @param int Student choice
  425. * @param string Textual answer
  426. * @param string Comment on answer
  427. * @param string Correct answer comment
  428. * @param int Exercise ID
  429. * @param int Question ID
  430. * @param bool Whether to show the answer comment or not
  431. */
  432. public static function display_multiple_answer_true_false(
  433. $exercise,
  434. $feedbackType,
  435. $answerType,
  436. $studentChoice,
  437. $answer,
  438. $answerComment,
  439. $answerCorrect,
  440. $id,
  441. $questionId,
  442. $ans,
  443. $resultsDisabled,
  444. $showTotalScoreAndUserChoices
  445. ) {
  446. $hide_expected_answer = false;
  447. $hideStudentChoice = false;
  448. switch ($resultsDisabled) {
  449. //case RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING:
  450. case RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER:
  451. $hideStudentChoice = false;
  452. $hide_expected_answer = true;
  453. break;
  454. case RESULT_DISABLE_SHOW_SCORE_ONLY:
  455. if ($feedbackType == 0) {
  456. $hide_expected_answer = true;
  457. }
  458. break;
  459. case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
  460. case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
  461. $hide_expected_answer = true;
  462. if ($showTotalScoreAndUserChoices) {
  463. $hide_expected_answer = false;
  464. }
  465. break;
  466. }
  467. $content = '<tr>';
  468. if ($hideStudentChoice === false) {
  469. $content .= '<td width="5%">';
  470. $course_id = api_get_course_int_id();
  471. $new_options = Question::readQuestionOption($questionId, $course_id);
  472. // Your choice
  473. if (isset($new_options[$studentChoice])) {
  474. $content .= get_lang($new_options[$studentChoice]['name']);
  475. } else {
  476. $content .= '-';
  477. }
  478. $content .= '</td>';
  479. }
  480. // Expected choice
  481. if ($exercise->showExpectedChoiceColumn()) {
  482. if (!$hide_expected_answer) {
  483. $content .= '<td width="5%">';
  484. if (isset($new_options[$answerCorrect])) {
  485. $content .= get_lang($new_options[$answerCorrect]['name']);
  486. } else {
  487. $content .= '-';
  488. }
  489. $content .= '</td>';
  490. }
  491. }
  492. $content .= '<td width="40%">';
  493. $content .= $answer;
  494. $content .= '</td>';
  495. if ($exercise->showExpectedChoice()) {
  496. $status = Display::label(get_lang('Incorrect'), 'danger');
  497. if (isset($new_options[$studentChoice])) {
  498. if ($studentChoice == $answerCorrect) {
  499. $status = Display::label(get_lang('Correct'), 'success');
  500. }
  501. }
  502. $content .= '<td width="20%">';
  503. $content .= $status;
  504. $content .= '</td>';
  505. }
  506. if ($feedbackType != EXERCISE_FEEDBACK_TYPE_EXAM) {
  507. $content .= '<td width="20%">';
  508. $color = 'black';
  509. if (isset($new_options[$studentChoice]) || in_array(
  510. $exercise->results_disabled,
  511. [
  512. RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
  513. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
  514. ]
  515. )
  516. ) {
  517. if ($studentChoice == $answerCorrect) {
  518. $color = 'green';
  519. }
  520. if ($hide_expected_answer) {
  521. $color = '';
  522. }
  523. $content .= '<span style="font-weight: bold; color: '.$color.';">'.nl2br($answerComment).'</span>';
  524. }
  525. $content .= '</td>';
  526. }
  527. $content .= '</tr>';
  528. echo $content;
  529. }
  530. /**
  531. * Display the answers to a multiple choice question.
  532. *
  533. * @param Exercise $exercise
  534. * @param int $feedbackType
  535. * @param int $studentChoice
  536. * @param int $studentChoiceDegree
  537. * @param string $answer
  538. * @param string $answerComment
  539. * @param int $answerCorrect
  540. * @param int $questionId
  541. * @param bool $inResultsDisabled
  542. */
  543. public static function displayMultipleAnswerTrueFalseDegreeCertainty(
  544. $exercise,
  545. $feedbackType,
  546. $studentChoice,
  547. $studentChoiceDegree,
  548. $answer,
  549. $answerComment,
  550. $answerCorrect,
  551. $questionId,
  552. $inResultsDisabled
  553. ) {
  554. $hideExpectedAnswer = false;
  555. if ($feedbackType == 0 && $inResultsDisabled == 2) {
  556. $hideExpectedAnswer = true;
  557. }
  558. echo '<tr><td width="5%">';
  559. $question = new MultipleAnswerTrueFalseDegreeCertainty();
  560. $courseId = api_get_course_int_id();
  561. $newOptions = Question::readQuestionOption($questionId, $courseId);
  562. // Your choice
  563. if (isset($newOptions[$studentChoice])) {
  564. echo get_lang($newOptions[$studentChoice]['name']);
  565. } else {
  566. echo '-';
  567. }
  568. echo '</td>';
  569. // Expected choice
  570. if ($exercise->showExpectedChoiceColumn()) {
  571. echo '<td width="5%">';
  572. if (!$hideExpectedAnswer) {
  573. if (isset($newOptions[$answerCorrect])) {
  574. echo get_lang($newOptions[$answerCorrect]['name']);
  575. } else {
  576. echo '-';
  577. }
  578. } else {
  579. echo '-';
  580. }
  581. echo '</td>';
  582. }
  583. echo '<td width="20%">';
  584. echo $answer;
  585. echo '</td><td width="5%" style="text-align:center;">';
  586. if (isset($newOptions[$studentChoiceDegree])) {
  587. echo $newOptions[$studentChoiceDegree]['name'];
  588. }
  589. echo '</td>';
  590. $position = isset($newOptions[$studentChoiceDegree]) ? $newOptions[$studentChoiceDegree]['position'] : '';
  591. $degreeInfo = $question->getResponseDegreeInfo(
  592. $studentChoice,
  593. $answerCorrect,
  594. $position
  595. );
  596. $degreeInfo['color'] = isset($degreeInfo['color']) ? $degreeInfo['color'] : '';
  597. $degreeInfo['background-color'] = isset($degreeInfo['background-color']) ? $degreeInfo['background-color'] : '';
  598. $degreeInfo['description'] = isset($degreeInfo['description']) ? $degreeInfo['description'] : '';
  599. $degreeInfo['label'] = isset($degreeInfo['label']) ? $degreeInfo['label'] : '';
  600. echo '
  601. <td width="15%">
  602. <div style="text-align:center;color: '.$degreeInfo['color'].';
  603. background-color: '.$degreeInfo['background-color'].';
  604. line-height:30px;height:30px;width: 100%;margin:auto;"
  605. title="'.$degreeInfo['description'].'">'.
  606. nl2br($degreeInfo['label']).
  607. '</div>
  608. </td>';
  609. if ($feedbackType != EXERCISE_FEEDBACK_TYPE_EXAM) {
  610. echo '<td width="20%">';
  611. if (isset($newOptions[$studentChoice])) {
  612. echo '<span style="font-weight: bold; color: black;">'.nl2br($answerComment).'</span>';
  613. }
  614. echo '</td>';
  615. } else {
  616. echo '<td>&nbsp;</td>';
  617. }
  618. echo '</tr>';
  619. }
  620. /**
  621. * Display the answers to a multiple choice question.
  622. *
  623. * @param Exercise $exercise
  624. * @param int Answer type
  625. * @param int Student choice
  626. * @param string Textual answer
  627. * @param string Comment on answer
  628. * @param string Correct answer comment
  629. * @param int Exercise ID
  630. * @param int Question ID
  631. * @param bool Whether to show the answer comment or not
  632. */
  633. public static function display_multiple_answer_combination_true_false(
  634. $exercise,
  635. $feedbackType,
  636. $answerType,
  637. $studentChoice,
  638. $answer,
  639. $answerComment,
  640. $answerCorrect,
  641. $id,
  642. $questionId,
  643. $ans,
  644. $resultsDisabled,
  645. $showTotalScoreAndUserChoices
  646. ) {
  647. $hide_expected_answer = false;
  648. $hideStudentChoice = false;
  649. switch ($resultsDisabled) {
  650. case RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING:
  651. case RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER:
  652. $hideStudentChoice = true;
  653. $hide_expected_answer = true;
  654. break;
  655. case RESULT_DISABLE_SHOW_SCORE_ONLY:
  656. if ($feedbackType == 0) {
  657. $hide_expected_answer = true;
  658. }
  659. break;
  660. case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
  661. case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
  662. $hide_expected_answer = true;
  663. if ($showTotalScoreAndUserChoices) {
  664. $hide_expected_answer = false;
  665. }
  666. break;
  667. }
  668. echo '<tr>';
  669. if ($hideStudentChoice === false) {
  670. echo '<td width="5%">';
  671. // Your choice
  672. $question = new MultipleAnswerCombinationTrueFalse();
  673. if (isset($question->options[$studentChoice])) {
  674. echo $question->options[$studentChoice];
  675. } else {
  676. echo $question->options[2];
  677. }
  678. echo '</td>';
  679. }
  680. // Expected choice
  681. if ($exercise->showExpectedChoiceColumn()) {
  682. if (!$hide_expected_answer) {
  683. echo '<td width="5%">';
  684. if (isset($question->options[$answerCorrect])) {
  685. echo $question->options[$answerCorrect];
  686. } else {
  687. echo $question->options[2];
  688. }
  689. echo '</td>';
  690. }
  691. }
  692. echo '<td width="40%">';
  693. echo $answer;
  694. echo '</td>';
  695. if ($exercise->showExpectedChoice()) {
  696. $status = '';
  697. if (isset($studentChoice)) {
  698. $status = Display::label(get_lang('Incorrect'), 'danger');
  699. if ($studentChoice == $answerCorrect) {
  700. $status = Display::label(get_lang('Correct'), 'success');
  701. }
  702. }
  703. echo '<td width="20%">';
  704. echo $status;
  705. echo '</td>';
  706. }
  707. if ($feedbackType != EXERCISE_FEEDBACK_TYPE_EXAM) {
  708. echo '<td width="20%">';
  709. //@todo replace this harcoded value
  710. if ($studentChoice || in_array($resultsDisabled, [
  711. RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
  712. RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
  713. ])
  714. ) {
  715. $color = 'black';
  716. if ($studentChoice == $answerCorrect) {
  717. $color = 'green';
  718. }
  719. if ($hide_expected_answer) {
  720. $color = '';
  721. }
  722. echo '<span style="font-weight: bold; color: '.$color.';">'.nl2br($answerComment).'</span>';
  723. }
  724. echo '</td>';
  725. } else {
  726. echo '<td>&nbsp;</td>';
  727. }
  728. echo '</tr>';
  729. }
  730. /**
  731. * @param int $feedbackType
  732. * @param int $exeId
  733. * @param int $questionId
  734. * @param null $questionScore
  735. * @param int $resultsDisabled
  736. */
  737. public static function displayAnnotationAnswer(
  738. $feedbackType,
  739. $exeId,
  740. $questionId,
  741. $questionScore = null,
  742. $resultsDisabled = 0
  743. ) {
  744. $comments = Event::get_comments($exeId, $questionId);
  745. if ($feedbackType != EXERCISE_FEEDBACK_TYPE_EXAM) {
  746. if ($questionScore <= 0 && empty($comments)) {
  747. echo '<br />'.ExerciseLib::getNotCorrectedYetText();
  748. }
  749. }
  750. }
  751. }