exercice_submit.php 30 KB

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