exercise_show_functions.lib.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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(
  42. $answer,
  43. $feedbackType,
  44. $resultsDisabled,
  45. $showTotalScoreAndUserChoices
  46. );
  47. // ofaj
  48. /*if (strpos($originalStudentAnswer, 'font color') !== false) {
  49. $answerHTML = $originalStudentAnswer;
  50. }*/
  51. if (empty($id)) {
  52. echo '<tr><td>';
  53. echo Security::remove_XSS($answerHTML, COURSEMANAGERLOWSECURITY);
  54. echo '</td></tr>';
  55. } else {
  56. echo '<tr><td>';
  57. echo Security::remove_XSS($answerHTML, COURSEMANAGERLOWSECURITY);
  58. echo '</td>';
  59. if (!api_is_allowed_to_edit(null, true) && $feedbackType != EXERCISE_FEEDBACK_TYPE_EXAM) {
  60. echo '<td>';
  61. $comm = Event::get_comments($id, $questionId);
  62. echo '</td>';
  63. }
  64. echo '</tr>';
  65. }
  66. }
  67. /**
  68. * Shows the answer to a calculated question, as HTML
  69. * @param Exercise $exercise
  70. * @param string Answer text
  71. * @param int Exercise ID
  72. * @param int Question ID
  73. * @return void
  74. */
  75. public static function display_calculated_answer(
  76. $exercise,
  77. $feedback_type,
  78. $answer,
  79. $id,
  80. $questionId,
  81. $results_disabled,
  82. $showTotalScoreAndUserChoices,
  83. $expectedChoice = '',
  84. $choice = '',
  85. $status = ''
  86. ) {
  87. if ($exercise->showExpectedChoice()) {
  88. if (empty($id)) {
  89. echo '<tr><td>'. Security::remove_XSS($answer).'</td>';
  90. echo '<td>'. Security::remove_XSS($choice).'</td>';
  91. echo '<td>'. Security::remove_XSS($expectedChoice).'</td>';
  92. echo '<td>'. Security::remove_XSS($status).'</td>';
  93. echo '</tr>';
  94. } else {
  95. echo '<tr><td>';
  96. echo Security::remove_XSS($answer);
  97. echo '</td><td>';
  98. echo Security::remove_XSS($choice);
  99. echo '</td><td>';
  100. echo Security::remove_XSS($expectedChoice);
  101. echo '</td><td>';
  102. echo Security::remove_XSS($status);
  103. echo '</td>';
  104. if (!api_is_allowed_to_edit(null, true) && $feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  105. echo '<td>';
  106. $comm = Event::get_comments($id, $questionId);
  107. echo '</td>';
  108. }
  109. echo '</tr>';
  110. }
  111. } else {
  112. if (empty($id)) {
  113. echo '<tr><td>'.Security::remove_XSS($answer).'</td></tr>';
  114. } else {
  115. echo '<tr><td>';
  116. echo Security::remove_XSS($answer);
  117. if (!api_is_allowed_to_edit(null, true) && $feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  118. echo '<td>';
  119. $comm = Event::get_comments($id, $questionId);
  120. echo '</td>';
  121. }
  122. echo '</tr>';
  123. }
  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 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 $fileUrl
  162. * @param int $results_disabled
  163. * @param int $questionScore
  164. */
  165. public static function display_oral_expression_answer(
  166. $feedback_type,
  167. $answer,
  168. $id,
  169. $questionId,
  170. $fileUrl = null,
  171. $results_disabled = 0,
  172. $questionScore = 0
  173. ) {
  174. if (isset($fileUrl)) {
  175. echo '
  176. <tr>
  177. <td><audio src="' . $fileUrl.'" controls></audio></td>
  178. </tr>
  179. ';
  180. }
  181. if (empty($id)) {
  182. echo '<tr>';
  183. echo Display::tag('td', Security::remove_XSS($answer), ['width'=>'55%']);
  184. echo '</tr>';
  185. if (!$questionScore && $feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  186. echo '<tr>';
  187. echo Display::tag('td', ExerciseLib::getNotCorrectedYetText(), ['width'=>'45%']);
  188. echo '</tr>';
  189. } else {
  190. echo '<tr><td>&nbsp;</td></tr>';
  191. }
  192. } else {
  193. echo '<tr>';
  194. echo '<td>';
  195. if (!empty($answer)) {
  196. echo Security::remove_XSS($answer);
  197. }
  198. echo '</td>';
  199. if (!api_is_allowed_to_edit(null, true) && $feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  200. echo '<td>';
  201. $comm = Event::get_comments($id, $questionId);
  202. echo '</td>';
  203. }
  204. echo '</tr>';
  205. }
  206. }
  207. /**
  208. * Displays the answer to a hotspot question
  209. * @param int $feedback_type
  210. * @param int $answerId
  211. * @param string $answer
  212. * @param string $studentChoice
  213. * @param string $answerComment
  214. * @param int $resultsDisabled
  215. * @param int $orderColor
  216. * @param bool $showTotalScoreAndUserChoices
  217. */
  218. public static function display_hotspot_answer(
  219. $feedback_type,
  220. $answerId,
  221. $answer,
  222. $studentChoice,
  223. $answerComment,
  224. $resultsDisabled,
  225. $orderColor,
  226. $showTotalScoreAndUserChoices
  227. ) {
  228. $hide_expected_answer = false;
  229. if ($feedback_type == 0 && $resultsDisabled == 2) {
  230. $hide_expected_answer = true;
  231. }
  232. if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  233. $hide_expected_answer = true;
  234. if ($showTotalScoreAndUserChoices) {
  235. $hide_expected_answer = false;
  236. }
  237. }
  238. $hotspot_colors = [
  239. "", // $i starts from 1 on next loop (ugly fix)
  240. "#4271B5",
  241. "#FE8E16",
  242. "#45C7F0",
  243. "#BCD631",
  244. "#D63173",
  245. "#D7D7D7",
  246. "#90AFDD",
  247. "#AF8640",
  248. "#4F9242",
  249. "#F4EB24",
  250. "#ED2024",
  251. "#3B3B3B",
  252. "#F7BDE2"
  253. ];
  254. echo '<table class="data_table"><tr>';
  255. echo '<td class="text-center" width="5%">';
  256. echo '<span class="fa fa-square fa-fw fa-2x" aria-hidden="true" style="color:'.$hotspot_colors[$orderColor].'"></span>';
  257. echo '</td>';
  258. echo '<td class="text-left" width="25%">';
  259. echo "$answerId - $answer";
  260. echo '</td>';
  261. echo '<td class="text-left" width="10%">';
  262. if (!$hide_expected_answer) {
  263. $status = Display::label(get_lang('Incorrect'), 'danger');
  264. if ($studentChoice) {
  265. $status = Display::label(get_lang('Correct'), 'success');
  266. }
  267. echo $status;
  268. }
  269. echo '</td>';
  270. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  271. echo '<td class="text-left" width="60%">';
  272. if ($studentChoice) {
  273. echo '<span style="font-weight: bold; color: #008000;">'.nl2br($answerComment).'</span>';
  274. }
  275. echo '</td>';
  276. } else {
  277. echo '<td class="text-left" width="60%">&nbsp;</td>';
  278. }
  279. echo '</tr>';
  280. }
  281. /**
  282. * Display the answers to a multiple choice question
  283. * @param Exercise $exercise
  284. * @param int $feedback_type Feedback type
  285. * @param int $answerType Answer type
  286. * @param int $studentChoice Student choice
  287. * @param string $answer Textual answer
  288. * @param string $answerComment Comment on answer
  289. * @param string $answerCorrect Correct answer comment
  290. * @param int $id Exercise ID
  291. * @param int $questionId Question ID
  292. * @param boolean $ans Whether to show the answer comment or not
  293. * @param bool $resultsDisabled
  294. * @param bool $showTotalScoreAndUserChoices
  295. * @param bool $export
  296. * @return void
  297. */
  298. public static function display_unique_or_multiple_answer(
  299. $exercise,
  300. $feedback_type,
  301. $answerType,
  302. $studentChoice,
  303. $answer,
  304. $answerComment,
  305. $answerCorrect,
  306. $id,
  307. $questionId,
  308. $ans,
  309. $resultsDisabled,
  310. $showTotalScoreAndUserChoices,
  311. $export = false
  312. ) {
  313. if ($export) {
  314. $answer = strip_tags_blacklist($answer, ['title', 'head']);
  315. // Fix answers that contains this tags
  316. $tags = [
  317. '<html>',
  318. '</html>',
  319. '<body>',
  320. '</body>'
  321. ];
  322. $answer = str_replace($tags, '', $answer);
  323. }
  324. $hide_expected_answer = false;
  325. if ($feedback_type == 0 && ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ONLY)) {
  326. $hide_expected_answer = true;
  327. }
  328. if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  329. $hide_expected_answer = true;
  330. if ($showTotalScoreAndUserChoices) {
  331. $hide_expected_answer = false;
  332. }
  333. }
  334. $icon = in_array($answerType, [UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION]) ? 'radio' : 'checkbox';
  335. $icon .= $studentChoice ? '_on' : '_off';
  336. $icon .= '.png';
  337. $iconAnswer = in_array($answerType, [UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION]) ? 'radio' : 'checkbox';
  338. $iconAnswer .= $answerCorrect ? '_on' : '_off';
  339. $iconAnswer .= '.png';
  340. echo '<tr>';
  341. echo '<td width="5%">';
  342. echo Display::return_icon($icon, null, null, ICON_SIZE_TINY);
  343. echo '</td><td width="5%">';
  344. if (!$hide_expected_answer) {
  345. echo Display::return_icon($iconAnswer, null, null, ICON_SIZE_TINY);
  346. } else {
  347. echo "-";
  348. }
  349. echo '</td><td width="40%">';
  350. echo $answer;
  351. echo '</td>';
  352. if ($exercise->showExpectedChoice()) {
  353. $status = Display::label(get_lang('Incorrect'), 'danger');
  354. if ($studentChoice) {
  355. if ($answerCorrect) {
  356. $status = Display::label(get_lang('Correct'), 'success');
  357. }
  358. }
  359. echo '<td width="20%">';
  360. echo $status;
  361. echo '</td>';
  362. }
  363. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  364. echo '<td width="20%">';
  365. if ($studentChoice) {
  366. $color = 'black';
  367. if ($answerCorrect) {
  368. $color = 'green';
  369. }
  370. if ($hide_expected_answer) {
  371. $color = '';
  372. }
  373. echo '<span style="font-weight: bold; color: '.$color.';">'.strip_tags($answerComment).'</span>';
  374. }
  375. echo '</td>';
  376. if ($ans == 1) {
  377. $comm = Event::get_comments($id, $questionId);
  378. }
  379. } else {
  380. echo '<td>&nbsp;</td>';
  381. }
  382. echo '</tr>';
  383. }
  384. /**
  385. * Display the answers to a multiple choice question
  386. *
  387. * @param Exercise $exercise
  388. * @param integer Answer type
  389. * @param integer Student choice
  390. * @param string Textual answer
  391. * @param string Comment on answer
  392. * @param string Correct answer comment
  393. * @param integer Exercise ID
  394. * @param integer Question ID
  395. * @param boolean Whether to show the answer comment or not
  396. * @return void
  397. */
  398. public static function display_multiple_answer_true_false(
  399. $exercise,
  400. $feedback_type,
  401. $answerType,
  402. $studentChoice,
  403. $answer,
  404. $answerComment,
  405. $answerCorrect,
  406. $id,
  407. $questionId,
  408. $ans,
  409. $resultsDisabled,
  410. $showTotalScoreAndUserChoices
  411. ) {
  412. $hide_expected_answer = false;
  413. if ($feedback_type == 0 && ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ONLY)) {
  414. $hide_expected_answer = true;
  415. }
  416. if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  417. $hide_expected_answer = true;
  418. if ($showTotalScoreAndUserChoices) {
  419. $hide_expected_answer = false;
  420. }
  421. }
  422. echo '<tr><td width="5%">';
  423. $course_id = api_get_course_int_id();
  424. $new_options = Question::readQuestionOption($questionId, $course_id);
  425. // Your choice
  426. if (isset($new_options[$studentChoice])) {
  427. echo get_lang($new_options[$studentChoice]['name']);
  428. } else {
  429. echo '-';
  430. }
  431. echo '</td><td width="5%">';
  432. // Expected choice
  433. if (!$hide_expected_answer) {
  434. if (isset($new_options[$answerCorrect])) {
  435. echo get_lang($new_options[$answerCorrect]['name']);
  436. } else {
  437. echo '-';
  438. }
  439. } else {
  440. echo '-';
  441. }
  442. echo '</td><td width="40%">';
  443. echo $answer;
  444. echo '</td>';
  445. if ($exercise->showExpectedChoice()) {
  446. $status = Display::label(get_lang('Incorrect'), 'danger');
  447. if (isset($new_options[$studentChoice])) {
  448. if ($studentChoice == $answerCorrect) {
  449. $status = Display::label(get_lang('Correct'), 'success');
  450. }
  451. }
  452. echo '<td width="20%">';
  453. echo $status;
  454. echo '</td>';
  455. }
  456. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  457. echo '<td width="20%">';
  458. $color = "black";
  459. if (isset($new_options[$studentChoice])) {
  460. if ($studentChoice == $answerCorrect) {
  461. $color = "green";
  462. }
  463. if ($hide_expected_answer) {
  464. $color = '';
  465. }
  466. echo '<span style="font-weight: bold; color: '.$color.';">'.nl2br($answerComment).'</span>';
  467. }
  468. echo '</td>';
  469. if ($ans == 1) {
  470. $comm = Event::get_comments($id, $questionId);
  471. }
  472. } else {
  473. echo '<td>&nbsp;</td>';
  474. }
  475. echo '</tr>';
  476. }
  477. /**
  478. * Display the answers to a multiple choice question
  479. *
  480. * @param Exercise $exercise
  481. * @param integer Answer type
  482. * @param integer Student choice
  483. * @param string Textual answer
  484. * @param string Comment on answer
  485. * @param string Correct answer comment
  486. * @param integer Exercise ID
  487. * @param integer Question ID
  488. * @param boolean Whether to show the answer comment or not
  489. * @return void
  490. */
  491. public static function display_multiple_answer_combination_true_false(
  492. $exercise,
  493. $feedback_type,
  494. $answerType,
  495. $studentChoice,
  496. $answer,
  497. $answerComment,
  498. $answerCorrect,
  499. $id,
  500. $questionId,
  501. $ans,
  502. $resultsDisabled,
  503. $showTotalScoreAndUserChoices
  504. ) {
  505. $hide_expected_answer = false;
  506. if ($feedback_type == 0 && ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ONLY)) {
  507. $hide_expected_answer = true;
  508. }
  509. if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  510. $hide_expected_answer = true;
  511. if ($showTotalScoreAndUserChoices) {
  512. $hide_expected_answer = false;
  513. }
  514. }
  515. echo '<tr><td width="5%">';
  516. // Your choice
  517. $question = new MultipleAnswerCombinationTrueFalse();
  518. if (isset($question->options[$studentChoice])) {
  519. echo $question->options[$studentChoice];
  520. } else {
  521. echo $question->options[2];
  522. }
  523. echo '</td><td width="5%">';
  524. // Expected choice
  525. if (!$hide_expected_answer) {
  526. if (isset($question->options[$answerCorrect])) {
  527. echo $question->options[$answerCorrect];
  528. } else {
  529. echo $question->options[2];
  530. }
  531. } else {
  532. echo '-';
  533. }
  534. echo '</td>';
  535. echo '<td width="40%">';
  536. // my answer
  537. echo $answer;
  538. echo '</td>';
  539. if ($exercise->showExpectedChoice()) {
  540. $status = '';
  541. if (isset($studentChoice)) {
  542. $status = Display::label(get_lang('Incorrect'), 'danger');
  543. if ($studentChoice == $answerCorrect) {
  544. $status = Display::label(get_lang('Correct'), 'success');
  545. }
  546. }
  547. echo '<td width="20%">';
  548. echo $status;
  549. echo '</td>';
  550. }
  551. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  552. echo '<td width="20%">';
  553. //@todo replace this harcoded value
  554. if ($studentChoice) {
  555. $color = "black";
  556. if ($studentChoice == $answerCorrect) {
  557. $color = "green";
  558. }
  559. if ($hide_expected_answer) {
  560. $color = '';
  561. }
  562. echo '<span style="font-weight: bold; color: '.$color.';">'.nl2br($answerComment).'</span>';
  563. }
  564. echo '</td>';
  565. if ($ans == 1) {
  566. $comm = Event::get_comments($id, $questionId);
  567. }
  568. } else {
  569. echo '<td>&nbsp;</td>';
  570. }
  571. echo '</tr>';
  572. }
  573. /**
  574. * @param $feedback_type
  575. * @param $exe_id
  576. * @param $questionId
  577. * @param null $questionScore
  578. * @param int $results_disabled
  579. */
  580. public static function displayAnnotationAnswer(
  581. $feedback_type,
  582. $exe_id,
  583. $questionId,
  584. $questionScore = null,
  585. $results_disabled = 0
  586. ) {
  587. $comments = Event::get_comments($exe_id, $questionId);
  588. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  589. if ($questionScore <= 0 && empty($comments)) {
  590. echo '<br />'.ExerciseLib::getNotCorrectedYetText();
  591. }
  592. }
  593. }
  594. }