dokeos-exercise
[ class tree: dokeos-exercise ] [ index: dokeos-exercise ] [ all elements ]

Source for file exercise.lib.php

Documentation is available at exercise.lib.php

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

Documentation generated on Thu, 12 Jun 2008 13:25:19 -0500 by phpDocumentor 1.4.1