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

Source for file exerciselink.class.php

Documentation is available at exerciselink.class.php

  1. <?php
  2.  
  3.  
  4. /**
  5.  * Defines a gradebook ExerciseLink object.
  6.  * @author Bert Stepp�
  7.  * @package dokeos.gradebook
  8.  */
  9. class ExerciseLink extends AbstractLink
  10. {
  11.  
  12.  
  13. // INTERNAL VARIABLES
  14.  
  15.     private $course_info = null;
  16.     private $exercise_table = null;
  17.     private $exercise_data = null;
  18.     
  19.  
  20. // CONSTRUCTORS
  21.  
  22.     function ExerciseLink()
  23.     {
  24.         $this->set_type(LINK_EXERCISE);
  25.     }
  26.  
  27.  
  28. // FUNCTIONS IMPLEMENTING ABSTRACTLINK
  29.  
  30.     /**
  31.      * Generate an array of exercises that a teacher hasn't created a link for.
  32.      * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  33.      */
  34.     public function get_not_created_links()
  35.     {
  36.         if (empty($this->course_code))
  37.             die('Error in get_not_created_links() : course code not set');
  38.         
  39.         $tbl_grade_links Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  40.  
  41.         $sql 'SELECT id,title from '.$this->get_exercise_table()
  42.                 .' WHERE id NOT IN'
  43.                 .' (SELECT ref_id FROM '.$tbl_grade_links
  44.                 .' WHERE type = '.LINK_EXERCISE
  45.                 ." AND course_code = '".$this->get_course_code()."'"
  46.                 .')';
  47.  
  48.         $result api_sql_query($sql__FILE____LINE__);
  49.  
  50.         $cats=array();
  51.         while ($data=mysql_fetch_array($result))
  52.         {
  53.             $cats[array ($data['id']$data['title']);
  54.         }
  55.         return $cats;
  56.     }
  57.     /**
  58.      * Generate an array of all exercises available.
  59.      * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  60.      */
  61.     public function get_all_links()
  62.     {
  63.         if (empty($this->course_code))
  64.             die('Error in get_not_created_links() : course code not set');
  65.         
  66.         $course_info api_get_course_info($this->course_code);
  67.         $tbl_grade_links Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK,$course_info['dbName']);
  68.  
  69.         $sql 'SELECT id,title from '.$this->get_exercise_table();
  70.         $result api_sql_query($sql__FILE____LINE__);
  71.  
  72.         $cats=array();
  73.         while ($data=mysql_fetch_array($result))
  74.         {
  75.             $cats[array ($data['id']$data['title']);
  76.         }
  77.         return $cats;
  78.     }
  79.  
  80.     /**
  81.      * Has anyone done this exercise yet ?
  82.      */
  83.     public function has_results()
  84.     {
  85.         $sql 'SELECT count(exe_id) AS number FROM '.$tbl_stats
  86.                 ." WHERE exe_cours_id = '".$this->get_course_code()."'"
  87.                 .' AND exe_exo_id = '.$this->get_ref_id();
  88.         $result api_sql_query($sql__FILE____LINE__);
  89.         $number=mysql_fetch_row($result);
  90.         return ($number[0!= 0);
  91.     }
  92.     
  93.     /**
  94.      * Get the score of this exercise. Only the first attempts are taken into account.
  95.      * @param $stud_id student id (default: all students who have results - then the average is returned)
  96.      * @return    array (score, max) if student is given
  97.      *              array (sum of scores, number of scores) otherwise
  98.      *              or null if no scores available
  99.      */
  100.     public function calc_score($stud_id null)
  101.     {
  102.         $sql 'SELECT * FROM '.$tbl_stats
  103.                 ." WHERE exe_cours_id = '".$this->get_course_code()."'"
  104.                 .' AND exe_exo_id = '.$this->get_ref_id();
  105.         
  106.         if (isset($stud_id))
  107.             $sql .= ' AND exe_user_id = '.$stud_id;
  108.             
  109.         // order by id, that way the student's first attempt is accessed first
  110.         $sql .= ' ORDER BY exe_id';
  111.  
  112.         $scores api_sql_query($sql__FILE____LINE__);
  113.  
  114.         // for 1 student
  115.         if (isset($stud_id))
  116.         {
  117.             if ($data=mysql_fetch_array($scores))
  118.                 return array ($data['exe_result']$data['exe_weighting']);
  119.             else
  120.                 return null;
  121.         }
  122.         
  123.         // all students -> get average
  124.         else
  125.         {
  126.             $students=array();  // user list, needed to make sure we only
  127.                                 // take first attempts into account
  128.             $rescount 0;
  129.             $sum 0;
  130.  
  131.             while ($data=mysql_fetch_array($scores))
  132.             {
  133.                 if (!(array_key_exists($data['exe_user_id'],$students)))
  134.                 {
  135.                     if ($data['exe_weighting'!= 0)
  136.                     {
  137.                         $students[$data['exe_user_id']] $data['exe_result'];
  138.                         $rescount++;
  139.                         $sum += ($data['exe_result'$data['exe_weighting']);
  140.                     }
  141.                 }
  142.             }
  143.  
  144.             if ($rescount == 0)
  145.                 return null;
  146.             else
  147.                 return array ($sum $rescount);
  148.         }
  149.     }
  150.     
  151.     /**
  152.      * Get URL where to go to if the user clicks on the link.
  153.      * First we go to exercise_jump.php and then to the result page.
  154.      * Check this php file for more info.
  155.      */
  156.     public function get_link()
  157.     {
  158.         $url api_get_path(WEB_PATH)
  159.             .'main/gradebook/exercise_jump.php?cid='.$this->get_course_code();
  160.         if (!api_is_allowed_to_create_course()
  161.             && $this->calc_score(api_get_user_id()) == null)
  162.         $url .= '&amp;doexercise='.$this->get_ref_id();
  163.         
  164.         return $url;
  165.     }
  166.     
  167.     /**
  168.      * Get name to display: same as exercise title
  169.      */
  170.     public function get_name()
  171.     {
  172.         $data $this->get_exercise_data();
  173.         return $data['title'];
  174.     }
  175.     
  176.     /**
  177.      * Get description to display: same as exercise description
  178.      */
  179.     public function get_description()
  180.     {
  181.         $data $this->get_exercise_data();
  182.         return $data['description'];
  183.     }
  184.  
  185.     /**
  186.      * Check if this still links to an exercise
  187.      */
  188.     public function is_valid_link()
  189.     {
  190.         $sql 'SELECT count(id) from '.$this->get_exercise_table()
  191.                 .' WHERE id = '.$this->get_ref_id();
  192.         $result api_sql_query($sql__FILE____LINE__);
  193.         $number=mysql_fetch_row($result);
  194.         return ($number[0!= 0);
  195.     }
  196.     
  197.     public function get_type_name()
  198.     {
  199.         return get_lang('DokeosExercises');
  200.     }
  201.     
  202.     public function needs_name_and_description()
  203.     {
  204.         return false;
  205.     }
  206.     
  207.     public function needs_max()
  208.     {
  209.         return false;
  210.     }
  211.  
  212.     public function needs_results()
  213.     {
  214.         return false;
  215.     }
  216.  
  217.     public function is_allowed_to_change_name()
  218.     {
  219.         return false;
  220.     }
  221.  
  222.     
  223. // INTERNAL FUNCTIONS
  224.     
  225.     /**
  226.      * Lazy load function to get the database table of the exercise
  227.      */
  228.     private function get_exercise_table ()
  229.     {
  230.         if (!isset($this->exercise_table))
  231.         {
  232.             $course_info Database :: get_course_info($this->get_course_code());
  233.             $database_name $course_info['db_name'];
  234.             $this->exercise_table = Database :: get_course_table(TABLE_QUIZ_TEST$database_name);
  235.         }
  236.            return $this->exercise_table;
  237.     }
  238.  
  239.     /**
  240.      * Lazy load function to get the database contents of this exercise
  241.      */
  242.     private function get_exercise_data()
  243.     {
  244.         if (!isset($this->exercise_data))
  245.         {
  246.             $sql 'SELECT * from '.$this->get_exercise_table()
  247.                     .' WHERE id = '.$this->get_ref_id();
  248.             $result api_sql_query($sql__FILE____LINE__);
  249.             $this->exercise_data=mysql_fetch_array($result);
  250.         }
  251.         return $this->exercise_data;
  252.     }
  253.     
  254.     
  255. }
  256. ?>

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