exercise_submit.php 61 KB

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