learnpathList.class.php 9.1 KB

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