exercise_submit.php 33 KB

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