hotspot_answers.as.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\TrackEHotspot;
  4. use Chamilo\CourseBundle\Entity\CQuizAnswer;
  5. /**
  6. * This file generates a json answer to the question preview.
  7. *
  8. * @package chamilo.exercise
  9. *
  10. * @author Toon Keppens, Julio Montoya adding hotspot "medical" support
  11. */
  12. require_once __DIR__.'/../inc/global.inc.php';
  13. api_protect_course_script();
  14. $_course = api_get_course_info();
  15. $questionId = isset($_GET['modifyAnswers']) ? (int) $_GET['modifyAnswers'] : 0;
  16. $exerciseId = isset($_GET['exerciseId']) ? (int) $_GET['exerciseId'] : 0;
  17. $exeId = isset($_GET['exeId']) ? (int) $_GET['exeId'] : 0;
  18. $userId = api_get_user_id();
  19. $courseId = api_get_course_int_id();
  20. $objExercise = new Exercise($courseId);
  21. $debug = false;
  22. if ($debug) {
  23. error_log("Call to hotspot_answers.as.php");
  24. }
  25. $trackExerciseInfo = $objExercise->get_stat_track_exercise_info_by_exe_id($exeId);
  26. // Check if student has access to the hotspot answers
  27. if (!api_is_allowed_to_edit(null, true)) {
  28. if (empty($exeId)) {
  29. api_not_allowed();
  30. }
  31. if (empty($trackExerciseInfo)) {
  32. api_not_allowed();
  33. }
  34. // Different exercise
  35. if ($exerciseId != $trackExerciseInfo['exe_exo_id']) {
  36. api_not_allowed();
  37. }
  38. // Different user
  39. if ($trackExerciseInfo['exe_user_id'] != $userId) {
  40. api_not_allowed();
  41. }
  42. }
  43. $objQuestion = Question::read($questionId, $objExercise->course);
  44. $objExercise->read($exerciseId);
  45. if (empty($objQuestion) || empty($objExercise)) {
  46. exit;
  47. }
  48. $em = Database::getManager();
  49. $picture = $objQuestion->getPicture();
  50. $pictureWidth = $picture->getResourceNode()->getResourceFile()->getWidth();
  51. $pictureHeight = $picture->getResourceNode()->getResourceFile()->getHeight();
  52. $data = [];
  53. $data['type'] = 'solution';
  54. $data['lang'] = [
  55. 'Square' => get_lang('Square'),
  56. 'Ellipse' => get_lang('Ellipse'),
  57. 'Polygon' => get_lang('Polygon'),
  58. 'HotspotStatus1' => get_lang('HotspotStatus1'),
  59. 'HotspotStatus2Polygon' => get_lang('HotspotStatus2Polygon'),
  60. 'HotspotStatus2Other' => get_lang('HotspotStatus2Other'),
  61. 'HotspotStatus3' => get_lang('HotspotStatus3'),
  62. 'HotspotShowUserPoints' => get_lang('HotspotShowUserPoints'),
  63. 'ShowHotspots' => get_lang('ShowHotspots'),
  64. 'Triesleft' => get_lang('Triesleft'),
  65. 'HotspotExerciseFinished' => get_lang('HotspotExerciseFinished'),
  66. 'NextAnswer' => get_lang('NextAnswer'),
  67. 'Delineation' => get_lang('Delineation'),
  68. 'CloseDelineation' => get_lang('CloseDelineation'),
  69. 'Oar' => get_lang('Oar'),
  70. 'ClosePolygon' => get_lang('ClosePolygon'),
  71. 'DelineationStatus1' => get_lang('DelineationStatus1'),
  72. ];
  73. $data['image'] = $objQuestion->selectPicturePath();
  74. $data['image_width'] = $pictureWidth;
  75. $data['image_height'] = $pictureHeight;
  76. $data['courseCode'] = $_course['path'];
  77. $data['hotspots'] = [];
  78. $showTotalScoreAndUserChoicesInLastAttempt = true;
  79. if (in_array(
  80. $objExercise->selectResultsDisabled(), [
  81. RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT,
  82. RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK,
  83. ]
  84. )
  85. ) {
  86. $showOnlyScore = true;
  87. $showResults = true;
  88. $lpId = isset($trackExerciseInfo['orig_lp_id']) ? $trackExerciseInfo['orig_lp_id'] : 0;
  89. $lpItemId = isset($trackExerciseInfo['orig_lp_item_id']) ? $trackExerciseInfo['orig_lp_item_id'] : 0;
  90. if ($objExercise->attempts > 0) {
  91. $attempts = Event::getExerciseResultsByUser(
  92. api_get_user_id(),
  93. $objExercise->id,
  94. $courseId,
  95. api_get_session_id(),
  96. $lpId,
  97. $lpItemId,
  98. 'desc'
  99. );
  100. $numberAttempts = count($attempts);
  101. $showTotalScoreAndUserChoicesInLastAttempt = false;
  102. if ($numberAttempts >= $objExercise->attempts) {
  103. $showResults = true;
  104. $showOnlyScore = false;
  105. $showTotalScoreAndUserChoicesInLastAttempt = true;
  106. }
  107. }
  108. }
  109. $hideExpectedAnswer = false;
  110. if ($objExercise->getFeedbackType() == 0 &&
  111. $objExercise->selectResultsDisabled() == RESULT_DISABLE_SHOW_SCORE_ONLY
  112. ) {
  113. $hideExpectedAnswer = true;
  114. }
  115. if (in_array(
  116. $objExercise->selectResultsDisabled(), [
  117. RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT,
  118. RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK,
  119. ]
  120. )
  121. ) {
  122. $hideExpectedAnswer = $showTotalScoreAndUserChoicesInLastAttempt ? false : true;
  123. }
  124. if (!$hideExpectedAnswer) {
  125. $qb = $em->createQueryBuilder();
  126. $qb
  127. ->select('a')
  128. ->from('ChamiloCourseBundle:CQuizAnswer', 'a');
  129. if ($objQuestion->selectType() == HOT_SPOT_DELINEATION) {
  130. $qb
  131. ->where($qb->expr()->eq('a.cId', $courseId))
  132. ->andWhere($qb->expr()->eq('a.questionId', $questionId))
  133. ->andWhere($qb->expr()->neq('a.hotspotType', 'noerror'))
  134. ->orderBy('a.id', 'ASC');
  135. } else {
  136. $qb
  137. ->where($qb->expr()->eq('a.cId', $courseId))
  138. ->andWhere($qb->expr()->eq('a.questionId', $questionId))
  139. ->orderBy('a.position', 'ASC');
  140. }
  141. $result = $qb->getQuery()->getResult();
  142. /** @var CQuizAnswer $hotSpotAnswer */
  143. foreach ($result as $hotSpotAnswer) {
  144. $hotSpot = [];
  145. $hotSpot['id'] = $hotSpotAnswer->getIid();
  146. $hotSpot['answer'] = $hotSpotAnswer->getAnswer();
  147. switch ($hotSpotAnswer->getHotspotType()) {
  148. case 'square':
  149. $hotSpot['type'] = 'square';
  150. break;
  151. case 'circle':
  152. $hotSpot['type'] = 'circle';
  153. break;
  154. case 'poly':
  155. $hotSpot['type'] = 'poly';
  156. break;
  157. case 'delineation':
  158. $hotSpot['type'] = 'delineation';
  159. break;
  160. case 'oar':
  161. $hotSpot['type'] = 'delineation';
  162. break;
  163. }
  164. $hotSpot['coord'] = $hotSpotAnswer->getHotspotCoordinates();
  165. $data['hotspots'][] = $hotSpot;
  166. }
  167. }
  168. $data['answers'] = [];
  169. $rs = $em
  170. ->getRepository('ChamiloCoreBundle:TrackEHotspot')
  171. ->findBy(
  172. [
  173. 'hotspotQuestionId' => $questionId,
  174. 'course' => $courseId,
  175. 'hotspotExeId' => $exeId,
  176. ],
  177. ['hotspotAnswerId' => 'ASC']
  178. );
  179. /** @var TrackEHotspot $row */
  180. foreach ($rs as $row) {
  181. $data['answers'][] = $row->getHotspotCoordinate();
  182. }
  183. $data['done'] = 'done';
  184. header('Content-Type: application/json');
  185. echo json_encode($data);
  186. if ($debug) {
  187. error_log("---------- End call to hotspot_answers.as.php------------");
  188. }