hotspot_answers.as.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. $questionId = isset($_GET['modifyAnswers']) ? (int) $_GET['modifyAnswers'] : 0;
  15. $exerciseId = isset($_GET['exerciseId']) ? (int) $_GET['exerciseId'] : 0;
  16. $exeId = isset($_GET['exeId']) ? (int) $_GET['exeId'] : 0;
  17. $userId = api_get_user_id();
  18. $courseId = api_get_course_int_id();
  19. $objExercise = new Exercise($courseId);
  20. $debug = false;
  21. if ($debug) {
  22. error_log("Call to hotspot_answers.as.php");
  23. }
  24. // Check if student has access to the hotspot answers
  25. if (!api_is_allowed_to_edit(null, true)) {
  26. if (empty($exeId)) {
  27. api_not_allowed();
  28. }
  29. $trackExerciseInfo = $objExercise->get_stat_track_exercise_info_by_exe_id($exeId);
  30. if (empty($trackExerciseInfo)) {
  31. api_not_allowed();
  32. }
  33. // Different exercise
  34. if ($exerciseId != $trackExerciseInfo['exe_exo_id']) {
  35. api_not_allowed();
  36. }
  37. // Different user
  38. if ($trackExerciseInfo['exe_user_id'] != $userId) {
  39. api_not_allowed();
  40. }
  41. }
  42. $objQuestion = Question::read($questionId, $courseId);
  43. $objExercise->read($exerciseId);
  44. if (empty($objQuestion) || empty($objExercise)) {
  45. exit;
  46. }
  47. $em = Database::getManager();
  48. $documentPath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  49. $picturePath = $documentPath.'/images';
  50. $pictureName = $objQuestion->getPictureFilename();
  51. $pictureSize = getimagesize($picturePath.'/'.$pictureName);
  52. $pictureWidth = $pictureSize[0];
  53. $pictureHeight = $pictureSize[1];
  54. $data = [];
  55. $data['type'] = 'solution';
  56. $data['lang'] = [
  57. 'Square' => get_lang('Square'),
  58. 'Ellipse' => get_lang('Ellipse'),
  59. 'Polygon' => get_lang('Polygon'),
  60. 'HotspotStatus1' => get_lang('HotspotStatus1'),
  61. 'HotspotStatus2Polygon' => get_lang('HotspotStatus2Polygon'),
  62. 'HotspotStatus2Other' => get_lang('HotspotStatus2Other'),
  63. 'HotspotStatus3' => get_lang('HotspotStatus3'),
  64. 'HotspotShowUserPoints' => get_lang('HotspotShowUserPoints'),
  65. 'ShowHotspots' => get_lang('ShowHotspots'),
  66. 'Triesleft' => get_lang('Triesleft'),
  67. 'HotspotExerciseFinished' => get_lang('HotspotExerciseFinished'),
  68. 'NextAnswer' => get_lang('NextAnswer'),
  69. 'Delineation' => get_lang('Delineation'),
  70. 'CloseDelineation' => get_lang('CloseDelineation'),
  71. 'Oar' => get_lang('Oar'),
  72. 'ClosePolygon' => get_lang('ClosePolygon'),
  73. 'DelineationStatus1' => get_lang('DelineationStatus1'),
  74. ];
  75. $data['image'] = $objQuestion->selectPicturePath();
  76. $data['image_width'] = $pictureWidth;
  77. $data['image_height'] = $pictureHeight;
  78. $data['courseCode'] = $_course['path'];
  79. $data['hotspots'] = [];
  80. $showTotalScoreAndUserChoicesInLastAttempt = true;
  81. if ($objExercise->selectResultsDisabled() == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  82. $showOnlyScore = true;
  83. $showResults = true;
  84. if ($objExercise->attempts > 0) {
  85. $attempts = Event::getExerciseResultsByUser(
  86. api_get_user_id(),
  87. $objExercise->id,
  88. $courseId,
  89. api_get_session_id(),
  90. $trackExerciseInfo['orig_lp_id'],
  91. $trackExerciseInfo['orig_lp_item_id'],
  92. 'desc'
  93. );
  94. $numberAttempts = count($attempts);
  95. $showTotalScoreAndUserChoicesInLastAttempt = false;
  96. if ($numberAttempts >= $objExercise->attempts) {
  97. $showResults = true;
  98. $showOnlyScore = false;
  99. $showTotalScoreAndUserChoicesInLastAttempt = true;
  100. }
  101. }
  102. }
  103. $hideExpectedAnswer = false;
  104. if ($objExercise->selectFeedbackType() == 0 && $objExercise->selectResultsDisabled() == 2) {
  105. $hideExpectedAnswer = true;
  106. }
  107. if ($objExercise->selectResultsDisabled() == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
  108. $hideExpectedAnswer = $showTotalScoreAndUserChoicesInLastAttempt ? false : true;
  109. }
  110. if (!$hideExpectedAnswer) {
  111. $qb = $em->createQueryBuilder();
  112. $qb
  113. ->select('a')
  114. ->from('ChamiloCourseBundle:CQuizAnswer', 'a');
  115. if ($objQuestion->selectType() == HOT_SPOT_DELINEATION) {
  116. $qb
  117. ->where($qb->expr()->eq('a.cId', $courseId))
  118. ->andWhere($qb->expr()->eq('a.questionId', $questionId))
  119. ->andWhere($qb->expr()->neq('a.hotspotType', 'noerror'))
  120. ->orderBy('a.id', 'ASC');
  121. } else {
  122. $qb
  123. ->where($qb->expr()->eq('a.cId', $courseId))
  124. ->andWhere($qb->expr()->eq('a.questionId', $questionId))
  125. ->orderBy('a.position', 'ASC');
  126. }
  127. $result = $qb->getQuery()->getResult();
  128. /** @var CQuizAnswer $hotSpotAnswer */
  129. foreach ($result as $hotSpotAnswer) {
  130. $hotSpot = [];
  131. $hotSpot['id'] = $hotSpotAnswer->getIid();
  132. $hotSpot['answer'] = $hotSpotAnswer->getAnswer();
  133. switch ($hotSpotAnswer->getHotspotType()) {
  134. case 'square':
  135. $hotSpot['type'] = 'square';
  136. break;
  137. case 'circle':
  138. $hotSpot['type'] = 'circle';
  139. break;
  140. case 'poly':
  141. $hotSpot['type'] = 'poly';
  142. break;
  143. case 'delineation':
  144. $hotSpot['type'] = 'delineation';
  145. break;
  146. case 'oar':
  147. $hotSpot['type'] = 'delineation';
  148. break;
  149. }
  150. $hotSpot['coord'] = $hotSpotAnswer->getHotspotCoordinates();
  151. $data['hotspots'][] = $hotSpot;
  152. }
  153. }
  154. $data['answers'] = [];
  155. $rs = $em
  156. ->getRepository('ChamiloCoreBundle:TrackEHotspot')
  157. ->findBy(
  158. [
  159. 'hotspotQuestionId' => $questionId,
  160. 'cId' => $courseId,
  161. 'hotspotExeId' => $exeId,
  162. ],
  163. ['hotspotAnswerId' => 'ASC']
  164. );
  165. /** @var TrackEHotspot $row */
  166. foreach ($rs as $row) {
  167. $data['answers'][] = $row->getHotspotCoordinate();
  168. }
  169. $data['done'] = 'done';
  170. header('Content-Type: application/json');
  171. echo json_encode($data);
  172. if ($debug) {
  173. error_log("---------- End call to hotspot_answers.as.php------------");
  174. }