page.lib.php 48 KB

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