index.php 30 KB

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