learnpathList.class.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. * @param int $categoryId
  33. * @param bool $ignoreCategoryFilter
  34. *
  35. * @return void
  36. */
  37. public function __construct(
  38. $user_id,
  39. $course_code = '',
  40. $session_id = null,
  41. $order_by = null,
  42. $check_publication_dates = false,
  43. $categoryId = null,
  44. $ignoreCategoryFilter = false
  45. ) {
  46. $course_info = api_get_course_info($course_code);
  47. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  48. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  49. $this->course_code = $course_code;
  50. $this->user_id = $user_id;
  51. $course_id = $course_info['real_id'];
  52. if (empty($course_id)) {
  53. return false;
  54. }
  55. // Condition for the session.
  56. if (isset($session_id)) {
  57. $session_id = intval($session_id);
  58. } else {
  59. $session_id = api_get_session_id();
  60. }
  61. $condition_session = api_get_session_condition($session_id, true, true);
  62. $order = "ORDER BY display_order ASC, name ASC";
  63. if (isset($order_by)) {
  64. $order = Database::parse_conditions(array('order' => $order_by));
  65. }
  66. $now = api_get_utc_datetime();
  67. $time_conditions = '';
  68. if ($check_publication_dates) {
  69. $time_conditions = " AND (
  70. (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
  71. (publicated_on <> '0000-00-00 00:00:00' AND publicated_on < '$now' AND expired_on = '0000-00-00 00:00:00') OR
  72. (publicated_on = '0000-00-00 00:00:00' AND expired_on <> '0000-00-00 00:00:00' AND expired_on > '$now') OR
  73. (publicated_on = '0000-00-00 00:00:00' AND expired_on = '0000-00-00 00:00:00' ))
  74. ";
  75. }
  76. $categoryFilter = '';
  77. if ($ignoreCategoryFilter == false) {
  78. if (!empty($categoryId)) {
  79. $categoryId = intval($categoryId);
  80. $categoryFilter = " AND category_id = $categoryId";
  81. } else {
  82. $categoryFilter = " AND (category_id = 0 OR category_id IS NULL) ";
  83. }
  84. }
  85. $sql = "SELECT * FROM $lp_table
  86. WHERE
  87. c_id = $course_id
  88. $time_conditions
  89. $condition_session
  90. $categoryFilter
  91. $order
  92. ";
  93. $res = Database::query($sql);
  94. $names = array();
  95. while ($row = Database::fetch_array($res,'ASSOC')) {
  96. // Use domesticate here instead of Database::escape_string because
  97. // it prevents ' to be slashed and the input (done by learnpath.class.php::toggle_visibility())
  98. // is done using domesticate()
  99. $myname = domesticate($row['name']);
  100. $mylink = 'newscorm/lp_controller.php?action=view&lp_id='.$row['id'].'&id_session='.$session_id;
  101. $sql2 = "SELECT * FROM $tbl_tool
  102. WHERE
  103. c_id = $course_id AND (
  104. name='$myname' AND
  105. image='scormbuilder.gif' AND
  106. link LIKE '$mylink%'
  107. )";
  108. $res2 = Database::query($sql2);
  109. if (Database::num_rows($res2) > 0) {
  110. $row2 = Database::fetch_array($res2);
  111. $pub = $row2['visibility'];
  112. } else {
  113. $pub = 'i';
  114. }
  115. // Check if visible.
  116. $vis = api_get_item_visibility(
  117. api_get_course_info($course_code),
  118. 'learnpath',
  119. $row['id'],
  120. $session_id
  121. );
  122. if (!empty($row['created_on']) && $row['created_on'] != '0000-00-00 00:00:00') {
  123. $row['created_on'] = $row['created_on'];
  124. } else {
  125. $row['created_on'] = '';
  126. }
  127. if (!empty($row['modified_on']) && $row['modified_on'] != '0000-00-00 00:00:00') {
  128. $row['modified_on'] = $row['modified_on'];
  129. } else {
  130. $row['modified_on'] = '';
  131. }
  132. if (!empty($row['publicated_on']) && $row['publicated_on'] != '0000-00-00 00:00:00') {
  133. $row['publicated_on'] = $row['publicated_on'];
  134. } else {
  135. $row['publicated_on'] = '';
  136. }
  137. if (!empty($row['expired_on']) && $row['expired_on'] != '0000-00-00 00:00:00') {
  138. $row['expired_on'] = $row['expired_on'];
  139. } else {
  140. $row['expired_on'] = '';
  141. }
  142. $this->list[$row['id']] = array(
  143. 'lp_type' => $row['lp_type'],
  144. 'lp_session' => $row['session_id'],
  145. 'lp_name' => stripslashes($row['name']),
  146. 'lp_desc' => stripslashes($row['description']),
  147. 'lp_path' => $row['path'],
  148. 'lp_view_mode' => $row['default_view_mod'],
  149. 'lp_force_commit' => $row['force_commit'],
  150. 'lp_maker' => stripslashes($row['content_maker']),
  151. 'lp_proximity' => $row['content_local'],
  152. 'lp_encoding' => api_get_system_encoding(),
  153. 'lp_visibility' => $vis,
  154. 'lp_published' => $pub,
  155. 'lp_prevent_reinit' => $row['prevent_reinit'],
  156. 'seriousgame_mode' => $row['seriousgame_mode'],
  157. 'lp_scorm_debug' => $row['debug'],
  158. 'lp_display_order' => $row['display_order'],
  159. 'lp_preview_image' => stripslashes($row['preview_image']),
  160. 'autolaunch' => $row['autolaunch'],
  161. 'session_id' => $row['session_id'],
  162. 'created_on' => $row['created_on'],
  163. 'modified_on' => $row['modified_on'],
  164. 'publicated_on' => $row['publicated_on'],
  165. 'expired_on' => $row['expired_on'],
  166. //'category_id' => $row['category_id'],
  167. 'subscribe_users' => $row['subscribe_users'],
  168. );
  169. $names[$row['name']] = $row['id'];
  170. }
  171. $this->alpha_list = asort($names);
  172. }
  173. /**
  174. * Gets references to learnpaths for all learnpaths IDs kept in the local list.
  175. * This applies a transformation internally on list and ref_list and returns a copy of the refs list
  176. * @return array List of references to learnpath objects
  177. */
  178. public function get_refs()
  179. {
  180. foreach ($this->list as $id => $dummy) {
  181. $this->ref_list[$id] = new learnpath($this->course_code, $id, $this->user_id);
  182. }
  183. $this->refs_active = true;
  184. return $this->ref_list;
  185. }
  186. /**
  187. * Gets a table of the different learnpaths we have at the moment
  188. * @return array Learnpath info as [lp_id] => ([lp_type]=> ..., [lp_name]=>...,[lp_desc]=>...,[lp_path]=>...)
  189. */
  190. public function get_flat_list()
  191. {
  192. return $this->list;
  193. }
  194. /**
  195. * Gets a list of lessons of the given course_code and session_id
  196. * This functions doesn't need user_id
  197. * @param string $course_code Text code of the course
  198. * @param int $session_id Id of session
  199. * @return array List of lessons with lessons id as keys
  200. */
  201. public static function get_course_lessons($course_code, $session_id)
  202. {
  203. $table = Database::get_course_table(TABLE_LP_MAIN);
  204. $course = api_get_course_info($course_code);
  205. // @todo AND session_id = %s ?
  206. $sql = "SELECT * FROM $table WHERE c_id = %s ";
  207. $sql_query = sprintf($sql, $course['real_id']);
  208. $result = Database::query($sql_query);
  209. $lessons = array();
  210. while ($row = Database::fetch_array($result)) {
  211. if (api_get_item_visibility($course, 'learnpath', $row['id'], $session_id)) {
  212. $lessons[$row['id']] = $row;
  213. }
  214. }
  215. return $lessons;
  216. }
  217. }