exercise_show_functions.lib.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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, $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 integer Answer type
  258. * @param integer Student choice
  259. * @param string Textual answer
  260. * @param string Comment on answer
  261. * @param string Correct answer comment
  262. * @param integer Exercise ID
  263. * @param integer Question ID
  264. * @param boolean Whether to show the answer comment or not
  265. * @return void
  266. */
  267. public static function display_unique_or_multiple_answer(
  268. $feedback_type,
  269. $answerType,
  270. $studentChoice,
  271. $answer,
  272. $answerComment,
  273. $answerCorrect,
  274. $id,
  275. $questionId,
  276. $ans,
  277. $resultsDisabled,
  278. $showTotalScoreAndUserChoices
  279. ) {
  280. $hide_expected_answer = false;
  281. if ($feedback_type == 0 && ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ONLY)) {
  282. $hide_expected_answer = true;
  283. }
  284. if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  285. if ($showTotalScoreAndUserChoices) {
  286. $hide_expected_answer = false;
  287. } else {
  288. $hide_expected_answer = true;
  289. }
  290. }
  291. $icon = in_array($answerType, array(UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION)) ? 'radio':'checkbox';
  292. $icon .= $studentChoice?'_on':'_off';
  293. $icon .= '.gif';
  294. $iconAnswer = in_array($answerType, array(UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION)) ? 'radio':'checkbox';
  295. $iconAnswer .= $answerCorrect?'_on':'_off';
  296. $iconAnswer .= '.gif';
  297. ?>
  298. <tr>
  299. <td width="5%">
  300. <?php echo Display::return_icon($icon); ?>
  301. </td>
  302. <td width="5%">
  303. <?php if (!$hide_expected_answer) {
  304. echo Display::return_icon($iconAnswer);
  305. } else {
  306. echo "-";
  307. } ?>
  308. </td>
  309. <td width="40%">
  310. <?php
  311. echo $answer;
  312. ?>
  313. </td>
  314. <?php if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
  315. <td width="20%">
  316. <?php
  317. if ($studentChoice) {
  318. if ($answerCorrect) {
  319. $color = 'green';
  320. //echo '<span style="font-weight: bold; color: #008000;">'.nl2br($answerComment).'</span>';
  321. } else {
  322. $color = 'black';
  323. //echo '<span style="font-weight: bold; color: #FF0000;">'.nl2br($answerComment).'</span>';
  324. }
  325. if ($hide_expected_answer) {
  326. $color = '';
  327. }
  328. echo '<span style="font-weight: bold; color: '.$color.';">'.nl2br($answerComment).'</span>';
  329. }
  330. ?>
  331. </td>
  332. <?php
  333. if ($ans==1) {
  334. $comm = Event::get_comments($id,$questionId);
  335. }
  336. ?>
  337. <?php } else { ?>
  338. <td>&nbsp;</td>
  339. <?php } ?>
  340. </tr>
  341. <?php
  342. }
  343. /**
  344. * Display the answers to a multiple choice question
  345. *
  346. * @param integer Answer type
  347. * @param integer Student choice
  348. * @param string Textual answer
  349. * @param string Comment on answer
  350. * @param string Correct answer comment
  351. * @param integer Exercise ID
  352. * @param integer Question ID
  353. * @param boolean Whether to show the answer comment or not
  354. * @return void
  355. */
  356. public static function display_multiple_answer_true_false(
  357. $feedback_type,
  358. $answerType,
  359. $studentChoice,
  360. $answer,
  361. $answerComment,
  362. $answerCorrect,
  363. $id,
  364. $questionId,
  365. $ans,
  366. $resultsDisabled,
  367. $showTotalScoreAndUserChoices
  368. ) {
  369. $hide_expected_answer = false;
  370. if ($feedback_type == 0 && ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ONLY)) {
  371. $hide_expected_answer = true;
  372. }
  373. if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  374. if ($showTotalScoreAndUserChoices) {
  375. $hide_expected_answer = false;
  376. } else {
  377. $hide_expected_answer = true;
  378. }
  379. }
  380. ?>
  381. <tr>
  382. <td width="5%">
  383. <?php
  384. $course_id = api_get_course_int_id();
  385. $new_options = Question::readQuestionOption($questionId, $course_id);
  386. //Your choice
  387. if (isset($new_options[$studentChoice])) {
  388. echo get_lang($new_options[$studentChoice]['name']);
  389. } else {
  390. echo '-';
  391. }
  392. ?>
  393. </td>
  394. <td width="5%">
  395. <?php
  396. //Expected choice
  397. if (!$hide_expected_answer) {
  398. if (isset($new_options[$answerCorrect])) {
  399. echo get_lang($new_options[$answerCorrect]['name']);
  400. } else {
  401. echo '-';
  402. }
  403. } else {
  404. echo '-';
  405. }
  406. ?>
  407. </td>
  408. <td width="40%">
  409. <?php echo $answer; ?>
  410. </td>
  411. <?php if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
  412. <td width="20%">
  413. <?php
  414. $color = "black";
  415. if (isset($new_options[$studentChoice])) {
  416. if ($studentChoice == $answerCorrect) {
  417. $color = "green";
  418. }
  419. if ($hide_expected_answer) {
  420. $color = '';
  421. }
  422. echo '<span style="font-weight: bold; color: '.$color.';">'.nl2br($answerComment).'</span>';
  423. }
  424. ?>
  425. </td>
  426. <?php
  427. if ($ans==1) {
  428. $comm = Event::get_comments($id, $questionId);
  429. }
  430. ?>
  431. <?php } else { ?>
  432. <td>&nbsp;</td>
  433. <?php } ?>
  434. </tr>
  435. <?php
  436. }
  437. /**
  438. * Display the answers to a multiple choice question
  439. *
  440. * @param integer Answer type
  441. * @param integer Student choice
  442. * @param string Textual answer
  443. * @param string Comment on answer
  444. * @param string Correct answer comment
  445. * @param integer Exercise ID
  446. * @param integer Question ID
  447. * @param boolean Whether to show the answer comment or not
  448. * @return void
  449. */
  450. public static function display_multiple_answer_combination_true_false(
  451. $feedback_type,
  452. $answerType,
  453. $studentChoice,
  454. $answer,
  455. $answerComment,
  456. $answerCorrect,
  457. $id,
  458. $questionId,
  459. $ans,
  460. $resultsDisabled,
  461. $showTotalScoreAndUserChoices
  462. ) {
  463. $hide_expected_answer = false;
  464. if ($feedback_type == 0 && ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ONLY)) {
  465. $hide_expected_answer = true;
  466. }
  467. if ($resultsDisabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  468. if ($showTotalScoreAndUserChoices) {
  469. $hide_expected_answer = false;
  470. } else {
  471. $hide_expected_answer = true;
  472. }
  473. }
  474. ?>
  475. <tr>
  476. <td width="5%">
  477. <?php
  478. //Your choice
  479. $question = new MultipleAnswerCombinationTrueFalse();
  480. if (isset($question->options[$studentChoice])) {
  481. echo $question->options[$studentChoice];
  482. } else {
  483. echo $question->options[2];
  484. }
  485. ?>
  486. </td>
  487. <td width="5%">
  488. <?php
  489. //Expected choice
  490. if (!$hide_expected_answer) {
  491. if (isset($question->options[$answerCorrect])) {
  492. echo $question->options[$answerCorrect];
  493. } else {
  494. echo $question->options[2];
  495. }
  496. }
  497. else {
  498. echo '-';
  499. }
  500. ?>
  501. </td>
  502. <td width="40%">
  503. <?php
  504. //my answer
  505. echo $answer;
  506. ?>
  507. </td>
  508. <?php if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
  509. <td width="20%">
  510. <?php
  511. //@todo replace this harcoded value
  512. if ($studentChoice) {
  513. $color = "black";
  514. if ($studentChoice == $answerCorrect) {
  515. $color = "green";
  516. }
  517. //echo '<span style="font-weight: bold; color: #000;">'.nl2br($answerComment).'</span>';
  518. if ($hide_expected_answer) {
  519. $color = '';
  520. }
  521. echo '<span style="font-weight: bold; color: '.$color.';">'.nl2br($answerComment).'</span>';
  522. }
  523. ?>
  524. </td>
  525. <?php
  526. if ($ans==1) {
  527. $comm = Event::get_comments($id,$questionId);
  528. }
  529. ?>
  530. <?php } else { ?>
  531. <td>&nbsp;</td>
  532. <?php } ?>
  533. </tr>
  534. <?php
  535. }
  536. }