exercise.lib.php 18 KB

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