exercise_show_functions.lib.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. * @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. * @param bool $showTotalScoreAndUserChoices
  29. *
  30. * @return void
  31. */
  32. public static function display_fill_in_blanks_answer(
  33. $feedbackType,
  34. $answer,
  35. $id,
  36. $questionId,
  37. $resultsDisabled,
  38. $originalStudentAnswer = '',
  39. $showTotalScoreAndUserChoices
  40. ) {
  41. $answerHTML = FillBlanks::getHtmlDisplayForAnswer($answer, $feedbackType, $resultsDisabled, $showTotalScoreAndUserChoices);
  42. /*if (strpos($originalStudentAnswer, 'font color') !== false) {
  43. $answerHTML = $originalStudentAnswer;
  44. }*/
  45. if (empty($id)) {
  46. echo '<tr><td>';
  47. echo Security::remove_XSS($answerHTML, COURSEMANAGERLOWSECURITY);
  48. echo '</td></tr>';
  49. } else {
  50. ?>
  51. <tr>
  52. <td>
  53. <?php echo nl2br(Security::remove_XSS($answerHTML, COURSEMANAGERLOWSECURITY)); ?>
  54. </td>
  55. <?php
  56. if (!api_is_allowed_to_edit(null, true) && $feedbackType != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
  57. <td>
  58. <?php
  59. $comm = Event::get_comments($id, $questionId);
  60. ?>
  61. </td>
  62. <?php } ?>
  63. </tr>
  64. <?php
  65. }
  66. }
  67. /**
  68. * Shows the answer to a calculated question, as HTML
  69. * @param string Answer text
  70. * @param int Exercise ID
  71. * @param int Question ID
  72. * @return void
  73. */
  74. public static function display_calculated_answer(
  75. $feedback_type,
  76. $answer,
  77. $id,
  78. $questionId,
  79. $results_disabled,
  80. $showTotalScoreAndUserChoices,
  81. $expectedChoice = '',
  82. $choice = '',
  83. $status = ''
  84. ) {
  85. if (empty($id)) {
  86. echo '<tr><td>'. Security::remove_XSS($answer).'</td>';
  87. echo '<td>'. Security::remove_XSS($choice).'</td>';
  88. echo '<td>'. Security::remove_XSS($expectedChoice).'</td>';
  89. echo '<td>'. Security::remove_XSS($status).'</td>';
  90. echo '</tr>';
  91. } else {
  92. ?>
  93. <tr>
  94. <td>
  95. <?php
  96. echo Security::remove_XSS($answer);
  97. ?>
  98. </td>
  99. <td>
  100. <?php
  101. echo Security::remove_XSS($choice);
  102. ?>
  103. </td>
  104. <td>
  105. <?php
  106. echo Security::remove_XSS($expectedChoice);
  107. ?>
  108. </td>
  109. <td>
  110. <?php
  111. echo Security::remove_XSS($status);
  112. ?>
  113. </td>
  114. <?php
  115. if (!api_is_allowed_to_edit(null, true) && $feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
  116. <td>
  117. <?php
  118. $comm = Event::get_comments($id, $questionId);
  119. ?>
  120. </td>
  121. <?php } ?>
  122. </tr>
  123. <?php
  124. }
  125. }
  126. /**
  127. * Shows the answer to a free-answer question, as HTML
  128. * @param string Answer text
  129. * @param int Exercise ID
  130. * @param int Question ID
  131. * @return void
  132. */
  133. public static function display_free_answer(
  134. $feedback_type,
  135. $answer,
  136. $exe_id,
  137. $questionId,
  138. $questionScore = null,
  139. $results_disabled = 0
  140. ) {
  141. $comments = Event::get_comments($exe_id, $questionId);
  142. if (!empty($answer)) {
  143. echo '<tr><td>';
  144. echo nl2br(Security::remove_XSS($answer));
  145. echo '</td></tr>';
  146. }
  147. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  148. if ($questionScore > 0 || !empty($comments)) {
  149. } else {
  150. echo '<tr>';
  151. echo Display::tag('td', ExerciseLib::getNotCorrectedYetText(), []);
  152. echo '</tr>';
  153. }
  154. }
  155. }
  156. /**
  157. * @param $feedback_type
  158. * @param $answer
  159. * @param $id
  160. * @param $questionId
  161. * @param null $nano
  162. * @param int $results_disabled
  163. */
  164. public static function display_oral_expression_answer($feedback_type, $answer, $id, $questionId, $fileUrl = null, $results_disabled = 0)
  165. {
  166. if (isset($fileUrl)) {
  167. echo '
  168. <tr>
  169. <td><audio src="' . $fileUrl.'" controls></audio></td>
  170. </tr>
  171. ';
  172. }
  173. if (empty($id)) {
  174. echo '<tr>';
  175. echo Display::tag('td', nl2br(Security::remove_XSS($answer)), array('width'=>'55%'));
  176. echo '</tr>';
  177. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  178. echo '<tr>';
  179. echo Display::tag('td', ExerciseLib::getNotCorrectedYetText(), array('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 nl2br(Security::remove_XSS($answer));
  189. }
  190. echo '</td>';
  191. if (!api_is_allowed_to_edit(null, true) && $feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  192. echo '<td>';
  193. $comm = Event::get_comments($id, $questionId);
  194. echo '</td>';
  195. }
  196. echo '</tr>';
  197. }
  198. }
  199. /**
  200. * Displays the answer to a hotspot question
  201. * @param int $feedback_type
  202. * @param int $answerId
  203. * @param string $answer
  204. * @param string $studentChoice
  205. * @param string $answerComment
  206. * @param int $resultsDisabled
  207. * @param int $orderColor
  208. * @param bool $showTotalScoreAndUserChoices
  209. */
  210. public static function display_hotspot_answer(
  211. $feedback_type,
  212. $answerId,
  213. $answer,
  214. $studentChoice,
  215. $answerComment,
  216. $resultsDisabled,
  217. $orderColor,
  218. $showTotalScoreAndUserChoices
  219. ) {
  220. $hide_expected_answer = false;
  221. if ($feedback_type == 0 && $resultsDisabled == 2) {
  222. $hide_expected_answer = true;
  223. }
  224. if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  225. if ($showTotalScoreAndUserChoices) {
  226. $hide_expected_answer = false;
  227. } else {
  228. $hide_expected_answer = true;
  229. }
  230. }
  231. $hotspot_colors = array(
  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. ?>
  248. <table class="data_table">
  249. <tr>
  250. <td class="text-center" width="5%">
  251. <span class="fa fa-square fa-fw fa-2x" aria-hidden="true" style="color: <?php echo $hotspot_colors[$orderColor]; ?>"></span>
  252. </td>
  253. <td class="text-left" width="25%">
  254. <?php echo "$answerId - $answer"; ?>
  255. </td>
  256. <td class="text-left" width="10%">
  257. <?php
  258. if (!$hide_expected_answer) {
  259. $my_choice = $studentChoice ? Display::label(get_lang('Correct'), 'success') : Display::label(get_lang('Incorrect'), 'danger');
  260. echo $my_choice;
  261. }
  262. ?>
  263. </td>
  264. <?php if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
  265. <td class="text-left" width="60%">
  266. <?php
  267. if ($studentChoice) {
  268. echo '<span style="font-weight: bold; color: #008000;">'.nl2br($answerComment).'</span>';
  269. }
  270. ?>
  271. </td>
  272. <?php } else { ?>
  273. <td class="text-left" width="60%">&nbsp;</td>
  274. <?php } ?>
  275. </tr>
  276. <?php
  277. }
  278. /**
  279. * Display the answers to a multiple choice question
  280. * @param int $feedback_type 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 boolean $ans Whether to show the answer comment or not
  289. * @param bool $resultsDisabled
  290. * @param bool $showTotalScoreAndUserChoices
  291. *
  292. * @return void
  293. */
  294. public static function display_unique_or_multiple_answer(
  295. $feedback_type,
  296. $answerType,
  297. $studentChoice,
  298. $answer,
  299. $answerComment,
  300. $answerCorrect,
  301. $id,
  302. $questionId,
  303. $ans,
  304. $resultsDisabled,
  305. $showTotalScoreAndUserChoices
  306. ) {
  307. $hide_expected_answer = false;
  308. if ($feedback_type == 0 && ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ONLY)) {
  309. $hide_expected_answer = true;
  310. }
  311. if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  312. if ($showTotalScoreAndUserChoices) {
  313. $hide_expected_answer = false;
  314. } else {
  315. $hide_expected_answer = true;
  316. }
  317. }
  318. $icon = in_array($answerType, array(UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION)) ? 'radio' : 'checkbox';
  319. $icon .= $studentChoice ? '_on' : '_off';
  320. $icon .= '.gif';
  321. $iconAnswer = in_array($answerType, array(UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION)) ? 'radio' : 'checkbox';
  322. $iconAnswer .= $answerCorrect ? '_on' : '_off';
  323. $iconAnswer .= '.gif';
  324. ?>
  325. <tr>
  326. <td width="5%">
  327. <?php echo Display::return_icon($icon); ?>
  328. </td>
  329. <td width="5%">
  330. <?php if (!$hide_expected_answer) {
  331. echo Display::return_icon($iconAnswer);
  332. } else {
  333. echo "-";
  334. } ?>
  335. </td>
  336. <td width="40%">
  337. <?php
  338. echo $answer;
  339. ?>
  340. </td>
  341. <?php if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
  342. <td width="20%">
  343. <?php
  344. if ($studentChoice) {
  345. if ($answerCorrect) {
  346. $color = 'green';
  347. //echo '<span style="font-weight: bold; color: #008000;">'.nl2br($answerComment).'</span>';
  348. } else {
  349. $color = 'black';
  350. //echo '<span style="font-weight: bold; color: #FF0000;">'.nl2br($answerComment).'</span>';
  351. }
  352. if ($hide_expected_answer) {
  353. $color = '';
  354. }
  355. echo '<span style="font-weight: bold; color: '.$color.';">'.nl2br($answerComment).'</span>';
  356. }
  357. ?>
  358. </td>
  359. <?php
  360. if ($ans == 1) {
  361. $comm = Event::get_comments($id, $questionId);
  362. }
  363. ?>
  364. <?php } else { ?>
  365. <td>&nbsp;</td>
  366. <?php } ?>
  367. </tr>
  368. <?php
  369. }
  370. /**
  371. * Display the answers to a multiple choice question
  372. *
  373. * @param integer Answer type
  374. * @param integer Student choice
  375. * @param string Textual answer
  376. * @param string Comment on answer
  377. * @param string Correct answer comment
  378. * @param integer Exercise ID
  379. * @param integer Question ID
  380. * @param boolean Whether to show the answer comment or not
  381. * @return void
  382. */
  383. public static function display_multiple_answer_true_false(
  384. $feedback_type,
  385. $answerType,
  386. $studentChoice,
  387. $answer,
  388. $answerComment,
  389. $answerCorrect,
  390. $id,
  391. $questionId,
  392. $ans,
  393. $resultsDisabled,
  394. $showTotalScoreAndUserChoices
  395. ) {
  396. $hide_expected_answer = false;
  397. if ($feedback_type == 0 && ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ONLY)) {
  398. $hide_expected_answer = true;
  399. }
  400. if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  401. if ($showTotalScoreAndUserChoices) {
  402. $hide_expected_answer = false;
  403. } else {
  404. $hide_expected_answer = true;
  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. //Your choice
  414. if (isset($new_options[$studentChoice])) {
  415. echo get_lang($new_options[$studentChoice]['name']);
  416. } else {
  417. echo '-';
  418. }
  419. ?>
  420. </td>
  421. <td width="5%">
  422. <?php
  423. //Expected choice
  424. if (!$hide_expected_answer) {
  425. if (isset($new_options[$answerCorrect])) {
  426. echo get_lang($new_options[$answerCorrect]['name']);
  427. } else {
  428. echo '-';
  429. }
  430. } else {
  431. echo '-';
  432. }
  433. $status = Display::label(get_lang('Incorrect'), 'danger');
  434. if (isset($new_options[$studentChoice])) {
  435. if ($studentChoice == $answerCorrect) {
  436. $status = Display::label(get_lang('Correct'), 'success');
  437. }
  438. }
  439. ?>
  440. </td>
  441. <td width="40%">
  442. <?php echo $answer; ?>
  443. </td>
  444. <?php if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
  445. <td width="20%">
  446. <?php
  447. $color = "black";
  448. if (isset($new_options[$studentChoice])) {
  449. if ($studentChoice == $answerCorrect) {
  450. $color = "green";
  451. }
  452. if ($hide_expected_answer) {
  453. $color = '';
  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. * Display the answers to a multiple choice question
  472. *
  473. * @param integer Answer type
  474. * @param integer Student choice
  475. * @param string Textual answer
  476. * @param string Comment on answer
  477. * @param string Correct answer comment
  478. * @param integer Exercise ID
  479. * @param integer Question ID
  480. * @param boolean Whether to show the answer comment or not
  481. * @return void
  482. */
  483. public static function display_multiple_answer_combination_true_false(
  484. $feedback_type,
  485. $answerType,
  486. $studentChoice,
  487. $answer,
  488. $answerComment,
  489. $answerCorrect,
  490. $id,
  491. $questionId,
  492. $ans,
  493. $resultsDisabled,
  494. $showTotalScoreAndUserChoices
  495. ) {
  496. $hide_expected_answer = false;
  497. if ($feedback_type == 0 && ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ONLY)) {
  498. $hide_expected_answer = true;
  499. }
  500. if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  501. if ($showTotalScoreAndUserChoices) {
  502. $hide_expected_answer = false;
  503. } else {
  504. $hide_expected_answer = true;
  505. }
  506. }
  507. ?>
  508. <tr>
  509. <td width="5%">
  510. <?php
  511. //Your choice
  512. $question = new MultipleAnswerCombinationTrueFalse();
  513. if (isset($question->options[$studentChoice])) {
  514. echo $question->options[$studentChoice];
  515. } else {
  516. echo $question->options[2];
  517. }
  518. ?>
  519. </td>
  520. <td width="5%">
  521. <?php
  522. //Expected choice
  523. if (!$hide_expected_answer) {
  524. if (isset($question->options[$answerCorrect])) {
  525. echo $question->options[$answerCorrect];
  526. } else {
  527. echo $question->options[2];
  528. }
  529. } else {
  530. echo '-';
  531. }
  532. ?>
  533. </td>
  534. <td width="40%">
  535. <?php
  536. //my answer
  537. echo $answer;
  538. ?>
  539. </td>
  540. <?php
  541. $status = '';
  542. if (isset($studentChoice)) {
  543. if ($studentChoice == $answerCorrect) {
  544. $status = Display::label(get_lang('Correct'), 'success');
  545. } else {
  546. $status = Display::label(get_lang('Incorrect'), 'danger');
  547. }
  548. }
  549. ?>
  550. <td width="20%">
  551. <?php
  552. echo $status;
  553. ?>
  554. </td>
  555. <?php if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
  556. <td width="20%">
  557. <?php
  558. //@todo replace this harcoded value
  559. if ($studentChoice) {
  560. $color = "black";
  561. if ($studentChoice == $answerCorrect) {
  562. $color = "green";
  563. }
  564. //echo '<span style="font-weight: bold; color: #000;">'.nl2br($answerComment).'</span>';
  565. if ($hide_expected_answer) {
  566. $color = '';
  567. }
  568. echo '<span style="font-weight: bold; color: '.$color.';">'.nl2br($answerComment).'</span>';
  569. }
  570. ?>
  571. </td>
  572. <?php
  573. if ($ans == 1) {
  574. $comm = Event::get_comments($id, $questionId);
  575. }
  576. ?>
  577. <?php } else { ?>
  578. <td>&nbsp;</td>
  579. <?php } ?>
  580. </tr>
  581. <?php
  582. }
  583. /**
  584. * @param $feedback_type
  585. * @param $exe_id
  586. * @param $questionId
  587. * @param null $questionScore
  588. * @param int $results_disabled
  589. */
  590. public static function displayAnnotationAnswer(
  591. $feedback_type,
  592. $exe_id,
  593. $questionId,
  594. $questionScore = null,
  595. $results_disabled = 0
  596. ) {
  597. $comments = Event::get_comments($exe_id, $questionId);
  598. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  599. if ($questionScore <= 0 && empty($comments)) {
  600. echo '<br />'.ExerciseLib::getNotCorrectedYetText();
  601. }
  602. }
  603. }
  604. }