exercise_submit.php 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Exercise submission
  6. * This script allows to run an exercise. According to the exercise type, questions
  7. * can be on an unique page, or one per page with a Next button.
  8. *
  9. * One exercise may contain different types of answers (unique or multiple selection,
  10. * matching, fill in blanks, free answer, hot-spot).
  11. *
  12. * Questions are selected randomly or not.
  13. *
  14. * When the user has answered all questions and clicks on the button "Ok",
  15. * it goes to exercise_result.php
  16. *
  17. * Notice : This script is also used to show a question before modifying it by
  18. * the administrator
  19. * @package chamilo.exercise
  20. * @author Olivier Brouckaert
  21. * @author Julio Montoya <gugli100@gmail.com>
  22. * Fill in blank option added (2008)
  23. * Cleaning exercises (2010),
  24. * Adding hotspot delineation support (2011)
  25. * Adding reminder + ajax support (2011)
  26. * Modified by hubert.borderiou (2011-10-21 question category)
  27. */
  28. $current_course_tool = TOOL_QUIZ;
  29. $this_section = SECTION_COURSES;
  30. $debug = false;
  31. // Notice for unauthorized people.
  32. api_protect_course_script(true);
  33. $origin = isset($_REQUEST['origin']) ? Security::remove_XSS($_REQUEST['origin']) : '';
  34. $is_allowedToEdit = api_is_allowed_to_edit(null,true);
  35. $glossaryExtraTools = api_get_setting('glossary.show_glossary_in_extra_tools');
  36. $showGlossary = in_array($glossaryExtraTools, array('true', 'exercise', 'exercise_and_lp'));
  37. if ($origin == 'learnpath') {
  38. $showGlossary = in_array($glossaryExtraTools, array('true', 'lp', 'exercise_and_lp'));
  39. }
  40. if ($showGlossary) {
  41. $htmlHeadXtra[] = '<script type="text/javascript" src="'.api_get_path(WEB_LIBRARY_JS_PATH).'jquery.highlight.js"></script>';
  42. $htmlHeadXtra[] = '<script type="text/javascript" src="'.api_get_path(WEB_CODE_PATH).'glossary/glossary.js.php?add_ready=1&'.api_get_cidreq().'"></script>';
  43. }
  44. $htmlHeadXtra[] = api_get_js('components/jsplumb/dist/js/jsPlumb-2.0.4.js');
  45. $htmlHeadXtra[] = api_get_js('components/jquery.ui.touch/jquery.ui.touch.js');
  46. $htmlHeadXtra[] = api_get_js('components/xcolor/jquery.xcolor.js');
  47. //This library is necessary for the time control feature
  48. //tmlHeadXtra[] = api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/epiclock/stylesheet/jquery.epiclock.css');
  49. $htmlHeadXtra[] = api_get_css(
  50. 'js/epiclock/renderers/minute/epiclock.minute.css'
  51. );
  52. $htmlHeadXtra[] = api_get_js('js/epiclock/javascript/jquery.dateformat.min.js');
  53. $htmlHeadXtra[] = api_get_js('js/epiclock/javascript/jquery.epiclock.min.js');
  54. $htmlHeadXtra[] = api_get_js('js/epiclock/renderers/minute/epiclock.minute.js');
  55. $template = \Chamilo\CoreBundle\Framework\Container::getTwig();
  56. // General parameters passed via POST/GET
  57. $learnpath_id = isset($_REQUEST['learnpath_id']) ? intval($_REQUEST['learnpath_id']) : 0;
  58. $learnpath_item_id = isset($_REQUEST['learnpath_item_id']) ? intval($_REQUEST['learnpath_item_id']) : 0;
  59. $learnpath_item_view_id = isset($_REQUEST['learnpath_item_view_id']) ? intval($_REQUEST['learnpath_item_view_id']) : 0;
  60. $reminder = isset($_REQUEST['reminder']) ? intval($_REQUEST['reminder']) : 0;
  61. $remind_question_id = isset($_REQUEST['remind_question_id']) ? intval($_REQUEST['remind_question_id']) : 0;
  62. $exerciseId = isset($_REQUEST['exerciseId']) ? intval($_REQUEST['exerciseId']) : 0;
  63. $formSent = isset($_REQUEST['formSent']) ? $_REQUEST['formSent'] : null;
  64. $exerciseResult = isset($_REQUEST['exerciseResult']) ? $_REQUEST['exerciseResult'] : null;
  65. $exerciseResultCoordinates = isset($_REQUEST['exerciseResultCoordinates']) ? $_REQUEST['exerciseResultCoordinates'] : null;
  66. $choice = isset($_REQUEST['choice']) ? $_REQUEST['choice'] : null;
  67. $choice = empty($choice) ? isset($_REQUEST['choice2']) ? $_REQUEST['choice2'] : null : null;
  68. //From submit modal
  69. $current_question = isset($_REQUEST['num']) ? intval($_REQUEST['num']) : null;
  70. $currentAnswer = isset($_REQUEST['num_answer']) ? intval($_REQUEST['num_answer']) : null;
  71. $endExercise = isset($_REQUEST['end_exercise']) && $_REQUEST['end_exercise'] == 1 ? true : false;
  72. //Error message
  73. $error = '';
  74. //Table calls
  75. $exercise_attempt_table = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  76. /* Teacher takes an exam and want to see a preview,
  77. we delete the objExercise from the session in order to get the latest
  78. changes in the exercise */
  79. if (api_is_allowed_to_edit(null, true) && isset($_GET['preview']) && $_GET['preview'] == 1 ) {
  80. Session::erase('objExercise');
  81. }
  82. // 1. Loading the $objExercise variable
  83. $exerciseFromSession = Session::read('objExercise');
  84. if (!isset($exerciseFromSession) ||
  85. $exerciseFromSession->id != $_REQUEST['exerciseId']
  86. ) {
  87. // Construction of Exercise
  88. $objExercise = new Exercise();
  89. Session::write('firstTime', true);
  90. if ($debug) {error_log('1. Setting the $objExercise variable'); };
  91. Session::erase('questionList');
  92. // if the specified exercise doesn't exist or is disabled
  93. if (!$objExercise->read($exerciseId) ||
  94. (!$objExercise->selectStatus() && !$is_allowedToEdit && $origin != 'learnpath')
  95. ) {
  96. if ($debug) {error_log('1.1. Error while reading the exercise'); };
  97. unset ($objExercise);
  98. $error = get_lang('ExerciseNotFound');
  99. } else {
  100. // Saves the object into the session
  101. Session::write('objExercise', $objExercise);
  102. if ($debug) {error_log('1.1. $exerciseInSession was unset - set now - end'); };
  103. }
  104. } else {
  105. Session::write('firstTime', false);
  106. }
  107. //2. Checking if $objExercise is set
  108. $exerciseFromSession = Session::read('objExercise');
  109. if (!isset($objExercise) && !empty($exerciseFromSession)) {
  110. if ($debug) { error_log('2. Loading $objExercise from session'); };
  111. $objExercise = Session::read('objExercise');
  112. }
  113. //3. $objExercise is not set, then return to the exercise list
  114. if (!is_object($objExercise)) {
  115. if ($debug) {error_log('3. $objExercise was not set, kill the script'); };
  116. header('Location: exercise.php');
  117. exit;
  118. }
  119. // If reminder ends we jump to the exercise_reminder
  120. if ($objExercise->review_answers) {
  121. if ($remind_question_id == -1) {
  122. header('Location: exercise_reminder.php?origin='.$origin.'&exerciseId='.$exerciseId.'&'.api_get_cidreq());
  123. exit;
  124. }
  125. }
  126. $template->addGlobal('shuffle_answers', $objExercise->random_answers);
  127. $htmlHeadXtra[] = $template->render(
  128. '@template_style/exercise/submit.html.twig'
  129. );
  130. $current_timestamp = time();
  131. $my_remind_list = array();
  132. $time_control = false;
  133. if ($objExercise->expired_time != 0) {
  134. $time_control = true;
  135. }
  136. // Generating the time control key for the user
  137. $current_expired_time_key = ExerciseLib::get_time_control_key($objExercise->id, $learnpath_id, $learnpath_item_id);
  138. //$_SESSION['duration_time'][$current_expired_time_key] = $current_timestamp;
  139. Session::write(
  140. 'duration_time',
  141. [$current_expired_time_key => $current_timestamp]
  142. );
  143. if ($time_control) {
  144. // Get the expired time of the current exercise in track_e_exercises
  145. $total_seconds = $objExercise->expired_time*60;
  146. }
  147. $show_clock = true;
  148. $user_id = api_get_user_id();
  149. if ($objExercise->selectAttempts() > 0) {
  150. $attempt_html = '';
  151. $attempt_count = Event::get_attempt_count(
  152. $user_id,
  153. $exerciseId,
  154. $learnpath_id,
  155. $learnpath_item_id,
  156. $learnpath_item_view_id
  157. );
  158. if ($attempt_count >= $objExercise->selectAttempts()) {
  159. $show_clock = false;
  160. if (!api_is_allowed_to_edit(null,true)) {
  161. if ($objExercise->results_disabled == 0 && $origin != 'learnpath') {
  162. // Showing latest attempt according with task BT#1628
  163. $exercise_stat_info = Event::getExerciseResultsByUser(
  164. $user_id,
  165. $exerciseId,
  166. api_get_course_id(),
  167. api_get_session_id()
  168. );
  169. if (!empty($exercise_stat_info)) {
  170. $max_exe_id = max(array_keys($exercise_stat_info));
  171. $last_attempt_info = $exercise_stat_info[$max_exe_id];
  172. $attempt_html .= Display::div(
  173. get_lang('Date').': '.api_get_local_time($last_attempt_info['exe_date']),
  174. array('id' => '')
  175. );
  176. $attempt_html .= Display::return_message(
  177. sprintf(
  178. get_lang('ReachedMaxAttempts'),
  179. $exercise_title,
  180. $objExercise->selectAttempts()
  181. ),
  182. 'warning',
  183. false
  184. );
  185. if (!empty($last_attempt_info['question_list'])) {
  186. foreach($last_attempt_info['question_list'] as $question_data) {
  187. $question_id = $question_data['question_id'];
  188. $marks = $question_data['marks'];
  189. $question_info = Question::read($question_id);
  190. $attempt_html .= Display::div($question_info->question, array('class'=>'question_title'));
  191. $attempt_html .= Display::div(get_lang('Score').' '.$marks, array('id'=>'question_question_titlescore'));
  192. }
  193. }
  194. $score = ExerciseLib::show_score($last_attempt_info['exe_result'], $last_attempt_info['exe_weighting']);
  195. $attempt_html .= Display::div(get_lang('YourTotalScore').' '.$score, array('id'=>'question_score'));
  196. } else {
  197. $attempt_html .= Display::return_message(sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()), 'warning', false);
  198. }
  199. } else {
  200. $attempt_html .= Display::return_message(sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()), 'warning', false);
  201. }
  202. } else {
  203. $attempt_html .= Display :: return_message(sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()), 'warning', false);
  204. }
  205. if ($origin == 'learnpath') {
  206. Display :: display_reduced_header();
  207. } else {
  208. Display :: display_header(get_lang('Exercises'));
  209. }
  210. echo $attempt_html;
  211. if ($origin != 'learnpath') {
  212. Display:: display_footer();
  213. }
  214. exit;
  215. }
  216. }
  217. if ($debug) {
  218. error_log("4. Setting the exe_id: $exe_id");
  219. }
  220. /* 5. Getting user exercise info (if the user took the exam before)
  221. generating exe_id */
  222. $exercise_stat_info = $objExercise->get_stat_track_exercise_info(
  223. $learnpath_id,
  224. $learnpath_item_id,
  225. $learnpath_item_view_id
  226. );
  227. // Fix in order to get the correct question list.
  228. $questionListUncompressed = $objExercise->getQuestionListWithMediasUncompressed();
  229. Session::write('question_list_uncompressed', $questionListUncompressed);
  230. $clock_expired_time = null;
  231. if (empty($exercise_stat_info)) {
  232. if ($debug) error_log('5 $exercise_stat_info is empty ');
  233. $total_weight = 0;
  234. $questionList = $objExercise->get_validated_question_list();
  235. foreach ($questionListUncompressed as $question_id) {
  236. $objQuestionTmp = Question::read($question_id);
  237. $total_weight += floatval($objQuestionTmp->weighting);
  238. }
  239. if ($time_control) {
  240. $expected_time = $current_timestamp + $total_seconds;
  241. if ($debug) error_log('5.1. $current_timestamp '.$current_timestamp);
  242. if ($debug) error_log('5.2. $expected_time '.$expected_time);
  243. $clock_expired_time = api_get_utc_datetime($expected_time);
  244. if ($debug) error_log('5.3. $expected_time '.$clock_expired_time);
  245. //Sessions that contain the expired time
  246. //$_SESSION['expired_time'][$current_expired_time_key] = $clock_expired_time;
  247. Session::write(
  248. 'expired_time',
  249. [$current_expired_time_key => $clock_expired_time]
  250. );
  251. //if ($debug) { error_log('5.4. Setting the $_SESSION[expired_time]: '.$_SESSION['expired_time'][$current_expired_time_key] ); };
  252. }
  253. $exe_id = $objExercise->save_stat_track_exercise_info(
  254. $clock_expired_time,
  255. $learnpath_id,
  256. $learnpath_item_id,
  257. $learnpath_item_view_id,
  258. $questionList,
  259. $total_weight
  260. );
  261. $exercise_stat_info = $objExercise->get_stat_track_exercise_info(
  262. $learnpath_id,
  263. $learnpath_item_id,
  264. $learnpath_item_view_id
  265. );
  266. if ($debug) error_log("5.5 exercise_stat_info[] exists getting exe_id $exe_id");
  267. } else {
  268. $exe_id = $exercise_stat_info['exe_id'];
  269. // Remember last question id position.
  270. $isFirstTime = Session::read('firstTime');
  271. if ($isFirstTime && $objExercise->type == ONE_PER_PAGE) {
  272. $resolvedQuestions = Event::getAllExerciseEventByExeId($exe_id);
  273. if (!empty($resolvedQuestions) &&
  274. !empty($exercise_stat_info['data_tracking'])
  275. ) {
  276. $last = current(end($resolvedQuestions));
  277. $attemptQuestionList = explode(',', $exercise_stat_info['data_tracking']);
  278. $count = 1;
  279. foreach ($attemptQuestionList as $question) {
  280. if ($last->getQuestionId() == $question) {
  281. break;
  282. }
  283. $count++;
  284. }
  285. $current_question = $count;
  286. }
  287. }
  288. if ($debug) error_log("5 exercise_stat_info[] exists getting exe_id $exe_id ");
  289. }
  290. $questionListInSession = Session::read('questionList');
  291. if (!isset($questionListInSession)) {
  292. // Selects the list of question ID
  293. $questionList = $objExercise->getQuestionList();
  294. // Media questions.
  295. $media_is_activated = $objExercise->mediaIsActivated();
  296. //Getting order from random
  297. if ($media_is_activated == false &&
  298. $objExercise->isRandom() &&
  299. isset($exercise_stat_info) &&
  300. !empty($exercise_stat_info['data_tracking'])
  301. ) {
  302. $questionList = explode(',', $exercise_stat_info['data_tracking']);
  303. }
  304. Session::write('questionList', $questionList);
  305. if ($debug > 0) {
  306. error_log('$_SESSION[questionList] was set');
  307. }
  308. } else {
  309. if (isset($objExercise) && isset($exerciseInSession)) {
  310. $questionList = Session::read('questionList');
  311. }
  312. }
  313. // Array to check in order to block the chat
  314. ExerciseLib::create_chat_exercise_session($exe_id);
  315. if ($debug) { error_log('6. $objExercise->get_stat_track_exercise_info function called:: '.print_r($exercise_stat_info, 1)); };
  316. if (!empty($exercise_stat_info['questions_to_check'])) {
  317. $my_remind_list = $exercise_stat_info['questions_to_check'];
  318. $my_remind_list = explode(',', $my_remind_list);
  319. $my_remind_list = array_filter($my_remind_list);
  320. }
  321. $params = "exe_id=$exe_id&exerciseId=$exerciseId&origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id&learnpath_item_view_id=$learnpath_item_view_id&".api_get_cidreq();
  322. if ($debug) { error_log("6.1 params: -> $params"); };
  323. if ($reminder == 2 && empty($my_remind_list)) {
  324. if ($debug) { error_log("6.2 calling the exercise_reminder.php "); };
  325. header('Location: exercise_reminder.php?'.$params);
  326. exit;
  327. }
  328. /*
  329. * 7. Loading Time control parameters
  330. * If the expired time is major that zero(0) then the expired time is compute on this time.
  331. */
  332. if ($time_control) {
  333. if ($debug) error_log('7.1. Time control is enabled');
  334. if ($debug) error_log('7.2. $current_expired_time_key '.$current_expired_time_key);
  335. //if ($debug) error_log('7.3. $_SESSION[expired_time][$current_expired_time_key] '.$_SESSION['expired_time'][$current_expired_time_key]);
  336. $expiredTimeFromSession = Session::read('expired_time');
  337. if (!isset($expiredTimeFromSession[$current_expired_time_key])) {
  338. //Timer - Get expired_time for a student
  339. if (!empty($exercise_stat_info)) {
  340. if ($debug) {error_log('7.4 Seems that the session ends and the user want to retake the exam'); };
  341. $expired_time_of_this_attempt = $exercise_stat_info['expired_time_control'];
  342. if ($debug) {error_log('7.5 $expired_time_of_this_attempt: '.$expired_time_of_this_attempt); }
  343. // Get the last attempt of an exercise
  344. $last_attempt_date = Event::getLastAttemptDateOfExercise($exercise_stat_info['exe_id']);
  345. /* This means that the user enters the exam but do not answer the
  346. first question we get the date from the track_e_exercises not from
  347. the track_et_attempt see #2069 */
  348. if (empty($last_attempt_date)) {
  349. $diff = $current_timestamp - api_strtotime($exercise_stat_info['start_date'], 'UTC');
  350. $last_attempt_date = api_get_utc_datetime(api_strtotime($exercise_stat_info['start_date'],'UTC') + $diff);
  351. } else {
  352. //Recalculate the time control due #2069
  353. $diff = $current_timestamp - api_strtotime($last_attempt_date,'UTC');
  354. $last_attempt_date = api_get_utc_datetime(api_strtotime($last_attempt_date,'UTC') + $diff);
  355. }
  356. if ($debug) {error_log('7.6. $last_attempt_date: '.$last_attempt_date); }
  357. //New expired time - it is due to the possible closure of session
  358. $new_expired_time_in_seconds = api_strtotime($expired_time_of_this_attempt, 'UTC') - api_strtotime($last_attempt_date,'UTC');
  359. if ($debug) {error_log('7.7. $new_expired_time_in_seconds: '.$new_expired_time_in_seconds); }
  360. $expected_time = $current_timestamp + $new_expired_time_in_seconds;
  361. if ($debug) {error_log('7.8. $expected_time1: '.$expected_time); }
  362. $clock_expired_time = api_get_utc_datetime($expected_time);
  363. if ($debug) {error_log('7.9. $clock_expired_time: '.$clock_expired_time); }
  364. // First we update the attempt to today
  365. /* How the expired time is changed into "track_e_exercises" table,
  366. then the last attempt for this student should be changed too */
  367. $sql = "UPDATE $exercise_attempt_table SET
  368. tms = '".api_get_utc_datetime()."'
  369. WHERE
  370. exe_id = '".$exercise_stat_info['exe_id']."' AND
  371. tms = '".$last_attempt_date."' ";
  372. if ($debug) {error_log('7.10. $sql: '.$sql); }
  373. Database::query($sql);
  374. //Sessions that contain the expired time
  375. // $_SESSION['expired_time'][$current_expired_time_key] = $clock_expired_time;
  376. Session::write(
  377. 'expired_time',
  378. [$current_expired_time_key => $clock_expired_time]
  379. );
  380. //if ($debug) {error_log('7.11. Setting the $_SESSION[expired_time]: '.$_SESSION['expired_time'][$current_expired_time_key] ); };
  381. }
  382. } else {
  383. $expiredTimeFromSession = Session::read('expired_time');
  384. $clock_expired_time = $expiredTimeFromSession[$current_expired_time_key];
  385. }
  386. } else {
  387. if ($debug) { error_log("7 No time control"); };
  388. }
  389. // Get time left for expiring time
  390. $time_left = api_strtotime($clock_expired_time,'UTC') - time();
  391. /*
  392. * The time control feature is enable here - this feature is enable for a jquery plugin called epiclock
  393. * for more details of how it works see this link : http://eric.garside.name/docs.html?p=epiclock
  394. */
  395. if ($time_control) { //Sends the exercise form when the expired time is finished
  396. $htmlHeadXtra[] = $objExercise->show_time_control_js($time_left);
  397. }
  398. // if the user has submitted the form
  399. $exercise_title = $objExercise->selectTitle();
  400. $exercise_sound = $objExercise->selectSound();
  401. //in LP's is enabled the "remember question" feature?
  402. $questionListFromSession = Session::read('questionList');
  403. if (!isset($questionListFromSession)) {
  404. // selects the list of question ID
  405. $questionList = $objExercise->get_validated_question_list();
  406. if ($objExercise->isRandom() && !empty($exercise_stat_info['data_tracking'])) {
  407. $questionList = explode(',', $exercise_stat_info['data_tracking']);
  408. }
  409. Session::write('questionList', $questionList);
  410. if ($debug > 0) { error_log('$_SESSION[questionList] was set'); }
  411. } else {
  412. $exerciseFromSession = Session::read('objExercise');
  413. if (isset($objExercise) && isset($exerciseFromSession)) {
  414. $questionList = Session::read('questionList');
  415. }
  416. }
  417. if ($debug) error_log('8. Question list loaded '.print_r($questionList, 1));
  418. //Real question count
  419. $question_count = 0;
  420. if (!empty($questionList)) {
  421. $question_count = count($questionList);
  422. }
  423. if ($current_question > $question_count) {
  424. $current_question = 0;
  425. }
  426. if ($formSent && isset($_POST)) {
  427. if ($debug) { error_log('9. $formSent was set'); }
  428. // Initializing
  429. if (!is_array($exerciseResult)) {
  430. $exerciseResult = array();
  431. $exerciseResultCoordinates = array();
  432. }
  433. //Only for hotspot
  434. if (!isset($choice) && isset($_REQUEST['hidden_hotspot_id'])) {
  435. $hotspot_id = (int)($_REQUEST['hidden_hotspot_id']);
  436. $choice = array($hotspot_id => '');
  437. }
  438. // if the user has answered at least one question
  439. if (is_array($choice)) {
  440. if ($debug) { error_log('9.1. $choice is an array '.print_r($choice, 1)); }
  441. // Also store hotspot spots in the session ($exerciseResultCoordinates
  442. // will be stored in the session at the end of this script)
  443. if (isset($_POST['hotspot'])) {
  444. $exerciseResultCoordinates = $_POST['hotspot'];
  445. if ($debug) { error_log('9.2. $_POST[hotspot] data '.print_r($exerciseResultCoordinates, 1)); }
  446. }
  447. if ($objExercise->type == ALL_ON_ONE_PAGE) {
  448. // $exerciseResult receives the content of the form.
  449. // Each choice of the student is stored into the array $choice
  450. $exerciseResult = $choice;
  451. } else {
  452. // gets the question ID from $choice. It is the key of the array
  453. list ($key) = array_keys($choice);
  454. // if the user didn't already answer this question
  455. if (!isset($exerciseResult[$key])) {
  456. // stores the user answer into the array
  457. $exerciseResult[$key] = $choice[$key];
  458. //saving each question
  459. if ($objExercise->feedback_type != EXERCISE_FEEDBACK_TYPE_DIRECT) {
  460. $nro_question = $current_question; // - 1;
  461. $questionId = $key;
  462. // gets the student choice for this question
  463. $choice = $exerciseResult[$questionId];
  464. if (isset($exe_id)) {
  465. // Manage the question and answer attempts
  466. if ($debug) {
  467. error_log('8.3. manage_answer exe_id: '.$exe_id.' - $questionId: '.$questionId.' Choice'.print_r($choice,1));
  468. }
  469. $objExercise->manage_answer(
  470. $exe_id,
  471. $questionId,
  472. $choice,
  473. 'exercise_show',
  474. $exerciseResultCoordinates,
  475. true,
  476. false,
  477. false,
  478. $objExercise->propagate_neg,
  479. array()
  480. );
  481. }
  482. //END of saving and qualifying
  483. }
  484. }
  485. }
  486. if ($debug) { error_log('9.3. $choice is an array - end'); }
  487. if ($debug) { error_log('9.4. $exerciseResult '.print_r($exerciseResult,1)); }
  488. }
  489. // the script "exercise_result.php" will take the variable $exerciseResult from the session
  490. Session::write('exerciseResult', $exerciseResult);
  491. Session::write('remind_list', $remind_list);
  492. Session::write('exerciseResultCoordinates',$exerciseResultCoordinates);
  493. // if all questions on one page OR if it is the last question (only for an exercise with one question per page)
  494. if ($objExercise->type == ALL_ON_ONE_PAGE || $current_question >= $question_count) {
  495. if (api_is_allowed_to_session_edit()) {
  496. // goes to the script that will show the result of the exercise
  497. if ($objExercise->type == ALL_ON_ONE_PAGE) {
  498. if ($debug) { error_log('10. Exercise ALL_ON_ONE_PAGE -> Redirecting to exercise_result.php'); }
  499. //We check if the user attempts before sending to the exercise_result.php
  500. if ($objExercise->selectAttempts() > 0) {
  501. $attempt_count = Event::get_attempt_count(
  502. api_get_user_id(),
  503. $exerciseId,
  504. $learnpath_id,
  505. $learnpath_item_id,
  506. $learnpath_item_view_id
  507. );
  508. if ($attempt_count >= $objExercise->selectAttempts()) {
  509. Display :: display_warning_message(
  510. sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()),
  511. false
  512. );
  513. if ($origin != 'learnpath') {
  514. //so we are not in learnpath tool
  515. echo '</div>'; //End glossary div
  516. Display :: display_footer();
  517. } else {
  518. echo '</body></html>';
  519. }
  520. }
  521. }
  522. header("Location: exercise_result.php?".api_get_cidreq()."&exe_id=$exe_id&origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id&learnpath_item_view_id=$learnpath_item_view_id");
  523. exit;
  524. } else {
  525. if ($debug) { error_log('10. Redirecting to exercise_show.php'); }
  526. header("Location: exercise_result.php?".api_get_cidreq()."&exe_id=$exe_id&origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id&learnpath_item_view_id=$learnpath_item_view_id");
  527. exit;
  528. }
  529. } else {
  530. if ($debug) { error_log('10. Redirecting to exercise_submit.php'); }
  531. header("Location: exercise_submit.php?".api_get_cidreq()."&exerciseId=$exerciseId&origin=$origin");
  532. exit;
  533. }
  534. }
  535. if ($debug) { error_log('11. $formSent was set - end'); }
  536. } else {
  537. if ($debug) { error_log('9. $formSent was NOT sent'); }
  538. }
  539. // If questionNum comes from POST and not from GET
  540. $latestQuestionId = Event::getLatestQuestionIdFromAttempt($exe_id);
  541. if (is_null($current_question)) {
  542. $current_question = 1;
  543. if ($latestQuestionId) {
  544. $current_question = $objExercise->getPositionInCompressedQuestionList($latestQuestionId);
  545. }
  546. } else {
  547. $current_question++;
  548. }
  549. if ($question_count != 0) {
  550. if (($objExercise->type == ALL_ON_ONE_PAGE ||
  551. $current_question > $question_count)
  552. ) {
  553. if (api_is_allowed_to_session_edit()) {
  554. // goes to the script that will show the result of the exercise
  555. if ($objExercise->type == ALL_ON_ONE_PAGE) {
  556. if ($debug) { error_log('12. Exercise ALL_ON_ONE_PAGE -> Redirecting to exercise_result.php'); }
  557. //We check if the user attempts before sending to the exercise_result.php
  558. if ($objExercise->selectAttempts() > 0) {
  559. $attempt_count = Event::get_attempt_count(
  560. api_get_user_id(),
  561. $exerciseId,
  562. $learnpath_id,
  563. $learnpath_item_id,
  564. $learnpath_item_view_id
  565. );
  566. if ($attempt_count >= $objExercise->selectAttempts()) {
  567. Display :: display_warning_message(
  568. sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()),
  569. false
  570. );
  571. if ($origin != 'learnpath') {
  572. //so we are not in learnpath tool
  573. echo '</div>'; //End glossary div
  574. Display :: display_footer();
  575. } else {
  576. echo '</body></html>';
  577. }
  578. exit;
  579. }
  580. }
  581. } else {
  582. if ($objExercise->review_answers) {
  583. header('Location: exercise_reminder.php?'.$params);
  584. exit;
  585. } else {
  586. header("Location: exercise_result.php?".api_get_cidreq()."&exe_id=$exe_id&origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id&learnpath_item_view_id=$learnpath_item_view_id");
  587. exit;
  588. }
  589. }
  590. } else {
  591. if ($debug) { error_log('Redirecting to exercise_submit.php'); }
  592. exit;
  593. }
  594. }
  595. } else {
  596. $error = get_lang('ThereAreNoQuestionsForThisExercise');
  597. // if we are in the case where user select random by category, but didn't choose the number of random question
  598. if ($objExercise->selectRandomByCat() > 0 && $objExercise->random <= 0) {
  599. $error .= "<br/>".get_lang('PleaseSelectSomeRandomQuestion');
  600. }
  601. }
  602. if (api_is_in_gradebook()) {
  603. $interbreadcrumb[]= array(
  604. 'url' => api_get_path(WEB_CODE_PATH).'gradebook/index.php?'.api_get_cidreq(),
  605. 'name' => get_lang('ToolGradebook')
  606. );
  607. }
  608. $interbreadcrumb[] = array ("url" => "exercise.php?".api_get_cidreq(), "name" => get_lang('Exercises'));
  609. $interbreadcrumb[] = array ("url" => "#", "name" => $objExercise->name);
  610. if ($origin != 'learnpath') { //so we are not in learnpath tool
  611. Display :: display_header(null,'Exercises');
  612. if (!api_is_allowed_to_session_edit() ) {
  613. Display :: display_warning_message(get_lang('SessionIsReadOnly'));
  614. }
  615. } else {
  616. $htmlHeadXtra[] = "
  617. <style>
  618. body { background: none;}
  619. </style>
  620. ";
  621. Display::display_reduced_header();
  622. echo '<div style="height:10px">&nbsp;</div>';
  623. }
  624. $show_quiz_edition = $objExercise->added_in_lp();
  625. // I'm in a preview mode
  626. if (api_is_course_admin() && $origin != 'learnpath') {
  627. echo '<div class="actions">';
  628. if ($show_quiz_edition == false) {
  629. echo '<a href="exercise_admin.php?' . api_get_cidreq() . '&modifyExercise=yes&exerciseId=' . $objExercise->id . '">'.Display :: return_icon('settings.png', get_lang('ModifyExercise'),'',ICON_SIZE_MEDIUM).'</a>';
  630. } else {
  631. echo '<a href="#">'.Display::return_icon('settings_na.png', get_lang('ModifyExercise'),'',ICON_SIZE_MEDIUM).'</a>';
  632. }
  633. echo '</div>';
  634. }
  635. $is_visible_return = $objExercise->is_visible(
  636. $learnpath_id,
  637. $learnpath_item_id,
  638. $learnpath_item_view_id
  639. );
  640. if ($is_visible_return['value'] == false) {
  641. echo $is_visible_return['message'];
  642. if ($origin != 'learnpath') {
  643. Display :: display_footer();
  644. }
  645. exit;
  646. }
  647. $limit_time_exists = (!empty($objExercise->start_time) || !empty($objExercise->end_time)) ? true : false;
  648. if ($limit_time_exists) {
  649. $exercise_start_time = api_strtotime($objExercise->start_time, 'UTC');
  650. $exercise_end_time = api_strtotime($objExercise->end_time, 'UTC');
  651. $time_now = time();
  652. if (!empty($objExercise->start_time)) {
  653. $permission_to_start = (($time_now - $exercise_start_time) > 0) ? true : false;
  654. } else {
  655. $permission_to_start = true;
  656. }
  657. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  658. if (!empty($objExercise->end_time)) {
  659. $exercise_timeover = (($time_now - $exercise_end_time) > 0) ? true : false;
  660. } else {
  661. $exercise_timeover = false;
  662. }
  663. }
  664. if (!$permission_to_start || $exercise_timeover) {
  665. if (!api_is_allowed_to_edit(null,true)) {
  666. $message_warning = $permission_to_start ? get_lang('ReachedTimeLimit') : get_lang('ExerciseNoStartedYet');
  667. Display :: display_warning_message(sprintf($message_warning, $exercise_title, $objExercise->selectAttempts()));
  668. if ($origin != 'learnpath') {
  669. Display :: display_footer();
  670. }
  671. exit;
  672. } else {
  673. $message_warning = $permission_to_start ? get_lang('ReachedTimeLimitAdmin') : get_lang('ExerciseNoStartedAdmin');
  674. Display :: display_warning_message(sprintf($message_warning, $exercise_title, $objExercise->selectAttempts()));
  675. }
  676. }
  677. }
  678. // Blocking empty start times see BT#2800
  679. global $_custom;
  680. if (isset($_custom['exercises_hidden_when_no_start_date']) &&
  681. $_custom['exercises_hidden_when_no_start_date']
  682. ) {
  683. if (empty($objExercise->start_time)) {
  684. Display:: display_warning_message(
  685. sprintf(
  686. get_lang('ExerciseNoStartedYet'),
  687. $exercise_title,
  688. $objExercise->selectAttempts()
  689. )
  690. );
  691. if ($origin != 'learnpath') {
  692. Display :: display_footer();
  693. }
  694. }
  695. }
  696. //Timer control
  697. if ($time_control) {
  698. echo $objExercise->return_time_left_div();
  699. echo '<div style="display:none" class="warning-message" id="expired-message-id">'.get_lang('ExerciseExpiredTimeMessage').'</div>';
  700. }
  701. if ($origin != 'learnpath') {
  702. echo '<div id="highlight-plugin" class="glossary-content">';
  703. }
  704. if ($reminder == 2) {
  705. if ($debug) { error_log(' $reminder == 2'); }
  706. $data_tracking = $exercise_stat_info['data_tracking'];
  707. $data_tracking = explode(',', $data_tracking);
  708. $current_question = 1; //set by default the 1st question
  709. if (!empty($my_remind_list)) {
  710. //Checking which questions we are going to call from the remind list
  711. for ($i = 0; $i < count($data_tracking); $i++) {
  712. for($j = 0; $j < count($my_remind_list); $j++) {
  713. if (!empty($remind_question_id)) {
  714. if ($remind_question_id == $my_remind_list[$j]) {
  715. if ($remind_question_id == $data_tracking[$i]) {
  716. if (isset($my_remind_list[$j+1])) {
  717. $remind_question_id = $my_remind_list[$j+1];
  718. $current_question = $i + 1;
  719. } else {
  720. // We end the remind list we go to the exercise_reminder.php please
  721. $remind_question_id = -1;
  722. $current_question = $i + 1; // last question
  723. }
  724. break 2;
  725. }
  726. }
  727. } else {
  728. if ($my_remind_list[$j] == $data_tracking[$i]) {
  729. if (isset($my_remind_list[$j+1])) {
  730. $remind_question_id = $my_remind_list[$j+1];
  731. $current_question = $i + 1; // last question
  732. } else {
  733. // We end the remind list we go to the exercise_reminder.php please
  734. $remind_question_id = -1;
  735. $current_question = $i + 1; // last question
  736. }
  737. break 2;
  738. }
  739. }
  740. }
  741. }
  742. } else {
  743. if ($objExercise->review_answers) {
  744. if ($debug) { error_log('. redirecting to exercise_reminder.php '); }
  745. header("Location: exercise_reminder.php?$params");
  746. exit;
  747. }
  748. }
  749. }
  750. if ($objExercise->review_answers) {
  751. $script_php = 'exercise_reminder.php';
  752. } else {
  753. $script_php = 'exercise_result.php';
  754. }
  755. if (!empty($error)) {
  756. Display :: display_error_message($error, false);
  757. } else {
  758. if (!empty ($exercise_sound)) {
  759. echo "<a href=\"../document/download.php?doc_url=%2Faudio%2F" . Security::remove_XSS($exercise_sound) . "\" target=\"_blank\">", "<img src=\"../img/sound.gif\" border=\"0\" align=\"absmiddle\" alt=", get_lang('Sound') . "\" /></a>";
  760. }
  761. // Get number of hotspot questions for javascript validation
  762. $number_of_hotspot_questions = 0;
  763. $onsubmit = '';
  764. $i = 0;
  765. if (!empty($questionList)) {
  766. foreach ($questionList as $questionId) {
  767. $i++;
  768. $objQuestionTmp = Question::read($questionId);
  769. // for sequential exercises
  770. if ($objExercise->type == ONE_PER_PAGE) {
  771. // if it is not the right question, goes to the next loop iteration
  772. if ($current_question != $i) {
  773. continue;
  774. } else {
  775. if ($objQuestionTmp->selectType() == HOT_SPOT || $objQuestionTmp->selectType() == HOT_SPOT_DELINEATION) {
  776. $number_of_hotspot_questions++;
  777. }
  778. break;
  779. }
  780. } else {
  781. if ($objQuestionTmp->selectType() == HOT_SPOT || $objQuestionTmp->selectType() == HOT_SPOT_DELINEATION) {
  782. $number_of_hotspot_questions++;
  783. }
  784. }
  785. }
  786. }
  787. if ($number_of_hotspot_questions > 0) {
  788. $onsubmit = "onsubmit=\"return validateFlashVar('" . $number_of_hotspot_questions . "', '" . get_lang('HotspotValidateError1') . "', '" . get_lang('HotspotValidateError2') . "');\"";
  789. }
  790. echo '<script>
  791. $(function() {
  792. //This pre-load the save.png icon
  793. var saveImage = new Image();
  794. saveImage.src = \'' . Display::return_icon(
  795. 'save.png',
  796. get_lang('Saved'),
  797. array(),
  798. ICON_SIZE_SMALL,
  799. false,
  800. true
  801. ) . '\';
  802. // Block form submition on enter
  803. $(".block_on_enter").keypress(function(event) {
  804. return event.keyCode != 13;
  805. });
  806. $(".main_question").mouseover(function() {
  807. //$(this).find(".exercise_save_now_button").show();
  808. //$(this).addClass("question_highlight");
  809. });
  810. $(".main_question").mouseout(function() {
  811. //$(this).find(".exercise_save_now_button").hide();
  812. $(this).removeClass("question_highlight");
  813. });
  814. $(".no_remind_highlight").hide();
  815. // if the users validates the form using return key, prevent form action and simulates click on validation button
  816. /*$("#exercise_form").submit(function(){
  817. $(".question-validate-btn").first().trigger("click");
  818. return false;
  819. });*/
  820. $(\'form#exercise_form\').prepend($(\'#exercise-description\'));
  821. });
  822. function previous_question(question_num) {
  823. url = "exercise_submit.php?'.$params.'&num="+question_num;
  824. window.location = url;
  825. }
  826. function previous_question_and_save(previous_question_id, question_id_to_save) {
  827. url = "exercise_submit.php?'.$params.'&num="+previous_question_id;
  828. //Save the current question
  829. save_now(question_id_to_save, url);
  830. }
  831. function save_question_list(question_list) {
  832. $.each(question_list, function(key, question_id) {
  833. save_now(question_id, null, false);
  834. });
  835. var url = "";
  836. if ('.$reminder.' == 1 ) {
  837. url = "exercise_reminder.php?'.$params.'&num='.$current_question.'";
  838. } else if ('.$reminder.' == 2 ) {
  839. url = "exercise_submit.php?'.$params.'&num='.$current_question.'&remind_question_id='.$remind_question_id.'&reminder=2";
  840. } else {
  841. url = "exercise_submit.php?'.$params.'&num='.$current_question.'&remind_question_id='.$remind_question_id.'";
  842. }
  843. //$("#save_for_now_"+question_id).html(\'' . Display::return_icon('save.png', get_lang('Saved'), array(), ICON_SIZE_SMALL) . '\');
  844. window.location = url;
  845. }
  846. function save_now(question_id, url_extra) {
  847. //1. Normal choice inputs
  848. var my_choice = $(\'*[name*="choice[\'+question_id+\']"]\').serialize();
  849. //2. Reminder checkbox
  850. var remind_list = $(\'*[name*="remind_list"]\').serialize();
  851. //3. Hotspots
  852. var hotspot = $(\'*[name*="hotspot[\'+question_id+\']"]\').serialize();
  853. // Checking FCK
  854. if (question_id) {
  855. if (CKEDITOR.instances["choice["+question_id+"]"]) {
  856. var ckContent = CKEDITOR.instances["choice["+question_id+"]"].getData();
  857. my_choice = {};
  858. my_choice["choice["+question_id+"]"] = ckContent;
  859. my_choice = $.param(my_choice);
  860. }
  861. }
  862. if ($(\'input[name="remind_list[\'+question_id+\']"]\').is(\':checked\')) {
  863. $("#question_div_"+question_id).addClass("remind_highlight");
  864. } else {
  865. $("#question_div_"+question_id).removeClass("remind_highlight");
  866. }
  867. // Only for the first time
  868. $("#save_for_now_"+question_id).html(\'' . Display::returnFontAwesomeIcon('spinner', true, null, 'fa-spin') . '\');
  869. $.ajax({
  870. type:"post",
  871. async: false,
  872. url: "'.api_get_path(WEB_AJAX_PATH).'exercise.ajax.php?'.api_get_cidreq().'&a=save_exercise_by_now",
  873. data: "'.$params.'&type=simple&question_id="+question_id+"&"+my_choice+"&"+hotspot+"&"+remind_list,
  874. success: function(return_value) {
  875. if (return_value == "ok") {
  876. $("#save_for_now_"+question_id).html(\'' . Display::return_icon('save.png', get_lang('Saved'), array(), ICON_SIZE_SMALL) . '\');
  877. } else if (return_value == "error") {
  878. $("#save_for_now_"+question_id).html(\'' . Display::return_icon('error.png', get_lang('Error'), array(), ICON_SIZE_SMALL) . '\');
  879. } else if (return_value == "one_per_page") {
  880. var url = "";
  881. if ('.$reminder.' == 1 ) {
  882. url = "exercise_reminder.php?'.$params.'&num='.$current_question.'";
  883. } else if ('.$reminder.' == 2 ) {
  884. url = "exercise_submit.php?'.$params.'&num='.$current_question.'&remind_question_id='.$remind_question_id.'&reminder=2";
  885. } else {
  886. url = "exercise_submit.php?'.$params.'&num='.$current_question.'&remind_question_id='.$remind_question_id.'";
  887. }
  888. if (url_extra) {
  889. url = url_extra;
  890. }
  891. $("#save_for_now_"+question_id).html(\'' . Display::return_icon('save.png', get_lang('Saved'), array(), ICON_SIZE_SMALL) . '\');
  892. window.location = url;
  893. }
  894. },
  895. error: function() {
  896. $("#save_for_now_"+question_id).html(\'' . Display::return_icon('error.png', get_lang('Error'), array(), ICON_SIZE_SMALL) . '\');
  897. }
  898. });
  899. return false;
  900. }
  901. function save_now_all(validate) {
  902. // 1. Input choice.
  903. var my_choice = $(\'*[name*="choice"]\').serialize();
  904. // 2. Reminder.
  905. var remind_list = $(\'*[name*="remind_list"]\').serialize();
  906. // 3. Hotspots.
  907. var hotspot = $(\'*[name*="hotspot"]\').serialize();
  908. // Question list.
  909. var question_list = ['.implode(',', $questionList).'];
  910. var free_answers = {};
  911. $.each(question_list, function(index, my_question_id) {
  912. // Checking FCK
  913. if (my_question_id) {
  914. if (CKEDITOR.instances["choice["+my_question_id+"]"]) {
  915. var ckContent = CKEDITOR.instances["choice["+my_question_id+"]"].getData();
  916. free_answers["free_choice["+my_question_id+"]"] = ckContent;
  917. }
  918. }
  919. });
  920. free_answers = $.param(free_answers);
  921. $("#save_all_reponse").html(\'' . Display::returnFontAwesomeIcon('spinner', true, null, 'fa-spin') . '\');
  922. $.ajax({
  923. type:"post",
  924. async: false,
  925. url: "'.api_get_path(WEB_AJAX_PATH).'exercise.ajax.php?'.api_get_cidreq().'&a=save_exercise_by_now",
  926. data: "'.$params.'&type=all&"+my_choice+"&"+hotspot+"&"+free_answers+"&"+remind_list,
  927. success: function(return_value) {
  928. if (return_value == "ok") {
  929. //$("#save_all_reponse").html(\'' . Display::return_icon('accept.png') . '\');
  930. if (validate == "validate") {
  931. window.location = "'.$script_php.'?'.$params.'";
  932. } else {
  933. $("#save_all_reponse").html(\'' . Display::return_icon('accept.png') . '\');
  934. }
  935. } else {
  936. $("#save_all_reponse").html(\'' . Display::return_icon('wrong.gif') . '\');
  937. }
  938. }
  939. });
  940. return false;
  941. }
  942. function validate_all() {
  943. save_now_all("validate");
  944. return false;
  945. }
  946. </script>';
  947. echo '<form id="exercise_form" method="post" action="'.api_get_self().'?'.api_get_cidreq().'&autocomplete=off&&exerciseId='.$exerciseId .'" name="frm_exercise" '.$onsubmit.'>
  948. <input type="hidden" name="formSent" value="1" />
  949. <input type="hidden" name="exerciseId" value="'.$exerciseId . '" />
  950. <input type="hidden" name="num" value="'.$current_question.'" id="num_current_id" />
  951. <input type="hidden" name="num_answer" value="'.$currentAnswer.'" id="num_current_answer_id" />
  952. <input type="hidden" name="exe_id" value="'.$exe_id . '" />
  953. <input type="hidden" name="origin" value="'.$origin . '" />
  954. <input type="hidden" name="learnpath_id" value="'.$learnpath_id . '" />
  955. <input type="hidden" name="learnpath_item_id" value="'.$learnpath_item_id . '" />
  956. <input type="hidden" name="learnpath_item_view_id" value="'.$learnpath_item_view_id . '" />';
  957. // Show list of questions
  958. $i = 1;
  959. $attempt_list = array();
  960. if (isset($exe_id)) {
  961. $attempt_list = Event::getAllExerciseEventByExeId($exe_id);
  962. }
  963. $remind_list = array();
  964. if (isset($exercise_stat_info['questions_to_check']) && !empty($exercise_stat_info['questions_to_check'])) {
  965. $remind_list = explode(',', $exercise_stat_info['questions_to_check']);
  966. }
  967. foreach ($questionList as $questionId) {
  968. // for sequential exercises
  969. if ($objExercise->type == ONE_PER_PAGE) {
  970. // if it is not the right question, goes to the next loop iteration
  971. if ($current_question != $i) {
  972. $i++;
  973. continue;
  974. } else {
  975. if ($objExercise->feedback_type != EXERCISE_FEEDBACK_TYPE_DIRECT) {
  976. // if the user has already answered this question
  977. if (isset($exerciseResult[$questionId])) {
  978. // construction of the Question object
  979. $objQuestionTmp = Question::read($questionId);
  980. $questionName = $objQuestionTmp->selectTitle();
  981. // destruction of the Question object
  982. unset ($objQuestionTmp);
  983. Display :: display_normal_message(get_lang('AlreadyAnswered'));
  984. $i++;
  985. break;
  986. }
  987. }
  988. }
  989. }
  990. $user_choice = null;
  991. if (isset($attempt_list[$questionId])) {
  992. $user_choice = $attempt_list[$questionId];
  993. } elseif ($objExercise->saveCorrectAnswers) {
  994. $correctAnswers = $objExercise->getCorrectAnswersInAllAttempts($learnpath_id, $learnpath_item_id);
  995. if (isset($correctAnswers[$questionId])) {
  996. $user_choice = $correctAnswers[$questionId];
  997. }
  998. }
  999. $remind_highlight = '';
  1000. //Hides questions when reviewing a ALL_ON_ONE_PAGE exercise see #4542 no_remind_highlight class hide with jquery
  1001. if ($objExercise->type == ALL_ON_ONE_PAGE && isset($_GET['reminder']) && $_GET['reminder'] == 2) {
  1002. $remind_highlight = 'no_remind_highlight';
  1003. }
  1004. $exercise_actions = '';
  1005. $is_remind_on = false;
  1006. $attributes = array('id' =>'remind_list['.$questionId.']');
  1007. if (in_array($questionId, $remind_list)) {
  1008. $is_remind_on = true;
  1009. $attributes['checked'] = 1;
  1010. $remind_question = true;
  1011. $remind_highlight = ' remind_highlight ';
  1012. }
  1013. // Showing the exercise description
  1014. if (!empty($objExercise->description)){
  1015. if ($objExercise->type == ONE_PER_PAGE || ($objExercise->type != ONE_PER_PAGE && $i==1)) {
  1016. echo Display::panelCollapse('<span>'.
  1017. get_lang('ExerciseDescriptionLabel').'</span>',
  1018. $objExercise->description,
  1019. 'exercise-description',
  1020. [],
  1021. 'description',
  1022. 'exercise-collapse',
  1023. false,
  1024. true
  1025. );
  1026. }
  1027. }
  1028. echo '<div id="question_div_'.$questionId.'" class="main-question '.$remind_highlight.'" >';
  1029. // Shows the question and its answers
  1030. ExerciseLib::showQuestion(
  1031. $questionId,
  1032. false,
  1033. $origin,
  1034. $i,
  1035. true,
  1036. false,
  1037. $user_choice,
  1038. false
  1039. );
  1040. // Button save and continue
  1041. switch ($objExercise->type) {
  1042. case ONE_PER_PAGE:
  1043. $exercise_actions .= $objExercise->show_button($questionId, $current_question);
  1044. break;
  1045. case ALL_ON_ONE_PAGE :
  1046. $button = '<a href="javascript://" class="btn btn-info" onclick="save_now(\''.$questionId.'\'); ">'.get_lang('SaveForNow').'</a>';
  1047. $button .= '<span id="save_for_now_'.$questionId.'"></span>&nbsp;';
  1048. $exercise_actions .= Display::div($button, array('class'=>'exercise_save_now_button'));
  1049. break;
  1050. }
  1051. // Checkbox review answers
  1052. if ($objExercise->review_answers) {
  1053. $remind_question_div = Display::tag(
  1054. 'label',
  1055. Display::input(
  1056. 'checkbox',
  1057. 'remind_list[' . $questionId . ']',
  1058. '',
  1059. $attributes
  1060. ) . get_lang('ReviewQuestionLater'),
  1061. array(
  1062. 'class' => 'checkbox',
  1063. 'for' => 'remind_list[' . $questionId . ']'
  1064. )
  1065. );
  1066. $exercise_actions .= Display::div($remind_question_div, array('class'=>'exercise_save_now_button'));
  1067. }
  1068. echo Display::div($exercise_actions, array('class'=>'form-actions'));
  1069. echo '</div>';
  1070. $i++;
  1071. // for sequential exercises
  1072. if ($objExercise->type == ONE_PER_PAGE) {
  1073. // quits the loop
  1074. break;
  1075. }
  1076. }
  1077. // end foreach()
  1078. if ($objExercise->type == ALL_ON_ONE_PAGE) {
  1079. $exercise_actions = $objExercise->show_button($questionId, $current_question);
  1080. echo Display::div($exercise_actions, array('class'=>'exercise_actions'));
  1081. echo '<br>';
  1082. }
  1083. echo '</form>';
  1084. }
  1085. if ($origin != 'learnpath') {
  1086. // So we are not in learnpath tool
  1087. echo '</div>'; //End glossary div
  1088. }
  1089. Display :: display_footer();