index.php 36 KB

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