exercise_show_functions.lib.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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', Display::return_message(get_lang('notCorrectedYet')), array());
  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', get_lang('notCorrectedYet'), 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. */
  209. public static function display_hotspot_answer(
  210. $feedback_type,
  211. $answerId,
  212. $answer,
  213. $studentChoice,
  214. $answerComment,
  215. $resultsDisabled,
  216. $orderColor,
  217. $showTotalScoreAndUserChoices
  218. ) {
  219. $hide_expected_answer = false;
  220. if ($feedback_type == 0 && $resultsDisabled == 2) {
  221. $hide_expected_answer = true;
  222. }
  223. if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  224. if ($showTotalScoreAndUserChoices) {
  225. $hide_expected_answer = false;
  226. } else {
  227. $hide_expected_answer = true;
  228. }
  229. }
  230. $hotspot_colors = array(
  231. "", // $i starts from 1 on next loop (ugly fix)
  232. "#4271B5",
  233. "#FE8E16",
  234. "#45C7F0",
  235. "#BCD631",
  236. "#D63173",
  237. "#D7D7D7",
  238. "#90AFDD",
  239. "#AF8640",
  240. "#4F9242",
  241. "#F4EB24",
  242. "#ED2024",
  243. "#3B3B3B",
  244. "#F7BDE2"
  245. );
  246. ?>
  247. <table class="data_table">
  248. <tr>
  249. <td class="text-center" width="5%">
  250. <span class="fa fa-square fa-fw fa-2x" aria-hidden="true" style="color: <?php echo $hotspot_colors[$orderColor]; ?>"></span>
  251. </td>
  252. <td class="text-left" width="25%">
  253. <?php echo "$answerId - $answer"; ?>
  254. </td>
  255. <td class="text-left" width="10%">
  256. <?php
  257. if (!$hide_expected_answer) {
  258. $my_choice = $studentChoice ? Display::label(get_lang('Correct'), 'success') : Display::label(get_lang('Incorrect'), 'danger');
  259. echo $my_choice;
  260. }
  261. ?>
  262. </td>
  263. <?php if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
  264. <td class="text-left" width="60%">
  265. <?php
  266. if ($studentChoice) {
  267. echo '<span style="font-weight: bold; color: #008000;">'.nl2br($answerComment).'</span>';
  268. }
  269. ?>
  270. </td>
  271. <?php } else { ?>
  272. <td class="text-left" width="60%">&nbsp;</td>
  273. <?php } ?>
  274. </tr>
  275. <?php
  276. }
  277. /**
  278. * Display the answers to a multiple choice question
  279. * @param int $feedback_type Feedback type
  280. * @param int $answerType Answer type
  281. * @param int $studentChoice Student choice
  282. * @param string $answer Textual answer
  283. * @param string $answerComment Comment on answer
  284. * @param string $answerCorrect Correct answer comment
  285. * @param int $id Exercise ID
  286. * @param int $questionId Question ID
  287. * @param boolean $ans Whether to show the answer comment or not
  288. * @param bool $resultsDisabled
  289. * @param bool $showTotalScoreAndUserChoices
  290. *
  291. * @return void
  292. */
  293. public static function display_unique_or_multiple_answer(
  294. $feedback_type,
  295. $answerType,
  296. $studentChoice,
  297. $answer,
  298. $answerComment,
  299. $answerCorrect,
  300. $id,
  301. $questionId,
  302. $ans,
  303. $resultsDisabled,
  304. $showTotalScoreAndUserChoices
  305. ) {
  306. $hide_expected_answer = false;
  307. if ($feedback_type == 0 && ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ONLY)) {
  308. $hide_expected_answer = true;
  309. }
  310. if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  311. if ($showTotalScoreAndUserChoices) {
  312. $hide_expected_answer = false;
  313. } else {
  314. $hide_expected_answer = true;
  315. }
  316. }
  317. $icon = in_array($answerType, array(UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION)) ? 'radio':'checkbox';
  318. $icon .= $studentChoice?'_on':'_off';
  319. $icon .= '.gif';
  320. $iconAnswer = in_array($answerType, array(UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION)) ? 'radio':'checkbox';
  321. $iconAnswer .= $answerCorrect?'_on':'_off';
  322. $iconAnswer .= '.gif';
  323. ?>
  324. <tr>
  325. <td width="5%">
  326. <?php echo Display::return_icon($icon); ?>
  327. </td>
  328. <td width="5%">
  329. <?php if (!$hide_expected_answer) {
  330. echo Display::return_icon($iconAnswer);
  331. } else {
  332. echo "-";
  333. } ?>
  334. </td>
  335. <td width="40%">
  336. <?php
  337. echo $answer;
  338. ?>
  339. </td>
  340. <?php if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
  341. <td width="20%">
  342. <?php
  343. if ($studentChoice) {
  344. if ($answerCorrect) {
  345. $color = 'green';
  346. //echo '<span style="font-weight: bold; color: #008000;">'.nl2br($answerComment).'</span>';
  347. } else {
  348. $color = 'black';
  349. //echo '<span style="font-weight: bold; color: #FF0000;">'.nl2br($answerComment).'</span>';
  350. }
  351. if ($hide_expected_answer) {
  352. $color = '';
  353. }
  354. echo '<span style="font-weight: bold; color: '.$color.';">'.nl2br($answerComment).'</span>';
  355. }
  356. ?>
  357. </td>
  358. <?php
  359. if ($ans == 1) {
  360. $comm = Event::get_comments($id, $questionId);
  361. }
  362. ?>
  363. <?php } else { ?>
  364. <td>&nbsp;</td>
  365. <?php } ?>
  366. </tr>
  367. <?php
  368. }
  369. /**
  370. * Display the answers to a multiple choice question
  371. *
  372. * @param integer Answer type
  373. * @param integer Student choice
  374. * @param string Textual answer
  375. * @param string Comment on answer
  376. * @param string Correct answer comment
  377. * @param integer Exercise ID
  378. * @param integer Question ID
  379. * @param boolean Whether to show the answer comment or not
  380. * @return void
  381. */
  382. public static function display_multiple_answer_true_false(
  383. $feedback_type,
  384. $answerType,
  385. $studentChoice,
  386. $answer,
  387. $answerComment,
  388. $answerCorrect,
  389. $id,
  390. $questionId,
  391. $ans,
  392. $resultsDisabled,
  393. $showTotalScoreAndUserChoices
  394. ) {
  395. $hide_expected_answer = false;
  396. if ($feedback_type == 0 && ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ONLY)) {
  397. $hide_expected_answer = true;
  398. }
  399. if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  400. if ($showTotalScoreAndUserChoices) {
  401. $hide_expected_answer = false;
  402. } else {
  403. $hide_expected_answer = true;
  404. }
  405. }
  406. ?>
  407. <tr>
  408. <td width="5%">
  409. <?php
  410. $course_id = api_get_course_int_id();
  411. $new_options = Question::readQuestionOption($questionId, $course_id);
  412. //Your choice
  413. if (isset($new_options[$studentChoice])) {
  414. echo get_lang($new_options[$studentChoice]['name']);
  415. } else {
  416. echo '-';
  417. }
  418. ?>
  419. </td>
  420. <td width="5%">
  421. <?php
  422. //Expected choice
  423. if (!$hide_expected_answer) {
  424. if (isset($new_options[$answerCorrect])) {
  425. echo get_lang($new_options[$answerCorrect]['name']);
  426. } else {
  427. echo '-';
  428. }
  429. } else {
  430. echo '-';
  431. }
  432. $status = Display::label(get_lang('Incorrect'), 'danger');
  433. if (isset($new_options[$studentChoice])) {
  434. if ($studentChoice == $answerCorrect) {
  435. $status = Display::label(get_lang('Correct'), 'success');
  436. }
  437. }
  438. ?>
  439. </td>
  440. <td width="40%">
  441. <?php echo $answer; ?>
  442. </td>
  443. <?php if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
  444. <td width="20%">
  445. <?php
  446. $color = "black";
  447. if (isset($new_options[$studentChoice])) {
  448. if ($studentChoice == $answerCorrect) {
  449. $color = "green";
  450. }
  451. if ($hide_expected_answer) {
  452. $color = '';
  453. }
  454. echo '<span style="font-weight: bold; color: '.$color.';">'.nl2br($answerComment).'</span>';
  455. }
  456. ?>
  457. </td>
  458. <?php
  459. if ($ans == 1) {
  460. $comm = Event::get_comments($id, $questionId);
  461. }
  462. ?>
  463. <?php } else { ?>
  464. <td>&nbsp;</td>
  465. <?php } ?>
  466. </tr>
  467. <?php
  468. }
  469. /**
  470. * Display the answers to a multiple choice question
  471. *
  472. * @param integer Answer type
  473. * @param integer Student choice
  474. * @param string Textual answer
  475. * @param string Comment on answer
  476. * @param string Correct answer comment
  477. * @param integer Exercise ID
  478. * @param integer Question ID
  479. * @param boolean Whether to show the answer comment or not
  480. * @return void
  481. */
  482. public static function display_multiple_answer_combination_true_false(
  483. $feedback_type,
  484. $answerType,
  485. $studentChoice,
  486. $answer,
  487. $answerComment,
  488. $answerCorrect,
  489. $id,
  490. $questionId,
  491. $ans,
  492. $resultsDisabled,
  493. $showTotalScoreAndUserChoices
  494. ) {
  495. $hide_expected_answer = false;
  496. if ($feedback_type == 0 && ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ONLY)) {
  497. $hide_expected_answer = true;
  498. }
  499. if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  500. if ($showTotalScoreAndUserChoices) {
  501. $hide_expected_answer = false;
  502. } else {
  503. $hide_expected_answer = true;
  504. }
  505. }
  506. ?>
  507. <tr>
  508. <td width="5%">
  509. <?php
  510. //Your choice
  511. $question = new MultipleAnswerCombinationTrueFalse();
  512. if (isset($question->options[$studentChoice])) {
  513. echo $question->options[$studentChoice];
  514. } else {
  515. echo $question->options[2];
  516. }
  517. ?>
  518. </td>
  519. <td width="5%">
  520. <?php
  521. //Expected choice
  522. if (!$hide_expected_answer) {
  523. if (isset($question->options[$answerCorrect])) {
  524. echo $question->options[$answerCorrect];
  525. } else {
  526. echo $question->options[2];
  527. }
  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. public static function displayAnnotationAnswer(
  584. $feedback_type,
  585. $exe_id,
  586. $questionId,
  587. $questionScore = null,
  588. $results_disabled = 0
  589. ) {
  590. $comments = Event::get_comments($exe_id, $questionId);
  591. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  592. if ($questionScore <= 0 && empty($comments)) {
  593. echo '<br>'.Display::return_message(get_lang('notCorrectedYet'));
  594. }
  595. }
  596. }
  597. }