learnpathList.class.php 9.6 KB

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