exercise_submit.php 53 KB

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