page.lib.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. <?php
  2. /**
  3. * Controller for pages presentation in general
  4. * @license see /license.txt
  5. * @package chamilo.page.controller
  6. * @author Julio Montoya <gugli100@gmail.com>
  7. */
  8. /**
  9. * Page controller
  10. */
  11. class PageController
  12. {
  13. /**
  14. * Returns an HTML block with the user picture (as a link in a <div>)
  15. * @param int User ID (if not provided, will use the user ID from session)
  16. * @return string HTML div with a link to the user's profile
  17. * @uses UserManager::get_user_pictur_path_by_id() to get the image path
  18. * @uses UserManager::get_picture_user() to get the details of the image in a specific format
  19. * @uses PageController::show_right_block() to include the image in a larger user block
  20. * @assert (-1) === false
  21. */
  22. static function return_user_image_block($user_id = null) {
  23. if (empty($user_id)) {
  24. $user_id = api_get_user_id();
  25. }
  26. //Always show the user image
  27. $img_array = UserManager::get_user_picture_path_by_id($user_id, 'web', true, true);
  28. $no_image = false;
  29. if ($img_array['file'] == 'unknown.jpg') {
  30. $no_image = true;
  31. }
  32. $img_array = UserManager::get_picture_user($user_id, $img_array['file'], 50, USER_IMAGE_SIZE_MEDIUM, ' width="90" height="90" ');
  33. $profile_content = null;
  34. if (api_get_setting('allow_social_tool') == 'true') {
  35. if (!$no_image) {
  36. $profile_content .='<a style="text-align:center" href="'.api_get_path(WEB_PATH).'main/social/home.php"><img src="'.$img_array['file'].'" '.$img_array['style'].' ></a>';
  37. } else {
  38. $profile_content .='<a style="text-align:center" href="'.api_get_path(WEB_PATH).'main/auth/profile.php"><img title="'.get_lang('EditProfile').'" src="'.$img_array['file'].'" '.$img_array['style'].'></a>';
  39. }
  40. }
  41. self::show_right_block(null, null, 'user_image_block', array('content' => $profile_content));
  42. }
  43. /**
  44. * Return a block with course-related links. The resulting HTML block's
  45. * contents are only based on the user defined by the active session.
  46. * @return string HTML <div> with links
  47. * @assert () != ''
  48. */
  49. static function return_course_block() {
  50. $show_create_link = false;
  51. $show_course_link = false;
  52. if ((api_get_setting('allow_users_to_create_courses') == 'false' && !api_is_platform_admin()) || api_is_student()) {
  53. $display_add_course_link = false;
  54. } else {
  55. $display_add_course_link = true;
  56. }
  57. //$display_add_course_link = api_is_allowed_to_create_course() && ($_SESSION['studentview'] != 'studentenview');
  58. if ($display_add_course_link) {
  59. $show_create_link = true;
  60. }
  61. if (api_is_platform_admin() || api_is_course_admin() || api_is_allowed_to_create_course()) {
  62. $show_course_link = true;
  63. } else {
  64. if (api_get_setting('allow_students_to_browse_courses') == 'true') {
  65. $show_course_link = true;
  66. }
  67. }
  68. // My account section
  69. $my_account_content = array();
  70. if ($show_create_link) {
  71. $my_account_content[] = array('href' => 'main/create_course/add_course.php', 'title' => api_get_setting('course_validation') == 'true' ? get_lang('CreateCourseRequest') : get_lang('CourseCreate'));
  72. }
  73. //Sort courses
  74. $url = api_get_path(WEB_CODE_PATH).'auth/courses.php?action=sortmycourses';
  75. $my_account_content[] = array('href' => $url, 'title' => get_lang('SortMyCourses'));
  76. //Course management
  77. if ($show_course_link) {
  78. if (!api_is_drh()) {
  79. $my_account_content[] = array('href' => 'main/auth/courses.php', 'title' => get_lang('CourseCatalog'));
  80. if (isset($_GET['history']) && intval($_GET['history']) == 1) {
  81. $my_account_content [] = array('href' => 'user_portal.php', 'title' => get_lang('DisplayTrainingList'));
  82. } else {
  83. $my_account_content [] = array('href' => 'user_portal.php?history=1', 'title' => get_lang('HistoryTrainingSessions'));
  84. }
  85. } else {
  86. $my_account_content .= array('href' => 'main/dashboard/index.php', 'title' => get_lang('Dashboard'));
  87. }
  88. }
  89. self::show_right_block(get_lang('Courses'), $my_account_content, 'course_block');
  90. }
  91. /**
  92. * Returns the profile block, showing links to the messaging and social
  93. * network tools. The user ID is taken from the active session
  94. * @return string HTML <div> block
  95. * @assert () != ''
  96. */
  97. static function return_profile_block() {
  98. // @todo Add a platform setting to add the user image.
  99. if (api_get_setting('allow_message_tool') == 'true') {
  100. if (api_get_setting('allow_social_tool') == 'true') {
  101. self::show_right_block(get_lang('Profile'), array(), 'profile_social_block');
  102. } else {
  103. self::show_right_block(get_lang('Profile'), array(), 'profile_block');
  104. }
  105. }
  106. }
  107. /**
  108. * Returns a list of the most popular courses of the moment (also called
  109. * "hot courses").
  110. * @uses CourseManager::return_hot_courses() in fact, the current method is only a bypass to this method
  111. * @return string HTML <div> with the most popular courses
  112. * @assert () != ''
  113. */
  114. static function return_hot_courses() {
  115. return CourseManager::return_hot_courses();
  116. }
  117. /**
  118. * Returns an online help block read from the home/home_menu_[lang].html
  119. * file
  120. * @return string HTML block
  121. * @assert () != ''
  122. */
  123. static function return_help() {
  124. $home = api_get_home_path();
  125. $user_selected_language = api_get_interface_language();
  126. $sys_path = api_get_path(SYS_PATH);
  127. $platformLanguage = api_get_setting('platformLanguage');
  128. if (!isset($user_selected_language)) {
  129. $user_selected_language = $platformLanguage;
  130. }
  131. $home_menu = @(string) file_get_contents($sys_path.$home.'home_menu_'.$user_selected_language.'.html');
  132. if (!empty($home_menu)) {
  133. $home_menu_content = api_to_system_encoding($home_menu, api_detect_encoding(strip_tags($home_menu)));
  134. self::show_right_block(get_lang('MenuGeneral'), null, 'help_block', array('content' => $home_menu_content));
  135. }
  136. }
  137. /**
  138. * Returns an HTML block with links to the skills tools
  139. * @return string HTML <div> block
  140. * @assert () != ''
  141. */
  142. static function return_skills_links() {
  143. if (api_get_setting('allow_skills_tool') == 'true') {
  144. $content = array();
  145. $content[]= array('title' => get_lang('MySkills'), 'href' => api_get_path(WEB_CODE_PATH).'social/skills_wheel.php');
  146. if (api_get_setting('allow_hr_skills_management') == 'true' || api_is_platform_admin()) {
  147. $content[]= array('title' => get_lang('ManageSkills'), 'href' => api_get_path(WEB_CODE_PATH).'admin/skills_wheel.php');
  148. }
  149. self::show_right_block(get_lang("Skills"), $content, 'skill_block');
  150. }
  151. }
  152. /**
  153. * Returns an HTML block with the notice, as found in the
  154. * home/home_notice_[lang].html file
  155. * @return string HTML <div> block
  156. * @assert () != ''
  157. */
  158. static function return_notice() {
  159. $sys_path = api_get_path(SYS_PATH);
  160. $user_selected_language = api_get_interface_language();
  161. $home = api_get_home_path();
  162. // Notice
  163. $home_notice = @(string) file_get_contents($sys_path.$home.'home_notice_'.$user_selected_language.'.html');
  164. if (empty($home_notice)) {
  165. $home_notice = @(string) file_get_contents($sys_path.$home.'home_notice.html');
  166. }
  167. if (!empty($home_notice)) {
  168. $home_notice = api_to_system_encoding($home_notice, api_detect_encoding(strip_tags($home_notice)));
  169. $home_notice = Display::div($home_notice, array('class' => 'homepage_notice'));
  170. self::show_right_block(get_lang('Notice'), null, 'notice_block', array('content' => $home_notice));
  171. }
  172. }
  173. /**
  174. * Returns the received content packaged in <div> block, with the title as
  175. * <h4>
  176. * @param string Title to include as h4
  177. * @param string Longer content to show (usually a <ul> list)
  178. * @param string ID to be added to the HTML attributes for the block
  179. * @param array Array of attributes to add to the HTML block
  180. * @return string HTML <div> block
  181. * @assert ('a','') != ''
  182. * @todo use the template system
  183. */
  184. static function show_right_block($title, $content, $id, $params = null) {
  185. //@todo do not use global
  186. global $app;
  187. if (!empty($id)) {
  188. $params['id'] = $id;
  189. }
  190. $block_menu = array(
  191. 'id' => $params['id'],
  192. 'title' => $title,
  193. 'elements' => $content,
  194. 'content' => isset($params['content']) ? $params['content'] : null
  195. );
  196. $app['template']->assign($id, $block_menu);
  197. }
  198. /**
  199. * Adds a form to let users login
  200. * @return string An HTML string with the user login form
  201. * @assert () != ''
  202. * @version 1.1
  203. */
  204. static function display_login_form() {
  205. $form = new FormValidator('formLogin', 'POST', null, null, array('class' => 'form-vertical'));
  206. // 'placeholder'=>get_lang('UserName')
  207. //'autocomplete'=>"off",
  208. $form->addElement('text', 'login', get_lang('UserName'), array('class' => 'span2 autocapitalize_off', 'autofocus' => 'autofocus'));
  209. $form->addElement('password', 'password', get_lang('Pass'), array('class' => 'span2'));
  210. $form->addElement('style_submit_button', 'submitAuth', get_lang('LoginEnter'), array('class' => 'btn'));
  211. $html = $form->return_form();
  212. if (api_get_setting('openid_authentication') == 'true') {
  213. include_once 'main/auth/openid/login.php';
  214. $html .= '<div>'.openid_form().'</div>';
  215. }
  216. return $html;
  217. }
  218. /**
  219. * Returns a content search form in an HTML <div>, pointing at the
  220. * main/search/ directory. If search_enabled is not set, then it returns
  221. * an empty string
  222. * @return string HTML <div> block showing the search form, or an empty string if search not enabled
  223. * @assert () !== false
  224. */
  225. static function return_search_block() {
  226. $html = '';
  227. if (api_get_setting('search_enabled') == 'true') {
  228. $html .= '<div class="searchbox">';
  229. $search_btn = get_lang('Search');
  230. $search_content = '<br />
  231. <form action="main/search/" method="post">
  232. <input type="text" id="query" class="span2" name="query" value="" />
  233. <button class="save" type="submit" name="submit" value="'.$search_btn.'" />'.$search_btn.' </button>
  234. </form></div>';
  235. $html .= self::show_right_block(get_lang('Search'), $search_content, 'search_block');
  236. }
  237. return $html;
  238. }
  239. /**
  240. * Returns a list of announcements
  241. * @param int User ID
  242. * @param bool True: show the announcements as a slider. False: show them as a vertical list
  243. * @return string HTML list of announcements
  244. * @assert () != ''
  245. * @assert (1) != ''
  246. */
  247. static function return_announcements($user_id = null, $show_slide = true) {
  248. // Display System announcements
  249. $announcement = isset($_GET['announcement']) ? intval($_GET['announcement']) : null;
  250. if (!api_is_anonymous() && $user_id) {
  251. $visibility = api_is_allowed_to_create_course() ? SystemAnnouncementManager::VISIBLE_TEACHER : SystemAnnouncementManager::VISIBLE_STUDENT;
  252. if ($show_slide) {
  253. $announcements = SystemAnnouncementManager :: display_announcements_slider($visibility, $announcement);
  254. } else {
  255. $announcements = SystemAnnouncementManager :: display_all_announcements($visibility, $announcement);
  256. }
  257. } else {
  258. if ($show_slide) {
  259. $announcements = SystemAnnouncementManager :: display_announcements_slider(SystemAnnouncementManager::VISIBLE_GUEST, $announcement);
  260. } else {
  261. $announcements = SystemAnnouncementManager :: display_all_announcements(SystemAnnouncementManager::VISIBLE_GUEST, $announcement);
  262. }
  263. }
  264. return $announcements;
  265. }
  266. /**
  267. * Return the homepage, including announcements
  268. * @return string The portal's homepage as an HTML string
  269. * @assert () != ''
  270. */
  271. static function return_home_page() {
  272. // Including the page for the news
  273. $html = null;
  274. $home = api_get_home_path();
  275. $home_top_temp = null;
  276. if (!empty($_GET['include']) && preg_match('/^[a-zA-Z0-9_-]*\.html$/', $_GET['include'])) {
  277. $open = @(string) file_get_contents(api_get_path(SYS_PATH).$home.$_GET['include']);
  278. $html = api_to_system_encoding($open, api_detect_encoding(strip_tags($open)));
  279. } else {
  280. if (!empty($_SESSION['user_language_choice'])) {
  281. $user_selected_language = $_SESSION['user_language_choice'];
  282. } elseif (!empty($_SESSION['_user']['language'])) {
  283. $user_selected_language = $_SESSION['_user']['language'];
  284. } else {
  285. $user_selected_language = api_get_setting('platformLanguage');
  286. }
  287. if (!file_exists($home.'home_news_'.$user_selected_language.'.html')) {
  288. if (file_exists($home.'home_top.html')) {
  289. $home_top_temp = file($home.'home_top.html');
  290. } else {
  291. //$home_top_temp = file('home/'.'home_top.html');
  292. }
  293. if (!empty($home_top_temp)) {
  294. $home_top_temp = implode('', $home_top_temp);
  295. }
  296. } else {
  297. if (file_exists($home.'home_top_'.$user_selected_language.'.html')) {
  298. $home_top_temp = file_get_contents($home.'home_top_'.$user_selected_language.'.html');
  299. } else {
  300. $home_top_temp = file_get_contents($home.'home_top.html');
  301. }
  302. }
  303. //trim($home_top_temp) == ''
  304. if (empty($home_top_temp) && api_is_platform_admin()) {
  305. $home_top_temp = get_lang('PortalHomepageDefaultIntroduction');
  306. }
  307. $open = str_replace('{rel_path}', api_get_path(REL_PATH), $home_top_temp);
  308. if (!empty($open)) {
  309. $html = api_to_system_encoding($open, api_detect_encoding(strip_tags($open)));
  310. }
  311. }
  312. return $html;
  313. }
  314. /**
  315. * Returns the reservation block (if the reservation tool is enabled)
  316. * @return string HTML block, or empty string if reservation tool is disabled
  317. * @assert () == ''
  318. */
  319. static function return_reservation_block() {
  320. $html = '';
  321. if (api_get_setting('allow_reservation') == 'true' && api_is_allowed_to_create_course()) {
  322. $booking_content .='<ul class="nav nav-list">';
  323. $booking_content .='<a href="main/reservation/reservation.php">'.get_lang('ManageReservations').'</a><br />';
  324. $booking_content .='</ul>';
  325. $html .= self::show_right_block(get_lang('Booking'), $booking_content, 'reservation_block');
  326. }
  327. return $html;
  328. }
  329. /**
  330. * Returns an HTML block with classes (if show_groups_to_users is true)
  331. * @return string A list of links to users classes tools, or an empty string if show_groups_to_users is disabled
  332. * @assert () == ''
  333. */
  334. static function return_classes_block() {
  335. $html = '';
  336. if (api_get_setting('show_groups_to_users') == 'true') {
  337. require_once api_get_path(LIBRARY_PATH).'usergroup.lib.php';
  338. $usergroup = new Usergroup();
  339. $usergroup_list = $usergroup->get_usergroup_by_user(api_get_user_id());
  340. $classes = '';
  341. if (!empty($usergroup_list)) {
  342. foreach ($usergroup_list as $group_id) {
  343. $data = $usergroup->get($group_id);
  344. $data['name'] = Display::url($data['name'], api_get_path(WEB_CODE_PATH).'user/classes.php?id='.$data['id']);
  345. $classes .= Display::tag('li', $data['name']);
  346. }
  347. }
  348. if (api_is_platform_admin()) {
  349. $classes .= Display::tag('li', Display::url(get_lang('AddClasses'), api_get_path(WEB_CODE_PATH).'admin/usergroups.php?action=add'));
  350. }
  351. if (!empty($classes)) {
  352. $classes = Display::tag('ul', $classes, array('class' => 'nav nav-list'));
  353. $html .= self::show_right_block(get_lang('Classes'), $classes, 'classes_block');
  354. }
  355. }
  356. return $html;
  357. }
  358. /**
  359. * Prepares a block with all the pending exercises in all courses
  360. * @param array Array of courses (arrays) of the user
  361. * @return void Doesn't return anything but prepares and HTML block for use in templates
  362. * @assert () !== 1
  363. */
  364. static function return_exercise_block($personal_course_list) {
  365. require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php';
  366. $exercise_list = array();
  367. if (!empty($personal_course_list)) {
  368. foreach ($personal_course_list as $course_item) {
  369. $course_code = $course_item['c'];
  370. $session_id = $course_item['id_session'];
  371. $exercises = get_exercises_to_be_taken($course_code, $session_id);
  372. foreach ($exercises as $exercise_item) {
  373. $exercise_item['course_code'] = $course_code;
  374. $exercise_item['session_id'] = $session_id;
  375. $exercise_item['tms'] = api_strtotime($exercise_item['end_time'], 'UTC');
  376. $exercise_list[] = $exercise_item;
  377. }
  378. }
  379. if (!empty($exercise_list)) {
  380. $exercise_list = msort($exercise_list, 'tms');
  381. $my_exercise = $exercise_list[0];
  382. $url = Display::url($my_exercise['title'], api_get_path(WEB_CODE_PATH).'exercice/overview.php?exerciseId='.$my_exercise['id'].'&cidReq='.$my_exercise['course_code'].'&id_session='.$my_exercise['session_id']);
  383. $this->page->assign('exercise_url', $url);
  384. $this->page->assign('exercise_end_date', api_convert_and_format_date($my_exercise['end_time'], DATE_FORMAT_SHORT));
  385. }
  386. }
  387. }
  388. /**
  389. * Returns links to teachers tools (create course, etc) based on the user
  390. * in the active session
  391. * @return string HTML <div> block
  392. * @assert () == ''
  393. */
  394. static function return_teacher_link() {
  395. $html = '';
  396. $user_id = api_get_user_id();
  397. if (!empty($user_id)) {
  398. // tabs that are deactivated are added here
  399. $show_menu = false;
  400. $show_create_link = false;
  401. $show_course_link = false;
  402. if (api_is_platform_admin() || api_is_course_admin() || api_is_allowed_to_create_course()) {
  403. $show_menu = true;
  404. $show_course_link = true;
  405. } else {
  406. if (api_get_setting('allow_students_to_browse_courses') == 'true') {
  407. $show_menu = true;
  408. $show_course_link = true;
  409. }
  410. }
  411. if ($show_menu && ($show_create_link || $show_course_link )) {
  412. $show_menu = true;
  413. } else {
  414. $show_menu = false;
  415. }
  416. }
  417. // My Account section
  418. $elements = array();
  419. if ($show_menu) {
  420. if ($show_create_link) {
  421. $elements[] = array('href' => 'main/create_course/add_course.php', 'title' => (api_get_setting('course_validation') == 'true' ? get_lang('CreateCourseRequest') : get_lang('CourseCreate')));
  422. }
  423. if ($show_course_link) {
  424. if (!api_is_drh() && !api_is_session_admin()) {
  425. $elements[] = array('href' => 'main/auth/courses.php', 'title' => get_lang('CourseCatalog'));
  426. } else {
  427. $elements[] = array('href' => 'main/dashboard/index.php', 'title' => get_lang('Dashboard'));
  428. }
  429. }
  430. }
  431. self::show_right_block(get_lang('Courses'), $elements, 'teacher_block');
  432. }
  433. /**
  434. * Display list of courses in a category.
  435. * (for anonymous users)
  436. *
  437. * @version 1.1
  438. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University - refactoring and code cleaning
  439. * @author Julio Montoya <gugli100@gmail.com>, Beeznest template modifs
  440. * @assert () !== 0
  441. */
  442. static function return_courses_in_categories() {
  443. $result = '';
  444. $stok = Security::get_token();
  445. // Initialization.
  446. $user_identified = (api_get_user_id() > 0 && !api_is_anonymous());
  447. $web_course_path = api_get_path(WEB_COURSE_PATH);
  448. $category = Database::escape_string($_GET['category']);
  449. $setting_show_also_closed_courses = api_get_setting('show_closed_courses') == 'true';
  450. // Database table definitions.
  451. $main_course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  452. $main_category_table = Database :: get_main_table(TABLE_MAIN_CATEGORY);
  453. // Get list of courses in category $category.
  454. $sql_get_course_list = "SELECT * FROM $main_course_table cours
  455. WHERE category_code = '".Database::escape_string($_GET['category'])."'
  456. ORDER BY title, UPPER(visual_code)";
  457. // Showing only the courses of the current access_url_id.
  458. global $_configuration;
  459. if ($_configuration['multiple_access_urls']) {
  460. $url_access_id = api_get_current_access_url_id();
  461. if ($url_access_id != -1) {
  462. $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  463. $sql_get_course_list = "SELECT * FROM $main_course_table as course INNER JOIN $tbl_url_rel_course as url_rel_course
  464. ON (url_rel_course.course_code=course.code)
  465. WHERE access_url_id = $url_access_id AND category_code = '".Database::escape_string($_GET['category'])."' ORDER BY title, UPPER(visual_code)";
  466. }
  467. }
  468. // Removed: AND cours.visibility='".COURSE_VISIBILITY_OPEN_WORLD."'
  469. $sql_result_courses = Database::query($sql_get_course_list);
  470. while ($course_result = Database::fetch_array($sql_result_courses)) {
  471. $course_list[] = $course_result;
  472. }
  473. $platform_visible_courses = '';
  474. // $setting_show_also_closed_courses
  475. if ($user_identified) {
  476. if ($setting_show_also_closed_courses) {
  477. $platform_visible_courses = '';
  478. } else {
  479. $platform_visible_courses = " AND (t3.visibility='".COURSE_VISIBILITY_OPEN_WORLD."' OR t3.visibility='".COURSE_VISIBILITY_OPEN_PLATFORM."' )";
  480. }
  481. } else {
  482. if ($setting_show_also_closed_courses) {
  483. $platform_visible_courses = '';
  484. } else {
  485. $platform_visible_courses = " AND (t3.visibility='".COURSE_VISIBILITY_OPEN_WORLD."' )";
  486. }
  487. }
  488. $sqlGetSubCatList = "
  489. SELECT t1.name,t1.code,t1.parent_id,t1.children_count,COUNT(DISTINCT t3.code) AS nbCourse
  490. FROM $main_category_table t1
  491. LEFT JOIN $main_category_table t2 ON t1.code=t2.parent_id
  492. LEFT JOIN $main_course_table t3 ON (t3.category_code=t1.code $platform_visible_courses)
  493. WHERE t1.parent_id ".(empty($category) ? "IS NULL" : "='$category'")."
  494. GROUP BY t1.name,t1.code,t1.parent_id,t1.children_count ORDER BY t1.tree_pos, t1.name";
  495. // Showing only the category of courses of the current access_url_id
  496. if ($_configuration['multiple_access_urls']) {
  497. $url_access_id = api_get_current_access_url_id();
  498. if ($url_access_id != -1) {
  499. $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  500. $sqlGetSubCatList = "
  501. SELECT t1.name,t1.code,t1.parent_id,t1.children_count,COUNT(DISTINCT t3.code) AS nbCourse
  502. FROM $main_category_table t1
  503. LEFT JOIN $main_category_table t2 ON t1.code=t2.parent_id
  504. LEFT JOIN $main_course_table t3 ON (t3.category_code=t1.code $platform_visible_courses)
  505. INNER JOIN $tbl_url_rel_course as url_rel_course
  506. ON (url_rel_course.course_code=t3.code)
  507. WHERE access_url_id = $url_access_id AND t1.parent_id ".(empty($category) ? "IS NULL" : "='$category'")."
  508. GROUP BY t1.name,t1.code,t1.parent_id,t1.children_count ORDER BY t1.tree_pos, t1.name";
  509. }
  510. }
  511. $resCats = Database::query($sqlGetSubCatList);
  512. $thereIsSubCat = false;
  513. if (Database::num_rows($resCats) > 0) {
  514. $htmlListCat = Display::page_header(get_lang('CatList'));
  515. $htmlListCat .= '<ul>';
  516. while ($catLine = Database::fetch_array($resCats)) {
  517. if ($catLine['code'] != $category) {
  518. $category_has_open_courses = self::category_has_open_courses($catLine['code']);
  519. if ($category_has_open_courses) {
  520. // The category contains courses accessible to anonymous visitors.
  521. $htmlListCat .= '<li>';
  522. $htmlListCat .= '<a href="'.api_get_self().'?category='.$catLine['code'].'">'.$catLine['name'].'</a>';
  523. if (api_get_setting('show_number_of_courses') == 'true') {
  524. $htmlListCat .= ' ('.$catLine['nbCourse'].' '.get_lang('Courses').')';
  525. }
  526. $htmlListCat .= "</li>";
  527. $thereIsSubCat = true;
  528. } elseif ($catLine['children_count'] > 0) {
  529. // The category has children, subcategories.
  530. $htmlListCat .= '<li>';
  531. $htmlListCat .= '<a href="'.api_get_self().'?category='.$catLine['code'].'">'.$catLine['name'].'</a>';
  532. $htmlListCat .= "</li>";
  533. $thereIsSubCat = true;
  534. }
  535. /* End changed code to eliminate the (0 courses) after empty categories. */ elseif (api_get_setting('show_empty_course_categories') == 'true') {
  536. $htmlListCat .= '<li>';
  537. $htmlListCat .= $catLine['name'];
  538. $htmlListCat .= "</li>";
  539. $thereIsSubCat = true;
  540. } // Else don't set thereIsSubCat to true to avoid printing things if not requested.
  541. } else {
  542. $htmlTitre = '<p>';
  543. if (api_get_setting('show_back_link_on_top_of_tree') == 'true') {
  544. $htmlTitre .= '<a href="'.api_get_self().'">&lt;&lt; '.get_lang('BackToHomePage').'</a>';
  545. }
  546. if (!is_null($catLine['parent_id']) || (api_get_setting('show_back_link_on_top_of_tree') != 'true' && !is_null($catLine['code']))) {
  547. $htmlTitre .= '<a href="'.api_get_self().'?category='.$catLine['parent_id'].'">&lt;&lt; '.get_lang('Up').'</a>';
  548. }
  549. $htmlTitre .= "</p>";
  550. if ($category != "" && !is_null($catLine['code'])) {
  551. $htmlTitre .= '<h3>'.$catLine['name']."</h3>";
  552. } else {
  553. $htmlTitre .= '<h3>'.get_lang('Categories')."</h3>";
  554. }
  555. }
  556. }
  557. $htmlListCat .= "</ul>";
  558. }
  559. $result .= $htmlTitre;
  560. if ($thereIsSubCat) {
  561. $result .= $htmlListCat;
  562. }
  563. while ($categoryName = Database::fetch_array($resCats)) {
  564. $result .= '<h3>'.$categoryName['name']."</h3>\n";
  565. }
  566. $numrows = Database::num_rows($sql_result_courses);
  567. $courses_list_string = '';
  568. $courses_shown = 0;
  569. if ($numrows > 0) {
  570. $courses_list_string .= Display::page_header(get_lang('CourseList'));
  571. $courses_list_string .= "<ul>";
  572. if (api_get_user_id()) {
  573. $courses_of_user = self::get_courses_of_user(api_get_user_id());
  574. }
  575. foreach ($course_list as $course) {
  576. // $setting_show_also_closed_courses
  577. if (!$setting_show_also_closed_courses) {
  578. // If we do not show the closed courses
  579. // we only show the courses that are open to the world (to everybody)
  580. // and the courses that are open to the platform (if the current user is a registered user.
  581. if (($user_identified && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM) || ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD)) {
  582. $courses_shown++;
  583. $courses_list_string .= "<li>\n";
  584. $courses_list_string .= '<a href="'.$web_course_path.$course['directory'].'/">'.$course['title'].'</a><br />';
  585. $course_details = array();
  586. if (api_get_setting('display_coursecode_in_courselist') == 'true') {
  587. $course_details[] = $course['visual_code'];
  588. }
  589. if (api_get_setting('display_teacher_in_courselist') == 'true') {
  590. $course_details[] = $course['tutor_name'];
  591. }
  592. if (api_get_setting('show_different_course_language') == 'true' && $course['course_language'] != api_get_setting('platformLanguage')) {
  593. $course_details[] = $course['course_language'];
  594. }
  595. $courses_list_string .= implode(' - ', $course_details);
  596. $courses_list_string .= "</li>\n";
  597. }
  598. } else {
  599. // We DO show the closed courses.
  600. // The course is accessible if (link to the course homepage):
  601. // 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);
  602. // 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);
  603. // 3. the user is logged in and the user is subscribed to the course and the course visibility is not COURSE_VISIBILITY_CLOSED;
  604. // 4. the user is logged in and the user is course admin of te course (regardless of the course visibility setting);
  605. // 5. the user is the platform admin api_is_platform_admin().
  606. //
  607. $courses_shown++;
  608. $courses_list_string .= "<li>\n";
  609. if ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD
  610. || ($user_identified && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM)
  611. || ($user_identified && key_exists($course['code'], $courses_of_user) && $course['visibility'] != COURSE_VISIBILITY_CLOSED)
  612. || $courses_of_user[$course['code']]['status'] == '1'
  613. || api_is_platform_admin()) {
  614. $courses_list_string .= '<a href="'.$web_course_path.$course['directory'].'/">';
  615. }
  616. $courses_list_string .= $course['title'];
  617. if ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD
  618. || ($user_identified && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM)
  619. || ($user_identified && key_exists($course['code'], $courses_of_user) && $course['visibility'] != COURSE_VISIBILITY_CLOSED)
  620. || $courses_of_user[$course['code']]['status'] == '1'
  621. || api_is_platform_admin()) {
  622. $courses_list_string .= '</a><br />';
  623. }
  624. $course_details = array();
  625. if (api_get_setting('display_coursecode_in_courselist') == 'true') {
  626. $course_details[] = $course['visual_code'];
  627. }
  628. // if (api_get_setting('display_coursecode_in_courselist') == 'true' && api_get_setting('display_teacher_in_courselist') == 'true') {
  629. // $courses_list_string .= ' - ';
  630. // }
  631. if (api_get_setting('display_teacher_in_courselist') == 'true') {
  632. $course_details[] = $course['tutor_name'];
  633. }
  634. if (api_get_setting('show_different_course_language') == 'true' && $course['course_language'] != api_get_setting('platformLanguage')) {
  635. $course_details[] = $course['course_language'];
  636. }
  637. if (api_get_setting('show_different_course_language') == 'true' && $course['course_language'] != api_get_setting('platformLanguage')) {
  638. $course_details[] = $course['course_language'];
  639. }
  640. $courses_list_string .= implode(' - ', $course_details);
  641. // We display a subscription link if:
  642. // 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
  643. // 2.
  644. if ($user_identified && !key_exists($course['code'], $courses_of_user)) {
  645. if ($course['subscribe'] == '1') {
  646. $courses_list_string .= '<form action="main/auth/courses.php?action=subscribe&category='.Security::remove_XSS($_GET['category']).'" method="post">';
  647. $courses_list_string .= '<input type="hidden" name="sec_token" value="'.$stok.'">';
  648. $courses_list_string .= '<input type="hidden" name="subscribe" value="'.$course['code'].'" />';
  649. $courses_list_string .= '<input type="image" name="unsub" src="main/img/enroll.gif" alt="'.get_lang('Subscribe').'" />'.get_lang('Subscribe').'</form>';
  650. } else {
  651. $courses_list_string .= '<br />'.get_lang('SubscribingNotAllowed');
  652. }
  653. }
  654. $courses_list_string .= "</li>";
  655. } //end else
  656. } // end foreach
  657. $courses_list_string .= "</ul>";
  658. }
  659. if ($courses_shown > 0) {
  660. // Only display the list of courses and categories if there was more than
  661. // 0 courses visible to the world (we're in the anonymous list here).
  662. $result .= $courses_list_string;
  663. }
  664. if ($category != '') {
  665. $result .= '<p><a href="'.api_get_self().'"> '.Display :: return_icon('back.png', get_lang('BackToHomePage')).get_lang('BackToHomePage').'</a></p>';
  666. }
  667. return $result;
  668. }
  669. /**
  670. * The most important function here, prints the session and course
  671. * list (user_portal.php)
  672. * @param int User ID
  673. * @return string HTML list of sessions and courses
  674. * @assert () === false
  675. */
  676. static function return_courses_and_sessions($user_id) {
  677. if (empty($user_id)) {
  678. return false;
  679. }
  680. $session_categories = array();
  681. $load_history = (isset($_GET['history']) && intval($_GET['history']) == 1) ? true : false;
  682. if ($load_history) {
  683. //Load sessions in category in *history*
  684. $session_categories = UserManager::get_sessions_by_category($user_id, true, false, true);
  685. } else {
  686. //Load sessions in category
  687. $session_categories = UserManager::get_sessions_by_category($user_id, false);
  688. }
  689. $html = '';
  690. //Showing history title
  691. if ($load_history) {
  692. $html .= Display::page_subheader(get_lang('HistoryTrainingSession'));
  693. if (empty($session_categories)) {
  694. $html .= get_lang('YouDoNotHaveAnySessionInItsHistory');
  695. }
  696. }
  697. $courses_html = '';
  698. $special_courses = '';
  699. $load_directories_preview = api_get_setting('show_documents_preview') == 'true' ? true : false;
  700. // If we're not in the history view...
  701. if (!isset($_GET['history'])) {
  702. //Display special courses
  703. $special_courses = CourseManager::display_special_courses($user_id, $load_directories_preview);
  704. //Display courses
  705. $courses_html .= CourseManager::display_courses($user_id, $load_directories_preview);
  706. }
  707. $sessions_with_category = '';
  708. $sessions_with_no_category = '';
  709. if (is_array($session_categories)) {
  710. foreach ($session_categories as $session_category) {
  711. $session_category_id = $session_category['session_category']['id'];
  712. // Sessions and courses that are not in a session category
  713. if ($session_category_id == 0) {
  714. // Independent sessions
  715. if (isset($session_category['sessions'])) {
  716. foreach ($session_category['sessions'] as $session) {
  717. $session_id = $session['session_id'];
  718. // Don't show empty sessions.
  719. if (count($session['courses']) < 1) {
  720. continue;
  721. }
  722. $html_courses_session = '';
  723. $count_courses_session = 0;
  724. foreach ($session['courses'] as $course) {
  725. //read only and accesible
  726. if (api_get_setting('hide_courses_in_sessions') == 'false') {
  727. $html_courses_session .= CourseManager :: get_logged_user_course_html($course, $session_id, $load_directories_preview);
  728. }
  729. $count_courses_session++;
  730. }
  731. if ($count_courses_session > 0) {
  732. $params = array();
  733. $params['icon'] = Display::return_icon('window_list.png', $session['session_name'], array('id' => 'session_img_'.$session_id), ICON_SIZE_LARGE);
  734. $params['is_session'] = true;
  735. //Default session name
  736. $session_link = $session['session_name'];
  737. $params['link'] = null;
  738. if (api_get_setting('session_page_enabled') == 'true' && !api_is_drh()) {
  739. //session name with link
  740. $session_link = Display::tag('a', $session['session_name'], array('href' => api_get_path(WEB_CODE_PATH).'session/index.php?session_id='.$session_id));
  741. $params['link'] = api_get_path(WEB_CODE_PATH).'session/index.php?session_id='.$session_id;
  742. }
  743. $params['title'] = $session_link;
  744. $moved_status = SessionManager::get_session_change_user_reason($session['moved_status']);
  745. $moved_status = isset($moved_status) && !empty($moved_status) ? ' ('.$moved_status.')' : null;
  746. $params['subtitle'] = isset($session['coach_info']) ? $session['coach_info']['complete_name'] : null.$moved_status;
  747. $params['dates'] = $session['date_message'];
  748. $params['right_actions'] = '';
  749. if (api_is_platform_admin()) {
  750. $params['right_actions'] .= '<a href="'.api_get_path(WEB_CODE_PATH).'admin/resume_session.php?id_session='.$session_id.'">';
  751. $params['right_actions'] .= Display::return_icon('edit.png', get_lang('Edit'), array('align' => 'absmiddle'), ICON_SIZE_SMALL).'</a>';
  752. }
  753. if (api_get_setting('hide_courses_in_sessions') == 'false') {
  754. // $params['extra'] .= $html_courses_session;
  755. }
  756. $sessions_with_no_category .= CourseManager::course_item_parent(CourseManager::course_item_html($params, true), $html_courses_session);
  757. }
  758. }
  759. }
  760. } else {
  761. // All sessions included in
  762. $count_courses_session = 0;
  763. $html_sessions = '';
  764. foreach ($session_category['sessions'] as $session) {
  765. $session_id = $session['session_id'];
  766. // Don't show empty sessions.
  767. if (count($session['courses']) < 1) {
  768. continue;
  769. }
  770. $html_courses_session = '';
  771. $count = 0;
  772. foreach ($session['courses'] as $course) {
  773. if (api_get_setting('hide_courses_in_sessions') == 'false') {
  774. $html_courses_session .= CourseManager :: get_logged_user_course_html($course, $session_id);
  775. }
  776. $count_courses_session++;
  777. $count++;
  778. }
  779. $params = array();
  780. if ($count > 0) {
  781. $params['icon'] = Display::return_icon('window_list.png', $session['session_name'], array('id' => 'session_img_'.$session_id), ICON_SIZE_LARGE);
  782. //Default session name
  783. $session_link = $session['session_name'];
  784. $params['link'] = null;
  785. if (api_get_setting('session_page_enabled') == 'true' && !api_is_drh()) {
  786. //session name with link
  787. $session_link = Display::tag('a', $session['session_name'], array('href' => api_get_path(WEB_CODE_PATH).'session/index.php?session_id='.$session_id));
  788. $params['link'] = api_get_path(WEB_CODE_PATH).'session/index.php?session_id='.$session_id;
  789. }
  790. $params['title'] .= $session_link;
  791. $moved_status = SessionManager::get_session_change_user_reason($session['moved_status']);
  792. $moved_status = isset($moved_status) && !empty($moved_status) ? ' ('.$moved_status.')' : null;
  793. $params['subtitle'] = isset($session['coach_info']) ? $session['coach_info']['complete_name'] : null.$moved_status;
  794. $params['dates'] = $session['date_message'];
  795. if (api_is_platform_admin()) {
  796. $params['right_actions'] .= '<a href="'.api_get_path(WEB_CODE_PATH).'admin/resume_session.php?id_session='.$session_id.'">'.Display::return_icon('edit.png', get_lang('Edit'), array('align' => 'absmiddle'), ICON_SIZE_SMALL).'</a>';
  797. }
  798. $html_sessions .= CourseManager::course_item_html($params, true).$html_courses_session;
  799. }
  800. }
  801. if ($count_courses_session > 0) {
  802. $params = array();
  803. $params['icon'] = Display::return_icon('folder_blue.png', $session_category['session_category']['name'], array(), ICON_SIZE_LARGE);
  804. if (api_is_platform_admin()) {
  805. $params['right_actions'] .= '<a href="'.api_get_path(WEB_CODE_PATH).'admin/session_category_edit.php?&id='.$session_category['session_category']['id'].'">'.Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL).'</a>';
  806. }
  807. $params['title'] .= $session_category['session_category']['name'];
  808. if (api_is_platform_admin()) {
  809. $params['link'] = api_get_path(WEB_CODE_PATH).'admin/session_category_edit.php?&id='.$session_category['session_category']['id'];
  810. }
  811. $session_category_start_date = $session_category['session_category']['date_start'];
  812. $session_category_end_date = $session_category['session_category']['date_end'];
  813. if (!empty($session_category_start_date) && $session_category_start_date != '0000-00-00' && !empty($session_category_end_date) && $session_category_end_date != '0000-00-00') {
  814. $params['subtitle'] = sprintf(get_lang('FromDateXToDateY'), $session_category['session_category']['date_start'], $session_category['session_category']['date_end']);
  815. } else {
  816. if (!empty($session_category_start_date) && $session_category_start_date != '0000-00-00') {
  817. $params['subtitle'] = get_lang('From').' '.$session_category_start_date;
  818. }
  819. if (!empty($session_category_end_date) && $session_category_end_date != '0000-00-00') {
  820. $params['subtitle'] = get_lang('Until').' '.$session_category_end_date;
  821. }
  822. }
  823. $sessions_with_category .= CourseManager::course_item_parent(CourseManager::course_item_html($params, true), $html_sessions);
  824. }
  825. }
  826. }
  827. }
  828. return $sessions_with_category.$sessions_with_no_category.$courses_html.$special_courses;
  829. }
  830. /**
  831. * Shows a welcome message when the user doesn't have any content in
  832. * the course list
  833. * @param object A Template object used to declare variables usable in the given template
  834. * @return void
  835. * @assert () === false
  836. */
  837. static function return_welcome_to_course_block($tpl) {
  838. if (empty($tpl)) {
  839. return false;
  840. }
  841. $count_courses = CourseManager::count_courses();
  842. $course_catalog_url = api_get_path(WEB_CODE_PATH).'auth/courses.php';
  843. $course_list_url = api_get_path(WEB_PATH).'user_portal.php';
  844. $tpl->assign('course_catalog_url', $course_catalog_url);
  845. $tpl->assign('course_list_url', $course_list_url);
  846. $tpl->assign('course_catalog_link', Display::url(get_lang('here'), $course_catalog_url));
  847. $tpl->assign('course_list_link', Display::url(get_lang('here'), $course_list_url));
  848. $tpl->assign('count_courses', $count_courses);
  849. $tpl->assign('welcome_to_course_block', 1);
  850. }
  851. static function return_debug() {
  852. global $app;
  853. $mtime = microtime();
  854. $mtime = explode(" ",$mtime);
  855. $mtime = $mtime[1] + $mtime[0];
  856. $message = "22---Page loaded in:".($mtime-START);
  857. $app['monolog']->addInfo($message);
  858. $message = "memory_get_usage: ".format_file_size(memory_get_usage(true));
  859. $app['monolog']->addInfo($message);
  860. $message = "memory_get_peak_usage: ".format_file_size(memory_get_peak_usage(true));
  861. $app['monolog']->addInfo($message);
  862. }
  863. }