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