learnpathList.class.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 string $course_code 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. $course_code = '',
  39. $session_id = 0,
  40. $order_by = null,
  41. $check_publication_dates = false,
  42. $categoryId = null,
  43. $ignoreCategoryFilter = false,
  44. $ignoreLpVisibility = false
  45. ) {
  46. $course_info = api_get_course_info($course_code);
  47. if (empty($course_info['real_id'])) {
  48. return false;
  49. }
  50. $this->course_code = $course_info['code'];
  51. $this->user_id = $user_id;
  52. $course_id = $course_info['real_id'];
  53. // Condition for the session.
  54. if (!empty($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. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  66. $order = "ORDER BY lp.displayOrder ASC, lp.name ASC";
  67. if (isset($order_by)) {
  68. $order = Database::parse_conditions(['order' => $order_by]);
  69. }
  70. $now = api_get_utc_datetime();
  71. $time_conditions = '';
  72. if ($check_publication_dates) {
  73. $time_conditions = " AND (
  74. (lp.publicatedOn IS NOT NULL AND lp.publicatedOn < '$now' AND lp.expiredOn IS NOT NULL AND lp.expiredOn > '$now') OR
  75. (lp.publicatedOn IS NOT NULL AND lp.publicatedOn < '$now' AND lp.expiredOn IS NULL) OR
  76. (lp.publicatedOn IS NULL AND lp.expiredOn IS NOT NULL AND lp.expiredOn > '$now') OR
  77. (lp.publicatedOn IS NULL AND lp.expiredOn IS NULL ))
  78. ";
  79. }
  80. $categoryFilter = '';
  81. if ($ignoreCategoryFilter == false) {
  82. if (!empty($categoryId)) {
  83. $categoryId = (int) $categoryId;
  84. $categoryFilter = " AND lp.categoryId = $categoryId";
  85. } else {
  86. $categoryFilter = " AND (lp.categoryId = 0 OR lp.categoryId IS NULL) ";
  87. }
  88. }
  89. $dql = "SELECT lp FROM ChamiloCourseBundle:CLp as lp
  90. WHERE
  91. lp.cId = $course_id
  92. $time_conditions
  93. $condition_session
  94. $categoryFilter
  95. $order
  96. ";
  97. $learningPaths = Database::getManager()->createQuery($dql)->getResult();
  98. $showBlockedPrerequisite = api_get_configuration_value('show_prerequisite_as_blocked');
  99. $names = [];
  100. /** @var CLp $row */
  101. foreach ($learningPaths as $row) {
  102. // Use domesticate here instead of Database::escape_string because
  103. // it prevents ' to be slashed and the input (done by learnpath.class.php::toggle_visibility())
  104. // is done using domesticate()
  105. $name = domesticate($row->getName());
  106. $link = 'lp/lp_controller.php?action=view&lp_id='.$row->getId().'&id_session='.$session_id;
  107. $oldLink = 'newscorm/lp_controller.php?action=view&lp_id='.$row->getId().'&id_session='.$session_id;
  108. $sql2 = "SELECT * FROM $tbl_tool
  109. WHERE
  110. c_id = $course_id AND
  111. name = '$name' AND
  112. image = 'scormbuilder.gif' AND
  113. (
  114. link LIKE '$link%' OR
  115. link LIKE '$oldLink%'
  116. )
  117. ";
  118. $res2 = Database::query($sql2);
  119. $pub = 'i';
  120. if (Database::num_rows($res2) > 0) {
  121. $row2 = Database::fetch_array($res2);
  122. $pub = $row2['visibility'];
  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 ($ignoreLpVisibility === false) {
  133. if ($showBlockedPrerequisite !== true && !api_is_allowed_to_edit()) {
  134. $lpVisibility = learnpath::is_lp_visible_for_student(
  135. $row->getId(),
  136. $user_id,
  137. $course_code
  138. );
  139. if ($lpVisibility === false) {
  140. continue;
  141. }
  142. }
  143. }
  144. $this->list[$row->getIid()] = [
  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. asort($names);
  177. $this->alpha_list = $names;
  178. }
  179. /**
  180. * Gets a table of the different learnpaths we have at the moment.
  181. *
  182. * @return array Learnpath info as [lp_id] => ([lp_type]=> ..., [lp_name]=>...,[lp_desc]=>...,[lp_path]=>...)
  183. */
  184. public function get_flat_list()
  185. {
  186. return $this->list;
  187. }
  188. /**
  189. * Gets a list of lessons of the given course_code and session_id
  190. * This functions doesn't need user_id.
  191. *
  192. * @param string $course_code Text code of the course
  193. * @param int $session_id Id of session
  194. *
  195. * @return array List of lessons with lessons id as keys
  196. */
  197. public static function get_course_lessons($course_code, $session_id)
  198. {
  199. $table = Database::get_course_table(TABLE_LP_MAIN);
  200. $course = api_get_course_info($course_code);
  201. // @todo AND session_id = %s ?
  202. $sql = "SELECT * FROM $table WHERE c_id = %s ";
  203. $sql_query = sprintf($sql, $course['real_id']);
  204. $result = Database::query($sql_query);
  205. $lessons = [];
  206. while ($row = Database::fetch_array($result)) {
  207. if (api_get_item_visibility($course, 'learnpath', $row['id'], $session_id)) {
  208. $lessons[$row['id']] = $row;
  209. }
  210. }
  211. return $lessons;
  212. }
  213. }