learnpathList.class.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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()->createQuery($dql)->getResult();
  99. $showBlockedPrerequisite = api_get_configuration_value('show_prerequisite_as_blocked');
  100. $names = [];
  101. /** @var CLp $row */
  102. foreach ($learningPaths as $row) {
  103. // Use domesticate here instead of Database::escape_string because
  104. // it prevents ' to be slashed and the input (done by learnpath.class.php::toggle_visibility())
  105. // is done using domesticate()
  106. $name = domesticate($row->getName());
  107. $link = 'lp/lp_controller.php?action=view&lp_id=' . $row->getId() . '&id_session='.$session_id;
  108. $oldLink = 'newscorm/lp_controller.php?action=view&lp_id=' . $row->getId() . '&id_session='.$session_id;
  109. $sql2 = "SELECT * FROM $tbl_tool
  110. WHERE
  111. c_id = $course_id AND
  112. name = '$name' AND
  113. image = 'scormbuilder.gif' AND
  114. (
  115. link LIKE '$link%' OR
  116. link LIKE '$oldLink%'
  117. )
  118. ";
  119. $res2 = Database::query($sql2);
  120. if (Database::num_rows($res2) > 0) {
  121. $row2 = Database::fetch_array($res2);
  122. $pub = $row2['visibility'];
  123. } else {
  124. $pub = 'i';
  125. }
  126. // Check if visible.
  127. $visibility = api_get_item_visibility(
  128. api_get_course_info($course_code),
  129. 'learnpath',
  130. $row->getId(),
  131. $session_id
  132. );
  133. // If option is not true then don't show invisible LP to user
  134. if ($showBlockedPrerequisite !== true && !api_is_allowed_to_edit()) {
  135. $lpVisibility = learnpath::is_lp_visible_for_student(
  136. $row->getId(),
  137. $user_id,
  138. $course_code
  139. );
  140. if ($lpVisibility === false) {
  141. continue;
  142. }
  143. }
  144. $this->list[$row->getIid()] = array(
  145. 'lp_type' => $row->getLpType(),
  146. 'lp_session' => $row->getSessionId(),
  147. 'lp_name' => stripslashes($row->getName()),
  148. 'lp_desc' => stripslashes($row->getDescription()),
  149. 'lp_path' => $row->getPath(),
  150. 'lp_view_mode' => $row->getDefaultViewMod(),
  151. 'lp_force_commit' => $row->getForceCommit(),
  152. 'lp_maker' => stripslashes($row->getContentMaker()),
  153. 'lp_proximity' => $row->getContentLocal(),
  154. 'lp_encoding' => api_get_system_encoding(),
  155. 'lp_visibility' => $visibility,
  156. 'lp_published' => $pub,
  157. 'lp_prevent_reinit' => $row->getPreventReinit(),
  158. 'seriousgame_mode' => $row->getSeriousgameMode(),
  159. 'lp_scorm_debug' => $row->getDebug(),
  160. 'lp_display_order' => $row->getDisplayOrder(),
  161. 'lp_preview_image' => stripslashes($row->getPreviewImage()),
  162. 'autolaunch' => $row->getAutolaunch(),
  163. 'session_id' => $row->getSessionId(),
  164. 'created_on' => $row->getCreatedOn() ? $row->getCreatedOn()->format('Y-m-d H:i:s') : null,
  165. 'modified_on' => $row->getModifiedOn() ? $row->getModifiedOn()->format('Y-m-d H:i:s') : null,
  166. 'publicated_on' => $row->getPublicatedOn() ? $row->getPublicatedOn()->format('Y-m-d H:i:s') : null,
  167. 'expired_on' => $row->getExpiredOn() ? $row->getExpiredOn()->format('Y-m-d H:i:s') : null,
  168. //'category_id' => $row['category_id'],
  169. 'subscribe_users' => $row->getSubscribeUsers(),
  170. 'lp_old_id' => $row->getId(),
  171. 'iid' => $row->getIid(),
  172. 'prerequisite' => $row->getPrerequisite()
  173. );
  174. $names[$row->getName()] = $row->getIid();
  175. }
  176. $this->alpha_list = asort($names);
  177. }
  178. /**
  179. * Gets references to learnpaths for all learnpaths IDs kept in the local list.
  180. * This applies a transformation internally on list and ref_list and returns a copy of the refs list
  181. * @return array List of references to learnpath objects
  182. */
  183. public function get_refs()
  184. {
  185. foreach ($this->list as $id => $dummy) {
  186. $this->ref_list[$id] = new learnpath($this->course_code, $id, $this->user_id);
  187. }
  188. $this->refs_active = true;
  189. return $this->ref_list;
  190. }
  191. /**
  192. * Gets a table of the different learnpaths we have at the moment
  193. * @return array Learnpath info as [lp_id] => ([lp_type]=> ..., [lp_name]=>...,[lp_desc]=>...,[lp_path]=>...)
  194. */
  195. public function get_flat_list()
  196. {
  197. return $this->list;
  198. }
  199. /**
  200. * Gets a list of lessons of the given course_code and session_id
  201. * This functions doesn't need user_id
  202. * @param string $course_code Text code of the course
  203. * @param int $session_id Id of session
  204. * @return array List of lessons with lessons id as keys
  205. */
  206. public static function get_course_lessons($course_code, $session_id)
  207. {
  208. $table = Database::get_course_table(TABLE_LP_MAIN);
  209. $course = api_get_course_info($course_code);
  210. // @todo AND session_id = %s ?
  211. $sql = "SELECT * FROM $table WHERE c_id = %s ";
  212. $sql_query = sprintf($sql, $course['real_id']);
  213. $result = Database::query($sql_query);
  214. $lessons = array();
  215. while ($row = Database::fetch_array($result)) {
  216. if (api_get_item_visibility($course, 'learnpath', $row['id'], $session_id)) {
  217. $lessons[$row['id']] = $row;
  218. }
  219. }
  220. return $lessons;
  221. }
  222. }