learnpathlink.class.php 6.9 KB

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