From ff3d791efd0adeac5c1458d98b59365611cabb00 Mon Sep 17 00:00:00 2001 From: Yannick Warnier Date: Sun, 16 Dec 2007 06:09:52 +0100 Subject: [PATCH] [svn r13995] Added learning paths to gradebook --- main/gradebook/lib/be/learnpathlink.class.php | 233 ++++++++++++++++++ main/gradebook/lib/be/linkfactory.class.php | 9 +- 2 files changed, 240 insertions(+), 2 deletions(-) create mode 100644 main/gradebook/lib/be/learnpathlink.class.php diff --git a/main/gradebook/lib/be/learnpathlink.class.php b/main/gradebook/lib/be/learnpathlink.class.php new file mode 100644 index 0000000000..5a1f1f5fdd --- /dev/null +++ b/main/gradebook/lib/be/learnpathlink.class.php @@ -0,0 +1,233 @@ + + * @author Bert Steppé + * @package dokeos.gradebook + */ +class LearnpathLink extends AbstractLink +{ +// INTERNAL VARIABLES + + private $course_info = null; + private $learnpath_table = null; + private $learnpath_data = null; + + +// CONSTRUCTORS + + function LearnpathLink() + { + $this->set_type(LINK_LEARNPATH); + } + + +// FUNCTIONS IMPLEMENTING ABSTRACTLINK + + /** + * Generate an array of learnpaths that a teacher hasn't created a link for. + * @return array 2-dimensional array - every element contains 2 subelements (id, name) + */ + public function get_not_created_links() + { + if (empty($this->course_code)) + die('Error in get_not_created_links() : course code not set'); + + $tbl_grade_links = Database :: get_gradebook_table(TABLE_GRADEBOOK_LINK); + + $sql = 'SELECT id,name from '.$this->get_learnpath_table() + .' WHERE id NOT IN' + .' (SELECT ref_id FROM '.$tbl_grade_links + .' WHERE type = '.LINK_LEARNPATH + ." AND course_code = '".$this->get_course_code()."'" + .')'; + + $result = api_sql_query($sql, __FILE__, __LINE__); + + $cats=array(); + while ($data=Database::fetch_array($result)) + { + $cats[] = array ($data['id'], $data['name']); + } + return $cats; + } + + /** + * Has anyone used this learnpath yet ? + */ + public function has_results() + { + $course_info = api_get_course_info($this->get_course_code); + $tbl_stats = Database::get_course_table(TABLE_LP_VIEW,$course_info['dbName']); + $sql = 'SELECT count(id) AS number FROM '.$tbl_stats + ." WHERE lp_id = '".$this->get_ref_id()."'"; + $result = api_sql_query($sql, __FILE__, __LINE__); + $number=Database::fetch_array($result,'NUM'); + return ($number[0] != 0); + } + + /** + * Get the progress of this learnpath. Only the last attempt are taken into account. + * @param $stud_id student id (default: all students who have results - then the average is returned) + * @return array (score, max) if student is given + * array (sum of scores, number of scores) otherwise + * or null if no scores available + */ + public function calc_score($stud_id = null) + { + $course_info = api_get_course_info($this->get_course_code); + $tbl_stats = Database::get_course_table(TABLE_LP_VIEW,$course_info['dbName']); + $sql = 'SELECT * FROM '.$tbl_stats + ." WHERE lp_id = ".$this->get_ref_id(); + + if (isset($stud_id)) + $sql .= ' AND user_id = '.$stud_id; + + // order by id, that way the student's first attempt is accessed first + $sql .= ' ORDER BY view_count DESC'; + + $scores = api_sql_query($sql, __FILE__, __LINE__); + + // for 1 student + if (isset($stud_id)) + { + if ($data=Database::fetch_array($scores)) + return array ($data['progress'], 100); + else + return null; + } + + // all students -> get average + else + { + $students=array(); // user list, needed to make sure we only + // take first attempts into account + $rescount = 0; + $sum = 0; + + while ($data=Database::fetch_array($scores)) + { + if (!(array_key_exists($data['user_id'],$students))) + { + $students[$data['user_id']] = $data['progress']; + $rescount++; + $sum += ($data['progress'] / 100); + } + } + + if ($rescount == 0) + return null; + else + return array ($sum , $rescount); + } + } + + /** + * Get URL where to go to if the user clicks on the link. + */ + public function get_link() + { + $url = api_get_path(WEB_PATH) + .'main/newscorm/lp_controller.php?cidReq='.$this->get_course_code(); + if (!api_is_allowed_to_create_course() + && $this->calc_score(api_get_user_id()) == null) + { + $url .= '&action=view&lp_id='.$this->get_ref_id(); + } + else + { + $url .= '&action=build&lp_id='.$this->get_ref_id(); + } + return $url; + } + + /** + * Get name to display: same as learnpath title + */ + public function get_name() + { + $data = $this->get_learnpath_data(); + return $data['name']; + } + + /** + * Get description to display: same as learnpath description + */ + public function get_description() + { + $data = $this->get_learnpath_data(); + return $data['description']; + } + + /** + * Check if this still links to a learnpath + */ + public function is_valid_link() + { + $sql = 'SELECT count(id) from '.$this->get_learnpath_table() + .' WHERE id = '.$this->get_ref_id(); + $result = api_sql_query($sql, __FILE__, __LINE__); + $number=Database::fetch_row($result,'NUM'); + return ($number[0] != 0); + } + + public function get_type_name() + { + return get_lang('LearningPath'); + } + + public function needs_name_and_description() + { + return false; + } + + public function needs_max() + { + return false; + } + + public function needs_results() + { + return false; + } + + public function is_allowed_to_change_name() + { + return false; + } + + +// INTERNAL FUNCTIONS + + /** + * Lazy load function to get the database table of the learnpath + */ + private function get_learnpath_table () + { + if (!isset($this->learnpath_table)) + { + $course_info = api_get_course_info($this->get_course_code()); + $database_name = $course_info['dbName']; + $this->learnpath_table = Database :: get_course_table(TABLE_LP_MAIN, $database_name); + } + return $this->learnpath_table; + } + + /** + * Lazy load function to get the database contents of this learnpath + */ + private function get_learnpath_data() + { + if (!isset($this->learnpath_data)) + { + $sql = 'SELECT * from '.$this->get_learnpath_table() + .' WHERE id = '.$this->get_ref_id(); + $result = api_sql_query($sql, __FILE__, __LINE__); + $this->learnpath_data=Database::fetch_array($result); + } + return $this->learnpath_data; + } + + +} +?> \ No newline at end of file diff --git a/main/gradebook/lib/be/linkfactory.class.php b/main/gradebook/lib/be/linkfactory.class.php index 9d71d8f768..04cdea22b6 100644 --- a/main/gradebook/lib/be/linkfactory.class.php +++ b/main/gradebook/lib/be/linkfactory.class.php @@ -10,6 +10,7 @@ define('LINK_EXERCISE',1); define('LINK_DROPBOX',2); define('LINK_STUDENTPUBLICATION',3); +define('LINK_LEARNPATH',4); @@ -18,11 +19,12 @@ include_once('exerciselink.class.php'); include_once('evallink.class.php'); include_once('dropboxlink.class.php'); include_once('studentpublicationlink.class.php'); +include_once('learnpathlink.class.php'); /** * Factory for link objects - * @author Bert Steppé + * @author Bert Stepp� * @package dokeos.gradebook */ class LinkFactory @@ -78,6 +80,7 @@ class LinkFactory if ($type == LINK_EXERCISE ) return new ExerciseLink(); elseif ($type == LINK_DROPBOX ) return new DropboxLink(); elseif ($type == LINK_STUDENTPUBLICATION ) return new StudentPublicationLink(); + elseif ($type == LINK_LEARNPATH ) return new LearnpathLink(); else return null; } @@ -88,7 +91,9 @@ class LinkFactory { return array (LINK_EXERCISE, LINK_DROPBOX, - LINK_STUDENTPUBLICATION); + LINK_STUDENTPUBLICATION, + LINK_LEARNPATH, + ); } }