learnpathList.class.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class learnpathList
  5. * This class is only a learning path list container with several practical methods for sorting the list and
  6. * provide links to specific paths
  7. * @uses Database.lib.php to use the database
  8. * @uses learnpath.class.php to generate learnpath objects to get in the list
  9. * @author Yannick Warnier <ywarnier@beeznest.org>
  10. *
  11. */
  12. class learnpathList
  13. {
  14. // Holds a flat list of learnpaths data from the database.
  15. public $list = array();
  16. // Holds a list of references to the learnpaths objects (only filled by get_refs()).
  17. public $ref_list = array();
  18. // Holds a flat list of learnpaths sorted by alphabetical name order.
  19. public $alpha_list = array();
  20. public $course_code;
  21. public $user_id;
  22. public $refs_active = false;
  23. /**
  24. * This method is the constructor for the learnpathList. It gets a list of available learning paths from
  25. * the database and creates the learnpath objects. This list depends on the user that is connected
  26. * (only displays) items if he has enough permissions to view them.
  27. * @param integer $user_id
  28. * @param string $course_code Optional course code (otherwise we use api_get_course_id())
  29. * @param int $session_id Optional session id (otherwise we use api_get_session_id())
  30. * @param string $order_by
  31. * @param string $check_publication_dates
  32. * @return void
  33. */
  34. public function __construct($user_id, $course_code = '', $session_id = null, $order_by = null, $check_publication_dates = false)
  35. {
  36. $course_info = api_get_course_info($course_code);
  37. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  38. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  39. $this->course_code = $course_code;
  40. $this->user_id = $user_id;
  41. $course_id = $course_info['real_id'];
  42. if (empty($course_id)) {
  43. return false;
  44. }
  45. // Condition for the session.
  46. if (isset($session_id)) {
  47. $session_id = intval($session_id);
  48. } else {
  49. $session_id = api_get_session_id();
  50. }
  51. $condition_session = api_get_session_condition($session_id, true, true);
  52. $order = "ORDER BY display_order ASC, name ASC";
  53. if (isset($order_by)) {
  54. $order = Database::parse_conditions(array('order'=>$order_by));
  55. }
  56. $now = api_get_utc_datetime();
  57. $time_conditions = '';
  58. if ($check_publication_dates) {
  59. $time_conditions = " AND (
  60. (publicated_on <> '0000-00-00 00:00:00' AND publicated_on < '$now' AND expired_on <> '0000-00-00 00:00:00' AND expired_on > '$now' ) OR
  61. (publicated_on <> '0000-00-00 00:00:00' AND publicated_on < '$now' AND expired_on = '0000-00-00 00:00:00') OR
  62. (publicated_on = '0000-00-00 00:00:00' AND expired_on <> '0000-00-00 00:00:00' AND expired_on > '$now') OR
  63. (publicated_on = '0000-00-00 00:00:00' AND expired_on = '0000-00-00 00:00:00' ))
  64. ";
  65. }
  66. $sql = "SELECT * FROM $lp_table
  67. WHERE c_id = $course_id $time_conditions $condition_session $order";
  68. $res = Database::query($sql);
  69. $names = array();
  70. while ($row = Database::fetch_array($res,'ASSOC')) {
  71. // Use domesticate here instead of Database::escape_string because
  72. // it prevents ' to be slashed and the input (done by learnpath.class.php::toggle_visibility())
  73. // is done using domesticate()
  74. $myname = domesticate($row['name']);
  75. $mylink = 'newscorm/lp_controller.php?action=view&lp_id='.$row['id'].'&id_session='.$session_id;
  76. $sql2 = "SELECT * FROM $tbl_tool
  77. WHERE
  78. c_id = $course_id AND (
  79. name='$myname' AND
  80. image='scormbuilder.gif' AND
  81. link LIKE '$mylink%'
  82. )";
  83. $res2 = Database::query($sql2);
  84. if (Database::num_rows($res2) > 0) {
  85. $row2 = Database::fetch_array($res2);
  86. $pub = $row2['visibility'];
  87. } else {
  88. $pub = 'i';
  89. }
  90. // Check if visible.
  91. $vis = api_get_item_visibility(api_get_course_info($course_code), 'learnpath', $row['id'], $session_id);
  92. if (!empty($row['created_on']) && $row['created_on'] != '0000-00-00 00:00:00') {
  93. $row['created_on'] = $row['created_on'];
  94. } else {
  95. $row['created_on'] = '';
  96. }
  97. if (!empty($row['modified_on']) && $row['modified_on'] != '0000-00-00 00:00:00') {
  98. $row['modified_on'] = $row['modified_on'];
  99. } else {
  100. $row['modified_on'] = '';
  101. }
  102. if (!empty($row['publicated_on']) && $row['publicated_on'] != '0000-00-00 00:00:00') {
  103. $row['publicated_on'] = $row['publicated_on'];
  104. } else {
  105. $row['publicated_on'] = '';
  106. }
  107. if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
  108. $row['expired_on'] = $row['expired_on'];
  109. } else {
  110. $row['expired_on'] = '';
  111. }
  112. $this->list[$row['id']] = array(
  113. 'lp_type' => $row['lp_type'],
  114. 'lp_session' => $row['session_id'],
  115. 'lp_name' => stripslashes($row['name']),
  116. 'lp_desc' => stripslashes($row['description']),
  117. 'lp_path' => $row['path'],
  118. 'lp_view_mode' => $row['default_view_mod'],
  119. 'lp_force_commit' => $row['force_commit'],
  120. 'lp_maker' => stripslashes($row['content_maker']),
  121. 'lp_proximity' => $row['content_local'],
  122. 'lp_encoding' => api_get_system_encoding(),
  123. 'lp_visibility' => $vis,
  124. 'lp_published' => $pub,
  125. 'lp_prevent_reinit' => $row['prevent_reinit'],
  126. 'seriousgame_mode' => $row['seriousgame_mode'],
  127. 'lp_scorm_debug' => $row['debug'],
  128. 'lp_display_order' => $row['display_order'],
  129. 'lp_preview_image' => stripslashes($row['preview_image']),
  130. 'autolaunch' => $row['autolunch'],
  131. 'session_id' => $row['session_id'],
  132. 'created_on' => $row['created_on'],
  133. 'modified_on' => $row['modified_on'],
  134. 'publicated_on' => $row['publicated_on'],
  135. 'expired_on' => $row['expired_on']
  136. );
  137. $names[$row['name']] = $row['id'];
  138. }
  139. $this->alpha_list = asort($names);
  140. }
  141. /**
  142. * Gets references to learnpaths for all learnpaths IDs kept in the local list.
  143. * This applies a transformation internally on list and ref_list and returns a copy of the refs list
  144. * @return array List of references to learnpath objects
  145. */
  146. function get_refs()
  147. {
  148. foreach ($this->list as $id => $dummy) {
  149. $this->ref_list[$id] = new learnpath($this->course_code, $id, $this->user_id);
  150. }
  151. $this->refs_active = true;
  152. return $this->ref_list;
  153. }
  154. /**
  155. * Gets a table of the different learnpaths we have at the moment
  156. * @return array Learnpath info as [lp_id] => ([lp_type]=> ..., [lp_name]=>...,[lp_desc]=>...,[lp_path]=>...)
  157. */
  158. function get_flat_list()
  159. {
  160. return $this->list;
  161. }
  162. /**
  163. * Gets a list of lessons of the given course_code and session_id
  164. * This functions doesn't need user_id
  165. * @param string $course_code Text code of the course
  166. * @param int $session_id Id of session
  167. * @return array List of lessons with lessons id as keys
  168. */
  169. static function get_course_lessons($course_code, $session_id)
  170. {
  171. $tbl_course_lp = Database::get_course_table(TABLE_LP_MAIN);
  172. $course = api_get_course_info($course_code);
  173. //QUery
  174. $sql = "SELECT * FROM $tbl_course_lp
  175. WHERE c_id = %s "; //TODO AND session_id = %s ?
  176. $sql_query = sprintf($sql, $course['real_id']);
  177. $result = Database::query($sql_query);
  178. $lessons = array();
  179. while ($row = Database::fetch_array($result))
  180. {
  181. if (api_get_item_visibility($course, 'learnpath', $row['id'], $session_id))
  182. {
  183. $lessons[$row['id']] = $row;
  184. }
  185. }
  186. return $lessons;
  187. }
  188. }