exercise.lib.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php // $Id: exercise.lib.php 9665 2006-10-24 10:43:48Z elixir_inter $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) Olivier Brouckaert
  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: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  17. ==============================================================================
  18. */
  19. /**
  20. ==============================================================================
  21. EXERCISE TOOL LIBRARY
  22. *
  23. * shows a question and its answers
  24. *
  25. * @returns 'number of answers' if question exists, otherwise false
  26. *
  27. * @author Olivier Brouckaert <oli.brouckaert@skynet.be>
  28. *
  29. * @param integer $questionId ID of the question to show
  30. * @param boolean $onlyAnswers set to true to show only answers
  31. * @package dokeos.exercise
  32. ==============================================================================
  33. */
  34. require("../inc/lib/fckeditor/fckeditor.php") ;
  35. function showQuestion($questionId, $onlyAnswers=false, $origin=false)
  36. {
  37. // construction of the Question object
  38. $objQuestionTmp=new Question();
  39. // reads question informations
  40. if(!$objQuestionTmp->read($questionId))
  41. {
  42. // question not found
  43. return false;
  44. }
  45. $answerType=$objQuestionTmp->selectType();
  46. $pictureName=$objQuestionTmp->selectPicture();
  47. if ($answerType != HOT_SPOT) // Question is not of type hotspot
  48. {
  49. if(!$onlyAnswers)
  50. {
  51. $questionName=$objQuestionTmp->selectTitle();
  52. $questionDescription=$objQuestionTmp->selectDescription();
  53. $s="<tr>
  54. <td valign='top' colspan='2'>";
  55. $questionName=api_parse_tex($questionName);
  56. $s.=$questionName;
  57. $s.="</td>
  58. </tr>
  59. <tr>
  60. <td valign='top' colspan='2'>
  61. <i>";
  62. $questionDescription=api_parse_tex($questionDescription);
  63. $s.=$questionDescription;
  64. $s.="</i>
  65. </td>
  66. </tr>";
  67. if(!empty($pictureName))
  68. {
  69. $s.="
  70. <tr>
  71. <td align='center' colspan='2'><img src='../document/download.php?doc_url=%2Fimages%2F'".$pictureName."' border='0'></td>
  72. </tr>";
  73. }
  74. } // end if(!$onlyAnswers)
  75. // construction of the Answer object
  76. $objAnswerTmp=new Answer($questionId);
  77. $nbrAnswers=$objAnswerTmp->selectNbrAnswers();
  78. // only used for the answer type "Matching"
  79. if($answerType == MATCHING)
  80. {
  81. $cpt1='A';
  82. $cpt2=1;
  83. $Select=array();
  84. }
  85. elseif($answerType == FREE_ANSWER)
  86. {
  87. #$comment = $objAnswerTmp->selectComment(1);
  88. //
  89. $upload_path = api_get_path(REL_COURSE_PATH).$_SESSION['_course']['path'].'/document/';
  90. $oFCKeditor = new FCKeditor("choice[".$questionId."]") ;
  91. $oFCKeditor->BasePath = api_get_path(WEB_LIBRARY_PATH)."fckeditor/";
  92. //$oFCKeditor->Config['CustomConfigurationsPath'] = api_get_path(WEB_PATH)."claroline/inc/lib/fckeditor_new/myconfig.js?".time(); //to clear cache we use time() but always clear history manually
  93. /*
  94. $oFCKeditor->Config['ImageBrowserURL'] = $oFCKeditor->BasePath . "editor/filemanager/browser/default/browser.html?Type=Image&Connector=connectors/php/connector.php&ServerPath=/$upload_path/";
  95. $oFCKeditor->Config['ImageUploadURL'] = $oFCKeditor->BasePath . "editor/filemanager/upload/php/upload.php?Type=Image&ServerPath=/$upload_path/" ;
  96. //for Link/File
  97. $oFCKeditor->Config['LinkBrowserURL'] = $oFCKeditor->BasePath . "editor/filemanager/browser/default/browser.html?Connector=connectors/php/connector.php&ServerPath=$upload_path";
  98. $oFCKeditor->Config['LinkUploadURL'] = $oFCKeditor->BasePath . "editor/filemanager/upload/php/upload.php?ServerPath=$upload_path" ;
  99. //for image
  100. $oFCKeditor->Config['ImageBrowserURL'] = $oFCKeditor->BasePath . "editor/filemanager/browser/default/browser.html?Type=Image&Connector=connectors/php/connector.php&ServerPath=$upload_path";
  101. $oFCKeditor->Config['ImageUploadURL'] = $oFCKeditor->BasePath . "editor/filemanager/upload/php/upload.php?Type=Image&ServerPath=$upload_path" ;
  102. //for flash
  103. $oFCKeditor->Config['FlashBrowserURL'] = $oFCKeditor->BasePath . "editor/filemanager/browser/default/browser.html?Type=Flash&Connector=connectors/php/connector.php&ServerPath=$upload_path";
  104. $oFCKeditor->Config['FlashUploadURL'] = $oFCKeditor->BasePath . "editor/filemanager/upload/php/upload.php?Type=Flash&ServerPath=$upload_path" ;
  105. //for MP3
  106. $oFCKeditor->Config['MP3BrowserURL'] = $oFCKeditor->BasePath . "editor/filemanager/browser/default/browser.html?Type=MP3&Connector=connectors/php/connector.php&ServerPath=$upload_path";
  107. $oFCKeditor->Config['MP3UploadURL'] = $oFCKeditor->BasePath . "editor/filemanager/upload/php/upload.php?Type=MP3&ServerPath=$upload_path" ;
  108. //for other media
  109. $oFCKeditor->Config['VideoBrowserURL'] = $oFCKeditor->BasePath . "editor/filemanager/browser/default/browser.html?Type=Video&Connector=connectors/php/connector.php&ServerPath=$upload_path";
  110. $oFCKeditor->Config['VideoUploadURL'] = $oFCKeditor->BasePath . "editor/filemanager/upload/php/upload.php?Type=Video&ServerPath=$upload_path" ;
  111. $oFCKeditor->ToolbarSet = 'Comment' ;*/
  112. $oFCKeditor->Width = '70%';
  113. $oFCKeditor->Height = '150';
  114. $oFCKeditor->Value = '' ;
  115. $s .= "<tr><td colspan='2'>".$oFCKeditor->CreateHtml()."</td></tr>";
  116. //$s.="<tr><td colspan='2'><textarea cols='80' rows='10' name='choice[".$questionId."]'>$answer</textarea></td></tr>";
  117. }
  118. for($answerId=1;$answerId <= $nbrAnswers;$answerId++)
  119. {
  120. $answer=$objAnswerTmp->selectAnswer($answerId);
  121. $answerCorrect=$objAnswerTmp->isCorrect($answerId);
  122. if($answerType == FILL_IN_BLANKS)
  123. {
  124. // splits text and weightings that are joined with the character '::'
  125. list($answer)=explode('::',$answer);
  126. // because [] is parsed here we follow this procedure:
  127. // 1. find everything between the [tex] and [/tex] tags
  128. $startlocations=strpos($answer,'[tex]');
  129. $endlocations=strpos($answer,'[/tex]');
  130. if($startlocations !== false && $endlocations !== false)
  131. {
  132. $texstring=substr($answer,$startlocations,$endlocations-$startlocations+6);
  133. // 2. replace this by {texcode}
  134. $answer=str_replace($texstring,'{texcode}',$answer);
  135. }
  136. // 3. do the normal matching parsing
  137. // replaces [blank] by an input field
  138. $answer=ereg_replace('\[[^]]+\]','<input type="text" name="choice['.$questionId.'][]" size="10">',nl2br($answer));
  139. // 4. replace the {texcode by the api_pare_tex parsed code}
  140. $texstring = api_parse_tex($texstring);
  141. $answer=str_replace("{texcode}",$texstring,$answer);
  142. }
  143. // unique answer
  144. if($answerType == UNIQUE_ANSWER)
  145. {
  146. $s.="
  147. <tr>
  148. <td width='5%' align='center'>
  149. <input class='checkbox' type='radio' name='choice[".$questionId."]' value='".$answerId."'>
  150. </td>
  151. <td width='95%'>";
  152. $answer=api_parse_tex($answer);
  153. $s.=$answer;
  154. $s.="</td></tr>";
  155. }
  156. // multiple answers
  157. elseif($answerType == MULTIPLE_ANSWER)
  158. {
  159. $s.="<tr>
  160. <td width='5%' align='center'>
  161. <input class='checkbox' type='checkbox' name='choice[".$questionId."][".$answerId."]' value='1'>
  162. </td>
  163. <td width='95%'>";
  164. $answer = api_parse_tex($answer);
  165. $s.=$answer;
  166. $s.="</td></tr>";
  167. }
  168. // fill in blanks
  169. elseif($answerType == FILL_IN_BLANKS)
  170. {
  171. $s.="<tr><td colspan='2'>$answer</td></tr>";
  172. }
  173. // free answer
  174. // matching
  175. else
  176. {
  177. if(!$answerCorrect)
  178. {
  179. // options (A, B, C, ...) that will be put into the list-box
  180. $Select[$answerId]['Lettre']=$cpt1++;
  181. // answers that will be shown at the right side
  182. $answer = api_parse_tex($answer);
  183. $Select[$answerId]['Reponse']=$answer;
  184. }
  185. else
  186. {
  187. $s.="
  188. <tr>
  189. <td colspan='2'>
  190. <table border='0' cellpadding='0' cellspacing='0' width='100%'>
  191. <tr>";
  192. $answer=api_parse_tex($answer);
  193. $s.="<td width='40%' valign='top'><b>".$cpt2."</b>.&nbsp".$answer."</td>
  194. <td width='20%' align='center'>&nbsp;&nbsp;<select name='choice[".$questionId."][".$answerId."]'>
  195. <option value='0'>--</option>";
  196. // fills the list-box
  197. foreach($Select as $key=>$val)
  198. {
  199. $s.="<option value='".$key."'>".$val['Lettre']."</option>";
  200. } // end foreach()
  201. $s.="</select>&nbsp;&nbsp;</td>
  202. <td width='40%' valign='top'>";
  203. if(isset($Select[$cpt2])) $s.='<b>'.$Select[$cpt2]['Lettre'].'.</b> '.$Select[$cpt2]['Reponse'];
  204. else $s.='&nbsp;';
  205. $s.="
  206. </td>
  207. </tr>
  208. </table>
  209. </td>
  210. </tr>";
  211. $cpt2++;
  212. // if the left side of the "matching" has been completely shown
  213. if($answerId == $nbrAnswers)
  214. {
  215. // if it remains answers to shown at the right side
  216. while(isset($Select[$cpt2]))
  217. {
  218. $s.="<tr>
  219. <td colspan='2'>
  220. <table border='0' cellpadding='0' cellspacing='0' width='100%'>
  221. <tr>
  222. <td width='60%' colspan='2'>&nbsp;</td>
  223. <td width='40%' valign='top'>";
  224. $s.='<b>'.$Select[$cpt2]['Lettre'].'.</b> '.$Select[$cpt2]['Reponse'];
  225. $s.="</td>
  226. </tr>
  227. </table>
  228. </td>
  229. </tr>";
  230. $cpt2++;
  231. } // end while()
  232. } // end if()
  233. }
  234. }
  235. } // end for()
  236. // destruction of the Answer object
  237. unset($objAnswerTmp);
  238. // destruction of the Question object
  239. unset($objQuestionTmp);
  240. if ($origin != 'export')
  241. {
  242. echo $s;
  243. }
  244. else
  245. {
  246. return($s);
  247. }
  248. }
  249. elseif ($answerType == HOT_SPOT) // Question is of type HOT_SPOT
  250. {
  251. $questionName=$objQuestionTmp->selectTitle();
  252. $questionDescription=$objQuestionTmp->selectDescription();
  253. // Get the answers, make a list
  254. $objAnswerTmp=new Answer($questionId);
  255. $nbrAnswers=$objAnswerTmp->selectNbrAnswers();
  256. $answer_list = '<div style="padding: 10px; margin-left: -8px; border: 1px solid #4271b5; height: 448px; width: 200px;"><b>'.get_lang('langHotspotZones').'</b><ol>';
  257. for($answerId=1;$answerId <= $nbrAnswers;$answerId++)
  258. {
  259. $answer_list .= '<li>'.$objAnswerTmp->selectAnswer($answerId).'</li>';
  260. }
  261. $answer_list .= '</ol></div>';
  262. if(!$onlyAnswers)
  263. {
  264. $s="<tr>
  265. <td valign='top' colspan='2'>";
  266. $questionName=api_parse_tex($questionName);
  267. $s.=$questionName;
  268. $s.="</td>
  269. </tr>
  270. <tr>
  271. <td valign='top' colspan='2'>
  272. <i>";
  273. $questionDescription=api_parse_tex($questionDescription);
  274. $s.=$questionDescription;
  275. $s.="</i>
  276. </td>
  277. </tr>";
  278. }
  279. $canClick = isset($_GET['editQuestion']) ? '0' : (isset($_GET['modifyAnswers']) ? '0' : '1');
  280. //$tes = isset($_GET['modifyAnswers']) ? '0' : '1';
  281. //echo $tes;
  282. $s .= '<tr><td valign="top" colspan="2"><table><tr><td>'."
  283. <script language=\"JavaScript\" type=\"text/javascript\">
  284. <!--
  285. // Version check based upon the values entered above in \"Globals\"
  286. var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
  287. // Check to see if the version meets the requirements for playback
  288. if (hasReqestedVersion) { // if we've detected an acceptable version
  289. var oeTags = '<object type=\"application/x-shockwave-flash\" data=\"../plugin/hotspot/hotspot_user.swf?modifyAnswers=".$questionId."&amp;canClick:".$canClick."\" width=\"380\" height=\"470\">'
  290. + '<param name=\"movie\" value=\"../plugin/hotspot/hotspot_user.swf?modifyAnswers=".$questionId."&amp;canClick:".$canClick."\" \/>'
  291. + '<\/object>';
  292. document.write(oeTags); // embed the Flash Content SWF when all tests are passed
  293. } else { // flash is too old or we can't detect the plugin
  294. var alternateContent = 'Error<br \/>'
  295. + 'Hotspots requires Macromedia Flash 7.<br \/>'
  296. + '<a href=http://www.macromedia.com/go/getflash/>Get Flash<\/a>';
  297. document.write(alternateContent); // insert non-flash content
  298. }
  299. // -->
  300. </script></td>
  301. <td valign='top'>$answer_list</td></tr></table>
  302. </td></tr>";
  303. echo $s;
  304. }
  305. return $nbrAnswers;
  306. }
  307. ?>