exercice_submit.php 32 KB

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