learnpathlink.class.php 6.9 KB

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