exercise_show_functions.lib.php 15 KB

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