hotspot_answers.as.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. $documentPath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  50. $picturePath = $documentPath.'/images';
  51. $pictureName = $objQuestion->getPictureFilename();
  52. $pictureSize = getimagesize($picturePath.'/'.$pictureName);
  53. $pictureWidth = $pictureSize[0];
  54. $pictureHeight = $pictureSize[1];
  55. $data = [];
  56. $data['type'] = 'solution';
  57. $data['lang'] = [
  58. 'Square' => get_lang('Square'),
  59. 'Ellipse' => get_lang('Ellipse'),
  60. 'Polygon' => get_lang('Polygon'),
  61. 'HotspotStatus1' => get_lang('HotspotStatus1'),
  62. 'HotspotStatus2Polygon' => get_lang('HotspotStatus2Polygon'),
  63. 'HotspotStatus2Other' => get_lang('HotspotStatus2Other'),
  64. 'HotspotStatus3' => get_lang('HotspotStatus3'),
  65. 'HotspotShowUserPoints' => get_lang('HotspotShowUserPoints'),
  66. 'ShowHotspots' => get_lang('ShowHotspots'),
  67. 'Triesleft' => get_lang('Triesleft'),
  68. 'HotspotExerciseFinished' => get_lang('HotspotExerciseFinished'),
  69. 'NextAnswer' => get_lang('NextAnswer'),
  70. 'Delineation' => get_lang('Delineation'),
  71. 'CloseDelineation' => get_lang('CloseDelineation'),
  72. 'Oar' => get_lang('Oar'),
  73. 'ClosePolygon' => get_lang('ClosePolygon'),
  74. 'DelineationStatus1' => get_lang('DelineationStatus1'),
  75. ];
  76. $data['image'] = $objQuestion->selectPicturePath();
  77. $data['image_width'] = $pictureWidth;
  78. $data['image_height'] = $pictureHeight;
  79. $data['courseCode'] = $_course['path'];
  80. $data['hotspots'] = [];
  81. $showTotalScoreAndUserChoicesInLastAttempt = true;
  82. if (in_array(
  83. $objExercise->selectResultsDisabled(), [
  84. RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT,
  85. RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK,
  86. ]
  87. )
  88. ) {
  89. $showOnlyScore = true;
  90. $showResults = true;
  91. $lpId = isset($trackExerciseInfo['orig_lp_id']) ? $trackExerciseInfo['orig_lp_id'] : 0;
  92. $lpItemId = isset($trackExerciseInfo['orig_lp_item_id']) ? $trackExerciseInfo['orig_lp_item_id'] : 0;
  93. if ($objExercise->attempts > 0) {
  94. $attempts = Event::getExerciseResultsByUser(
  95. api_get_user_id(),
  96. $objExercise->id,
  97. $courseId,
  98. api_get_session_id(),
  99. $lpId,
  100. $lpItemId,
  101. 'desc'
  102. );
  103. $numberAttempts = count($attempts);
  104. $showTotalScoreAndUserChoicesInLastAttempt = false;
  105. if ($numberAttempts >= $objExercise->attempts) {
  106. $showResults = true;
  107. $showOnlyScore = false;
  108. $showTotalScoreAndUserChoicesInLastAttempt = true;
  109. }
  110. }
  111. }
  112. $hideExpectedAnswer = false;
  113. if ($objExercise->getFeedbackType() == 0 &&
  114. $objExercise->selectResultsDisabled() == RESULT_DISABLE_SHOW_SCORE_ONLY
  115. ) {
  116. $hideExpectedAnswer = true;
  117. }
  118. if (in_array(
  119. $objExercise->selectResultsDisabled(), [
  120. RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT,
  121. RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK,
  122. ]
  123. )
  124. ) {
  125. $hideExpectedAnswer = $showTotalScoreAndUserChoicesInLastAttempt ? false : true;
  126. }
  127. if (!$hideExpectedAnswer) {
  128. $qb = $em->createQueryBuilder();
  129. $qb
  130. ->select('a')
  131. ->from('ChamiloCourseBundle:CQuizAnswer', 'a');
  132. if ($objQuestion->selectType() == HOT_SPOT_DELINEATION) {
  133. $qb
  134. ->where($qb->expr()->eq('a.cId', $courseId))
  135. ->andWhere($qb->expr()->eq('a.questionId', $questionId))
  136. ->andWhere($qb->expr()->neq('a.hotspotType', 'noerror'))
  137. ->orderBy('a.id', 'ASC');
  138. } else {
  139. $qb
  140. ->where($qb->expr()->eq('a.cId', $courseId))
  141. ->andWhere($qb->expr()->eq('a.questionId', $questionId))
  142. ->orderBy('a.position', 'ASC');
  143. }
  144. $result = $qb->getQuery()->getResult();
  145. /** @var CQuizAnswer $hotSpotAnswer */
  146. foreach ($result as $hotSpotAnswer) {
  147. $hotSpot = [];
  148. $hotSpot['id'] = $hotSpotAnswer->getIid();
  149. $hotSpot['answer'] = $hotSpotAnswer->getAnswer();
  150. switch ($hotSpotAnswer->getHotspotType()) {
  151. case 'square':
  152. $hotSpot['type'] = 'square';
  153. break;
  154. case 'circle':
  155. $hotSpot['type'] = 'circle';
  156. break;
  157. case 'poly':
  158. $hotSpot['type'] = 'poly';
  159. break;
  160. case 'delineation':
  161. $hotSpot['type'] = 'delineation';
  162. break;
  163. case 'oar':
  164. $hotSpot['type'] = 'delineation';
  165. break;
  166. }
  167. $hotSpot['coord'] = $hotSpotAnswer->getHotspotCoordinates();
  168. $data['hotspots'][] = $hotSpot;
  169. }
  170. }
  171. $data['answers'] = [];
  172. $rs = $em
  173. ->getRepository('ChamiloCoreBundle:TrackEHotspot')
  174. ->findBy(
  175. [
  176. 'hotspotQuestionId' => $questionId,
  177. 'cId' => $courseId,
  178. 'hotspotExeId' => $exeId,
  179. ],
  180. ['hotspotAnswerId' => 'ASC']
  181. );
  182. /** @var TrackEHotspot $row */
  183. foreach ($rs as $row) {
  184. $data['answers'][] = $row->getHotspotCoordinate();
  185. }
  186. $data['done'] = 'done';
  187. header('Content-Type: application/json');
  188. echo json_encode($data);
  189. if ($debug) {
  190. error_log("---------- End call to hotspot_answers.as.php------------");
  191. }