index.php 32 KB

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