courses.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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(
  126. $_REQUEST['search_term'],
  127. null,
  128. null,
  129. null,
  130. $limit,
  131. true
  132. );
  133. }
  134. }
  135. // Subscribe user to course
  136. if (isset($_REQUEST['subscribe_course'])) {
  137. if ($ctok == $_GET['sec_token']) {
  138. $courses_controller->subscribe_user(
  139. $_GET['subscribe_course'],
  140. $_GET['search_term'],
  141. $categoryCode
  142. );
  143. }
  144. }
  145. // We are unsubscribing from a course (=Unsubscribe from course).
  146. if (isset($_GET['unsubscribe'])) {
  147. $search_term = isset($_GET['search_term']) ? $_GET['search_term'] : null;
  148. if ($ctok == $_GET['sec_token']) {
  149. $courses_controller->unsubscribe_user_from_course(
  150. $_GET['unsubscribe'],
  151. $search_term,
  152. $categoryCode
  153. );
  154. }
  155. }
  156. // We are unsubscribing from a course (=Unsubscribe from course).
  157. if (isset($_POST['unsubscribe'])) {
  158. if ($ctok == $_POST['sec_token']) {
  159. $courses_controller->unsubscribe_user_from_course($_POST['unsubscribe']);
  160. }
  161. }
  162. switch ($action) {
  163. case 'subscribe_user_with_password':
  164. $courses_controller->subscribe_user(
  165. isset($_POST['subscribe_user_with_password']) ? $_POST['subscribe_user_with_password'] : '',
  166. isset($_POST['search_term']) ? $_POST['search_term'] : '',
  167. isset($_POST['category_code']) ? $_POST['category_code'] : ''
  168. );
  169. break;
  170. case 'createcoursecategory':
  171. $courses_controller->categories_list($action);
  172. break;
  173. case 'deletecoursecategory':
  174. $courses_controller->courses_list($action);
  175. break;
  176. case 'sortmycourses':
  177. $courses_controller->courses_list($action);
  178. break;
  179. case 'subscribe':
  180. if (!$user_can_view_page) {
  181. api_not_allowed(true);
  182. }
  183. header('Location: '.api_get_self());
  184. exit;
  185. /* if (!CoursesAndSessionsCatalog::is(CATALOG_SESSIONS)) {
  186. $courses_controller->courses_categories(
  187. $action,
  188. $categoryCode,
  189. null,
  190. null,
  191. null,
  192. $limit
  193. );
  194. } else {
  195. header('Location: ' . api_get_self());
  196. exit;
  197. }*/
  198. break;
  199. case 'display_random_courses':
  200. if (!$user_can_view_page) {
  201. api_not_allowed(true);
  202. }
  203. $courses_controller->courses_categories($action);
  204. break;
  205. case 'display_courses':
  206. if (!$user_can_view_page) {
  207. api_not_allowed(true);
  208. }
  209. $courses_controller->courses_categories(
  210. $action,
  211. $categoryCode,
  212. null,
  213. null,
  214. null,
  215. $limit
  216. );
  217. break;
  218. case 'display_sessions':
  219. if (!$user_can_view_page) {
  220. api_not_allowed(true);
  221. }
  222. $courses_controller->sessionsList($action, $nameTools, $limit);
  223. break;
  224. case 'subscribe_to_session':
  225. if (!$user_can_view_page) {
  226. api_not_allowed(true);
  227. }
  228. $userId = api_get_user_id();
  229. $confirmed = isset($_GET['confirm']);
  230. $sessionId = intval($_GET['session_id']);
  231. if (empty($userId)) {
  232. api_not_allowed();
  233. exit;
  234. }
  235. if (!$confirmed) {
  236. $template = new Template(null, false, false, false, false, false);
  237. $template->assign('session_id', $sessionId);
  238. $layout = $template->get_template('auth/confirm_session_subscription.tpl');
  239. echo $template->fetch($layout);
  240. exit;
  241. }
  242. $registrationAllowed = api_get_setting('catalog_allow_session_auto_subscription');
  243. if ($registrationAllowed === 'true') {
  244. $entityManager = Database::getManager();
  245. $repository = $entityManager->getRepository('ChamiloCoreBundle:SequenceResource');
  246. $sequences = $repository->getRequirements(
  247. $sessionId,
  248. SequenceResource::SESSION_TYPE
  249. );
  250. if (count($sequences) > 0) {
  251. $requirementsData = SequenceResourceManager::checkRequirementsForUser(
  252. $sequences,
  253. SequenceResource::SESSION_TYPE,
  254. $userId
  255. );
  256. $continueWithSubscription = SequenceResourceManager::checkSequenceAreCompleted($requirementsData);
  257. if (!$continueWithSubscription) {
  258. header('Location: '.api_get_path(WEB_CODE_PATH).'auth/courses.php');
  259. exit;
  260. }
  261. }
  262. SessionManager::subscribe_users_to_session(
  263. $_GET['session_id'],
  264. array($userId),
  265. SESSION_VISIBLE_READ_ONLY,
  266. false
  267. );
  268. $coursesList = SessionManager::get_course_list_by_session_id($_GET['session_id']);
  269. $count = count($coursesList);
  270. $url = '';
  271. if ($count <= 0) {
  272. // no course in session -> return to catalog
  273. $url = api_get_path(WEB_CODE_PATH).'auth/courses.php';
  274. } elseif ($count == 1) {
  275. // only one course, so redirect directly to this course
  276. foreach ($coursesList as $course) {
  277. $url = api_get_path(WEB_COURSE_PATH).$course['directory'].'/index.php?id_session='.intval($_GET['session_id']);
  278. }
  279. } else {
  280. $url = api_get_path(WEB_CODE_PATH).'session/index.php?session_id='.intval($_GET['session_id']);
  281. }
  282. header('Location: '.$url);
  283. exit;
  284. }
  285. //else show error message?
  286. break;
  287. case 'search_tag':
  288. if (!$user_can_view_page) {
  289. api_not_allowed(true);
  290. }
  291. $courses_controller->sessionsListByCoursesTag($limit);
  292. break;
  293. case 'search_session':
  294. if (!$user_can_view_page) {
  295. api_not_allowed(true);
  296. }
  297. $courses_controller->sessionListBySearch($limit);
  298. break;
  299. }