courses.php 9.5 KB

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