learnpathList.class.php 8.8 KB

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