courses.php 13 KB

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