exercice_submit.php 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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 dokeos.exercise
  19. * @author Olivier Brouckaert
  20. * @author Julio Montoya multiple fill in blank option added
  21. * @version $Id: exercice_submit.php 22201 2009-07-17 19:57:03Z cfasanando $
  22. */
  23. require_once 'exercise.class.php';
  24. require_once 'question.class.php';
  25. require_once 'answer.class.php';
  26. require_once 'exercise.lib.php';
  27. // debug var. Set to 0 to hide all debug display. Set to 1 to display debug messages.
  28. $debug = 0;
  29. // name of the language file that needs to be included
  30. $language_file = 'exercice';
  31. require_once '../inc/global.inc.php';
  32. $this_section = SECTION_COURSES;
  33. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.js" type="text/javascript" language="javascript"></script>'; //jQuery
  34. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.corners.min.js" type="text/javascript"></script>';
  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. //This library is necessary for the time control feature
  40. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.epiclock.min.js" type="text/javascript" language="javascript"></script>'; //jQuery
  41. if (!ereg("MSIE",$_SERVER["HTTP_USER_AGENT"])) {
  42. $htmlHeadXtra[] = "<script type='text/javascript' language='javascript'>
  43. $(document).ready( function(){
  44. $('.rounded').corners();
  45. $('.exercise_options').corners();
  46. });</script>";
  47. }
  48. /* ------------ ACCESS RIGHTS ------------ */
  49. // notice for unauthorized people.
  50. api_protect_course_script(true);
  51. require_once api_get_path(LIBRARY_PATH) . 'text.lib.php';
  52. $is_allowedToEdit = api_is_allowed_to_edit(null,true);
  53. $_configuration['live_exercise_tracking'] = true;
  54. $stat_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  55. $exercice_attemp_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  56. $TBL_EXERCICE_QUESTION = Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION);
  57. $TBL_EXERCICES = Database :: get_course_table(TABLE_QUIZ_TEST);
  58. $TBL_QUESTIONS = Database :: get_course_table(TABLE_QUIZ_QUESTION);
  59. $TBL_REPONSES = Database :: get_course_table(TABLE_QUIZ_ANSWER);
  60. // general parameters passed via POST/GET
  61. if (empty ($origin)) {
  62. $origin = $_REQUEST['origin'];
  63. }
  64. if (empty ($learnpath_id)) {
  65. $learnpath_id = Security::remove_XSS($_REQUEST['learnpath_id']);
  66. }
  67. if (empty ($learnpath_item_id)) {
  68. $learnpath_item_id = Security::remove_XSS($_REQUEST['learnpath_item_id']);
  69. }
  70. if (empty ($formSent)) {
  71. $formSent = $_REQUEST['formSent'];
  72. }
  73. if (empty ($exerciseResult)) {
  74. $exerciseResult = $_REQUEST['exerciseResult'];
  75. }
  76. if (empty ($exerciseResultCoordinates)) {
  77. $exerciseResultCoordinates = $_REQUEST['exerciseResultCoordinates'];
  78. }
  79. if (empty ($exerciseType)) {
  80. $exerciseType = $_REQUEST['exerciseType'];
  81. }
  82. if (empty ($exerciseId)) {
  83. $exerciseId = Database::escape_string(intval($_REQUEST['exerciseId']));
  84. }
  85. if (empty ($choice)) {
  86. $choice = $_REQUEST['choice'];
  87. }
  88. if (empty ($_REQUEST['choice'])) {
  89. $choice = $_REQUEST['choice2'];
  90. }
  91. if (empty ($questionNum)) {
  92. $questionNum = Database :: escape_string($_REQUEST['questionNum']);
  93. }
  94. if (empty ($nbrQuestions)) {
  95. $nbrQuestions = Database :: escape_string($_REQUEST['nbrQuestions']);
  96. }
  97. if (empty ($buttonCancel)) {
  98. $buttonCancel = $_REQUEST['buttonCancel'];
  99. }
  100. $error = '';
  101. if (!isset ($exerciseType)) {
  102. $exe_start_date = time();
  103. $_SESSION['exercice_start_date'] = $exe_start_date;
  104. }
  105. // if the user has clicked on the "Cancel" button
  106. if ($buttonCancel) {
  107. // returns to the exercise list
  108. header("Location: exercice.php?origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id");
  109. exit ();
  110. }
  111. if ($origin == 'learnpath' && isset ($_GET['not_multiple_attempt']) && $_GET['not_multiple_attempt'] == strval(intval($_GET['not_multiple_attempt']))) {
  112. $not_multiple_attempt = (int) $_GET['not_multiple_attempt'];
  113. if ($not_multiple_attempt === 1) {
  114. require_once '../inc/reduced_header.inc.php';
  115. echo '<div style="height:10px">&nbsp;</div>';
  116. Display :: display_warning_message(get_lang('ReachedOneAttempt'));
  117. exit;
  118. }
  119. }
  120. if ($origin == 'builder') {
  121. /*******************************/
  122. /* Clears the exercise session */
  123. /*******************************/
  124. if (isset ($_SESSION['objExercise'])) {
  125. api_session_unregister('objExercise');
  126. unset ($objExercise);
  127. }
  128. if (isset ($_SESSION['objQuestion'])) {
  129. api_session_unregister('objQuestion');
  130. unset ($objQuestion);
  131. }
  132. if (isset ($_SESSION['objAnswer'])) {
  133. api_session_unregister('objAnswer');
  134. unset ($objAnswer);
  135. }
  136. if (isset ($_SESSION['questionList'])) {
  137. api_session_unregister('questionList');
  138. unset ($questionList);
  139. }
  140. if (isset ($_SESSION['newquestionList'])) {
  141. api_session_unregister('newquestionList');
  142. unset ($newquestionList);
  143. }
  144. if (isset ($_SESSION['exerciseResult'])) {
  145. api_session_unregister('exerciseResult');
  146. unset ($exerciseResult);
  147. }
  148. if (isset ($_SESSION['exerciseResultCoordinates'])) {
  149. api_session_unregister('exerciseResultCoordinates');
  150. unset ($exerciseResultCoordinates);
  151. }
  152. }
  153. $course_code = api_get_course_id();
  154. $session_id = api_get_session_id();
  155. $safe_lp_id = ($learnpath_id == '') ? 0 : (int) $learnpath_id;
  156. $safe_lp_item_id = ($learnpath_item_id == '') ? 0 : (int) $learnpath_item_id;
  157. $condition = ' WHERE ' .
  158. 'exe_exo_id = ' . "'" . $exerciseId . "'" . ' AND ' .
  159. 'exe_user_id = ' . "'" . api_get_user_id() . "'" . ' AND ' .
  160. 'exe_cours_id = ' . "'" . $_course['id'] . "'" . ' AND ' .
  161. 'status = ' . "'incomplete'" . ' AND ' .
  162. 'orig_lp_id = ' . "'" . $safe_lp_id . "'" . ' AND ' .
  163. 'orig_lp_item_id = ' . "'" . $safe_lp_item_id . "'" . ' AND ' .
  164. 'session_id = ' . "'" . (int) $session_id . "'";
  165. $TBL_EXERCICES = Database :: get_course_table(TABLE_QUIZ_TEST);
  166. $sql_track_exercice = "SELECT type,feedback_type,expired_time FROM $TBL_EXERCICES WHERE id=$exerciseId";
  167. $result = Database::query($sql_track_exercice);
  168. $exercise_row = Database :: fetch_array($result);
  169. $exerciseType = $exercise_row['type'];
  170. $exerciseFeedbackType = $exercise_row['feedback_type'];
  171. /*
  172. * Time control feature
  173. * if the expired time is major that zero(0) then
  174. * the expired time is compute on this time
  175. */
  176. //Get the expired time of the current exercice in track_e_exercices
  177. $total_minutes = $exercise_row["expired_time"];
  178. $total_seconds = $total_minutes*60;
  179. $current_timestamp = time();
  180. $current_expired_time_key = $course_code.'_'.$session_id.'_'.$exerciseId;
  181. //Disable for learning path
  182. if ($exercise_row['expired_time'] != 0 && $origin != 'learnpath') {
  183. if (!isset($_SESSION['expired_time'][$current_expired_time_key])) {
  184. //In case that the current session php is broken
  185. //Timer - Get expired_time for a student
  186. $condition = ' WHERE ' .
  187. 'exe_exo_id = '."'".Database::escape_string($exerciseId)."'".' AND ' .
  188. 'exe_user_id = '."'".api_get_user_id()."'".' AND ' .
  189. 'exe_cours_id = '."'".api_get_course_id()."'".' AND ' .
  190. 'status = '."'incomplete'".' AND '.
  191. 'session_id = '."'".$session_id."'";
  192. $sql_track = 'SELECT exe_id,expired_time_control FROM '.$stat_table.$condition;
  193. $rs_sql = Database::query($sql_track);
  194. $exists_into_database = Database::num_rows($rs_sql);
  195. $track_exercice_row = Database::fetch_array($rs_sql);
  196. $expired_time_of_this_attempt = $track_exercice_row['expired_time_control'];
  197. //Get the last attempt of an exercice
  198. $sql_track_attempt = 'SELECT max(tms) as last_attempt_date FROM '.$exercice_attemp_table.' WHERE exe_id="'.$track_exercice_row['exe_id'].'"';
  199. $rs_last_attempt = Database::query($sql_track_attempt);
  200. $row_last_attempt = Database::fetch_array($rs_last_attempt);
  201. $my_last_attempt_date = $row_last_attempt['last_attempt_date'];//Get the date of last attempt
  202. $date_of_last_attempt = $my_last_attempt_date;// Necessary for to change the last attempt
  203. //change the date format
  204. $my_last_attempt_date = strtotime($my_last_attempt_date);
  205. $expired_time_of_this_attempt = strtotime($expired_time_of_this_attempt);
  206. //New expired time - it is due to the possible closure of session
  207. $new_expired_time_in_seconds = $expired_time_of_this_attempt - $my_last_attempt_date;
  208. if ($exists_into_database == 1) {
  209. $expected_time = $current_timestamp + $new_expired_time_in_seconds;
  210. $plugin_expired_time = date('M d, Y H:i:s', $expected_time);
  211. $clock_expired_time = date('Y-m-d H:i:s' , $expected_time);
  212. $new_last_attempt = date('Y-m-d H:i:s' , $current_timestamp);
  213. //We modify the "expired_time_control" field in track_e_exercices for this attempt
  214. $sql_track_e_exe = "UPDATE $stat_table SET expired_time_control = '".$clock_expired_time."' WHERE exe_id = '".$track_exercice_row['exe_id']."'";
  215. Database::query($sql_track_e_exe);
  216. // How the expired time is changed into "track_e_exercices" table,then the last attempt for this student should be changed too,so
  217. $sql_track_e_exe = "UPDATE $exercice_attemp_table SET tms = '".$new_last_attempt."' WHERE exe_id = '".$track_exercice_row['exe_id']."' and tms = '".$date_of_last_attempt."' ";
  218. Database::query($sql_track_e_exe);
  219. //Sessions that contain the expired time
  220. $_SESSION['expired_time'][$current_expired_time_key] = $clock_expired_time;
  221. $_SESSION['end_expired_time'][$current_expired_time_key] = date('M d, Y H:i:s',$expected_time);
  222. } else {
  223. $expected_time = $current_timestamp + $total_seconds;
  224. $plugin_expired_time = date('M d, Y H:i:s',$expected_time);
  225. $clock_expired_time = date('Y-m-d H:i:s',$expected_time);
  226. //Sessions that contain the expired time
  227. $_SESSION['expired_time'][$current_expired_time_key] = $clock_expired_time;
  228. $_SESSION['end_expired_time'][$current_expired_time_key] = date('M d, Y H:i:s',$expected_time);
  229. }
  230. } else {
  231. $plugin_expired_time = $_SESSION['end_expired_time'][$current_expired_time_key];
  232. }
  233. }
  234. /*
  235. * The time control feature is enable here - this feature is enable for a jquery plugin called epiclock
  236. * for more details of how it works see this link : http://eric.garside.name/docs.html?p=epiclock
  237. */
  238. //Disable for learning path
  239. if ($exercise_row['expired_time'] != 0 && $origin != 'learnpath') { //Sends the exercice form when the expired time is finished
  240. $htmlHeadXtra[] = "<script type=\"text/javascript\">
  241. $(document).ready(function(){
  242. function onExpiredTimeExercise() {
  243. $('#wrapper-clock').hide();
  244. $('#expired-message-id').show();
  245. $('form#my_frm_exercise').submit();
  246. }
  247. $('#text-content').epiclock({
  248. mode: EC_COUNTDOWN,
  249. format: 'x{ : } i{ : } s{}',
  250. target: '".$plugin_expired_time."',
  251. onTimer: function(){ onExpiredTimeExercise() }
  252. }).clocks(EC_RUN);
  253. $('.rounded').corners('transparent');
  254. $('#submit_save').click(function () {
  255. });
  256. });
  257. </script>";
  258. }
  259. if ($_configuration['live_exercise_tracking'] == true && $exerciseType == ONE_PER_PAGE && $exerciseFeedbackType != EXERCISE_FEEDBACK_TYPE_DIRECT) {
  260. $query = 'SELECT * FROM ' . $stat_table . $condition;
  261. $result_select = Database::query($query);
  262. if (Database :: num_rows($result_select) > 0) {
  263. $getIncomplete = Database :: fetch_array($result_select);
  264. $exe_id = $getIncomplete['exe_id'];
  265. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  266. define('QUESTION_LIST_ALREADY_LOGGED', 1);
  267. $recorded['questionList'] = explode(',', $getIncomplete['data_tracking']);
  268. $query = 'SELECT * FROM ' . $exercice_attemp_table . ' WHERE exe_id = ' . $getIncomplete['exe_id'] . ' ORDER BY tms ASC';
  269. $result = Database::query($query);
  270. while ($row = Database :: fetch_array($result)) {
  271. $recorded['exerciseResult'][$row['question_id']] = 1;
  272. }
  273. $exerciseResult = $_SESSION['exerciseResult'] = $recorded['exerciseResult'];
  274. $exerciseType = ONE_PER_PAGE;
  275. $questionNum = count($recorded['exerciseResult']);
  276. $questionNum++;
  277. $questionList = $_SESSION['questionList'] = $recorded['questionList'];
  278. }
  279. } else {
  280. $table_recorded_not_exist = true;
  281. }
  282. }
  283. // if the user has submitted the form
  284. if ($formSent) {
  285. if ($debug > 0) {
  286. echo str_repeat('&nbsp;', 0) . '$formSent was set' . "<br />\n";
  287. }
  288. // initializing
  289. if (!is_array($exerciseResult)) {
  290. $exerciseResult = array ();
  291. $exerciseResultCoordinates = array ();
  292. }
  293. //Only for hotspot
  294. if (!isset($choice) && isset($_REQUEST['hidden_hotspot_id'])) {
  295. $hotspot_id = (int)($_REQUEST['hidden_hotspot_id']);
  296. $choice = array($hotspot_id => '');
  297. }
  298. // if the user has answered at least one question
  299. if (is_array($choice)) {
  300. if ($debug > 0) {
  301. echo str_repeat('&nbsp;', 0) . '$choice is an array' . "<br />\n";
  302. }
  303. if ($exerciseType == ALL_ON_ONE_PAGE) {
  304. // $exerciseResult receives the content of the form.
  305. // Each choice of the student is stored into the array $choice
  306. $exerciseResult = $choice;
  307. // Also store hotspot spots in the session ($exerciseResultCoordinates
  308. // will be stored in the session at the end of this script)
  309. // The results will be stored by exercise_result.php if we are in
  310. // an exercise of type 1 (=all on one page)
  311. if (isset ($_POST['hotspot'])) {
  312. $exerciseResultCoordinates = $_POST['hotspot'];
  313. }
  314. } else {
  315. // gets the question ID from $choice. It is the key of the array
  316. list ($key) = array_keys($choice);
  317. // if the user didn't already answer this question
  318. if (!isset ($exerciseResult[$key])) {
  319. // stores the user answer into the array
  320. $exerciseResult[$key] = $choice[$key];
  321. //saving each question
  322. if ($_configuration['live_exercise_tracking'] == true && $exerciseType == ONE_PER_PAGE && $exerciseFeedbackType != EXERCISE_FEEDBACK_TYPE_DIRECT) {
  323. $nro_question = $questionNum; // - 1;
  324. //START of saving and qualifying each question submitted
  325. //------------------------------------------------------------------------------------------
  326. //
  327. define('ENABLED_LIVE_EXERCISE_TRACKING', 1);
  328. require_once 'question.class.php';
  329. require_once 'answer.class.php';
  330. $counter = 0;
  331. $main_course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  332. $table_ans = Database :: get_course_table(TABLE_QUIZ_ANSWER);
  333. //foreach($questionList as $questionId)
  334. if (true) {
  335. $exeId = $exe_id;
  336. $questionId = $key;
  337. $counter++;
  338. // gets the student choice for this question
  339. $choice = $exerciseResult[$questionId];
  340. // creates a temporary Question object
  341. $objQuestionTmp = Question :: read($questionId);
  342. $questionName = $objQuestionTmp->selectTitle();
  343. $questionDescription = $objQuestionTmp->selectDescription();
  344. $questionWeighting = $objQuestionTmp->selectWeighting();
  345. $answerType = $objQuestionTmp->selectType();
  346. $quesId = $objQuestionTmp->selectId(); //added by priya saini
  347. // destruction of the Question object
  348. unset ($objQuestionTmp);
  349. if (isset ($_POST['hotspot']) && isset($_POST['hotspot'][$key])) {
  350. $exerciseResultCoordinates[$key] = $_POST['hotspot'][$key];
  351. }
  352. // construction of the Answer object
  353. $objAnswerTmp = new Answer($questionId);
  354. $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
  355. $questionScore = 0;
  356. if ($answerType == FREE_ANSWER) {
  357. $nbrAnswers = 1;
  358. }
  359. $real_answers = array();
  360. for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
  361. $answer = $objAnswerTmp->selectAnswer($answerId);
  362. $answerComment = $objAnswerTmp->selectComment($answerId);
  363. $answerCorrect = $objAnswerTmp->isCorrect($answerId);
  364. $answerWeighting = $objAnswerTmp->selectWeighting($answerId);
  365. $numAnswer=$objAnswerTmp->selectAutoId($answerId);
  366. switch ($answerType) {
  367. // for unique answer
  368. case UNIQUE_ANSWER :
  369. $studentChoice=($choice == $numAnswer)?1:0;
  370. if ($studentChoice) {
  371. $questionScore+=$answerWeighting;
  372. $totalScore+=$answerWeighting;
  373. }
  374. break;
  375. // for multiple answers
  376. case MULTIPLE_ANSWER :
  377. $studentChoice=$choice[$numAnswer];
  378. if ($studentChoice) {
  379. $questionScore+=$answerWeighting;
  380. $totalScore+=$answerWeighting;
  381. }
  382. break;
  383. case MULTIPLE_ANSWER_COMBINATION:
  384. $studentChoice=$choice[$numAnswer];
  385. if ($answerCorrect == 1) {
  386. if ($studentChoice) {
  387. $real_answers[$answerId] = true;
  388. } else {
  389. $real_answers[$answerId] = false;
  390. }
  391. } else {
  392. if ($studentChoice) {
  393. $real_answers[$answerId] = false;
  394. } else {
  395. $real_answers[$answerId] = true;
  396. }
  397. }
  398. $final_answer = true;
  399. foreach($real_answers as $my_answer) {
  400. if (!$my_answer) {
  401. $final_answer = false;
  402. }
  403. }
  404. break;
  405. // for fill in the blanks
  406. case FILL_IN_BLANKS :
  407. // the question is encoded like this
  408. // [A] B [C] D [E] F::10,10,10@1
  409. // number 1 before the "@" means that is a switchable fill in blank question
  410. // [A] B [C] D [E] F::10,10,10@ or [A] B [C] D [E] F::10,10,10
  411. // means that is a normal fill blank question
  412. // first we explode the "::"
  413. $pre_array = explode('::', $answer);
  414. // is switchable fill blank or not
  415. $last = count($pre_array) - 1;
  416. $is_set_switchable = explode('@', $pre_array[$last]);
  417. $switchable_answer_set = false;
  418. if (isset ($is_set_switchable[1]) && $is_set_switchable[1] == 1) {
  419. $switchable_answer_set = true;
  420. }
  421. $answer = '';
  422. for ($k = 0; $k < $last; $k++) {
  423. $answer .= $pre_array[$k];
  424. }
  425. // splits weightings that are joined with a comma
  426. $answerWeighting = explode(',', $is_set_switchable[0]);
  427. // we save the answer because it will be modified
  428. $temp = $answer;
  429. // TeX parsing
  430. // 1. find everything between the [tex] and [/tex] tags
  431. $startlocations = api_strpos($temp, '[tex]');
  432. $endlocations = api_strpos($temp, '[/tex]');
  433. if ($startlocations !== false && $endlocations !== false) {
  434. $texstring = api_substr($temp, $startlocations, $endlocations - $startlocations +6);
  435. // 2. replace this by {texcode}
  436. $temp = str_replace($texstring, '{texcode}', $temp);
  437. }
  438. $answer = '';
  439. $j = 0;
  440. //initialise answer tags
  441. $user_tags = array ();
  442. $correct_tags = array ();
  443. $real_text = array ();
  444. // the loop will stop at the end of the text
  445. while (1) {
  446. // quits the loop if there are no more blanks (detect '[')
  447. if (($pos = api_strpos($temp, '[')) === false) {
  448. // adds the end of the text
  449. $answer = $temp;
  450. // TeX parsing - replacement of texcode tags
  451. $texstring = api_parse_tex($texstring);
  452. $answer = str_replace("{texcode}", $texstring, $answer);
  453. $real_text[] = $answer;
  454. break; //no more "blanks", quit the loop
  455. }
  456. // adds the piece of text that is before the blank
  457. //and ends with '[' into a general storage array
  458. $real_text[] = api_substr($temp, 0, $pos +1);
  459. $answer .= api_substr($temp, 0, $pos +1);
  460. //take the string remaining (after the last "[" we found)
  461. $temp = api_substr($temp, $pos +1);
  462. // quit the loop if there are no more blanks, and update $pos to the position of next ']'
  463. if (($pos = api_strpos($temp, ']')) === false) {
  464. // adds the end of the text
  465. $answer .= $temp;
  466. break;
  467. }
  468. $choice[$j] = trim($choice[$j]);
  469. $user_tags[] = api_strtolower($choice[$j]);
  470. //put the contents of the [] answer tag into correct_tags[]
  471. $correct_tags[] = api_strtolower(substr($temp, 0, $pos));
  472. $j++;
  473. $temp = api_substr($temp, $pos +1);
  474. //$answer .= ']';
  475. }
  476. $answer = '';
  477. $real_correct_tags = $correct_tags;
  478. $chosen_list = array ();
  479. for ($i = 0; $i < count($real_correct_tags); $i++) {
  480. if ($i == 0) {
  481. $answer .= $real_text[0];
  482. }
  483. if (!$switchable_answer_set) {
  484. //needed to parse ' and " characters
  485. $user_tags[$i] = stripslashes($user_tags[$i]);
  486. if ($correct_tags[$i] == $user_tags[$i]) {
  487. // gives the related weighting to the student
  488. $questionScore += $answerWeighting[$i];
  489. // increments total score
  490. $totalScore += $answerWeighting[$i];
  491. // adds the word in green at the end of the string
  492. $answer .= $correct_tags[$i];
  493. }
  494. // else if the word entered by the student IS NOT the same as the one defined by the professor
  495. elseif (!empty ($user_tags[$i])) {
  496. // adds the word in red at the end of the string, and strikes it
  497. $answer .= '<font color="red"><s>' . $user_tags[$i] . '</s></font>';
  498. } else {
  499. // adds a tabulation if no word has been typed by the student
  500. $answer .= '&nbsp;&nbsp;&nbsp;';
  501. }
  502. } else {
  503. // switchable fill in the blanks
  504. if (in_array($user_tags[$i], $correct_tags)) {
  505. $chosen_list[] = $user_tags[$i];
  506. $correct_tags = array_diff($correct_tags, $chosen_list);
  507. // gives the related weighting to the student
  508. $questionScore += $answerWeighting[$i];
  509. // increments total score
  510. $totalScore += $answerWeighting[$i];
  511. // adds the word in green at the end of the string
  512. $answer .= $user_tags[$i];
  513. }
  514. elseif (!empty ($user_tags[$i])) {
  515. // else if the word entered by the student IS NOT the same as the one defined by the professor
  516. // adds the word in red at the end of the string, and strikes it
  517. $answer .= '<font color="red"><s>' . $user_tags[$i] . '</s></font>';
  518. } else {
  519. // adds a tabulation if no word has been typed by the student
  520. $answer .= '&nbsp;&nbsp;&nbsp;';
  521. }
  522. }
  523. // adds the correct word, followed by ] to close the blank
  524. $answer .= ' / <font color="green"><b>' . $real_correct_tags[$i] . '</b></font>]';
  525. if (isset ($real_text[$i +1])) {
  526. $answer .= $real_text[$i +1];
  527. }
  528. }
  529. break;
  530. // for free answer
  531. case FREE_ANSWER :
  532. $studentChoice = $choice;
  533. if ($studentChoice) {
  534. //Score is at -1 because the question has'nt been corected
  535. $questionScore = -1;
  536. $totalScore += 0;
  537. }
  538. break;
  539. // for matching
  540. case MATCHING :
  541. $numAnswer=$objAnswerTmp->selectAutoId($answerId);
  542. if ($answerCorrect) {
  543. if ($answerCorrect == $choice[$numAnswer]) {
  544. $questionScore+=$answerWeighting;
  545. $totalScore+=$answerWeighting;
  546. }
  547. $matching[$numAnswer] = $choice[$numAnswer];
  548. }
  549. break;
  550. // for hotspot with no order
  551. case HOT_SPOT :
  552. $studentChoice = $choice[$answerId];
  553. if ($studentChoice) {
  554. $questionScore += $answerWeighting;
  555. $totalScore += $answerWeighting;
  556. }
  557. break;
  558. // for hotspot with fixed order
  559. case HOT_SPOT_ORDER :
  560. $studentChoice = $choice['order'][$answerId];
  561. if ($studentChoice == $answerId) {
  562. $questionScore += $answerWeighting;
  563. $totalScore += $answerWeighting;
  564. $studentChoice = true;
  565. } else {
  566. $studentChoice = false;
  567. }
  568. break;
  569. } // end switch Answertype
  570. } // end for that loops over all answers of the current question
  571. // destruction of Answer
  572. //we add the total score after dealing with the answers
  573. if ($answerType == MULTIPLE_ANSWER_COMBINATION) {
  574. if ($final_answer) {
  575. //getting only the first score where we save the weight of all the question
  576. $answerWeighting=$objAnswerTmp->selectWeighting(1);
  577. $questionScore+=$answerWeighting;
  578. $totalScore+=$answerWeighting;
  579. }
  580. }
  581. unset ($objAnswerTmp);
  582. $i++;
  583. $totalWeighting += $questionWeighting;
  584. //added by priya saini
  585. // Store results directly in the database
  586. // For all in one page exercises, the results will be
  587. // stored by exercise_results.php (using the session)
  588. if ($_configuration['tracking_enabled']) {
  589. if (empty ($choice)) {
  590. $choice = 0;
  591. }
  592. if ($answerType == MULTIPLE_ANSWER) {
  593. if ($choice != 0) {
  594. $reply = array_keys($choice);
  595. for ($i = 0; $i < sizeof($reply); $i++) {
  596. $ans = $reply[$i];
  597. exercise_attempt($questionScore, $ans, $quesId, $exeId, $i);
  598. }
  599. } else {
  600. exercise_attempt($questionScore, 0, $quesId, $exeId, 0);
  601. }
  602. } elseif ($answerType == MULTIPLE_ANSWER_COMBINATION) {
  603. if ($choice != 0) {
  604. $reply = array_keys($choice);
  605. for ($i = 0; $i < sizeof($reply); $i++) {
  606. $ans = $reply[$i];
  607. exercise_attempt($questionScore, $ans, $quesId, $exeId, $i);
  608. }
  609. } else {
  610. exercise_attempt($questionScore, 0, $quesId, $exeId, 0);
  611. }
  612. } elseif ($answerType == MATCHING) {
  613. foreach ($matching as $j => $val) {
  614. exercise_attempt($questionScore, $val, $quesId, $exeId, $j);
  615. }
  616. } elseif ($answerType == FREE_ANSWER) {
  617. $answer = $choice;
  618. exercise_attempt($questionScore, $answer, $quesId, $exeId, 0);
  619. } elseif ($answerType == UNIQUE_ANSWER) {
  620. $answer = $choice;
  621. exercise_attempt($questionScore, $answer, $quesId, $exeId, 0);
  622. } elseif ($answerType == HOT_SPOT) {
  623. exercise_attempt($questionScore, $answer, $quesId, $exeId, 0);
  624. if (is_array($exerciseResultCoordinates[$key])) {
  625. foreach($exerciseResultCoordinates[$key] as $idx => $val) {
  626. exercise_attempt_hotspot($exeId,$quesId,$idx,$choice[$idx],$val);
  627. }
  628. }
  629. } else {
  630. exercise_attempt($questionScore, $answer, $quesId, $exeId, 0);
  631. }
  632. }
  633. }
  634. // end huge foreach() block that loops over all questions
  635. //at loops over all questions
  636. if (isset($exe_id)) {
  637. $sql_update = 'UPDATE ' . $stat_table . ' SET exe_result = exe_result + ' . (int) $totalScore . ',exe_weighting = exe_weighting + ' . (int) $totalWeighting . ' WHERE exe_id = ' . Database::escape_string($exe_id);
  638. Database::query($sql_update);
  639. }
  640. //END of saving and qualifying
  641. //------------------------------------------------------------------------------------------
  642. //
  643. }
  644. }
  645. }
  646. if ($debug > 0) {
  647. echo str_repeat('&nbsp;', 0) . '$choice is an array - end' . "<br />\n";
  648. }
  649. }
  650. // the script "exercise_result.php" will take the variable $exerciseResult from the session
  651. api_session_register('exerciseResult');
  652. api_session_register('exerciseResultCoordinates');
  653. // if all questions on one page OR if it is the last question (only for an exercise with one question per page)
  654. if ($exerciseType == ALL_ON_ONE_PAGE || $questionNum >= $nbrQuestions) {
  655. if ($debug > 0) {
  656. echo str_repeat('&nbsp;', 0) . 'Redirecting to exercise_result.php - Remove debug option to let this happen' . "<br />\n";
  657. }
  658. if ( api_is_allowed_to_session_edit() ) {
  659. // goes to the script that will show the result of the exercise
  660. if ($exerciseType == ALL_ON_ONE_PAGE) {
  661. header("Location: exercise_result.php?exerciseType=$exerciseType&origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id");
  662. } else {
  663. if ($exe_id != '') {
  664. //Verify if the current test is fraudulent
  665. $current_time = time();
  666. if (isset($_SESSION['expired_time'][$current_expired_time_key]) && $exercise_row['expired_time'][$exerciseId] != 0) {
  667. $expired_date = $_SESSION['expired_time'][$current_expired_time_key];
  668. $expired_time = strtotime($expired_date);
  669. //Validation in case of fraud
  670. $total_time_allowed = $expired_time + 30;
  671. if ($total_time_allowed < $current_time) {
  672. $sql_exe_result = ",exe_result = 0";
  673. }
  674. } else {
  675. $sql_exe_result = "";
  676. }
  677. //clean incomplete
  678. $update_query = 'UPDATE ' . $stat_table . ' SET ' . "status = '', data_tracking='', exe_date = '" . date('Y-m-d H:i:s') . "' $sql_exe_result " . ' WHERE exe_id = ' . Database::escape_string($exe_id);
  679. Database::query($update_query);
  680. }
  681. header("Location: exercise_show.php?id=$exe_id&exerciseType=$exerciseType&origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id");
  682. }
  683. exit ();
  684. } else {
  685. header("Location: exercice_submit.php?exerciseId=$exerciseId");
  686. exit;
  687. }
  688. }
  689. if ($debug > 0) {
  690. echo str_repeat('&nbsp;', 0) . '$formSent was set - end' . "<br />\n";
  691. }
  692. }
  693. // if the object is not in the session
  694. //why destroying the exercise when a LP is loaded ?
  695. //if (!isset($_SESSION['objExercise']) || $origin == 'learnpath' || $_SESSION['objExercise']->id != $_REQUEST['exerciseId']) {
  696. if (!isset ($_SESSION['objExercise']) || $_SESSION['objExercise']->id != $_REQUEST['exerciseId']) {
  697. if ($debug > 0) {
  698. echo str_repeat('&nbsp;', 0) . '$_SESSION[objExercise] was unset' . "<br />\n";
  699. }
  700. // construction of Exercise
  701. $objExercise = new Exercise();
  702. unset ($_SESSION['questionList']);
  703. // if the specified exercise doesn't exist or is disabled
  704. if (!$objExercise->read($exerciseId) || (!$objExercise->selectStatus() && !$is_allowedToEdit && ($origin != 'learnpath'))) {
  705. unset ($objExercise);
  706. $error = get_lang('ExerciseNotFound');
  707. //die(get_lang('ExerciseNotFound'));
  708. } else {
  709. // saves the object into the session
  710. api_session_register('objExercise');
  711. if ($debug > 0) {
  712. echo str_repeat('&nbsp;', 0) . '$_SESSION[objExercise] was unset - set now - end' . "<br />\n";
  713. }
  714. }
  715. }
  716. if (!isset ($objExcercise) && isset ($_SESSION['objExercise'])) {
  717. $objExercise = $_SESSION['objExercise'];
  718. }
  719. if (!is_object($objExercise)) {
  720. header('Location: exercice.php');
  721. exit ();
  722. }
  723. $Exe_starttime = $objExercise->start_time;
  724. $Exe_endtime = $objExercise->end_time;
  725. $quizID = $objExercise->selectId();
  726. $exerciseAttempts = $objExercise->selectAttempts();
  727. $exerciseTitle = $objExercise->selectTitle();
  728. $exerciseDescription = $objExercise->selectDescription();
  729. $exerciseDescription = $exerciseDescription;
  730. $exerciseSound = $objExercise->selectSound();
  731. $randomQuestions = $objExercise->isRandom();
  732. $exerciseType = $objExercise->selectType();
  733. $table_quiz_test = Database :: get_course_table(TABLE_QUIZ_TEST);
  734. //if (!isset($_SESSION['questionList']) || $origin == 'learnpath') {
  735. //in LP's is enabled the "remember question" feature?
  736. $my_exe_id = Security :: remove_XSS($_GET['exerciseId']);
  737. if (!isset ($_SESSION['questionList'])) {
  738. if ($debug > 0) {
  739. echo str_repeat('&nbsp;', 0) . '$_SESSION[questionList] was unset' . "<br />\n";
  740. }
  741. // selects the list of question ID
  742. $my_question_list = array ();
  743. $questionList = ($randomQuestions ? $objExercise->selectRandomList() : $objExercise->selectQuestionList());
  744. // saves the question list into the session
  745. $sql = 'SELECT random FROM ' . $table_quiz_test . ' WHERE id="' . Database :: escape_string($my_exe_id) . '";';
  746. $rs = Database::query($sql);
  747. $row_number = Database :: fetch_array($rs);
  748. $z = 0;
  749. if ($row_number['random'] <> 0) {
  750. foreach ($questionList as $infoquestionList) {
  751. if ($z < $row_number['random']) {
  752. $my_question_list[$z] = $infoquestionList;
  753. } else {
  754. break;
  755. }
  756. $z++;
  757. }
  758. // $questionList=array();
  759. $questionList = $my_question_list;
  760. }
  761. api_session_register('questionList');
  762. if ($debug > 0) {
  763. echo str_repeat('&nbsp;', 0) . '$_SESSION[questionList] was unset - set now - end' . "<br />\n";
  764. }
  765. }
  766. if (!isset ($objExcercise) && isset ($_SESSION['objExercise'])) {
  767. $questionList = $_SESSION['questionList'];
  768. }
  769. $quizStartTime = time();
  770. api_session_register('quizStartTime');
  771. $nbrQuestions = sizeof($questionList);
  772. // if questionNum comes from POST and not from GET
  773. if (!$questionNum || $_POST['questionNum']) {
  774. // only used for sequential exercises (see $exerciseType)
  775. if (!$questionNum) {
  776. $questionNum = 1;
  777. } else {
  778. $questionNum++;
  779. }
  780. }
  781. if (!empty ($_GET['gradebook']) && $_GET['gradebook'] == 'view') {
  782. $_SESSION['gradebook'] = Security :: remove_XSS($_GET['gradebook']);
  783. $gradebook = $_SESSION['gradebook'];
  784. }
  785. elseif (empty ($_GET['gradebook'])) {
  786. unset ($_SESSION['gradebook']);
  787. $gradebook = '';
  788. }
  789. if (!empty ($gradebook) && $gradebook == 'view') {
  790. $interbreadcrumb[] = array (
  791. 'url' => '../gradebook/' . $_SESSION['gradebook_dest'],
  792. 'name' => get_lang('ToolGradebook')
  793. );
  794. }
  795. //$nameTools=get_lang('Exercice');
  796. $interbreadcrumb[] = array (
  797. "url" => "exercice.php?gradebook=$gradebook",
  798. "name" => get_lang('Exercices')
  799. );
  800. $interbreadcrumb[] = array (
  801. "url" => api_get_self()."?gradebook=$gradebook",
  802. "name" => $exerciseTitle
  803. );
  804. if ($origin != 'learnpath') { //so we are not in learnpath tool
  805. $htmlHeadXtra[] = "<script type=\"text/javascript\" src=\"../plugin/hotspot/JavaScriptFlashGateway.js\"></script>
  806. <script src=\"../plugin/hotspot/hotspot.js\" type=\"text/javascript\"></script>
  807. <script language=\"JavaScript\" type=\"text/javascript\">
  808. <!--
  809. // -----------------------------------------------------------------------------
  810. // Globals
  811. // Major version of Flash required
  812. var requiredMajorVersion = 7;
  813. // Minor version of Flash required
  814. var requiredMinorVersion = 0;
  815. // Minor version of Flash required
  816. var requiredRevision = 0;
  817. // the version of javascript supported
  818. var jsVersion = 1.0;
  819. // -----------------------------------------------------------------------------
  820. // -->
  821. </script>
  822. <script language=\"VBScript\" type=\"text/vbscript\">
  823. <!-- // Visual basic helper required to detect Flash Player ActiveX control version information
  824. Function VBGetSwfVer(i)
  825. on error resume next
  826. Dim swControl, swVersion
  827. swVersion = 0
  828. set swControl = CreateObject(\"ShockwaveFlash.ShockwaveFlash.\" + CStr(i))
  829. if (IsObject(swControl)) then
  830. swVersion = swControl.GetVariable(\"\$version\")
  831. end if
  832. VBGetSwfVer = swVersion
  833. End Function
  834. // -->
  835. </script>
  836. <script language=\"JavaScript1.1\" type=\"text/javascript\">
  837. <!-- // Detect Client Browser type
  838. var isIE = (navigator.appVersion.indexOf(\"MSIE\") != -1) ? true : false;
  839. var isWin = (navigator.appVersion.toLowerCase().indexOf(\"win\") != -1) ? true : false;
  840. var isOpera = (navigator.userAgent.indexOf(\"Opera\") != -1) ? true : false;
  841. jsVersion = 1.1;
  842. // JavaScript helper required to detect Flash Player PlugIn version information
  843. function JSGetSwfVer(i){
  844. // NS/Opera version >= 3 check for Flash plugin in plugin array
  845. if (navigator.plugins != null && navigator.plugins.length > 0) {
  846. if (navigator.plugins[\"Shockwave Flash 2.0\"] || navigator.plugins[\"Shockwave Flash\"]) {
  847. var swVer2 = navigator.plugins[\"Shockwave Flash 2.0\"] ? \" 2.0\" : \"\";
  848. var flashDescription = navigator.plugins[\"Shockwave Flash\" + swVer2].description;
  849. descArray = flashDescription.split(\" \");
  850. tempArrayMajor = descArray[2].split(\".\");
  851. versionMajor = tempArrayMajor[0];
  852. versionMinor = tempArrayMajor[1];
  853. if ( descArray[3] != \"\" ) {
  854. tempArrayMinor = descArray[3].split(\"r\");
  855. } else {
  856. tempArrayMinor = descArray[4].split(\"r\");
  857. }
  858. versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
  859. flashVer = versionMajor + \".\" + versionMinor + \".\" + versionRevision;
  860. } else {
  861. flashVer = -1;
  862. }
  863. }
  864. // MSN/WebTV 2.6 supports Flash 4
  865. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv/2.6\") != -1) flashVer = 4;
  866. // WebTV 2.5 supports Flash 3
  867. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv/2.5\") != -1) flashVer = 3;
  868. // older WebTV supports Flash 2
  869. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv\") != -1) flashVer = 2;
  870. // Can't detect in all other cases
  871. else {
  872. flashVer = -1;
  873. }
  874. return flashVer;
  875. }
  876. // When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
  877. function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
  878. {
  879. reqVer = parseFloat(reqMajorVer + \".\" + reqRevision);
  880. // loop backwards through the versions until we find the newest version
  881. for (i=25;i>0;i--) {
  882. if (isIE && isWin && !isOpera) {
  883. versionStr = VBGetSwfVer(i);
  884. } else {
  885. versionStr = JSGetSwfVer(i);
  886. }
  887. if (versionStr == -1 ) {
  888. return false;
  889. } else if (versionStr != 0) {
  890. if(isIE && isWin && !isOpera) {
  891. tempArray = versionStr.split(\" \");
  892. tempString = tempArray[1];
  893. versionArray = tempString .split(\",\");
  894. } else {
  895. versionArray = versionStr.split(\".\");
  896. }
  897. versionMajor = versionArray[0];
  898. versionMinor = versionArray[1];
  899. versionRevision = versionArray[2];
  900. versionString = versionMajor + \".\" + versionRevision; // 7.0r24 == 7.24
  901. versionNum = parseFloat(versionString);
  902. // is the major.revision >= requested major.revision AND the minor version >= requested minor
  903. if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
  904. return true;
  905. } else {
  906. return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );
  907. }
  908. }
  909. }
  910. }
  911. // -->
  912. </script>";
  913. Display :: display_header($nameTools, "Exercise");
  914. if (!api_is_allowed_to_session_edit() ) {
  915. Display :: display_warning_message(get_lang('SessionIsReadOnly'));
  916. }
  917. } else {
  918. /*
  919. * HTML HEADER
  920. */
  921. Display::display_reduced_header();
  922. echo '<div style="height:10px">&nbsp;</div>';
  923. }
  924. $show_quiz_edition = true;
  925. if (isset($exerciseId) && !empty($exerciseId)) {
  926. $TBL_LP_ITEM = Database::get_course_table(TABLE_LP_ITEM);
  927. $sql="SELECT max_score FROM $TBL_LP_ITEM
  928. WHERE item_type = '".TOOL_QUIZ."' AND path ='".Database::escape_string($exerciseId)."'";
  929. $result = Database::query($sql);
  930. if (Database::num_rows($result) > 0) {
  931. $show_quiz_edition = false;
  932. }
  933. }
  934. // I'm in a preview mode
  935. if (api_is_course_admin() && $origin != 'learnpath') {
  936. echo '<div class="actions">';
  937. echo '<a href="exercice.php?show=test">' . Display :: return_icon('message_reply_forum.png', get_lang('GoBackToQuestionList')) . get_lang('GoBackToQuestionList') . '</a>';
  938. if ($show_quiz_edition) {
  939. echo Display :: return_icon('edit.gif', get_lang('ModifyExercise')) . '<a href="exercise_admin.php?' . api_get_cidreq() . '&modifyExercise=yes&exerciseId=' . $objExercise->id . '">' . get_lang('ModifyExercise') . '</a>';
  940. } else {
  941. echo Display::return_icon('edit_na.gif', get_lang('ModifyExercise')).'<a href="#">'.get_lang('ModifyExercise').'</a>';
  942. }
  943. echo '</div>';
  944. }
  945. //Timer control
  946. if ($exercise_row['expired_time'] != 0 && $origin != 'learnpath') {
  947. echo '<div align="left" id="wrapper-clock"><div id="square" class="rounded"><div id="text-content" align="center" class="count_down"></div></div></div>';
  948. echo '<div style="display:none" class="warning-message" id="expired-message-id">'.get_lang('ExerciceExpiredTimeMessage').'</div>';
  949. }
  950. $exerciseTitle = api_parse_tex($exerciseTitle);
  951. echo "<h3>" . $exerciseTitle . "</h3>";
  952. if ($exerciseAttempts > 0) {
  953. $user_id = api_get_user_id();
  954. $sql = "SELECT count(*) FROM $stat_table WHERE exe_exo_id = '$quizID'
  955. AND exe_user_id = '$user_id'
  956. AND status != 'incomplete'
  957. AND orig_lp_id = $safe_lp_id
  958. AND orig_lp_item_id = $safe_lp_item_id
  959. AND exe_cours_id = '$course_code' AND session_id = '" . (int) $session_id . "'";
  960. $aquery = Database::query($sql);
  961. $attempt = Database :: fetch_array($aquery);
  962. if ($attempt[0] >= $exerciseAttempts) {
  963. if (!api_is_allowed_to_edit(null,true)) {
  964. Display :: display_warning_message(sprintf(get_lang('ReachedMaxAttempts'), $exerciseTitle, $exerciseAttempts), false);
  965. if ($origin != 'learnpath')
  966. Display :: display_footer();
  967. exit;
  968. } else {
  969. Display :: display_warning_message(sprintf(get_lang('ReachedMaxAttemptsAdmin'), $exerciseTitle, $exerciseAttempts), false);
  970. }
  971. }
  972. }
  973. if (!function_exists('convert_date_to_number')) {
  974. function convert_date_to_number($default) {
  975. // 2008-10-12 00:00:00 ---to--> 12345672218 (timestamp)
  976. $parts = split(' ', $default);
  977. list ($d_year, $d_month, $d_day) = split('-', $parts[0]);
  978. list ($d_hour, $d_minute, $d_second) = split(':', $parts[1]);
  979. return mktime($d_hour, $d_minute, $d_second, $d_month, $d_day, $d_year);
  980. }
  981. }
  982. $limit_time_exists = (($Exe_starttime != '0000-00-00 00:00:00') || ($Exe_endtime != '0000-00-00 00:00:00')) ? true : false;
  983. if ($limit_time_exists) {
  984. $exercise_start_time = convert_date_to_number($Exe_starttime);
  985. $exercise_end_time = convert_date_to_number($Exe_endtime);
  986. $time_now = convert_date_to_number(date('Y-m-d H:i:s'));
  987. $permission_to_start = (($time_now - $exercise_start_time) > 0) ? true : false;
  988. if ($_SERVER['REQUEST_METHOD'] != 'POST')
  989. $exercise_timeover = (($time_now - $exercise_end_time) > 0) ? true : false;
  990. if ($permission_to_start == false || $exercise_timeover == true) { //
  991. if (!api_is_allowed_to_edit(null,true)) {
  992. $message_warning = ($permission_to_start == false) ? get_lang('ExerciseNoStartedYet') : get_lang('ReachedTimeLimit');
  993. Display :: display_warning_message(sprintf($message_warning, $exerciseTitle, $exerciseAttempts));
  994. Display :: display_footer();
  995. exit;
  996. } else {
  997. $message_warning = ($permission_to_start == false) ? get_lang('ExerciseNoStartedAdmin') : get_lang('ReachedTimeLimitAdmin');
  998. Display :: display_warning_message(sprintf($message_warning, $exerciseTitle, $exerciseAttempts));
  999. }
  1000. }
  1001. }
  1002. if ($origin != 'learnpath') {
  1003. echo '<div id="highlight-plugin" class="glossary-content">';
  1004. }
  1005. if (!empty ($error)) {
  1006. Display :: display_error_message($error, false);
  1007. } else {
  1008. if (!empty ($exerciseSound)) {
  1009. 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') .
  1010. "\" /></a>";
  1011. }
  1012. // Get number of hotspot questions for javascript validation
  1013. $number_of_hotspot_questions = 0;
  1014. $onsubmit = '';
  1015. $i = 0;
  1016. //i have a doubt in this line cvargas
  1017. //var_dump($questionList);
  1018. if (!strcmp($questionList[0], '') === 0) {
  1019. foreach ($questionList as $questionId) {
  1020. $i++;
  1021. $objQuestionTmp = Question :: read($questionId);
  1022. // for sequential exercises
  1023. if ($exerciseType == ONE_PER_PAGE) {
  1024. // if it is not the right question, goes to the next loop iteration
  1025. if ($questionNum != $i) {
  1026. continue;
  1027. } else {
  1028. if ($objQuestionTmp->selectType() == HOT_SPOT) {
  1029. $number_of_hotspot_questions++;
  1030. }
  1031. break;
  1032. }
  1033. } else {
  1034. if ($objQuestionTmp->selectType() == HOT_SPOT) {
  1035. $number_of_hotspot_questions++;
  1036. }
  1037. }
  1038. }
  1039. }
  1040. if ($number_of_hotspot_questions > 0) {
  1041. $onsubmit = "onsubmit=\"return validateFlashVar('" . $number_of_hotspot_questions . "', '" . get_lang('HotspotValidateError1') . "', '" . get_lang('HotspotValidateError2') . "');\"";
  1042. }
  1043. $s = "<p>$exerciseDescription</p>";
  1044. if ($exerciseType == ONE_PER_PAGE)
  1045. {
  1046. $s2 = "&exerciseId=" . $exerciseId;
  1047. }
  1048. $s .= " <form method='post' action='" . api_get_self() . "?".api_get_cidreq()."&autocomplete=off&gradebook=$gradebook" . $s2 . "' id='my_frm_exercise' name='frm_exercise' $onsubmit>
  1049. <input type='hidden' name='formSent' value='1' />
  1050. <input type='hidden' name='exerciseType' value='" . $exerciseType . "' />
  1051. <input type='hidden' name='exerciseId' value='" . $exerciseId . "' />
  1052. <input type='hidden' name='questionNum' value='" . $questionNum . "' />
  1053. <input type='hidden' name='nbrQuestions' value='" . $nbrQuestions . "' />
  1054. <input type='hidden' name='origin' value='" . $origin . "' />
  1055. <input type='hidden' name='learnpath_id' value='" . $learnpath_id . "' />
  1056. <input type='hidden' name='learnpath_item_id' value='" . $learnpath_item_id . "' />
  1057. <table width='100%' border='0' cellpadding='1' cellspacing='0'>
  1058. <tr>
  1059. <td>
  1060. <table width='100%' cellpadding='3' cellspacing='0' border='0'>";
  1061. echo $s;
  1062. $i = 1;
  1063. foreach ($questionList as $questionId) {
  1064. // for sequential exercises
  1065. if ($exerciseType == ONE_PER_PAGE) {
  1066. // if it is not the right question, goes to the next loop iteration
  1067. if ($questionNum != $i) {
  1068. $i++;
  1069. continue;
  1070. } else {
  1071. if ($exerciseFeedbackType != EXERCISE_FEEDBACK_TYPE_DIRECT) {
  1072. // if the user has already answered this question
  1073. if (isset ($exerciseResult[$questionId])) {
  1074. // construction of the Question object
  1075. $objQuestionTmp = Question :: read($questionId);
  1076. $questionName = $objQuestionTmp->selectTitle();
  1077. // destruction of the Question object
  1078. unset ($objQuestionTmp);
  1079. Display :: display_normal_message(get_lang('AlreadyAnswered'));
  1080. $i++;
  1081. //echo '<tr><td>'.get_lang('AlreadyAnswered').' &quot;'.$questionName.'&quot;</td></tr>';
  1082. break;
  1083. }
  1084. }
  1085. }
  1086. }
  1087. // shows the question and its answers
  1088. showQuestion($questionId, false, $origin, $i, $nbrQuestions);
  1089. $i++;
  1090. // for sequential exercises
  1091. if ($exerciseType == ONE_PER_PAGE) {
  1092. // quits the loop
  1093. break;
  1094. }
  1095. }
  1096. // end foreach()
  1097. //echo "<!-- <button type='submit' name='buttonCancel' class='cancel'>" . get_lang('Cancel') . "</button>&nbsp;&nbsp; //--><br />";
  1098. echo '<div style="margin-top:-10px;">';
  1099. $submit_btn = "<button class='next' type='submit' name='submit' name='submit_save' id='submit_save'>";
  1100. // $submit_btn.=get_lang('ValidateAnswer');
  1101. if ($objExercise->selectFeedbackType() == 1 && $_SESSION['objExercise']->selectType() == 2) {
  1102. $submit_btn = '';
  1103. echo '<script src="' . api_get_path(WEB_LIBRARY_PATH) . 'javascript/thickbox.js" type="text/javascript"></script>';
  1104. echo '<style type="text/css" media="all">@import "' . api_get_path(WEB_LIBRARY_PATH) . 'javascript/thickbox.css";</style>';
  1105. $hotspot_get = $_POST['hotspot'];
  1106. // Show a hidden div
  1107. echo "<script>$(document).ready( function(){
  1108. $('.rounded').corners();
  1109. $('.rounded_inner').corners();
  1110. });</script>";
  1111. //echo '<br /><span class="rounded" style="width: 300px; margin-top: 10px; padding: 3px; background-color:#ccc;"><span style="width: 300px" class="rounded_inner" ><a href="exercise_submit_modal.php?hotspot='.$hotspot_get.'&nbrQuestions='.$nbrQuestions.'&questionnum='.$questionNum.'&exerciseType='.$exerciseType.'&exerciseId='.$exerciseId.'&placeValuesBeforeTB_=savedValues&TB_iframe=true&height=480&width=640&modal=true" title="" class="thickbox" id="validationButton">'.get_lang('ValidateAnswer').'</a></span></span><br /><br /><br />';
  1112. } else {
  1113. if (api_is_allowed_to_session_edit() ) {
  1114. if ($exerciseType == ALL_ON_ONE_PAGE || $nbrQuestions == $questionNum) {
  1115. $submit_btn .= get_lang('ValidateAnswer');
  1116. $name_btn = get_lang('ValidateAnswer');
  1117. } else {
  1118. $submit_btn .= get_lang('NextQuestion');
  1119. $name_btn = get_lang('NextQuestion');
  1120. }
  1121. $submit_btn .= "</button>";
  1122. if ($exercise_row['expired_time'] != 0) {
  1123. echo $submit_btn ="<input class='submit_next' type='submit' id='submit_save' value='".$name_btn."' name='submit_save'/>";
  1124. } else {
  1125. echo $submit_btn;
  1126. }
  1127. }
  1128. }
  1129. echo '</div>'; //margin top -10
  1130. echo '</table>
  1131. </td>
  1132. </tr>
  1133. </table></form>';
  1134. $b = 2;
  1135. }
  1136. if ($_configuration['live_exercise_tracking'] == true && $exerciseFeedbackType != EXERCISE_FEEDBACK_TYPE_DIRECT) {
  1137. if ($table_recorded_not_exist) { //$table_recorded_not_exist
  1138. if ($exercise_row['expired_time'] != 0) {
  1139. $sql_fields = "expired_time_control, ";
  1140. $sql_fields_values = "'"."$clock_expired_time"."',";
  1141. } else {
  1142. $sql_fields = "";
  1143. $sql_fields_values = "";
  1144. }
  1145. if ($exerciseType == ONE_PER_PAGE) {
  1146. $sql = "INSERT INTO $stat_table($sql_fields exe_exo_id,exe_user_id,exe_cours_id,status,session_id,data_tracking,start_date,orig_lp_id,orig_lp_item_id)
  1147. VALUES($sql_fields_values '$exerciseId','" . api_get_user_id() . "','" . $_course['id'] . "','incomplete','" . $session_id . "','" . implode(',', $questionList) . "','" . date('Y-m-d H:i:s') . "',$safe_lp_id,$safe_lp_item_id)";
  1148. Database::query($sql);
  1149. } else {
  1150. echo $sql = "INSERT INTO $stat_table ($sql_fields exe_exo_id,exe_user_id,exe_cours_id,status,session_id,start_date,orig_lp_id,orig_lp_item_id)
  1151. VALUES($sql_fields_values '$exerciseId','" . api_get_user_id() . "','" . $_course['id'] . "','incomplete','" . $session_id . "','" . date('Y-m-d H:i:s') . "',$safe_lp_id,$safe_lp_item_id)";
  1152. Database::query($sql);
  1153. }
  1154. }
  1155. }
  1156. if ($origin != 'learnpath') {
  1157. //so we are not in learnpath tool
  1158. echo '</div>'; //End glossary div
  1159. Display :: display_footer();
  1160. } else {
  1161. echo '</body></html>';
  1162. }