exercise_submit.php 54 KB

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