index.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.main
  5. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Refactoring
  6. * @todo check the different @todos in this page and really do them
  7. * @todo check if the news management works as expected
  8. */
  9. // Only this script should have this constant defined. This is used to activate the javascript that
  10. // gives the login name automatic focus in header.inc.html.
  11. /** @todo Couldn't this be done using the $HtmlHeadXtra array? */
  12. define('CHAMILO_HOMEPAGE', true);
  13. $language_file = array('courses', 'index');
  14. /* Flag forcing the 'current course' reset, as we're not inside a course anymore. */
  15. // Maybe we should change this into an api function? an example: Coursemanager::unset();
  16. $cidReset = true;
  17. /* Included libraries */
  18. /** @todo Make all the library files consistent, use filename.lib.php and not filename.lib.inc.php. */
  19. require_once 'main/inc/global.inc.php';
  20. require_once api_get_path(LIBRARY_PATH).'course.lib.php';
  21. require_once api_get_path(LIBRARY_PATH).'events.lib.inc.php';
  22. require_once api_get_path(LIBRARY_PATH).'system_announcements.lib.php';
  23. require_once api_get_path(LIBRARY_PATH).'groupmanager.lib.php';
  24. require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
  25. require_once 'main/chat/chat_functions.lib.php';
  26. require_once api_get_path(LIBRARY_PATH).'custompages.lib.php';
  27. $loginFailed = isset($_GET['loginFailed']) ? true : isset($loginFailed);
  28. $setting_show_also_closed_courses = api_get_setting('show_closed_courses') == 'true';
  29. // The section (for the tabs).
  30. $this_section = SECTION_CAMPUS;
  31. unset($_SESSION['this_section']);//for hmtl editor repository
  32. /* Action Handling */
  33. /** @todo Wouldn't it make more sense if this would be done in local.inc.php so that local.inc.php become the only place where authentication is done?
  34. * by doing this you could logout from any page instead of only from index.php. From the moment there is a logout=true in the url you will be logged out
  35. * this can be usefull when you are on an open course and you need to log in to edit something and you immediately want to check how anonymous users
  36. * will see it.
  37. */
  38. $my_user_id = api_get_user_id();
  39. if (!empty($_GET['logout'])) {
  40. logout();
  41. }
  42. /* Table definitions */
  43. $main_course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  44. $main_category_table = Database :: get_main_table(TABLE_MAIN_CATEGORY);
  45. $track_login_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  46. /* Constants and CONFIGURATION parameters */
  47. /** @todo these configuration settings should move to the Chamilo config settings. */
  48. /** Defines wether or not anonymous visitors can see a list of the courses on the Chamilo homepage that are open to the world. */
  49. $_setting['display_courses_to_anonymous_users'] = 'true';
  50. /** @todo Remove this piece of code because this is not used. */
  51. if (isset($_user['user_id'])) {
  52. $nameTools = api_get_setting('siteName');
  53. }
  54. /* LOGIN */
  55. /**
  56. * @todo This piece of code should probably move to local.inc.php where the actual login / logout procedure is handled.
  57. * @todo Consider removing this piece of code because does nothing.
  58. */
  59. if (isset($_GET['submitAuth']) && $_GET['submitAuth'] == 1) {
  60. // nice lie!!!
  61. echo 'Attempted breakin - sysadmins notified.';
  62. session_destroy();
  63. die();
  64. }
  65. // Delete session neccesary for legal terms
  66. if (api_get_setting('allow_terms_conditions') == 'true') {
  67. unset($_SESSION['update_term_and_condition']);
  68. unset($_SESSION['info_current_user']);
  69. }
  70. /**
  71. * @todo This piece of code should probably move to local.inc.php where the actual login procedure is handled.
  72. * @todo Check if this code is used. I think this code is never executed because after clicking the submit button
  73. * the code does the stuff in local.inc.php and then redirects to index.php or user_portal.php depending
  74. * on api_get_setting('page_after_login').
  75. */
  76. if (!empty($_POST['submitAuth'])) {
  77. // The user has been already authenticated, we are now to find the last login of the user.
  78. if (isset ($_user['user_id'])) {
  79. $sql_last_login = "SELECT UNIX_TIMESTAMP(login_date)
  80. FROM $track_login_table
  81. WHERE login_user_id = '".$_user['user_id']."'
  82. ORDER BY login_date DESC LIMIT 1";
  83. $result_last_login = Database::query($sql_last_login);
  84. if (!$result_last_login) {
  85. if (Database::num_rows($result_last_login) > 0) {
  86. $user_last_login_datetime = Database::fetch_array($result_last_login);
  87. $user_last_login_datetime = $user_last_login_datetime[0];
  88. api_session_register('user_last_login_datetime');
  89. }
  90. }
  91. Database::free_result($result_last_login);
  92. //event_login();
  93. if (api_is_platform_admin()) {
  94. // decode all open event informations and fill the track_c_* tables
  95. include api_get_path(LIBRARY_PATH).'stats.lib.inc.php';
  96. decodeOpenInfos();
  97. }
  98. }
  99. } // End login -- if ($_POST['submitAuth'])
  100. else {
  101. // Only if login form was not sent because if the form is sent the user was already on the page.
  102. event_open();
  103. }
  104. //checks user status and redirect him through custom page if setting is enabled
  105. $user_status = (isset($_SESSION['_user']['user_id'])) ? $_user['status'] : null;
  106. if ( $user_status == STUDENT && !api_get_setting('student_page_after_login') == '' ){
  107. $redirect_url = html_entity_decode(api_get_setting('student_page_after_login'));
  108. if ($redirect_url[0] == "/") {
  109. $redirect_url = substr(api_get_path(WEB_PATH), 0, -1).$redirect_url;
  110. }
  111. header('Location: '.$redirect_url);
  112. exit();
  113. }
  114. if ( $user_status == COURSEMANAGER && !api_get_setting('teacher_page_after_login') == '' ){
  115. $redirect_url = html_entity_decode(api_get_setting('teacher_page_after_login'));
  116. if ($redirect_url[0] == "/") {
  117. $redirect_url = substr(api_get_path(WEB_PATH), 0, -1).$redirect_url;
  118. }
  119. header('Location: '.$redirect_url);
  120. exit();
  121. }
  122. if ( $user_status == DRH && !api_get_setting('DRH_page_after_login') == '' ){
  123. $redirect_url = html_entity_decode(api_get_setting('DRH_page_after_login'));
  124. if ($redirect_url[0] == "/") {
  125. $redirect_url = substr(api_get_path(WEB_PATH), 0, -1).$redirect_url;
  126. }
  127. header('Location: '.$redirect_url);
  128. exit();
  129. }
  130. if ( $user_status == SESSIONADMIN && !api_get_setting('sessionadmin_page_after_login') == '' ){
  131. $redirect_url = html_entity_decode(api_get_setting('sessionadmin_page_after_login'));
  132. if ($redirect_url[0] == "/") {
  133. $redirect_url = substr(api_get_path(WEB_PATH), 0, -1).$redirect_url;
  134. }
  135. header('Location: '.$redirect_url);
  136. exit();
  137. }
  138. // Custom pages
  139. if (api_get_setting('use_custom_pages') == 'true') {
  140. if (api_get_user_id()) {
  141. CustomPages::displayPage('index-logged');
  142. }
  143. else {
  144. CustomPages::displayPage('index-unlogged');
  145. }
  146. }
  147. // The header.
  148. /*$header_title = get_lang('Homepage');
  149. //$sitename = api_get_setting('siteName');
  150. if (!api_get_user_id()) {
  151. $header_title = null;
  152. }*/
  153. $header_title = null;
  154. if (!api_is_anonymous()) {
  155. $header_title = " ";
  156. }
  157. Display::display_header($header_title);
  158. /* MAIN CODE */
  159. echo '<div class="maincontent" id="content">';
  160. // Plugins for loginpage_main AND campushomepage_main.
  161. if (!api_get_user_id()) {
  162. api_plugin('loginpage_main');
  163. } else {
  164. api_plugin('campushomepage_main');
  165. }
  166. $home = 'home/';
  167. if ($_configuration['multiple_access_urls']) {
  168. $access_url_id = api_get_current_access_url_id();
  169. if ($access_url_id != -1){
  170. $url_info = api_get_access_url($access_url_id);
  171. $url = api_remove_trailing_slash(preg_replace('/https?:\/\//i', '', $url_info['url']));
  172. $clean_url = replace_dangerous_char($url);
  173. $clean_url = str_replace('/', '-', $clean_url);
  174. $clean_url .= '/';
  175. $home_old = 'home/';
  176. $home = 'home/'.$clean_url;
  177. }
  178. }
  179. // Including the page for the news
  180. $page_included = false;
  181. if (!empty($_GET['include']) && preg_match('/^[a-zA-Z0-9_-]*\.html$/', $_GET['include'])) {
  182. $open = @(string)file_get_contents(api_get_path(SYS_PATH).$home.$_GET['include']);
  183. $open = api_to_system_encoding($open, api_detect_encoding(strip_tags($open)));
  184. echo $open;
  185. $page_included = true;
  186. } else {
  187. if (!empty($_SESSION['user_language_choice'])) {
  188. $user_selected_language = $_SESSION['user_language_choice'];
  189. } elseif (!empty($_SESSION['_user']['language'])) {
  190. $user_selected_language = $_SESSION['_user']['language'];
  191. } else {
  192. $user_selected_language = api_get_setting('platformLanguage');
  193. }
  194. if (!file_exists($home.'home_news_'.$user_selected_language.'.html')) {
  195. if (file_exists($home.'home_top.html')) {
  196. $home_top_temp = file($home.'home_top.html');
  197. } else {
  198. $home_top_temp = file($home_old.'home_top.html');
  199. }
  200. $home_top_temp = implode('', $home_top_temp);
  201. } else {
  202. if (file_exists($home.'home_top_'.$user_selected_language.'.html')) {
  203. $home_top_temp = file_get_contents($home.'home_top_'.$user_selected_language.'.html');
  204. } else {
  205. $home_top_temp = file_get_contents($home.'home_top.html');
  206. }
  207. }
  208. if (trim($home_top_temp) == '' && api_is_platform_admin()) {
  209. $home_top_temp = get_lang('PortalHomepageDefaultIntroduction');
  210. }
  211. $open = str_replace('{rel_path}', api_get_path(REL_PATH), $home_top_temp);
  212. $open = api_to_system_encoding($open, api_detect_encoding(strip_tags($open)));
  213. echo $open;
  214. }
  215. // Display courses and category list.
  216. if (!$page_included) {
  217. // Display System announcements
  218. $announcement = isset($_GET['announcement']) ? $_GET['announcement'] : -1;
  219. $announcement = intval($announcement);
  220. if (isset($_user['user_id'])) {
  221. $visibility = api_is_allowed_to_create_course() ? VISIBLE_TEACHER : VISIBLE_STUDENT;
  222. SystemAnnouncementManager :: display_announcements($visibility, $announcement);
  223. } else {
  224. SystemAnnouncementManager :: display_announcements(VISIBLE_GUEST, $announcement);
  225. }
  226. if (api_get_setting('display_categories_on_homepage') == 'true') {
  227. echo '<div class="home_cats">';
  228. display_anonymous_course_list();
  229. echo '</div>';
  230. }
  231. }
  232. echo '</div>';
  233. echo '<div id="menu-wrapper">';
  234. if (!api_is_anonymous()) {
  235. // @todo move all this in a class/function
  236. //Always show the user image
  237. $img_array = UserManager::get_user_picture_path_by_id(api_get_user_id(), 'web', true, true);
  238. $no_image = false;
  239. if ($img_array['file'] == 'unknown.jpg') {
  240. $no_image = true;
  241. }
  242. $img_array = UserManager::get_picture_user(api_get_user_id(), $img_array['file'], 50, USER_IMAGE_SIZE_MEDIUM, ' width="90" height="90" ');
  243. $profile_content .='<div id="social_widget">';
  244. $profile_content .= '<div id="social_widget_image">';
  245. if (api_get_setting('allow_social_tool') == 'true') {
  246. if (!$no_image) {
  247. $profile_content .='<a href="'.api_get_path(WEB_PATH).'main/social/home.php"><img src="'.$img_array['file'].'" '.$img_array['style'].' border="1"></a>';
  248. } else {
  249. $profile_content .='<a href="'.api_get_path(WEB_PATH).'main/auth/profile.php"><img title="'.get_lang('EditProfile').'" src="'.$img_array['file'].'" '.$img_array['style'].' border="1"></a>';
  250. }
  251. } else {
  252. $profile_content .='<a href="'.api_get_path(WEB_PATH).'main/auth/profile.php"><img title="'.get_lang('EditProfile').'" src="'.$img_array['file'].'" '.$img_array['style'].' border="1"></a>';
  253. }
  254. $profile_content .= ' </div></div>';
  255. if (api_get_setting('allow_message_tool') == 'true') {
  256. require_once api_get_path(LIBRARY_PATH).'message.lib.php';
  257. require_once api_get_path(LIBRARY_PATH).'social.lib.php';
  258. require_once api_get_path(LIBRARY_PATH).'group_portal_manager.lib.php';
  259. // New messages.
  260. $number_of_new_messages = MessageManager::get_new_messages();
  261. // New contact invitations.
  262. $number_of_new_messages_of_friend = SocialManager::get_message_number_invitation_by_user_id(api_get_user_id());
  263. // New group invitations sent by a moderator.
  264. $group_pending_invitations = GroupPortalManager::get_groups_by_user(api_get_user_id(), GROUP_USER_PERMISSION_PENDING_INVITATION, false);
  265. $group_pending_invitations = count($group_pending_invitations);
  266. $total_invitations = $number_of_new_messages_of_friend + $group_pending_invitations;
  267. $cant_msg = '';
  268. if ($number_of_new_messages > 0) {
  269. $cant_msg = ' ('.$number_of_new_messages.')';
  270. }
  271. $profile_content .= '<div class="clear"></div>';
  272. $profile_content .= '<div class="message-content"><ul class="menulist">';
  273. $link = '';
  274. if (api_get_setting('allow_social_tool') == 'true') {
  275. $link = '?f=social';
  276. }
  277. $profile_content .= '<li><a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php'.$link.'" class="message-body">'.get_lang('Inbox').$cant_msg.' </a></li>';
  278. $profile_content .= '<li><a href="'.api_get_path(WEB_PATH).'main/messages/new_message.php'.$link.'" class="message-body">'.get_lang('Compose').' </a></li>';
  279. if (api_get_setting('allow_social_tool') == 'true') {
  280. if ($total_invitations == 0) {
  281. $total_invitations = '';
  282. } else {
  283. $total_invitations = ' ('.$total_invitations.')';
  284. }
  285. $profile_content .= '<li><a href="'.api_get_path(WEB_PATH).'main/social/invitations.php" class="message-body">'.get_lang('PendingInvitations').' '.$total_invitations.' </a></li>';
  286. }
  287. $profile_content .= '</ul>';
  288. $profile_content .= '</div>';
  289. }
  290. //Profile content
  291. echo show_right_block(get_lang('Profile'), $profile_content);
  292. }
  293. // Display right menu: language form, login section + useful weblinks.
  294. display_anonymous_right_menu();
  295. echo '</div>';
  296. /* Footer */
  297. Display :: display_footer();
  298. /* Functions */
  299. /**
  300. * This function handles the logout and is called whenever there is a $_GET['logout']
  301. *
  302. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  303. */
  304. function logout() {
  305. global $_configuration, $extAuthSource;
  306. // Variable initialisation.
  307. $query_string = '';
  308. if (!empty($_SESSION['user_language_choice'])) {
  309. $query_string = '?language='.$_SESSION['user_language_choice'];
  310. }
  311. // Database table definition.
  312. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  313. // Selecting the last login of the user.
  314. $uid = intval($_GET['uid']);
  315. $sql_last_connection = "SELECT login_id, login_date FROM $tbl_track_login WHERE login_user_id='$uid' ORDER BY login_date DESC LIMIT 0,1";
  316. $q_last_connection = Database::query($sql_last_connection);
  317. if (Database::num_rows($q_last_connection) > 0) {
  318. $i_id_last_connection = Database::result($q_last_connection, 0, 'login_id');
  319. }
  320. if (!isset($_SESSION['login_as'])) {
  321. $current_date = date('Y-m-d H:i:s', time());
  322. $s_sql_update_logout_date = "UPDATE $tbl_track_login SET logout_date='".$current_date."' WHERE login_id='$i_id_last_connection'";
  323. Database::query($s_sql_update_logout_date);
  324. }
  325. LoginDelete($uid); // From inc/lib/online.inc.php - removes the "online" status.
  326. // The following code enables the use of an external logout function.
  327. // Example: define a $extAuthSource['ldap']['logout'] = 'file.php' in configuration.php.
  328. // Then a function called ldap_logout() inside that file
  329. // (using *authent_name*_logout as the function name) and the following code
  330. // will find and execute it.
  331. $uinfo = api_get_user_info($uid);
  332. if (($uinfo['auth_source'] != PLATFORM_AUTH_SOURCE) && is_array($extAuthSource)) {
  333. if (is_array($extAuthSource[$uinfo['auth_source']])) {
  334. $subarray = $extAuthSource[$uinfo['auth_source']];
  335. if (!empty($subarray['logout']) && file_exists($subarray['logout'])) {
  336. include_once ($subarray['logout']);
  337. $logout_function = $uinfo['auth_source'].'_logout';
  338. if (function_exists($logout_function)) {
  339. $logout_function($uinfo);
  340. }
  341. }
  342. }
  343. }
  344. exit_of_chat($uid);
  345. api_session_destroy();
  346. header("Location: index.php$query_string");
  347. exit();
  348. }
  349. /**
  350. * This function checks if there are courses that are open to the world in the platform course categories (=faculties)
  351. *
  352. * @param unknown_type $category
  353. * @return boolean
  354. */
  355. function category_has_open_courses($category) {
  356. global $setting_show_also_closed_courses;
  357. $user_identified = (api_get_user_id() > 0 && !api_is_anonymous());
  358. $main_course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  359. $sql_query = "SELECT * FROM $main_course_table WHERE category_code='$category'";
  360. $sql_result = Database::query($sql_query);
  361. while ($course = Database::fetch_array($sql_result)) {
  362. if (!$setting_show_also_closed_courses) {
  363. if ((api_get_user_id() > 0
  364. && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM)
  365. || ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD)) {
  366. return true; //at least one open course
  367. }
  368. } else {
  369. if (isset($course['visibility'])) {
  370. return true; // At least one course (it does not matter weither it's open or not because $setting_show_also_closed_courses = true).
  371. }
  372. }
  373. }
  374. return false;
  375. }
  376. /**
  377. * Display create course link
  378. */
  379. function display_create_course_link() {
  380. echo '<li><a href="main/create_course/add_course.php">'.
  381. (api_get_setting('course_validation') == 'true' ? get_lang('CreateCourseRequest') : get_lang('CourseCreate')).'</a></li>';
  382. }
  383. /**
  384. * Display edit course list links
  385. */
  386. function display_edit_course_list_links() {
  387. echo '<li><a href="main/auth/courses.php">'.get_lang('CourseManagement').'</a></li>';
  388. }
  389. /**
  390. * Display dashboard link
  391. */
  392. function display_dashboard_link() {
  393. echo '<li><a href="main/dashboard/index.php">'.get_lang('Dashboard').'</a></li>';
  394. }
  395. /**
  396. * Displays the right-hand menu for anonymous users:
  397. * login form, useful links, help section
  398. * Warning: function defines globals
  399. * @version 1.0.1
  400. * @todo does $_plugins need to be global?
  401. */
  402. function display_anonymous_right_menu() {
  403. global $loginFailed, $_plugins, $_user, $menu_navigation, $home, $home_old;
  404. $platformLanguage = api_get_setting('platformLanguage');
  405. $sys_path = api_get_path(SYS_PATH);
  406. $user_selected_language = api_get_interface_language();
  407. echo '<div class="menu" id="menu">';
  408. if (!($_user['user_id']) || api_is_anonymous($_user['user_id']) ) { // Only display if the user isn't logged in.
  409. api_display_language_form(true);
  410. echo '<br />';
  411. display_login_form();
  412. if ($loginFailed) {
  413. echo '<br />';
  414. handle_login_failed();
  415. }
  416. if (api_get_setting('allow_lostpassword') == 'true' || api_get_setting('allow_registration') == 'true') {
  417. echo '<div class="menusection"><span class="menusectioncaption">'.get_lang('MenuUser').'</span><ul class="menulist">';
  418. if (api_get_setting('allow_registration') != 'false') {
  419. echo '<li><a href="main/auth/inscription.php">'.get_lang('Reg').'</a></li>';
  420. }
  421. if (api_get_setting('allow_lostpassword') == 'true') {
  422. display_lost_password_info();
  423. }
  424. echo '</ul></div>';
  425. }
  426. if (api_number_of_plugins('loginpage_menu') > 0) {
  427. echo '<div class="note" style="background: none">';
  428. api_plugin('loginpage_menu');
  429. echo '</div>';
  430. }
  431. }
  432. // My Account section.
  433. if (isset($_SESSION['_user']['user_id']) && $_SESSION['_user']['user_id'] != 0) {
  434. // tabs that are deactivated are added here
  435. $show_menu = false;
  436. $show_create_link = false;
  437. $show_course_link = false;
  438. $display_add_course_link = api_is_allowed_to_create_course() && ($_SESSION['studentview'] != 'studentenview');
  439. if ($display_add_course_link) {
  440. //display_create_course_link();
  441. $show_menu = true;
  442. $show_create_link = true;
  443. }
  444. if (api_is_platform_admin() || api_is_course_admin() || api_is_allowed_to_create_course()) {
  445. $show_menu = true;
  446. $show_course_link = true;
  447. } else {
  448. if (api_get_setting('allow_students_to_browse_courses') == 'true') {
  449. $show_menu = true;
  450. $show_course_link = true;
  451. }
  452. }
  453. if ($show_menu) {
  454. echo '<div class="menusection">';
  455. echo '<span class="menusectioncaption">'.get_lang('MenuUser').'</span>';
  456. echo '<ul class="menulist">';
  457. if ($show_create_link) {
  458. display_create_course_link();
  459. }
  460. if ($show_course_link) {
  461. if (!api_is_drh() && !api_is_session_admin()) {
  462. display_edit_course_list_links();
  463. } else {
  464. display_dashboard_link();
  465. }
  466. }
  467. echo '</ul></div>';
  468. }
  469. }
  470. echo '</div>';
  471. // Notice
  472. $home_notice = @(string)file_get_contents($sys_path.$home.'home_notice_'.$user_selected_language.'.html');
  473. if (empty($home_notice)) {
  474. $home_notice = @(string)file_get_contents($sys_path.$home.'home_notice.html');
  475. }
  476. if (!empty($home_notice)) {
  477. $home_notice = api_to_system_encoding($home_notice, api_detect_encoding(strip_tags($home_notice)));
  478. echo show_right_block('', $home_notice, 'note');
  479. }
  480. if (isset($_SESSION['_user']['user_id']) && $_SESSION['_user']['user_id'] != 0) {
  481. // Deleting the myprofile link.
  482. if (api_get_setting('allow_social_tool') == 'true') {
  483. unset($menu_navigation['myprofile']);
  484. }
  485. if (!empty($menu_navigation)) {
  486. $content = '<ul class="menulist">';
  487. foreach ($menu_navigation as $section => $navigation_info) {
  488. $current = $section == $GLOBALS['this_section'] ? ' id="current"' : '';
  489. $content .='<li'.$current.'><a href="'.$navigation_info['url'].'" target="_self">'.$navigation_info['title'].'</a></li>';
  490. }
  491. $content .= '</ul>';
  492. echo show_right_block(get_lang('MainNavigation'), $content);
  493. }
  494. }
  495. // Help section.
  496. /* Hide right menu "general" and other parts on anonymous right menu. */
  497. if (!isset($user_selected_language)) {
  498. $user_selected_language = $platformLanguage;
  499. }
  500. $home_menu = @(string)file_get_contents($sys_path.$home.'home_menu_'.$user_selected_language.'.html');
  501. if (!empty($home_menu)) {
  502. $home_menu_content .= '<ul class="menulist">';
  503. $home_menu_content .= api_to_system_encoding($home_menu, api_detect_encoding(strip_tags($home_menu)));
  504. $home_menu_content .= '</ul>';
  505. echo show_right_block(get_lang('MenuGeneral'), $home_menu_content);
  506. }
  507. //Plugin
  508. if ($_user['user_id'] && api_number_of_plugins('campushomepage_menu') > 0) {
  509. ob_start();
  510. api_plugin('campushomepage_menu');
  511. $plugin_content = ob_get_contents();
  512. ob_end_clean();
  513. echo show_right_block('', $plugin_content);
  514. }
  515. }
  516. /**
  517. * Reacts on a failed login:
  518. * Displays an explanation with a link to the registration form.
  519. *
  520. * @version 1.0.1
  521. */
  522. function handle_login_failed() {
  523. if (!isset($_GET['error'])) {
  524. $message = get_lang('InvalidId');
  525. if (api_is_self_registration_allowed()) {
  526. $message = get_lang('InvalidForSelfRegistration');
  527. }
  528. } else {
  529. switch ($_GET['error']) {
  530. case '':
  531. $message = get_lang('InvalidId');
  532. if (api_is_self_registration_allowed()) {
  533. $message = get_lang('InvalidForSelfRegistration');
  534. }
  535. break;
  536. case 'account_expired':
  537. $message = get_lang('AccountExpired');
  538. break;
  539. case 'account_inactive':
  540. $message = get_lang('AccountInactive');
  541. break;
  542. case 'user_password_incorrect':
  543. $message = get_lang('InvalidId');
  544. break;
  545. case 'access_url_inactive':
  546. $message = get_lang('AccountURLInactive');
  547. break;
  548. }
  549. }
  550. echo '<div id="login_fail">'.$message.'</div>';
  551. }
  552. /**
  553. * Adds a form to let users login
  554. * @version 1.1
  555. */
  556. function display_login_form() {
  557. $form = new FormValidator('formLogin');
  558. $form->addElement('text', 'login', get_lang('UserName'), array('size' => 17));
  559. $form->addElement('password', 'password', get_lang('Pass'), array('size' => 17));
  560. $form->addElement('style_submit_button','submitAuth', get_lang('LoginEnter'), array('class' => 'login'));
  561. $renderer =& $form->defaultRenderer();
  562. $renderer->setElementTemplate('<div><label>{label}</label></div><div>{element}</div>');
  563. $form->display();
  564. if (api_get_setting('openid_authentication') == 'true') {
  565. include_once 'main/auth/openid/login.php';
  566. echo '<div>'.openid_form().'</div>';
  567. }
  568. }
  569. /**
  570. * Displays a link to the lost password section
  571. */
  572. function display_lost_password_info() {
  573. echo '<li><a href="main/auth/lostPassword.php">'.get_lang('LostPassword').'</a></li>';
  574. }
  575. /**
  576. * Display list of courses in a category.
  577. * (for anonymous users)
  578. *
  579. * @version 1.1
  580. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University - refactoring and code cleaning
  581. */
  582. function display_anonymous_course_list() {
  583. $ctok = $_SESSION['sec_token'];
  584. $stok = Security::get_token();
  585. // Initialization.
  586. $user_identified = (api_get_user_id() > 0 && !api_is_anonymous());
  587. $web_course_path = api_get_path(WEB_COURSE_PATH);
  588. $category = Database::escape_string($_GET['category']);
  589. global $setting_show_also_closed_courses;
  590. // Database table definitions.
  591. $main_course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  592. $main_category_table = Database :: get_main_table(TABLE_MAIN_CATEGORY);
  593. $platformLanguage = api_get_setting('platformLanguage');
  594. // Get list of courses in category $category.
  595. $sql_get_course_list = "SELECT * FROM $main_course_table cours
  596. WHERE category_code = '".Database::escape_string($_GET['category'])."'
  597. ORDER BY title, UPPER(visual_code)";
  598. // Showing only the courses of the current access_url_id.
  599. global $_configuration;
  600. if ($_configuration['multiple_access_urls']) {
  601. $url_access_id = api_get_current_access_url_id();
  602. if ($url_access_id != -1) {
  603. $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  604. $sql_get_course_list = "SELECT * FROM $main_course_table as course INNER JOIN $tbl_url_rel_course as url_rel_course
  605. ON (url_rel_course.course_code=course.code)
  606. WHERE access_url_id = $url_access_id AND category_code = '".Database::escape_string($_GET['category'])."' ORDER BY title, UPPER(visual_code)";
  607. }
  608. }
  609. // Removed: AND cours.visibility='".COURSE_VISIBILITY_OPEN_WORLD."'
  610. $sql_result_courses = Database::query($sql_get_course_list);
  611. while ($course_result = Database::fetch_array($sql_result_courses)) {
  612. $course_list[] = $course_result;
  613. }
  614. $platform_visible_courses = '';
  615. // $setting_show_also_closed_courses
  616. if ($user_identified) {
  617. if ($setting_show_also_closed_courses) {
  618. $platform_visible_courses = '';
  619. } else {
  620. $platform_visible_courses = " AND (t3.visibility='".COURSE_VISIBILITY_OPEN_WORLD."' OR t3.visibility='".COURSE_VISIBILITY_OPEN_PLATFORM."' )";
  621. }
  622. } else {
  623. if ($setting_show_also_closed_courses) {
  624. $platform_visible_courses = '';
  625. } else {
  626. $platform_visible_courses = " AND (t3.visibility='".COURSE_VISIBILITY_OPEN_WORLD."' )";
  627. }
  628. }
  629. $sqlGetSubCatList = "
  630. SELECT t1.name,t1.code,t1.parent_id,t1.children_count,COUNT(DISTINCT t3.code) AS nbCourse
  631. FROM $main_category_table t1
  632. LEFT JOIN $main_category_table t2 ON t1.code=t2.parent_id
  633. LEFT JOIN $main_course_table t3 ON (t3.category_code=t1.code $platform_visible_courses)
  634. WHERE t1.parent_id ". (empty ($category) ? "IS NULL" : "='$category'")."
  635. GROUP BY t1.name,t1.code,t1.parent_id,t1.children_count ORDER BY t1.tree_pos, t1.name";
  636. // Showing only the category of courses of the current access_url_id.
  637. global $_configuration;
  638. if ($_configuration['multiple_access_urls']) {
  639. $url_access_id = api_get_current_access_url_id();
  640. if ($url_access_id != -1) {
  641. $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  642. $sqlGetSubCatList = "
  643. SELECT t1.name,t1.code,t1.parent_id,t1.children_count,COUNT(DISTINCT t3.code) AS nbCourse
  644. FROM $main_category_table t1
  645. LEFT JOIN $main_category_table t2 ON t1.code=t2.parent_id
  646. LEFT JOIN $main_course_table t3 ON (t3.category_code=t1.code $platform_visible_courses)
  647. INNER JOIN $tbl_url_rel_course as url_rel_course
  648. ON (url_rel_course.course_code=t3.code)
  649. WHERE access_url_id = $url_access_id AND t1.parent_id ".(empty($category) ? "IS NULL" : "='$category'")."
  650. GROUP BY t1.name,t1.code,t1.parent_id,t1.children_count ORDER BY t1.tree_pos, t1.name";
  651. }
  652. }
  653. $resCats = Database::query($sqlGetSubCatList);
  654. $thereIsSubCat = false;
  655. if (Database::num_rows($resCats) > 0) {
  656. $htmlListCat = '<h4 style="margin-top: 0px;">'.get_lang('CatList').'</h4><ul>';
  657. while ($catLine = Database::fetch_array($resCats)) {
  658. if ($catLine['code'] != $category) {
  659. $category_has_open_courses = category_has_open_courses($catLine['code']);
  660. if ($category_has_open_courses) {
  661. // The category contains courses accessible to anonymous visitors.
  662. $htmlListCat .= '<li>';
  663. $htmlListCat .= '<a href="'.api_get_self().'?category='.$catLine['code'].'">'.$catLine['name'].'</a>';
  664. if (api_get_setting('show_number_of_courses') == 'true') {
  665. $htmlListCat .= ' ('.$catLine['nbCourse'].' '.get_lang('Courses').')';
  666. }
  667. $htmlListCat .= "</li>\n";
  668. $thereIsSubCat = true;
  669. } elseif ($catLine['children_count'] > 0) {
  670. // The category has children, subcategories.
  671. $htmlListCat .= '<li>';
  672. $htmlListCat .= '<a href="'.api_get_self().'?category='.$catLine['code'].'">'.$catLine['name'].'</a>';
  673. $htmlListCat .= "</li>\n";
  674. $thereIsSubCat = true;
  675. }
  676. /* End changed code to eliminate the (0 courses) after empty categories. */
  677. elseif (api_get_setting('show_empty_course_categories') == 'true') {
  678. $htmlListCat .= '<li>';
  679. $htmlListCat .= $catLine['name'];
  680. $htmlListCat .= "</li>\n";
  681. $thereIsSubCat = true;
  682. } // Else don't set thereIsSubCat to true to avoid printing things if not requested.
  683. } else {
  684. $htmlTitre = '<p>';
  685. if (api_get_setting('show_back_link_on_top_of_tree') == 'true') {
  686. $htmlTitre .= '<a href="'.api_get_self().'">&lt;&lt; '.get_lang('BackToHomePage').'</a>';
  687. }
  688. if (!is_null($catLine['parent_id']) || (api_get_setting('show_back_link_on_top_of_tree') != 'true' && !is_null($catLine['code']))) {
  689. $htmlTitre .= '<a href="'.api_get_self().'?category='.$catLine['parent_id'].'">&lt;&lt; '.get_lang('Up').'</a>';
  690. }
  691. $htmlTitre .= "</p>\n";
  692. if ($category != "" && !is_null($catLine['code'])) {
  693. $htmlTitre .= '<h3>'.$catLine['name']."</h3>\n";
  694. } else {
  695. $htmlTitre .= '<h3>'.get_lang('Categories')."</h3>\n";
  696. }
  697. }
  698. }
  699. $htmlListCat .= "</ul>\n";
  700. }
  701. echo $htmlTitre;
  702. if ($thereIsSubCat) {
  703. echo $htmlListCat;
  704. }
  705. while ($categoryName = Database::fetch_array($resCats)) {
  706. echo '<h3>', $categoryName['name'], "</h3>\n";
  707. }
  708. $numrows = Database::num_rows($sql_result_courses);
  709. $courses_list_string = '';
  710. $courses_shown = 0;
  711. if ($numrows > 0) {
  712. if ($thereIsSubCat) {
  713. $courses_list_string .= "<hr size=\"1\" noshade=\"noshade\">\n";
  714. }
  715. $courses_list_string .= '<h4 style="margin-top: 0px;">'.get_lang('CourseList')."</h4>\n<ul>\n";
  716. if (api_get_user_id()) {
  717. $courses_of_user = get_courses_of_user(api_get_user_id());
  718. }
  719. foreach ($course_list as $course) {
  720. // $setting_show_also_closed_courses
  721. if (!$setting_show_also_closed_courses) {
  722. // If we do not show the closed courses
  723. // we only show the courses that are open to the world (to everybody)
  724. // and the courses that are open to the platform (if the current user is a registered user.
  725. if( ($user_identified && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM) || ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD)) {
  726. $courses_shown++;
  727. $courses_list_string .= "<li>\n";
  728. $courses_list_string .= '<a href="'.$web_course_path.$course['directory'].'/">'.$course['title'].'</a><br />';
  729. if (api_get_setting('display_coursecode_in_courselist') == 'true') {
  730. $courses_list_string .= $course['visual_code'];
  731. }
  732. if (api_get_setting('display_coursecode_in_courselist') == 'true' && api_get_setting('display_teacher_in_courselist') == 'true') {
  733. $courses_list_string .= ' - ';
  734. }
  735. if (api_get_setting('display_teacher_in_courselist') == 'true') {
  736. $courses_list_string .= $course['tutor_name'];
  737. }
  738. if (api_get_setting('show_different_course_language') == 'true' && $course['course_language'] != api_get_setting('platformLanguage')) {
  739. $courses_list_string .= ' - '.$course['course_language'];
  740. }
  741. $courses_list_string .= "</li>\n";
  742. }
  743. }
  744. // We DO show the closed courses.
  745. // The course is accessible if (link to the course homepage):
  746. // 1. the course is open to the world (doesn't matter if the user is logged in or not): $course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD);
  747. // 2. the user is logged in and the course is open to the world or open to the platform: ($user_identified && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM);
  748. // 3. the user is logged in and the user is subscribed to the course and the course visibility is not COURSE_VISIBILITY_CLOSED;
  749. // 4. the user is logged in and the user is course admin of te course (regardless of the course visibility setting);
  750. // 5. the user is the platform admin api_is_platform_admin().
  751. //
  752. else {
  753. $courses_shown++;
  754. $courses_list_string .= "<li>\n";
  755. if ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD
  756. || ($user_identified && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM)
  757. || ($user_identified && key_exists($course['code'], $courses_of_user) && $course['visibility'] != COURSE_VISIBILITY_CLOSED)
  758. || $courses_of_user[$course['code']]['status'] == '1'
  759. || api_is_platform_admin()) {
  760. $courses_list_string .= '<a href="'.$web_course_path.$course['directory'].'/">';
  761. }
  762. $courses_list_string .= $course['title'];
  763. if ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD
  764. || ($user_identified && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM)
  765. || ($user_identified && key_exists($course['code'], $courses_of_user) && $course['visibility'] != COURSE_VISIBILITY_CLOSED)
  766. || $courses_of_user[$course['code']]['status'] == '1'
  767. || api_is_platform_admin()) {
  768. $courses_list_string .= '</a><br />';
  769. }
  770. if (api_get_setting('display_coursecode_in_courselist') == 'true') {
  771. $courses_list_string .= ' '.$course['visual_code'];
  772. }
  773. if (api_get_setting('display_coursecode_in_courselist') == 'true' && api_get_setting('display_teacher_in_courselist') == 'true') {
  774. $courses_list_string .= ' - ';
  775. }
  776. if (api_get_setting('display_teacher_in_courselist') == 'true') {
  777. $courses_list_string .= $course['tutor_name'];
  778. }
  779. if (api_get_setting('show_different_course_language') == 'true' && $course['course_language'] != api_get_setting('platformLanguage')) {
  780. $courses_list_string .= ' - '.$course['course_language'];
  781. }
  782. if (api_get_setting('show_different_course_language') == 'true' && $course['course_language'] != api_get_setting('platformLanguage')) {
  783. $courses_list_string .= ' - '.$course['course_language'];
  784. }
  785. // We display a subscription link if:
  786. // 1. it is allowed to register for the course and if the course is not already in the courselist of the user and if the user is identiefied
  787. // 2.
  788. if ($user_identified && !key_exists($course['code'], $courses_of_user)) {
  789. if ($course['subscribe'] == '1') {
  790. $courses_list_string .= '<form action="main/auth/courses.php?action=subscribe&category='.Security::remove_XSS($_GET['category']).'" method="post">';
  791. $courses_list_string .= '<input type="hidden" name="sec_token" value="'.$stok.'">';
  792. $courses_list_string .= '<input type="hidden" name="subscribe" value="'.$course['code'].'" />';
  793. $courses_list_string .= '<input type="image" name="unsub" src="main/img/enroll.gif" alt="'.get_lang('Subscribe').'" />'.get_lang('Subscribe').'</form>';
  794. } else {
  795. $courses_list_string .= '<br />'.get_lang('SubscribingNotAllowed');
  796. }
  797. }
  798. $courses_list_string .= "</li>\n";
  799. }
  800. }
  801. $courses_list_string .= "</ul>\n";
  802. } else {
  803. //echo '<blockquote>', get_lang('_No_course_publicly_available'), "</blockquote>\n";
  804. }
  805. if ($courses_shown > 0) { // Only display the list of courses and categories if there was more than
  806. // 0 courses visible to the world (we're in the anonymous list here).
  807. echo $courses_list_string;
  808. }
  809. if ($category != '') {
  810. echo '<p><a href="'.api_get_self().'"> ', Display :: return_icon('back.png', get_lang('BackToHomePage')), get_lang('BackToHomePage'), '</a></p>', "\n";
  811. }
  812. }
  813. /**
  814. * retrieves all the courses that the user has already subscribed to
  815. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  816. * @param int $user_id: the id of the user
  817. * @return array an array containing all the information of the courses of the given user
  818. */
  819. function get_courses_of_user($user_id) {
  820. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  821. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  822. // Secondly we select the courses that are in a category (user_course_cat <> 0) and sort these according to the sort of the category
  823. $user_id = intval($user_id);
  824. $sql_select_courses = "SELECT course.code k, course.visual_code vc, course.subscribe subscr, course.unsubscribe unsubscr,
  825. course.title i, course.tutor_name t, course.db_name db, course.directory dir, course_rel_user.status status,
  826. course_rel_user.sort sort, course_rel_user.user_course_cat user_course_cat
  827. FROM $table_course course,
  828. $table_course_user course_rel_user
  829. WHERE course.code = course_rel_user.course_code
  830. AND course_rel_user.user_id = '".$user_id."'
  831. AND course_rel_user.relation_type<>".COURSE_RELATION_TYPE_RRHH."
  832. ORDER BY course_rel_user.sort ASC";
  833. $result = Database::query($sql_select_courses);
  834. $courses = array();
  835. while ($row = Database::fetch_array($result)) {
  836. // We only need the database name of the course.
  837. $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']);
  838. }
  839. return $courses;
  840. }
  841. function show_right_block($title, $content, $class = '') {
  842. $html = '';
  843. $html.= '<div id="menu" class="menu">';
  844. $html.= '<div class="menusection '.$class.' ">';
  845. if (!empty($title)) {
  846. $html.= '<span class="menusectioncaption">'.$title.'</span>';
  847. }
  848. $html.= $content;
  849. $html.= '</div>';
  850. $html.= '</div>';
  851. return $html;
  852. }