exercise_submit.php 34 KB

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