courses.php 10 KB

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