exercise_submit.php 50 KB

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