exercise.lib.php 18 KB

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