courses.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 dispatching
  6. * to the controllers depend on the current action.
  7. *
  8. * @author Christian Fasanando <christian1827@gmail.com> - Beeznest
  9. *
  10. * @package chamilo.auth
  11. */
  12. // Delete the globals['_cid'], we don't need it here.
  13. $cidReset = true; // Flag forcing the 'current course' reset
  14. require_once __DIR__.'/../inc/global.inc.php';
  15. $ctok = Security::get_existing_token();
  16. // Get Limit data
  17. $limit = CoursesController::getLimitArray();
  18. // Section for the tabs.
  19. $this_section = SECTION_CATALOG;
  20. if (api_get_setting('course_catalog_published') !== 'true') {
  21. // Access rights: anonymous users can't do anything useful here.
  22. api_block_anonymous_users();
  23. }
  24. // For students
  25. $user_can_view_page = true;
  26. if (api_get_setting('allow_students_to_browse_courses') === 'false') {
  27. $user_can_view_page = false;
  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 = [
  35. 'subscribe',
  36. 'display_courses',
  37. 'display_random_courses',
  38. 'subscribe_user_with_password',
  39. 'display_sessions',
  40. 'subscribe_to_session',
  41. 'search_tag',
  42. 'subscribe_course_validation',
  43. 'subscribe_course',
  44. ];
  45. $action = CoursesAndSessionsCatalog::is(CATALOG_SESSIONS) ? 'display_sessions' : 'display_courses';
  46. if (isset($_GET['action']) && in_array($_GET['action'], $actions)) {
  47. $action = Security::remove_XSS($_GET['action']);
  48. }
  49. $categoryCode = isset($_GET['category_code']) && !empty($_GET['category_code']) ? $_GET['category_code'] : 'ALL';
  50. $searchTerm = isset($_REQUEST['search_term']) ? Security::remove_XSS($_REQUEST['search_term']) : '';
  51. $nameTools = CourseCategory::getCourseCatalogNameTools($action);
  52. if (empty($nameTools)) {
  53. $nameTools = get_lang('CourseManagement');
  54. } else {
  55. if (!in_array(
  56. $action,
  57. ['display_random_courses', 'display_courses', 'subscribe']
  58. )) {
  59. $interbreadcrumb[] = [
  60. 'url' => api_get_path(WEB_CODE_PATH).'auth/courses.php',
  61. 'name' => get_lang('CourseManagement'),
  62. ];
  63. }
  64. $interbreadcrumb[] = ['url' => '#', 'name' => $nameTools];
  65. }
  66. // course description controller object
  67. $courseController = new CoursesController();
  68. // search courses
  69. if (isset($_REQUEST['search_course'])) {
  70. if (!empty($_REQUEST['sec_token']) && $ctok == $_REQUEST['sec_token']) {
  71. $courseController->search_courses(
  72. $searchTerm,
  73. null,
  74. null,
  75. null,
  76. $limit,
  77. true
  78. );
  79. exit;
  80. }
  81. }
  82. // We are unsubscribing from a course (=Unsubscribe from course).
  83. if (isset($_GET['unsubscribe'])) {
  84. if (!empty($_GET['sec_token']) && $ctok == $_GET['sec_token']) {
  85. $courseController->unsubscribe_user_from_course(
  86. $_GET['unsubscribe'],
  87. $searchTerm,
  88. $categoryCode
  89. );
  90. }
  91. }
  92. // We are unsubscribing from a course (=Unsubscribe from course).
  93. if (isset($_POST['unsubscribe'])) {
  94. if (!empty($_POST['sec_token']) && $ctok == $_POST['sec_token']) {
  95. $courseController->unsubscribe_user_from_course($_POST['unsubscribe']);
  96. }
  97. }
  98. switch ($action) {
  99. case 'subscribe_course':
  100. if (api_is_anonymous()) {
  101. header('Location: '.api_get_path(WEB_CODE_PATH).'auth/inscription.php?c='.$courseCodeToSubscribe);
  102. exit;
  103. }
  104. $courseCodeToSubscribe = isset($_GET['subscribe_course']) ? Security::remove_XSS($_GET['subscribe_course']) : '';
  105. if (Security::check_token('get')) {
  106. CourseManager::autoSubscribeToCourse($courseCodeToSubscribe);
  107. header('Location: '.api_get_self());
  108. exit;
  109. }
  110. break;
  111. case 'subscribe_course_validation':
  112. $courseCodeToSubscribe = isset($_GET['subscribe_course']) ? Security::remove_XSS($_GET['subscribe_course']) : '';
  113. $courseInfo = api_get_course_info($courseCodeToSubscribe);
  114. if (empty($courseInfo)) {
  115. header('Location: '.api_get_self());
  116. exit;
  117. }
  118. $message = get_lang('CourseRequiresPassword').' ';
  119. $message .= $courseInfo['title'].' ('.$courseInfo['visual_code'].') ';
  120. $action = api_get_self().
  121. '?action=subscribe_course_validation&sec_token='.Security::getTokenFromSession().'&subscribe_course='.$courseCodeToSubscribe;
  122. $form = new FormValidator(
  123. 'subscribe_user_with_password',
  124. 'post',
  125. $action
  126. );
  127. $form->addHeader($message);
  128. $form->addElement('hidden', 'sec_token', Security::getTokenFromSession());
  129. $form->addElement('hidden', 'subscribe_user_with_password', $courseInfo['code']);
  130. $form->addElement('text', 'course_registration_code');
  131. $form->addButtonSave(get_lang('SubmitRegistrationCode'));
  132. $content = $form->returnForm();
  133. if ($form->validate()) {
  134. if (sha1($_POST['course_registration_code']) === $courseInfo['registration_code']) {
  135. CourseManager::autoSubscribeToCourse($_POST['subscribe_user_with_password']);
  136. header('Location: '.api_get_self());
  137. exit;
  138. } else {
  139. Display::addFlash(Display::return_message(get_lang('CourseRegistrationCodeIncorrect')), 'warning');
  140. header('Location: '.$action);
  141. exit;
  142. }
  143. }
  144. $template = new Template(get_lang('Subscribe'), true, true, false, false, false);
  145. $template->assign('content', $content);
  146. $template->display_one_col_template();
  147. break;
  148. case 'subscribe':
  149. if (!$user_can_view_page) {
  150. api_not_allowed(true);
  151. }
  152. header('Location: '.api_get_self());
  153. exit;
  154. break;
  155. case 'display_random_courses':
  156. if (!$user_can_view_page) {
  157. api_not_allowed(true);
  158. }
  159. $courseController->courses_categories($action);
  160. break;
  161. case 'display_courses':
  162. if (!$user_can_view_page) {
  163. api_not_allowed(true);
  164. }
  165. $courseController->courses_categories(
  166. $action,
  167. $categoryCode,
  168. null,
  169. null,
  170. null,
  171. $limit
  172. );
  173. break;
  174. case 'display_sessions':
  175. if (!$user_can_view_page) {
  176. api_not_allowed(true);
  177. }
  178. $courseController->sessionList($action, $nameTools, $limit);
  179. break;
  180. case 'subscribe_to_session':
  181. if (!$user_can_view_page) {
  182. api_not_allowed(true);
  183. }
  184. $userId = api_get_user_id();
  185. $confirmed = isset($_GET['confirm']);
  186. $sessionId = (int) $_GET['session_id'];
  187. if (empty($userId)) {
  188. api_not_allowed();
  189. exit;
  190. }
  191. if (!$confirmed) {
  192. $template = new Template(null, false, false, false, false, false);
  193. $template->assign('session_id', $sessionId);
  194. $layout = $template->get_template('auth/confirm_session_subscription.tpl');
  195. echo $template->fetch($layout);
  196. exit;
  197. }
  198. $registrationAllowed = api_get_setting('catalog_allow_session_auto_subscription');
  199. if ($registrationAllowed === 'true') {
  200. $entityManager = Database::getManager();
  201. $repository = $entityManager->getRepository('ChamiloCoreBundle:SequenceResource');
  202. $sequences = $repository->getRequirements(
  203. $sessionId,
  204. SequenceResource::SESSION_TYPE
  205. );
  206. if (count($sequences) > 0) {
  207. $requirementsData = SequenceResourceManager::checkRequirementsForUser(
  208. $sequences,
  209. SequenceResource::SESSION_TYPE,
  210. $userId
  211. );
  212. $continueWithSubscription = SequenceResourceManager::checkSequenceAreCompleted($requirementsData);
  213. if (!$continueWithSubscription) {
  214. header('Location: '.api_get_path(WEB_CODE_PATH).'auth/courses.php');
  215. exit;
  216. }
  217. }
  218. SessionManager::subscribeUsersToSession(
  219. $sessionId,
  220. [$userId],
  221. SESSION_VISIBLE_READ_ONLY,
  222. false
  223. );
  224. $coursesList = SessionManager::get_course_list_by_session_id($sessionId);
  225. $count = count($coursesList);
  226. $url = '';
  227. if ($count <= 0) {
  228. // no course in session -> return to catalog
  229. $url = api_get_path(WEB_CODE_PATH).'auth/courses.php';
  230. } elseif ($count == 1) {
  231. // only one course, so redirect directly to this course
  232. foreach ($coursesList as $course) {
  233. $url = api_get_path(WEB_COURSE_PATH).$course['directory'].'/index.php?id_session='.$sessionId;
  234. }
  235. } else {
  236. $url = api_get_path(WEB_CODE_PATH).'session/index.php?session_id='.$sessionId;
  237. }
  238. header('Location: '.$url);
  239. exit;
  240. }
  241. break;
  242. case 'search_tag':
  243. if (!$user_can_view_page) {
  244. api_not_allowed(true);
  245. }
  246. $courseController->sessionsListByCoursesTag($limit);
  247. break;
  248. }