courses.php 10 KB

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