hotspot_answers.as.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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->selectFeedbackType() == 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. $hotSpotAnswerId = $hotSpotAnswer->getIid();
  148. // Show only correct hotspots
  149. /*
  150. if ($objExercise->selectResultsDisabled() == RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER) {
  151. $TBL_TRACK_HOTSPOT = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
  152. // Check auto id
  153. $sql = "SELECT hotspot_correct
  154. FROM $TBL_TRACK_HOTSPOT
  155. WHERE
  156. hotspot_exe_id = $exeId AND
  157. hotspot_question_id= $questionId AND
  158. hotspot_answer_id = ".$hotSpotAnswerId."
  159. ORDER BY hotspot_id ASC";
  160. $result = Database::query($sql);
  161. $studentChoice = false;
  162. if (Database::num_rows($result)) {
  163. $studentChoice = Database::result(
  164. $result,
  165. 0,
  166. 'hotspot_correct'
  167. );
  168. }
  169. if (!$studentChoice) {
  170. continue;
  171. }
  172. }*/
  173. $hotSpot = [];
  174. $hotSpot['id'] = $hotSpotAnswerId;
  175. $hotSpot['answer'] = $hotSpotAnswer->getAnswer();
  176. switch ($hotSpotAnswer->getHotspotType()) {
  177. case 'square':
  178. $hotSpot['type'] = 'square';
  179. break;
  180. case 'circle':
  181. $hotSpot['type'] = 'circle';
  182. break;
  183. case 'poly':
  184. $hotSpot['type'] = 'poly';
  185. break;
  186. case 'delineation':
  187. $hotSpot['type'] = 'delineation';
  188. break;
  189. case 'oar':
  190. $hotSpot['type'] = 'delineation';
  191. break;
  192. }
  193. $hotSpot['coord'] = $hotSpotAnswer->getHotspotCoordinates();
  194. $data['hotspots'][] = $hotSpot;
  195. }
  196. }
  197. $data['answers'] = [];
  198. $rs = $em
  199. ->getRepository('ChamiloCoreBundle:TrackEHotspot')
  200. ->findBy(
  201. [
  202. 'hotspotQuestionId' => $questionId,
  203. 'cId' => $courseId,
  204. 'hotspotExeId' => $exeId,
  205. ],
  206. ['hotspotAnswerId' => 'ASC']
  207. );
  208. /** @var TrackEHotspot $row */
  209. foreach ($rs as $row) {
  210. $data['answers'][] = $row->getHotspotCoordinate();
  211. }
  212. $data['done'] = 'done';
  213. header('Content-Type: application/json');
  214. echo json_encode($data);
  215. if ($debug) {
  216. error_log("---------- End call to hotspot_answers.as.php------------");
  217. }