learnpathlink.class.php 8.3 KB

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