learnpathlink.class.php 7.3 KB

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