courses.php 10 KB

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