exercise_submit.php 50 KB

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