exercise.lib.php 16 KB

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