exercice_submit.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. <?php // $Id: exercice_submit.php 10204 2006-11-26 20:46:53Z pcool $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) Olivier Brouckaert
  9. Copyright (c) Denes Nagy
  10. For a full list of contributors, see "credits.txt".
  11. The full license can be read in "license.txt".
  12. This program is free software; you can redistribute it and/or
  13. modify it under the terms of the GNU General Public License
  14. as published by the Free Software Foundation; either version 2
  15. of the License, or (at your option) any later version.
  16. See the GNU General Public License for more details.
  17. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. ==============================================================================
  22. EXERCISE SUBMISSION
  23. *
  24. * This script allows to run an exercise. According to the exercise type, questions
  25. * can be on an unique page, or one per page with a Next button.
  26. *
  27. * One exercise may contain different types of answers (unique or multiple selection,
  28. * matching and fill in blanks).
  29. *
  30. * Questions are selected randomly or not.
  31. *
  32. * When the user has answered all questions and clicks on the button "Ok",
  33. * it goes to exercise_result.php
  34. *
  35. * Notice : This script is also used to show a question before modifying it by
  36. * the administrator
  37. *
  38. * @author Olivier Brouckaert
  39. * @package dokeos.exercise
  40. ==============================================================================
  41. */
  42. include('exercise.class.php');
  43. include('question.class.php');
  44. include('answer.class.php');
  45. include('exercise.lib.php');
  46. // debug var. Set to 0 to hide all debug display. Set to 1 to display debug messages.
  47. $debug = 0;
  48. // answer types
  49. define('UNIQUE_ANSWER', 1);
  50. define('MULTIPLE_ANSWER', 2);
  51. define('FILL_IN_BLANKS', 3);
  52. define('MATCHING', 4);
  53. define('FREE_ANSWER', 5);
  54. define('HOT_SPOT', 6);
  55. define('HOT_SPOT_ORDER', 7);
  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. include_once(api_get_path(LIBRARY_PATH).'text.lib.php');
  61. $is_allowedToEdit=$is_courseAdmin;
  62. $TBL_EXERCICE_QUESTION = $_course['dbNameGlu'].'quiz_rel_question';
  63. $TBL_EXERCICES = $_course['dbNameGlu'].'quiz';
  64. $TBL_QUESTIONS = $_course['dbNameGlu'].'quiz_question';
  65. $TBL_REPONSES = $_course['dbNameGlu'].'quiz_answer';
  66. // general parameters passed via POST/GET
  67. if ( empty ( $origin ) ) {
  68. $origin = $_REQUEST['origin'];
  69. }
  70. if ( empty ( $learnpath_id ) ) {
  71. $learnpath_id = mysql_real_escape_string($_REQUEST['learnpath_id']);
  72. }
  73. if ( empty ( $learnpath_item_id ) ) {
  74. $learnpath_item_id = mysql_real_escape_string($_REQUEST['learnpath_item_id']);
  75. }
  76. if ( empty ( $formSent ) ) {
  77. $formSent = $_REQUEST['formSent'];
  78. }
  79. if ( empty ( $exerciseResult ) ) {
  80. $exerciseResult = $_REQUEST['exerciseResult'];
  81. }
  82. if ( empty ( $exerciseResultCoordinates ) ) {
  83. $exerciseResultCoordinates = $_REQUEST['exerciseResultCoordinates'];
  84. }
  85. if ( empty ( $exerciseType ) ) {
  86. $exerciseType = $_REQUEST['exerciseType'];
  87. }
  88. if ( empty ( $exerciseId ) ) {
  89. $exerciseId = $_REQUEST['exerciseId'];
  90. }
  91. if ( empty ( $choice ) ) {
  92. $choice = $_REQUEST['choice'];
  93. }
  94. if ( empty ( $questionNum ) ) {
  95. $questionNum = mysql_real_escape_string($_REQUEST['questionNum']);
  96. }
  97. if ( empty ( $nbrQuestions ) ) {
  98. $nbrQuestions = mysql_real_escape_string($_REQUEST['nbrQuestions']);
  99. }
  100. if ( empty ($buttonCancel) ) {
  101. $buttonCancel = $_REQUEST['buttonCancel'];
  102. }
  103. // if the user has clicked on the "Cancel" button
  104. if($buttonCancel)
  105. {
  106. // returns to the exercise list
  107. header("Location: exercice.php?origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id");
  108. exit();
  109. }
  110. if ($origin=='builder') {
  111. /*******************************/
  112. /* Clears the exercise session */
  113. /*******************************/
  114. if(isset($_SESSION['objExercise'])) { api_session_unregister('objExercise'); unset($objExercise); }
  115. if(isset($_SESSION['objQuestion'])) { api_session_unregister('objQuestion'); unset($objQuestion); }
  116. if(isset($_SESSION['objAnswer'])) { api_session_unregister('objAnswer'); unset($objAnswer); }
  117. if(isset($_SESSION['questionList'])) { api_session_unregister('questionList'); unset($questionList); }
  118. if(isset($_SESSION['exerciseResult'])) { api_session_unregister('exerciseResult'); unset($exerciseResult); }
  119. if(isset($_SESSION['exerciseResultCoordinates'])) { api_session_unregister('exerciseResultCoordinates'); unset($exerciseResultCoordinates); }
  120. }
  121. // if the user has submitted the form
  122. if($formSent)
  123. {
  124. if($debug>0){echo str_repeat('&nbsp;',0).'$formSent was set'."<br />\n";}
  125. // initializing
  126. if(!is_array($exerciseResult))
  127. {
  128. $exerciseResult=array();
  129. $exerciseResultCoordinates=array();
  130. }
  131. // if the user has answered at least one question
  132. if(is_array($choice))
  133. {
  134. if($debug>0){echo str_repeat('&nbsp;',0).'$choice is an array'."<br />\n";}
  135. if($exerciseType == 1)
  136. {
  137. // $exerciseResult receives the content of the form.
  138. // Each choice of the student is stored into the array $choice
  139. $exerciseResult=$choice;
  140. if (isset($_POST['hotspot']))
  141. {
  142. $exerciseResultCoordinates = $_POST['hotspot'];
  143. }
  144. }
  145. else
  146. {
  147. // gets the question ID from $choice. It is the key of the array
  148. list($key)=array_keys($choice);
  149. // if the user didn't already answer this question
  150. if(!isset($exerciseResult[$key]))
  151. {
  152. // stores the user answer into the array
  153. $exerciseResult[$key]=$choice[$key];
  154. if (isset($_POST['hotspot']))
  155. {
  156. $exerciseResultCoordinates[$key] = $_POST['hotspot'][$key];
  157. }
  158. }
  159. }
  160. if($debug>0){echo str_repeat('&nbsp;',0).'$choice is an array - end'."<br />\n";}
  161. }
  162. // the script "exercise_result.php" will take the variable $exerciseResult from the session
  163. api_session_register('exerciseResult');
  164. api_session_register('exerciseResultCoordinates');
  165. // if it is the last question (only for a sequential exercise)
  166. if($exerciseType == 1 || $questionNum >= $nbrQuestions)
  167. {
  168. if($debug>0){echo str_repeat('&nbsp;',0).'Redirecting to exercise_result.php - Remove debug option to let this happen'."<br />\n";}
  169. // goes to the script that will show the result of the exercise
  170. header("Location: exercise_result.php?origin=$origin&learnpath_id=$learnpath_id&learnpath_item_id=$learnpath_item_id");
  171. exit();
  172. }
  173. if($debug>0){echo str_repeat('&nbsp;',0).'$formSent was set - end'."<br />\n";}
  174. }
  175. // if the object is not in the session
  176. if(!isset($_SESSION['objExercise']))
  177. {
  178. if($debug>0){echo str_repeat('&nbsp;',0).'$_SESSION[objExercise] was unset'."<br />\n";}
  179. // construction of Exercise
  180. $objExercise=new Exercise();
  181. #$sql="SELECT title,description,sound,type,random,active FROM `$TBL_EXERCICES` WHERE id='$exerciseId'";
  182. // if the specified exercise doesn't exist or is disabled
  183. if(!$objExercise->read($exerciseId) || (!$objExercise->selectStatus() && !$is_allowedToEdit && ($origin != 'learnpath') ))
  184. {
  185. die(get_lang('ExerciseNotFound'));
  186. }
  187. // saves the object into the session
  188. api_session_register('objExercise');
  189. if($debug>0){echo str_repeat('&nbsp;',0).'$_SESSION[objExercise] was unset - set now - end'."<br />\n";}
  190. }
  191. if(!is_object($objExercise))
  192. {
  193. header('Location: exercice.php');
  194. exit();
  195. }
  196. $exerciseTitle=$objExercise->selectTitle();
  197. $exerciseDescription=$objExercise->selectDescription();
  198. $exerciseSound=$objExercise->selectSound();
  199. $randomQuestions=$objExercise->isRandom();
  200. $exerciseType=$objExercise->selectType();
  201. if(!isset($_SESSION['questionList']))
  202. {
  203. if($debug>0){echo str_repeat('&nbsp;',0).'$_SESSION[questionList] was unset'."<br />\n";}
  204. // selects the list of question ID
  205. $questionList = ($randomQuestions?$objExercise->selectRandomList():$objExercise->selectQuestionList());
  206. // saves the question list into the session
  207. api_session_register('questionList');
  208. if($debug>0){echo str_repeat('&nbsp;',0).'$_SESSION[questionList] was unset - set now - end'."<br />\n";}
  209. }
  210. $nbrQuestions=sizeof($questionList);
  211. // if questionNum comes from POST and not from GET
  212. if(!$questionNum || $_POST['questionNum'])
  213. {
  214. // only used for sequential exercises (see $exerciseType)
  215. if(!$questionNum)
  216. {
  217. $questionNum=1;
  218. }
  219. else
  220. {
  221. $questionNum++;
  222. }
  223. }
  224. //$nameTools=get_lang('Exercice');
  225. $interbreadcrumb[]=array("url" => "exercice.php","name" => get_lang('Exercices'));
  226. if ($origin != 'learnpath') { //so we are not in learnpath tool
  227. $htmlHeadXtra[] = "<script type=\"text/javascript\" src=\"../plugin/hotspot/JavaScriptFlashGateway.js\"></script>
  228. <script src=\"../plugin/hotspot/hotspot.js\" type=\"text/javascript\"></script>
  229. <script language=\"JavaScript\" type=\"text/javascript\">
  230. <!--
  231. // -----------------------------------------------------------------------------
  232. // Globals
  233. // Major version of Flash required
  234. var requiredMajorVersion = 7;
  235. // Minor version of Flash required
  236. var requiredMinorVersion = 0;
  237. // Minor version of Flash required
  238. var requiredRevision = 0;
  239. // the version of javascript supported
  240. var jsVersion = 1.0;
  241. // -----------------------------------------------------------------------------
  242. // -->
  243. </script>
  244. <script language=\"VBScript\" type=\"text/vbscript\">
  245. <!-- // Visual basic helper required to detect Flash Player ActiveX control version information
  246. Function VBGetSwfVer(i)
  247. on error resume next
  248. Dim swControl, swVersion
  249. swVersion = 0
  250. set swControl = CreateObject(\"ShockwaveFlash.ShockwaveFlash.\" + CStr(i))
  251. if (IsObject(swControl)) then
  252. swVersion = swControl.GetVariable(\"\$version\")
  253. end if
  254. VBGetSwfVer = swVersion
  255. End Function
  256. // -->
  257. </script>
  258. <script language=\"JavaScript1.1\" type=\"text/javascript\">
  259. <!-- // Detect Client Browser type
  260. var isIE = (navigator.appVersion.indexOf(\"MSIE\") != -1) ? true : false;
  261. var isWin = (navigator.appVersion.toLowerCase().indexOf(\"win\") != -1) ? true : false;
  262. var isOpera = (navigator.userAgent.indexOf(\"Opera\") != -1) ? true : false;
  263. jsVersion = 1.1;
  264. // JavaScript helper required to detect Flash Player PlugIn version information
  265. function JSGetSwfVer(i){
  266. // NS/Opera version >= 3 check for Flash plugin in plugin array
  267. if (navigator.plugins != null && navigator.plugins.length > 0) {
  268. if (navigator.plugins[\"Shockwave Flash 2.0\"] || navigator.plugins[\"Shockwave Flash\"]) {
  269. var swVer2 = navigator.plugins[\"Shockwave Flash 2.0\"] ? \" 2.0\" : \"\";
  270. var flashDescription = navigator.plugins[\"Shockwave Flash\" + swVer2].description;
  271. descArray = flashDescription.split(\" \");
  272. tempArrayMajor = descArray[2].split(\".\");
  273. versionMajor = tempArrayMajor[0];
  274. versionMinor = tempArrayMajor[1];
  275. if ( descArray[3] != \"\" ) {
  276. tempArrayMinor = descArray[3].split(\"r\");
  277. } else {
  278. tempArrayMinor = descArray[4].split(\"r\");
  279. }
  280. versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
  281. flashVer = versionMajor + \".\" + versionMinor + \".\" + versionRevision;
  282. } else {
  283. flashVer = -1;
  284. }
  285. }
  286. // MSN/WebTV 2.6 supports Flash 4
  287. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv/2.6\") != -1) flashVer = 4;
  288. // WebTV 2.5 supports Flash 3
  289. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv/2.5\") != -1) flashVer = 3;
  290. // older WebTV supports Flash 2
  291. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv\") != -1) flashVer = 2;
  292. // Can't detect in all other cases
  293. else {
  294. flashVer = -1;
  295. }
  296. return flashVer;
  297. }
  298. // When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
  299. function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
  300. {
  301. reqVer = parseFloat(reqMajorVer + \".\" + reqRevision);
  302. // loop backwards through the versions until we find the newest version
  303. for (i=25;i>0;i--) {
  304. if (isIE && isWin && !isOpera) {
  305. versionStr = VBGetSwfVer(i);
  306. } else {
  307. versionStr = JSGetSwfVer(i);
  308. }
  309. if (versionStr == -1 ) {
  310. return false;
  311. } else if (versionStr != 0) {
  312. if(isIE && isWin && !isOpera) {
  313. tempArray = versionStr.split(\" \");
  314. tempString = tempArray[1];
  315. versionArray = tempString .split(\",\");
  316. } else {
  317. versionArray = versionStr.split(\".\");
  318. }
  319. versionMajor = versionArray[0];
  320. versionMinor = versionArray[1];
  321. versionRevision = versionArray[2];
  322. versionString = versionMajor + \".\" + versionRevision; // 7.0r24 == 7.24
  323. versionNum = parseFloat(versionString);
  324. // is the major.revision >= requested major.revision AND the minor version >= requested minor
  325. if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
  326. return true;
  327. } else {
  328. return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );
  329. }
  330. }
  331. }
  332. }
  333. // -->
  334. </script>";
  335. Display::display_header($nameTools,"Exercise");
  336. }
  337. else
  338. {
  339. ?>
  340. <link rel="stylesheet" type="text/css" href="<?php echo api_get_path(WEB_CODE_PATH); ?>css/frames.css" />
  341. <?php
  342. }
  343. $exerciseTitle=api_parse_tex($exerciseTitle);
  344. echo "<h3>".$exerciseTitle."</h3>";
  345. if(!empty($exerciseSound))
  346. {
  347. echo "<a href=\"../document/download.php?doc_url=%2Faudio%2F".$exerciseSound."\" target=\"_blank\">",
  348. "<img src=\"../img/sound.gif\" border=\"0\" align=\"absmiddle\" alt=",get_lang('Sound')."\" /></a>";
  349. }
  350. /* <ERM> */
  351. // Get number of hotspot questions for javascript validation
  352. $number_of_hotspot_questions = 0;
  353. $onsubmit = '';
  354. $i=0;
  355. foreach($questionList as $questionId)
  356. {
  357. $i++;
  358. $objQuestionTmp=new Question();
  359. $objQuestionTmp->read($questionId);
  360. // for sequential exercises
  361. if($exerciseType == 2)
  362. {
  363. // if it is not the right question, goes to the next loop iteration
  364. if($questionNum != $i)
  365. {
  366. continue;
  367. }
  368. else
  369. {
  370. if ($objQuestionTmp->selectType() == HOT_SPOT)
  371. {
  372. $number_of_hotspot_questions++;
  373. }
  374. break;
  375. }
  376. }
  377. else
  378. {
  379. if ($objQuestionTmp->selectType() == HOT_SPOT)
  380. {
  381. $number_of_hotspot_questions++;
  382. }
  383. }
  384. }
  385. if($number_of_hotspot_questions > 0)
  386. {
  387. $onsubmit = "onsubmit=\"return validateFlashVar('".$number_of_hotspot_questions."', '".get_lang('HotspotValidateError1')."', '".get_lang('HotspotValidateError2')."')\"";
  388. }
  389. $s="
  390. <p>$exerciseDescription</p>
  391. <form method='post' action='".$_SERVER['PHP_SELF']."?autocomplete=off' name='frm_exercise' $onsubmit>
  392. <input type='hidden' name='formSent' value='1' />
  393. <input type='hidden' name='exerciseType' value='".$exerciseType."' />
  394. <input type='hidden' name='questionNum' value='".$questionNum."' />
  395. <input type='hidden' name='nbrQuestions' value='".$nbrQuestions."' />
  396. <input type='hidden' name='origin' value='".$origin."' />
  397. <input type='hidden' name='learnpath_id' value='".$learnpath_id."' />
  398. <input type='hidden' name='learnpath_item_id' value='".$learnpath_item_id."' />
  399. <table width='100%' border='0' cellpadding='1' cellspacing='0'>
  400. <tr>
  401. <td>
  402. <table width='100%' cellpadding='4' cellspacing='2' border='0'>";
  403. echo $s;
  404. /* </ERM> */
  405. if (isset($_POST['submit']))
  406. {
  407. echo "Form submited";
  408. }
  409. $i=0;
  410. foreach($questionList as $questionId)
  411. {
  412. $i++;
  413. // for sequential exercises
  414. if($exerciseType == 2)
  415. {
  416. // if it is not the right question, goes to the next loop iteration
  417. if($questionNum != $i)
  418. {
  419. continue;
  420. }
  421. else
  422. {
  423. // if the user has already answered this question
  424. if(isset($exerciseResult[$questionId]))
  425. {
  426. // construction of the Question object
  427. $objQuestionTmp=new Question();
  428. // reads question informations
  429. $objQuestionTmp->read($questionId);
  430. $questionName=$objQuestionTmp->selectTitle();
  431. // destruction of the Question object
  432. unset($objQuestionTmp);
  433. echo '<tr><td>'.get_lang('AlreadyAnswered').' &quot;'.$questionName.'&quot;</td></tr>';
  434. break;
  435. }
  436. }
  437. }
  438. $s="<tr bgcolor='#e6e6e6'>
  439. <td valign='top' colspan='2'>
  440. ".get_lang('Question')." ";
  441. $s.=$i;
  442. if($exerciseType == 2) $s.=' / '.$nbrQuestions;
  443. $s.='</td></tr>';
  444. echo $s;
  445. // shows the question and its answers
  446. showQuestion($questionId, false, $origin);
  447. // for sequential exercises
  448. if($exerciseType == 2)
  449. {
  450. // quits the loop
  451. break;
  452. }
  453. } // end foreach()
  454. $s="</table>
  455. </td>
  456. </tr>
  457. <tr>
  458. <td><br/>
  459. <!-- <input type='submit' name='buttonCancel' value=".get_lang('Cancel')." />
  460. &nbsp;&nbsp; //-->
  461. <input type='submit' name='submit' value='";
  462. if ($exerciseType == 1 || $nbrQuestions == $questionNum) {
  463. $s.=get_lang('Ok');
  464. }
  465. else
  466. {
  467. $s.=get_lang('Next').' &gt;';
  468. }
  469. $s.='\'&gt;';
  470. $s.="</td></tr></form></table>";
  471. $b=2;
  472. echo $s;
  473. if ($origin != 'learnpath') { //so we are not in learnpath tool
  474. Display::display_footer();
  475. } else {
  476. ?>
  477. <link rel="stylesheet" type="text/css" href="<?php echo $clarolineRepositoryWeb ?>css/frames.css" />
  478. <?php
  479. }
  480. ?>