courses.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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_random_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']) ? $_GET['category_code'] : '';
  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. if ($ctok == $_GET['sec_token']) {
  138. $courses_controller->unsubscribe_user_from_course($_GET['unsubscribe'], $_GET['search_term'], $categoryCode);
  139. //$message = remove_user_from_course($_user['user_id'], $_POST['unsubscribe']);
  140. }
  141. }
  142. // We are unsubscribing from a course (=Unsubscribe from course).
  143. if (isset($_POST['unsubscribe'])) {
  144. if ($ctok == $_POST['sec_token']) {
  145. $courses_controller->unsubscribe_user_from_course($_POST['unsubscribe']);
  146. //$message = remove_user_from_course($_user['user_id'], $_POST['unsubscribe']);
  147. }
  148. }
  149. switch ($action) {
  150. case 'subscribe_user_with_password':
  151. $courses_controller->subscribe_user(
  152. $_POST['subscribe_user_with_password'],
  153. $_POST['search_term'],
  154. $_POST['category_code']
  155. );
  156. exit;
  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. $courses_controller->courses_categories(
  172. $action,
  173. $categoryCode,
  174. null,
  175. null,
  176. null,
  177. $limit
  178. );
  179. break;
  180. case 'display_random_courses':
  181. if (!$user_can_view_page) {
  182. api_not_allowed(true);
  183. }
  184. $courses_controller->courses_categories($action);
  185. break;
  186. case 'display_courses':
  187. if (!$user_can_view_page) {
  188. api_not_allowed(true);
  189. }
  190. $courses_controller->courses_categories(
  191. $action,
  192. $categoryCode,
  193. null,
  194. null,
  195. null,
  196. $limit
  197. );
  198. break;
  199. case 'display_sessions':
  200. if (!$user_can_view_page) {
  201. api_not_allowed(true);
  202. }
  203. $courses_controller->sessionsList($action, $nameTools, $limit);
  204. break;
  205. case 'subscribe_to_session':
  206. if (!$user_can_view_page) {
  207. api_not_allowed(true);
  208. }
  209. $userId = api_get_user_id();
  210. $confirmed = isset($_GET['confirm']);
  211. $sessionId = intval($_GET['session_id']);
  212. if (empty($userId)) {
  213. api_not_allowed();
  214. exit;
  215. }
  216. if (!$confirmed) {
  217. $template = new Template(null, false, false, false, false, false);
  218. $template->assign('session_id', $sessionId);
  219. $layout = $template->get_template('auth/confirm_session_subscription.tpl');
  220. echo $template->fetch($layout);
  221. exit;
  222. }
  223. $registrationAllowed = api_get_setting('catalog_allow_session_auto_subscription');
  224. if ($registrationAllowed === 'true') {
  225. $entityManager = Database::getManager();
  226. $repository = $entityManager->getRepository('ChamiloCoreBundle:SequenceResource');
  227. $sequences = $repository->getRequirements(
  228. $sessionId,
  229. SequenceResource::SESSION_TYPE
  230. );
  231. if (count($sequences) > 0) {
  232. $requirementsData = SequenceResourceManager::checkRequirementsForUser(
  233. $sequences,
  234. SequenceResource::SESSION_TYPE,
  235. $userId
  236. );
  237. $continueWithSubscription = SequenceResourceManager::checkSequenceAreCompleted($requirementsData);
  238. if (!$continueWithSubscription) {
  239. header('Location: ' . api_get_path(WEB_CODE_PATH) . 'auth/courses.php');
  240. exit;
  241. }
  242. }
  243. SessionManager::suscribe_users_to_session(
  244. $_GET['session_id'],
  245. array($userId)
  246. );
  247. $coursesList = SessionManager::get_course_list_by_session_id($_GET['session_id']);
  248. $count = count($coursesList);
  249. $url = '';
  250. if ($count <= 0) {
  251. // no course in session -> return to catalog
  252. $url = api_get_path(WEB_CODE_PATH) . 'auth/courses.php';
  253. } elseif ($count == 1) {
  254. // only one course, so redirect directly to this course
  255. foreach ($coursesList as $course) {
  256. $url = api_get_path(WEB_COURSE_PATH) . $course['directory'] . '/index.php?id_session=' . intval($_GET['session_id']);
  257. }
  258. } else {
  259. $url = api_get_path(WEB_CODE_PATH) . 'session/index.php?session_id=' . intval($_GET['session_id']);
  260. }
  261. header('Location: ' . $url);
  262. exit;
  263. }
  264. //else show error message?
  265. break;
  266. case 'search_tag':
  267. if (!$user_can_view_page) {
  268. api_not_allowed(true);
  269. }
  270. $courses_controller->sessionsListByCoursesTag($limit);
  271. break;
  272. case 'search_session':
  273. if (!$user_can_view_page) {
  274. api_not_allowed(true);
  275. }
  276. $courses_controller->sessionListBySearch($limit);
  277. break;
  278. }