exercise_show_functions.lib.php 17 KB

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