exercise_show_functions.lib.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. 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. 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. static function display_oral_expression_answer($feedback_type, $answer, $id, $questionId, $nano = null)
  113. {
  114. if (isset($nano)) {
  115. echo $nano->show_audio_file();
  116. }
  117. if (empty($id)) {
  118. echo '<tr>';
  119. echo Display::tag('td', nl2br(Security::remove_XSS($answer)), array('width'=>'55%'));
  120. echo '</tr>';
  121. if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  122. echo '<tr>';
  123. echo Display::tag('td',get_lang('notCorrectedYet'), array('width'=>'45%'));
  124. echo '</tr>';
  125. } else {
  126. echo '<tr><td>&nbsp;</td></tr>';
  127. }
  128. } else {
  129. echo '<tr>';
  130. echo '<td>';
  131. if (!empty($answer)) {
  132. echo nl2br(Security::remove_XSS($answer));
  133. }
  134. echo '</td>';
  135. if (!api_is_allowed_to_edit(null,true) && $feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {
  136. echo '<td>';
  137. $comm = Event::get_comments($id,$questionId);
  138. echo '</td>';
  139. }
  140. echo '</tr>';
  141. }
  142. }
  143. /**
  144. * Displays the answer to a hotspot question
  145. *
  146. * @param int $answerId
  147. * @param string $answer
  148. * @param string $studentChoice
  149. * @param string $answerComment
  150. */
  151. static function display_hotspot_answer($feedback_type, $answerId, $answer, $studentChoice, $answerComment, $in_results_disabled)
  152. {
  153. $hide_expected_answer = false;
  154. if ($feedback_type == 0 && $in_results_disabled == 2) {
  155. $hide_expected_answer = true;
  156. }
  157. $hotspot_colors = array(
  158. "", // $i starts from 1 on next loop (ugly fix)
  159. "#4271B5",
  160. "#FE8E16",
  161. "#45C7F0",
  162. "#BCD631",
  163. "#D63173",
  164. "#D7D7D7",
  165. "#90AFDD",
  166. "#AF8640",
  167. "#4F9242",
  168. "#F4EB24",
  169. "#ED2024",
  170. "#3B3B3B",
  171. "#F7BDE2");
  172. ?>
  173. <table class="data_table">
  174. <tr>
  175. <td class="text-center" width="5%">
  176. <span class="fa fa-square fa-fw fa-2x" aria-hidden="true" style="color: <?php echo $hotspot_colors[$answerId]; ?>"></span>
  177. </td>
  178. <td class="text-left" width="25%">
  179. <?php echo "$answerId - $answer"; ?>
  180. </td>
  181. <td class="text-left" width="10%">
  182. <?php
  183. if (!$hide_expected_answer) {
  184. $my_choice = ($studentChoice)?get_lang('Correct'):get_lang('Fault');
  185. echo $my_choice;
  186. }
  187. ?>
  188. </td>
  189. <?php if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
  190. <td class="text-left" width="60%">
  191. <?php
  192. if ($studentChoice) {
  193. echo '<span style="font-weight: bold; color: #008000;">'.nl2br($answerComment).'</span>';
  194. }
  195. ?>
  196. </td>
  197. <?php } else { ?>
  198. <td class="text-left" width="60%">&nbsp;</td>
  199. <?php } ?>
  200. </tr>
  201. <?php
  202. }
  203. /**
  204. * Display the answers to a multiple choice question
  205. * @param int $feedback_type Feedback type
  206. * @param integer Answer type
  207. * @param integer Student choice
  208. * @param string Textual answer
  209. * @param string Comment on answer
  210. * @param string Correct answer comment
  211. * @param integer Exercise ID
  212. * @param integer Question ID
  213. * @param boolean Whether to show the answer comment or not
  214. * @return void
  215. */
  216. static function display_unique_or_multiple_answer(
  217. $feedback_type,
  218. $answerType,
  219. $studentChoice,
  220. $answer,
  221. $answerComment,
  222. $answerCorrect,
  223. $id,
  224. $questionId,
  225. $ans,
  226. $in_results_disabled
  227. ) {
  228. $hide_expected_answer = false;
  229. if ($feedback_type == 0 && $in_results_disabled == 2) {
  230. $hide_expected_answer = true;
  231. }
  232. ?>
  233. <tr>
  234. <td width="5%">
  235. <img src="../img/<?php echo (in_array($answerType, array(UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION))) ? 'radio':'checkbox'; echo $studentChoice?'_on':'_off'; ?>.gif"
  236. border="0" alt="" />
  237. </td>
  238. <td width="5%">
  239. <?php if (!$hide_expected_answer) { ?>
  240. <img src="../img/<?php echo (in_array($answerType, array(UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION))) ? 'radio':'checkbox'; echo $answerCorrect?'_on':'_off'; ?>.gif" border="0" alt=" " />
  241. <?php }
  242. else {
  243. echo "-";
  244. }?>
  245. </td>
  246. <td width="40%">
  247. <?php
  248. echo $answer;
  249. ?>
  250. </td>
  251. <?php if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
  252. <td width="20%">
  253. <?php
  254. if ($studentChoice) {
  255. if ($answerCorrect) {
  256. $color = 'green';
  257. //echo '<span style="font-weight: bold; color: #008000;">'.nl2br($answerComment).'</span>';
  258. } else {
  259. $color = 'black';
  260. //echo '<span style="font-weight: bold; color: #FF0000;">'.nl2br($answerComment).'</span>';
  261. }
  262. echo '<span style="font-weight: bold; color: '.$color.';">'.nl2br($answerComment).'</span>';
  263. } else {
  264. if ($answerCorrect) {
  265. //echo '<span style="font-weight: bold; color: #000;">'.nl2br($answerComment).'</span>';
  266. } else {
  267. //echo '<span style="font-weight: normal; color: #000;">'.nl2br($answerComment).'</span>';
  268. }
  269. }
  270. ?>
  271. </td>
  272. <?php
  273. if ($ans==1) {
  274. $comm = Event::get_comments($id,$questionId);
  275. }
  276. ?>
  277. <?php } else { ?>
  278. <td>&nbsp;</td>
  279. <?php } ?>
  280. </tr>
  281. <?php
  282. }
  283. /**
  284. * Display the answers to a multiple choice question
  285. *
  286. * @param integer Answer type
  287. * @param integer Student choice
  288. * @param string Textual answer
  289. * @param string Comment on answer
  290. * @param string Correct answer comment
  291. * @param integer Exercise ID
  292. * @param integer Question ID
  293. * @param boolean Whether to show the answer comment or not
  294. * @return void
  295. */
  296. static function display_multiple_answer_true_false(
  297. $feedback_type,
  298. $answerType,
  299. $studentChoice,
  300. $answer,
  301. $answerComment,
  302. $answerCorrect,
  303. $id,
  304. $questionId,
  305. $ans,
  306. $in_results_disabled
  307. ) {
  308. $hide_expected_answer = false;
  309. if ($feedback_type == 0 && $in_results_disabled == 2) {
  310. $hide_expected_answer = true;
  311. }
  312. ?>
  313. <tr>
  314. <td width="5%">
  315. <?php
  316. $question = new MultipleAnswerTrueFalse();
  317. $course_id = api_get_course_int_id();
  318. $new_options = Question::readQuestionOption($questionId, $course_id);
  319. //Your choice
  320. if (isset($new_options[$studentChoice])) {
  321. echo get_lang($new_options[$studentChoice]['name']);
  322. } else {
  323. echo '-';
  324. }
  325. ?>
  326. </td>
  327. <td width="5%">
  328. <?php
  329. //Expected choice
  330. if (!$hide_expected_answer) {
  331. if (isset($new_options[$answerCorrect])) {
  332. echo get_lang($new_options[$answerCorrect]['name']);
  333. } else {
  334. echo '-';
  335. }
  336. } else {
  337. echo '-';
  338. }
  339. ?>
  340. </td>
  341. <td width="40%">
  342. <?php echo $answer; ?>
  343. </td>
  344. <?php if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
  345. <td width="20%">
  346. <?php
  347. $color = "black";
  348. if (isset($new_options[$studentChoice])) {
  349. if ($studentChoice == $answerCorrect) {
  350. $color = "green";
  351. }
  352. echo '<span style="font-weight: bold; color: '.$color.';">'.nl2br($answerComment).'</span>';
  353. }
  354. ?>
  355. </td>
  356. <?php
  357. if ($ans==1) {
  358. $comm = Event::get_comments($id, $questionId);
  359. }
  360. ?>
  361. <?php } else { ?>
  362. <td>&nbsp;</td>
  363. <?php } ?>
  364. </tr>
  365. <?php
  366. }
  367. /**
  368. * Display the answers to a multiple choice question
  369. *
  370. * @param integer Answer type
  371. * @param integer Student choice
  372. * @param string Textual answer
  373. * @param string Comment on answer
  374. * @param string Correct answer comment
  375. * @param integer Exercise ID
  376. * @param integer Question ID
  377. * @param boolean Whether to show the answer comment or not
  378. * @return void
  379. */
  380. static function display_multiple_answer_combination_true_false(
  381. $feedback_type,
  382. $answerType,
  383. $studentChoice,
  384. $answer,
  385. $answerComment,
  386. $answerCorrect,
  387. $id,
  388. $questionId,
  389. $ans,
  390. $in_results_disabled
  391. ) {
  392. $hide_expected_answer = false;
  393. if ($feedback_type == 0 && $in_results_disabled == 2) {
  394. $hide_expected_answer = true;
  395. }
  396. ?>
  397. <tr>
  398. <td width="5%">
  399. <?php
  400. //Your choice
  401. $question = new MultipleAnswerCombinationTrueFalse();
  402. if (isset($question->options[$studentChoice])) {
  403. echo $question->options[$studentChoice];
  404. } else {
  405. echo $question->options[2];
  406. }
  407. ?>
  408. </td>
  409. <td width="5%">
  410. <?php
  411. //Expected choice
  412. if (!$hide_expected_answer) {
  413. if (isset($question->options[$answerCorrect])) {
  414. echo $question->options[$answerCorrect];
  415. } else {
  416. echo $question->options[2];
  417. }
  418. }
  419. else {
  420. echo '-';
  421. }
  422. ?>
  423. </td>
  424. <td width="40%">
  425. <?php
  426. //my answer
  427. echo $answer;
  428. ?>
  429. </td>
  430. <?php if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
  431. <td width="20%">
  432. <?php
  433. //@todo replace this harcoded value
  434. if ($studentChoice) {
  435. $color = "black";
  436. if ($studentChoice == $answerCorrect) {
  437. $color = "green";
  438. }
  439. echo '<span style="font-weight: bold; color: '.$color.';">'.nl2br($answerComment).'</span>';
  440. }
  441. if ($studentChoice == 2 || $studentChoice == '') {
  442. //echo '<span style="font-weight: bold; color: #000;">'.nl2br($answerComment).'</span>';
  443. } else {
  444. if ($studentChoice == $answerCorrect) {
  445. //echo '<span style="font-weight: bold; color: #008000;">'.nl2br($answerComment).'</span>';
  446. } else {
  447. //echo '<span style="font-weight: bold; color: #FF0000;">'.nl2br($answerComment).'</span>';
  448. }
  449. }
  450. ?>
  451. </td>
  452. <?php
  453. if ($ans==1) {
  454. $comm = Event::get_comments($id,$questionId);
  455. }
  456. ?>
  457. <?php } else { ?>
  458. <td>&nbsp;</td>
  459. <?php } ?>
  460. </tr>
  461. <?php
  462. }
  463. }