user_portal.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This is the index file displayed when a user is logged in on Chamilo.
  5. *
  6. * It displays:
  7. * - personal course list
  8. * - menu bar
  9. * Search for CONFIGURATION parameters to modify settings
  10. * @package chamilo.main
  11. * @todo Shouldn't the SCRIPTVAL_ and CONFVAL_ constant be moved to the config page? Has anybody any idea what the are used for?
  12. * If these are really configuration settings then we can add those to the dokeos config settings.
  13. * @todo check for duplication of functions with index.php (user_portal.php is orginally a copy of index.php)
  14. * @todo display_digest, shouldn't this be removed and be made into an extension?
  15. */
  16. /**
  17. * INIT SECTION
  18. */
  19. // Language files that should be included.
  20. use \ChamiloSession as Session;
  21. $language_file = array('courses', 'index','admin');
  22. $cidReset = true; /* Flag forcing the 'current course' reset,
  23. as we're not inside a course anymore */
  24. if (isset($_SESSION['this_section']))
  25. unset($_SESSION['this_section']); // For HTML editor repository.
  26. /* Included libraries */
  27. require_once './main/inc/global.inc.php';
  28. api_block_anonymous_users(); // Only users who are logged in can proceed.
  29. $nameTools = get_lang('MyCourses');
  30. $this_section = SECTION_COURSES;
  31. $load_dirs = api_get_setting('show_documents_preview');
  32. if ($load_dirs) {
  33. $url = api_get_path(WEB_AJAX_PATH).'document.ajax.php?a=document_preview';
  34. $folder_icon = api_get_path(WEB_IMG_PATH).'icons/22/folder.png';
  35. $close_icon = api_get_path(WEB_IMG_PATH).'loading1.gif';
  36. $htmlHeadXtra[] = '<script>
  37. $(document).ready(function() {
  38. $(".document_preview_container").hide();
  39. $(".document_preview").click(function() {
  40. var my_id = this.id;
  41. var course_id = my_id.split("_")[2];
  42. var session_id = my_id.split("_")[3];
  43. //showing div
  44. $(".document_preview_container").hide();
  45. $("#document_result_" +course_id+"_" + session_id).show();
  46. //Loading
  47. var image = $("img", this);
  48. image.attr("src", "'.$close_icon.'");
  49. $.ajax({
  50. url: "'.$url.'",
  51. data: "course_id="+course_id+"&session_id="+session_id,
  52. success: function(return_value) {
  53. image.attr("src", "'.$folder_icon.'");
  54. $("#document_result_" +course_id+"_" + session_id).html(return_value);
  55. }
  56. });
  57. });
  58. });
  59. </script>';
  60. }
  61. use Silex\Application;
  62. use Symfony\Component\HttpFoundation\Response;
  63. class UserPortalController
  64. {
  65. function indexAction(Application $app)
  66. {
  67. // Check if a user is enrolled only in one course for going directly to the course after the login.
  68. if (api_get_setting('go_to_course_after_login') == 'true') {
  69. // Get the courses list
  70. $personal_course_list = UserManager::get_personal_session_course_list(api_get_user_id());
  71. $my_session_list = array();
  72. $count_of_courses_no_sessions = 0;
  73. $count_of_courses_with_sessions = 0;
  74. foreach ($personal_course_list as $course) {
  75. if (!empty($course['id_session'])) {
  76. $my_session_list[$course['id_session']] = true;
  77. $count_of_courses_with_sessions++;
  78. } else {
  79. $count_of_courses_no_sessions++;
  80. }
  81. }
  82. $count_of_sessions = count($my_session_list);
  83. if ($count_of_sessions == 1 && $count_of_courses_no_sessions == 0) {
  84. $key = array_keys($personal_course_list);
  85. $course_info = $personal_course_list[$key[0]];
  86. $course_directory = $course_info['course_info']['path'];
  87. $id_session = isset($course_info['id_session']) ? $course_info['id_session'] : 0;
  88. $url = api_get_path(WEB_CODE_PATH).'session/?session_id='.$id_session;
  89. header('location:'.$url);
  90. exit;
  91. }
  92. if (!isset($_SESSION['coursesAlreadyVisited']) && $count_of_sessions == 0 && $count_of_courses_no_sessions == 1) {
  93. $key = array_keys($personal_course_list);
  94. $course_info = $personal_course_list[$key[0]];
  95. $course_directory = $course_info['course_info']['path'];
  96. $id_session = isset($course_info['id_session']) ? $course_info['id_session'] : 0;
  97. $url = api_get_path(WEB_COURSE_PATH).$course_directory.'/?id_session='.$id_session;
  98. header('location:'.$url);
  99. exit;
  100. }
  101. }
  102. /* Sniffing system */
  103. /*
  104. //store posts to sessions
  105. if ($_SESSION['sniff_navigator']!="checked") {
  106. $_SESSION['sniff_navigator']=Security::remove_XSS($_POST['sniff_navigator']);
  107. $_SESSION['sniff_screen_size_w']=Security::remove_XSS($_POST['sniff_navigator_screen_size_w']);
  108. $_SESSION['sniff__screen_size_h']=Security::remove_XSS($_POST['sniff_navigator_screen_size_h']);
  109. $_SESSION['sniff_type_mimetypes']=Security::remove_XSS($_POST['sniff_navigator_type_mimetypes']);
  110. $_SESSION['sniff_suffixes_mimetypes']=Security::remove_XSS($_POST['sniff_navigator_suffixes_mimetypes']);
  111. $_SESSION['sniff_list_plugins']=Security::remove_XSS($_POST['sniff_navigator_list_plugins']);
  112. $_SESSION['sniff_check_some_activex']=Security::remove_XSS($_POST['sniff_navigator_check_some_activex']);
  113. $_SESSION['sniff_check_some_plugins']=Security::remove_XSS($_POST['sniff_navigator_check_some_plugins']);
  114. $_SESSION['sniff_java']=Security::remove_XSS($_POST['sniff_navigator_java']);
  115. $_SESSION['sniff_java_sun_ver']=Security::remove_XSS($_POST['sniff_navigator_java_sun_ver']);
  116. }*/
  117. // Main courses and session list
  118. $courses_and_sessions = PageController::return_courses_and_sessions(api_get_user_id());
  119. //Show the chamilo mascot
  120. if (empty($courses_and_sessions) && !isset($_GET['history'])) {
  121. PageController::return_welcome_to_course_block($app['template']);
  122. }
  123. $app['template']->assign('content', $courses_and_sessions);
  124. /*
  125. if (api_get_setting('allow_browser_sniffer') == 'true') {
  126. if ($_SESSION['sniff_navigator']!="checked") {
  127. $app['template']->assign('show_sniff', 1);
  128. } else {
  129. $app['template']->assign('show_sniff', 0);
  130. }
  131. }
  132. //check for flash and message
  133. $sniff_notification = '';
  134. $some_activex=$_SESSION['sniff_check_some_activex'];
  135. $some_plugins=$_SESSION['sniff_check_some_plugins'];
  136. if(!empty($some_activex) || !empty($some_plugins)){
  137. if (! preg_match("/flash_yes/", $some_activex) && ! preg_match("/flash_yes/", $some_plugins)) {
  138. $sniff_notification = Display::return_message(get_lang('NoFlash'), 'warning', true);
  139. //js verification - To annoying of redirecting every time the page
  140. $app['template']->assign('sniff_notification', $sniff_notification);
  141. }
  142. }*/
  143. PageController::return_profile_block();
  144. PageController::return_user_image_block();
  145. PageController::return_course_block();
  146. $app['template']->assign('navigation_course_links', $app['template']->return_navigation_links());
  147. PageController::return_reservation_block();
  148. $app['template']->assign('search_block', PageController::return_search_block());
  149. $app['template']->assign('classes_block', PageController::return_classes_block());
  150. PageController::return_skills_links();
  151. // Deleting the session_id.
  152. Session::erase('session_id');
  153. $response = $app['template']->render_template('userportal/index.tpl');
  154. //return new Response($response, 200, array('Cache-Control' => 's-maxage=3600, private'));
  155. return new Response($response, 200, array());
  156. }
  157. function check_last_login() {
  158. /**
  159. * @todo This piece of code should probably move to local.inc.php where the actual login procedure is handled.
  160. * @todo Check if this code is used. I think this code is never executed because after clicking the submit button
  161. * the code does the stuff in local.inc.php and then redirects to index.php or user_portal.php depending
  162. * on api_get_setting('page_after_login').
  163. */
  164. if (!empty($_POST['submitAuth'])) {
  165. // The user has been already authenticated, we are now to find the last login of the user.
  166. if (!empty($this->user_id)) {
  167. $track_login_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  168. $sql_last_login = "SELECT login_date
  169. FROM $track_login_table
  170. WHERE login_user_id = '".$this->user_id."'
  171. ORDER BY login_date DESC LIMIT 1";
  172. $result_last_login = Database::query($sql_last_login);
  173. if (!$result_last_login) {
  174. if (Database::num_rows($result_last_login) > 0) {
  175. $user_last_login_datetime = Database::fetch_array($result_last_login);
  176. $user_last_login_datetime = $user_last_login_datetime[0];
  177. Session::write('user_last_login_datetime', $user_last_login_datetime);
  178. }
  179. }
  180. Database::free_result($result_last_login);
  181. if (api_is_platform_admin()) {
  182. // decode all open event informations and fill the track_c_* tables
  183. include api_get_path(LIBRARY_PATH).'stats.lib.inc.php';
  184. decodeOpenInfos();
  185. }
  186. }
  187. // End login -- if ($_POST['submitAuth'])
  188. } else {
  189. // Only if login form was not sent because if the form is sent the user was already on the page.
  190. event_open();
  191. }
  192. }
  193. function set_login_form() {
  194. global $loginFailed;
  195. $login_form = '';
  196. if (!($this->user_id) || api_is_anonymous($this->user_id)) {
  197. // Only display if the user isn't logged in.
  198. $this->page->assign('login_language_form', api_display_language_form(true));
  199. $this->page->assign('login_form', self::display_login_form());
  200. if ($loginFailed) {
  201. $this->page->assign('login_failed', self::handle_login_failed());
  202. }
  203. if (api_get_setting('allow_lostpassword') == 'true' || api_get_setting('allow_registration') == 'true') {
  204. $login_form .= '<ul class="nav nav-list">';
  205. if (api_get_setting('allow_registration') != 'false') {
  206. $login_form .= '<li><a href="main/auth/inscription.php">'.get_lang('Reg').'</a></li>';
  207. }
  208. if (api_get_setting('allow_lostpassword') == 'true') {
  209. $login_form .= '<li><a href="main/auth/lostPassword.php">'.get_lang('LostPassword').'</a></li>';
  210. }
  211. $login_form .= '</ul>';
  212. }
  213. $this->page->assign('login_options', $login_form);
  214. }
  215. }
  216. /**
  217. * Alias for the online_logout() function
  218. */
  219. function logout() {
  220. online_logout($this->user_id, true);
  221. }
  222. /**
  223. * This function checks if there are courses that are open to the world in the platform course categories (=faculties)
  224. *
  225. * @param string $category
  226. * @return boolean
  227. */
  228. function category_has_open_courses($category) {
  229. $setting_show_also_closed_courses = api_get_setting('show_closed_courses') == 'true';
  230. $main_course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  231. $category = Database::escape_string($category);
  232. $sql_query = "SELECT * FROM $main_course_table WHERE category_code='$category'";
  233. $sql_result = Database::query($sql_query);
  234. while ($course = Database::fetch_array($sql_result)) {
  235. if (!$setting_show_also_closed_courses) {
  236. if ((api_get_user_id() > 0 && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM) || ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD)) {
  237. return true; //at least one open course
  238. }
  239. } else {
  240. if (isset($course['visibility'])) {
  241. return true; // At least one course (it does not matter weither it's open or not because $setting_show_also_closed_courses = true).
  242. }
  243. }
  244. }
  245. return false;
  246. }
  247. /**
  248. * Reacts on a failed login:
  249. * Displays an explanation with a link to the registration form.
  250. *
  251. * @version 1.0.1
  252. */
  253. function handle_login_failed() {
  254. $message = get_lang('InvalidId');
  255. if (!isset($_GET['error'])) {
  256. if (api_is_self_registration_allowed()) {
  257. $message = get_lang('InvalidForSelfRegistration');
  258. }
  259. } else {
  260. switch ($_GET['error']) {
  261. case '':
  262. if (api_is_self_registration_allowed()) {
  263. $message = get_lang('InvalidForSelfRegistration');
  264. }
  265. break;
  266. case 'account_expired':
  267. $message = get_lang('AccountExpired');
  268. break;
  269. case 'account_inactive':
  270. $message = get_lang('AccountInactive');
  271. break;
  272. case 'user_password_incorrect':
  273. $message = get_lang('InvalidId');
  274. break;
  275. case 'access_url_inactive':
  276. $message = get_lang('AccountURLInactive');
  277. break;
  278. case 'unrecognize_sso_origin':
  279. //$message = get_lang('SSOError');
  280. break;
  281. }
  282. }
  283. return Display::return_message($message, 'error');
  284. }
  285. /**
  286. * retrieves all the courses that the user has already subscribed to
  287. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  288. * @param int $user_id: the id of the user
  289. * @return array an array containing all the information of the courses of the given user
  290. */
  291. function get_courses_of_user($user_id) {
  292. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  293. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  294. // Secondly we select the courses that are in a category (user_course_cat <> 0) and sort these according to the sort of the category
  295. $user_id = intval($user_id);
  296. $sql_select_courses = "SELECT course.code k, course.visual_code vc, course.subscribe subscr, course.unsubscribe unsubscr,
  297. course.title i, course.tutor_name t, course.db_name db, course.directory dir, course_rel_user.status status,
  298. course_rel_user.sort sort, course_rel_user.user_course_cat user_course_cat
  299. FROM $table_course course,
  300. $table_course_user course_rel_user
  301. WHERE course.code = course_rel_user.course_code
  302. AND course_rel_user.user_id = '".$user_id."'
  303. AND course_rel_user.relation_type<>".COURSE_RELATION_TYPE_RRHH."
  304. ORDER BY course_rel_user.sort ASC";
  305. $result = Database::query($sql_select_courses);
  306. $courses = array();
  307. while ($row = Database::fetch_array($result)) {
  308. // We only need the database name of the course.
  309. $courses[$row['k']] = array('db' => $row['db'], 'code' => $row['k'], 'visual_code' => $row['vc'], 'title' => $row['i'], 'directory' => $row['dir'], 'status' => $row['status'], 'tutor' => $row['t'], 'subscribe' => $row['subscr'], 'unsubscribe' => $row['unsubscr'], 'sort' => $row['sort'], 'user_course_category' => $row['user_course_cat']);
  310. }
  311. return $courses;
  312. }
  313. }
  314. $app->get('/', 'UserPortalController::indexAction');
  315. $app->run();
  316. //$app['http_cache']->run();