courses.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. '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. 'set_collapsable',
  47. 'subscribe_course_validation',
  48. 'subscribe_course',
  49. ];
  50. $action = CoursesAndSessionsCatalog::is(CATALOG_SESSIONS) ? 'display_sessions' : 'display_courses';
  51. if (isset($_GET['action']) && in_array($_GET['action'], $actions)) {
  52. $action = Security::remove_XSS($_GET['action']);
  53. }
  54. $categoryCode = isset($_GET['category_code']) && !empty($_GET['category_code']) ? $_GET['category_code'] : 'ALL';
  55. $searchTerm = isset($_REQUEST['search_term']) ? Security::remove_XSS($_REQUEST['search_term']) : '';
  56. $nameTools = CourseCategory::getCourseCatalogNameTools($action);
  57. if (empty($nameTools)) {
  58. $nameTools = get_lang('CourseManagement');
  59. } else {
  60. if (!in_array(
  61. $action,
  62. ['sortmycourses', 'createcoursecategory', 'display_random_courses', 'display_courses', 'subscribe']
  63. )) {
  64. $interbreadcrumb[] = [
  65. 'url' => api_get_path(WEB_CODE_PATH).'auth/courses.php',
  66. 'name' => get_lang('CourseManagement'),
  67. ];
  68. }
  69. if ($action === 'createcoursecategory') {
  70. $interbreadcrumb[] = [
  71. 'url' => api_get_path(WEB_CODE_PATH).'auth/courses.php?action=sortmycourses',
  72. 'name' => get_lang('SortMyCourses'),
  73. ];
  74. }
  75. $interbreadcrumb[] = ['url' => '#', 'name' => $nameTools];
  76. }
  77. // course description controller object
  78. $courseController = new CoursesController();
  79. // We are moving a course or category of the user up/down the list (=Sort My Courses).
  80. if (isset($_GET['move'])) {
  81. if (isset($_GET['course'])) {
  82. $courseController->move_course(
  83. $_GET['move'],
  84. $_GET['course'],
  85. $_GET['category']
  86. );
  87. }
  88. if (isset($_GET['category']) && !isset($_GET['course'])) {
  89. $courseController->move_category($_GET['move'], $_GET['category']);
  90. }
  91. }
  92. // We are moving the course of the user to a different user defined course category (=Sort My Courses).
  93. if (isset($_POST['submit_change_course_category'])) {
  94. if (!empty($_POST['sec_token']) && $ctok == $_POST['sec_token']) {
  95. $courseController->change_course_category(
  96. $_POST['course_2_edit_category'],
  97. $_POST['course_categories']
  98. );
  99. }
  100. }
  101. // We edit course category
  102. if (isset($_POST['submit_edit_course_category']) &&
  103. isset($_POST['title_course_category']) &&
  104. strlen(trim($_POST['title_course_category'])) > 0
  105. ) {
  106. if (!empty($_POST['sec_token']) && $ctok == $_POST['sec_token']) {
  107. $courseController->edit_course_category(
  108. $_POST['title_course_category'],
  109. $_POST['edit_course_category']
  110. );
  111. }
  112. }
  113. // We are creating a new user defined course category (= Create Course Category).
  114. if (isset($_POST['create_course_category']) &&
  115. isset($_POST['title_course_category']) &&
  116. strlen(trim($_POST['title_course_category'])) > 0
  117. ) {
  118. if (!empty($_POST['sec_token']) && $ctok == $_POST['sec_token']) {
  119. $courseController->addCourseCategory($_POST['title_course_category']);
  120. }
  121. }
  122. // search courses
  123. if (isset($_REQUEST['search_course'])) {
  124. if (!empty($_REQUEST['sec_token']) && $ctok == $_REQUEST['sec_token']) {
  125. $courseController->search_courses(
  126. $searchTerm,
  127. null,
  128. null,
  129. null,
  130. $limit,
  131. true
  132. );
  133. exit;
  134. }
  135. }
  136. // We are unsubscribing from a course (=Unsubscribe from course).
  137. if (isset($_GET['unsubscribe'])) {
  138. if (!empty($_GET['sec_token']) && $ctok == $_GET['sec_token']) {
  139. $courseController->unsubscribe_user_from_course(
  140. $_GET['unsubscribe'],
  141. $searchTerm,
  142. $categoryCode
  143. );
  144. }
  145. }
  146. // We are unsubscribing from a course (=Unsubscribe from course).
  147. if (isset($_POST['unsubscribe'])) {
  148. if (!empty($_POST['sec_token']) && $ctok == $_POST['sec_token']) {
  149. $courseController->unsubscribe_user_from_course($_POST['unsubscribe']);
  150. }
  151. }
  152. switch ($action) {
  153. case 'deletecoursecategory':
  154. // we are deleting a course category
  155. if (isset($_GET['id'])) {
  156. if (Security::check_token('get')) {
  157. $courseController->delete_course_category($_GET['id']);
  158. header('Location: '.api_get_self());
  159. exit;
  160. }
  161. }
  162. $courseController->courseList($action);
  163. break;
  164. case 'subscribe_course':
  165. if (api_is_anonymous()) {
  166. header('Location: '.api_get_path(WEB_CODE_PATH).'auth/inscription.php?c='.$courseCodeToSubscribe);
  167. exit;
  168. }
  169. $courseCodeToSubscribe = isset($_GET['subscribe_course']) ? Security::remove_XSS($_GET['subscribe_course']) : '';
  170. if (Security::check_token('get')) {
  171. CourseManager::autoSubscribeToCourse($courseCodeToSubscribe);
  172. header('Location: '.api_get_self());
  173. exit;
  174. }
  175. break;
  176. case 'subscribe_course_validation':
  177. $courseCodeToSubscribe = isset($_GET['subscribe_course']) ? Security::remove_XSS($_GET['subscribe_course']) : '';
  178. $courseInfo = api_get_course_info($courseCodeToSubscribe);
  179. if (empty($courseInfo)) {
  180. header('Location: '.api_get_self());
  181. exit;
  182. }
  183. $message = get_lang('CourseRequiresPassword').' ';
  184. $message .= $courseInfo['title'].' ('.$courseInfo['visual_code'].') ';
  185. $action = api_get_self().
  186. '?action=subscribe_course_validation&sec_token='.Security::getTokenFromSession().'&subscribe_course='.$courseCodeToSubscribe;
  187. $form = new FormValidator(
  188. 'subscribe_user_with_password',
  189. 'post',
  190. $action
  191. );
  192. $form->addHeader($message);
  193. $form->addElement('hidden', 'sec_token', Security::getTokenFromSession());
  194. $form->addElement('hidden', 'subscribe_user_with_password', $courseInfo['code']);
  195. $form->addElement('text', 'course_registration_code');
  196. $form->addButtonSave(get_lang('SubmitRegistrationCode'));
  197. $content = $form->returnForm();
  198. if ($form->validate()) {
  199. if (sha1($_POST['course_registration_code']) === $courseInfo['registration_code']) {
  200. CourseManager::autoSubscribeToCourse($_POST['subscribe_user_with_password']);
  201. header('Location: '.api_get_self());
  202. exit;
  203. } else {
  204. Display::addFlash(Display::return_message(get_lang('CourseRegistrationCodeIncorrect')), 'warning');
  205. header('Location: '.$action);
  206. exit;
  207. }
  208. }
  209. $template = new Template(get_lang('Subscribe'), true, true, false, false, false);
  210. $template->assign('content', $content);
  211. $template->display_one_col_template();
  212. break;
  213. case 'createcoursecategory':
  214. $courseController->categoryList();
  215. break;
  216. case 'sortmycourses':
  217. $courseController->courseList($action);
  218. break;
  219. case 'subscribe':
  220. if (!$user_can_view_page) {
  221. api_not_allowed(true);
  222. }
  223. header('Location: '.api_get_self());
  224. exit;
  225. break;
  226. case 'display_random_courses':
  227. if (!$user_can_view_page) {
  228. api_not_allowed(true);
  229. }
  230. $courseController->courses_categories($action);
  231. break;
  232. case 'display_courses':
  233. if (!$user_can_view_page) {
  234. api_not_allowed(true);
  235. }
  236. $courseController->courses_categories(
  237. $action,
  238. $categoryCode,
  239. null,
  240. null,
  241. null,
  242. $limit
  243. );
  244. break;
  245. case 'display_sessions':
  246. if (!$user_can_view_page) {
  247. api_not_allowed(true);
  248. }
  249. $courseController->sessionList($action, $nameTools, $limit);
  250. break;
  251. case 'subscribe_to_session':
  252. if (!$user_can_view_page) {
  253. api_not_allowed(true);
  254. }
  255. $userId = api_get_user_id();
  256. $confirmed = isset($_GET['confirm']);
  257. $sessionId = (int) $_GET['session_id'];
  258. if (empty($userId)) {
  259. api_not_allowed();
  260. exit;
  261. }
  262. if (!$confirmed) {
  263. $template = new Template(null, false, false, false, false, false);
  264. $template->assign('session_id', $sessionId);
  265. $layout = $template->get_template('auth/confirm_session_subscription.tpl');
  266. echo $template->fetch($layout);
  267. exit;
  268. }
  269. $registrationAllowed = api_get_setting('catalog_allow_session_auto_subscription');
  270. if ($registrationAllowed === 'true') {
  271. $entityManager = Database::getManager();
  272. $repository = $entityManager->getRepository('ChamiloCoreBundle:SequenceResource');
  273. $sequences = $repository->getRequirements(
  274. $sessionId,
  275. SequenceResource::SESSION_TYPE
  276. );
  277. if (count($sequences) > 0) {
  278. $requirementsData = SequenceResourceManager::checkRequirementsForUser(
  279. $sequences,
  280. SequenceResource::SESSION_TYPE,
  281. $userId
  282. );
  283. $continueWithSubscription = SequenceResourceManager::checkSequenceAreCompleted($requirementsData);
  284. if (!$continueWithSubscription) {
  285. header('Location: '.api_get_path(WEB_CODE_PATH).'auth/courses.php');
  286. exit;
  287. }
  288. }
  289. SessionManager::subscribeUsersToSession(
  290. $sessionId,
  291. [$userId],
  292. SESSION_VISIBLE_READ_ONLY,
  293. false
  294. );
  295. $coursesList = SessionManager::get_course_list_by_session_id($sessionId);
  296. $count = count($coursesList);
  297. $url = '';
  298. if ($count <= 0) {
  299. // no course in session -> return to catalog
  300. $url = api_get_path(WEB_CODE_PATH).'auth/courses.php';
  301. } elseif ($count == 1) {
  302. // only one course, so redirect directly to this course
  303. foreach ($coursesList as $course) {
  304. $url = api_get_path(WEB_COURSE_PATH).$course['directory'].'/index.php?id_session='.$sessionId;
  305. }
  306. } else {
  307. $url = api_get_path(WEB_CODE_PATH).'session/index.php?session_id='.$sessionId;
  308. }
  309. header('Location: '.$url);
  310. exit;
  311. }
  312. //else show error message?
  313. break;
  314. case 'search_tag':
  315. if (!$user_can_view_page) {
  316. api_not_allowed(true);
  317. }
  318. $courseController->sessionsListByCoursesTag($limit);
  319. break;
  320. case 'search_session':
  321. if (!$user_can_view_page) {
  322. api_not_allowed(true);
  323. }
  324. $courseController->sessionListBySearch($limit);
  325. break;
  326. case 'set_collapsable':
  327. api_block_anonymous_users();
  328. if (!api_get_configuration_value('allow_user_course_category_collapsable')) {
  329. api_not_allowed(true);
  330. }
  331. $userId = api_get_user_id();
  332. $categoryId = isset($_REQUEST['categoryid']) ? (int) $_REQUEST['categoryid'] : 0;
  333. $option = isset($_REQUEST['option']) ? (int) $_REQUEST['option'] : 0;
  334. $redirect = isset($_REQUEST['redirect']) ? $_REQUEST['redirect'] : 0;
  335. if (empty($userId) || empty($categoryId)) {
  336. api_not_allowed(true);
  337. }
  338. $table = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
  339. $sql = "UPDATE $table
  340. SET collapsed = $option
  341. WHERE user_id = $userId AND id = $categoryId";
  342. Database::query($sql);
  343. Display::addFlash(Display::return_message(get_lang('Updated')));
  344. if ($redirect === 'home') {
  345. $url = api_get_path(WEB_PATH).'user_portal.php';
  346. header('Location: '.$url);
  347. exit;
  348. }
  349. $url = api_get_path(WEB_CODE_PATH).'auth/courses.php?action=sortmycourses';
  350. header('Location: '.$url);
  351. exit;
  352. break;
  353. }