learnpathlink.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. /**
  10. * Class
  11. * @package chamilo.gradebook
  12. */
  13. class LearnpathLink extends AbstractLink
  14. {
  15. // INTERNAL VARIABLES
  16. private $course_info = null;
  17. private $learnpath_table = null;
  18. private $learnpath_data = null;
  19. // CONSTRUCTORS
  20. function __construct() {
  21. parent::__construct();
  22. $this->set_type(LINK_LEARNPATH);
  23. }
  24. // FUNCTIONS IMPLEMENTING ABSTRACTLINK
  25. /**
  26. * Generate an array of learnpaths that a teacher hasn't created a link for.
  27. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  28. */
  29. public function get_not_created_links() {
  30. return false;
  31. if (empty($this->course_code))
  32. die('Error in get_not_created_links() : course code not set');
  33. $tbl_grade_links = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  34. $sql = 'SELECT id, name from '.$this->get_learnpath_table().' lp
  35. WHERE c_id = '.$this->course_id.' AND id NOT IN '
  36. .' (SELECT ref_id FROM '.$tbl_grade_links
  37. .' WHERE type = '.LINK_LEARNPATH
  38. ." AND course_code = '".$this->get_course_code()."'"
  39. .') AND lp.session_id='.api_get_session_id().'';
  40. $result = Database::query($sql);
  41. $cats=array();
  42. while ($data=Database::fetch_array($result)) {
  43. $cats[] = array ($data['id'], $data['name']);
  44. }
  45. return $cats;
  46. }
  47. /**
  48. * Generate an array of all learnpaths available.
  49. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  50. */
  51. public function get_all_links() {
  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. $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) {
  88. $tbl_stats = Database::get_course_table(TABLE_LP_VIEW);
  89. $session_id = api_get_session_id();
  90. $sql = "SELECT * FROM $tbl_stats
  91. WHERE c_id = ".$this->course_id." AND
  92. lp_id = ".$this->get_ref_id()." AND
  93. session_id = $session_id ";
  94. if (isset($stud_id))
  95. $sql .= ' AND user_id = '.intval($stud_id);
  96. // order by id, that way the student's first attempt is accessed first
  97. $sql .= ' ORDER BY view_count DESC';
  98. $scores = Database::query($sql);
  99. // for 1 student
  100. if (isset($stud_id)) {
  101. if ($data = Database::fetch_array($scores)) {
  102. return array ($data['progress'], 100);
  103. } else
  104. return null;
  105. }
  106. // all students -> get average
  107. else {
  108. $students=array(); // user list, needed to make sure we only
  109. // take first attempts into account
  110. $rescount = 0;
  111. $sum = 0;
  112. while ($data=Database::fetch_array($scores)) {
  113. if (!(array_key_exists($data['user_id'], $students))) {
  114. $students[$data['user_id']] = $data['progress'];
  115. $rescount++;
  116. $sum += ($data['progress'] / 100);
  117. }
  118. }
  119. if ($rescount == 0)
  120. return null;
  121. else
  122. return array ($sum , $rescount);
  123. }
  124. }
  125. /**
  126. * Get URL where to go to if the user clicks on the link.
  127. */
  128. public function get_link() {
  129. $url = api_get_path(WEB_PATH).'main/newscorm/lp_controller.php?cidReq='.$this->get_course_code().'&gradebook=view';
  130. $session_id = api_get_session_id();
  131. if (!api_is_allowed_to_edit() || $this->calc_score(api_get_user_id()) == null) {
  132. $url .= '&action=view&session_id='.$session_id.'&lp_id='.$this->get_ref_id();
  133. } else {
  134. $url .= '&action=build&session_id='.$session_id.'&lp_id='.$this->get_ref_id();
  135. }
  136. return $url;
  137. }
  138. /**
  139. * Get name to display: same as learnpath title
  140. */
  141. public function get_name()
  142. {
  143. $data = $this->get_learnpath_data();
  144. return $data['name'];
  145. }
  146. /**
  147. * Get description to display: same as learnpath description
  148. */
  149. public function get_description()
  150. {
  151. $data = $this->get_learnpath_data();
  152. return $data['description'];
  153. }
  154. /**
  155. * Check if this still links to a learnpath
  156. */
  157. public function is_valid_link() {
  158. $sql = 'SELECT count(id) FROM '.$this->get_learnpath_table().'
  159. WHERE c_id = '.$this->course_id.' AND id = '.$this->get_ref_id().' ';
  160. $result = Database::query($sql);
  161. $number = Database::fetch_row($result,'NUM');
  162. return ($number[0] != 0);
  163. }
  164. public function get_type_name() {
  165. return get_lang('LearningPaths');
  166. }
  167. public function needs_name_and_description() {
  168. return false;
  169. }
  170. public function needs_max() {
  171. return false;
  172. }
  173. public function needs_results() {
  174. return false;
  175. }
  176. public function is_allowed_to_change_name() {
  177. return false;
  178. }
  179. // INTERNAL FUNCTIONS
  180. /**
  181. * Lazy load function to get the database table of the learnpath
  182. */
  183. private function get_learnpath_table() {
  184. $this->learnpath_table = Database :: get_course_table(TABLE_LP_MAIN);
  185. return $this->learnpath_table;
  186. }
  187. /**
  188. * Lazy load function to get the database contents of this learnpath
  189. */
  190. private function get_learnpath_data() {
  191. if (!isset($this->learnpath_data)) {
  192. $sql = 'SELECT * FROM '.$this->get_learnpath_table().'
  193. WHERE c_id = '.$this->course_id.' AND id = '.$this->get_ref_id().' ';
  194. $result = Database::query($sql);
  195. $this->learnpath_data=Database::fetch_array($result);
  196. }
  197. return $this->learnpath_data;
  198. }
  199. public function get_icon_name() {
  200. return 'learnpath';
  201. }
  202. }