exercise.lib.php 15 KB

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