learnpathlink.class.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. }
  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. }
  55. $session_id = api_get_session_id();
  56. if (empty($session_id)) {
  57. $session_condition = api_get_session_condition(0, true);
  58. } else {
  59. $session_condition = api_get_session_condition($session_id, true, true);
  60. }
  61. $sql = 'SELECT id, name FROM '.$this->get_learnpath_table().'
  62. WHERE c_id = ' . $this->course_id.' '.$session_condition.' ';
  63. $result = Database::query($sql);
  64. $cats = array();
  65. while ($data = Database::fetch_array($result)) {
  66. $cats[] = array($data['id'], $data['name']);
  67. }
  68. return $cats;
  69. }
  70. /**
  71. * Has anyone used this learnpath yet ?
  72. */
  73. public function has_results()
  74. {
  75. $tbl_stats = Database::get_course_table(TABLE_LP_VIEW);
  76. $sql = "SELECT count(id) AS number FROM $tbl_stats
  77. WHERE c_id = ".$this->course_id." AND lp_id = ".$this->get_ref_id();
  78. $result = Database::query($sql);
  79. $number = Database::fetch_array($result, 'NUM');
  80. return ($number[0] != 0);
  81. }
  82. /**
  83. * Get the progress of this learnpath. Only the last attempt are taken into account.
  84. * @param $stud_id student id (default: all students who have results - then the average is returned)
  85. * @param $type The type of score we want to get: best|average|ranking
  86. * @return array (score, max) if student is given
  87. * array (sum of scores, number of scores) otherwise
  88. * or null if no scores available
  89. */
  90. public function calc_score($stud_id = null, $type = null)
  91. {
  92. $tbl_stats = Database::get_course_table(TABLE_LP_VIEW);
  93. $session_id = $this->get_session_id();
  94. if (empty($session_id)) {
  95. $session_id = api_get_session_id();
  96. }
  97. $sql = "SELECT * FROM $tbl_stats
  98. WHERE
  99. c_id = ".$this->course_id." AND
  100. lp_id = " . $this->get_ref_id()." AND
  101. session_id = $session_id ";
  102. if (isset($stud_id)) {
  103. $sql .= ' AND user_id = '.intval($stud_id);
  104. }
  105. // order by id, that way the student's first attempt is accessed first
  106. $sql .= ' ORDER BY view_count DESC';
  107. $scores = Database::query($sql);
  108. // for 1 student
  109. if (isset($stud_id)) {
  110. if ($data = Database::fetch_assoc($scores)) {
  111. return array($data['progress'], 100);
  112. } else {
  113. return null;
  114. }
  115. } else {
  116. // all students -> get average
  117. $students = array(); // user list, needed to make sure we only
  118. // take first attempts into account
  119. $rescount = 0;
  120. $sum = 0;
  121. $bestResult = 0;
  122. $sumResult = 0;
  123. while ($data = Database::fetch_array($scores)) {
  124. if (!(array_key_exists($data['user_id'], $students))) {
  125. $students[$data['user_id']] = $data['progress'];
  126. $rescount++;
  127. $sum += $data['progress'] / 100;
  128. $sumResult += $data['progress'];
  129. if ($data['progress'] > $bestResult) {
  130. $bestResult = $data['progress'];
  131. }
  132. }
  133. }
  134. if ($rescount == 0) {
  135. return null;
  136. } else {
  137. switch ($type) {
  138. case 'best':
  139. return array($bestResult, 100);
  140. break;
  141. case 'average':
  142. return array($sumResult / $rescount, 100);
  143. break;
  144. case 'ranking':
  145. return AbstractLink::getCurrentUserRanking($stud_id, $students);
  146. break;
  147. default:
  148. return array($sum, $rescount);
  149. break;
  150. }
  151. }
  152. }
  153. }
  154. /**
  155. * Get URL where to go to if the user clicks on the link.
  156. */
  157. public function get_link()
  158. {
  159. $session_id = api_get_session_id();
  160. $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.api_get_cidreq_params($this->get_course_code(),
  161. $session_id).'&gradebook=view';
  162. if (!api_is_allowed_to_edit() || $this->calc_score(api_get_user_id()) == null) {
  163. $url .= '&action=view&lp_id='.$this->get_ref_id();
  164. } else {
  165. $url .= '&action=build&lp_id='.$this->get_ref_id();
  166. }
  167. return $url;
  168. }
  169. /**
  170. * Get name to display: same as learnpath title
  171. */
  172. public function get_name()
  173. {
  174. $data = $this->get_learnpath_data();
  175. return $data['name'];
  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. * Check if this still links to a learnpath
  187. */
  188. public function is_valid_link()
  189. {
  190. $sql = 'SELECT count(id) FROM '.$this->get_learnpath_table().'
  191. WHERE c_id = ' . $this->course_id.' AND id = '.$this->get_ref_id().' ';
  192. $result = Database::query($sql);
  193. $number = Database::fetch_row($result, 'NUM');
  194. return ($number[0] != 0);
  195. }
  196. public function get_type_name()
  197. {
  198. return get_lang('LearningPaths');
  199. }
  200. public function needs_name_and_description()
  201. {
  202. return false;
  203. }
  204. public function needs_max()
  205. {
  206. return false;
  207. }
  208. public function needs_results()
  209. {
  210. return false;
  211. }
  212. public function is_allowed_to_change_name()
  213. {
  214. return false;
  215. }
  216. // INTERNAL FUNCTIONS
  217. /**
  218. * Lazy load function to get the database table of the learnpath
  219. */
  220. private function get_learnpath_table()
  221. {
  222. $this->learnpath_table = Database::get_course_table(TABLE_LP_MAIN);
  223. return $this->learnpath_table;
  224. }
  225. /**
  226. * Lazy load function to get the database contents of this learnpath
  227. */
  228. private function get_learnpath_data()
  229. {
  230. if (!isset($this->learnpath_data)) {
  231. $sql = 'SELECT * FROM '.$this->get_learnpath_table().'
  232. WHERE c_id = ' . $this->course_id.' AND id = '.$this->get_ref_id().' ';
  233. $result = Database::query($sql);
  234. $this->learnpath_data = Database::fetch_array($result);
  235. }
  236. return $this->learnpath_data;
  237. }
  238. public function get_icon_name()
  239. {
  240. return 'learnpath';
  241. }
  242. }