courses.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\SequenceResource;
  4. /**
  5. * Template (front controller in MVC pattern) used for distpaching
  6. * to the controllers depend on the current action
  7. * @author Christian Fasanando <christian1827@gmail.com> - Beeznest
  8. * @package chamilo.auth
  9. */
  10. // Delete the globals['_cid'], we don't need it here.
  11. $cidReset = true; // Flag forcing the 'current course' reset
  12. require_once __DIR__.'/../inc/global.inc.php';
  13. $ctok = Security::get_existing_token();
  14. // Get Limit data
  15. $limit = CourseCategory::getLimitArray();
  16. // Section for the tabs.
  17. $this_section = SECTION_CATALOG;
  18. if (api_get_setting('course_catalog_published') !== 'true') {
  19. // Access rights: anonymous users can't do anything useful here.
  20. api_block_anonymous_users();
  21. }
  22. $user_can_view_page = false;
  23. //For students
  24. if (api_get_setting('allow_students_to_browse_courses') === 'false') {
  25. $user_can_view_page = false;
  26. } else {
  27. $user_can_view_page = true;
  28. }
  29. //For teachers/admins
  30. if (api_is_platform_admin() || api_is_course_admin() || api_is_allowed_to_create_course()) {
  31. $user_can_view_page = true;
  32. }
  33. // filter actions
  34. $actions = array(
  35. 'sortmycourses',
  36. 'createcoursecategory',
  37. 'subscribe',
  38. 'deletecoursecategory',
  39. 'display_courses',
  40. 'display_random_courses',
  41. 'subscribe_user_with_password',
  42. 'display_sessions',
  43. 'subscribe_to_session',
  44. 'search_tag',
  45. 'search_session'
  46. );
  47. $action = CoursesAndSessionsCatalog::is(CATALOG_SESSIONS) ? 'display_sessions' : 'display_courses';
  48. if (isset($_GET['action']) && in_array($_GET['action'], $actions)) {
  49. $action = Security::remove_XSS($_GET['action']);
  50. }
  51. $categoryCode = isset($_GET['category_code']) && !empty($_GET['category_code']) ? $_GET['category_code'] : 'ALL';
  52. $nameTools = CourseCategory::getCourseCatalogNameTools($action);
  53. if (empty($nameTools)) {
  54. $nameTools = get_lang('CourseManagement');
  55. } else {
  56. if (!in_array($action, array('sortmycourses', 'createcoursecategory', 'display_random_courses', 'display_courses', 'subscribe'))) {
  57. $interbreadcrumb[] = array(
  58. 'url' => api_get_path(WEB_CODE_PATH).'auth/courses.php',
  59. 'name' => get_lang('CourseManagement'),
  60. );
  61. }
  62. if ($action === 'createcoursecategory') {
  63. $interbreadcrumb[] = array(
  64. 'url' => api_get_path(WEB_CODE_PATH).'auth/courses.php?action=sortmycourses',
  65. 'name' => get_lang('SortMyCourses'),
  66. );
  67. }
  68. $interbreadcrumb[] = array('url' => '#', 'name' => $nameTools);
  69. }
  70. // course description controller object
  71. $courses_controller = new CoursesController();
  72. // We are moving a course or category of the user up/down the list (=Sort My Courses).
  73. if (isset($_GET['move'])) {
  74. if (isset($_GET['course'])) {
  75. $courses_controller->move_course(
  76. $_GET['move'],
  77. $_GET['course'],
  78. $_GET['category']
  79. );
  80. }
  81. if (isset($_GET['category']) && !isset($_GET['course'])) {
  82. $courses_controller->move_category($_GET['move'], $_GET['category']);
  83. }
  84. }
  85. // We are moving the course of the user to a different user defined course category (=Sort My Courses).
  86. if (isset($_POST['submit_change_course_category'])) {
  87. if ($ctok == $_POST['sec_token']) {
  88. $courses_controller->change_course_category(
  89. $_POST['course_2_edit_category'],
  90. $_POST['course_categories']
  91. );
  92. }
  93. }
  94. // We edit course category
  95. if (isset($_POST['submit_edit_course_category']) &&
  96. isset($_POST['title_course_category']) &&
  97. strlen(trim($_POST['title_course_category'])) > 0
  98. ) {
  99. if ($ctok == $_POST['sec_token']) {
  100. $courses_controller->edit_course_category(
  101. $_POST['title_course_category'],
  102. $_POST['edit_course_category']
  103. );
  104. }
  105. }
  106. // we are deleting a course category
  107. if ($action == 'deletecoursecategory' && isset($_GET['id'])) {
  108. if ($ctok == $_GET['sec_token']) {
  109. $get_id_cat = intval($_GET['id']);
  110. $courses_controller->delete_course_category($get_id_cat);
  111. }
  112. }
  113. // We are creating a new user defined course category (= Create Course Category).
  114. if (isset($_POST['create_course_category']) &&
  115. isset($_POST['title_course_category']) &&
  116. strlen(trim($_POST['title_course_category'])) > 0
  117. ) {
  118. if ($ctok == $_POST['sec_token']) {
  119. $courses_controller->add_course_category($_POST['title_course_category']);
  120. }
  121. }
  122. // search courses
  123. if (isset($_REQUEST['search_course'])) {
  124. if ($ctok == $_REQUEST['sec_token']) {
  125. $courses_controller->search_courses($_REQUEST['search_term'], null, null, null, $limit, true);
  126. }
  127. }
  128. // Subscribe user to course
  129. if (isset($_REQUEST['subscribe_course'])) {
  130. if ($ctok == $_GET['sec_token']) {
  131. $courses_controller->subscribe_user(
  132. $_GET['subscribe_course'],
  133. $_GET['search_term'],
  134. $categoryCode
  135. );
  136. }
  137. }
  138. // We are unsubscribing from a course (=Unsubscribe from course).
  139. if (isset($_GET['unsubscribe'])) {
  140. $search_term = isset($_GET['search_term']) ? $_GET['search_term'] : null;
  141. if ($ctok == $_GET['sec_token']) {
  142. $courses_controller->unsubscribe_user_from_course($_GET['unsubscribe'], $search_term, $categoryCode);
  143. }
  144. }
  145. // We are unsubscribing from a course (=Unsubscribe from course).
  146. if (isset($_POST['unsubscribe'])) {
  147. if ($ctok == $_POST['sec_token']) {
  148. $courses_controller->unsubscribe_user_from_course($_POST['unsubscribe']);
  149. }
  150. }
  151. switch ($action) {
  152. case 'subscribe_user_with_password':
  153. $courses_controller->subscribe_user(
  154. isset($_POST['subscribe_user_with_password']) ? $_POST['subscribe_user_with_password'] : '',
  155. isset($_POST['search_term']) ? $_POST['search_term'] : '',
  156. isset($_POST['category_code']) ? $_POST['category_code'] : ''
  157. );
  158. break;
  159. case 'createcoursecategory':
  160. $courses_controller->categories_list($action);
  161. break;
  162. case 'deletecoursecategory':
  163. $courses_controller->courses_list($action);
  164. break;
  165. case 'sortmycourses':
  166. $courses_controller->courses_list($action);
  167. break;
  168. case 'subscribe':
  169. if (!$user_can_view_page) {
  170. api_not_allowed(true);
  171. }
  172. header('Location: '.api_get_self());
  173. exit;
  174. /* if (!CoursesAndSessionsCatalog::is(CATALOG_SESSIONS)) {
  175. $courses_controller->courses_categories(
  176. $action,
  177. $categoryCode,
  178. null,
  179. null,
  180. null,
  181. $limit
  182. );
  183. } else {
  184. header('Location: ' . api_get_self());
  185. exit;
  186. }*/
  187. break;
  188. case 'display_random_courses':
  189. if (!$user_can_view_page) {
  190. api_not_allowed(true);
  191. }
  192. $courses_controller->courses_categories($action);
  193. break;
  194. case 'display_courses':
  195. if (!$user_can_view_page) {
  196. api_not_allowed(true);
  197. }
  198. $courses_controller->courses_categories(
  199. $action,
  200. $categoryCode,
  201. null,
  202. null,
  203. null,
  204. $limit
  205. );
  206. break;
  207. case 'display_sessions':
  208. if (!$user_can_view_page) {
  209. api_not_allowed(true);
  210. }
  211. $courses_controller->sessionsList($action, $nameTools, $limit);
  212. break;
  213. case 'subscribe_to_session':
  214. if (!$user_can_view_page) {
  215. api_not_allowed(true);
  216. }
  217. $userId = api_get_user_id();
  218. $confirmed = isset($_GET['confirm']);
  219. $sessionId = intval($_GET['session_id']);
  220. if (empty($userId)) {
  221. api_not_allowed();
  222. exit;
  223. }
  224. if (!$confirmed) {
  225. $template = new Template(null, false, false, false, false, false);
  226. $template->assign('session_id', $sessionId);
  227. $layout = $template->get_template('auth/confirm_session_subscription.tpl');
  228. echo $template->fetch($layout);
  229. exit;
  230. }
  231. $registrationAllowed = api_get_setting('catalog_allow_session_auto_subscription');
  232. if ($registrationAllowed === 'true') {
  233. $entityManager = Database::getManager();
  234. $repository = $entityManager->getRepository('ChamiloCoreBundle:SequenceResource');
  235. $sequences = $repository->getRequirements(
  236. $sessionId,
  237. SequenceResource::SESSION_TYPE
  238. );
  239. if (count($sequences) > 0) {
  240. $requirementsData = SequenceResourceManager::checkRequirementsForUser(
  241. $sequences,
  242. SequenceResource::SESSION_TYPE,
  243. $userId
  244. );
  245. $continueWithSubscription = SequenceResourceManager::checkSequenceAreCompleted($requirementsData);
  246. if (!$continueWithSubscription) {
  247. header('Location: '.api_get_path(WEB_CODE_PATH).'auth/courses.php');
  248. exit;
  249. }
  250. }
  251. SessionManager::subscribe_users_to_session(
  252. $_GET['session_id'],
  253. array($userId),
  254. SESSION_VISIBLE_READ_ONLY,
  255. false
  256. );
  257. $coursesList = SessionManager::get_course_list_by_session_id($_GET['session_id']);
  258. $count = count($coursesList);
  259. $url = '';
  260. if ($count <= 0) {
  261. // no course in session -> return to catalog
  262. $url = api_get_path(WEB_CODE_PATH).'auth/courses.php';
  263. } elseif ($count == 1) {
  264. // only one course, so redirect directly to this course
  265. foreach ($coursesList as $course) {
  266. $url = api_get_path(WEB_COURSE_PATH).$course['directory'].'/index.php?id_session='.intval($_GET['session_id']);
  267. }
  268. } else {
  269. $url = api_get_path(WEB_CODE_PATH).'session/index.php?session_id='.intval($_GET['session_id']);
  270. }
  271. header('Location: '.$url);
  272. exit;
  273. }
  274. //else show error message?
  275. break;
  276. case 'search_tag':
  277. if (!$user_can_view_page) {
  278. api_not_allowed(true);
  279. }
  280. $courses_controller->sessionsListByCoursesTag($limit);
  281. break;
  282. case 'search_session':
  283. if (!$user_can_view_page) {
  284. api_not_allowed(true);
  285. }
  286. $courses_controller->sessionListBySearch($limit);
  287. break;
  288. }