learnpathList.class.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. $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(
  62. $session_id,
  63. true,
  64. true,
  65. 'lp.sessionId'
  66. );
  67. $order = "ORDER BY lp.displayOrder ASC, lp.name ASC";
  68. if (isset($order_by)) {
  69. $order = Database::parse_conditions(array('order' => $order_by));
  70. }
  71. $now = api_get_utc_datetime();
  72. $time_conditions = '';
  73. if ($check_publication_dates) {
  74. $time_conditions = " AND (
  75. (lp.publicatedOn IS NOT NULL AND lp.publicatedOn < '$now' AND lp.expiredOn IS NOT NULL AND lp.expiredOn > '$now') OR
  76. (lp.publicatedOn IS NOT NULL AND lp.publicatedOn < '$now' AND lp.expiredOn IS NULL) OR
  77. (lp.publicatedOn IS NULL AND lp.expiredOn IS NOT NULL AND lp.expiredOn > '$now') OR
  78. (lp.publicatedOn IS NULL AND lp.expiredOn IS NULL ))
  79. ";
  80. }
  81. $categoryFilter = '';
  82. if ($ignoreCategoryFilter == false) {
  83. if (!empty($categoryId)) {
  84. $categoryId = intval($categoryId);
  85. $categoryFilter = " AND lp.categoryId = $categoryId";
  86. } else {
  87. $categoryFilter = " AND (lp.categoryId = 0 OR lp.categoryId IS NULL) ";
  88. }
  89. }
  90. $dql = "SELECT lp FROM ChamiloCourseBundle:CLp as lp
  91. WHERE
  92. lp.cId = $course_id
  93. $time_conditions
  94. $condition_session
  95. $categoryFilter
  96. $order
  97. ";
  98. $learningPaths = Database::getManager()
  99. ->createQuery($dql)
  100. ->getResult();
  101. $names = [];
  102. /** @var CLp $row */
  103. foreach ($learningPaths as $row) {
  104. // Use domesticate here instead of Database::escape_string because
  105. // it prevents ' to be slashed and the input (done by learnpath.class.php::toggle_visibility())
  106. // is done using domesticate()
  107. $name = domesticate($row->getName());
  108. $link = 'lp/lp_controller.php?action=view&lp_id=' . $row->getId() . '&id_session='.$session_id;
  109. $oldLink = 'newscorm/lp_controller.php?action=view&lp_id=' . $row->getId() . '&id_session='.$session_id;
  110. $sql2 = "SELECT * FROM $tbl_tool
  111. WHERE
  112. c_id = $course_id AND
  113. name = '$name' AND
  114. image = 'scormbuilder.gif' AND
  115. (
  116. link LIKE '$link%' OR
  117. link LIKE '$oldLink%'
  118. )
  119. ";
  120. $res2 = Database::query($sql2);
  121. if (Database::num_rows($res2) > 0) {
  122. $row2 = Database::fetch_array($res2);
  123. $pub = $row2['visibility'];
  124. } else {
  125. $pub = 'i';
  126. }
  127. // Check if visible.
  128. $vis = api_get_item_visibility(
  129. api_get_course_info($course_code),
  130. 'learnpath',
  131. $row->getId(),
  132. $session_id
  133. );
  134. $this->list[$row->getIid()] = array(
  135. 'lp_type' => $row->getLpType(),
  136. 'lp_session' => $row->getSessionId(),
  137. 'lp_name' => stripslashes($row->getName()),
  138. 'lp_desc' => stripslashes($row->getDescription()),
  139. 'lp_path' => $row->getPath(),
  140. 'lp_view_mode' => $row->getDefaultViewMod(),
  141. 'lp_force_commit' => $row->getForceCommit(),
  142. 'lp_maker' => stripslashes($row->getContentMaker()),
  143. 'lp_proximity' => $row->getContentLocal(),
  144. 'lp_encoding' => api_get_system_encoding(),
  145. 'lp_visibility' => $vis,
  146. 'lp_published' => $pub,
  147. 'lp_prevent_reinit' => $row->getPreventReinit(),
  148. 'seriousgame_mode' => $row->getSeriousgameMode(),
  149. 'lp_scorm_debug' => $row->getDebug(),
  150. 'lp_display_order' => $row->getDisplayOrder(),
  151. 'lp_preview_image' => stripslashes($row->getPreviewImage()),
  152. 'autolaunch' => $row->getAutolaunch(),
  153. 'session_id' => $row->getSessionId(),
  154. 'created_on' => $row->getCreatedOn() ? $row->getCreatedOn()->format('Y-m-d H:i:s') : null,
  155. 'modified_on' => $row->getModifiedOn() ? $row->getModifiedOn()->format('Y-m-d H:i:s') : null,
  156. 'publicated_on' => $row->getPublicatedOn() ? $row->getPublicatedOn()->format('Y-m-d H:i:s') : null,
  157. 'expired_on' => $row->getExpiredOn() ? $row->getExpiredOn()->format('Y-m-d H:i:s') : null,
  158. //'category_id' => $row['category_id'],
  159. 'subscribe_users' => $row->getSubscribeUsers(),
  160. 'lp_old_id' => $row->getId(),
  161. 'iid' => $row->getIid(),
  162. 'prerequisite' => $row->getPrerequisite()
  163. );
  164. $names[$row->getName()] = $row->getIid();
  165. }
  166. $this->alpha_list = asort($names);
  167. }
  168. /**
  169. * Gets references to learnpaths for all learnpaths IDs kept in the local list.
  170. * This applies a transformation internally on list and ref_list and returns a copy of the refs list
  171. * @return array List of references to learnpath objects
  172. */
  173. public function get_refs()
  174. {
  175. foreach ($this->list as $id => $dummy) {
  176. $this->ref_list[$id] = new learnpath($this->course_code, $id, $this->user_id);
  177. }
  178. $this->refs_active = true;
  179. return $this->ref_list;
  180. }
  181. /**
  182. * Gets a table of the different learnpaths we have at the moment
  183. * @return array Learnpath info as [lp_id] => ([lp_type]=> ..., [lp_name]=>...,[lp_desc]=>...,[lp_path]=>...)
  184. */
  185. public function get_flat_list()
  186. {
  187. return $this->list;
  188. }
  189. /**
  190. * Gets a list of lessons of the given course_code and session_id
  191. * This functions doesn't need user_id
  192. * @param string $course_code Text code of the course
  193. * @param int $session_id Id of session
  194. * @return array List of lessons with lessons id as keys
  195. */
  196. public static function get_course_lessons($course_code, $session_id)
  197. {
  198. $table = Database::get_course_table(TABLE_LP_MAIN);
  199. $course = api_get_course_info($course_code);
  200. // @todo AND session_id = %s ?
  201. $sql = "SELECT * FROM $table WHERE c_id = %s ";
  202. $sql_query = sprintf($sql, $course['real_id']);
  203. $result = Database::query($sql_query);
  204. $lessons = array();
  205. while ($row = Database::fetch_array($result)) {
  206. if (api_get_item_visibility($course, 'learnpath', $row['id'], $session_id)) {
  207. $lessons[$row['id']] = $row;
  208. }
  209. }
  210. return $lessons;
  211. }
  212. }