index.php 41 KB

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