exercice_submit.php 53 KB

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