exercise.lib.php 20 KB

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