exercise_show_functions.lib.php 16 KB

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