exercise_show_functions.lib.php 14 KB

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