exercise.lib.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php // $Id: exercise.lib.php 20200 2009-04-29 22:14:55Z cvargas1 $
  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 library
  22. * shows a question and its answers
  23. * @package dokeos.exercise
  24. * @author Olivier Brouckaert <oli.brouckaert@skynet.be>
  25. * @version $Id: exercise.lib.php 20200 2009-04-29 22:14:55Z cvargas1 $
  26. */
  27. /**
  28. * @param int question id
  29. * @param boolean only answers
  30. * @param boolean origin i.e = learnpath
  31. * @param int current item from the list of questions
  32. * @param int number of total questions
  33. * */
  34. require("../inc/lib/fckeditor/fckeditor.php") ;
  35. function showQuestion($questionId, $onlyAnswers=false, $origin=false,$current_item, $total_item)
  36. {
  37. if (!ereg("MSIE", $_SERVER["HTTP_USER_AGENT"])) {
  38. echo '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.js" type="text/javascript"></script>';
  39. echo '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.corners.min.js" type="text/javascript"></script>';
  40. }
  41. // reads question informations
  42. if(!$objQuestionTmp = Question::read($questionId))
  43. {
  44. // question not found
  45. return false;
  46. }
  47. $answerType=$objQuestionTmp->selectType();
  48. $pictureName=$objQuestionTmp->selectPicture();
  49. if ($answerType != HOT_SPOT) // Question is not of type hotspot
  50. {
  51. if(!$onlyAnswers) {
  52. $questionName=$objQuestionTmp->selectTitle();
  53. $questionDescription=$objQuestionTmp->selectDescription();
  54. $questionName=api_parse_tex($questionName);
  55. $s="<div id=\"question_title\" class=\"sectiontitle\">
  56. ".get_lang('Question').' ';
  57. $s.=$current_item;
  58. //@todo I need the get the feedback type
  59. //if($answerType != 1)
  60. //$s.=' / '.$total_item;
  61. echo $s;
  62. echo ' : ';
  63. echo $questionName.'</div>';
  64. $s='';
  65. $s.="<table class='exercise_questions' style='margin:5px;padding:5px;'>
  66. <tr><td valign='top' colspan='2'>";
  67. $questionDescription=api_parse_tex($questionDescription);
  68. $s.=$questionDescription;
  69. $s.="</td></tr></table>";
  70. if(!empty($pictureName))
  71. {
  72. $s.="
  73. <tr>
  74. <td align='center' colspan='2'><img src='../document/download.php?doc_url=%2Fimages%2F'".$pictureName."' border='0'></td>
  75. </tr>";
  76. }
  77. }
  78. $s.= '</table>';
  79. if (!ereg("MSIE",$_SERVER["HTTP_USER_AGENT"])) {
  80. $s.="<script>$(document).ready( function(){
  81. $('.rounded').corners();
  82. $('.exercise_options').corners();
  83. });</script>";
  84. $s.="<div class=\"rounded exercise_questions\" style=\"width: 720px; padding: 3px; background-color:#ccc;\">";
  85. } else {
  86. $option_ie="margin-left:10px";
  87. }
  88. $s.="<table width=\"720\" class='exercise_options' style=\"width: 720px;$option_ie background-color:#fff;\">";
  89. // construction of the Answer object
  90. $objAnswerTmp=new Answer($questionId);
  91. $nbrAnswers=$objAnswerTmp->selectNbrAnswers();
  92. // only used for the answer type "Matching"
  93. if($answerType == MATCHING)
  94. {
  95. $cpt1='A';
  96. $cpt2=1;
  97. $Select=array();
  98. }
  99. elseif($answerType == FREE_ANSWER)
  100. {
  101. #$comment = $objAnswerTmp->selectComment(1);
  102. //
  103. $oFCKeditor = new FCKeditor("choice[".$questionId."]") ;
  104. $oFCKeditor->ToolbarSet = "FreeAnswer";
  105. $oFCKeditor->Width = '100%';
  106. $oFCKeditor->Height = '200';
  107. $oFCKeditor->Value = '' ;
  108. $s .= "<tr><td colspan='2'>".$oFCKeditor->CreateHtml()."</td></tr>";
  109. //$s.="<tr><td colspan='2'><textarea cols='80' rows='10' name='choice[".$questionId."]'>$answer</textarea></td></tr>";
  110. }
  111. for($answerId=1;$answerId <= $nbrAnswers;$answerId++) {
  112. $answer=$objAnswerTmp->selectAnswer($answerId);
  113. $answerCorrect=$objAnswerTmp->isCorrect($answerId);
  114. if($answerType == FILL_IN_BLANKS) {
  115. // splits text and weightings that are joined with the character '::'
  116. list($answer)=explode('::',$answer);
  117. // because [] is parsed here we follow this procedure:
  118. // 1. find everything between the [tex] and [/tex] tags
  119. $startlocations=strpos($answer,'[tex]');
  120. $endlocations=strpos($answer,'[/tex]');
  121. if($startlocations !== false && $endlocations !== false) {
  122. $texstring=substr($answer,$startlocations,$endlocations-$startlocations+6);
  123. // 2. replace this by {texcode}
  124. $answer=str_replace($texstring,'{texcode}',$answer);
  125. }
  126. // 3. do the normal matching parsing
  127. // replaces [blank] by an input field
  128. $answer=ereg_replace('\[[^]]+\]','<input type="text" name="choice['.$questionId.'][]" size="10">',($answer));
  129. // 4. replace the {texcode by the api_pare_tex parsed code}
  130. $texstring = api_parse_tex($texstring);
  131. $answer=str_replace("{texcode}",$texstring,$answer);
  132. }
  133. // unique answer
  134. if($answerType == UNIQUE_ANSWER) {
  135. $s.="<input type='hidden' name='choice2[".$questionId."]' value='0'>
  136. <tr>
  137. <td><div style='float:left;margin-top:10px;margin-right:10px;margin-left:5px'>
  138. <input class='checkbox' type='radio' name='choice[".$questionId."]' value='".$answerId."'>
  139. </div>
  140. <div style='float:rigth;margin-top:10px;margin-right:10px;margin-left:30px'>";
  141. $answer=api_parse_tex($answer);
  142. $s.=$answer;
  143. $s.="</div></td></tr>";
  144. } elseif($answerType == MULTIPLE_ANSWER) {
  145. // multiple answers
  146. $s.="<tr >
  147. <td ><div style='float:left;margin-top:10px;margin-right:10px;margin-left:5px'>
  148. <input class='checkbox' type='checkbox' name='choice[".$questionId."][".$answerId."]' value='1'>
  149. <input type='hidden' name='choice2[".$questionId."][0]' value='0'>
  150. </div><div style='float:rigth;margin-top:10px;margin-right:10px;margin-left:30px'>";
  151. $answer = api_parse_tex($answer);
  152. $s.=$answer;
  153. $s.="</div></td></tr>";
  154. }
  155. // fill in blanks
  156. elseif($answerType == FILL_IN_BLANKS)
  157. {
  158. $s.="<tr><td colspan='2'>$answer</td></tr>";
  159. }
  160. // free answer
  161. // matching
  162. else {
  163. if(!$answerCorrect) {
  164. // options (A, B, C, ...) that will be put into the list-box
  165. $Select[$answerId]['Lettre']=$cpt1++;
  166. // answers that will be shown at the right side
  167. $answer = api_parse_tex($answer);
  168. $Select[$answerId]['Reponse']=$answer;
  169. } else {
  170. $s.="
  171. <tr>
  172. <td colspan='2'>
  173. <table border='0' cellpadding='0' cellspacing='0' width='100%'>
  174. <tr>";
  175. $answer=api_parse_tex($answer);
  176. $s.="<td width='40%' valign='top'><b>".$cpt2."</b>.&nbsp;".$answer."</td>
  177. <td width='20%' align='center'>&nbsp;&nbsp;<select name='choice[".$questionId."][".$answerId."]'>
  178. <option value='0'>--</option>";
  179. // fills the list-box
  180. foreach($Select as $key=>$val) {
  181. $s.="<option value='".$key."'>".$val['Lettre']."</option>";
  182. } // end foreach()
  183. $s.="</select>&nbsp;&nbsp;</td>
  184. <td width='40%' valign='top'>";
  185. if(isset($Select[$cpt2]))
  186. $s.='<b>'.$Select[$cpt2]['Lettre'].'.</b> '.$Select[$cpt2]['Reponse'];
  187. else
  188. $s.='&nbsp;';
  189. $s.="
  190. </td>
  191. </tr>
  192. </table>
  193. </td>
  194. </tr>";
  195. $cpt2++;
  196. // if the left side of the "matching" has been completely shown
  197. if($answerId == $nbrAnswers)
  198. {
  199. // if it remains answers to shown at the right side
  200. while(isset($Select[$cpt2])) {
  201. $s.="<tr>
  202. <td colspan='2'>
  203. <table border='0' cellpadding='0' cellspacing='0' width='100%'>
  204. <tr>
  205. <td width='60%' colspan='2'>&nbsp;</td>
  206. <td width='40%' valign='top'>";
  207. $s.='<b>'.$Select[$cpt2]['Lettre'].'.</b> '.$Select[$cpt2]['Reponse'];
  208. $s.="</td>
  209. </tr>
  210. </table>
  211. </td>
  212. </tr>";
  213. $cpt2++;
  214. } // end while()
  215. } // end if()
  216. }
  217. }
  218. } // end for()
  219. if (!ereg("MSIE", $_SERVER["HTTP_USER_AGENT"])) {
  220. $s .= '</table>';
  221. }
  222. $s .= '</div>';
  223. // destruction of the Answer object
  224. unset($objAnswerTmp);
  225. // destruction of the Question object
  226. unset($objQuestionTmp);
  227. if ($origin != 'export')
  228. {
  229. echo $s;
  230. }
  231. else
  232. {
  233. return($s);
  234. }
  235. }
  236. elseif ($answerType == HOT_SPOT) // Question is of type HOT_SPOT
  237. {
  238. $questionName=$objQuestionTmp->selectTitle();
  239. $questionDescription=$objQuestionTmp->selectDescription();
  240. // Get the answers, make a list
  241. $objAnswerTmp=new Answer($questionId);
  242. $nbrAnswers=$objAnswerTmp->selectNbrAnswers();
  243. $answer_list = '<div style="padding: 10px; margin-left: 0px; border: 1px solid #A4A4A4; height: 481px; width: 200px;"><b>'.get_lang('HotspotZones').'</b><ol>';
  244. for($answerId=1;$answerId <= $nbrAnswers;$answerId++)
  245. {
  246. $answer_list .= '<li>'.$objAnswerTmp->selectAnswer($answerId).'</li>';
  247. }
  248. $answer_list .= '</ol></div>';
  249. if(!$onlyAnswers)
  250. {
  251. $s="<div id=\"question_title\" class=\"sectiontitle\">
  252. ".get_lang('Question').' ';
  253. $s.=$current_item;
  254. //@todo I need to the get the feedback type
  255. //if($answerType == 2)
  256. // $s.=' / '.$total_item;
  257. echo $s;
  258. echo ': ';
  259. $s =$questionName.'</div>';
  260. $s.="<table class='exercise_questions'>
  261. <tr>
  262. <td valign='top' colspan='2'>
  263. ";
  264. $questionDescription=api_parse_tex($questionDescription);
  265. $s.=$questionDescription;
  266. $s.="
  267. </td>
  268. </tr>";
  269. }
  270. $canClick = isset($_GET['editQuestion']) ? '0' : (isset($_GET['modifyAnswers']) ? '0' : '1');
  271. //$tes = isset($_GET['modifyAnswers']) ? '0' : '1';
  272. //echo $tes;
  273. $s .= "<script type=\"text/javascript\" src=\"../plugin/hotspot/JavaScriptFlashGateway.js\"></script>
  274. <script src=\"../plugin/hotspot/hotspot.js\" type=\"text/javascript\"></script>
  275. <script language=\"JavaScript\" type=\"text/javascript\">
  276. <!--
  277. // -----------------------------------------------------------------------------
  278. // Globals
  279. // Major version of Flash required
  280. var requiredMajorVersion = 7;
  281. // Minor version of Flash required
  282. var requiredMinorVersion = 0;
  283. // Minor version of Flash required
  284. var requiredRevision = 0;
  285. // the version of javascript supported
  286. var jsVersion = 1.0;
  287. // -----------------------------------------------------------------------------
  288. // -->
  289. </script>
  290. <script language=\"VBScript\" type=\"text/vbscript\">
  291. <!-- // Visual basic helper required to detect Flash Player ActiveX control version information
  292. Function VBGetSwfVer(i)
  293. on error resume next
  294. Dim swControl, swVersion
  295. swVersion = 0
  296. set swControl = CreateObject(\"ShockwaveFlash.ShockwaveFlash.\" + CStr(i))
  297. if (IsObject(swControl)) then
  298. swVersion = swControl.GetVariable(\"\$version\")
  299. end if
  300. VBGetSwfVer = swVersion
  301. End Function
  302. // -->
  303. </script>
  304. <script language=\"JavaScript1.1\" type=\"text/javascript\">
  305. <!-- // Detect Client Browser type
  306. var isIE = (navigator.appVersion.indexOf(\"MSIE\") != -1) ? true : false;
  307. var isWin = (navigator.appVersion.toLowerCase().indexOf(\"win\") != -1) ? true : false;
  308. var isOpera = (navigator.userAgent.indexOf(\"Opera\") != -1) ? true : false;
  309. jsVersion = 1.1;
  310. // JavaScript helper required to detect Flash Player PlugIn version information
  311. function JSGetSwfVer(i){
  312. // NS/Opera version >= 3 check for Flash plugin in plugin array
  313. if (navigator.plugins != null && navigator.plugins.length > 0) {
  314. if (navigator.plugins[\"Shockwave Flash 2.0\"] || navigator.plugins[\"Shockwave Flash\"]) {
  315. var swVer2 = navigator.plugins[\"Shockwave Flash 2.0\"] ? \" 2.0\" : \"\";
  316. var flashDescription = navigator.plugins[\"Shockwave Flash\" + swVer2].description;
  317. descArray = flashDescription.split(\" \");
  318. tempArrayMajor = descArray[2].split(\".\");
  319. versionMajor = tempArrayMajor[0];
  320. versionMinor = tempArrayMajor[1];
  321. if ( descArray[3] != \"\" ) {
  322. tempArrayMinor = descArray[3].split(\"r\");
  323. } else {
  324. tempArrayMinor = descArray[4].split(\"r\");
  325. }
  326. versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
  327. flashVer = versionMajor + \".\" + versionMinor + \".\" + versionRevision;
  328. } else {
  329. flashVer = -1;
  330. }
  331. }
  332. // MSN/WebTV 2.6 supports Flash 4
  333. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv/2.6\") != -1) flashVer = 4;
  334. // WebTV 2.5 supports Flash 3
  335. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv/2.5\") != -1) flashVer = 3;
  336. // older WebTV supports Flash 2
  337. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv\") != -1) flashVer = 2;
  338. // Can't detect in all other cases
  339. else
  340. {
  341. flashVer = -1;
  342. }
  343. return flashVer;
  344. }
  345. // When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
  346. function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
  347. {
  348. reqVer = parseFloat(reqMajorVer + \".\" + reqRevision);
  349. // loop backwards through the versions until we find the newest version
  350. for (i=25;i>0;i--) {
  351. if (isIE && isWin && !isOpera) {
  352. versionStr = VBGetSwfVer(i);
  353. } else {
  354. versionStr = JSGetSwfVer(i);
  355. }
  356. if (versionStr == -1 ) {
  357. return false;
  358. } else if (versionStr != 0) {
  359. if(isIE && isWin && !isOpera) {
  360. tempArray = versionStr.split(\" \");
  361. tempString = tempArray[1];
  362. versionArray = tempString .split(\",\");
  363. } else {
  364. versionArray = versionStr.split(\".\");
  365. }
  366. versionMajor = versionArray[0];
  367. versionMinor = versionArray[1];
  368. versionRevision = versionArray[2];
  369. versionString = versionMajor + \".\" + versionRevision; // 7.0r24 == 7.24
  370. versionNum = parseFloat(versionString);
  371. // is the major.revision >= requested major.revision AND the minor version >= requested minor
  372. if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
  373. return true;
  374. } else {
  375. return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );
  376. }
  377. }
  378. }
  379. }
  380. // -->
  381. </script>";
  382. $s .= '<tr><td valign="top" colspan="2" width="100%"><table><tr><td width="570">'."
  383. <script language=\"JavaScript\" type=\"text/javascript\">
  384. <!--
  385. // Version check based upon the values entered above in \"Globals\"
  386. var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
  387. // Check to see if the version meets the requirements for playback
  388. if (hasReqestedVersion) { // if we've detected an acceptable version
  389. var oeTags = '<object type=\"application/x-shockwave-flash\" data=\"../plugin/hotspot/hotspot_user.swf?modifyAnswers=".$questionId."&amp;canClick:".$canClick."\" width=\"556\" height=\"501\">'
  390. + '<param name=\"movie\" value=\"../plugin/hotspot/hotspot_user.swf?modifyAnswers=".$questionId."&amp;canClick:".$canClick."\" \/>'
  391. + '<\/object>';
  392. document.write(oeTags); // embed the Flash Content SWF when all tests are passed
  393. } else { // flash is too old or we can't detect the plugin
  394. var alternateContent = 'Error<br \/>'
  395. + 'Hotspots requires Macromedia Flash 7.<br \/>'
  396. + '<a href=http://www.macromedia.com/go/getflash/>Get Flash<\/a>';
  397. document.write(alternateContent); // insert non-flash content
  398. }
  399. // -->
  400. </script></td>
  401. <td valign='top' align='left'>$answer_list</td></tr></table>
  402. </td></tr>";
  403. echo $s;
  404. }
  405. echo "<tr><td colspan='2'>&nbsp;</td></tr></table>";
  406. return $nbrAnswers;
  407. }
  408. ?>