index.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. <?php // $Id: index.php 16947 2008-11-26 14:04:43Z iflorespaz $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos SPRL
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) various contributors
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  17. Mail: info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. * @package dokeos.main
  22. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Refactoring
  23. * @version $Id: index.php 16947 2008-11-26 14:04:43Z iflorespaz $
  24. * @todo check the different @todos in this page and really do them
  25. * @todo check if the news management works as expected
  26. */
  27. // only this script should have this constant defined. This is used to activate the javascript that
  28. // gives the login name automatic focus in header.inc.html.
  29. /** @todo Couldn't this be done using the $HtmlHeadXtra array? */
  30. define('DOKEOS_HOMEPAGE', true);
  31. // the language file
  32. $language_file = array ('courses', 'index');
  33. /* Flag forcing the 'current course' reset, as we're not inside a course anymore */
  34. // maybe we should change this into an api function? an example: Coursemanager::unset();
  35. $cidReset = true;
  36. /*
  37. -----------------------------------------------------------
  38. Included libraries
  39. -----------------------------------------------------------
  40. */
  41. /** @todo make all the library files consistent, use filename.lib.php and not filename.lib.inc.php */
  42. require_once ('main/inc/global.inc.php');
  43. include_once (api_get_path(LIBRARY_PATH).'course.lib.php');
  44. include_once (api_get_path(LIBRARY_PATH).'debug.lib.inc.php');
  45. include_once (api_get_path(LIBRARY_PATH).'events.lib.inc.php');
  46. include_once (api_get_path(LIBRARY_PATH).'system_announcements.lib.php');
  47. include_once (api_get_path(LIBRARY_PATH).'groupmanager.lib.php');
  48. include_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  49. $loginFailed = isset($_GET['loginFailed']) ? true : isset($loginFailed);
  50. $setting_show_also_closed_courses = (api_get_setting('show_closed_courses')=='true') ? true : false;
  51. // the section (for the tabs)
  52. $this_section = SECTION_CAMPUS;
  53. /*
  54. -----------------------------------------------------------
  55. Action Handling
  56. -----------------------------------------------------------
  57. */
  58. /** @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?
  59. * 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
  60. * 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
  61. * will see it.
  62. */
  63. if (!empty($_GET['logout'])) {
  64. logout();
  65. }
  66. /*
  67. -----------------------------------------------------------
  68. Table definitions
  69. -----------------------------------------------------------
  70. */
  71. $main_course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  72. $main_category_table = Database :: get_main_table(TABLE_MAIN_CATEGORY);
  73. $track_login_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  74. /*
  75. -----------------------------------------------------------
  76. Constants and CONFIGURATION parameters
  77. -----------------------------------------------------------
  78. */
  79. /** @todo these configuration settings should move to the dokeos config settings */
  80. /** defines wether or not anonymous visitors can see a list of the courses on the Dokeos homepage that are open to the world */
  81. $_setting['display_courses_to_anonymous_users'] = 'true';
  82. /** @todo remove this piece of code because this is not used */
  83. if (isset ($_user['user_id'])) {
  84. $nameTools = api_get_setting('siteName');
  85. }
  86. /*
  87. ==============================================================================
  88. LOGIN
  89. ==============================================================================
  90. */
  91. /**
  92. * @todo This piece of code should probably move to local.inc.php where the actual login / logout procedure is handled.
  93. * @todo consider removing this piece of code because does nothing.
  94. */
  95. if (isset($_GET['submitAuth']) && $_GET['submitAuth'] == 1) {
  96. // nice lie!!!
  97. echo 'Attempted breakin - sysadmins notified.';
  98. session_destroy();
  99. die();
  100. }
  101. /**
  102. * @todo This piece of code should probably move to local.inc.php where the actual login procedure is handled.
  103. * @todo check if this code is used. I think this code is never executed because after clicking the submit button
  104. * the code does the stuff in local.inc.php and then redirects to index.php or user_portal.php depending
  105. * on api_get_setting('page_after_login')
  106. */
  107. if (!empty($_POST["submitAuth"])) {
  108. // the user is already authenticated, we now find the last login of the user.
  109. if (isset ($_user['user_id'])) {
  110. $sql_last_login = "SELECT UNIX_TIMESTAMP(login_date)
  111. FROM $track_login_table
  112. WHERE login_user_id = '".$_user['user_id']."'
  113. ORDER BY login_date DESC LIMIT 1";
  114. $result_last_login = api_sql_query($sql_last_login, __FILE__, __LINE__);
  115. if (!$result_last_login)
  116. if (Database::num_rows($result_last_login) > 0) {
  117. $user_last_login_datetime = Database::fetch_array($result_last_login);
  118. $user_last_login_datetime = $user_last_login_datetime[0];
  119. api_session_register('user_last_login_datetime');
  120. }
  121. mysql_free_result($result_last_login);
  122. //event_login();
  123. if (api_is_platform_admin()) {
  124. // decode all open event informations and fill the track_c_* tables
  125. include (api_get_path(LIBRARY_PATH)."stats.lib.inc.php");
  126. decodeOpenInfos();
  127. }
  128. }
  129. } // end login -- if($_POST["submitAuth"])
  130. else {
  131. // only if login form was not sent because if the form is sent the user was already on the page.
  132. event_open();
  133. }
  134. // the header
  135. Display :: display_header('', 'dokeos');
  136. /*
  137. ==============================================================================
  138. MAIN CODE
  139. ==============================================================================
  140. */
  141. echo '<div class="maincontent" id="content">';
  142. // Plugins for loginpage_main AND campushomepage_main
  143. if (!api_get_user_id()) {
  144. api_plugin('loginpage_main');
  145. } else {
  146. api_plugin('campushomepage_main');
  147. }
  148. // Including the page for the news
  149. $page_included = false;
  150. if (!empty ($_GET['include']) && preg_match('/^[a-zA-Z0-9_-]*\.html$/',$_GET['include'])) {
  151. include ('./home/'.$_GET['include']);
  152. $page_included = true;
  153. } else {
  154. if (!empty($_SESSION['user_language_choice'])) {
  155. $user_selected_language=$_SESSION['user_language_choice'];
  156. } elseif(!empty($_SESSION['_user']['language'])) {
  157. $user_selected_language=$_SESSION['_user']['language'];
  158. } else {
  159. $user_selected_language=get_setting('platformLanguage');
  160. }
  161. if(!file_exists('home/home_news_'.$user_selected_language.'.html')) {
  162. $home_top_temp=file('home/home_top.html');
  163. $home_top_temp=implode('',$home_top_temp);
  164. $open=str_replace('{rel_path}',api_get_path(REL_PATH),$home_top_temp);
  165. echo $open;
  166. } else {
  167. if(file_exists('home/home_top_'.$user_selected_language.'.html')) {
  168. $home_top_temp = file_get_contents('home/home_top_'.$user_selected_language.'.html');
  169. } else {
  170. $home_top_temp = file_get_contents('home/home_top.html');
  171. }
  172. $open=str_replace('{rel_path}',api_get_path(REL_PATH),$home_top_temp);
  173. echo $open;
  174. }
  175. }
  176. // Display System announcements
  177. $announcement = isset($_GET['announcement']) ? $_GET['announcement'] : -1;
  178. $announcement = intval($announcement);
  179. if (isset($_user['user_id'])) {
  180. $visibility = api_is_allowed_to_create_course() ? VISIBLE_TEACHER : VISIBLE_STUDENT;
  181. SystemAnnouncementManager :: display_announcements($visibility, $announcement);
  182. } else {
  183. SystemAnnouncementManager :: display_announcements(VISIBLE_GUEST, $announcement);
  184. }
  185. // Display courses and category list
  186. if (!$page_included) {
  187. if (api_get_setting('display_categories_on_homepage') == 'true') {
  188. echo '<div class="home_cats">';
  189. display_anonymous_course_list();
  190. echo '</div>';
  191. }
  192. }
  193. echo '</div>';
  194. // display right menu: language form, login section + useful weblinks
  195. echo '<div class="menu" id="menu">';
  196. display_anonymous_right_menu();
  197. echo '</div>';
  198. /*
  199. ==============================================================================
  200. FOOTER
  201. ==============================================================================
  202. */
  203. Display :: display_footer();
  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=api_sql_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. $s_sql_update_logout_date="UPDATE $tbl_track_login SET logout_date=NOW() WHERE login_id='$i_id_last_connection'";
  227. api_sql_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. api_session_destroy();
  249. header("Location: index.php$query_string");
  250. exit();
  251. }
  252. /**
  253. * This function checks if there are courses that are open to the world in the platform course categories (=faculties)
  254. *
  255. * @param unknown_type $category
  256. * @return boolean
  257. */
  258. function category_has_open_courses($category) {
  259. global $setting_show_also_closed_courses;
  260. $user_identified = (api_get_user_id()>0 && !api_is_anonymous());
  261. $main_course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  262. $sql_query = "SELECT * FROM $main_course_table WHERE category_code='$category'";
  263. $sql_result = api_sql_query($sql_query, __FILE__, __LINE__);
  264. while ($course = Database::fetch_array($sql_result)) {
  265. if ($setting_show_also_closed_courses == false) {
  266. if ((api_get_user_id()>0
  267. and $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM)
  268. or ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD)) {
  269. return true; //at least one open course
  270. }
  271. } else {
  272. if(isset($course['visibility'])){
  273. return true; //at least one course (does not matter weither it's open or not because $setting_show_also_closed_courses = true
  274. }
  275. }
  276. }
  277. return false;
  278. }
  279. function display_create_course_link() {
  280. echo "<li><a href=\"main/create_course/add_course.php\">".get_lang("CourseCreate")."</a></li>";
  281. }
  282. function display_edit_course_list_links() {
  283. echo "<li><a href=\"main/auth/courses.php\">".get_lang("CourseManagement")."</a></li>";
  284. }
  285. /**
  286. * Displays the right-hand menu for anonymous users:
  287. * login form, useful links, help section
  288. * Warning: function defines globals
  289. * @version 1.0.1
  290. * @todo does $_plugins need to be global?
  291. */
  292. function display_anonymous_right_menu() {
  293. global $loginFailed, $_plugins, $_user, $menu_navigation;
  294. $platformLanguage = api_get_setting('platformLanguage');
  295. if ( !($_user['user_id']) or api_is_anonymous($_user['user_id']) ) { // only display if the user isn't logged in
  296. api_display_language_form(true);
  297. echo '<br />';
  298. display_login_form();
  299. if ($loginFailed) {
  300. handle_login_failed();
  301. }
  302. if (api_get_setting('allow_lostpassword') == 'true' OR api_get_setting('allow_registration') == 'true') {
  303. echo '<div class="menusection"><span class="menusectioncaption">'.get_lang('MenuUser').'</span><ul class="menulist">';
  304. if (get_setting('allow_registration') <> 'false') {
  305. echo '<li><a href="main/auth/inscription.php">'.get_lang('Reg').'</a></li>';
  306. }
  307. if (get_setting('allow_lostpassword') == 'true') {
  308. display_lost_password_info();
  309. }
  310. echo '</ul></div>';
  311. }
  312. if (api_number_of_plugins('loginpage_menu') > 0) {
  313. echo '<div class="note" style="background: none">';
  314. api_plugin('loginpage_menu');
  315. echo '</div>';
  316. }
  317. }
  318. /*** hide right menu "general" and other parts on anonymous right menu *****/
  319. echo "<div class=\"menusection\">", "<span class=\"menusectioncaption\">".get_lang("MenuGeneral")."</span>";
  320. echo "<ul class=\"menulist\">";
  321. $user_selected_language = api_get_interface_language();
  322. if (!isset ($user_selected_language))
  323. $user_selected_language = $platformLanguage;
  324. if (!file_exists('home/home_menu_'.$user_selected_language.'.html')) {
  325. include ('home/home_menu.html');
  326. } else {
  327. include('home/home_menu_'.$user_selected_language.'.html');
  328. }
  329. echo '</ul>';
  330. echo '</div>';
  331. if ($_user['user_id'] && api_number_of_plugins('campushomepage_menu') > 0) {
  332. echo '<div class="note" style="background: none">';
  333. api_plugin('campushomepage_menu');
  334. echo '</div>';
  335. }
  336. /**
  337. * User section
  338. */
  339. if (isset($_SESSION['_user']['user_id']) && $_SESSION['_user']['user_id']!=0) {
  340. // tabs that are deactivated are added here
  341. if (!empty($menu_navigation)) {
  342. echo "<div class=\"menusection\">";
  343. echo "<span class=\"menusectioncaption\">".get_lang("MainNavigation")."</span>";
  344. echo "<ul class=\"menulist\">";
  345. foreach($menu_navigation as $section => $navigation_info) {
  346. $current = ($section == $GLOBALS['this_section'] ? ' id="current"' : '');
  347. echo '<li'.$current.'>';
  348. echo '<a href="'.$navigation_info['url'].'" target="_top">'.$navigation_info['title'].'</a>';
  349. echo '</li>';
  350. echo "\n";
  351. }
  352. echo "</ul>";
  353. echo '</div>';
  354. }
  355. echo "<div class=\"menusection\">";
  356. echo "<span class=\"menusectioncaption\">".get_lang("MenuUser")."</span>";
  357. echo "<ul class=\"menulist\">";
  358. $display_add_course_link = api_is_allowed_to_create_course() && ($_SESSION["studentview"] != "studentenview");
  359. if ($display_add_course_link) {
  360. if (api_get_setting('allow_users_to_create_courses')=='false' && !api_is_platform_admin()) {
  361. //echo get_lang('NotHavePermissionToCreateCourses');
  362. } else {
  363. display_create_course_link();
  364. }
  365. }
  366. display_edit_course_list_links();
  367. echo "</ul>";
  368. echo "</div>";
  369. }
  370. // includes for any files to be displayed below anonymous right menu
  371. if (!file_exists('home/home_notice_'.$user_selected_language.'.html') && file_get_contents('home/home_notice.html')!='') {
  372. echo '<div class="note">';
  373. include ('home/home_notice.html');
  374. echo '</div>';
  375. } elseif(file_exists('home/home_notice_'.$user_selected_language.'.html') && file_get_contents('home/home_notice_'.$user_selected_language.'.html')!='') {
  376. echo '<div class="note">';
  377. include('home/home_notice_'.$user_selected_language.'.html');
  378. echo '</div>';
  379. }
  380. }
  381. /**
  382. * Reacts on a failed login:
  383. * displays an explanation with
  384. * a link to the registration form.
  385. *
  386. * @version 1.0.1
  387. */
  388. function handle_login_failed() {
  389. if (!isset($_GET['error'])) {
  390. $message = get_lang("InvalidId");
  391. if (api_is_self_registration_allowed()) {
  392. $message = get_lang("InvalidForSelfRegistration");
  393. }
  394. } else {
  395. switch ($_GET['error']) {
  396. case '':
  397. $message = get_lang("InvalidId");
  398. if (api_is_self_registration_allowed()) {
  399. $message = get_lang("InvalidForSelfRegistration");
  400. }
  401. break;
  402. case 'account_expired':
  403. $message=get_lang('AccountExpired');
  404. break;
  405. case 'account_inactive':
  406. $message=get_lang('AccountInactive');
  407. break;
  408. case 'user_password_incorrect':
  409. $message=get_lang('InvalidId');
  410. break;
  411. }
  412. }
  413. echo "<div id=\"login_fail\">".$message."</div>";
  414. }
  415. /**
  416. * Adds a form to let users login
  417. * @version 1.1
  418. */
  419. function display_login_form() {
  420. $form = new FormValidator('formLogin');
  421. $form->addElement('text','login',get_lang('UserName'),array('size'=>15));
  422. $form->addElement('password','password',get_lang('Pass'),array('size'=>15));
  423. $form->addElement('submit','submitAuth',get_lang('Ok'));
  424. $renderer =& $form->defaultRenderer();
  425. $renderer->setElementTemplate('<div><label>{label}</label></div><div>{element}</div>');
  426. $form->display();
  427. if (api_get_setting('openid_authentication')=='true') {
  428. include_once('main/auth/openid/login.php');
  429. echo '<div>'.openid_form().'</div>';
  430. }
  431. }
  432. /**
  433. * Displays a link to the lost password section
  434. */
  435. function display_lost_password_info() {
  436. echo "<li><a href=\"main/auth/lostPassword.php\">".get_lang("LostPassword")."</a></li>";
  437. }
  438. /**
  439. * Display list of courses in a category.
  440. * (for anonymous users)
  441. *
  442. * @version 1.1
  443. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University - refactoring and code cleaning
  444. */
  445. function display_anonymous_course_list() {
  446. $ctok = $_SESSION['sec_token'];
  447. $stok = Security::get_token();
  448. //init
  449. $user_identified = (api_get_user_id()>0 && !api_is_anonymous());
  450. $web_course_path = api_get_path(WEB_COURSE_PATH);
  451. $category = $_GET["category"];
  452. global $setting_show_also_closed_courses;
  453. // Database table definitions
  454. $main_course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  455. $main_category_table = Database :: get_main_table(TABLE_MAIN_CATEGORY);
  456. $platformLanguage = api_get_setting('platformLanguage');
  457. //get list of courses in category $category
  458. $sql_get_course_list = "SELECT * FROM $main_course_table cours
  459. WHERE category_code = '".Database::escape_string($_GET["category"])."'
  460. ORDER BY title, UPPER(visual_code)";
  461. //removed: AND cours.visibility='".COURSE_VISIBILITY_OPEN_WORLD."'
  462. $sql_result_courses = api_sql_query($sql_get_course_list, __FILE__, __LINE__);
  463. while ($course_result = Database::fetch_array($sql_result_courses)) {
  464. $course_list[] = $course_result;
  465. }
  466. $platform_visible_courses = '';
  467. // $setting_show_also_closed_courses
  468. if($user_identified) {
  469. if ($setting_show_also_closed_courses) {
  470. $platform_visible_courses = '';
  471. } else {
  472. $platform_visible_courses = " AND (t3.visibility='".COURSE_VISIBILITY_OPEN_WORLD."' OR t3.visibility='".COURSE_VISIBILITY_OPEN_PLATFORM."' )";
  473. }
  474. } else {
  475. if ($setting_show_also_closed_courses) {
  476. $platform_visible_courses = '';
  477. } else {
  478. $platform_visible_courses = " AND (t3.visibility='".COURSE_VISIBILITY_OPEN_WORLD."' )";
  479. }
  480. }
  481. $sqlGetSubCatList = "
  482. SELECT t1.name,t1.code,t1.parent_id,t1.children_count,COUNT(DISTINCT t3.code) AS nbCourse
  483. FROM $main_category_table t1
  484. LEFT JOIN $main_category_table t2 ON t1.code=t2.parent_id
  485. LEFT JOIN $main_course_table t3 ON (t3.category_code=t1.code $platform_visible_courses)
  486. WHERE t1.parent_id ". (empty ($category) ? "IS NULL" : "='$category'")."
  487. GROUP BY t1.name,t1.code,t1.parent_id,t1.children_count ORDER BY t1.tree_pos, t1.name";
  488. $resCats = api_sql_query($sqlGetSubCatList, __FILE__, __LINE__);
  489. $thereIsSubCat = false;
  490. if (Database::num_rows($resCats) > 0) {
  491. $htmlListCat = "<h4 style=\"margin-top: 0px;\">".get_lang("CatList")."</h4>"."<ul>";
  492. while ($catLine = Database::fetch_array($resCats)) {
  493. if ($catLine['code'] != $category) {
  494. $category_has_open_courses = category_has_open_courses($catLine['code']);
  495. if ($category_has_open_courses) {
  496. //the category contains courses accessible to anonymous visitors
  497. $htmlListCat .= "<li>";
  498. $htmlListCat .= "<a href=\"".api_get_self()."?category=".$catLine['code']."\">".$catLine['name']."</a>";
  499. if (api_get_setting('show_number_of_courses') == 'true') {
  500. $htmlListCat .= " (".$catLine['nbCourse']." ".get_lang("Courses").")";
  501. }
  502. $htmlListCat .= "</li>\n";
  503. $thereIsSubCat = true;
  504. } elseif ($catLine['children_count'] > 0) {
  505. //the category has children, subcategories
  506. $htmlListCat .= "<li>";
  507. $htmlListCat .= "<a href=\"".api_get_self()."?category=".$catLine['code']."\">".$catLine['name']."</a>";
  508. $htmlListCat .= "</li>\n";
  509. $thereIsSubCat = true;
  510. }
  511. /************************************************************************
  512. end changed code to eliminate the (0 courses) after empty categories
  513. ************************************************************************/
  514. elseif (api_get_setting('show_empty_course_categories') == 'true') {
  515. $htmlListCat .= "<li>";
  516. $htmlListCat .= $catLine['name'];
  517. $htmlListCat .= "</li>\n";
  518. $thereIsSubCat = true;
  519. }//else don't set thereIsSubCat to true to avoid printing things if not requested
  520. } else {
  521. $htmlTitre = "<p>";
  522. if (api_get_setting('show_back_link_on_top_of_tree') == 'true') {
  523. $htmlTitre .= "<a href=\"".api_get_self()."\">"."&lt;&lt; ".get_lang("BackToHomePage")."</a>";
  524. }
  525. if (!is_null($catLine['parent_id']) || (api_get_setting('show_back_link_on_top_of_tree') <> 'true' && !is_null($catLine['code']))) {
  526. $htmlTitre .= "<a href=\"".api_get_self()."?category=".$catLine['parent_id']."\">"."&lt;&lt; ".get_lang("Up")."</a>";
  527. }
  528. $htmlTitre .= "</p>\n";
  529. if ($category != "" && !is_null($catLine['code'])) {
  530. $htmlTitre .= "<h3>".$catLine['name']."</h3>\n";
  531. } else {
  532. $htmlTitre .= "<h3>".get_lang("Categories")."</h3>\n";
  533. }
  534. }
  535. }
  536. $htmlListCat .= "</ul>\n";
  537. }
  538. echo $htmlTitre;
  539. if ($thereIsSubCat) {
  540. echo $htmlListCat;
  541. }
  542. while ($categoryName = Database::fetch_array($resCats)) {
  543. echo "<h3>", $categoryName['name'], "</h3>\n";
  544. }
  545. $numrows = Database::num_rows($sql_result_courses);
  546. $courses_list_string = '';
  547. $courses_shown = 0;
  548. if ($numrows > 0) {
  549. if ($thereIsSubCat) {
  550. $courses_list_string .= "<hr size=\"1\" noshade=\"noshade\">\n";
  551. }
  552. $courses_list_string .= "<h4 style=\"margin-top: 0px;\">".get_lang("CourseList")."</h4>\n"."<ul>\n";
  553. if (api_get_user_id()) {
  554. $courses_of_user = get_courses_of_user(api_get_user_id());
  555. }
  556. foreach ($course_list AS $course) {
  557. // $setting_show_also_closed_courses
  558. if ($setting_show_also_closed_courses==false) {
  559. // if we do not show the closed courses
  560. // we only show the courses that are open to the world (to everybody)
  561. // and the courses that are open to the platform (if the current user is a registered user
  562. if( ($user_identified && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM) OR ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD)) {
  563. $courses_shown++;
  564. $courses_list_string .= "<li>\n";
  565. $courses_list_string .= "<a href=\"".$web_course_path.$course['directory']."/\">".$course['title']."</a><br />";
  566. if (get_setting("display_coursecode_in_courselist") == "true") {
  567. $courses_list_string .= $course['visual_code'];
  568. }
  569. if (get_setting("display_coursecode_in_courselist") == "true" AND get_setting("display_teacher_in_courselist") == "true") {
  570. $courses_list_string .= " - ";
  571. }
  572. if (get_setting("display_teacher_in_courselist") == "true") {
  573. $courses_list_string .= $course['tutor_name'];
  574. }
  575. if (api_get_setting('show_different_course_language') == 'true' && $course['course_language'] <> api_get_setting('platformLanguage')) {
  576. $courses_list_string .= ' - '.$course['course_language'];
  577. }
  578. $courses_list_string .= "</li>\n";
  579. }
  580. }
  581. // we DO show the closed courses.
  582. // the course is accessible if (link to the course homepage)
  583. // 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)
  584. // 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)
  585. // 3. the user is logged in and the user is subscribed to the course and the course visibility is not COURSE_VISIBILITY_CLOSED
  586. // 4. the user is logged in and the user is course admin of te course (regardless of the course visibility setting)
  587. // 5. the user is the platform admin api_is_platform_admin()
  588. //
  589. else {
  590. $courses_shown++;
  591. $courses_list_string .= "<li>\n";
  592. if ( $course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD
  593. OR ($user_identified AND $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM)
  594. OR ($user_identified AND key_exists($course['code'],$courses_of_user) AND $course['visibility'] <> COURSE_VISIBILITY_CLOSED)
  595. OR $courses_of_user[$course['code']]['status'] == '1'
  596. OR api_is_platform_admin()) {
  597. $courses_list_string .= "<a href=\"".$web_course_path.$course['directory']."/\">";
  598. }
  599. $courses_list_string .= $course['title'];
  600. if ( $course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD
  601. OR ($user_identified AND $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM)
  602. OR ($user_identified AND key_exists($course['code'],$courses_of_user) AND $course['visibility'] <> COURSE_VISIBILITY_CLOSED)
  603. OR $courses_of_user[$course['code']]['status'] == '1'
  604. OR api_is_platform_admin()) {
  605. $courses_list_string .="</a><br />";
  606. }
  607. if (get_setting("display_coursecode_in_courselist") == "true") {
  608. $courses_list_string .= $course['visual_code'];
  609. }
  610. if (get_setting("display_coursecode_in_courselist") == "true" AND get_setting("display_teacher_in_courselist") == "true") {
  611. $courses_list_string .= " - ";
  612. }
  613. if (get_setting("display_teacher_in_courselist") == "true")
  614. {
  615. $courses_list_string .= $course['tutor_name'];
  616. }
  617. if (api_get_setting('show_different_course_language') == 'true' && $course['course_language'] <> api_get_setting('platformLanguage')) {
  618. $courses_list_string .= ' - '.$course['course_language'];
  619. }
  620. if (api_get_setting('show_different_course_language') == 'true' && $course['course_language'] <> api_get_setting('platformLanguage')) {
  621. $courses_list_string .= ' - '.$course['course_language'];
  622. }
  623. // We display a subscription link if
  624. // 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
  625. // 2
  626. if ($user_identified AND !key_exists($course['code'],$courses_of_user)) {
  627. if ($course['subscribe'] == '1') {
  628. $courses_list_string .= "<form action=\"main/auth/courses.php?action=subscribe&category=".$_GET['category']."\" method=\"post\">";
  629. $courses_list_string .= '<input type="hidden" name="sec_token" value="'.$stok.'">';
  630. $courses_list_string .= "<input type=\"hidden\" name=\"subscribe\" value=\"".$course['code']."\" />";
  631. $courses_list_string .= "<input type=\"image\" name=\"unsub\" src=\"main/img/enroll.gif\" alt=\"".get_lang("Subscribe")."\" />".get_lang("Subscribe")."</form>";
  632. } else {
  633. $courses_list_string .= '<br />'.get_lang("SubscribingNotAllowed");
  634. }
  635. }
  636. $courses_list_string .= "</li>\n";
  637. }
  638. }
  639. $courses_list_string .= "</ul>\n";
  640. } else {
  641. // echo "<blockquote>",get_lang('_No_course_publicly_available'),"</blockquote>\n";
  642. }
  643. if ($courses_shown > 0) { //only display the list of courses and categories if there was more than
  644. // 0 courses visible to the world (we're in the anonymous list here)
  645. echo $courses_list_string;
  646. }
  647. if ($category != "") {
  648. echo "<p>", "<a href=\"".api_get_self()."\"><b>&lt;&lt;</b> ", get_lang("BackToHomePage"), "</a>", "</p>\n";
  649. }
  650. }
  651. /**
  652. * retrieves all the courses that the user has already subscribed to
  653. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  654. * @param int $user_id: the id of the user
  655. * @return array an array containing all the information of the courses of the given user
  656. */
  657. function get_courses_of_user($user_id) {
  658. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  659. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  660. // Secondly we select the courses that are in a category (user_course_cat<>0) and sort these according to the sort of the category
  661. $user_id = intval($user_id);
  662. $sql_select_courses="SELECT course.code k, course.visual_code vc, course.subscribe subscr, course.unsubscribe unsubscr,
  663. course.title i, course.tutor_name t, course.db_name db, course.directory dir, course_rel_user.status status,
  664. course_rel_user.sort sort, course_rel_user.user_course_cat user_course_cat
  665. FROM $table_course course,
  666. $table_course_user course_rel_user
  667. WHERE course.code = course_rel_user.course_code
  668. AND course_rel_user.user_id = '".$user_id."'
  669. ORDER BY course_rel_user.sort ASC";
  670. $result = api_sql_query($sql_select_courses,__FILE__,__LINE__);
  671. while ($row=Database::fetch_array($result)) {
  672. // we only need the database name of the course
  673. $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']);
  674. }
  675. return $courses;
  676. }