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

Source for file learnpathlink.class.php

Documentation is available at learnpathlink.class.php

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

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