exercice_submit.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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 multiple fill in blank option added (2008) and Cleaning exercises (2010)
  21. */
  22. require_once 'exercise.class.php';
  23. require_once 'question.class.php';
  24. require_once 'answer.class.php';
  25. require_once 'exercise.lib.php';
  26. // debug var. Set to 0 to hide all debug display. Set to 1 to display debug messages.
  27. $debug = 0;
  28. // name of the language file that needs to be included
  29. $language_file = 'exercice';
  30. require_once '../inc/global.inc.php';
  31. $this_section = SECTION_COURSES;
  32. // Notice for unauthorized people.
  33. api_protect_course_script(true);
  34. $is_allowedToEdit = api_is_allowed_to_edit(null,true);
  35. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.js" type="text/javascript" language="javascript"></script>'; //jQuery
  36. if (api_get_setting('show_glossary_in_extra_tools') == 'true') {
  37. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/glossary.js" type="text/javascript" language="javascript"></script>'; //Glossary
  38. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.highlight.js" type="text/javascript" language="javascript"></script>';
  39. }
  40. //@todo we should only enable this when there is a time control
  41. //This library is necessary for the time control feature
  42. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.epiclock.min.js" type="text/javascript" language="javascript"></script>'; //jQuery
  43. $_configuration['live_exercise_tracking'] = true;
  44. $stat_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  45. $exercice_attemp_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  46. $TBL_EXERCICE_QUESTION = Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION);
  47. $TBL_EXERCICES = Database :: get_course_table(TABLE_QUIZ_TEST);
  48. $TBL_QUESTIONS = Database :: get_course_table(TABLE_QUIZ_QUESTION);
  49. $TBL_REPONSES = Database :: get_course_table(TABLE_QUIZ_ANSWER);
  50. // General parameters passed via POST/GET
  51. if (empty ($origin)) {
  52. $origin = $_REQUEST['origin'];
  53. }
  54. if (empty ($learnpath_id)) {
  55. $learnpath_id = intval($_REQUEST['learnpath_id']);
  56. }
  57. if (empty ($learnpath_item_id)) {
  58. $learnpath_item_id = intval($_REQUEST['learnpath_item_id']);
  59. }
  60. if (empty ($learnpath_item_view_id)) {
  61. $learnpath_item_view_id = intval($_REQUEST['learnpath_item_view_id']);
  62. }
  63. if (empty ($formSent)) {
  64. $formSent = $_REQUEST['formSent'];
  65. }
  66. if (empty ($exerciseResult)) {
  67. $exerciseResult = $_REQUEST['exerciseResult'];
  68. }
  69. if (empty ($exerciseType)) {
  70. $exerciseType = $_REQUEST['exerciseType'];
  71. }
  72. if (empty ($exerciseId)) {
  73. $exerciseId = intval($_REQUEST['exerciseId']);
  74. }
  75. if (empty ($choice)) {
  76. $choice = $_REQUEST['choice'];
  77. }
  78. if (empty ($_REQUEST['choice'])) {
  79. $choice = $_REQUEST['choice2'];
  80. }
  81. if (empty ($questionNum)) {
  82. $questionNum = intval($_REQUEST['questionNum']);
  83. }
  84. if (empty ($nbrQuestions)) {
  85. $nbrQuestions = intval($_REQUEST['nbrQuestions']);
  86. }
  87. if (empty ($buttonCancel)) {
  88. $buttonCancel = $_REQUEST['buttonCancel'];
  89. }
  90. $error = '';
  91. // if the user has clicked on the "Cancel" button
  92. if ($buttonCancel) {
  93. // returns to the exercise list
  94. header("Location: exercice.php?origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id&learnpath_item_view_id=$learnpath_item_view_id");
  95. exit;
  96. }
  97. if ($origin == 'builder') {
  98. /* Clears the exercise session */
  99. if ($debug) {error_log('origin = builder'); };
  100. if (isset ($_SESSION['objExercise'])) {
  101. api_session_unregister('objExercise');
  102. unset ($objExercise);
  103. }
  104. if (isset ($_SESSION['objQuestion'])) {
  105. api_session_unregister('objQuestion');
  106. unset ($objQuestion);
  107. }
  108. if (isset ($_SESSION['objAnswer'])) {
  109. api_session_unregister('objAnswer');
  110. unset ($objAnswer);
  111. }
  112. if (isset ($_SESSION['questionList'])) {
  113. api_session_unregister('questionList');
  114. unset ($questionList);
  115. }
  116. if (isset ($_SESSION['newquestionList'])) {
  117. api_session_unregister('newquestionList');
  118. unset ($newquestionList);
  119. }
  120. if (isset ($_SESSION['exerciseResult'])) {
  121. api_session_unregister('exerciseResult');
  122. unset ($exerciseResult);
  123. }
  124. if (isset ($_SESSION['exerciseResultCoordinates'])) {
  125. api_session_unregister('exerciseResultCoordinates');
  126. unset ($exerciseResultCoordinates);
  127. }
  128. }
  129. $safe_lp_id = ($learnpath_id == '') ? 0 : $learnpath_id;
  130. $safe_lp_item_id = ($learnpath_item_id == '') ? 0 : $learnpath_item_id;
  131. $safe_lp_item_view_id = ($learnpath_item_view_id == '') ? 0 : $learnpath_item_view_id;
  132. // Loading the $objExercise variable
  133. //if (!isset($_SESSION['objExercise']) || $origin == 'learnpath' || $_SESSION['objExercise']->id != $_REQUEST['exerciseId']) {
  134. if (!isset ($_SESSION['objExercise']) || $_SESSION['objExercise']->id != $_REQUEST['exerciseId']) {
  135. // Construction of Exercise
  136. $objExercise = new Exercise();
  137. if ($debug) {error_log('Setting the $objExercise variable'); };
  138. unset($_SESSION['questionList']);
  139. // if the specified exercise doesn't exist or is disabled
  140. if (!$objExercise->read($exerciseId) || (!$objExercise->selectStatus() && !$is_allowedToEdit && ($origin != 'learnpath'))) {
  141. if ($debug) {error_log('Error while reading the exercise'); };
  142. unset ($objExercise);
  143. $error = get_lang('ExerciseNotFound');
  144. } else {
  145. // saves the object into the session
  146. api_session_register('objExercise');
  147. if ($debug) {error_log('$_SESSION[objExercise] was unset - set now - end'); };
  148. }
  149. }
  150. if (!isset ($objExercise) && isset($_SESSION['objExercise'])) {
  151. if ($debug) {error_log('Loading $objExercise from session'); };
  152. $objExercise = $_SESSION['objExercise'];
  153. }
  154. if (!is_object($objExercise)) {
  155. if ($debug) {error_log('$objExercise was not set, kill the script'); };
  156. header('Location: exercice.php');
  157. exit;
  158. }
  159. $exerciseType = $objExercise->type;
  160. $current_timestamp = time();
  161. //Getting track exercise info
  162. $exercise_stat_info = $objExercise->get_stat_track_exercise_info($safe_lp_id, $safe_lp_item_id, $safe_lp_item_view_id);
  163. if ($debug) {error_log('$objExercise->get_stat_track_exercise_info function called:: '.print_r($exercise_stat_info, 1)); };
  164. /*
  165. * Time control feature
  166. * if the expired time is major that zero(0) then the expired time is compute on this time. Disable for learning path
  167. */
  168. $time_control = false;
  169. if ($objExercise->expired_time != 0 && $origin != 'learnpath') {
  170. $time_control = true;
  171. }
  172. if ($time_control) {
  173. //Get the expired time of the current exercice in track_e_exercices
  174. $total_seconds = $objExercise->expired_time*60;
  175. //Generating the time control key
  176. $current_expired_time_key = generate_time_control_key($objExercise->id);
  177. if ($debug) {error_log('$current_expired_time_key '.$current_expired_time_key); };
  178. if (!isset($_SESSION['expired_time'][$current_expired_time_key])) {
  179. //Timer - Get expired_time for a student
  180. if (!empty($exercise_stat_info)) {
  181. if ($debug) {error_log('Seems that the session ends and the user want to retake the exam'); };
  182. $expired_time_of_this_attempt = $exercise_stat_info['expired_time_control'];
  183. if ($debug) {error_log('$expired_time_of_this_attempt: '.$expired_time_of_this_attempt); }
  184. //Get the last attempt of an exercice
  185. $last_attempt_date = get_last_attempt_date_of_exercise($exercise_stat_info['exe_id']);
  186. //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
  187. if (empty($last_attempt_date)) {
  188. $diff = $current_timestamp - api_strtotime($exercise_stat_info['start_date'], 'UTC');
  189. $last_attempt_date = api_get_utc_datetime(api_strtotime($exercise_stat_info['start_date'],'UTC') + $diff);
  190. } else {
  191. //Recalculate the time control due #2069
  192. $diff = $current_timestamp - api_strtotime($last_attempt_date,'UTC');
  193. $last_attempt_date = api_get_utc_datetime(api_strtotime($last_attempt_date,'UTC') + $diff);
  194. }
  195. if ($debug) {error_log('$last_attempt_date: '.$last_attempt_date); }
  196. //New expired time - it is due to the possible closure of session
  197. $new_expired_time_in_seconds = api_strtotime($expired_time_of_this_attempt, 'UTC') - api_strtotime($last_attempt_date,'UTC');
  198. if ($debug) {error_log('$new_expired_time_in_seconds: '.$new_expired_time_in_seconds); }
  199. $expected_time = $current_timestamp + $new_expired_time_in_seconds;
  200. if ($debug) {error_log('$expected_time1: '.$expected_time); }
  201. //$plugin_expired_time = date('M d, Y H:i:s', $expected_time);
  202. $clock_expired_time = api_get_utc_datetime($expected_time);
  203. if ($debug) {error_log('$clock_expired_time: '.$clock_expired_time); }
  204. //@todo check this validation with Fasa: With this change the user can log out and login from the system and the counter will not work
  205. /*
  206. //We modify the "expired_time_control" field in track_e_exercices for this attempt
  207. $sql_track_e_exe = "UPDATE $stat_table SET expired_time_control = '".$clock_expired_time."' WHERE exe_id = '".$exercise_stat_info['exe_id']."'";
  208. if ($debug) {error_log('$sql_track_e_exe1: '.$sql_track_e_exe); }
  209. Database::query($sql_track_e_exe);
  210. */
  211. //First we update the attempt to today
  212. // How the expired time is changed into "track_e_exercices" table,then the last attempt for this student should be changed too,so
  213. $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."' ";
  214. if ($debug) {error_log('$sql_track_e_exe2: '.$sql_track_e_exe); }
  215. Database::query($sql_track_e_exe);
  216. //Sessions that contain the expired time
  217. $_SESSION['expired_time'][$current_expired_time_key] = $clock_expired_time;
  218. if ($debug) {error_log('1. Setting the $_SESSION[expired_time]: '.$_SESSION['expired_time'][$current_expired_time_key] ); };
  219. } else {
  220. $expected_time = $current_timestamp + $total_seconds;
  221. if ($debug) error_log('$current_timestamp '.$current_timestamp);
  222. if ($debug) error_log('$expected_time '.$expected_time);
  223. //$expected_time = api_strtotime(api_get_utc_datetime($expected_time));
  224. //$plugin_expired_time = date('M d, Y H:i:s', $expected_time);
  225. //$clock_expired_time = date('Y-m-d H:i:s', $expected_time);
  226. $clock_expired_time = api_get_utc_datetime($expected_time);
  227. if ($debug) error_log('$expected_time '.$clock_expired_time);
  228. //Sessions that contain the expired time
  229. $_SESSION['expired_time'][$current_expired_time_key] = $clock_expired_time;
  230. // $_SESSION['end_expired_time'][$current_expired_time_key] = $plugin_expired_time;
  231. if ($debug) {error_log('2. Setting the $_SESSION[expired_time]: '.$_SESSION['expired_time'][$current_expired_time_key] ); };
  232. //if ($debug) {error_log('2. Setting the $_SESSION[end_expired_time]: '.$_SESSION['end_expired_time'][$current_expired_time_key] ); };
  233. }
  234. } else {
  235. // $plugin_expired_time = $_SESSION['end_expired_time'][$current_expired_time_key];
  236. $clock_expired_time = $_SESSION['expired_time'][$current_expired_time_key];
  237. if ($debug) {error_log('Getting the $_SESSION[end_expired_time]: '.$_SESSION['end_expired_time'][$current_expired_time_key] ); };
  238. }
  239. }
  240. // get time left for exipiring time
  241. //$time_left = api_strtotime($plugin_expired_time) - api_strtotime(api_get_utc_datetime());
  242. // get time left for exipiring time
  243. $time_left = api_strtotime($clock_expired_time,'UTC') - time();
  244. /*
  245. * The time control feature is enable here - this feature is enable for a jquery plugin called epiclock
  246. * for more details of how it works see this link : http://eric.garside.name/docs.html?p=epiclock
  247. */
  248. if ($time_control) { //Sends the exercice form when the expired time is finished
  249. $htmlHeadXtra[] = $objExercise->show_time_control_js($time_left);
  250. }
  251. if ($_configuration['live_exercise_tracking'] && $objExercise->type == ONE_PER_PAGE && $objExercise->feedbacktype != EXERCISE_FEEDBACK_TYPE_DIRECT) {
  252. if (!empty($exercise_stat_info)) {
  253. $exe_id = $exercise_stat_info['exe_id'];
  254. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  255. define('QUESTION_LIST_ALREADY_LOGGED', 1);
  256. $recorded['questionList'] = explode(',', $exercise_stat_info['data_tracking']);
  257. $query = 'SELECT * FROM ' . $exercice_attemp_table . ' WHERE exe_id = ' . $exercise_stat_info['exe_id'] . ' ORDER BY tms ASC';
  258. $result = Database::query($query);
  259. while ($row = Database :: fetch_array($result,'ASSOC')) {
  260. $recorded['exerciseResult'][$row['question_id']] = 1;
  261. }
  262. $exerciseResult = $_SESSION['exerciseResult'] = $recorded['exerciseResult'];
  263. $questionNum = count($recorded['exerciseResult']);
  264. $questionNum++;
  265. $questionList = $_SESSION['questionList'] = $recorded['questionList'];
  266. }
  267. }
  268. }
  269. // if the user has submitted the form
  270. if ($formSent && isset($_POST)) {
  271. if ($debug > 0) { error_log('$formSent was set'); }
  272. // Initializing
  273. if (!is_array($exerciseResult)) {
  274. $exerciseResult = array ();
  275. $exerciseResultCoordinates = array();
  276. }
  277. //Only for hotspot
  278. if (!isset($choice) && isset($_REQUEST['hidden_hotspot_id'])) {
  279. $hotspot_id = (int)($_REQUEST['hidden_hotspot_id']);
  280. $choice = array($hotspot_id => '');
  281. }
  282. // if the user has answered at least one question
  283. if (is_array($choice)) {
  284. if ($debug > 0) {if ($debug > 0) { error_log('$choice is an array'); } }
  285. // Also store hotspot spots in the session ($exerciseResultCoordinates
  286. // will be stored in the session at the end of this script)
  287. if (isset ($_POST['hotspot'])) {
  288. $exerciseResultCoordinates = $_POST['hotspot'];
  289. }
  290. if ($exerciseType == ALL_ON_ONE_PAGE) {
  291. // $exerciseResult receives the content of the form.
  292. // Each choice of the student is stored into the array $choice
  293. $exerciseResult = $choice;
  294. } else {
  295. // gets the question ID from $choice. It is the key of the array
  296. list ($key) = array_keys($choice);
  297. // if the user didn't already answer this question
  298. if (!isset ($exerciseResult[$key])) {
  299. // stores the user answer into the array
  300. $exerciseResult[$key] = $choice[$key];
  301. //saving each question
  302. if ($_configuration['live_exercise_tracking'] && $objExercise->feedbacktype != EXERCISE_FEEDBACK_TYPE_DIRECT) {
  303. $nro_question = $questionNum; // - 1;
  304. //START of saving and qualifying each question submitted
  305. define('ENABLED_LIVE_EXERCISE_TRACKING', 1);
  306. $questionId = $key;
  307. // gets the student choice for this question
  308. $choice = $exerciseResult[$questionId];
  309. if (isset($exe_id)) {
  310. //Manage the question and answer attempts
  311. $objExercise->manage_answer($exe_id, $questionId, $choice,'exercise_show',$exerciseResultCoordinates, true, false,false);
  312. }
  313. //END of saving and qualifying
  314. }
  315. }
  316. }
  317. if ($debug > 0) { error_log('$choice is an array - end'); }
  318. }
  319. // the script "exercise_result.php" will take the variable $exerciseResult from the session
  320. api_session_register('exerciseResult');
  321. api_session_register('exerciseResultCoordinates');
  322. // if all questions on one page OR if it is the last question (only for an exercise with one question per page)
  323. if ($exerciseType == ALL_ON_ONE_PAGE || $questionNum >= $nbrQuestions) {
  324. if ($debug > 0) { error_log('Redirecting to exercise_result.php - Remove debug option to let this happen'); }
  325. if ( api_is_allowed_to_session_edit() ) {
  326. // goes to the script that will show the result of the exercise
  327. if ($exerciseType == ALL_ON_ONE_PAGE) {
  328. header("Location: exercise_result.php?exerciseType=$exerciseType&origin=$origin&learnpath_id=$safe_lp_id&learnpath_item_id=$safe_lp_item_id&learnpath_item_view_id=$safe_lp_item_view_id");
  329. exit;
  330. } else {
  331. if (!empty($exe_id) && is_numeric($exe_id)) {
  332. //Verify if the current test is fraudulent
  333. if (exercise_time_control_is_valid($exerciseId)) {
  334. $sql_exe_result = "";
  335. } else {
  336. $sql_exe_result = ", exe_result = 0";
  337. }
  338. //Clean incomplete - @todo why setting to blank the data_tracking?
  339. //$update_query = 'UPDATE ' . $stat_table . ' SET ' . "status = '', data_tracking='', exe_date = '" . api_get_utc_datetime() . "' $sql_exe_result " . ' WHERE exe_id = ' . Database::escape_string($exe_id);
  340. $update_query = "UPDATE $stat_table SET status = '', exe_date = '".api_get_utc_datetime() ."' , orig_lp_item_view_id = '$safe_lp_item_view_id' $sql_exe_result WHERE exe_id = ".$exe_id;
  341. if ($debug) {error_log($update_query);};
  342. Database::query($update_query);
  343. }
  344. header("Location: exercise_show.php?id=$exe_id&exerciseType=$exerciseType&origin=$origin&learnpath_id=$safe_lp_id&learnpath_item_id=$safe_lp_item_id&learnpath_item_view_id=$safe_lp_item_view_id");
  345. exit;
  346. }
  347. } else {
  348. header("Location: exercice_submit.php?exerciseId=$exerciseId");
  349. exit;
  350. }
  351. }
  352. if ($debug > 0) { error_log('$formSent was set - end'); }
  353. }
  354. $exerciseTitle = $objExercise->selectTitle();
  355. $exerciseDescription= $objExercise->selectDescription();
  356. $exerciseSound = $objExercise->selectSound();
  357. $exerciseType = $objExercise->selectType();
  358. //if (!isset($_SESSION['questionList']) || $origin == 'learnpath') {
  359. //in LP's is enabled the "remember question" feature?
  360. if (!isset($_SESSION['questionList'])) {
  361. // selects the list of question ID
  362. $questionList = ($objExercise->isRandom() ? $objExercise->selectRandomList() : $objExercise->selectQuestionList());
  363. api_session_register('questionList');
  364. if ($debug > 0) { error_log('$_SESSION[questionList] was set'); }
  365. }
  366. if (!isset ($objExercise) && isset ($_SESSION['objExercise'])) {
  367. $questionList = $_SESSION['questionList'];
  368. }
  369. $quizStartTime = time();
  370. api_session_register('quizStartTime');
  371. //Real question count
  372. $nbrQuestions = count($questionList);
  373. // if questionNum comes from POST and not from GET
  374. if (!$questionNum || $_POST['questionNum']) {
  375. // only used for sequential exercises (see $exerciseType)
  376. if (!$questionNum) {
  377. $questionNum = 1;
  378. } else {
  379. $questionNum++;
  380. }
  381. }
  382. if (!empty ($_GET['gradebook']) && $_GET['gradebook'] == 'view') {
  383. $_SESSION['gradebook'] = Security :: remove_XSS($_GET['gradebook']);
  384. $gradebook = $_SESSION['gradebook'];
  385. } elseif (empty ($_GET['gradebook'])) {
  386. unset ($_SESSION['gradebook']);
  387. $gradebook = '';
  388. }
  389. if (!empty ($gradebook) && $gradebook == 'view') {
  390. $interbreadcrumb[] = array ('url' => '../gradebook/' . Security::remove_XSS($_SESSION['gradebook_dest']),'name' => get_lang('ToolGradebook'));
  391. }
  392. $interbreadcrumb[] = array ("url" => "exercice.php?gradebook=$gradebook", "name" => get_lang('Exercices'));
  393. $interbreadcrumb[] = array ("url" => api_get_self()."?gradebook=$gradebook","name" => $objExercise->selectTitle());
  394. if ($origin != 'learnpath') { //so we are not in learnpath tool
  395. //$htmlHeadXtra[] = $objExercise->show_lp_javascript();
  396. Display :: display_header($nameTools,'Exercises');
  397. if (!api_is_allowed_to_session_edit() ) {
  398. Display :: display_warning_message(get_lang('SessionIsReadOnly'));
  399. }
  400. } else {
  401. Display::display_reduced_header();
  402. echo '<div style="height:10px">&nbsp;</div>';
  403. }
  404. $show_quiz_edition = true;
  405. if (isset($exerciseId) && !empty($exerciseId)) {
  406. $TBL_LP_ITEM = Database::get_course_table(TABLE_LP_ITEM);
  407. $sql="SELECT max_score FROM $TBL_LP_ITEM WHERE item_type = '".TOOL_QUIZ."' AND path ='".$exerciseId."'";
  408. $result = Database::query($sql);
  409. if (Database::num_rows($result) > 0) {
  410. $show_quiz_edition = false;
  411. }
  412. }
  413. // I'm in a preview mode
  414. if (api_is_course_admin() && $origin != 'learnpath') {
  415. echo '<div class="actions">';
  416. echo '<a href="exercice.php?show=test&id_session='.api_get_session_id().'">' . Display :: return_icon('back.png', get_lang('BackToExercisesList'),'','32').'</a>';
  417. if ($show_quiz_edition) {
  418. echo '<a href="exercise_admin.php?' . api_get_cidreq() . '&modifyExercise=yes&exerciseId=' . $objExercise->id . '">'.Display :: return_icon('settings.png', get_lang('ModifyExercise'),'','32').'</a>';
  419. //echo Display :: return_icon('wizard.gif', get_lang('QuestionList')) . '<a href="exercice/admin.php?' . api_get_cidreq() . '&exerciseId=' . $objExercise->id . '">' . get_lang('QuestionList') . '</a>';
  420. } else {
  421. echo '<a href="#">'.Display::return_icon('settings_na.png', get_lang('ModifyExercise'),'','32').'</a>';
  422. }
  423. echo '</div>';
  424. }
  425. $exerciseTitle = text_filter($objExercise->selectTitle());
  426. echo Display::tag('h2', $exerciseTitle);
  427. $show_clock = true;
  428. $user_id = api_get_user_id();
  429. if ($objExercise->selectAttempts() > 0) {
  430. $attempt_count = get_attempt_count($user_id, $exerciseId, $safe_lp_id, $safe_lp_item_id, $safe_lp_item_view_id);
  431. if ($attempt_count >= $objExercise->selectAttempts()) {
  432. $show_clock = false;
  433. if (!api_is_allowed_to_edit(null,true)) {
  434. if ($objExercise->results_disabled == 0 && $origin != 'learnpath') {
  435. //Showing latest attempt according with task BT#1628
  436. $exercise_stat_info = get_all_exercise_results_by_user(api_get_user_id(), $exerciseId, api_get_course_id(), api_get_session_id());
  437. if (!empty($exercise_stat_info)) {
  438. $max_exe_id = max(array_keys($exercise_stat_info));
  439. $last_attempt_info = $exercise_stat_info[$max_exe_id];
  440. echo Display::div(get_lang('Date').': '.api_get_local_time($last_attempt_info['exe_date']), array('id'=>'')).'<br />';
  441. if (!empty($last_attempt_info['question_list'])) {
  442. foreach($last_attempt_info['question_list'] as $question_data) {
  443. $question_id = $question_data['question_id'];
  444. $marks = $question_data['marks'];
  445. $question_info = Question::read($question_id);
  446. echo Display::div($question_info->question, array('id'=>'question_title','class'=>'sectiontitle'));
  447. echo Display::div(get_lang('Score').' '.$marks, array('id'=>'question_score'));
  448. }
  449. }
  450. $score = show_score($last_attempt_info['exe_result'], $last_attempt_info['exe_weighting']);
  451. echo Display::div(get_lang('YourTotalScore').' '.$score, array('id'=>'question_score'));
  452. } else {
  453. Display :: display_warning_message(sprintf(get_lang('ReachedMaxAttempts'), $exerciseTitle, $objExercise->selectAttempts()), false);
  454. }
  455. } else {
  456. Display :: display_warning_message(sprintf(get_lang('ReachedMaxAttempts'), $exerciseTitle, $objExercise->selectAttempts()), false);
  457. }
  458. if ($origin != 'learnpath')
  459. Display :: display_footer();
  460. exit;
  461. } else {
  462. Display :: display_warning_message(sprintf(get_lang('ReachedMaxAttemptsAdmin'), $exerciseTitle, $objExercise->selectAttempts()), false);
  463. }
  464. }
  465. }
  466. $limit_time_exists = (($objExercise->start_time != '0000-00-00 00:00:00') || ($objExercise->end_time != '0000-00-00 00:00:00')) ? true : false;
  467. if ($limit_time_exists) {
  468. $exercise_start_time = api_strtotime($objExercise->start_time,'UTC');
  469. $exercise_end_time = api_strtotime($objExercise->end_time,'UTC');
  470. $time_now = time();
  471. $permission_to_start = (($time_now - $exercise_start_time) > 0) ? true : false;
  472. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  473. $exercise_timeover = (($time_now - $exercise_end_time) > 0) ? true : false;
  474. }
  475. if (!$permission_to_start || $exercise_timeover) {
  476. if (!api_is_allowed_to_edit(null,true)) {
  477. $message_warning = $permission_to_start ? get_lang('ReachedTimeLimit') : get_lang('ExerciseNoStartedYet');
  478. Display :: display_warning_message(sprintf($message_warning, $exerciseTitle, $objExercise->selectAttempts()));
  479. if ($origin != 'learnpath') {
  480. Display :: display_footer();
  481. }
  482. exit;
  483. } else {
  484. $message_warning = $permission_to_start ? get_lang('ReachedTimeLimitAdmin') : get_lang('ExerciseNoStartedAdmin');
  485. Display :: display_warning_message(sprintf($message_warning, $exerciseTitle, $objExercise->selectAttempts()));
  486. exit;
  487. }
  488. }
  489. }
  490. //Timer control
  491. if ($time_control) {
  492. echo '<div align="left" id="wrapper-clock"><div id="square" class="rounded"><div id="text-content" align="center" class="count_down"></div></div></div>';
  493. echo '<div style="display:none" class="warning-message" id="expired-message-id">'.get_lang('ExerciceExpiredTimeMessage').'</div>';
  494. }
  495. if ($origin != 'learnpath') {
  496. echo '<div id="highlight-plugin" class="glossary-content">';
  497. }
  498. if (!empty ($error)) {
  499. Display :: display_error_message($error, false);
  500. } else {
  501. if (!empty ($exerciseSound)) {
  502. echo "<a href=\"../document/download.php?doc_url=%2Faudio%2F" . Security::remove_XSS($exerciseSound) . "\" target=\"_blank\">", "<img src=\"../img/sound.gif\" border=\"0\" align=\"absmiddle\" alt=", get_lang('Sound') . "\" /></a>";
  503. }
  504. // Get number of hotspot questions for javascript validation
  505. $number_of_hotspot_questions = 0;
  506. $onsubmit = '';
  507. $i = 0;
  508. //i have a doubt in this line cvargas
  509. //var_dump($questionList);
  510. if (!strcmp($questionList[0], '') === 0) {
  511. foreach ($questionList as $questionId) {
  512. $i++;
  513. $objQuestionTmp = Question :: read($questionId);
  514. // for sequential exercises
  515. if ($exerciseType == ONE_PER_PAGE) {
  516. // if it is not the right question, goes to the next loop iteration
  517. if ($questionNum != $i) {
  518. continue;
  519. } else {
  520. if ($objQuestionTmp->selectType() == HOT_SPOT) {
  521. $number_of_hotspot_questions++;
  522. }
  523. break;
  524. }
  525. } else {
  526. if ($objQuestionTmp->selectType() == HOT_SPOT) {
  527. $number_of_hotspot_questions++;
  528. }
  529. }
  530. }
  531. }
  532. if ($number_of_hotspot_questions > 0) {
  533. $onsubmit = "onsubmit=\"return validateFlashVar('" . $number_of_hotspot_questions . "', '" . get_lang('HotspotValidateError1') . "', '" . get_lang('HotspotValidateError2') . "');\"";
  534. }
  535. echo "<p>$exerciseDescription</p>";
  536. $exercise_condition = '';
  537. if ($exerciseType == ONE_PER_PAGE) {
  538. $exercise_condition = "&exerciseId=" . $exerciseId;
  539. }
  540. echo '<form id="exercise_form" method="post" action="'.api_get_self().'?'.api_get_cidreq().'&autocomplete=off&gradebook='.$gradebook.$exercise_condition .'" name="frm_exercise" '.$onsubmit.'>
  541. <input type="hidden" name="formSent" value="1" />
  542. <input type="hidden" name="exerciseType" value="' . $exerciseType . '" />
  543. <input type="hidden" name="exerciseId" value="' . $exerciseId . '" />
  544. <input type="hidden" name="questionNum" value="' . $questionNum . '" />
  545. <input type="hidden" name="nbrQuestions" value="' . $nbrQuestions . '" />
  546. <input type="hidden" name="origin" value="' . $origin . '" />
  547. <input type="hidden" name="learnpath_id" value="' . $learnpath_id . '" />
  548. <input type="hidden" name="learnpath_item_id" value="'. $learnpath_item_id . '" />
  549. <table id="question_list" width="100%" border="0" cellpadding="1" cellspacing="0">
  550. <tr>
  551. <td>
  552. <table width="100%" cellpadding="3" cellspacing="0" border="0">';
  553. //Show list of questions
  554. $i = 1;
  555. foreach ($questionList as $questionId) {
  556. // for sequential exercises
  557. if ($exerciseType == ONE_PER_PAGE) {
  558. // if it is not the right question, goes to the next loop iteration
  559. if ($questionNum != $i) {
  560. $i++;
  561. continue;
  562. } else {
  563. if ($objExercise->feedbacktype != EXERCISE_FEEDBACK_TYPE_DIRECT) {
  564. // if the user has already answered this question
  565. if (isset ($exerciseResult[$questionId])) {
  566. // construction of the Question object
  567. $objQuestionTmp = Question :: read($questionId);
  568. $questionName = $objQuestionTmp->selectTitle();
  569. // destruction of the Question object
  570. unset ($objQuestionTmp);
  571. Display :: display_normal_message(get_lang('AlreadyAnswered'));
  572. $i++;
  573. break;
  574. }
  575. }
  576. }
  577. }
  578. // shows the question and its answers
  579. showQuestion($questionId, false, $origin, $i);
  580. $i++;
  581. // for sequential exercises
  582. if ($exerciseType == ONE_PER_PAGE) {
  583. // quits the loop
  584. break;
  585. }
  586. }
  587. // end foreach()
  588. echo $objExercise->show_button($nbrQuestions, $questionNum);
  589. echo '</table>
  590. </td>
  591. </tr>
  592. </table></form>';
  593. }
  594. if ($objExercise->type == ONE_PER_PAGE) {
  595. if (empty($exercise_stat_info)) {
  596. $objExercise->save_stat_track_exercise_info($clock_expired_time, $safe_lp_id, $safe_lp_item_id, $safe_lp_item_view_id, $questionList);
  597. }
  598. }
  599. if ($origin != 'learnpath') {
  600. //so we are not in learnpath tool
  601. echo '</div>'; //End glossary div
  602. Display :: display_footer();
  603. } else {
  604. echo '</body></html>';
  605. }