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

Source for file answer.class.php

Documentation is available at answer.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. *    This class allows to instantiate an object of type Answer
  23. *    5 arrays are created to receive the attributes of each answer belonging to a specified question
  24. *     @package dokeos.exercise
  25. *     @author Olivier Brouckaert
  26. *     @version $Id: answer.class.php 13732 2007-11-21 15:19:33Z yannoo $
  27. */
  28.  
  29.  
  30. if(!class_exists('Answer')):
  31.  
  32.  
  33. class Answer
  34. {
  35.     var $questionId;
  36.  
  37.     // these are arrays
  38.     var $answer;
  39.     var $correct;
  40.     var $comment;
  41.     var $weighting;
  42.     var $position;
  43.     var $hotspot_coordinates;
  44.     var $hotspot_type;
  45.  
  46.     // these arrays are used to save temporarily new answers
  47.     // then they are moved into the arrays above or deleted in the event of cancellation
  48.     var $new_answer;
  49.     var $new_correct;
  50.     var $new_comment;
  51.     var $new_weighting;
  52.     var $new_position;
  53.     var $new_hotspot_type;
  54.  
  55.     var $nbrAnswers;
  56.     var $new_nbrAnswers;
  57.  
  58. /**
  59.      * constructor of the class
  60.      *
  61.      * @author     Olivier Brouckaert
  62.      * @param     integer    Question ID that answers belong to
  63.      */
  64.     function Answer($questionId)
  65.     {
  66.         $this->questionType=$questionType;
  67.         $this->questionId=$questionId;
  68.         $this->answer=array();
  69.         $this->correct=array();
  70.         $this->comment=array();
  71.         $this->weighting=array();
  72.         $this->position=array();
  73.         $this->hotspot_coordinates=array();
  74.         $this->hotspot_type=array();
  75.  
  76.         // clears $new_* arrays
  77.         $this->cancel();
  78.  
  79.         // fills arrays
  80.         $this->read();
  81.     }
  82.  
  83.     /**
  84.      * clears $new_* arrays
  85.      *
  86.      * @author - Olivier Brouckaert
  87.      */
  88.     function cancel()
  89.     {
  90.         $this->new_answer=array();
  91.         $this->new_correct=array();
  92.         $this->new_comment=array();
  93.         $this->new_weighting=array();
  94.         $this->new_position=array();
  95.         $this->new_hotspot_coordinates=array();
  96.         $this->new_hotspot_type=array();
  97.  
  98.         $this->new_nbrAnswers=0;
  99.     }
  100.  
  101.     /**
  102.      * reads answer informations from the data base
  103.      *
  104.      * @author - Olivier Brouckaert
  105.      */
  106.     function read()
  107.     {
  108.         global $_course;
  109.         $TBL_ANSWER Database::get_course_table(TABLE_QUIZ_ANSWER);
  110.  
  111.         $questionId=$this->questionId;
  112.         //$answerType=$this->selectType();
  113.  
  114.         $sql="SELECT answer,correct,comment,ponderation,position, hotspot_coordinates, hotspot_type FROM $TBL_ANSWER WHERE question_id='$questionId' ORDER BY position";
  115.  
  116.         $result=api_sql_query($sql,__FILE__,__LINE__);
  117.  
  118.         $i=1;
  119.  
  120.         // while a record is found
  121.         while($object=mysql_fetch_object($result))
  122.         {
  123.             $this->answer[$i]=$object->answer;
  124.             $this->correct[$i]=$object->correct;
  125.             $this->comment[$i]=$object->comment;
  126.             $this->weighting[$i]=$object->ponderation;
  127.             $this->position[$i]=$object->position;
  128. $this->hotspot_coordinates[$i]=$object->hotspot_coordinates;
  129.             $this->hotspot_type[$i]=$object->hotspot_type;
  130.  
  131.             $i++;
  132.         }
  133.  
  134.         $this->nbrAnswers=$i-1;
  135.     }
  136.     /**
  137.      * reads answer informations from the data base ordered by parameter
  138.      * @param    string    Field we want to order by
  139.      * @param    string    DESC or ASC
  140.      * @author     Frederic Vauthier
  141.      */
  142.     function readOrderedBy($field,$order=ASC)
  143.     {
  144.         global $_course;
  145.         $field mysql_real_escape_string($field);
  146.         if(empty($field)){
  147.             $field 'position';
  148.         }
  149.         if($order != 'ASC' and $order!='DESC'){
  150.             $order 'ASC';
  151.         }
  152.         $TBL_ANSWER Database::get_course_table('quiz_answer');
  153.  
  154.         $questionId=$this->questionId;
  155.         //$answerType=$this->selectType();
  156.  
  157.         $sql="SELECT answer,correct,comment,ponderation,position, hotspot_coordinates, hotspot_type " .
  158.                 "FROM $TBL_ANSWER .
  159.                 "WHERE question_id='$questionId.
  160.                 "ORDER BY $field $order";
  161.  
  162.         $result=api_sql_query($sql,__FILE__,__LINE__);
  163.  
  164.         $i=1;
  165.  
  166.         // while a record is found
  167.         while($object=mysql_fetch_object($result))
  168.         {
  169.             $this->answer[$i]=$object->answer;
  170.             $this->correct[$i]=$object->correct;
  171.             $this->comment[$i]=$object->comment;
  172.             $this->weighting[$i]=$object->ponderation;
  173.             $this->position[$i]=$object->position;
  174.  
  175.             $i++;
  176.         }
  177.  
  178.         $this->nbrAnswers=$i-1;
  179.     }
  180.  
  181.     /**
  182.      * returns the number of answers in this question
  183.      *
  184.      * @author - Olivier Brouckaert
  185.      * @return integer - number of answers
  186.      */
  187.     function selectNbrAnswers()
  188.     {
  189.         return $this->nbrAnswers;
  190.     }
  191.  
  192.     /**
  193.      * returns the question ID which the answers belong to
  194.      *
  195.      * @author - Olivier Brouckaert
  196.      * @return integer - the question ID
  197.      */
  198.     function selectQuestionId()
  199.     {
  200.         return $this->questionId;
  201.     }
  202.  
  203.     /**
  204.      * returns the answer title
  205.      *
  206.      * @author - Olivier Brouckaert
  207.      * @param integer $id - answer ID
  208.      * @return string - answer title
  209.      */
  210.     function selectAnswer($id)
  211.     {
  212.         return $this->answer[$id];
  213.     }
  214.  
  215.     /**
  216.      * Returns a list of answers
  217.      * @author Yannick Warnier <ywarnier@beeznest.org>
  218.      * @return array    List of answers where each answer is an array of (id, answer, comment, grade) and grade=weighting
  219.      */
  220.      function getAnswersList()
  221.      {
  222.          $list array();
  223.          for($i 1$i<=$this->nbrAnswers;$i++){
  224.              if(!empty($this->answer[$i])){
  225.                  $list[array(
  226.                         'id'=>$i,
  227.                         'answer'=>addslashes($this->answer[$i]),
  228.                         'comment'=>$this->comment[$i],
  229.                         'grade' => $this->weighting[$i],
  230.                         'hotspot_coord' => $this->hotspot_coordinates[$i],
  231.                         'hotspot_type'    => $this->hotspot_type[$i],
  232.                         'correct'        => $this->correct[$i]
  233.                 );
  234.              }
  235.          }
  236.          return $list;
  237.      }
  238.     /**
  239.      * Returns a list of grades
  240.      * @author Yannick Warnier <ywarnier@beeznest.org>
  241.      * @return array    List of grades where grade=weighting (?)
  242.      */
  243.      function getGradesList()
  244.      {
  245.          $list array();
  246.          for($i 0$i<$this->nbrAnswers;$i++){
  247.              if(!empty($this->answer[$i])){
  248.                  $list[$i$this->weighting[$i];
  249.              }
  250.          }
  251.          return $list;
  252.      }
  253.  
  254.      /**
  255.       * Returns the question type
  256.       * @author    Yannick Warnier <ywarnier@beeznest.org>
  257.       * @return    integer    The type of the question this answer is bound to
  258.       */
  259.      function getQuestionType()
  260.      {
  261.          $TBL_QUESTIONS Database::get_course_table(TABLE_QUIZ_QUESTION);
  262.          $sql "SELECT * FROM $TBL_QUESTIONS WHERE id = '".$this->questionId."'";
  263.          $res api_sql_query($sql,__FILE__,__LINE__);
  264.          if(Database::num_rows($res)<=0){
  265.              return null;
  266.          }
  267.          $row Database::fetch_array($res);
  268.          return $row['type'];
  269.      }
  270.  
  271.  
  272.     /**
  273.      * tells if answer is correct or not
  274.      *
  275.      * @author - Olivier Brouckaert
  276.      * @param integer $id - answer ID
  277.      * @return integer - 0 if bad answer, not 0 if good answer
  278.      */
  279.     function isCorrect($id)
  280.     {
  281.         return $this->correct[$id];
  282.     }
  283.  
  284.     /**
  285.      * returns answer comment
  286.      *
  287.      * @author - Olivier Brouckaert
  288.      * @param integer $id - answer ID
  289.      * @return string - answer comment
  290.      */
  291.     function selectComment($id)
  292.     {
  293.         return $this->comment[$id];
  294.     }
  295.  
  296.     /**
  297.      * returns answer weighting
  298.      *
  299.      * @author - Olivier Brouckaert
  300.      * @param integer $id - answer ID
  301.      * @return integer - answer weighting
  302.      */
  303.     function selectWeighting($id)
  304.     {
  305.         return $this->weighting[$id];
  306.     }
  307.  
  308.     /**
  309.      * returns answer position
  310.      *
  311.      * @author - Olivier Brouckaert
  312.      * @param integer $id - answer ID
  313.      * @return integer - answer position
  314.      */
  315.     function selectPosition($id)
  316.     {
  317.         return $this->position[$id];
  318.     }
  319.  
  320.     /**
  321.      * returns answer hotspot coordinates
  322.      *
  323.      * @author    Olivier Brouckaert
  324.      * @param    integer    Answer ID
  325.      * @return    integer    Answer position
  326.      */
  327.     function selectHotspotCoordinates($id)
  328.     {
  329.         return $this->hotspot_coordinates[$id];
  330.     }
  331.  
  332.     /**
  333.      * returns answer hotspot type
  334.      *
  335.      * @author    Toon Keppens
  336.      * @param    integer        Answer ID
  337.      * @return    integer        Answer position
  338.      */
  339.     function selectHotspotType($id)
  340.     {
  341.         return $this->hotspot_type[$id];
  342.     }
  343.  
  344.     /**
  345.      * creates a new answer
  346.      *
  347.      * @author Olivier Brouckaert
  348.      * @param string     answer title
  349.      * @param integer     0 if bad answer, not 0 if good answer
  350.      * @param string     answer comment
  351.      * @param integer     answer weighting
  352.      * @param integer     answer position
  353.      * @param coordinates     Coordinates for hotspot exercises (optional)
  354.      * @param integer        Type for hotspot exercises (optional)
  355.      */
  356.     function createAnswer($answer,$correct,$comment,$weighting,$position,$new_hotspot_coordinates NULL$new_hotspot_type NULL)
  357.     {
  358.         $this->new_nbrAnswers++;
  359.  
  360.         $id=$this->new_nbrAnswers;
  361.  
  362.         $this->new_answer[$id]=$answer;
  363.         $this->new_correct[$id]=$correct;
  364.         $this->new_comment[$id]=$comment;
  365.         $this->new_weighting[$id]=$weighting;
  366.         $this->new_position[$id]=$position;
  367.         $this->new_hotspot_coordinates[$id]=$new_hotspot_coordinates;
  368.         $this->new_hotspot_type[$id]=$new_hotspot_type;
  369.     }
  370.  
  371.     /**
  372.      * updates an answer
  373.      *
  374.      * @author Toon Keppens
  375.      * @param    string    Answer title
  376.      * @param    string    Answer comment
  377.      * @param    integer    Answer weighting
  378.      * @param    integer    Answer position
  379.      */
  380.     function updateAnswers($answer,$comment,$weighting,$position)
  381.     {
  382.         global $TBL_REPONSES;
  383.  
  384.         $questionId=$this->questionId;
  385.  
  386.         $sql "UPDATE $TBL_REPONSES SET .
  387.                 "`answer` = '$answer', .
  388.                 "`comment` = '$comment', .
  389.                 "`ponderation` = '$weighting', .
  390.                 "`position` = '$position.
  391.                 "WHERE `id` =$position .
  392.                 "AND `question_id` =$questionId";
  393.  
  394.         api_sql_query($sql,__FILE__,__LINE__);
  395.     }
  396.  
  397.     /**
  398.      * records answers into the data base
  399.      *
  400.      * @author - Olivier Brouckaert
  401.      */
  402.     function save()
  403.     {
  404.         global $TBL_REPONSES;
  405.  
  406.         $questionId=$this->questionId;
  407.  
  408.         // removes old answers before inserting of new ones
  409.         $sql="DELETE FROM $TBL_REPONSES WHERE question_id='$questionId'";
  410.         api_sql_query($sql,__FILE__,__LINE__);
  411.  
  412.         // inserts new answers into data base
  413.         $sql="INSERT INTO $TBL_REPONSES.
  414.                 "(id,question_id,answer,correct,comment," .
  415.                 "ponderation,position,hotspot_coordinates,hotspot_type) VALUES";
  416.  
  417.         for($i=1;$i <= $this->new_nbrAnswers;$i++)
  418.         {
  419.             $answer=addslashes($this->new_answer[$i]);
  420.             $correct=$this->new_correct[$i];
  421.             $comment=addslashes($this->new_comment[$i]);
  422.             $weighting=$this->new_weighting[$i];
  423.             $position=$this->new_position[$i];
  424.             $hotspot_coordinates=$this->new_hotspot_coordinates[$i];
  425.             $hotspot_type=$this->new_hotspot_type[$i];
  426.  
  427.             $sql.="('$i','$questionId','$answer','$correct','$comment',
  428.                     '$weighting','$position','$hotspot_coordinates','$hotspot_type'),";
  429.         }
  430.  
  431.         $sql substr($sql,0,-1);
  432.  
  433.         api_sql_query($sql,__FILE__,__LINE__);
  434.  
  435.         // moves $new_* arrays
  436.         $this->answer=$this->new_answer;
  437.         $this->correct=$this->new_correct;
  438.         $this->comment=$this->new_comment;
  439.         $this->weighting=$this->new_weighting;
  440.         $this->position=$this->new_position;
  441.         $this->hotspot_coordinates=$this->new_hotspot_coordinates;
  442.         $this->hotspot_type=$this->new_hotspot_type;
  443.  
  444.         $this->nbrAnswers=$this->new_nbrAnswers;
  445.  
  446.         // clears $new_* arrays
  447.         $this->cancel();
  448.     }
  449.  
  450.     /**
  451.      * duplicates answers by copying them into another question
  452.      *
  453.      * @author - Olivier Brouckaert
  454.      * @param integer $newQuestionId - ID of the new question
  455.      */
  456.     function duplicate($newQuestionId)
  457.     {
  458.         global $TBL_REPONSES;
  459.  
  460.         // if at least one answer
  461.         if($this->nbrAnswers)
  462.         {
  463.             // inserts new answers into data base
  464.             $sql="INSERT INTO $TBL_REPONSES.
  465.                     "(id,question_id,answer,correct,comment," .
  466.                     "ponderation,position,hotspot_coordinates,hotspot_type) VALUES";
  467.  
  468.             for($i=1;$i <= $this->nbrAnswers;$i++)
  469.             {
  470.                 $answer=addslashes($this->answer[$i]);
  471.                 $correct=$this->correct[$i];
  472.                 $comment=addslashes($this->comment[$i]);
  473.                 $weighting=$this->weighting[$i];
  474.                 $position=$this->position[$i];
  475.                 $hotspot_coordinates=$this->hotspot_coordinates[$i];
  476.                 $hotspot_type=$this->hotspot_type[$i];
  477.  
  478.                 $sql.="('$i','$newQuestionId','$answer','$correct','$comment',.
  479.                         "'$weighting','$position','$hotspot_coordinates','$hotspot_type'),";
  480.             }
  481.  
  482.             $sql=substr($sql,0,-1);
  483.             api_sql_query($sql,__FILE__,__LINE__);
  484.         }
  485.     }
  486. }
  487.  
  488. endif;
  489. ?>

Documentation generated on Thu, 12 Jun 2008 12:58:32 -0500 by phpDocumentor 1.4.1