exercise.lib.php 19 KB

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