index.php 30 KB

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