exercise_submit_modal.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * @package chamilo.exercise
  6. *
  7. * @author Julio Montoya <gugli100@gmail.com>
  8. */
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. api_protect_course_script();
  11. require_once api_get_path(LIBRARY_PATH).'geometry.lib.php';
  12. /** @var Exercise $objExercise */
  13. $objExercise = Session::read('objExercise');
  14. $exerciseResult = Session::read('exerciseResult');
  15. if (empty($objExercise)) {
  16. api_not_allowed();
  17. }
  18. $feedbackType = $objExercise->getFeedbackType();
  19. $exerciseType = $objExercise->type;
  20. if (!in_array($feedbackType, [EXERCISE_FEEDBACK_TYPE_DIRECT, EXERCISE_FEEDBACK_TYPE_POPUP])) {
  21. api_not_allowed();
  22. }
  23. $learnpath_id = isset($_REQUEST['learnpath_id']) ? (int) $_REQUEST['learnpath_id'] : 0;
  24. $learnpath_item_id = isset($_REQUEST['learnpath_item_id']) ? (int) $_REQUEST['learnpath_item_id'] : 0;
  25. $questionList = Session::read('questionList');
  26. $exerciseId = (int) $_GET['exerciseId'];
  27. $questionNum = (int) $_GET['num'];
  28. $questionId = $questionList[$questionNum];
  29. $choiceValue = isset($_GET['choice']) ? $_GET['choice'] : '';
  30. $hotSpot = isset($_GET['hotspot']) ? $_GET['hotspot'] : '';
  31. $loaded = isset($_GET['loaded']);
  32. if (empty($choiceValue) && isset($exerciseResult[$questionId])) {
  33. $choiceValue = $exerciseResult[$questionId];
  34. }
  35. if (!empty($hotSpot)) {
  36. if (isset($hotSpot[$questionId])) {
  37. $hotSpot = $hotSpot[$questionId];
  38. }
  39. }
  40. if (!empty($choiceValue)) {
  41. if (isset($choiceValue[$questionId])) {
  42. $choiceValue = $choiceValue[$questionId];
  43. }
  44. }
  45. echo '<script>
  46. function tryAgain() {
  47. $(function () {
  48. $("#global-modal").modal("hide");
  49. });
  50. }
  51. function SendEx(num) {
  52. if (num == -1) {
  53. window.location.href = "exercise_result.php?'.api_get_cidreq().'&take_session=1&exerciseId='.$exerciseId.'&num="+num+"&learnpath_item_id='.$learnpath_item_id.'&learnpath_id='.$learnpath_id.'";
  54. } else {
  55. num -= 1;
  56. window.location.href = "exercise_submit.php?'.api_get_cidreq().'&tryagain=1&exerciseId='.$exerciseId.'&num="+num+"&learnpath_item_id='.$learnpath_item_id.'&learnpath_id='.$learnpath_id.'";
  57. }
  58. return false;
  59. }
  60. </script>';
  61. echo '<div id="delineation-container">';
  62. // Getting the options by js
  63. if (empty($choiceValue) && empty($hotSpot) && $loaded) {
  64. $nextQuestion = $questionNum + 1;
  65. $destinationId = isset($questionList[$nextQuestion]) ? $questionList[$nextQuestion] : -1;
  66. $icon = Display::return_icon(
  67. 'reload.png',
  68. '',
  69. ['style' => 'width:22px; height:22px; padding-left:0px;padding-right:5px;']
  70. );
  71. $links = '<a onclick="tryAgain();" href="#">'.get_lang('TryAgain').'</a>&nbsp;'.$icon.'&nbsp;';
  72. // the link to finish the test
  73. if ($destinationId == -1) {
  74. $links .= Display::return_icon(
  75. 'finish.gif',
  76. '',
  77. ['style' => 'width:22px; height:22px; padding-left:0px;padding-right:5px;']
  78. ).'<a onclick="SendEx(-1);" href="#">'.get_lang('EndActivity').'</a><br /><br />';
  79. } else {
  80. // the link to other question
  81. if (in_array($destinationId, $questionList)) {
  82. $num_value_array = array_keys($questionList, $destinationId);
  83. $icon = Display::return_icon(
  84. 'quiz.png',
  85. '',
  86. ['style' => 'padding-left:0px;padding-right:5px;']
  87. );
  88. $links .= '<a onclick="SendEx('.$num_value_array[0].');" href="#">'.
  89. get_lang('Question').' '.$num_value_array[0].'</a>&nbsp;';
  90. $links .= $icon;
  91. }
  92. }
  93. echo '<div class="row"><div class="col-md-5 col-md-offset-7"><h5 class="pull-right">'.$links.'</h5></div></div>';
  94. exit;
  95. }
  96. if (empty($choiceValue) && empty($hotSpot)) {
  97. echo "<script>
  98. // this works for only radio buttons
  99. var f = window.document.frm_exercise;
  100. var choice_js = {answers: []};
  101. var hotspot = new Array();
  102. var hotspotcoord = new Array();
  103. var counter = 0;
  104. for (var i = 0; i < f.elements.length; i++) {
  105. if (f.elements[i].type == 'radio' && f.elements[i].checked) {
  106. choice_js.answers.push(f.elements[i].value);
  107. counter ++;
  108. }
  109. if (f.elements[i].type == 'checkbox' && f.elements[i].checked) {
  110. choice_js.answers.push(f.elements[i].value);
  111. counter ++;
  112. }
  113. if (f.elements[i].type == 'hidden') {
  114. var name = f.elements[i].name;
  115. if (name.substr(0,7) == 'hotspot') {
  116. hotspot.push(f.elements[i].value);
  117. }
  118. if (name.substr(0,20) == 'hotspot_coordinates') {
  119. hotspotcoord.push(f.elements[i].value);
  120. }
  121. }
  122. }
  123. var my_choice = $('*[name*=\"choice[".$questionId."]\"]').serialize();
  124. var hotspot = $('*[name*=\"hotspot[".$questionId."]\"]').serialize();
  125. ";
  126. // IMPORTANT
  127. // This is the real redirect function
  128. $extraUrl = '&loaded=1&exerciseId='.$exerciseId.'&num='.$questionNum.'&learnpath_id='.$learnpath_id.'&learnpath_item_id='.$learnpath_item_id;
  129. $url = api_get_path(WEB_CODE_PATH).'exercise/exercise_submit_modal.php?'.api_get_cidreq().$extraUrl;
  130. echo ' url = "'.addslashes($url).'&hotspotcoord="+ hotspotcoord + "&"+ hotspot + "&"+ my_choice;';
  131. echo "$('#global-modal .modal-body').load(url);";
  132. echo '</script>';
  133. exit;
  134. }
  135. $choice = [];
  136. $choice[$questionId] = isset($choiceValue) ? $choiceValue : null;
  137. if (!is_array($exerciseResult)) {
  138. $exerciseResult = [];
  139. }
  140. // if the user has answered at least one question
  141. if (is_array($choice)) {
  142. if (in_array($exerciseType, [EXERCISE_FEEDBACK_TYPE_DIRECT, EXERCISE_FEEDBACK_TYPE_POPUP])) {
  143. // $exerciseResult receives the content of the form.
  144. // Each choice of the student is stored into the array $choice
  145. $exerciseResult = $choice;
  146. } else {
  147. // gets the question ID from $choice. It is the key of the array
  148. list($key) = array_keys($choice);
  149. // if the user didn't already answer this question
  150. if (!isset($exerciseResult[$key])) {
  151. // stores the user answer into the array
  152. $exerciseResult[$key] = $choice[$key];
  153. }
  154. }
  155. }
  156. // the script "exercise_result.php" will take the variable $exerciseResult from the session
  157. Session::write('exerciseResult', $exerciseResult);
  158. $objQuestionTmp = Question::read($questionId);
  159. $answerType = $objQuestionTmp->selectType();
  160. $showResult = false;
  161. $objAnswerTmp = new Answer($questionId, api_get_course_int_id());
  162. if ($objExercise->getFeedbackType() === EXERCISE_FEEDBACK_TYPE_DIRECT) {
  163. $showResult = true;
  164. }
  165. switch ($answerType) {
  166. case MULTIPLE_ANSWER:
  167. if (is_array($choiceValue)) {
  168. $choiceValue = array_combine(array_values($choiceValue), array_values($choiceValue));
  169. }
  170. break;
  171. case UNIQUE_ANSWER:
  172. if (is_array($choiceValue) && isset($choiceValue[0])) {
  173. $choiceValue = $choiceValue[0];
  174. }
  175. break;
  176. case DRAGGABLE:
  177. break;
  178. case HOT_SPOT_DELINEATION:
  179. $showResult = true;
  180. if (is_array($hotSpot)) {
  181. $choiceValue = isset($hotSpot[1]) ? $hotSpot[1] : '';
  182. $_SESSION['exerciseResultCoordinates'][$questionId] = $choiceValue; //needed for exercise_result.php
  183. $delineation_cord = $objAnswerTmp->selectHotspotCoordinates(1);
  184. $answer_delineation_destination = $objAnswerTmp->selectDestination(1);
  185. $_SESSION['hotspot_coord'][$questionId][1] = $delineation_cord;
  186. $_SESSION['hotspot_dest'][$questionId][1] = $answer_delineation_destination;
  187. }
  188. break;
  189. case CALCULATED_ANSWER:
  190. /*$_SESSION['calculatedAnswerId'][$questionId] = mt_rand(
  191. 1,
  192. $nbrAnswers
  193. );*/
  194. //var_dump($_SESSION['calculatedAnswerId'][$questionId]);
  195. break;
  196. }
  197. ob_start();
  198. $result = $objExercise->manage_answer(
  199. 0,
  200. $questionId,
  201. $choiceValue,
  202. 'exercise_result',
  203. null,
  204. false,
  205. false,
  206. $showResult,
  207. null,
  208. [],
  209. true,
  210. false,
  211. true
  212. );
  213. $manageAnswerHtmlContent = ob_get_clean();
  214. $contents = '';
  215. $answerCorrect = false;
  216. if (!empty($result)) {
  217. switch ($answerType) {
  218. case UNIQUE_ANSWER:
  219. case MULTIPLE_ANSWER:
  220. case DRAGGABLE:
  221. case HOT_SPOT_DELINEATION:
  222. case CALCULATED_ANSWER:
  223. if ($result['score'] == $result['weight']) {
  224. $answerCorrect = true;
  225. }
  226. break;
  227. }
  228. }
  229. if ($objExercise->getFeedbackType() === EXERCISE_FEEDBACK_TYPE_DIRECT) {
  230. if (isset($result['correct_answer_id'])) {
  231. /** @var Answer $answer */
  232. $answerId = $result['correct_answer_id'];
  233. $contents = $objAnswerTmp->selectComment($answerId);
  234. }
  235. } else {
  236. $contents = Display::return_message(get_lang('Incorrect'), 'warning');
  237. if ($answerCorrect) {
  238. $contents = Display::return_message(get_lang('Correct'), 'success');
  239. }
  240. }
  241. if ($answerType === HOT_SPOT_DELINEATION) {
  242. $contents = $manageAnswerHtmlContent;
  243. }
  244. $links = '';
  245. if ($objExercise->getFeedbackType() === EXERCISE_FEEDBACK_TYPE_DIRECT) {
  246. if (isset($choiceValue) && $choiceValue == -1) {
  247. if ($answerType != HOT_SPOT_DELINEATION) {
  248. $links .= '<a href="#" onclick="tb_remove();">'.get_lang('ChooseAnAnswer').'</a><br />';
  249. }
  250. }
  251. }
  252. $destinationId = null;
  253. if (isset($result['answer_destination'])) {
  254. $itemList = explode('@@', $result['answer_destination']);
  255. $try = $itemList[0];
  256. $lp = $itemList[1];
  257. $destinationId = $itemList[2];
  258. $url = $itemList[3];
  259. }
  260. // the link to retry the question
  261. if (isset($try) && $try == 1) {
  262. $num_value_array = array_keys($questionList, $questionId);
  263. $links .= Display:: return_icon(
  264. 'reload.gif',
  265. '',
  266. ['style' => 'padding-left:0px;padding-right:5px;']
  267. ).'<a onclick="SendEx('.$num_value_array[0].');" href="#">'.get_lang('TryAgain').'</a><br /><br />';
  268. }
  269. // the link to theory (a learning path)
  270. if (!empty($lp)) {
  271. $lp_url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.api_get_cidreq().'&action=view&lp_id='.$lp;
  272. $links .= Display:: return_icon(
  273. 'theory.gif',
  274. '',
  275. ['style' => 'padding-left:0px;padding-right:5px;']
  276. ).'<a target="_blank" href="'.$lp_url.'">'.get_lang('SeeTheory').'</a><br />';
  277. }
  278. $links .= '<br />';
  279. // the link to an external website or link
  280. if (!empty($url) && $url != -1) {
  281. $links .= Display:: return_icon(
  282. 'link.gif',
  283. '',
  284. ['style' => 'padding-left:0px;padding-right:5px;']
  285. ).'<a target="_blank" href="'.$url.'">'.get_lang('VisitUrl').'</a><br /><br />';
  286. }
  287. $nextQuestion = $questionNum + 1;
  288. $destinationId = isset($questionList[$nextQuestion]) ? $questionList[$nextQuestion] : -1;
  289. // the link to finish the test
  290. if ($destinationId == -1) {
  291. $links .= Display:: return_icon(
  292. 'finish.gif',
  293. '',
  294. ['style' => 'width:22px; height:22px; padding-left:0px;padding-right:5px;']
  295. ).'<a onclick="SendEx(-1);" href="#">'.get_lang('EndActivity').'</a><br /><br />';
  296. } else {
  297. // the link to other question
  298. if (in_array($destinationId, $questionList)) {
  299. $num_value_array = array_keys($questionList, $destinationId);
  300. $icon = Display::return_icon(
  301. 'quiz.png',
  302. '',
  303. ['style' => 'padding-left:0px;padding-right:5px;']
  304. );
  305. $links .= '<a onclick="SendEx('.$num_value_array[0].');" href="#">'.
  306. get_lang('Question').' '.$num_value_array[0].'</a>&nbsp;';
  307. $links .= $icon;
  308. }
  309. }
  310. if (!empty($links)) {
  311. echo '<div>'.$contents.'</div>';
  312. echo '<div style="padding-left: 450px"><h5>'.$links.'</h5></div>';
  313. echo '</div>';
  314. } else {
  315. $questionNum++;
  316. echo '<script>
  317. window.location.href = "exercise_submit.php?exerciseId='.$exerciseId.'&num='.$questionNum.'&'.api_get_cidreq().'";
  318. </script>';
  319. }
  320. echo '</div>';