learnpathlink.class.php 6.8 KB

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