exercise_show_functions.lib.php 26 KB

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