exercise.lib.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. <?php // $Id: exercise.lib.php 20451 2009-05-10 12:02:22Z ivantcholakov $
  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 20451 2009-05-10 12:02:22Z ivantcholakov $
  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=api_strpos($answer,'[tex]');
  120. $endlocations=api_strpos($answer,'[/tex]');
  121. if($startlocations !== false && $endlocations !== false) {
  122. $texstring=api_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. //getting the matches
  129. $answer=api_ereg_replace('\[[^]]+\]','<input type="text" name="choice['.$questionId.'][]" size="10">',($answer));
  130. // Change input size
  131. /*
  132. preg_match_all('/\[[^]]+]/',$answer,$matches);
  133. $answer=ereg_replace('\[[^]]+\]','<input type="text" name="choice['.$questionId.'][]" size="@@">',($answer));
  134. // 4. resize the input
  135. foreach($matches[0] as $match) {
  136. $answer_len = strlen($match)-2;
  137. //we will only replace 1 item
  138. // echo implode("replace term", explode("search term", "input", $limit));
  139. if ($answer_len <= 5) {
  140. $answer = (implode("5", explode("@@", $answer, 2)));
  141. } elseif($answer_len <= 10) {
  142. $answer = (implode("10", explode("@@", $answer, 2)));
  143. } elseif($answer_len <= 20) {
  144. $answer = (implode("20", explode("@@", $answer, 2)));
  145. } elseif($answer_len <= 30) {
  146. $answer = (implode("30", explode("@@", $answer, 2)));
  147. } elseif($answer_len <= 40) {
  148. $answer = (implode("40", explode("@@", $answer, 2)));
  149. } elseif($answer_len > 40) {
  150. $answer = (implode("55", explode("@@", $answer, 2)));
  151. }
  152. }
  153. */
  154. // 5. replace the {texcode by the api_pare_tex parsed code}
  155. $texstring = api_parse_tex($texstring);
  156. $answer=str_replace("{texcode}",$texstring,$answer);
  157. }
  158. // unique answer
  159. if($answerType == UNIQUE_ANSWER) {
  160. $s.="<input type='hidden' name='choice2[".$questionId."]' value='0'>
  161. <tr>
  162. <td><div style='float:left;margin-top:10px;margin-right:10px;margin-left:5px'>
  163. <input class='checkbox' type='radio' name='choice[".$questionId."]' value='".$answerId."'>
  164. </div>
  165. <div style='float:rigth;margin-top:10px;margin-right:10px;margin-left:30px'>";
  166. $answer=api_parse_tex($answer);
  167. $s.=$answer;
  168. $s.="</div></td></tr>";
  169. } elseif($answerType == MULTIPLE_ANSWER) {
  170. // multiple answers
  171. $s.="<tr >
  172. <td ><div style='float:left;margin-top:10px;margin-right:10px;margin-left:5px'>
  173. <input class='checkbox' type='checkbox' name='choice[".$questionId."][".$answerId."]' value='1'>
  174. <input type='hidden' name='choice2[".$questionId."][0]' value='0'>
  175. </div><div style='float:rigth;margin-top:10px;margin-right:10px;margin-left:30px'>";
  176. $answer = api_parse_tex($answer);
  177. $s.=$answer;
  178. $s.="</div></td></tr>";
  179. }
  180. // fill in blanks
  181. elseif($answerType == FILL_IN_BLANKS)
  182. {
  183. $s.="<tr><td colspan='2'>$answer</td></tr>";
  184. }
  185. // free answer
  186. // matching
  187. else {
  188. if(!$answerCorrect) {
  189. // options (A, B, C, ...) that will be put into the list-box
  190. $Select[$answerId]['Lettre']=$cpt1++;
  191. // answers that will be shown at the right side
  192. $answer = api_parse_tex($answer);
  193. $Select[$answerId]['Reponse']=$answer;
  194. } else {
  195. $s.="
  196. <tr>
  197. <td colspan='2'>
  198. <table border='0' cellpadding='0' cellspacing='0' width='100%'>
  199. <tr>";
  200. $answer=api_parse_tex($answer);
  201. $s.="<td width='40%' valign='top'><b>".$cpt2."</b>.&nbsp;".$answer."</td>
  202. <td width='20%' align='center'>&nbsp;&nbsp;<select name='choice[".$questionId."][".$answerId."]'>
  203. <option value='0'>--</option>";
  204. // fills the list-box
  205. foreach($Select as $key=>$val) {
  206. $s.="<option value='".$key."'>".$val['Lettre']."</option>";
  207. } // end foreach()
  208. $s.="</select>&nbsp;&nbsp;</td>
  209. <td width='40%' valign='top'>";
  210. if(isset($Select[$cpt2]))
  211. $s.='<b>'.$Select[$cpt2]['Lettre'].'.</b> '.$Select[$cpt2]['Reponse'];
  212. else
  213. $s.='&nbsp;';
  214. $s.="
  215. </td>
  216. </tr>
  217. </table>
  218. </td>
  219. </tr>";
  220. $cpt2++;
  221. // if the left side of the "matching" has been completely shown
  222. if($answerId == $nbrAnswers)
  223. {
  224. // if it remains answers to shown at the right side
  225. while(isset($Select[$cpt2])) {
  226. $s.="<tr>
  227. <td colspan='2'>
  228. <table border='0' cellpadding='0' cellspacing='0' width='100%'>
  229. <tr>
  230. <td width='60%' colspan='2'>&nbsp;</td>
  231. <td width='40%' valign='top'>";
  232. $s.='<b>'.$Select[$cpt2]['Lettre'].'.</b> '.$Select[$cpt2]['Reponse'];
  233. $s.="</td>
  234. </tr>
  235. </table>
  236. </td>
  237. </tr>";
  238. $cpt2++;
  239. } // end while()
  240. } // end if()
  241. }
  242. }
  243. } // end for()
  244. if (!ereg("MSIE", $_SERVER["HTTP_USER_AGENT"])) {
  245. $s .= '</table>';
  246. }
  247. $s .= '</div>';
  248. // destruction of the Answer object
  249. unset($objAnswerTmp);
  250. // destruction of the Question object
  251. unset($objQuestionTmp);
  252. if ($origin != 'export')
  253. {
  254. echo $s;
  255. }
  256. else
  257. {
  258. return($s);
  259. }
  260. }
  261. elseif ($answerType == HOT_SPOT) // Question is of type HOT_SPOT
  262. {
  263. $questionName=$objQuestionTmp->selectTitle();
  264. $questionDescription=$objQuestionTmp->selectDescription();
  265. // Get the answers, make a list
  266. $objAnswerTmp=new Answer($questionId);
  267. $nbrAnswers=$objAnswerTmp->selectNbrAnswers();
  268. $answer_list = '<div style="padding: 10px; margin-left: 0px; border: 1px solid #A4A4A4; height: 481px; width: 200px;"><b>'.get_lang('HotspotZones').'</b><ol>';
  269. for($answerId=1;$answerId <= $nbrAnswers;$answerId++)
  270. {
  271. $answer_list .= '<li>'.$objAnswerTmp->selectAnswer($answerId).'</li>';
  272. }
  273. $answer_list .= '</ol></div>';
  274. if(!$onlyAnswers)
  275. {
  276. $s="<div id=\"question_title\" class=\"sectiontitle\">
  277. ".get_lang('Question').' ';
  278. $s.=$current_item;
  279. //@todo I need to the get the feedback type
  280. //if($answerType == 2)
  281. // $s.=' / '.$total_item;
  282. echo $s;
  283. echo ': ';
  284. $s =$questionName.'</div>';
  285. $s.="<table class='exercise_questions'>
  286. <tr>
  287. <td valign='top' colspan='2'>
  288. ";
  289. $questionDescription=api_parse_tex($questionDescription);
  290. $s.=$questionDescription;
  291. $s.="
  292. </td>
  293. </tr>";
  294. }
  295. $canClick = isset($_GET['editQuestion']) ? '0' : (isset($_GET['modifyAnswers']) ? '0' : '1');
  296. //$tes = isset($_GET['modifyAnswers']) ? '0' : '1';
  297. //echo $tes;
  298. $s .= "<script type=\"text/javascript\" src=\"../plugin/hotspot/JavaScriptFlashGateway.js\"></script>
  299. <script src=\"../plugin/hotspot/hotspot.js\" type=\"text/javascript\"></script>
  300. <script language=\"JavaScript\" type=\"text/javascript\">
  301. <!--
  302. // -----------------------------------------------------------------------------
  303. // Globals
  304. // Major version of Flash required
  305. var requiredMajorVersion = 7;
  306. // Minor version of Flash required
  307. var requiredMinorVersion = 0;
  308. // Minor version of Flash required
  309. var requiredRevision = 0;
  310. // the version of javascript supported
  311. var jsVersion = 1.0;
  312. // -----------------------------------------------------------------------------
  313. // -->
  314. </script>
  315. <script language=\"VBScript\" type=\"text/vbscript\">
  316. <!-- // Visual basic helper required to detect Flash Player ActiveX control version information
  317. Function VBGetSwfVer(i)
  318. on error resume next
  319. Dim swControl, swVersion
  320. swVersion = 0
  321. set swControl = CreateObject(\"ShockwaveFlash.ShockwaveFlash.\" + CStr(i))
  322. if (IsObject(swControl)) then
  323. swVersion = swControl.GetVariable(\"\$version\")
  324. end if
  325. VBGetSwfVer = swVersion
  326. End Function
  327. // -->
  328. </script>
  329. <script language=\"JavaScript1.1\" type=\"text/javascript\">
  330. <!-- // Detect Client Browser type
  331. var isIE = (navigator.appVersion.indexOf(\"MSIE\") != -1) ? true : false;
  332. var isWin = (navigator.appVersion.toLowerCase().indexOf(\"win\") != -1) ? true : false;
  333. var isOpera = (navigator.userAgent.indexOf(\"Opera\") != -1) ? true : false;
  334. jsVersion = 1.1;
  335. // JavaScript helper required to detect Flash Player PlugIn version information
  336. function JSGetSwfVer(i){
  337. // NS/Opera version >= 3 check for Flash plugin in plugin array
  338. if (navigator.plugins != null && navigator.plugins.length > 0) {
  339. if (navigator.plugins[\"Shockwave Flash 2.0\"] || navigator.plugins[\"Shockwave Flash\"]) {
  340. var swVer2 = navigator.plugins[\"Shockwave Flash 2.0\"] ? \" 2.0\" : \"\";
  341. var flashDescription = navigator.plugins[\"Shockwave Flash\" + swVer2].description;
  342. descArray = flashDescription.split(\" \");
  343. tempArrayMajor = descArray[2].split(\".\");
  344. versionMajor = tempArrayMajor[0];
  345. versionMinor = tempArrayMajor[1];
  346. if ( descArray[3] != \"\" ) {
  347. tempArrayMinor = descArray[3].split(\"r\");
  348. } else {
  349. tempArrayMinor = descArray[4].split(\"r\");
  350. }
  351. versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
  352. flashVer = versionMajor + \".\" + versionMinor + \".\" + versionRevision;
  353. } else {
  354. flashVer = -1;
  355. }
  356. }
  357. // MSN/WebTV 2.6 supports Flash 4
  358. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv/2.6\") != -1) flashVer = 4;
  359. // WebTV 2.5 supports Flash 3
  360. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv/2.5\") != -1) flashVer = 3;
  361. // older WebTV supports Flash 2
  362. else if (navigator.userAgent.toLowerCase().indexOf(\"webtv\") != -1) flashVer = 2;
  363. // Can't detect in all other cases
  364. else
  365. {
  366. flashVer = -1;
  367. }
  368. return flashVer;
  369. }
  370. // When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
  371. function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
  372. {
  373. reqVer = parseFloat(reqMajorVer + \".\" + reqRevision);
  374. // loop backwards through the versions until we find the newest version
  375. for (i=25;i>0;i--) {
  376. if (isIE && isWin && !isOpera) {
  377. versionStr = VBGetSwfVer(i);
  378. } else {
  379. versionStr = JSGetSwfVer(i);
  380. }
  381. if (versionStr == -1 ) {
  382. return false;
  383. } else if (versionStr != 0) {
  384. if(isIE && isWin && !isOpera) {
  385. tempArray = versionStr.split(\" \");
  386. tempString = tempArray[1];
  387. versionArray = tempString .split(\",\");
  388. } else {
  389. versionArray = versionStr.split(\".\");
  390. }
  391. versionMajor = versionArray[0];
  392. versionMinor = versionArray[1];
  393. versionRevision = versionArray[2];
  394. versionString = versionMajor + \".\" + versionRevision; // 7.0r24 == 7.24
  395. versionNum = parseFloat(versionString);
  396. // is the major.revision >= requested major.revision AND the minor version >= requested minor
  397. if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
  398. return true;
  399. } else {
  400. return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );
  401. }
  402. }
  403. }
  404. }
  405. // -->
  406. </script>";
  407. $s .= '<tr><td valign="top" colspan="2" width="100%"><table><tr><td width="570">'."
  408. <script language=\"JavaScript\" type=\"text/javascript\">
  409. <!--
  410. // Version check based upon the values entered above in \"Globals\"
  411. var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
  412. // Check to see if the version meets the requirements for playback
  413. if (hasReqestedVersion) { // if we've detected an acceptable version
  414. var oeTags = '<object type=\"application/x-shockwave-flash\" data=\"../plugin/hotspot/hotspot_user.swf?modifyAnswers=".$questionId."&amp;canClick:".$canClick."\" width=\"556\" height=\"501\">'
  415. + '<param name=\"movie\" value=\"../plugin/hotspot/hotspot_user.swf?modifyAnswers=".$questionId."&amp;canClick:".$canClick."\" \/>'
  416. + '<\/object>';
  417. document.write(oeTags); // embed the Flash Content SWF when all tests are passed
  418. } else { // flash is too old or we can't detect the plugin
  419. var alternateContent = 'Error<br \/>'
  420. + 'Hotspots requires Macromedia Flash 7.<br \/>'
  421. + '<a href=http://www.macromedia.com/go/getflash/>Get Flash<\/a>';
  422. document.write(alternateContent); // insert non-flash content
  423. }
  424. // -->
  425. </script></td>
  426. <td valign='top' align='left'>$answer_list</td></tr></table>
  427. </td></tr>";
  428. echo $s;
  429. }
  430. echo "<tr><td colspan='2'>&nbsp;</td></tr></table>";
  431. return $nbrAnswers;
  432. }
  433. ?>