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

Source for file fill_blanks.class.php

Documentation is available at fill_blanks.class.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. *    File containing the FillBlanks class.
  23. *    @package dokeos.exercise
  24. *     @author Eric Marguin
  25. *     @version $Id: admin.php 10680 2007-01-11 21:26:23Z pcool $
  26. */
  27.  
  28.  
  29. if(!class_exists('FillBlanks')):
  30.  
  31. /**
  32.     CLASS FillBlanks
  33.  *
  34.  *    This class allows to instantiate an object of type MULTIPLE_ANSWER (MULTIPLE CHOICE, MULTIPLE ANSWER),
  35.  *    extending the class question
  36.  *
  37.  *    @author Eric Marguin
  38.  *    @package dokeos.exercise
  39.  ***/
  40.  
  41. class FillBlanks extends Question {
  42.  
  43.     static $typePicture 'fill_in_blanks.gif';
  44.     static $explanationLangVar 'FillBlanks';
  45.  
  46.     /**
  47.      * Constructor
  48.      */
  49.     function FillBlanks(){
  50.         parent::question();
  51.         $this -> type = FILL_IN_BLANKS;
  52.     }
  53.  
  54.     /**
  55.      * function which redifines Question::createAnswersForm
  56.      * @param the formvalidator instance
  57.      */
  58.     function createAnswersForm ($form{
  59.  
  60.  
  61.  
  62.         $defaults array();
  63.  
  64.         if(!empty($this->id))
  65.         {
  66.             $objAnswer new answer($this->id);
  67.             $a_answer explode('::'$objAnswer->selectAnswer(1));
  68.             $defaults['answer'$a_answer[0];
  69.             $a_weightings explode(',',$a_answer[1]);
  70.         }
  71.         else
  72.         {
  73.             $defaults['answer'get_lang('DefaultTextInBlanks');
  74.         }
  75.  
  76.         // javascript
  77.         echo '
  78.         <script type="text/javascript">
  79.         var firstTime = true;
  80.         function updateBlanks() {
  81.             field = document.getElementById("answer");
  82.             var answer = field.value;
  83.             var blanks = answer.match(/\[[^\]]*\]/g);
  84.  
  85.             var fields = "<div class=\"row\"><div class=\"label\">'.get_lang('Weighting').'</div><div class=\"formw\"><table>";
  86.             if(blanks!=null){
  87.                 for(i=0 ; i<blanks.length ; i++){
  88.                     if(document.getElementById("weighting["+i+"]"))
  89.                         value = document.getElementById("weighting["+i+"]").value;
  90.                     else
  91.                         value = "10";
  92.                     fields += "<tr><td>"+blanks[i]+"</td><td><input style=\"margin-left: 0em;\" size=\"5\" value=\""+value+"\" type=\"text\" id=\"weighting["+i+"]\" name=\"weighting["+i+"]\" /></td></tr>";
  93.  
  94.                 }
  95.             }
  96.             document.getElementById("blanks_weighting").innerHTML = fields + "</table></div></div>";
  97.             if(firstTime){
  98.                 firstTime = false;
  99.             ';
  100.  
  101.         if(count($a_weightings)>0)
  102.         {
  103.             foreach($a_weightings as $i=>$weighting)
  104.             {
  105.                 echo 'document.getElementById("weighting['.$i.']").value = "'.$weighting.'";';
  106.  
  107.             }
  108.         }
  109.         echo '}
  110.         }
  111.         window.onload = updateBlanks;
  112.         </script>
  113.         ';
  114.  
  115.  
  116.         // answer
  117.         $form -> addElement ('html''<br /><br /><div class="row"><div class="label"></div><div class="formw">'.get_lang('TypeTextBelow').', '.get_lang('And').' '.get_lang('UseTagForBlank').'</div></div>');
  118.         $form -> addElement ('textarea''answer',get_lang('Answer'),'id="answer" cols="65" rows="6" onkeyup="updateBlanks(this)"');
  119.         $form -> addRule ('answer',get_lang('GiveText'),'required');
  120.         $form -> addRule ('answer',get_lang('DefineBlanks'),'regex','/\[.*\]/');
  121.  
  122.  
  123.         $form -> addElement('html','<div id="blanks_weighting"></div>');
  124.  
  125.         $form -> setDefaults($defaults);
  126.  
  127.     }
  128.  
  129.  
  130.     /**
  131.      * abstract function which creates the form to create / edit the answers of the question
  132.      * @param the formvalidator instance
  133.      */
  134.     function processAnswersCreation($form
  135.     {
  136.         $answer $form -> getSubmitValue('answer');
  137.  
  138.         //remove the :: eventually written by the user
  139.         $answer str_replace('::','',$answer);
  140.  
  141.         // get the blanks weightings
  142.         $nb preg_match_all('/\[[^\]]*\]/'$answer$blanks);
  143.         if(isset($_GET['editQuestion'])){
  144.             $this -> weighting = 0;
  145.         }
  146.         if($nb>0)
  147.         {
  148.             $answer .= '::';
  149.             for($i=$i<$nb ++$i)
  150.             {
  151.                 $answer .= $form -> getSubmitValue('weighting['.$i.']').',';
  152.                 $this -> weighting += $form -> getSubmitValue('weighting['.$i.']');
  153.             }
  154.             $answer substr($answer,0,-1);
  155.         }
  156.         $this -> save();
  157.         $objAnswer new answer($this->id);
  158.         $objAnswer->createAnswer($answer,0,'',0,'');
  159.         $objAnswer->save();
  160.     }
  161. }
  162.  
  163. endif;
  164. ?>

Documentation generated on Thu, 12 Jun 2008 13:29:52 -0500 by phpDocumentor 1.4.1