user_portal.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * This is the index file displayed when a user is logged in on Chamilo.
  6. *
  7. * It displays:
  8. * - personal course list
  9. * - menu bar
  10. * Search for CONFIGURATION parameters to modify settings
  11. *
  12. * @package chamilo.main
  13. *
  14. * @todo Shouldn't the CONFVAL_ constant be moved to the config page? Has anybody any idea what the are used for?
  15. * If these are really configuration settings then we can add those to the dokeos config settings.
  16. * @todo check for duplication of functions with index.php (user_portal.php is orginally a copy of index.php)
  17. * @todo display_digest, shouldn't this be removed and be made into an extension?
  18. */
  19. /* Flag forcing the 'current course' reset, as we're not inside a course anymore */
  20. $cidReset = true;
  21. /* Included libraries */
  22. require_once './main/inc/global.inc.php';
  23. // For HTML editor repository.
  24. Session::erase('this_section');
  25. $this_section = SECTION_COURSES;
  26. api_block_anonymous_users(); // Only users who are logged in can proceed.
  27. $logInfo = [
  28. 'tool' => SECTION_COURSES,
  29. 'tool_id' => 0,
  30. 'tool_id_detail' => 0,
  31. 'action' => '',
  32. 'info' => '',
  33. ];
  34. Event::registerLog($logInfo);
  35. $userId = api_get_user_id();
  36. $collapsable = api_get_configuration_value('allow_user_session_collapsable');
  37. if ($collapsable) {
  38. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
  39. $sessionId = isset($_REQUEST['session_id']) ? $_REQUEST['session_id'] : '';
  40. $value = isset($_REQUEST['value']) ? (int) $_REQUEST['value'] : '';
  41. switch ($action) {
  42. case 'collapse_session':
  43. if (!empty($sessionId)) {
  44. $userRelSession = SessionManager::getUserSession($userId, $sessionId);
  45. if ($userRelSession) {
  46. $table = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  47. $sql = "UPDATE $table SET collapsed = $value WHERE id = ".$userRelSession['id'];
  48. Database::query($sql);
  49. Display::addFlash(Display::return_message(get_lang('Update successful')));
  50. }
  51. header('Location: user_portal.php');
  52. exit;
  53. }
  54. break;
  55. }
  56. }
  57. /* Constants and CONFIGURATION parameters */
  58. $load_dirs = api_get_setting('show_documents_preview');
  59. $displayMyCourseViewBySessionLink = api_get_setting('my_courses_view_by_session') === 'true';
  60. $nameTools = get_lang('My courses');
  61. $loadHistory = isset($_GET['history']) && intval($_GET['history']) == 1 ? true : false;
  62. // Load course notification by ajax
  63. $loadNotificationsByAjax = api_get_configuration_value('user_portal_load_notification_by_ajax');
  64. if ($loadNotificationsByAjax) {
  65. $htmlHeadXtra[] = '<script>
  66. $(function() {
  67. $(".course_notification").each(function(index) {
  68. var div = $(this);
  69. var id = $(this).attr("id");
  70. var idList = id.split("_");
  71. var courseId = idList[1];
  72. var sessionId = idList[2];
  73. var status = idList[3];
  74. $.ajax({
  75. type: "GET",
  76. url: "'.api_get_path(WEB_AJAX_PATH).'course_home.ajax.php?a=get_notification&course_id="+courseId+"&session_id="+sessionId+"&status="+status,
  77. success: function(data) {
  78. div.append(data);
  79. }
  80. });
  81. });
  82. });
  83. </script>';
  84. }
  85. /*
  86. Header
  87. Include the HTTP, HTML headers plus the top banner.
  88. */
  89. if ($load_dirs) {
  90. $url = api_get_path(WEB_AJAX_PATH).'document.ajax.php?a=document_preview';
  91. $folder_icon = api_get_path(WEB_IMG_PATH).'icons/22/folder.png';
  92. $close_icon = api_get_path(WEB_IMG_PATH).'loading1.gif';
  93. $htmlHeadXtra[] = '<script>
  94. $(document).ready(function() {
  95. $(".document_preview_container").hide();
  96. $(".document_preview").click(function() {
  97. var my_id = this.id;
  98. var course_id = my_id.split("_")[2];
  99. var session_id = my_id.split("_")[3];
  100. //showing div
  101. $(".document_preview_container").hide();
  102. $("#document_result_" +course_id+"_" + session_id).show();
  103. // Loading
  104. var image = $("img", this);
  105. image.attr("src", "'.$close_icon.'");
  106. $.ajax({
  107. url: "'.$url.'",
  108. data: "course_id="+course_id+"&session_id="+session_id,
  109. success: function(return_value) {
  110. image.attr("src", "'.$folder_icon.'");
  111. $("#document_result_" +course_id+"_" + session_id).html(return_value);
  112. }
  113. });
  114. });
  115. });
  116. </script>';
  117. }
  118. if ($displayMyCourseViewBySessionLink) {
  119. $htmlHeadXtra[] = '
  120. <script>
  121. userId = '.$userId.'
  122. $(document).ready(function() {
  123. changeMyCoursesView($.cookie("defaultMyCourseView" + userId));
  124. });
  125. /**
  126. * Keep in cookie the last teacher view for the My Courses Tab. default view, or view by session
  127. * @param inView
  128. */
  129. function changeMyCoursesView(inView) {
  130. $.cookie("defaultMyCourseView"+userId, inView, { expires: 365 });
  131. if (inView == '.IndexManager::VIEW_BY_SESSION.') {
  132. $("#viewBySession").addClass("btn-primary");
  133. $("#viewByDefault").removeClass("btn-primary");
  134. } else {
  135. $("#viewByDefault").addClass("btn-primary");
  136. $("#viewBySession").removeClass("btn-primary");
  137. }
  138. }
  139. </script>';
  140. }
  141. $myCourseListAsCategory = api_get_configuration_value('my_courses_list_as_category');
  142. $controller = new IndexManager(get_lang('My courses'));
  143. if (!$myCourseListAsCategory) {
  144. // Main courses and session list
  145. if (isset($_COOKIE['defaultMyCourseView'.$userId]) &&
  146. $_COOKIE['defaultMyCourseView'.$userId] == IndexManager::VIEW_BY_SESSION &&
  147. $displayMyCourseViewBySessionLink
  148. ) {
  149. $courseAndSessions = $controller->returnCoursesAndSessionsViewBySession($userId);
  150. IndexManager::setDefaultMyCourseView(IndexManager::VIEW_BY_SESSION, $userId);
  151. } else {
  152. $courseAndSessions = $controller->returnCoursesAndSessions($userId, true, null, true, $loadHistory);
  153. IndexManager::setDefaultMyCourseView(IndexManager::VIEW_BY_DEFAULT, $userId);
  154. }
  155. // if teacher, session coach or admin, display the button to change te course view
  156. if ($displayMyCourseViewBySessionLink &&
  157. (
  158. api_is_drh() ||
  159. api_is_session_general_coach() ||
  160. api_is_platform_admin() ||
  161. api_is_session_admin() ||
  162. api_is_teacher()
  163. )
  164. ) {
  165. $courseAndSessions['html'] = "
  166. <div class='view-by-session-link'>
  167. <div class='btn-group pull-right'>
  168. <a class='btn btn-default' id='viewByDefault' href='user_portal.php'
  169. onclick='changeMyCoursesView(\"".IndexManager::VIEW_BY_DEFAULT."\")'>
  170. ".get_lang('MyCoursesDefaultView')."
  171. </a>
  172. <a class='btn btn-default' id='viewBySession' href='user_portal.php'
  173. onclick='changeMyCoursesView(\"".IndexManager::VIEW_BY_SESSION."\")'>
  174. ".get_lang('MyCoursesSessionView')."
  175. </a>
  176. </div>
  177. </div>
  178. <br /><br />
  179. ".$courseAndSessions['html'];
  180. }
  181. } else {
  182. $categoryCode = isset($_GET['category']) ? $_GET['category'] : '';
  183. if (!$categoryCode) {
  184. $courseAndSessions = $controller->returnCourseCategoryListFromUser($userId);
  185. } else {
  186. $courseAndSessions = $controller->returnCoursesAndSessions(
  187. $userId,
  188. false,
  189. $categoryCode,
  190. true,
  191. $loadHistory
  192. );
  193. $getCategory = CourseCategory::getCategory($categoryCode);
  194. $controller->tpl->assign('category', $getCategory);
  195. }
  196. }
  197. // Check if a user is enrolled only in one course for going directly to the course after the login.
  198. if (api_get_setting('go_to_course_after_login') === 'true') {
  199. $count_of_sessions = $courseAndSessions['session_count'];
  200. $count_of_courses_no_sessions = $courseAndSessions['course_count'];
  201. // User is subscribe in 1 session and 0 courses.
  202. if ($count_of_sessions == 1 && $count_of_courses_no_sessions == 0) {
  203. $sessions = SessionManager::get_sessions_by_user($userId);
  204. if (isset($sessions[0])) {
  205. $sessionInfo = $sessions[0];
  206. // Session only has 1 course.
  207. if (isset($sessionInfo['courses']) &&
  208. count($sessionInfo['courses']) == 1
  209. ) {
  210. $courseCode = $sessionInfo['courses'][0]['code'];
  211. $courseInfo = api_get_course_info_by_id($sessionInfo['courses'][0]['real_id']);
  212. $courseUrl = $courseInfo['course_public_url'].'?id_session='.$sessionInfo['session_id'];
  213. header('Location:'.$courseUrl);
  214. exit;
  215. }
  216. // Session has many courses.
  217. if (isset($sessionInfo['session_id'])) {
  218. $url = api_get_path(WEB_CODE_PATH).'session/index.php?session_id='.$sessionInfo['session_id'];
  219. header('Location:'.$url);
  220. exit;
  221. }
  222. }
  223. }
  224. // User is subscribed to 1 course.
  225. if (!isset($_SESSION['coursesAlreadyVisited']) &&
  226. $count_of_sessions == 0 &&
  227. $count_of_courses_no_sessions == 1
  228. ) {
  229. $courses = CourseManager::get_courses_list_by_user_id($userId);
  230. if (!empty($courses) && isset($courses[0]) && isset($courses[0]['code'])) {
  231. $courseInfo = api_get_course_info_by_id($courses[0]['real_id']);
  232. if (!empty($courseInfo)) {
  233. $courseUrl = $courseInfo['course_public_url'];
  234. header('Location:'.$courseUrl);
  235. exit;
  236. }
  237. }
  238. }
  239. }
  240. $showWelcomeCourse = false;
  241. // Show the chamilo mascot
  242. if (empty($courseAndSessions['html_courses']) && !isset($_GET['history'])) {
  243. $controller->setWelComeCourse();
  244. $showWelcomeCourse = true;
  245. }
  246. $controller->tpl->assign('content', $courseAndSessions['html']);
  247. // Display the Site Use Cookie Warning Validation
  248. $useCookieValidation = api_get_setting('cookie_warning');
  249. if ($useCookieValidation === 'true') {
  250. if (isset($_POST['acceptCookies'])) {
  251. api_set_site_use_cookie_warning_cookie();
  252. } else {
  253. if (!api_site_use_cookie_warning_cookie_exist()) {
  254. if (Template::isToolBarDisplayedForUser()) {
  255. $controller->tpl->assign('toolBarDisplayed', true);
  256. } else {
  257. $controller->tpl->assign('toolBarDisplayed', false);
  258. }
  259. $controller->tpl->assign('displayCookieUsageWarning', true);
  260. }
  261. }
  262. }
  263. $historyClass = '';
  264. if (!empty($_GET['history'])) {
  265. $historyClass = 'courses-history';
  266. }
  267. $controller->tpl->assign('course_history_page', $historyClass);
  268. if ($myCourseListAsCategory) {
  269. $controller->tpl->assign('header', get_lang('My courses'));
  270. }
  271. $controller->setGradeBookDependencyBar($userId);
  272. // Deleting the session_id.
  273. Session::erase('session_id');
  274. Session::erase('id_session');
  275. Session::erase('studentview');
  276. api_remove_in_gradebook();
  277. $controller->tpl->assign('content', $controller->tpl->fetch('@ChamiloTheme/Index/userportal.html.twig'));
  278. $controller->tpl->display_one_col_template();