exercise_submit.php 49 KB

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