exercise_submit.php 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Exercise submission
  5. * This script allows to run an exercise. According to the exercise type, questions
  6. * can be on an unique page, or one per page with a Next button.
  7. *
  8. * One exercise may contain different types of answers (unique or multiple selection,
  9. * matching, fill in blanks, free answer, hot-spot).
  10. *
  11. * Questions are selected randomly or not.
  12. *
  13. * When the user has answered all questions and clicks on the button "Ok",
  14. * it goes to exercise_result.php
  15. *
  16. * Notice : This script is also used to show a question before modifying it by
  17. * the administrator
  18. * @package chamilo.exercise
  19. * @author Olivier Brouckaert
  20. * @author Julio Montoya <gugli100@gmail.com>
  21. * Fill in blank option added (2008)
  22. * Cleaning exercises (2010),
  23. * Adding hotspot delineation support (2011)
  24. * Adding reminder + ajax support (2011)
  25. * Modified by hubert.borderiou (2011-10-21 question category)
  26. */
  27. /**
  28. * Code
  29. */
  30. use \ChamiloSession as Session;
  31. require_once 'exercise.class.php';
  32. require_once 'question.class.php';
  33. require_once 'answer.class.php';
  34. // name of the language file that needs to be included
  35. $language_file = 'exercice';
  36. require_once '../inc/global.inc.php';
  37. $current_course_tool = TOOL_QUIZ;
  38. require_once 'exercise.lib.php';
  39. $this_section = SECTION_COURSES;
  40. if ($debug) { error_log('--- Enter to the exercise_submit.php ---- '); error_log('0. POST variables : '.print_r($_POST,1)); }
  41. // Notice for unauthorized people.
  42. api_protect_course_script(true);
  43. $is_allowedToEdit = api_is_allowed_to_edit(null,true);
  44. if (api_get_setting('show_glossary_in_extra_tools') == 'true') {
  45. $htmlHeadXtra[] = api_get_js('glossary.js'); //Glossary
  46. $htmlHeadXtra[] = api_get_js('jquery.highlight.js'); //highlight
  47. }
  48. //This library is necessary for the time control feature
  49. $htmlHeadXtra[]= api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/epiclock/stylesheet/jquery.epiclock.css');
  50. $htmlHeadXtra[]= api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/epiclock/renderers/minute/epiclock.minute.css');
  51. $htmlHeadXtra[]= api_get_js('epiclock/javascript/jquery.dateformat.min.js');
  52. $htmlHeadXtra[]= api_get_js('epiclock/javascript/jquery.epiclock.min.js');
  53. $htmlHeadXtra[]= api_get_js('epiclock/renderers/minute/epiclock.minute.js');
  54. // General parameters passed via POST/GET
  55. $learnpath_id = isset($_REQUEST['learnpath_id']) ? intval($_REQUEST['learnpath_id']) : 0;
  56. $learnpath_item_id = isset($_REQUEST['learnpath_item_id']) ? intval($_REQUEST['learnpath_item_id']) : 0;
  57. $learnpath_item_view_id = isset($_REQUEST['learnpath_item_view_id']) ? intval($_REQUEST['learnpath_item_view_id']) : 0;
  58. $origin = isset($_REQUEST['origin']) ? Security::remove_XSS($_REQUEST['origin']) : '';
  59. $reminder = isset($_REQUEST['reminder']) ? intval($_REQUEST['reminder']) : 0;
  60. $remind_question_id = isset($_REQUEST['remind_question_id']) ? intval($_REQUEST['remind_question_id']) : 0;
  61. $exerciseId = isset($_REQUEST['exerciseId']) ? intval($_REQUEST['exerciseId']) : 0;
  62. if (empty($formSent)) {
  63. $formSent = $_REQUEST['formSent'];
  64. }
  65. if (empty($exerciseResult)) {
  66. $exerciseResult = $_REQUEST['exerciseResult'];
  67. }
  68. if (empty($exerciseResultCoordinates)) {
  69. $exerciseResultCoordinates = $_REQUEST['exerciseResultCoordinates'];
  70. }
  71. $choice = isset($_REQUEST['choice']) ? $_REQUEST['choice'] : null;
  72. $choice = empty($choice) ? $_REQUEST['choice2'] : null;
  73. //From submit modal
  74. $current_question = isset($_REQUEST['num']) ? intval($_REQUEST['num']) : null;
  75. //Error message
  76. $error = '';
  77. //Table calls
  78. $exercice_attemp_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  79. /* Teacher takes an exam and want to see a preview, we delete the objExercise from the session in order to get the latest changes in the exercise */
  80. if (api_is_allowed_to_edit(null,true) && $_GET['preview'] == 1 ) {
  81. Session::erase('objExercise');
  82. }
  83. // 1. Loading the $objExercise variable
  84. if (!isset($_SESSION['objExercise']) || $_SESSION['objExercise']->id != $_REQUEST['exerciseId']) {
  85. // Construction of Exercise
  86. $objExercise = new Exercise();
  87. if ($debug) {error_log('1. Setting the $objExercise variable'); };
  88. unset($_SESSION['questionList']);
  89. // if the specified exercise doesn't exist or is disabled
  90. if (!$objExercise->read($exerciseId) || (!$objExercise->selectStatus() && !$is_allowedToEdit && ($origin != 'learnpath'))) {
  91. if ($debug) {error_log('1.1. Error while reading the exercise'); };
  92. unset ($objExercise);
  93. $error = get_lang('ExerciseNotFound');
  94. } else {
  95. // Saves the object into the session
  96. Session::write('objExercise',$objExercise);
  97. if ($debug) {error_log('1.1. $_SESSION[objExercise] was unset - set now - end'); };
  98. }
  99. }
  100. //2. Checking if $objExercise is set
  101. if (!isset($objExercise) && isset($_SESSION['objExercise'])) {
  102. if ($debug) { error_log('2. Loading $objExercise from session'); };
  103. $objExercise = $_SESSION['objExercise'];
  104. }
  105. //3. $objExercise is not set, then return to the exercise list
  106. if (!is_object($objExercise)) {
  107. if ($debug) {error_log('3. $objExercise was not set, kill the script'); };
  108. header('Location: exercice.php');
  109. exit;
  110. }
  111. //if reminder ends we jump to the exercise_reminder
  112. if ($objExercise->review_answers) {
  113. if ($remind_question_id == -1) {
  114. header('Location: exercise_reminder.php?origin='.$origin.'&exerciseId='.$exerciseId);
  115. exit;
  116. }
  117. }
  118. $current_timestamp = time();
  119. $my_remind_list = array();
  120. $time_control = false;
  121. if ($objExercise->expired_time != 0) {
  122. $time_control = true;
  123. }
  124. //Generating the time control key for the user
  125. $current_expired_time_key = get_time_control_key($objExercise->id, $learnpath_id, $learnpath_item_id);
  126. $_SESSION['duration_time'][$current_expired_time_key] = $current_timestamp;
  127. if ($time_control) {
  128. //Get the expired time of the current exercice in track_e_exercices
  129. $total_seconds = $objExercise->expired_time*60;
  130. }
  131. $show_clock = true;
  132. $user_id = api_get_user_id();
  133. if ($objExercise->selectAttempts() > 0) {
  134. $attempt_html = '';
  135. $attempt_count = get_attempt_count($user_id, $exerciseId, $learnpath_id, $learnpath_item_id, $learnpath_item_view_id);
  136. if ($attempt_count >= $objExercise->selectAttempts()) {
  137. $show_clock = false;
  138. if (!api_is_allowed_to_edit(null,true)) {
  139. if ($objExercise->results_disabled == 0 && $origin != 'learnpath') {
  140. //Showing latest attempt according with task BT#1628
  141. $exercise_stat_info = get_exercise_results_by_user($user_id, $exerciseId, api_get_course_id(), api_get_session_id());
  142. if (!empty($exercise_stat_info)) {
  143. $max_exe_id = max(array_keys($exercise_stat_info));
  144. $last_attempt_info = $exercise_stat_info[$max_exe_id];
  145. $attempt_html .= Display::div(get_lang('Date').': '.api_get_local_time($last_attempt_info['exe_date']), array('id'=>''));
  146. $attempt_html .= Display::return_message(sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()), 'warning', false);
  147. if (!empty($last_attempt_info['question_list'])) {
  148. foreach($last_attempt_info['question_list'] as $question_data) {
  149. $question_id = $question_data['question_id'];
  150. $marks = $question_data['marks'];
  151. $question_info = Question::read($question_id);
  152. $attempt_html .= Display::div($question_info->question, array('class'=>'question_title'));
  153. $attempt_html .= Display::div(get_lang('Score').' '.$marks, array('id'=>'question_score'));
  154. }
  155. }
  156. $score = show_score($last_attempt_info['exe_result'], $last_attempt_info['exe_weighting']);
  157. $attempt_html .= Display::div(get_lang('YourTotalScore').' '.$score, array('id'=>'question_score'));
  158. } else {
  159. $attempt_html .= Display::return_message(sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()), 'warning', false);
  160. }
  161. } else {
  162. $attempt_html .= Display::return_message(sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()), 'warning', false);
  163. }
  164. } else {
  165. $attempt_html .= Display :: return_message(sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()), 'warning', false);
  166. }
  167. if ($origin == 'learnpath') {
  168. Display :: display_reduced_header();
  169. } else {
  170. Display :: display_header($nameTools,'Exercises');
  171. }
  172. echo $attempt_html;
  173. if ($origin != 'learnpath')
  174. Display :: display_footer();
  175. exit;
  176. }
  177. }
  178. if ($debug) { error_log("4. Setting the exe_id: $exe_id");} ;
  179. //5. Getting user exercise info (if the user took the exam before) - generating exe_id
  180. //var_dump($learnpath_id.' - '.$learnpath_item_id.' - '.$learnpath_item_view_id);
  181. $exercise_stat_info = $objExercise->get_stat_track_exercise_info($learnpath_id, $learnpath_item_id, $learnpath_item_view_id);
  182. if (empty($exercise_stat_info)) {
  183. if ($debug) error_log('5 $exercise_stat_info is empty ');
  184. $total_weight = 0;
  185. $questionList = $objExercise->get_question_list(true);
  186. foreach ($questionList as $question_id) {
  187. $objQuestionTmp = Question::read($question_id);
  188. $total_weight += floatval($objQuestionTmp->weighting);
  189. }
  190. $clock_expired_time = '';
  191. if ($time_control) {
  192. $expected_time = $current_timestamp + $total_seconds;
  193. if ($debug) error_log('5.1. $current_timestamp '.$current_timestamp);
  194. if ($debug) error_log('5.2. $expected_time '.$expected_time);
  195. $clock_expired_time = api_get_utc_datetime($expected_time);
  196. if ($debug) error_log('5.3. $expected_time '.$clock_expired_time);
  197. //Sessions that contain the expired time
  198. $_SESSION['expired_time'][$current_expired_time_key] = $clock_expired_time;
  199. if ($debug) { error_log('5.4. Setting the $_SESSION[expired_time]: '.$_SESSION['expired_time'][$current_expired_time_key] ); };
  200. }
  201. $exe_id = $objExercise->save_stat_track_exercise_info($clock_expired_time, $learnpath_id, $learnpath_item_id, $learnpath_item_view_id, $questionList, $total_weight);
  202. $exercise_stat_info = $objExercise->get_stat_track_exercise_info($learnpath_id, $learnpath_item_id, $learnpath_item_view_id);
  203. if ($debug) error_log("5.5 exercise_stat_info[] exists getting exe_id $exe_id");
  204. } else {
  205. $exe_id = $exercise_stat_info['exe_id'];
  206. if ($debug) error_log("5 exercise_stat_info[] exists getting exe_id $exe_id ");
  207. }
  208. //Array to check in order to block the chat
  209. create_chat_exercise_session($exe_id);
  210. if ($debug) { error_log('6. $objExercise->get_stat_track_exercise_info function called:: '.print_r($exercise_stat_info, 1)); };
  211. if (!empty($exercise_stat_info['questions_to_check'])) {
  212. $my_remind_list = $exercise_stat_info['questions_to_check'];
  213. $my_remind_list = explode(',', $my_remind_list);
  214. $my_remind_list = array_filter($my_remind_list);
  215. }
  216. $params = 'exe_id='.$exe_id.'&exerciseId='.$exerciseId.'&origin='.$origin.'&learnpath_id='.$learnpath_id.'&learnpath_item_id='.$learnpath_item_id.'&learnpath_item_view_id='.$learnpath_item_view_id;
  217. if ($debug) { error_log("6.1 params: -> $params"); };
  218. if ($reminder == 2 && empty($my_remind_list)) {
  219. if ($debug) { error_log("6.2 calling the exercise_reminder.php "); };
  220. header('Location: exercise_reminder.php?'.$params);
  221. exit;
  222. }
  223. /*
  224. * 7. Loading Time control parameters
  225. * If the expired time is major that zero(0) then the expired time is compute on this time.
  226. */
  227. if ($time_control) {
  228. if ($debug) error_log('7.1. Time control is enabled');
  229. if ($debug) error_log('7.2. $current_expired_time_key '.$current_expired_time_key);
  230. if ($debug) error_log('7.3. $_SESSION[expired_time][$current_expired_time_key] '.$_SESSION['expired_time'][$current_expired_time_key]);
  231. if (!isset($_SESSION['expired_time'][$current_expired_time_key])) {
  232. //Timer - Get expired_time for a student
  233. if (!empty($exercise_stat_info)) {
  234. if ($debug) {error_log('7.4 Seems that the session ends and the user want to retake the exam'); };
  235. $expired_time_of_this_attempt = $exercise_stat_info['expired_time_control'];
  236. if ($debug) {error_log('7.5 $expired_time_of_this_attempt: '.$expired_time_of_this_attempt); }
  237. //Get the last attempt of an exercice
  238. $last_attempt_date = get_last_attempt_date_of_exercise($exercise_stat_info['exe_id']);
  239. //This means that the user enters the exam but do not answer the first question we get the date from the track_e_exercises not from the track_et_attempt see #2069
  240. if (empty($last_attempt_date)) {
  241. $diff = $current_timestamp - api_strtotime($exercise_stat_info['start_date'], 'UTC');
  242. $last_attempt_date = api_get_utc_datetime(api_strtotime($exercise_stat_info['start_date'],'UTC') + $diff);
  243. } else {
  244. //Recalculate the time control due #2069
  245. $diff = $current_timestamp - api_strtotime($last_attempt_date,'UTC');
  246. $last_attempt_date = api_get_utc_datetime(api_strtotime($last_attempt_date,'UTC') + $diff);
  247. }
  248. if ($debug) {error_log('7.6. $last_attempt_date: '.$last_attempt_date); }
  249. //New expired time - it is due to the possible closure of session
  250. $new_expired_time_in_seconds = api_strtotime($expired_time_of_this_attempt, 'UTC') - api_strtotime($last_attempt_date,'UTC');
  251. if ($debug) {error_log('7.7. $new_expired_time_in_seconds: '.$new_expired_time_in_seconds); }
  252. $expected_time = $current_timestamp + $new_expired_time_in_seconds;
  253. if ($debug) {error_log('7.8. $expected_time1: '.$expected_time); }
  254. $clock_expired_time = api_get_utc_datetime($expected_time);
  255. if ($debug) {error_log('7.9. $clock_expired_time: '.$clock_expired_time); }
  256. // First we update the attempt to today
  257. // How the expired time is changed into "track_e_exercices" table,then the last attempt for this student should be changed too,so
  258. $sql_track_e_exe = "UPDATE $exercice_attemp_table SET tms = '".api_get_utc_datetime()."' WHERE exe_id = '".$exercise_stat_info['exe_id']."' AND tms = '".$last_attempt_date."' ";
  259. if ($debug) {error_log('7.10. $sql_track_e_exe2: '.$sql_track_e_exe); }
  260. Database::query($sql_track_e_exe);
  261. //Sessions that contain the expired time
  262. $_SESSION['expired_time'][$current_expired_time_key] = $clock_expired_time;
  263. if ($debug) {error_log('7.11. Setting the $_SESSION[expired_time]: '.$_SESSION['expired_time'][$current_expired_time_key] ); };
  264. }
  265. } else {
  266. $clock_expired_time = $_SESSION['expired_time'][$current_expired_time_key];
  267. }
  268. } else {
  269. if ($debug) { error_log("7 No time control"); };
  270. }
  271. // Get time left for exipiring time
  272. $time_left = api_strtotime($clock_expired_time,'UTC') - time();
  273. /*
  274. * The time control feature is enable here - this feature is enable for a jquery plugin called epiclock
  275. * for more details of how it works see this link : http://eric.garside.name/docs.html?p=epiclock
  276. */
  277. if ($time_control) { //Sends the exercice form when the expired time is finished
  278. $htmlHeadXtra[] = $objExercise->show_time_control_js($time_left);
  279. }
  280. // if the user has submitted the form
  281. $exercise_title = $objExercise->selectTitle();
  282. $exercise_sound = $objExercise->selectSound();
  283. //Media questions
  284. $media_questions = $objExercise->get_media_list();
  285. $media_is_activated = $objExercise->media_is_activated($media_questions);
  286. //in LP's is enabled the "remember question" feature?
  287. if (!isset($_SESSION['questionList'])) {
  288. // selects the list of question ID
  289. $questionList = $objExercise->get_question_list(false);
  290. //Getting order from random
  291. if ($media_is_activated == false && $objExercise->isRandom() && !empty($exercise_stat_info['data_tracking'])) {
  292. $questionList = explode(',', $exercise_stat_info['data_tracking']);
  293. }
  294. Session::write('questionList', $questionList);
  295. if ($debug > 0) { error_log('$_SESSION[questionList] was set'); }
  296. } else {
  297. if (isset($objExercise) && isset($_SESSION['objExercise'])) {
  298. $questionList = Session::read('questionList');
  299. }
  300. }
  301. if ($debug) error_log('8. Question list loaded '.print_r($questionList, 1));
  302. if ($debug) error_log('8.1 Media list loaded '.print_r($media_questions, 1));
  303. $question_count = $objExercise->get_count_question_list();
  304. if ($formSent && isset($_POST)) {
  305. if ($debug) { error_log('9. $formSent was set'); }
  306. // Initializing
  307. if (!is_array($exerciseResult)) {
  308. $exerciseResult = array();
  309. $exerciseResultCoordinates = array();
  310. }
  311. //Only for hotspot
  312. if (!isset($choice) && isset($_REQUEST['hidden_hotspot_id'])) {
  313. $hotspot_id = (int)($_REQUEST['hidden_hotspot_id']);
  314. $choice = array($hotspot_id => '');
  315. }
  316. // Filling array exercise result
  317. // if the user has answered at least one question
  318. if (is_array($choice)) {
  319. if ($debug) { error_log('9.1. $choice is an array '.print_r($choice, 1)); }
  320. // Also store hotspot spots in the session ($exerciseResultCoordinates
  321. // will be stored in the session at the end of this script)
  322. if (isset($_POST['hotspot'])) {
  323. $exerciseResultCoordinates = $_POST['hotspot'];
  324. if ($debug) { error_log('9.2. $_POST[hotspot] data '.print_r($exerciseResultCoordinates, 1)); }
  325. }
  326. if ($objExercise->type == ALL_ON_ONE_PAGE) {
  327. // $exerciseResult receives the content of the form.
  328. // Each choice of the student is stored into the array $choice
  329. $exerciseResult = $choice;
  330. } else {
  331. // gets the question ID from $choice. It is the key of the array
  332. list ($key) = array_keys($choice);
  333. // if the user didn't already answer this question
  334. if (!isset($exerciseResult[$key])) {
  335. // stores the user answer into the array
  336. $exerciseResult[$key] = $choice[$key];
  337. //saving each question
  338. if ($objExercise->feedback_type != EXERCISE_FEEDBACK_TYPE_DIRECT) {
  339. $nro_question = $current_question; // - 1;
  340. $questionId = $key;
  341. // gets the student choice for this question
  342. $choice = $exerciseResult[$questionId];
  343. if (isset($exe_id)) {
  344. //Manage the question and answer attempts
  345. if ($debug) { error_log('8.3. manage_answer exe_id: '.$exe_id.' - $questionId: '.$questionId.' Choice'.print_r($choice,1)); }
  346. $objExercise->manage_answer($exe_id, $questionId, $choice,'exercise_show',$exerciseResultCoordinates, true, false,false, $objExercise->propagate_neg);
  347. }
  348. //END of saving and qualifying
  349. }
  350. }
  351. }
  352. if ($debug) { error_log('9.3. $choice is an array - end'); }
  353. if ($debug) { error_log('9.4. $exerciseResult '.print_r($exerciseResult,1)); }
  354. }
  355. // the script "exercise_result.php" will take the variable $exerciseResult from the session
  356. Session::write('exerciseResult',$exerciseResult);
  357. Session::write('remind_list', $remind_list);
  358. Session::write('exerciseResultCoordinates',$exerciseResultCoordinates);
  359. // if all questions on one page OR if it is the last question (only for an exercise with one question per page)
  360. if (($objExercise->type == ALL_ON_ONE_PAGE || $current_question >= $question_count)) {
  361. if (api_is_allowed_to_session_edit()) {
  362. // goes to the script that will show the result of the exercise
  363. if ($objExercise->type == ALL_ON_ONE_PAGE) {
  364. if ($debug) { error_log('10. Exercise ALL_ON_ONE_PAGE -> Redirecting to exercise_result.php'); }
  365. //We check if the user attempts before sending to the exercise_result.php
  366. if ($objExercise->selectAttempts() > 0) {
  367. $attempt_count = get_attempt_count(api_get_user_id(), $exerciseId, $learnpath_id, $learnpath_item_id, $learnpath_item_view_id);
  368. if ($attempt_count >= $objExercise->selectAttempts()) {
  369. Display :: display_warning_message(sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()), false);
  370. if ($origin != 'learnpath') {
  371. //so we are not in learnpath tool
  372. echo '</div>'; //End glossary div
  373. Display :: display_footer();
  374. } else {
  375. echo '</body></html>';
  376. }
  377. }
  378. }
  379. header("Location: exercise_result.php?".api_get_cidreq()."&exe_id=$exe_id&origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id&learnpath_item_view_id=$learnpath_item_view_id");
  380. exit;
  381. } else {
  382. //Time control is only enabled for ONE PER PAGE
  383. if (!empty($exe_id) && is_numeric($exe_id)) {
  384. //Verify if the current test is fraudulent
  385. if (exercise_time_control_is_valid($exerciseId, $learnpath_id, $learnpath_item_id)) {
  386. $sql_exe_result = "";
  387. if ($debug) { error_log('exercise_time_control_is_valid is valid'); }
  388. } else {
  389. $sql_exe_result = ", exe_result = 0";
  390. if ($debug) { error_log('exercise_time_control_is_valid is NOT valid then exe_result = 0 '); }
  391. }
  392. /*
  393. //Clean incomplete - @todo why setting to blank the status?
  394. $stat_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  395. $update_query = "UPDATE $stat_table SET status = '', exe_date = '".api_get_utc_datetime() ."' , orig_lp_item_view_id = '$learnpath_item_view_id' $sql_exe_result WHERE exe_id = ".$exe_id;
  396. if ($debug) { error_log('Updating track_e_exercises '.$update_query); }
  397. Database::query($update_query);*/
  398. }
  399. if ($debug) { error_log('10. Redirecting to exercise_show.php'); }
  400. //header("Location: exercise_show.php?id=$exe_id&origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id&learnpath_item_view_id=$learnpath_item_view_id");
  401. header("Location: exercise_result.php?".api_get_cidreq()."&exe_id=$exe_id&origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id&learnpath_item_view_id=$learnpath_item_view_id");
  402. exit;
  403. }
  404. } else {
  405. if ($debug) { error_log('10. Redirecting to exercise_submit.php'); }
  406. header("Location: exercise_submit.php?".api_get_cidreq()."&exerciseId=$exerciseId&origin=$origin");
  407. exit;
  408. }
  409. }
  410. if ($debug) { error_log('11. $formSent was set - end'); }
  411. } else {
  412. if ($debug) { error_log('9. $formSent was NOT sent'); }
  413. }
  414. // if questionNum comes from POST and not from GET
  415. if (!$current_question || $_REQUEST['num']) {
  416. if (!$current_question) {
  417. $current_question = 1;
  418. } else {
  419. $current_question++;
  420. }
  421. }
  422. if ($question_count != 0) {
  423. if (($objExercise->type == ALL_ON_ONE_PAGE || $current_question > $question_count)) {
  424. if (api_is_allowed_to_session_edit()) {
  425. // goes to the script that will show the result of the exercise
  426. if ($objExercise->type == ALL_ON_ONE_PAGE) {
  427. if ($debug) { error_log('12. Exercise ALL_ON_ONE_PAGE -> Redirecting to exercise_result.php'); }
  428. //We check if the user attempts before sending to the exercise_result.php
  429. if ($objExercise->selectAttempts() > 0) {
  430. $attempt_count = get_attempt_count(api_get_user_id(), $exerciseId, $learnpath_id, $learnpath_item_id, $learnpath_item_view_id);
  431. if ($attempt_count >= $objExercise->selectAttempts()) {
  432. Display :: display_warning_message(sprintf(get_lang('ReachedMaxAttempts'), $exercise_title, $objExercise->selectAttempts()), false);
  433. if ($origin != 'learnpath') {
  434. //so we are not in learnpath tool
  435. echo '</div>'; //End glossary div
  436. Display :: display_footer();
  437. } else {
  438. echo '</body></html>';
  439. }
  440. exit;
  441. }
  442. }
  443. } else {
  444. //Time control is only enabled for ONE PER PAGE
  445. if (!empty($exe_id) && is_numeric($exe_id)) {
  446. //Verify if the current test is fraudulent
  447. $check = exercise_time_control_is_valid($exerciseId, $learnpath_id, $learnpath_item_id);
  448. if ($check) {
  449. $sql_exe_result = "";
  450. if ($debug) { error_log('12. exercise_time_control_is_valid is valid'); }
  451. } else {
  452. $sql_exe_result = ", exe_result = 0";
  453. if ($debug) { error_log('12. exercise_time_control_is_valid is NOT valid then exe_result = 0 '); }
  454. }
  455. }
  456. if ($objExercise->review_answers) {
  457. header('Location: exercise_reminder.php?'.$params);
  458. exit;
  459. } else {
  460. header("Location: exercise_result.php?".api_get_cidreq()."&exe_id=$exe_id&origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id&learnpath_item_view_id=$learnpath_item_view_id");
  461. }
  462. }
  463. } else {
  464. if ($debug) { error_log('Redirecting to exercise_submit.php'); }
  465. //header("Location: exercise_submit.php?exerciseId=$exerciseId");
  466. exit;
  467. }
  468. }
  469. } else {
  470. $error = get_lang('ThereAreNoQuestionsForThisExercise');
  471. // if we are in the case where user select random by category, but didn't choose the number of random question
  472. if ($objExercise->selectRandomByCat() > 0 && $objExercise->random <= 0) {
  473. $error .= "<br/>".get_lang('PleaseSelectSomeRandomQuestion');
  474. }
  475. }
  476. if (!empty ($_GET['gradebook']) && $_GET['gradebook'] == 'view') {
  477. $_SESSION['gradebook'] = Security :: remove_XSS($_GET['gradebook']);
  478. $gradebook = $_SESSION['gradebook'];
  479. } elseif (empty ($_GET['gradebook'])) {
  480. unset ($_SESSION['gradebook']);
  481. $gradebook = '';
  482. }
  483. if (!empty ($gradebook) && $gradebook == 'view') {
  484. $interbreadcrumb[]= array ('url' => '../gradebook/' . Security::remove_XSS($_SESSION['gradebook_dest']),'name' => get_lang('ToolGradebook'));
  485. }
  486. $interbreadcrumb[]= array ("url" => "exercice.php?gradebook=$gradebook", "name" => get_lang('Exercices'));
  487. $interbreadcrumb[]= array ("url" => "#","name" => $objExercise->name);
  488. if ($origin != 'learnpath') { //so we are not in learnpath tool
  489. Display :: display_header($nameTools,'Exercises');
  490. if (!api_is_allowed_to_session_edit()) {
  491. Display :: display_warning_message(get_lang('SessionIsReadOnly'));
  492. }
  493. } else {
  494. Display::display_reduced_header();
  495. echo '<div style="height:10px">&nbsp;</div>';
  496. }
  497. $show_quiz_edition = $objExercise->added_in_lp();
  498. // I'm in a preview mode
  499. if (api_is_course_admin() && $origin != 'learnpath') {
  500. echo '<div class="actions">';
  501. //echo '<a href="exercice.php?show=test&id_session='.api_get_session_id().'">' . Display :: return_icon('back.png', get_lang('BackToExercisesList'),'',ICON_SIZE_MEDIUM).'</a>';
  502. if ($show_quiz_edition == false) {
  503. 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>';
  504. } else {
  505. echo '<a href="#">'.Display::return_icon('settings_na.png', get_lang('ModifyExercise'),'',ICON_SIZE_MEDIUM).'</a>';
  506. }
  507. echo '</div>';
  508. }
  509. $is_visible_return = $objExercise->is_visible($learnpath_id, $learnpath_item_id, $learnpath_item_view_id);
  510. if ($is_visible_return['value'] == false) {
  511. echo $is_visible_return['message'];
  512. if ($origin != 'learnpath') {
  513. Display :: display_footer();
  514. }
  515. exit;
  516. }
  517. $limit_time_exists = (($objExercise->start_time != '0000-00-00 00:00:00') || ($objExercise->end_time != '0000-00-00 00:00:00')) ? true : false;
  518. if ($limit_time_exists) {
  519. $exercise_start_time = api_strtotime($objExercise->start_time, 'UTC');
  520. $exercise_end_time = api_strtotime($objExercise->end_time, 'UTC');
  521. $time_now = time();
  522. if ($objExercise->start_time != '0000-00-00 00:00:00') {
  523. $permission_to_start = (($time_now - $exercise_start_time) > 0) ? true : false;
  524. } else {
  525. $permission_to_start = true;
  526. }
  527. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  528. if ($objExercise->end_time != '0000-00-00 00:00:00') {
  529. $exercise_timeover = (($time_now - $exercise_end_time) > 0) ? true : false;
  530. } else {
  531. $exercise_timeover = false;
  532. }
  533. }
  534. if (!$permission_to_start || $exercise_timeover) {
  535. if (!api_is_allowed_to_edit(null,true)) {
  536. $message_warning = $permission_to_start ? get_lang('ReachedTimeLimit') : get_lang('ExerciseNoStartedYet');
  537. Display :: display_warning_message(sprintf($message_warning, $exercise_title, $objExercise->selectAttempts()));
  538. if ($origin != 'learnpath') {
  539. Display :: display_footer();
  540. }
  541. exit;
  542. } else {
  543. $message_warning = $permission_to_start ? get_lang('ReachedTimeLimitAdmin') : get_lang('ExerciseNoStartedAdmin');
  544. Display :: display_warning_message(sprintf($message_warning, $exercise_title, $objExercise->selectAttempts()));
  545. }
  546. }
  547. }
  548. // Blocking empty start times see BT#2800
  549. global $_custom;
  550. if (isset($_custom['exercises_hidden_when_no_start_date']) && $_custom['exercises_hidden_when_no_start_date']) {
  551. if (empty($objExercise->start_time) || $objExercise->start_time == '0000-00-00 00:00:00') {
  552. Display :: display_warning_message(sprintf(get_lang('ExerciseNoStartedYet'), $exercise_title, $objExercise->selectAttempts()));
  553. if ($origin != 'learnpath') {
  554. Display :: display_footer();
  555. }
  556. }
  557. }
  558. //Timer control
  559. if ($time_control) {
  560. echo $objExercise->return_time_left_div();
  561. echo '<div style="display:none" class="warning-message" id="expired-message-id">'.get_lang('ExerciceExpiredTimeMessage').'</div>';
  562. }
  563. if (!empty($objExercise->description)) {
  564. echo Display::generate_accordion(array( array('title' => get_lang('ExerciseDescriptionLabel'), 'content' => $objExercise->description)));
  565. }
  566. if ($origin != 'learnpath') {
  567. echo '<div id="highlight-plugin" class="glossary-content">';
  568. }
  569. if ($reminder == 2) {
  570. if ($debug) { error_log('. $reminder == 2'); }
  571. $data_tracking = $exercise_stat_info['data_tracking'];
  572. $data_tracking = explode(',', $data_tracking);
  573. $current_question = 1; //set by default the 1st question
  574. if (!empty($my_remind_list)) {
  575. //Checking which questions we are going to call from the remind list
  576. for ($i = 0; $i < count($data_tracking); $i++) {
  577. for($j = 0; $j < count($my_remind_list); $j++) {
  578. if (!empty($remind_question_id)) {
  579. if ($remind_question_id == $my_remind_list[$j]) {
  580. if ($remind_question_id == $data_tracking[$i]) {
  581. if (isset($my_remind_list[$j+1])) {
  582. $remind_question_id = $my_remind_list[$j+1];
  583. $current_question = $i + 1;
  584. } else {
  585. $remind_question_id = -1; //We end the remind list we go to the exercise_reminder.php please
  586. $current_question = $i + 1; // last question
  587. }
  588. break 2;
  589. }
  590. }
  591. } else {
  592. if ($my_remind_list[$j] == $data_tracking[$i]) {
  593. if (isset($my_remind_list[$j+1])) {
  594. $remind_question_id = $my_remind_list[$j+1];
  595. $current_question = $i + 1; // last question
  596. } else {
  597. $remind_question_id = -1; //We end the remind list we go to the exercise_reminder.php please
  598. $current_question = $i + 1; // last question
  599. }
  600. break 2;
  601. }
  602. }
  603. }
  604. }
  605. } else {
  606. if ($objExercise->review_answers) {
  607. if ($debug) { error_log('. redirecting to exercise_reminder.php '); }
  608. header("Location: exercise_reminder.php?$params");
  609. exit;
  610. }
  611. }
  612. //var_dump($remind_question_id, $my_remind_list, $data_tracking, $current_question);
  613. }
  614. if ($objExercise->review_answers) {
  615. $script_php = 'exercise_reminder.php';
  616. } else {
  617. $script_php = 'exercise_result.php';
  618. }
  619. if (!empty($error)) {
  620. Display :: display_error_message($error, false);
  621. } else {
  622. if (!empty ($exercise_sound)) {
  623. 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>";
  624. }
  625. // Get number of hotspot questions for javascript validation
  626. $number_of_hotspot_questions = 0;
  627. $onsubmit = '';
  628. $i = 0;
  629. if (!empty($questionList)) {
  630. foreach ($questionList as $questionId) {
  631. $i++;
  632. $objQuestionTmp = Question::read($questionId);
  633. // for sequential exercises
  634. if ($objExercise->type == ONE_PER_PAGE) {
  635. // if it is not the right question, goes to the next loop iteration
  636. if ($current_question != $i) {
  637. continue;
  638. } else {
  639. if ($objQuestionTmp->selectType() == HOT_SPOT || $objQuestionTmp->selectType() == HOT_SPOT_DELINEATION) {
  640. $number_of_hotspot_questions++;
  641. }
  642. break;
  643. }
  644. } else {
  645. if ($objQuestionTmp->selectType() == HOT_SPOT || $objQuestionTmp->selectType() == HOT_SPOT_DELINEATION) {
  646. $number_of_hotspot_questions++;
  647. }
  648. }
  649. }
  650. }
  651. if ($number_of_hotspot_questions > 0) {
  652. $onsubmit = "onsubmit=\"return validateFlashVar('" . $number_of_hotspot_questions . "', '" . get_lang('HotspotValidateError1') . "', '" . get_lang('HotspotValidateError2') . "');\"";
  653. }
  654. echo '<script>
  655. $(function() {
  656. //$(".exercise_save_now_button").hide();
  657. $(".main_question").mouseover(function() {
  658. //$(this).find(".exercise_save_now_button").show();
  659. //$(this).addClass("question_highlight");
  660. });
  661. $(".main_question").mouseout(function() {
  662. //$(this).find(".exercise_save_now_button").hide();
  663. $(this).removeClass("question_highlight");
  664. });
  665. $(".no_remind_highlight").hide();
  666. });
  667. function previous_question(question_num) {
  668. url = "exercise_submit.php?'.$params.'&num="+question_num;
  669. window.location = url;
  670. }
  671. function previous_question_and_save(previous_question_id, question_id_to_save) {
  672. url = "exercise_submit.php?'.$params.'&num="+previous_question_id;
  673. //Save the current question
  674. save_now(question_id_to_save, url);
  675. }
  676. function save_question_list(question_list) {
  677. $.each(question_list, function(key, question_id) {
  678. save_now(question_id, null, false);
  679. });
  680. var url = "";
  681. if ('.$reminder.' == 1 ) {
  682. url = "exercise_reminder.php?'.$params.'&num='.$current_question.'";
  683. } else if ('.$reminder.' == 2 ) {
  684. url = "exercise_submit.php?'.$params.'&num='.$current_question.'&remind_question_id='.$remind_question_id.'&reminder=2";
  685. } else {
  686. url = "exercise_submit.php?'.$params.'&num='.$current_question.'&remind_question_id='.$remind_question_id.'";
  687. }
  688. //$("#save_for_now_"+question_id).html("'.addslashes(Display::return_icon('save.png', get_lang('Saved'), array(), ICON_SIZE_SMALL)).'");
  689. window.location = url;
  690. }
  691. function save_now(question_id, url_extra, redirect) {
  692. if (redirect == undefined) {
  693. redirect = true;
  694. }
  695. //1. Normal choice inputs
  696. var my_choice = $(\'*[name*="choice[\'+question_id+\']"]\').serialize();
  697. //2. Reminder checkbox
  698. var remind_list = $(\'*[name*="remind_list"]\').serialize();
  699. //3. Hotspots
  700. var hotspot = $(\'*[name*="hotspot[\'+question_id+\']"]\').serialize();
  701. //Checking FCK
  702. if (typeof(FCKeditorAPI) !== "undefined") {
  703. var oEditor = FCKeditorAPI.GetInstance("choice["+question_id+"]") ;
  704. var fck_content = "";
  705. if (oEditor) {
  706. fck_content = oEditor.GetHTML();
  707. my_choice = {};
  708. my_choice["choice["+question_id+"]"] = fck_content;
  709. my_choice = $.param(my_choice);
  710. }
  711. }
  712. if ($(\'input[name="remind_list[\'+question_id+\']"]\').is(\':checked\')) {
  713. $("#question_div_"+question_id).addClass("remind_highlight");
  714. } else {
  715. $("#question_div_"+question_id).removeClass("remind_highlight");
  716. }
  717. // Only for the first time
  718. $("#save_for_now_"+question_id).html(" '.addslashes(Display::return_icon('loading1.gif')).'");
  719. $.ajax({
  720. type:"post",
  721. async: false,
  722. url: "'.api_get_path(WEB_AJAX_PATH).'exercise.ajax.php?a=save_exercise_by_now",
  723. data: "'.$params.'&type=simple&question_id="+question_id+"&"+my_choice+"&"+hotspot+"&"+remind_list,
  724. success: function(return_value) {
  725. if (return_value == "ok") {
  726. $("#save_for_now_"+question_id).html("'.addslashes(Display::return_icon('save.png', get_lang('Saved'), array(), ICON_SIZE_SMALL)).'");
  727. } else if (return_value == "error") {
  728. $("#save_for_now_"+question_id).html("'.addslashes(Display::return_icon('error.png', get_lang('Error'), array(), ICON_SIZE_SMALL)).'");
  729. } else if (return_value == "one_per_page") {
  730. var url = "";
  731. if ('.$reminder.' == 1 ) {
  732. url = "exercise_reminder.php?'.$params.'&num='.$current_question.'";
  733. } else if ('.$reminder.' == 2 ) {
  734. url = "exercise_submit.php?'.$params.'&num='.$current_question.'&remind_question_id='.$remind_question_id.'&reminder=2";
  735. } else {
  736. url = "exercise_submit.php?'.$params.'&num='.$current_question.'&remind_question_id='.$remind_question_id.'";
  737. }
  738. if (url_extra) {
  739. url = url_extra;
  740. }
  741. $("#save_for_now_"+question_id).html("'.addslashes(Display::return_icon('save.png', get_lang('Saved'), array(), ICON_SIZE_SMALL)).'");
  742. if (redirect) {
  743. window.location = url;
  744. }
  745. }
  746. },
  747. error: function() {
  748. $("#save_for_now_"+question_id).html("'.addslashes(Display::return_icon('error.png', get_lang('Error'), array(), ICON_SIZE_SMALL)).'");
  749. }
  750. });
  751. return false;
  752. }
  753. function save_now_all(validate) {
  754. //1. Input choice
  755. var my_choice = $(\'*[name*="choice"]\').serialize();
  756. //2. Reminder
  757. var remind_list = $(\'*[name*="remind_list"]\').serialize();
  758. //3. Hotspots
  759. var hotspot = $(\'*[name*="hotspot"]\').serialize();
  760. //Question list
  761. var question_list = ['.implode(',', $questionList).'];
  762. var free_answers = {};
  763. $.each(question_list, function(index, my_question_id) {
  764. //Checking FCK
  765. if (typeof(FCKeditorAPI) !== "undefined") {
  766. var oEditor = FCKeditorAPI.GetInstance("choice["+my_question_id+"]") ;
  767. var fck_content = "";
  768. if (oEditor) {
  769. fck_content = oEditor.GetHTML();
  770. //alert(index + " " +my_question_id + " " +fck_content);
  771. free_answers["free_choice["+my_question_id+"]"] = fck_content;
  772. }
  773. }
  774. });
  775. //lok+(fgt)= data base
  776. free_answers = $.param(free_answers);
  777. $("#save_all_reponse").html("'.addslashes(Display::return_icon('loading1.gif')).'");
  778. $.ajax({
  779. type:"post",
  780. async: false,
  781. url: "'.api_get_path(WEB_AJAX_PATH).'exercise.ajax.php?a=save_exercise_by_now",
  782. data: "'.$params.'&type=all&"+my_choice+"&"+hotspot+"&"+free_answers+"&"+remind_list,
  783. success: function(return_value) {
  784. if (return_value == "ok") {
  785. //$("#save_all_reponse").html("'.addslashes(Display::return_icon('accept.png')).'");
  786. if (validate == "validate") {
  787. window.location = "'.$script_php.'?'.$params.'";
  788. } else {
  789. $("#save_all_reponse").html("'.addslashes(Display::return_icon('accept.png')).'");
  790. }
  791. } else {
  792. $("#save_all_reponse").html("'.addslashes(Display::return_icon('wrong.gif')).'");
  793. }
  794. },
  795. });
  796. return false;
  797. }
  798. function validate_all() {
  799. save_now_all("validate");
  800. return false;
  801. }
  802. </script>';
  803. echo '<form id="exercise_form" method="post" action="'.api_get_self().'?'.api_get_cidreq().'&autocomplete=off&gradebook='.$gradebook."&exerciseId=" . $exerciseId .'" name="frm_exercise" '.$onsubmit.'>
  804. <input type="hidden" name="formSent" value="1" />
  805. <input type="hidden" name="exerciseId" value="'.$exerciseId . '" />
  806. <input type="hidden" name="num" value="'.$current_question.'" id="num_current_id" />
  807. <input type="hidden" name="exe_id" value="'.$exe_id . '" />
  808. <input type="hidden" name="origin" value="'.$origin . '" />
  809. <input type="hidden" name="learnpath_id" value="'.$learnpath_id . '" />
  810. <input type="hidden" name="learnpath_item_id" value="'.$learnpath_item_id . '" />
  811. <input type="hidden" name="learnpath_item_view_id" value="'.$learnpath_item_view_id . '" />';
  812. //Show list of questions
  813. $attempt_list = array();
  814. if (isset($exe_id)) {
  815. $attempt_list = get_all_exercise_event_by_exe_id($exe_id);
  816. }
  817. $remind_list = array();
  818. if (isset($exercise_stat_info['questions_to_check']) && !empty($exercise_stat_info['questions_to_check'])) {
  819. $remind_list = explode(',', $exercise_stat_info['questions_to_check']);
  820. }
  821. render_question_list($objExercise, $questionList, $current_question, $exerciseResult, $attempt_list, $remind_list, $media_questions);
  822. echo '</form>';
  823. }
  824. function render_question_list($objExercise, $questionList, $current_question, $exerciseResult, $attempt_list, $remind_list, $media_questions = array()) {
  825. global $origin;
  826. $i = 1;
  827. //Normal question list render
  828. foreach ($questionList as $questionId) {
  829. // for sequential exercises
  830. if ($objExercise->type == ONE_PER_PAGE) {
  831. // if it is not the right question, goes to the next loop iteration
  832. if ($current_question != $i) {
  833. $i++;
  834. continue;
  835. } else {
  836. if ($objExercise->feedback_type != EXERCISE_FEEDBACK_TYPE_DIRECT) {
  837. // if the user has already answered this question
  838. if (isset($exerciseResult[$questionId])) {
  839. // construction of the Question object
  840. $objQuestionTmp = Question::read($questionId);
  841. // destruction of the Question object
  842. unset ($objQuestionTmp);
  843. Display :: display_normal_message(get_lang('AlreadyAnswered'));
  844. $i++;
  845. break;
  846. }
  847. }
  848. }
  849. }
  850. //Medias question render
  851. if (isset($media_questions) && !empty($media_questions)) {
  852. $media_question_list = $media_questions[$questionId];
  853. $objQuestionTmp = Question::read($questionId);
  854. $counter = 1;
  855. if ($objQuestionTmp->type == MEDIA_QUESTION) {
  856. echo $objQuestionTmp->show_media_content();
  857. $count_of_questions_inside_media = count($media_question_list);
  858. //var_dump($media_question_list);
  859. //Show questions that belongs to a media
  860. if (!empty($media_question_list)) {
  861. foreach ($media_question_list as $my_question_id) {
  862. if ($counter == $count_of_questions_inside_media) {
  863. $last_question_in_media = true;
  864. }
  865. render_question($objExercise, $my_question_id, $attempt_list, $remind_list, $i, $current_question, $media_question_list, $last_question_in_media);
  866. $counter++;
  867. }
  868. }
  869. } else {
  870. render_question($objExercise, $questionId, $attempt_list, $remind_list, $i, $current_question);
  871. }
  872. } else {
  873. //Normal question render
  874. render_question($objExercise, $questionId, $attempt_list, $remind_list, $i, $current_question);
  875. }
  876. $i++;
  877. // for sequential exercises
  878. if ($objExercise->type == ONE_PER_PAGE) {
  879. // quits the loop
  880. break;
  881. }
  882. }
  883. // end foreach()
  884. if ($objExercise->type == ALL_ON_ONE_PAGE) {
  885. $exercise_actions = $objExercise->show_button($questionId, $current_question);
  886. echo Display::div($exercise_actions, array('class'=>'exercise_actions'));
  887. }
  888. }
  889. function render_question($objExercise, $questionId, $attempt_list, $remind_list, $i, $current_question, $questions_in_media = array(), $last_question_in_media = false) {
  890. global $origin;
  891. $user_choice = isset($attempt_list[$questionId]) ? $attempt_list[$questionId] : null;
  892. $remind_highlight = null;
  893. //Hides questions when reviewing a ALL_ON_ONE_PAGE exercise see #4542 no_remind_highlight class hide with jquery
  894. if ($objExercise->type == ALL_ON_ONE_PAGE && isset($_GET['reminder']) && $_GET['reminder'] == 2) {
  895. $remind_highlight = 'no_remind_highlight';
  896. }
  897. $is_remind_on = false;
  898. $attributes = array('id' =>'remind_list['.$questionId.']');
  899. if (in_array($questionId, $remind_list)) {
  900. $is_remind_on = true;
  901. $attributes['checked'] = 1;
  902. $remind_highlight = ' remind_highlight ';
  903. }
  904. //Showing the question
  905. $exercise_actions = null;
  906. echo '<div id="question_div_'.$questionId.'" class="main_question '.$remind_highlight.'" >';
  907. //Shows the question + possible answers
  908. showQuestion($questionId, false, $origin, $i, true, false, $user_choice, false);
  909. //BUtton save and continue
  910. switch ($objExercise->type) {
  911. case ONE_PER_PAGE:
  912. $exercise_actions .= $objExercise->show_button($questionId, $current_question);
  913. break;
  914. case ALL_ON_ONE_PAGE :
  915. $button = '<a href="javascript://" class="btn" onclick="save_now(\''.$questionId.'\'); ">'.get_lang('SaveForNow').'</a>';
  916. $button .= '<span id="save_for_now_'.$questionId.'" class="exercise_save_mini_message"></span>&nbsp;';
  917. $exercise_actions .= Display::div($button, array('class'=>'exercise_save_now_button'));
  918. break;
  919. }
  920. if (!empty($questions_in_media)) {
  921. /*$button = '<a href="javascript://" class="btn" onclick="save_now(\''.$questionId.'\'); ">'.get_lang('SaveForNow').'</a>';
  922. $button .= '<span id="save_for_now_'.$questionId.'"></span>&nbsp;';
  923. $exercise_actions = Display::div($button, array('class'=>'exercise_save_now_button'));
  924. $exercise_actions .= $objExercise->show_button($questionId, $current_question);*/
  925. $count_of_questions_inside_media = count($questions_in_media);
  926. if ($count_of_questions_inside_media > 1) {
  927. $button = '<a href="javascript://" class="btn" onclick="save_now(\''.$questionId.'\', false, false); ">'.get_lang('SaveForNow').'</a>';
  928. $button .= '<span id="save_for_now_'.$questionId.'" class="exercise_save_mini_message"></span>&nbsp;';
  929. $exercise_actions = Display::div($button, array('class'=>'exercise_save_now_button'));
  930. }
  931. if ($last_question_in_media) {
  932. $exercise_actions = $objExercise->show_button($questionId, $current_question, $questions_in_media);
  933. }
  934. }
  935. //Checkbox review answers
  936. if ($objExercise->review_answers) {
  937. $remind_question_div = Display::tag('label', Display::input('checkbox', 'remind_list['.$questionId.']', '', $attributes).get_lang('ReviewQuestionLater'), array('class' => 'checkbox', 'for' =>'remind_list['.$questionId.']'));
  938. $exercise_actions .= Display::div($remind_question_div, array('class'=>'exercise_save_now_button'));
  939. }
  940. echo Display::div($exercise_actions, array('class'=>'form-actions'));
  941. echo '</div>';
  942. }
  943. if ($origin != 'learnpath') {
  944. //so we are not in learnpath tool
  945. echo '</div>'; //End glossary div
  946. Display :: display_footer();
  947. } else {
  948. echo '</body></html>';
  949. }