index.php 29 KB

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