userportal.lib.php 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class IndexManager
  5. */
  6. class IndexManager
  7. {
  8. // An instance of the template engine
  9. public $tpl = false;
  10. public $name = '';
  11. public $home = '';
  12. public $default_home = 'home/';
  13. /**
  14. * Construct
  15. * @param string $title
  16. */
  17. public function __construct($title)
  18. {
  19. $this->tpl = new Template($title);
  20. $this->home = api_get_home_path();
  21. $this->user_id = api_get_user_id();
  22. $this->load_directories_preview = false;
  23. if (api_get_setting('show_documents_preview') == 'true') {
  24. $this->load_directories_preview = true;
  25. }
  26. }
  27. /**
  28. * @param bool $setLoginForm
  29. */
  30. function set_login_form($setLoginForm = true)
  31. {
  32. global $loginFailed;
  33. $login_form = '';
  34. if (!($this->user_id) || api_is_anonymous($this->user_id)) {
  35. // Only display if the user isn't logged in.
  36. $this->tpl->assign('login_language_form', api_display_language_form(true));
  37. if ($setLoginForm) {
  38. $this->tpl->assign('login_form', self::display_login_form());
  39. if ($loginFailed) {
  40. $this->tpl->assign('login_failed', self::handle_login_failed());
  41. }
  42. if (api_get_setting('allow_lostpassword') == 'true' || api_get_setting('allow_registration') == 'true') {
  43. $login_form .= '<ul class="nav nav-pills nav-stacked">';
  44. if (api_get_setting('allow_registration') != 'false') {
  45. $login_form .= '<li><a href="main/auth/inscription.php">'.get_lang('SignUp').'</a></li>';
  46. }
  47. if (api_get_setting('allow_lostpassword') == 'true') {
  48. $login_form .= '<li><a href="main/auth/lostPassword.php">'.get_lang('LostPassword').'</a></li>';
  49. }
  50. $login_form .= '</ul>';
  51. }
  52. $this->tpl->assign('login_options', $login_form);
  53. }
  54. }
  55. }
  56. function return_exercise_block($personal_course_list)
  57. {
  58. $exercise_list = array();
  59. if (!empty($personal_course_list)) {
  60. foreach($personal_course_list as $course_item) {
  61. $course_code = $course_item['c'];
  62. $session_id = $course_item['id_session'];
  63. $exercises = ExerciseLib::get_exercises_to_be_taken($course_code, $session_id);
  64. foreach($exercises as $exercise_item) {
  65. $exercise_item['course_code'] = $course_code;
  66. $exercise_item['session_id'] = $session_id;
  67. $exercise_item['tms'] = api_strtotime($exercise_item['end_time'], 'UTC');
  68. $exercise_list[] = $exercise_item;
  69. }
  70. }
  71. if (!empty($exercise_list)) {
  72. $exercise_list = msort($exercise_list, 'tms');
  73. $my_exercise = $exercise_list[0];
  74. $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']);
  75. $this->tpl->assign('exercise_url', $url);
  76. $this->tpl->assign('exercise_end_date', api_convert_and_format_date($my_exercise['end_time'], DATE_FORMAT_SHORT));
  77. }
  78. }
  79. }
  80. function return_announcements($show_slide = true) {
  81. //// Display System announcements
  82. $hideAnnouncements = api_get_setting('hide_global_announcements_when_not_connected');
  83. if ($hideAnnouncements == 'true' && empty($userId)) {
  84. return null;
  85. }
  86. $announcement = isset($_GET['announcement']) ? $_GET['announcement'] : null;
  87. $announcement = intval($announcement);
  88. if (!api_is_anonymous() && $this->user_id) {
  89. $visibility = api_is_allowed_to_create_course() ? SystemAnnouncementManager::VISIBLE_TEACHER : SystemAnnouncementManager::VISIBLE_STUDENT;
  90. if ($show_slide) {
  91. $announcements = SystemAnnouncementManager :: display_announcements_slider($visibility, $announcement);
  92. } else {
  93. $announcements = SystemAnnouncementManager :: display_all_announcements($visibility, $announcement);
  94. }
  95. } else {
  96. if ($show_slide) {
  97. $announcements = SystemAnnouncementManager :: display_announcements_slider(SystemAnnouncementManager::VISIBLE_GUEST, $announcement);
  98. } else {
  99. $announcements = SystemAnnouncementManager :: display_all_announcements(SystemAnnouncementManager::VISIBLE_GUEST, $announcement);
  100. }
  101. }
  102. return $announcements;
  103. }
  104. /**
  105. * Alias for the online_logout() function
  106. */
  107. function logout() {
  108. online_logout($this->user_id, true);
  109. }
  110. /**
  111. * This function checks if there are courses that are open to the world in the platform course categories (=faculties)
  112. *
  113. * @param string $category
  114. * @return boolean
  115. */
  116. function category_has_open_courses($category) {
  117. $setting_show_also_closed_courses = api_get_setting('show_closed_courses') == 'true';
  118. $main_course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  119. $category = Database::escape_string($category);
  120. $sql_query = "SELECT * FROM $main_course_table WHERE category_code='$category'";
  121. $sql_result = Database::query($sql_query);
  122. while ($course = Database::fetch_array($sql_result)) {
  123. if (!$setting_show_also_closed_courses) {
  124. if ((api_get_user_id() > 0 && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM) || ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD)) {
  125. return true; //at least one open course
  126. }
  127. } else {
  128. if (isset($course['visibility'])) {
  129. return true; // At least one course (it does not matter weither it's open or not because $setting_show_also_closed_courses = true).
  130. }
  131. }
  132. }
  133. return false;
  134. }
  135. /**
  136. * Displays the right-hand menu for anonymous users:
  137. * login form, useful links, help section
  138. * Warning: function defines globals
  139. * @version 1.0.1
  140. * @todo does $_plugins need to be global?
  141. */
  142. function display_anonymous_right_menu() {
  143. global $loginFailed, $_user;
  144. $display_add_course_link = api_is_allowed_to_create_course() && ($_SESSION['studentview'] != 'studentenview');
  145. $current_user_id = api_get_user_id();
  146. echo self::set_login_form(false);
  147. echo self::return_teacher_link();
  148. echo self::return_notice();
  149. }
  150. function return_teacher_link()
  151. {
  152. $html = '';
  153. $show_menu = false;
  154. if (!empty($this->user_id)) {
  155. // tabs that are deactivated are added here
  156. $show_menu = false;
  157. $show_create_link = false;
  158. $show_course_link = false;
  159. if (api_is_platform_admin() || api_is_course_admin() || api_is_allowed_to_create_course()) {
  160. $show_menu = true;
  161. $show_course_link = true;
  162. $show_create_link = true;
  163. } else {
  164. if (api_get_setting('allow_students_to_browse_courses') == 'true') {
  165. $show_menu = true;
  166. $show_course_link = true;
  167. }
  168. }
  169. if ($show_menu && ($show_create_link || $show_course_link )) {
  170. $show_menu = true;
  171. } else {
  172. $show_menu = false;
  173. }
  174. }
  175. // My Account section
  176. if ($show_menu) {
  177. $html .= '<ul class="nav nav-pills nav-stacked">';
  178. if ($show_create_link) {
  179. $html .= '<li class="add-course"><a href="' . api_get_path(WEB_CODE_PATH) . 'create_course/add_course.php">'.(api_get_setting('course_validation') == 'true' ? get_lang('CreateCourseRequest') : get_lang('CourseCreate')).'</a></li>';
  180. }
  181. if ($show_course_link) {
  182. if (!api_is_drh() && !api_is_session_admin()) {
  183. $html .= '<li class="list-course"><a href="' . api_get_path(WEB_CODE_PATH) . 'auth/courses.php">'.get_lang('CourseCatalog').'</a></li>';
  184. } else {
  185. $html .= '<li><a href="' . api_get_path(WEB_CODE_PATH) . 'dashboard/index.php">'.get_lang('Dashboard').'</a></li>';
  186. }
  187. }
  188. $html .= '</ul>';
  189. }
  190. if (!empty($html)) {
  191. $html = self::show_right_block(get_lang('Courses'), $html, 'teacher_block');
  192. }
  193. return $html;
  194. }
  195. /**
  196. * Includes a created page
  197. * @return string
  198. */
  199. public function return_home_page()
  200. {
  201. $userId = api_get_user_id();
  202. global $_configuration;
  203. // Including the page for the news
  204. $html = '';
  205. if (!empty($_GET['include']) && preg_match('/^[a-zA-Z0-9_-]*\.html$/', $_GET['include'])) {
  206. $open = @(string)file_get_contents(api_get_path(SYS_PATH).$this->home.$_GET['include']);
  207. $html = api_to_system_encoding($open, api_detect_encoding(strip_tags($open)));
  208. } else {
  209. // Hiding home top when user not connected.
  210. $hideTop = api_get_setting('hide_home_top_when_connected');
  211. if ($hideTop == 'true' && !empty($userId)) {
  212. return $html;
  213. }
  214. if (!empty($_SESSION['user_language_choice'])) {
  215. $user_selected_language = $_SESSION['user_language_choice'];
  216. } elseif (!empty($_SESSION['_user']['language'])) {
  217. $user_selected_language = $_SESSION['_user']['language'];
  218. } else {
  219. $user_selected_language = api_get_setting('platformLanguage');
  220. }
  221. // Try language specific home
  222. if (file_exists($this->home.'home_top_'.$user_selected_language.'.html')) {
  223. $home_top_temp = file_get_contents($this->home.'home_top_'.$user_selected_language.'.html');
  224. }
  225. // Try default language home
  226. if (empty($home_top_temp)) {
  227. if (file_exists($this->home.'home_top.html')) {
  228. $home_top_temp = file_get_contents($this->home.'home_top.html');
  229. } else {
  230. if (file_exists($this->default_home.'home_top.html')) {
  231. $home_top_temp = file_get_contents($this->default_home . 'home_top.html');
  232. }
  233. }
  234. }
  235. if (trim($home_top_temp) == '' && api_is_platform_admin()) {
  236. $home_top_temp = '<div class="welcome-mascot">' . get_lang('PortalHomepageDefaultIntroduction') . '</div>';
  237. } else {
  238. $home_top_temp = '<div class="welcome-home-top-temp">' . $home_top_temp . '</div>';
  239. }
  240. $open = str_replace('{rel_path}', api_get_path(REL_PATH), $home_top_temp);
  241. $html = api_to_system_encoding($open, api_detect_encoding(strip_tags($open)));
  242. }
  243. return $html;
  244. }
  245. function return_notice() {
  246. $sys_path = api_get_path(SYS_PATH);
  247. $user_selected_language = api_get_interface_language();
  248. $html = '';
  249. // Notice
  250. $home_notice = @(string)file_get_contents($sys_path.$this->home.'home_notice_'.$user_selected_language.'.html');
  251. if (empty($home_notice)) {
  252. $home_notice = @(string)file_get_contents($sys_path.$this->home.'home_notice.html');
  253. }
  254. if (!empty($home_notice)) {
  255. $home_notice = api_to_system_encoding($home_notice, api_detect_encoding(strip_tags($home_notice)));
  256. $home_notice = Display::div($home_notice, array('class' => 'homepage_notice'));
  257. $html = self::show_right_block(get_lang('Notice'), $home_notice, 'notice_block');
  258. }
  259. return $html;
  260. }
  261. function return_help() {
  262. $user_selected_language = api_get_interface_language();
  263. $sys_path = api_get_path(SYS_PATH);
  264. $platformLanguage = api_get_setting('platformLanguage');
  265. // Help section.
  266. /* Hide right menu "general" and other parts on anonymous right menu. */
  267. if (!isset($user_selected_language)) {
  268. $user_selected_language = $platformLanguage;
  269. }
  270. $html = null;
  271. $home_menu = @(string)file_get_contents($sys_path.$this->home.'home_menu_'.$user_selected_language.'.html');
  272. if (!empty($home_menu)) {
  273. $home_menu_content = '<ul class="nav nav-pills nav-stacked">';
  274. $home_menu_content .= api_to_system_encoding($home_menu, api_detect_encoding(strip_tags($home_menu)));
  275. $home_menu_content .= '</ul>';
  276. $html .= self::show_right_block(get_lang('MenuGeneral'), $home_menu_content, 'help_block');
  277. }
  278. return $html;
  279. }
  280. function return_skills_links() {
  281. $html = '';
  282. $content = '';
  283. $content .= '<ul class="nav nav-pills nav-stacked">';
  284. /**
  285. * Generate the block for show a panel with links to My Certificates and Certificates Search pages
  286. * @return string The HTML code for the panel
  287. */
  288. $certificatesItem = null;
  289. if (!api_is_anonymous()) {
  290. $certificatesItem = Display::tag(
  291. 'li',
  292. Display::url(Display::return_icon('graduation.png',get_lang('MyCertificates'),null,ICON_SIZE_SMALL).
  293. get_lang('MyCertificates'),
  294. api_get_path(WEB_CODE_PATH) . "gradebook/my_certificates.php"
  295. )
  296. );
  297. }
  298. $searchItem = null;
  299. if (api_get_setting('allow_public_certificates') == 'true') {
  300. $searchItem = Display::tag(
  301. 'li',
  302. Display::url(Display::return_icon('search_graduation.png',get_lang('Search'),null,ICON_SIZE_SMALL).
  303. get_lang('Search'),
  304. api_get_path(WEB_CODE_PATH) . "gradebook/search.php"
  305. )
  306. );
  307. }
  308. if (empty($certificatesItem) && empty($searchItem)) {
  309. return null;
  310. }else{
  311. $content.= $certificatesItem;
  312. $content.= $searchItem;
  313. }
  314. if (api_get_setting('allow_skills_tool') == 'true') {
  315. $content .= Display::tag('li', Display::url(Display::return_icon('skill-badges.png',get_lang('MySkills'),null,ICON_SIZE_SMALL).get_lang('MySkills'), api_get_path(WEB_CODE_PATH).'social/my_skills_report.php'));
  316. $allowSkillsManagement = api_get_setting('allow_hr_skills_management') == 'true';
  317. if (($allowSkillsManagement && api_is_drh()) || api_is_platform_admin()) {
  318. $content .= Display::tag('li',
  319. Display::url(Display::return_icon('edit-skill.png', get_lang('MySkills'), null,
  320. ICON_SIZE_SMALL) . get_lang('ManageSkills'),
  321. api_get_path(WEB_CODE_PATH) . 'admin/skills_wheel.php'));
  322. }
  323. }
  324. $content .= '</ul>';
  325. $html = self::show_right_block(get_lang("Skills"), $content, 'skill_block');
  326. return $html;
  327. }
  328. /**
  329. * Reacts on a failed login:
  330. * Displays an explanation with a link to the registration form.
  331. *
  332. * @version 1.0.1
  333. */
  334. function handle_login_failed() {
  335. $message = get_lang('InvalidId');
  336. if (!isset($_GET['error'])) {
  337. if (api_is_self_registration_allowed()) {
  338. $message = get_lang('InvalidForSelfRegistration');
  339. }
  340. } else {
  341. switch ($_GET['error']) {
  342. case '':
  343. if (api_is_self_registration_allowed()) {
  344. $message = get_lang('InvalidForSelfRegistration');
  345. }
  346. break;
  347. case 'account_expired':
  348. $message = get_lang('AccountExpired');
  349. break;
  350. case 'account_inactive':
  351. $message = get_lang('AccountInactive');
  352. break;
  353. case 'user_password_incorrect':
  354. $message = get_lang('InvalidId');
  355. break;
  356. case 'access_url_inactive':
  357. $message = get_lang('AccountURLInactive');
  358. break;
  359. case 'wrong_captcha':
  360. $message = get_lang('TheTextYouEnteredDoesNotMatchThePicture');
  361. break;
  362. case 'blocked_by_captcha':
  363. $message = get_lang('AccountBlockedByCaptcha');
  364. break;
  365. case 'multiple_connection_not_allowed':
  366. $message = get_lang('MultipleConnectionsAreNotAllow');
  367. break;
  368. case 'unrecognize_sso_origin':
  369. //$message = get_lang('SSOError');
  370. break;
  371. }
  372. }
  373. return Display::return_message($message, 'error');
  374. }
  375. /**
  376. * Display list of courses in a category.
  377. * (for anonymous users)
  378. *
  379. * @version 1.1
  380. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University - refactoring and code cleaning
  381. * @author Julio Montoya <gugli100@gmail.com>, Beeznest template modifs
  382. */
  383. function return_courses_in_categories()
  384. {
  385. $result = '';
  386. $stok = Security::get_token();
  387. // Initialization.
  388. $user_identified = (api_get_user_id() > 0 && !api_is_anonymous());
  389. $web_course_path = api_get_path(WEB_COURSE_PATH);
  390. $category = Database::escape_string($_GET['category']);
  391. $setting_show_also_closed_courses = api_get_setting('show_closed_courses') == 'true';
  392. // Database table definitions.
  393. $main_course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  394. $main_category_table = Database :: get_main_table(TABLE_MAIN_CATEGORY);
  395. // Get list of courses in category $category.
  396. $sql_get_course_list = "SELECT * FROM $main_course_table cours
  397. WHERE category_code = '".Database::escape_string($_GET['category'])."'
  398. ORDER BY title, UPPER(visual_code)";
  399. // Showing only the courses of the current access_url_id.
  400. global $_configuration;
  401. if (api_is_multiple_url_enabled()) {
  402. $url_access_id = api_get_current_access_url_id();
  403. if ($url_access_id != -1) {
  404. $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  405. $sql_get_course_list = "SELECT * FROM $main_course_table as course
  406. INNER JOIN $tbl_url_rel_course as url_rel_course
  407. ON (url_rel_course.c_id = course.id)
  408. WHERE
  409. access_url_id = $url_access_id AND
  410. category_code = '".Database::escape_string($_GET['category'])."'
  411. ORDER BY title, UPPER(visual_code)";
  412. }
  413. }
  414. // Removed: AND cours.visibility='".COURSE_VISIBILITY_OPEN_WORLD."'
  415. $sql_result_courses = Database::query($sql_get_course_list);
  416. while ($course_result = Database::fetch_array($sql_result_courses)) {
  417. $course_list[] = $course_result;
  418. }
  419. // $setting_show_also_closed_courses
  420. if ($user_identified) {
  421. if ($setting_show_also_closed_courses) {
  422. $platform_visible_courses = '';
  423. } else {
  424. $platform_visible_courses = " AND (t3.visibility='".COURSE_VISIBILITY_OPEN_WORLD."' OR t3.visibility='".COURSE_VISIBILITY_OPEN_PLATFORM."' )";
  425. }
  426. } else {
  427. if ($setting_show_also_closed_courses) {
  428. $platform_visible_courses = '';
  429. } else {
  430. $platform_visible_courses = " AND (t3.visibility='".COURSE_VISIBILITY_OPEN_WORLD."' )";
  431. }
  432. }
  433. $sqlGetSubCatList = "
  434. SELECT t1.name,
  435. t1.code,
  436. t1.parent_id,
  437. t1.children_count,COUNT(DISTINCT t3.code) AS nbCourse
  438. FROM $main_category_table t1
  439. LEFT JOIN $main_category_table t2 ON t1.code=t2.parent_id
  440. LEFT JOIN $main_course_table t3 ON (t3.category_code = t1.code $platform_visible_courses)
  441. WHERE t1.parent_id ". (empty ($category) ? "IS NULL" : "='$category'")."
  442. GROUP BY t1.name,t1.code,t1.parent_id,t1.children_count ORDER BY t1.tree_pos, t1.name";
  443. // Showing only the category of courses of the current access_url_id
  444. if (api_is_multiple_url_enabled()) {
  445. $courseCategoryCondition = null;
  446. if (isMultipleUrlSupport()) {
  447. $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
  448. $courseCategoryCondition = " INNER JOIN $table a ON (t1.id = a.course_category_id)";
  449. }
  450. $url_access_id = api_get_current_access_url_id();
  451. if ($url_access_id != -1) {
  452. $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  453. $sqlGetSubCatList = "
  454. SELECT t1.name,
  455. t1.code,
  456. t1.parent_id,
  457. t1.children_count,
  458. COUNT(DISTINCT t3.code) AS nbCourse
  459. FROM $main_category_table t1
  460. $courseCategoryCondition
  461. LEFT JOIN $main_category_table t2 ON t1.code = t2.parent_id
  462. LEFT JOIN $main_course_table t3 ON (t3.category_code = t1.code $platform_visible_courses)
  463. INNER JOIN $tbl_url_rel_course as url_rel_course
  464. ON (url_rel_course.c_id = t3.id)
  465. WHERE url_rel_course.access_url_id = $url_access_id AND t1.parent_id ".(empty($category) ? "IS NULL" : "='$category'")."
  466. GROUP BY t1.name,t1.code,t1.parent_id,t1.children_count ORDER BY t1.tree_pos, t1.name";
  467. }
  468. }
  469. $resCats = Database::query($sqlGetSubCatList);
  470. $thereIsSubCat = false;
  471. if (Database::num_rows($resCats) > 0) {
  472. $htmlListCat = Display::page_header(get_lang('CatList'));
  473. $htmlListCat .= '<ul>';
  474. $htmlTitre = '';
  475. while ($catLine = Database::fetch_array($resCats)) {
  476. $category_has_open_courses = self::category_has_open_courses($catLine['code']);
  477. if ($category_has_open_courses) {
  478. // The category contains courses accessible to anonymous visitors.
  479. $htmlListCat .= '<li>';
  480. $htmlListCat .= '<a href="'.api_get_self().'?category='.$catLine['code'].'">'.$catLine['name'].'</a>';
  481. if (api_get_setting('show_number_of_courses') == 'true') {
  482. $htmlListCat .= ' ('.$catLine['nbCourse'].' '.get_lang('Courses').')';
  483. }
  484. $htmlListCat .= "</li>";
  485. $thereIsSubCat = true;
  486. } elseif ($catLine['children_count'] > 0) {
  487. // The category has children, subcategories.
  488. $htmlListCat .= '<li>';
  489. $htmlListCat .= '<a href="'.api_get_self().'?category='.$catLine['code'].'">'.$catLine['name'].'</a>';
  490. $htmlListCat .= "</li>";
  491. $thereIsSubCat = true;
  492. } elseif (api_get_setting('show_empty_course_categories') == 'true') {
  493. /* End changed code to eliminate the (0 courses) after empty categories. */
  494. $htmlListCat .= '<li>';
  495. $htmlListCat .= $catLine['name'];
  496. $htmlListCat .= "</li>";
  497. $thereIsSubCat = true;
  498. } // Else don't set thereIsSubCat to true to avoid printing things if not requested.
  499. // TODO: deprecate this useless feature - this includes removing system variable
  500. if (empty($htmlTitre)) {
  501. $htmlTitre = '<p>';
  502. if (api_get_setting('show_back_link_on_top_of_tree') == 'true') {
  503. $htmlTitre .= '<a href="'.api_get_self().'">&lt;&lt; '.get_lang('BackToHomePage').'</a>';
  504. }
  505. $htmlTitre .= "</p>";
  506. }
  507. }
  508. $htmlListCat .= "</ul>";
  509. }
  510. $result .= $htmlTitre;
  511. if ($thereIsSubCat) {
  512. $result .= $htmlListCat;
  513. }
  514. while ($categoryName = Database::fetch_array($resCats)) {
  515. $result .= '<h3>' . $categoryName['name'] . "</h3>\n";
  516. }
  517. $numrows = Database::num_rows($sql_result_courses);
  518. $courses_list_string = '';
  519. $courses_shown = 0;
  520. if ($numrows > 0) {
  521. $courses_list_string .= Display::page_header(get_lang('CourseList'));
  522. $courses_list_string .= "<ul>";
  523. if (api_get_user_id()) {
  524. $courses_of_user = self::get_courses_of_user(api_get_user_id());
  525. }
  526. foreach ($course_list as $course) {
  527. // $setting_show_also_closed_courses
  528. if ($course['visibility'] == COURSE_VISIBILITY_HIDDEN) { continue; }
  529. if (!$setting_show_also_closed_courses) {
  530. // If we do not show the closed courses
  531. // we only show the courses that are open to the world (to everybody)
  532. // and the courses that are open to the platform (if the current user is a registered user.
  533. if (($user_identified && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM) || ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD)) {
  534. $courses_shown++;
  535. $courses_list_string .= "<li>";
  536. $courses_list_string .= '<a href="'.$web_course_path.$course['directory'].'/">'.$course['title'].'</a><br />';
  537. $course_details = array();
  538. if (api_get_setting('display_coursecode_in_courselist') == 'true') {
  539. $course_details[] = $course['visual_code'];
  540. }
  541. if (api_get_setting('display_teacher_in_courselist') == 'true') {
  542. $course_details[] = CourseManager::get_teacher_list_from_course_code_to_string($course['code']);
  543. }
  544. if (api_get_setting('show_different_course_language') == 'true' && $course['course_language'] != api_get_setting('platformLanguage')) {
  545. $course_details[] = $course['course_language'];
  546. }
  547. $courses_list_string .= implode(' - ', $course_details);
  548. $courses_list_string .= "</li>";
  549. }
  550. } else {
  551. // We DO show the closed courses.
  552. // The course is accessible if (link to the course homepage):
  553. // 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);
  554. // 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);
  555. // 3. the user is logged in and the user is subscribed to the course and the course visibility is not COURSE_VISIBILITY_CLOSED;
  556. // 4. the user is logged in and the user is course admin of te course (regardless of the course visibility setting);
  557. // 5. the user is the platform admin api_is_platform_admin().
  558. //
  559. $courses_shown++;
  560. $courses_list_string .= "<li>";
  561. if ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD
  562. || ($user_identified && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM)
  563. || ($user_identified && array_key_exists($course['code'], $courses_of_user)
  564. && $course['visibility'] != COURSE_VISIBILITY_CLOSED)
  565. || $courses_of_user[$course['code']]['status'] == '1'
  566. || api_is_platform_admin()) {
  567. $courses_list_string .= '<a href="'.$web_course_path.$course['directory'].'/">';
  568. }
  569. $courses_list_string .= $course['title'];
  570. if ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD
  571. || ($user_identified && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM)
  572. || ($user_identified && array_key_exists($course['code'], $courses_of_user)
  573. && $course['visibility'] != COURSE_VISIBILITY_CLOSED)
  574. || $courses_of_user[$course['code']]['status'] == '1'
  575. || api_is_platform_admin()) {
  576. $courses_list_string .= '</a><br />';
  577. }
  578. $course_details = array();
  579. if (api_get_setting('display_coursecode_in_courselist') == 'true') {
  580. $course_details[] = $course['visual_code'];
  581. }
  582. // if (api_get_setting('display_coursecode_in_courselist') == 'true' && api_get_setting('display_teacher_in_courselist') == 'true') {
  583. // $courses_list_string .= ' - ';
  584. // }
  585. if (api_get_setting('display_teacher_in_courselist') == 'true') {
  586. if (!empty($course['tutor_name'])) {
  587. $course_details[] = $course['tutor_name'];
  588. }
  589. }
  590. if (api_get_setting('show_different_course_language') == 'true' && $course['course_language'] != api_get_setting('platformLanguage')) {
  591. $course_details[] = $course['course_language'];
  592. }
  593. $courses_list_string .= implode(' - ', $course_details);
  594. // We display a subscription link if:
  595. // 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
  596. // 2.
  597. if ($user_identified && !array_key_exists($course['code'], $courses_of_user)) {
  598. if ($course['subscribe'] == '1') {
  599. /*$courses_list_string .= '<form action="main/auth/courses.php?action=subscribe&category='.Security::remove_XSS($_GET['category']).'" method="post">';
  600. $courses_list_string .= '<input type="hidden" name="sec_token" value="'.$stok.'">';
  601. $courses_list_string .= '<input type="hidden" name="subscribe" value="'.$course['code'].'" />';
  602. $courses_list_string .= '<input type="image" name="unsub" src="main/img/enroll.gif" alt="'.get_lang('Subscribe').'" />'.get_lang('Subscribe').'</form>';
  603. */
  604. $courses_list_string .= '&nbsp;<a class="btn btn-primary" href="main/auth/courses.php?action=subscribe_course&amp;sec_token='.$stok.'&amp;subscribe_course='.$course['code'].'&amp;category_code='.Security::remove_XSS($_GET['category']).'">'.get_lang('Subscribe').'</a><br />';
  605. } else {
  606. $courses_list_string .= '<br />'.get_lang('SubscribingNotAllowed');
  607. }
  608. }
  609. $courses_list_string .= "</li>";
  610. } //end else
  611. } // end foreach
  612. $courses_list_string .= "</ul>";
  613. }
  614. if ($courses_shown > 0) {
  615. // Only display the list of courses and categories if there was more than
  616. // 0 courses visible to the world (we're in the anonymous list here).
  617. $result .= $courses_list_string;
  618. }
  619. if ($category != '') {
  620. $result .= '<p><a href="'.api_get_self().'"> ' . Display :: return_icon('back.png', get_lang('BackToHomePage')) . get_lang('BackToHomePage') . '</a></p>';
  621. }
  622. return $result;
  623. }
  624. /**
  625. * retrieves all the courses that the user has already subscribed to
  626. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  627. * @param int $user_id: the id of the user
  628. * @return array an array containing all the information of the courses of the given user
  629. */
  630. public function get_courses_of_user($user_id)
  631. {
  632. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  633. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  634. // Secondly we select the courses that are in a category (user_course_cat <> 0) and sort these according to the sort of the category
  635. $user_id = intval($user_id);
  636. $sql_select_courses = "SELECT
  637. course.code k,
  638. course.visual_code vc,
  639. course.subscribe subscr,
  640. course.unsubscribe unsubscr,
  641. course.title i,
  642. course.tutor_name t,
  643. course.directory dir,
  644. course_rel_user.status status,
  645. course_rel_user.sort sort,
  646. course_rel_user.user_course_cat user_course_cat
  647. FROM
  648. $table_course course,
  649. $table_course_user course_rel_user
  650. WHERE
  651. course.id = course_rel_user.c_id AND
  652. course_rel_user.user_id = '".$user_id."' AND
  653. course_rel_user.relation_type <> ".COURSE_RELATION_TYPE_RRHH."
  654. ORDER BY course_rel_user.sort ASC";
  655. $result = Database::query($sql_select_courses);
  656. $courses = array();
  657. while ($row = Database::fetch_array($result)) {
  658. // We only need the database name of the course.
  659. $courses[$row['k']] = array(
  660. 'code' => $row['k'],
  661. 'visual_code' => $row['vc'],
  662. 'title' => $row['i'],
  663. 'directory' => $row['dir'],
  664. 'status' => $row['status'],
  665. 'tutor' => $row['t'],
  666. 'subscribe' => $row['subscr'],
  667. 'unsubscribe' => $row['unsubscr'],
  668. 'sort' => $row['sort'],
  669. 'user_course_category' => $row['user_course_cat']
  670. );
  671. }
  672. return $courses;
  673. }
  674. /**
  675. * @todo use the template system
  676. */
  677. function show_right_block($title, $content, $id = null, $params = null) {
  678. if (!empty($id)) {
  679. $params['id'] = $id;
  680. }
  681. $params['class'] = 'panel panel-default';
  682. $html = null;
  683. if (!empty($title)) {
  684. $html.= '<div class="panel-heading">'.$title.'</div>';
  685. }
  686. $html.= '<div class="panel-body">'.$content.'</div>';
  687. $html = Display::div($html, $params);
  688. return $html;
  689. }
  690. /**
  691. * Adds a form to let users login
  692. * @version 1.1
  693. */
  694. public function display_login_form()
  695. {
  696. $form = new FormValidator(
  697. 'formLogin',
  698. 'POST',
  699. null,
  700. null,
  701. null,
  702. FormValidator::LAYOUT_BOX_NO_LABEL
  703. );
  704. $form->addText(
  705. 'login',
  706. get_lang('UserName'),
  707. true,
  708. array('id' => 'login', 'autofocus' => 'autofocus', 'icon' => 'user')
  709. );
  710. $form->addElement(
  711. 'password',
  712. 'password',
  713. get_lang('Pass'),
  714. array('id' => 'password', 'icon' => 'lock')
  715. );
  716. // Captcha
  717. $captcha = api_get_setting('allow_captcha');
  718. $allowCaptcha = $captcha == 'true';
  719. if ($allowCaptcha) {
  720. $useCaptcha = isset($_SESSION['loginFailed']) ? $_SESSION['loginFailed'] : null;
  721. if ($useCaptcha) {
  722. $ajax = api_get_path(WEB_AJAX_PATH).'form.ajax.php?a=get_captcha';
  723. $options = array(
  724. 'width' => 250,
  725. 'height' => 90,
  726. 'callback' => $ajax.'&var='.basename(__FILE__, '.php'),
  727. 'sessionVar' => basename(__FILE__, '.php'),
  728. 'imageOptions' => array(
  729. 'font_size' => 20,
  730. 'font_path' => api_get_path(SYS_FONTS_PATH) . 'opensans/',
  731. 'font_file' => 'OpenSans-Regular.ttf',
  732. //'output' => 'gif'
  733. )
  734. );
  735. // Minimum options using all defaults (including defaults for Image_Text):
  736. //$options = array('callback' => 'qfcaptcha_image.php');
  737. $captcha_question = $form->addElement('CAPTCHA_Image', 'captcha_question', '', $options);
  738. $form->addHtml(get_lang('ClickOnTheImageForANewOne'));
  739. $form->addElement('text', 'captcha', get_lang('EnterTheLettersYouSee'));
  740. $form->addRule('captcha', get_lang('EnterTheCharactersYouReadInTheImage'), 'required', null, 'client');
  741. $form->addRule('captcha', get_lang('TheTextYouEnteredDoesNotMatchThePicture'), 'CAPTCHA', $captcha_question);
  742. }
  743. }
  744. $form->addButton('submitAuth', get_lang('LoginEnter'), null, 'primary', null, 'btn-block');
  745. $html = $form->returnForm();
  746. // The validation is located in the local.inc
  747. /*if ($form->validate()) {
  748. // Prevent re-use of the same CAPTCHA phrase
  749. $captcha_question->destroy();
  750. }*/
  751. if (api_get_setting('openid_authentication') == 'true') {
  752. include_once 'main/auth/openid/login.php';
  753. $html .= '<div>'.openid_form().'</div>';
  754. }
  755. return $html;
  756. }
  757. /**
  758. * @todo use FormValidator
  759. * @return string
  760. */
  761. public function return_search_block()
  762. {
  763. $html = '';
  764. if (api_get_setting('search_enabled') == 'true') {
  765. $search_btn = get_lang('Search');
  766. $search_content = '<form action="main/search/" method="post">
  767. <div class="form-group">
  768. <input type="text" id="query" class="form-control" name="query" value="" />
  769. <button class="btn btn-default" type="submit" name="submit" value="'.$search_btn.'" />'.$search_btn.' </button>
  770. </div></form>';
  771. $html .= self::show_right_block(get_lang('Search'), $search_content, 'search_block');
  772. }
  773. return $html;
  774. }
  775. /**
  776. * @return string
  777. */
  778. public function return_classes_block()
  779. {
  780. $html = '';
  781. if (api_get_setting('show_groups_to_users') == 'true') {
  782. $usergroup = new UserGroup();
  783. $usergroup_list = $usergroup->get_usergroup_by_user(api_get_user_id());
  784. $classes = '';
  785. if (!empty($usergroup_list)) {
  786. foreach($usergroup_list as $group_id) {
  787. $data = $usergroup->get($group_id);
  788. $data['name'] = Display::url($data['name'], api_get_path(WEB_CODE_PATH).'user/classes.php?id='.$data['id']);
  789. $classes .= Display::tag('li', $data['name']);
  790. }
  791. }
  792. if (api_is_platform_admin()) {
  793. $classes .= Display::tag(
  794. 'li',
  795. Display::url(get_lang('AddClasses') ,api_get_path(WEB_CODE_PATH).'admin/usergroups.php?action=add')
  796. );
  797. }
  798. if (!empty($classes)) {
  799. $classes = Display::tag('ul', $classes, array('class'=>'nav nav-pills nav-stacked'));
  800. $html .= self::show_right_block(get_lang('Classes'), $classes, 'classes_block');
  801. }
  802. }
  803. return $html;
  804. }
  805. /**
  806. * @return null|string
  807. */
  808. public function return_user_image_block()
  809. {
  810. $html = null;
  811. if (!api_is_anonymous()) {
  812. $userPicture = UserManager::getUserPicture(api_get_user_id());
  813. $content = null;
  814. if (api_get_setting('allow_social_tool') == 'true') {
  815. $content .= '<a style="text-align:center" href="' . api_get_path(WEB_PATH) . 'main/social/home.php">
  816. <img src="' . $userPicture . '" ></a>';
  817. } else {
  818. $content .= '<a style="text-align:center" href="' . api_get_path(WEB_PATH) . 'main/auth/profile.php">
  819. <img title="' . get_lang('EditProfile') . '" src="' . $userPicture. '" ></a>';
  820. }
  821. $html = self::show_right_block(
  822. null,
  823. $content,
  824. 'user_image_block',
  825. array('style' => 'text-align:center;')
  826. );
  827. }
  828. return $html;
  829. }
  830. /**
  831. * @return null|string|void
  832. */
  833. public function return_profile_block()
  834. {
  835. global $_configuration;
  836. $user_id = api_get_user_id();
  837. if (empty($user_id)) {
  838. return;
  839. }
  840. $userGroup = new UserGroup();
  841. $profile_content = '<ul class="nav nav-pills nav-stacked">';
  842. // @todo Add a platform setting to add the user image.
  843. if (api_get_setting('allow_message_tool') == 'true') {
  844. // New messages.
  845. $number_of_new_messages = MessageManager::get_new_messages();
  846. // New contact invitations.
  847. $number_of_new_messages_of_friend = SocialManager::get_message_number_invitation_by_user_id(api_get_user_id());
  848. // New group invitations sent by a moderator.
  849. $group_pending_invitations = $userGroup->get_groups_by_user(
  850. api_get_user_id(),
  851. GROUP_USER_PERMISSION_PENDING_INVITATION,
  852. false
  853. );
  854. $group_pending_invitations = count($group_pending_invitations);
  855. $total_invitations = $number_of_new_messages_of_friend + $group_pending_invitations;
  856. $cant_msg = Display::badge($number_of_new_messages);
  857. $link = '';
  858. if (api_get_setting('allow_social_tool') == 'true') {
  859. $link = '?f=social';
  860. }
  861. $profile_content .= '<li class="inbox-message-social"><a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php'.$link.'">'.Display::return_icon('inbox.png',get_lang('Inbox'),null,ICON_SIZE_SMALL).get_lang('Inbox').$cant_msg.' </a></li>';
  862. $profile_content .= '<li class="new-message-social"><a href="'.api_get_path(WEB_PATH).'main/messages/new_message.php'.$link.'">'.Display::return_icon('new-message.png',get_lang('Compose'),null,ICON_SIZE_SMALL).get_lang('Compose').' </a></li>';
  863. if (api_get_setting('allow_social_tool') == 'true') {
  864. $total_invitations = Display::badge($total_invitations);
  865. $profile_content .= '<li class="invitations-social"><a href="'.api_get_path(WEB_PATH).'main/social/invitations.php">'.Display::return_icon('invitations.png',get_lang('PendingInvitations'),null,ICON_SIZE_SMALL).get_lang('PendingInvitations').$total_invitations.'</a></li>';
  866. }
  867. if (isset($_configuration['allow_my_files_link_in_homepage']) && $_configuration['allow_my_files_link_in_homepage']) {
  868. $profile_content .= '<li class="myfiles-social"><a href="'.api_get_path(WEB_PATH).'main/social/myfiles.php">'.get_lang('MyFiles').'</a></li>';
  869. }
  870. }
  871. $editProfileUrl = Display::getProfileEditionLink($user_id);
  872. $profile_content .= '<li class="profile-social"><a href="' . $editProfileUrl . '">'.Display::return_icon('edit-profile.png',get_lang('EditProfile'),null,ICON_SIZE_SMALL).get_lang('EditProfile').'</a></li>';
  873. $profile_content .= '</ul>';
  874. $html = self::show_right_block(get_lang('Profile'), $profile_content, 'profile_block');
  875. return $html;
  876. }
  877. public function return_navigation_links()
  878. {
  879. $html = '';
  880. // Deleting the myprofile link.
  881. if (api_get_setting('allow_social_tool') == 'true') {
  882. unset($this->tpl->menu_navigation['myprofile']);
  883. }
  884. // Main navigation section.
  885. // Tabs that are deactivated are added here.
  886. if (!empty($this->tpl->menu_navigation)) {
  887. $content = '<ul class="nav nav-pills nav-stacked">';
  888. foreach ($this->tpl->menu_navigation as $section => $navigation_info) {
  889. $current = $section == $GLOBALS['this_section'] ? ' id="current"' : '';
  890. $content .= '<li'.$current.'>';
  891. $content .= '<a href="'.$navigation_info['url'].'" target="_self">'.$navigation_info['title'].'</a>';
  892. $content .= '</li>';
  893. }
  894. $content .= '</ul>';
  895. $html = self::show_right_block(get_lang('MainNavigation'), $content, 'navigation_link_block');
  896. }
  897. return $html;
  898. }
  899. /**
  900. * @return null|string
  901. */
  902. public function return_course_block()
  903. {
  904. $html = '';
  905. $show_create_link = false;
  906. $show_course_link = false;
  907. if ((api_get_setting('allow_users_to_create_courses') == 'false' &&
  908. !api_is_platform_admin()) || api_is_student()
  909. ) {
  910. $display_add_course_link = false;
  911. } else {
  912. $display_add_course_link = true;
  913. }
  914. if ($display_add_course_link) {
  915. $show_create_link = true;
  916. }
  917. if (api_is_platform_admin() || api_is_course_admin() || api_is_allowed_to_create_course()) {
  918. $show_course_link = true;
  919. } else {
  920. if (api_get_setting('allow_students_to_browse_courses') == 'true') {
  921. $show_course_link = true;
  922. }
  923. }
  924. // My account section
  925. $my_account_content = '<ul class="nav nav-pills nav-stacked">';
  926. if ($show_create_link) {
  927. $my_account_content .= '<li class="add-course"><a href="main/create_course/add_course.php">';
  928. if (api_get_setting('course_validation') == 'true' && !api_is_platform_admin()) {
  929. $my_account_content .= Display::return_icon('new-course.png',get_lang('CourseCreateRequest'),null,ICON_SIZE_SMALL);
  930. $my_account_content .= get_lang('CreateCourseRequest');
  931. } else {
  932. $my_account_content .= Display::return_icon('new-course.png',get_lang('CourseCreate'),null,ICON_SIZE_SMALL);
  933. $my_account_content .= get_lang('CourseCreate');
  934. }
  935. $my_account_content .= '</a></li>';
  936. if (SessionManager::allowToManageSessions()) {
  937. $my_account_content .= '<li class="add-course"><a href="main/session/session_add.php">';
  938. $my_account_content .= Display::return_icon('session.png',get_lang('AddSession'),null,ICON_SIZE_SMALL);
  939. $my_account_content .= get_lang('AddSession');
  940. $my_account_content .= '</a></li>';
  941. }
  942. }
  943. //Sort courses
  944. $url = api_get_path(WEB_CODE_PATH).'auth/courses.php?action=sortmycourses';
  945. $img_order= Display::return_icon('order-course.png',get_lang('SortMyCourses'),null,ICON_SIZE_SMALL);
  946. $my_account_content .= '<li class="order-course">'.Display::url($img_order.get_lang('SortMyCourses'), $url, array('class' => 'sort course')).'</li>';
  947. // Session history
  948. if (isset($_GET['history']) && intval($_GET['history']) == 1) {
  949. $my_account_content .= '<li class="history-course"><a href="user_portal.php">'.Display::return_icon('history-course.png',get_lang('DisplayTrainingList'),null,ICON_SIZE_SMALL).get_lang('DisplayTrainingList').'</a></li>';
  950. } else {
  951. $my_account_content .= '<li class="history-course"><a href="user_portal.php?history=1" >'.Display::return_icon('history-course.png',get_lang('HistoryTrainingSessions'),null,ICON_SIZE_SMALL).get_lang('HistoryTrainingSessions').'</a></li>';
  952. }
  953. // Course catalog
  954. if ($show_course_link) {
  955. if (!api_is_drh()) {
  956. $my_account_content .= '<li class="list-course"><a href="main/auth/courses.php" >'.Display::return_icon('catalog-course.png',get_lang('CourseCatalog'),null,ICON_SIZE_SMALL).get_lang('CourseCatalog').'</a></li>';
  957. } else {
  958. $my_account_content .= '<li><a href="main/dashboard/index.php">'.get_lang('Dashboard').'</a></li>';
  959. }
  960. }
  961. $my_account_content .= '</ul>';
  962. if (!empty($my_account_content)) {
  963. $html = self::show_right_block(get_lang('Courses'), $my_account_content, 'course_block');
  964. }
  965. return $html;
  966. }
  967. /**
  968. * Prints the session and course list (user_portal.php)
  969. * @param int $user_id
  970. * @return string
  971. */
  972. public function returnCoursesAndSessions($user_id)
  973. {
  974. global $_configuration;
  975. $gamificationModeIsActive = api_get_setting('gamification_mode');
  976. $load_history = (isset($_GET['history']) && intval($_GET['history']) == 1) ? true : false;
  977. if ($load_history) {
  978. // Load sessions in category in *history*
  979. $session_categories = UserManager::get_sessions_by_category($user_id, true);
  980. } else {
  981. // Load sessions in category
  982. $session_categories = UserManager::get_sessions_by_category($user_id, false);
  983. }
  984. $html = '';
  985. // Showing history title
  986. if ($load_history) {
  987. $html .= Display::page_subheader(get_lang('HistoryTrainingSession'));
  988. if (empty($session_categories)) {
  989. $html .= get_lang('YouDoNotHaveAnySessionInItsHistory');
  990. }
  991. }
  992. $courses_html = '';
  993. $special_courses = '';
  994. $sessionCount = 0;
  995. $courseCount = 0;
  996. // If we're not in the history view...
  997. if (!isset($_GET['history'])) {
  998. // Display special courses.
  999. $specialCourses = CourseManager::display_special_courses(
  1000. $user_id,
  1001. $this->load_directories_preview
  1002. );
  1003. $special_courses = $specialCourses['html'];
  1004. // Display courses.
  1005. $courses = CourseManager::display_courses(
  1006. $user_id,
  1007. $this->load_directories_preview
  1008. );
  1009. $courses_html .= $courses['html'];
  1010. $courseCount = $specialCourses['course_count'] + $courses['course_count'];
  1011. }
  1012. $sessions_with_category = '';
  1013. $sessions_with_no_category = '';
  1014. if (is_array($session_categories)) {
  1015. foreach ($session_categories as $session_category) {
  1016. $session_category_id = $session_category['session_category']['id'];
  1017. // Sessions and courses that are not in a session category
  1018. if (
  1019. empty($session_category_id) &&
  1020. isset($session_category['sessions'])
  1021. ) {
  1022. // Independent sessions
  1023. foreach ($session_category['sessions'] as $session) {
  1024. $session_id = $session['session_id'];
  1025. // Don't show empty sessions.
  1026. if (count($session['courses']) < 1) {
  1027. continue;
  1028. }
  1029. // Courses inside the current session.
  1030. $date_session_start = $session['access_start_date'];
  1031. $date_session_end = $session['access_end_date'];
  1032. $coachAccessStartDate = $session['coach_access_start_date'];
  1033. $coachAccessEndDate = $session['coach_access_end_date'];
  1034. $session_now = time();
  1035. $count_courses_session = 0;
  1036. // Loop course content
  1037. $html_courses_session = [];
  1038. $atLeastOneCourseIsVisible = false;
  1039. foreach ($session['courses'] as $course) {
  1040. $is_coach_course = api_is_coach($session_id, $course['real_id']);
  1041. $allowed_time = 0;
  1042. $dif_time_after = 0;
  1043. if (!empty($date_session_start) &&
  1044. $date_session_start != '0000-00-00 00:00:00'
  1045. ) {
  1046. if ($is_coach_course) {
  1047. $allowed_time = api_strtotime($coachAccessStartDate);
  1048. } else {
  1049. $allowed_time = api_strtotime($date_session_start);
  1050. }
  1051. if (!isset($_GET['history'])) {
  1052. if (!empty($date_session_end) &&
  1053. $date_session_end != '0000-00-00 00:00:00'
  1054. ) {
  1055. $endSessionToTms = api_strtotime($date_session_end);
  1056. if ($session_now > $endSessionToTms) {
  1057. $dif_time_after = $session_now - $endSessionToTms;
  1058. $dif_time_after = round($dif_time_after / 86400);
  1059. }
  1060. }
  1061. }
  1062. }
  1063. if (
  1064. $session_now > $allowed_time
  1065. //($coachAccessEndDate > $dif_time_after - 1)
  1066. ) {
  1067. // Read only and accessible.
  1068. $atLeastOneCourseIsVisible = true;
  1069. if (api_get_setting('hide_courses_in_sessions') == 'false') {
  1070. $c = CourseManager::get_logged_user_course_html(
  1071. $course,
  1072. $session_id,
  1073. 'session_course_item',
  1074. true,
  1075. $this->load_directories_preview
  1076. );
  1077. if (isset($c[1])) {
  1078. $html_courses_session[] = $c['1'];
  1079. }
  1080. }
  1081. $count_courses_session++;
  1082. }
  1083. }
  1084. // No courses to show.
  1085. if ($atLeastOneCourseIsVisible == false) {
  1086. if (empty($html_courses_session)) {
  1087. continue;
  1088. }
  1089. }
  1090. if ($count_courses_session > 0) {
  1091. $params = array(
  1092. 'id' => $session_id
  1093. );
  1094. $session_box = Display::get_session_title_box($session_id);
  1095. $extra_info = !empty($session_box['coach']) ? $session_box['coach'] : null;
  1096. $extra_info .= !empty($session_box['coach'])
  1097. ? ' - ' . $session_box['dates']
  1098. : $session_box['dates'];
  1099. $extra_info .= isset($session_box['duration'])
  1100. ? ' ' . $session_box['duration']
  1101. : null;
  1102. $params['extra_fields'] = $session_box['extra_fields'];
  1103. $params['show_link_to_session'] = api_is_drh() ? false : true;
  1104. $params['title'] = $session_box['title'];
  1105. $params['subtitle'] = $extra_info;
  1106. $params['show_actions'] = api_is_platform_admin() ? true : false;
  1107. if (api_get_setting('hide_courses_in_sessions') == 'false') {
  1108. // $params['extra'] .= $html_courses_session;
  1109. }
  1110. $params['description'] = $session_box['description'];
  1111. $params['show_description'] = $session_box['show_description'];
  1112. $params['courses'] = $html_courses_session;
  1113. $params['show_simple_session_info'] = false;
  1114. if (
  1115. isset($_configuration['show_simple_session_info']) &&
  1116. $_configuration['show_simple_session_info']
  1117. ) {
  1118. $params['show_simple_session_info'] = true;
  1119. }
  1120. if ($gamificationModeIsActive) {
  1121. $params['stars'] = SessionManager::getNumberOfStarsFromGamification($params['id']);
  1122. $params['progress'] = GamificationUtils::getSessionProgress($params['id'], $this->user_id);
  1123. $params['points'] = GamificationUtils::getSessionPoints($params['id'], $this->user_id);
  1124. }
  1125. $this->tpl->assign('session', $params);
  1126. $this->tpl->assign('gamification_mode', $gamificationModeIsActive);
  1127. $sessions_with_no_category .= $this->tpl->fetch(
  1128. "{$this->tpl->templateFolder}/user_portal/session.tpl"
  1129. );
  1130. $sessionCount++;
  1131. }
  1132. }
  1133. } else {
  1134. // All sessions included in
  1135. $count_courses_session = 0;
  1136. $html_sessions = '';
  1137. if (isset($session_category['sessions'])) {
  1138. foreach ($session_category['sessions'] as $session) {
  1139. $session_id = $session['session_id'];
  1140. // Don't show empty sessions.
  1141. if (count($session['courses']) < 1) {
  1142. continue;
  1143. }
  1144. $date_session_start = $session['access_start_date'];
  1145. $date_session_end = $session['access_end_date'];
  1146. $coachAccessStartDate = $session['coach_access_start_date'];
  1147. $coachAccessEndDate = $session['coach_access_end_date'];
  1148. $session_now = time();
  1149. $html_courses_session = [];
  1150. $count = 0;
  1151. foreach ($session['courses'] as $course) {
  1152. $is_coach_course = api_is_coach(
  1153. $session_id,
  1154. $course['real_id']
  1155. );
  1156. $dif_time_after = 0;
  1157. $allowed_time = 0;
  1158. if ($is_coach_course) {
  1159. // 24 hours = 86400
  1160. if ($date_session_start != '0000-00-00 00:00:00') {
  1161. $allowed_time = api_strtotime($coachAccessStartDate);
  1162. }
  1163. if (!isset($_GET['history'])) {
  1164. if ($date_session_end != '0000-00-00 00:00:00') {
  1165. $endSessionToTms = api_strtotime(
  1166. $date_session_end
  1167. );
  1168. if ($session_now > $endSessionToTms) {
  1169. $dif_time_after = $session_now - $endSessionToTms;
  1170. $dif_time_after = round(
  1171. $dif_time_after / 86400
  1172. );
  1173. }
  1174. }
  1175. }
  1176. } else {
  1177. $allowed_time = api_strtotime(
  1178. $date_session_start
  1179. );
  1180. }
  1181. if (
  1182. $session_now > $allowed_time //&&
  1183. //$coachAccessEndDate > $dif_time_after - 1
  1184. ) {
  1185. if (api_get_setting('hide_courses_in_sessions') == 'false') {
  1186. $c = CourseManager:: get_logged_user_course_html(
  1187. $course,
  1188. $session_id,
  1189. 'session_course_item'
  1190. );
  1191. $html_courses_session[] = $c[1];
  1192. }
  1193. $count_courses_session++;
  1194. $count++;
  1195. }
  1196. }
  1197. $sessionParams = array();
  1198. //Category
  1199. if ($count > 0) {
  1200. $session_box = Display:: get_session_title_box($session_id);
  1201. $sessionParams['id'] = $session_id;
  1202. $sessionParams['show_link_to_session'] = !api_is_drh();
  1203. $sessionParams['title'] = $session_box['title'];
  1204. $sessionParams['subtitle'] = (!empty($session_box['coach'])
  1205. ? $session_box['coach'] . ' | '
  1206. : '') . $session_box['dates'];
  1207. $sessionParams['show_actions'] = api_is_platform_admin();
  1208. $sessionParams['courses'] = $html_courses_session;
  1209. $sessionParams['show_simple_session_info'] = false;
  1210. if (
  1211. isset($_configuration['show_simple_session_info']) &&
  1212. $_configuration['show_simple_session_info']
  1213. ) {
  1214. $sessionParams['show_simple_session_info'] = true;
  1215. }
  1216. $this->tpl->assign('session', $sessionParams);
  1217. $html_sessions .= $this->tpl->fetch(
  1218. "{$this->tpl->templateFolder}/user_portal/session.tpl"
  1219. );
  1220. $sessionCount++;
  1221. }
  1222. }
  1223. }
  1224. if ($count_courses_session > 0) {
  1225. $categoryParams = array(
  1226. 'id' => $session_category['session_category']['id'],
  1227. 'title' => $session_category['session_category']['name'],
  1228. 'show_actions' => api_is_platform_admin(),
  1229. 'subtitle' => null,
  1230. 'sessions' => $html_sessions
  1231. );
  1232. $session_category_start_date = $session_category['session_category']['date_start'];
  1233. $session_category_end_date = $session_category['session_category']['date_end'];
  1234. if (
  1235. !empty($session_category_start_date) &&
  1236. $session_category_start_date != '0000-00-00' &&
  1237. !empty($session_category_end_date) &&
  1238. $session_category_end_date != '0000-00-00'
  1239. ) {
  1240. $categoryParams['subtitle'] = sprintf(
  1241. get_lang('FromDateXToDateY'),
  1242. $session_category['session_category']['date_start'],
  1243. $session_category['session_category']['date_end']
  1244. );
  1245. } else {
  1246. if (
  1247. !empty($session_category_start_date) &&
  1248. $session_category_start_date != '0000-00-00'
  1249. ) {
  1250. $categoryParams['subtitle'] = get_lang('From') . ' ' . $session_category_start_date;
  1251. }
  1252. if (
  1253. !empty($session_category_end_date) &&
  1254. $session_category_end_date != '0000-00-00'
  1255. ) {
  1256. $categoryParams['subtitle'] = get_lang('Until') . ' ' . $session_category_end_date;
  1257. }
  1258. }
  1259. $this->tpl->assign('session_category', $categoryParams);
  1260. $sessions_with_category .= $this->tpl->fetch(
  1261. "{$this->tpl->templateFolder}/user_portal/session_category.tpl"
  1262. );
  1263. }
  1264. }
  1265. }
  1266. }
  1267. return [
  1268. 'html' => $sessions_with_category . $sessions_with_no_category . $courses_html . $special_courses,
  1269. 'session_count' => $sessionCount,
  1270. 'course_count' => $courseCount
  1271. ];
  1272. }
  1273. /**
  1274. * Shows a welcome message when the user doesn't have any content in the course list
  1275. */
  1276. public function return_welcome_to_course_block()
  1277. {
  1278. $count_courses = CourseManager::count_courses();
  1279. $tpl = $this->tpl->get_template('layout/welcome_to_course.tpl');
  1280. $course_catalog_url = api_get_path(WEB_CODE_PATH).'auth/courses.php';
  1281. $course_list_url = api_get_path(WEB_PATH).'user_portal.php';
  1282. $this->tpl->assign('course_catalog_url', $course_catalog_url);
  1283. $this->tpl->assign('course_list_url', $course_list_url);
  1284. $this->tpl->assign('course_catalog_link', Display::url(get_lang('Here'), $course_catalog_url));
  1285. $this->tpl->assign('course_list_link', Display::url(get_lang('Here'), $course_list_url));
  1286. $this->tpl->assign('count_courses', $count_courses);
  1287. return $this->tpl->fetch($tpl);
  1288. }
  1289. /**
  1290. * @return array
  1291. */
  1292. public function return_hot_courses()
  1293. {
  1294. return CourseManager::return_hot_courses();
  1295. }
  1296. }