index.php 34 KB

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