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

Source for file matching.class.php

Documentation is available at matching.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 Matching 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('Matching')):
  30.  
  31. /**
  32.     CLASS Matching
  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 Matching extends Question {
  42.  
  43.  
  44.     static $typePicture 'matching.gif';
  45.     static $explanationLangVar 'Matching';
  46.  
  47.     /**
  48.      * Constructor
  49.      */
  50.     function Matching(){
  51.         parent::question();
  52.         $this -> type = MATCHING;
  53.     }
  54.  
  55.     /**
  56.      * function which redifines Question::createAnswersForm
  57.      * @param the formvalidator instance
  58.      */
  59.     function createAnswersForm ($form{
  60.  
  61.         $defaults array();
  62.  
  63.         $nb_matches $nb_options 2;
  64.         if($form -> isSubmitted())
  65.         {
  66.             $nb_matches $form -> getSubmitValue('nb_matches');
  67.             $nb_options $form -> getSubmitValue('nb_options');
  68.             if(isset($_POST['lessMatches']))
  69.                 $nb_matches--;
  70.             if(isset($_POST['moreMatches']))
  71.                 $nb_matches++;
  72.             if(isset($_POST['lessOptions']))
  73.                 $nb_options--;
  74.             if(isset($_POST['moreOptions']))
  75.                 $nb_options++;
  76.  
  77.         }
  78.         else if(!empty($this -> id))
  79.         {
  80.             $answer new Answer($this -> id);
  81.             $answer -> read();
  82.             if(count($answer->nbrAnswers)>0)
  83.             {
  84.                 $a_matches $a_options array();
  85.                 $nb_matches $nb_options 0;
  86.                 for($i=$i<=$answer->nbrAnswers $i++){
  87.                     if($answer -> isCorrect($i))
  88.                     {
  89.                         $nb_matches++;
  90.                         $defaults['answer['.$nb_matches.']'$answer -> selectAnswer($i);
  91.                         $defaults['weighting['.$nb_matches.']'$answer -> selectWeighting($i);
  92.                         $defaults['matches['.$nb_matches.']'$answer -> correct[$i];
  93.                     }
  94.                     else
  95.                     {
  96.                         $nb_options++;
  97.                         $defaults['option['.$nb_options.']'$answer -> selectAnswer($i);
  98.                     }
  99.                 }
  100.  
  101.             }
  102.         }
  103.         else {
  104.             $defaults['answer[1]'get_lang('DefaultMakeCorrespond1');
  105.             $defaults['answer[2]'get_lang('DefaultMakeCorrespond2');
  106.             $defaults['matches[2]''2';
  107.             $defaults['option[1]'get_lang('DefaultMatchingOptA');
  108.             $defaults['option[2]'get_lang('DefaultMatchingOptB');
  109.         }
  110.         $a_matches array();
  111.         for($i=$i<=$nb_options ++$i)
  112.         {
  113.             $a_matches[$ichr(64+$i);  // fill the array with A, B, C.....
  114.         }
  115.  
  116.  
  117.  
  118.  
  119.  
  120.         $form -> addElement('hidden''nb_matches'$nb_matches);
  121.         $form -> addElement('hidden''nb_options'$nb_options);
  122.  
  123.  
  124.         ////////////////////////
  125.         // DISPLAY MATCHES ////
  126.         //////////////////////
  127.  
  128.         $html='
  129.         <div class="row">
  130.             <div class="label">
  131.             '.get_lang('Answers').'
  132.             </div>
  133.             <div class="formw">
  134.                 '.get_lang('MakeCorrespond').'
  135.                 <table class="data_table">
  136.                     <tr style="text-align: center">
  137.                         <th>
  138.                             '.get_lang('Number').'
  139.                         </th>
  140.                         <th>
  141.                             '.get_lang('Answer').'
  142.                         </th>
  143.                         <th>
  144.                             '.get_lang('MatchesTo').'
  145.                         </th>
  146.                         <th>
  147.                             '.get_lang('Weighting').'
  148.                         </th>
  149.                         
  150.                     </tr>';
  151.         $form -> addElement ('html'$html);
  152.  
  153.  
  154.         for($i $i <= $nb_matches ++$i)
  155.         {
  156.  
  157.             $form -> addElement ('html''<tr><td>');
  158.  
  159.             $group array();
  160.             $puce FormValidator :: createElement ('text'null,null,'value="'.$i.'"');
  161.             $puce->freeze();
  162.             $group[$puce;
  163.             $group[FormValidator :: createElement ('text''answer['.$i.']',null'size="30" style="margin-left: 0em;"');
  164.             $group[FormValidator :: createElement ('select''matches['.$i.']',null,$a_matches);
  165.             $group[FormValidator :: createElement ('text''weighting['.$i.']',null'style="vertical-align:middle;margin-left: 0em;" size="2" value="10"');
  166.             $form -> addGroup($groupnullnull'</td><td width="0">');
  167.  
  168.             $form -> addElement ('html''</td></tr>');
  169.  
  170.         }
  171.  
  172.         $form -> addElement ('html''</table></div></div>');
  173.         $group array();
  174.         $group[FormValidator :: createElement ('submit''lessMatches'get_lang('DelElem'));
  175.         $group[FormValidator :: createElement ('submit''moreMatches'get_lang('AddElem'));
  176.         $form -> addGroup($group);
  177.  
  178.  
  179.  
  180.         ////////////////////////
  181.         // DISPLAY OPTIONS ////
  182.         //////////////////////
  183.         $html='
  184.         <div class="row">
  185.             <div class="label">
  186.             </div>
  187.             <div class="formw"><br /><br />
  188.                 <table class="data_table">
  189.                     <tr style="text-align: center;">
  190.                         <th>
  191.                             '.get_lang('Number').'                        
  192.                         </th>
  193.                         <th>
  194.                             '.get_lang('Answer').'
  195.                         </th>
  196.                         
  197.                     </tr>';
  198.         $form -> addElement ('html'$html);
  199.  
  200.  
  201.  
  202.         for($i $i <= $nb_options ++$i)
  203.         {
  204.             $form -> addElement ('html''<tr><td>');
  205.  
  206.             $group array();
  207.             $puce FormValidator :: createElement ('text'null,null,'value="'.chr(64+$i).'"');
  208.             $puce->freeze();
  209.             $group[$puce;
  210.             $group[FormValidator :: createElement ('text''option['.$i.']',null'size="30" style="margin-left: 0em;"');
  211.             $form -> addGroup($groupnullnull'</td><td width="0">');
  212.  
  213.             $form -> addElement ('html''</td></tr>');
  214.  
  215.         }
  216.  
  217.         $form -> addElement ('html''</table></div></div>');
  218.         $group array();
  219.         $group[FormValidator :: createElement ('submit''lessOptions'get_lang('DelElem'));
  220.         $group[FormValidator :: createElement ('submit''moreOptions',get_lang('AddElem'));
  221.         $form -> addGroup($group);
  222.  
  223.         $form -> setDefaults($defaults);
  224.  
  225.         $form->setConstants(array('nb_matches' => $nb_matches,'nb_options' => $nb_options));
  226.  
  227.     }
  228.  
  229.  
  230.     /**
  231.      * abstract function which creates the form to create / edit the answers of the question
  232.      * @param the formvalidator instance
  233.      */
  234.     function processAnswersCreation($form{
  235.  
  236.         $nb_matches $form -> getSubmitValue('nb_matches');
  237.         $nb_options $form -> getSubmitValue('nb_options');
  238.         $this -> weighting = 0;
  239.         $objAnswer new Answer($this->id);
  240.  
  241.         $position 0;
  242.  
  243.         // insert the options
  244.         for($i=$i<=$nb_options ++$i)
  245.         {
  246.             $position++;
  247.             $option $form -> getSubmitValue('option['.$i.']');
  248.             $objAnswer->createAnswer($option0''0$position);
  249.         }
  250.  
  251.         // insert the answers
  252.         for($i=$i<=$nb_matches ++$i)
  253.         {
  254.             $position++;
  255.             $answer $form -> getSubmitValue('answer['.$i.']');
  256.             $matches $form -> getSubmitValue('matches['.$i.']');
  257.             $weighting $form -> getSubmitValue('weighting['.$i.']');
  258.             $this -> weighting += $weighting;
  259.             $objAnswer->createAnswer($answer,$matches,'',$weighting,$position);
  260.         }
  261.  
  262.         $objAnswer->save();
  263.         $this->save();
  264.  
  265.     }
  266.  
  267. }
  268.  
  269. endif;
  270. ?>

Documentation generated on Thu, 12 Jun 2008 14:05:03 -0500 by phpDocumentor 1.4.1