exercise_submit.php 55 KB

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