userportal.lib.php 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class IndexManager
  5. */
  6. class IndexManager
  7. {
  8. const VIEW_BY_DEFAULT = 0;
  9. const VIEW_BY_SESSION = 1;
  10. // An instance of the template engine
  11. //No need to initialize because IndexManager is not static, and the constructor immediately instantiates a Template
  12. public $tpl;
  13. public $name = '';
  14. public $home = '';
  15. public $default_home = 'home/';
  16. /**
  17. * Construct
  18. * @param string $title
  19. */
  20. public function __construct($title)
  21. {
  22. $this->tpl = new Template($title);
  23. $this->home = api_get_home_path();
  24. $this->user_id = api_get_user_id();
  25. $this->load_directories_preview = false;
  26. if (api_get_setting('show_documents_preview') === 'true') {
  27. $this->load_directories_preview = true;
  28. }
  29. }
  30. /**
  31. * @param array $personal_course_list
  32. */
  33. public function return_exercise_block($personal_course_list)
  34. {
  35. $exercise_list = array();
  36. if (!empty($personal_course_list)) {
  37. foreach ($personal_course_list as $course_item) {
  38. $course_code = $course_item['c'];
  39. $session_id = $course_item['id_session'];
  40. $exercises = ExerciseLib::get_exercises_to_be_taken(
  41. $course_code,
  42. $session_id
  43. );
  44. foreach ($exercises as $exercise_item) {
  45. $exercise_item['course_code'] = $course_code;
  46. $exercise_item['session_id'] = $session_id;
  47. $exercise_item['tms'] = api_strtotime($exercise_item['end_time'], 'UTC');
  48. $exercise_list[] = $exercise_item;
  49. }
  50. }
  51. if (!empty($exercise_list)) {
  52. $exercise_list = msort($exercise_list, 'tms');
  53. $my_exercise = $exercise_list[0];
  54. $url = Display::url(
  55. $my_exercise['title'],
  56. api_get_path(WEB_CODE_PATH).'exercise/overview.php?exerciseId='.$my_exercise['id'].'&cidReq='.$my_exercise['course_code'].'&id_session='.$my_exercise['session_id']
  57. );
  58. $this->tpl->assign('exercise_url', $url);
  59. $this->tpl->assign(
  60. 'exercise_end_date',
  61. api_convert_and_format_date($my_exercise['end_time'], DATE_FORMAT_SHORT)
  62. );
  63. }
  64. }
  65. }
  66. /**
  67. * @param bool $show_slide
  68. * @return null|string
  69. */
  70. public function return_announcements($show_slide = true)
  71. {
  72. $hideAnnouncements = api_get_setting('hide_global_announcements_when_not_connected');
  73. $currentUserId = api_get_user_id();
  74. if ($hideAnnouncements == 'true' && empty($currentUserId)) {
  75. return null;
  76. }
  77. $announcement = isset($_GET['announcement']) ? $_GET['announcement'] : null;
  78. $announcement = intval($announcement);
  79. if (!api_is_anonymous() && $this->user_id) {
  80. $visibility = SystemAnnouncementManager::getCurrentUserVisibility();
  81. if ($show_slide) {
  82. $announcements = SystemAnnouncementManager::displayAnnouncementsSlider(
  83. $visibility,
  84. $announcement
  85. );
  86. } else {
  87. $announcements = SystemAnnouncementManager::displayAllAnnouncements(
  88. $visibility,
  89. $announcement
  90. );
  91. }
  92. } else {
  93. if ($show_slide) {
  94. $announcements = SystemAnnouncementManager::displayAnnouncementsSlider(
  95. SystemAnnouncementManager::VISIBLE_GUEST,
  96. $announcement
  97. );
  98. } else {
  99. $announcements = SystemAnnouncementManager::displayAllAnnouncements(
  100. SystemAnnouncementManager::VISIBLE_GUEST,
  101. $announcement
  102. );
  103. }
  104. }
  105. return $announcements;
  106. }
  107. /**
  108. * Alias for the online_logout() function
  109. * @param bool $redirect Whether to ask online_logout to redirect to index.php or not
  110. * @param array $logoutInfo Information stored by local.inc.php before new context ['uid'=> x, 'cid'=>y, 'sid'=>z]
  111. */
  112. public function logout($redirect = true, $logoutInfo = [])
  113. {
  114. online_logout($this->user_id, true);
  115. Event::courseLogout($logoutInfo);
  116. }
  117. /**
  118. * This function checks if there are courses that are open to the world in the platform course categories (=faculties)
  119. *
  120. * @param string $category
  121. * @return boolean
  122. */
  123. public function category_has_open_courses($category)
  124. {
  125. $setting_show_also_closed_courses = api_get_setting('show_closed_courses') == 'true';
  126. $main_course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  127. $category = Database::escape_string($category);
  128. $sql_query = "SELECT * FROM $main_course_table WHERE category_code='$category'";
  129. $sql_result = Database::query($sql_query);
  130. while ($course = Database::fetch_array($sql_result)) {
  131. if (!$setting_show_also_closed_courses) {
  132. if ((api_get_user_id() > 0 && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM) || ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD)) {
  133. return true; //at least one open course
  134. }
  135. } else {
  136. if (isset($course['visibility'])) {
  137. return true; // At least one course (it does not matter weither it's open or not because $setting_show_also_closed_courses = true).
  138. }
  139. }
  140. }
  141. return false;
  142. }
  143. /**
  144. * @return null|string
  145. */
  146. public function return_teacher_link()
  147. {
  148. $html = '';
  149. $show_menu = false;
  150. if (!empty($this->user_id)) {
  151. // tabs that are deactivated are added here
  152. $show_menu = false;
  153. $show_create_link = false;
  154. $show_course_link = false;
  155. if (api_is_allowed_to_create_course()) {
  156. $show_menu = true;
  157. $show_course_link = true;
  158. $show_create_link = true;
  159. } else {
  160. if (api_get_setting('allow_students_to_browse_courses') === 'true') {
  161. $show_menu = true;
  162. $show_course_link = true;
  163. }
  164. if (api_is_allowed_to_create_course()) {
  165. $show_create_link = true;
  166. }
  167. }
  168. if ($show_menu && ($show_create_link || $show_course_link)) {
  169. $show_menu = true;
  170. } else {
  171. $show_menu = false;
  172. }
  173. }
  174. // My Account section
  175. if ($show_menu) {
  176. $html .= '<ul class="nav nav-pills nav-stacked">';
  177. if ($show_create_link) {
  178. $html .= '<li class="add-course"><a href="'.api_get_path(WEB_CODE_PATH).'create_course/add_course.php">'
  179. .Display::return_icon('new-course.png', get_lang('CourseCreate'))
  180. .(api_get_setting('course_validation') == 'true' ? get_lang('CreateCourseRequest') : get_lang('CourseCreate'))
  181. .'</a></li>';
  182. }
  183. if ($show_course_link) {
  184. if (!api_is_drh() && !api_is_session_admin()) {
  185. $html .= '<li class="list-course"><a href="'.api_get_path(WEB_CODE_PATH).'auth/courses.php">'
  186. .Display::return_icon('catalog-course.png', get_lang('CourseCatalog'))
  187. .get_lang('CourseCatalog')
  188. .'</a></li>';
  189. } else {
  190. $html .= '<li><a href="'.api_get_path(WEB_CODE_PATH).'dashboard/index.php">'.get_lang('Dashboard').'</a></li>';
  191. }
  192. }
  193. $html .= '</ul>';
  194. }
  195. if (!empty($html)) {
  196. $html = self::show_right_block(
  197. get_lang('Courses'),
  198. $html,
  199. 'teacher_block',
  200. null,
  201. 'teachers',
  202. 'teachersCollapse'
  203. );
  204. }
  205. return $html;
  206. }
  207. /**
  208. * Includes a created page
  209. * @return string
  210. */
  211. public function return_home_page()
  212. {
  213. $userId = api_get_user_id();
  214. // Including the page for the news
  215. $html = '';
  216. if (!empty($_GET['include']) && preg_match('/^[a-zA-Z0-9_-]*\.html$/', $_GET['include'])) {
  217. $open = @(string) file_get_contents($this->home.$_GET['include']);
  218. $html = api_to_system_encoding($open, api_detect_encoding(strip_tags($open)));
  219. } else {
  220. // Hiding home top when user not connected.
  221. $hideTop = api_get_setting('hide_home_top_when_connected');
  222. if ($hideTop == 'true' && !empty($userId)) {
  223. return $html;
  224. }
  225. if (!empty($_SESSION['user_language_choice'])) {
  226. $user_selected_language = $_SESSION['user_language_choice'];
  227. } elseif (!empty($_SESSION['_user']['language'])) {
  228. $user_selected_language = $_SESSION['_user']['language'];
  229. } else {
  230. $user_selected_language = api_get_setting('platformLanguage');
  231. }
  232. $home_top_temp = '';
  233. // Try language specific home
  234. if (file_exists($this->home.'home_top_'.$user_selected_language.'.html')) {
  235. $home_top_temp = file_get_contents($this->home.'home_top_'.$user_selected_language.'.html');
  236. }
  237. // Try default language home
  238. if (empty($home_top_temp)) {
  239. if (file_exists($this->home.'home_top.html')) {
  240. $home_top_temp = file_get_contents($this->home.'home_top.html');
  241. } else {
  242. if (file_exists($this->default_home.'home_top.html')) {
  243. $home_top_temp = file_get_contents($this->default_home.'home_top.html');
  244. }
  245. }
  246. }
  247. if (trim($home_top_temp) == '' && api_is_platform_admin()) {
  248. $home_top_temp = '<div class="welcome-mascot">'.get_lang('PortalHomepageDefaultIntroduction').'</div>';
  249. } else {
  250. $home_top_temp = '<div class="welcome-home-top-temp">'.$home_top_temp.'</div>';
  251. }
  252. $open = str_replace('{rel_path}', api_get_path(REL_PATH), $home_top_temp);
  253. $html = api_to_system_encoding($open, api_detect_encoding(strip_tags($open)));
  254. }
  255. return $html;
  256. }
  257. /**
  258. * @return string
  259. */
  260. public function return_notice()
  261. {
  262. $user_selected_language = api_get_interface_language();
  263. $html = '';
  264. // Notice
  265. $home_notice = @(string) file_get_contents($this->home.'home_notice_'.$user_selected_language.'.html');
  266. if (empty($home_notice)) {
  267. $home_notice = @(string) file_get_contents($this->home.'home_notice.html');
  268. }
  269. if (!empty($home_notice)) {
  270. $home_notice = api_to_system_encoding($home_notice, api_detect_encoding(strip_tags($home_notice)));
  271. $html = self::show_right_block(
  272. get_lang('Notice'),
  273. $home_notice,
  274. 'notice_block',
  275. null,
  276. 'notices',
  277. 'noticesCollapse'
  278. );
  279. }
  280. return $html;
  281. }
  282. /**
  283. * @return string
  284. */
  285. public function return_help()
  286. {
  287. $user_selected_language = api_get_interface_language();
  288. $platformLanguage = api_get_setting('platformLanguage');
  289. // Help section.
  290. /* Hide right menu "general" and other parts on anonymous right menu. */
  291. if (!isset($user_selected_language)) {
  292. $user_selected_language = $platformLanguage;
  293. }
  294. $html = '';
  295. $home_menu = @(string) file_get_contents($this->home.'home_menu_'.$user_selected_language.'.html');
  296. if (!empty($home_menu)) {
  297. $home_menu_content = '<ul class="nav nav-pills nav-stacked">';
  298. $home_menu_content .= api_to_system_encoding($home_menu, api_detect_encoding(strip_tags($home_menu)));
  299. $home_menu_content .= '</ul>';
  300. $html .= self::show_right_block(
  301. get_lang('MenuGeneral'),
  302. $home_menu_content,
  303. 'help_block',
  304. null,
  305. 'helps',
  306. 'helpsCollapse'
  307. );
  308. }
  309. return $html;
  310. }
  311. /**
  312. * Generate the block for show a panel with links to My Certificates and Certificates Search pages
  313. * @return array The HTML code for the panel
  314. */
  315. public function returnSkillLinks()
  316. {
  317. $items = [];
  318. if (!api_is_anonymous() &&
  319. api_get_configuration_value('hide_my_certificate_link') === false
  320. ) {
  321. $items[] = [
  322. 'icon' => Display::return_icon('graduation.png', get_lang('MyCertificates')),
  323. 'link' => api_get_path(WEB_CODE_PATH).'gradebook/my_certificates.php',
  324. 'title' => get_lang('MyCertificates')
  325. ];
  326. }
  327. if (api_get_setting('allow_public_certificates') == 'true') {
  328. $items[] = [
  329. 'icon' => Display::return_icon('search_graduation.png', get_lang('Search')),
  330. 'link' => api_get_path(WEB_CODE_PATH).'gradebook/search.php',
  331. 'title' => get_lang('Search')
  332. ];
  333. }
  334. if (Skill::isAllow(0, false)) {
  335. $items[] = [
  336. 'icon' => Display::return_icon('skill-badges.png', get_lang('MySkills')),
  337. 'link' => api_get_path(WEB_CODE_PATH).'social/my_skills_report.php',
  338. 'title' => get_lang('MySkills')
  339. ];
  340. $allowSkillsManagement = api_get_setting('allow_hr_skills_management') == 'true';
  341. if (($allowSkillsManagement && api_is_drh()) || api_is_platform_admin()) {
  342. $items[] = [
  343. 'icon' => Display::return_icon('edit-skill.png', get_lang('MySkills')),
  344. 'link' => api_get_path(WEB_CODE_PATH).'admin/skills_wheel.php',
  345. 'title' => get_lang('ManageSkills')
  346. ];
  347. }
  348. }
  349. return $items;
  350. }
  351. /**
  352. * Reacts on a failed login:
  353. * Displays an explanation with a link to the registration form.
  354. *
  355. * @version 1.0.1
  356. */
  357. public function handle_login_failed()
  358. {
  359. return $this->tpl->handleLoginFailed();
  360. }
  361. /**
  362. * Display list of courses in a category.
  363. * (for anonymous users)
  364. *
  365. * @version 1.1
  366. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University - refactoring and code cleaning
  367. * @author Julio Montoya <gugli100@gmail.com>, Beeznest template modifs
  368. */
  369. public function return_courses_in_categories()
  370. {
  371. $result = '';
  372. $stok = Security::get_token();
  373. // Initialization.
  374. $user_identified = (api_get_user_id() > 0 && !api_is_anonymous());
  375. $web_course_path = api_get_path(WEB_COURSE_PATH);
  376. $category = isset($_GET['category']) ? Database::escape_string($_GET['category']) : '';
  377. $setting_show_also_closed_courses = api_get_setting('show_closed_courses') == 'true';
  378. // Database table definitions.
  379. $main_course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  380. $main_category_table = Database::get_main_table(TABLE_MAIN_CATEGORY);
  381. // Get list of courses in category $category.
  382. $sql = "SELECT * FROM $main_course_table cours
  383. WHERE category_code = '".$category."'
  384. ORDER BY title, UPPER(visual_code)";
  385. // Showing only the courses of the current access_url_id.
  386. if (api_is_multiple_url_enabled()) {
  387. $url_access_id = api_get_current_access_url_id();
  388. if ($url_access_id != -1) {
  389. $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  390. $sql = "SELECT * FROM $main_course_table as course
  391. INNER JOIN $tbl_url_rel_course as url_rel_course
  392. ON (url_rel_course.c_id = course.id)
  393. WHERE
  394. access_url_id = $url_access_id AND
  395. category_code = '".$category."'
  396. ORDER BY title, UPPER(visual_code)";
  397. }
  398. }
  399. // Removed: AND cours.visibility='".COURSE_VISIBILITY_OPEN_WORLD."'
  400. $queryResult = Database::query($sql);
  401. while ($course_result = Database::fetch_array($queryResult)) {
  402. $course_list[] = $course_result;
  403. }
  404. $numRows = Database::num_rows($queryResult);
  405. // $setting_show_also_closed_courses
  406. if ($user_identified) {
  407. if ($setting_show_also_closed_courses) {
  408. $platform_visible_courses = '';
  409. } else {
  410. $platform_visible_courses = " AND (t3.visibility='".COURSE_VISIBILITY_OPEN_WORLD."' OR t3.visibility='".COURSE_VISIBILITY_OPEN_PLATFORM."' )";
  411. }
  412. } else {
  413. if ($setting_show_also_closed_courses) {
  414. $platform_visible_courses = '';
  415. } else {
  416. $platform_visible_courses = " AND (t3.visibility='".COURSE_VISIBILITY_OPEN_WORLD."' )";
  417. }
  418. }
  419. $sqlGetSubCatList = "
  420. SELECT t1.name,
  421. t1.code,
  422. t1.parent_id,
  423. t1.children_count,COUNT(DISTINCT t3.code) AS nbCourse
  424. FROM $main_category_table t1
  425. LEFT JOIN $main_category_table t2
  426. ON t1.code=t2.parent_id
  427. LEFT JOIN $main_course_table t3
  428. ON (t3.category_code = t1.code $platform_visible_courses)
  429. WHERE t1.parent_id ".(empty($category) ? "IS NULL" : "='$category'")."
  430. GROUP BY t1.name,t1.code,t1.parent_id,t1.children_count
  431. ORDER BY t1.tree_pos, t1.name";
  432. // Showing only the category of courses of the current access_url_id
  433. if (api_is_multiple_url_enabled()) {
  434. $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
  435. $courseCategoryCondition = " INNER JOIN $table a ON (t1.id = a.course_category_id)";
  436. $url_access_id = api_get_current_access_url_id();
  437. if ($url_access_id != -1) {
  438. $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  439. $sqlGetSubCatList = "
  440. SELECT t1.name,
  441. t1.code,
  442. t1.parent_id,
  443. t1.children_count,
  444. COUNT(DISTINCT t3.code) AS nbCourse
  445. FROM $main_category_table t1
  446. $courseCategoryCondition
  447. LEFT JOIN $main_category_table t2 ON t1.code = t2.parent_id
  448. LEFT JOIN $main_course_table t3 ON (t3.category_code = t1.code $platform_visible_courses)
  449. INNER JOIN $tbl_url_rel_course as url_rel_course
  450. ON (url_rel_course.c_id = t3.id)
  451. WHERE
  452. url_rel_course.access_url_id = $url_access_id AND
  453. t1.parent_id ".(empty($category) ? "IS NULL" : "='$category'")."
  454. GROUP BY t1.name,t1.code,t1.parent_id,t1.children_count
  455. ORDER BY t1.tree_pos, t1.name";
  456. }
  457. }
  458. $resCats = Database::query($sqlGetSubCatList);
  459. $thereIsSubCat = false;
  460. if (Database::num_rows($resCats) > 0) {
  461. $htmlListCat = Display::page_header(get_lang('CatList'));
  462. $htmlListCat .= '<ul>';
  463. $htmlTitre = '';
  464. while ($catLine = Database::fetch_array($resCats)) {
  465. $category_has_open_courses = self::category_has_open_courses($catLine['code']);
  466. if ($category_has_open_courses) {
  467. // The category contains courses accessible to anonymous visitors.
  468. $htmlListCat .= '<li>';
  469. $htmlListCat .= '<a href="'.api_get_self().'?category='.$catLine['code'].'">'.$catLine['name'].'</a>';
  470. if (api_get_setting('show_number_of_courses') == 'true') {
  471. $htmlListCat .= ' ('.$catLine['nbCourse'].' '.get_lang('Courses').')';
  472. }
  473. $htmlListCat .= "</li>";
  474. $thereIsSubCat = true;
  475. } elseif ($catLine['children_count'] > 0) {
  476. // The category has children, subcategories.
  477. $htmlListCat .= '<li>';
  478. $htmlListCat .= '<a href="'.api_get_self().'?category='.$catLine['code'].'">'.$catLine['name'].'</a>';
  479. $htmlListCat .= "</li>";
  480. $thereIsSubCat = true;
  481. } elseif (api_get_setting('show_empty_course_categories') == 'true') {
  482. /* End changed code to eliminate the (0 courses) after empty categories. */
  483. $htmlListCat .= '<li>';
  484. $htmlListCat .= $catLine['name'];
  485. $htmlListCat .= "</li>";
  486. $thereIsSubCat = true;
  487. } // Else don't set thereIsSubCat to true to avoid printing things if not requested.
  488. // TODO: deprecate this useless feature - this includes removing system variable
  489. if (empty($htmlTitre)) {
  490. $htmlTitre = '<p>';
  491. if (api_get_setting('show_back_link_on_top_of_tree') == 'true') {
  492. $htmlTitre .= '<a href="'.api_get_self().'">&lt;&lt; '.get_lang('BackToHomePage').'</a>';
  493. }
  494. $htmlTitre .= "</p>";
  495. }
  496. }
  497. $htmlListCat .= "</ul>";
  498. }
  499. $result .= $htmlTitre;
  500. if ($thereIsSubCat) {
  501. $result .= $htmlListCat;
  502. }
  503. while ($categoryName = Database::fetch_array($resCats)) {
  504. $result .= '<h3>'.$categoryName['name']."</h3>\n";
  505. }
  506. $courses_list_string = '';
  507. $courses_shown = 0;
  508. if ($numRows > 0) {
  509. $courses_list_string .= Display::page_header(get_lang('CourseList'));
  510. $courses_list_string .= "<ul>";
  511. if (api_get_user_id()) {
  512. $courses_of_user = self::get_courses_of_user(api_get_user_id());
  513. }
  514. foreach ($course_list as $course) {
  515. // $setting_show_also_closed_courses
  516. if ($course['visibility'] == COURSE_VISIBILITY_HIDDEN) {
  517. continue;
  518. }
  519. if (!$setting_show_also_closed_courses) {
  520. // If we do not show the closed courses
  521. // we only show the courses that are open to the world (to everybody)
  522. // and the courses that are open to the platform (if the current user is a registered user.
  523. if (($user_identified && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM) || ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD)) {
  524. $courses_shown++;
  525. $courses_list_string .= "<li>";
  526. $courses_list_string .= '<a href="'.$web_course_path.$course['directory'].'/">'.$course['title'].'</a><br />';
  527. $course_details = array();
  528. if (api_get_setting('display_coursecode_in_courselist') === 'true') {
  529. $course_details[] = $course['visual_code'];
  530. }
  531. if (api_get_setting('display_teacher_in_courselist') === 'true') {
  532. $course_details[] = CourseManager::get_teacher_list_from_course_code_to_string($course['code']);
  533. }
  534. if (api_get_setting('show_different_course_language') === 'true' && $course['course_language'] != api_get_setting('platformLanguage')) {
  535. $course_details[] = $course['course_language'];
  536. }
  537. $courses_list_string .= implode(' - ', $course_details);
  538. $courses_list_string .= "</li>";
  539. }
  540. } else {
  541. // We DO show the closed courses.
  542. // The course is accessible if (link to the course homepage):
  543. // 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);
  544. // 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);
  545. // 3. the user is logged in and the user is subscribed to the course and the course visibility is not COURSE_VISIBILITY_CLOSED;
  546. // 4. the user is logged in and the user is course admin of te course (regardless of the course visibility setting);
  547. // 5. the user is the platform admin api_is_platform_admin().
  548. $courses_shown++;
  549. $courses_list_string .= "<li>";
  550. if ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD
  551. || ($user_identified && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM)
  552. || ($user_identified && array_key_exists($course['code'], $courses_of_user)
  553. && $course['visibility'] != COURSE_VISIBILITY_CLOSED)
  554. || $courses_of_user[$course['code']]['status'] == '1'
  555. || api_is_platform_admin()
  556. ) {
  557. $courses_list_string .= '<a href="'.$web_course_path.$course['directory'].'/">';
  558. }
  559. $courses_list_string .= $course['title'];
  560. if ($course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD
  561. || ($user_identified && $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM)
  562. || ($user_identified && array_key_exists($course['code'], $courses_of_user)
  563. && $course['visibility'] != COURSE_VISIBILITY_CLOSED)
  564. || $courses_of_user[$course['code']]['status'] == '1'
  565. || api_is_platform_admin()
  566. ) {
  567. $courses_list_string .= '</a><br />';
  568. }
  569. $course_details = array();
  570. if (api_get_setting('display_coursecode_in_courselist') == 'true') {
  571. $course_details[] = $course['visual_code'];
  572. }
  573. if (api_get_setting('display_teacher_in_courselist') === 'true') {
  574. if (!empty($course['tutor_name'])) {
  575. $course_details[] = $course['tutor_name'];
  576. }
  577. }
  578. if (api_get_setting('show_different_course_language') == 'true' && $course['course_language'] != api_get_setting('platformLanguage')) {
  579. $course_details[] = $course['course_language'];
  580. }
  581. $courses_list_string .= implode(' - ', $course_details);
  582. // We display a subscription link if:
  583. // 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
  584. // 2.
  585. if ($user_identified && !array_key_exists($course['code'], $courses_of_user)) {
  586. if ($course['subscribe'] == '1') {
  587. $courses_list_string .= '&nbsp;<a class="btn btn-primary" href="main/auth/courses.php?action=subscribe_course&sec_token='.$stok.'&subscribe_course='.$course['code'].'&category_code='.Security::remove_XSS($_GET['category']).'">'.get_lang('Subscribe').'</a><br />';
  588. } else {
  589. $courses_list_string .= '<br />'.get_lang('SubscribingNotAllowed');
  590. }
  591. }
  592. $courses_list_string .= "</li>";
  593. } //end else
  594. } // end foreach
  595. $courses_list_string .= "</ul>";
  596. }
  597. if ($courses_shown > 0) {
  598. // Only display the list of courses and categories if there was more than
  599. // 0 courses visible to the world (we're in the anonymous list here).
  600. $result .= $courses_list_string;
  601. }
  602. if ($category != '') {
  603. $result .= '<p><a href="'.api_get_self().'">'
  604. .Display:: return_icon('back.png', get_lang('BackToHomePage'))
  605. .get_lang('BackToHomePage').'</a></p>';
  606. }
  607. return $result;
  608. }
  609. /**
  610. * retrieves all the courses that the user has already subscribed to
  611. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  612. * @param int $user_id : the id of the user
  613. * @return array an array containing all the information of the courses of the given user
  614. */
  615. public function get_courses_of_user($user_id)
  616. {
  617. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  618. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  619. // Secondly we select the courses that are in a category (user_course_cat <> 0)
  620. // and sort these according to the sort of the category
  621. $user_id = intval($user_id);
  622. $sql = "SELECT
  623. course.code k,
  624. course.visual_code vc,
  625. course.subscribe subscr,
  626. course.unsubscribe unsubscr,
  627. course.title i,
  628. course.tutor_name t,
  629. course.directory dir,
  630. course_rel_user.status status,
  631. course_rel_user.sort sort,
  632. course_rel_user.user_course_cat user_course_cat
  633. FROM
  634. $table_course course,
  635. $table_course_user course_rel_user
  636. WHERE
  637. course.id = course_rel_user.c_id AND
  638. course_rel_user.user_id = '".$user_id."' AND
  639. course_rel_user.relation_type <> ".COURSE_RELATION_TYPE_RRHH."
  640. ORDER BY course_rel_user.sort ASC";
  641. $result = Database::query($sql);
  642. $courses = array();
  643. while ($row = Database::fetch_array($result)) {
  644. // We only need the database name of the course.
  645. $courses[$row['k']] = array(
  646. 'code' => $row['k'],
  647. 'visual_code' => $row['vc'],
  648. 'title' => $row['i'],
  649. 'directory' => $row['dir'],
  650. 'status' => $row['status'],
  651. 'tutor' => $row['t'],
  652. 'subscribe' => $row['subscr'],
  653. 'unsubscribe' => $row['unsubscr'],
  654. 'sort' => $row['sort'],
  655. 'user_course_category' => $row['user_course_cat']
  656. );
  657. }
  658. return $courses;
  659. }
  660. /**
  661. * @todo use the template system
  662. * @param $title
  663. * @param $content
  664. * @param string $id
  665. * @param array $params
  666. * @param string $idAccordion
  667. * @param string $idCollapse
  668. * @return string
  669. */
  670. public function show_right_block(
  671. $title,
  672. $content,
  673. $id = '',
  674. $params = [],
  675. $idAccordion = '',
  676. $idCollapse = ''
  677. ) {
  678. $html = '';
  679. if (!empty($idAccordion)) {
  680. $html .= '<div class="panel-group" id="'.$idAccordion.'" role="tablist" aria-multiselectable="true">';
  681. $html .= '<div class="panel panel-default" id="'.$id.'">';
  682. $html .= '<div class="panel-heading" role="tab"><h4 class="panel-title">';
  683. $html .= '<a role="button" data-toggle="collapse" data-parent="#'.$idAccordion.'" href="#'.$idCollapse.'" aria-expanded="true" aria-controls="'.$idCollapse.'">'.$title.'</a>';
  684. $html .= '</h4></div>';
  685. $html .= '<div id="'.$idCollapse.'" class="panel-collapse collapse in" role="tabpanel">';
  686. $html .= '<div class="panel-body">'.$content.'</div>';
  687. $html .= '</div></div></div>';
  688. } else {
  689. if (!empty($id)) {
  690. $params['id'] = $id;
  691. }
  692. $params['class'] = 'panel panel-default';
  693. $html = null;
  694. if (!empty($title)) {
  695. $html .= '<div class="panel-heading">'.$title.'</div>';
  696. }
  697. $html .= '<div class="panel-body">'.$content.'</div>';
  698. $html = Display::div($html, $params);
  699. }
  700. return $html;
  701. }
  702. /**
  703. * @todo use FormValidator
  704. * @return string
  705. */
  706. public function return_search_block()
  707. {
  708. $html = '';
  709. if (api_get_setting('search_enabled') == 'true') {
  710. $search_btn = get_lang('Search');
  711. $search_content = '<form action="main/search/" method="post">
  712. <div class="form-group">
  713. <input type="text" id="query" class="form-control" name="query" value="" />
  714. <button class="btn btn-default" type="submit" name="submit" value="'.$search_btn.'" />'.$search_btn.' </button>
  715. </div></form>';
  716. $html .= self::show_right_block(get_lang('Search'), $search_content, 'search_block');
  717. }
  718. return $html;
  719. }
  720. /**
  721. * @return string
  722. */
  723. public function return_classes_block()
  724. {
  725. if (api_get_setting('show_groups_to_users') !== 'true') {
  726. return '';
  727. }
  728. $usergroup = new UserGroup();
  729. $usergroup_list = $usergroup->get_usergroup_by_user(api_get_user_id());
  730. $items = [];
  731. if (!empty($usergroup_list)) {
  732. foreach ($usergroup_list as $group_id) {
  733. $data = $usergroup->get($group_id);
  734. $items[] = [
  735. 'link' => api_get_path(WEB_CODE_PATH).'user/classes.php?id='.$data['id'],
  736. 'title' => $data['name']
  737. ];
  738. }
  739. }
  740. if (api_is_platform_admin()) {
  741. $items[] = [
  742. 'link' => api_get_path(WEB_CODE_PATH).'admin/usergroups.php?action=add',
  743. 'title' => get_lang('AddClasses')
  744. ];
  745. }
  746. $html = self::show_right_block(
  747. get_lang('Classes'),
  748. self::returnRightBlockItems($items),
  749. 'classes_block'
  750. );
  751. return $html;
  752. }
  753. /**
  754. * @return string
  755. */
  756. public function return_user_image_block()
  757. {
  758. $html = '';
  759. if (!api_is_anonymous()) {
  760. $userPicture = UserManager::getUserPicture(api_get_user_id(), USER_IMAGE_SIZE_ORIGINAL);
  761. $content = null;
  762. if (api_get_setting('allow_social_tool') == 'true') {
  763. $content .= '<a style="text-align:center" href="'.api_get_path(WEB_PATH).'main/social/home.php">
  764. <img class="img-circle" src="'.$userPicture.'"></a>';
  765. } else {
  766. $content .= '<a style="text-align:center" href="'.api_get_path(WEB_PATH).'main/auth/profile.php">
  767. <img class="img-circle" title="'.get_lang('EditProfile').'" src="'.$userPicture.'"></a>';
  768. }
  769. $html = self::show_right_block(
  770. null,
  771. $content,
  772. 'user_image_block',
  773. array('style' => 'text-align:center;')
  774. );
  775. }
  776. return $html;
  777. }
  778. /**
  779. * @return array
  780. */
  781. public function return_profile_block()
  782. {
  783. $userInfo = api_get_user_info();
  784. $userId = api_get_user_id();
  785. if (empty($userId)) {
  786. return;
  787. }
  788. $items = [];
  789. $userGroup = new UserGroup();
  790. // @todo Add a platform setting to add the user image.
  791. if (api_get_setting('allow_message_tool') == 'true') {
  792. // New messages.
  793. $number_of_new_messages = MessageManager::getCountNewMessages();
  794. // New contact invitations.
  795. $number_of_new_messages_of_friend = SocialManager::get_message_number_invitation_by_user_id(
  796. $userId
  797. );
  798. // New group invitations sent by a moderator.
  799. $group_pending_invitations = $userGroup->get_groups_by_user(
  800. $userId,
  801. GROUP_USER_PERMISSION_PENDING_INVITATION,
  802. false
  803. );
  804. $group_pending_invitations = count($group_pending_invitations);
  805. $total_invitations = $number_of_new_messages_of_friend + $group_pending_invitations;
  806. $cant_msg = Display::badge($number_of_new_messages);
  807. $link = '';
  808. if (api_get_setting('allow_social_tool') == 'true') {
  809. $link = '?f=social';
  810. }
  811. $items[] = [
  812. 'class' => 'inbox-message-social',
  813. 'icon' => Display::return_icon('inbox.png', get_lang('Inbox')),
  814. 'link' => api_get_path(WEB_PATH).'main/messages/inbox.php'.$link,
  815. 'title' => get_lang('Inbox').$cant_msg
  816. ];
  817. $items[] = [
  818. 'class' => 'new-message-social',
  819. 'icon' => Display::return_icon('new-message.png', get_lang('Compose')),
  820. 'link' => api_get_path(WEB_PATH).'main/messages/new_message.php'.$link,
  821. 'title' => get_lang('Compose')
  822. ];
  823. if (api_get_setting('allow_social_tool') == 'true') {
  824. $total_invitations = Display::badge($total_invitations);
  825. $items[] = [
  826. 'class' => 'invitations-social',
  827. 'icon' => Display::return_icon('invitations.png', get_lang('PendingInvitations')),
  828. 'link' => api_get_path(WEB_PATH).'main/social/invitations.php',
  829. 'title' => get_lang('PendingInvitations').$total_invitations
  830. ];
  831. }
  832. if (api_get_configuration_value('allow_my_files_link_in_homepage')) {
  833. if (api_get_setting('allow_my_files') !== 'false') {
  834. $items[] = [
  835. 'class' => 'myfiles-social',
  836. 'icon' => Display::return_icon('sn-files.png', get_lang('Files')),
  837. 'link' => api_get_path(WEB_PATH).'main/social/myfiles.php',
  838. 'title' => get_lang('MyFiles')
  839. ];
  840. }
  841. }
  842. }
  843. $items[] = [
  844. 'class' => 'profile-social',
  845. 'icon' => Display::return_icon('edit-profile.png', get_lang('EditProfile')),
  846. 'link' => Display::getProfileEditionLink($userId),
  847. 'title' => get_lang('EditProfile')
  848. ];
  849. if (api_get_configuration_value('show_link_request_hrm_user') &&
  850. api_is_drh()
  851. ) {
  852. $label = get_lang('RequestLinkingToUser');
  853. $items[] = [
  854. 'icon' => Display::return_icon('new_group.png', $label),
  855. 'link' => api_get_path(WEB_CODE_PATH).'social/require_user_linking.php',
  856. 'title' => $label
  857. ];
  858. }
  859. if (bbb::showGlobalConferenceLink($userInfo)) {
  860. $bbb = new bbb('', '', true, api_get_user_id());
  861. $url = $bbb->getListingUrl();
  862. $items[] = [
  863. 'class' => 'video-conference',
  864. 'icon' => Display::return_icon(
  865. 'bbb.png',
  866. get_lang('VideoConference')
  867. ),
  868. 'link' => $url,
  869. 'title' => get_lang('VideoConference')
  870. ];
  871. }
  872. return $items;
  873. }
  874. /**
  875. * @return array
  876. */
  877. public function return_navigation_links()
  878. {
  879. $items = [];
  880. // Deleting the myprofile link.
  881. if (api_get_setting('allow_social_tool') == 'true') {
  882. unset($this->tpl->menu_navigation['myprofile']);
  883. }
  884. $hideMenu = api_get_configuration_value('hide_main_navigation_menu');
  885. if ($hideMenu === true) {
  886. return '';
  887. }
  888. // Main navigation section.
  889. // Tabs that are deactivated are added here.
  890. if (!empty($this->tpl->menu_navigation)) {
  891. //$content = '<ul class="nav nav-pills nav-stacked">';
  892. foreach ($this->tpl->menu_navigation as $section => $navigation_info) {
  893. $items [] = [
  894. 'icon' => null,
  895. 'link' => $navigation_info['url'],
  896. 'title' => $navigation_info['title']
  897. ];
  898. }
  899. }
  900. return $items;
  901. }
  902. /**
  903. * @return null|string
  904. */
  905. public function return_course_block()
  906. {
  907. $isHrm = api_is_drh();
  908. $show_create_link = false;
  909. $show_course_link = false;
  910. if (api_is_allowed_to_create_course()) {
  911. $show_create_link = true;
  912. }
  913. if (api_get_setting('allow_students_to_browse_courses') === 'true') {
  914. $show_course_link = true;
  915. }
  916. $items = [];
  917. // My account section
  918. if ($show_create_link) {
  919. if (api_get_setting('course_validation') == 'true' && !api_is_platform_admin()) {
  920. $items[] = [
  921. 'class' => 'add-course',
  922. 'icon' => Display::return_icon('new-course.png', get_lang('CreateCourseRequest')),
  923. 'link' => 'main/create_course/add_course.php',
  924. 'title' => get_lang('CreateCourseRequest')
  925. ];
  926. } else {
  927. $items[] = [
  928. 'class' => 'add-course',
  929. 'icon' => Display::return_icon('new-course.png', get_lang('CourseCreate')),
  930. 'link' => 'main/create_course/add_course.php',
  931. 'title' => get_lang('CourseCreate')
  932. ];
  933. }
  934. if (SessionManager::allowToManageSessions()) {
  935. $items[] = [
  936. 'class' => 'add-course',
  937. 'icon' => Display::return_icon('session.png', get_lang('AddSession')),
  938. 'link' => 'main/session/session_add.php',
  939. 'title' => get_lang('AddSession')
  940. ];
  941. }
  942. }
  943. // Sort courses
  944. $items[] = [
  945. 'class' => 'order-course',
  946. 'icon' => Display::return_icon('order-course.png', get_lang('SortMyCourses')),
  947. 'link' => api_get_path(WEB_CODE_PATH).'auth/courses.php?action=sortmycourses',
  948. 'title' => get_lang('SortMyCourses')
  949. ];
  950. // Session history
  951. if (isset($_GET['history']) && intval($_GET['history']) == 1) {
  952. $items[] = [
  953. 'class' => 'history-course',
  954. 'icon' => Display::return_icon('history-course.png', get_lang('DisplayTrainingList')),
  955. 'link' => 'user_portal.php',
  956. 'title' => get_lang('DisplayTrainingList')
  957. ];
  958. } else {
  959. $items[] = [
  960. 'class' => 'history-course',
  961. 'icon' => Display::return_icon('history-course.png', get_lang('HistoryTrainingSessions')),
  962. 'link' => 'user_portal.php?history=1',
  963. 'title' => get_lang('HistoryTrainingSessions')
  964. ];
  965. }
  966. if ($isHrm) {
  967. $items[] = [
  968. 'link' => api_get_path(WEB_CODE_PATH).'auth/hrm_courses.php',
  969. 'title' => get_lang('HrmAssignedUsersCourseList')
  970. ];
  971. }
  972. // Course catalog
  973. if ($show_course_link) {
  974. if (!api_is_drh()) {
  975. $items[] = [
  976. 'class' => 'list-course',
  977. 'icon' => Display::return_icon('catalog-course.png', get_lang('CourseCatalog')),
  978. 'link' => 'main/auth/courses.php',
  979. 'title' => get_lang('CourseCatalog')
  980. ];
  981. } else {
  982. $items[] = [
  983. 'link' => 'main/dashboard/index.php',
  984. 'title' => get_lang('Dashboard')
  985. ];
  986. }
  987. }
  988. return $items;
  989. }
  990. /**
  991. * Generate the HTML code for items when displaying the right-side blocks
  992. * @param array $items
  993. * @return string
  994. */
  995. private static function returnRightBlockItems(array $items)
  996. {
  997. $my_account_content = '';
  998. foreach ($items as $item) {
  999. if (empty($item['link']) && empty($item['title'])) {
  1000. continue;
  1001. }
  1002. $my_account_content .= '<li class="list-group-item '.(empty($item['class']) ? '' : $item['class']).'">'
  1003. .(empty($item['icon']) ? '' : '<span class="item-icon">'.$item['icon'].'</span>')
  1004. .'<a href="'.$item['link'].'">'.$item['title'].'</a>'
  1005. .'</li>';
  1006. }
  1007. return '<ul class="list-group">'.$my_account_content.'</ul>';
  1008. }
  1009. /**
  1010. * Prints the session and course list (user_portal.php)
  1011. * @param int $user_id
  1012. * @param bool $showSessions
  1013. * @param string $categoryCodeFilter
  1014. * @param bool $useUserLanguageFilterIfAvailable
  1015. * @return string
  1016. */
  1017. public function returnCoursesAndSessions(
  1018. $user_id,
  1019. $showSessions = true,
  1020. $categoryCodeFilter = '',
  1021. $useUserLanguageFilterIfAvailable = true
  1022. ) {
  1023. $gameModeIsActive = api_get_setting('gamification_mode');
  1024. $listCourse = '';
  1025. $specialCourseList = '';
  1026. $load_history = isset($_GET['history']) && intval($_GET['history']) == 1 ? true : false;
  1027. $viewGridCourses = api_get_configuration_value('view_grid_courses') === 'true';
  1028. $showSimpleSessionInfo = api_get_configuration_value('show_simple_session_info');
  1029. $coursesWithoutCategoryTemplate = '/user_portal/classic_courses_without_category.tpl';
  1030. $coursesWithCategoryTemplate = '/user_portal/classic_courses_with_category.tpl';
  1031. if ($load_history) {
  1032. // Load sessions in category in *history*
  1033. $session_categories = UserManager::get_sessions_by_category($user_id, true);
  1034. } else {
  1035. // Load sessions in category
  1036. $session_categories = UserManager::get_sessions_by_category($user_id, false);
  1037. }
  1038. $sessionCount = 0;
  1039. $courseCount = 0;
  1040. // Student info code check (shows student progress information on
  1041. // courses list
  1042. $studentInfo = api_get_configuration_value('course_student_info');
  1043. $viewGrid = api_get_configuration_value('view_grid_courses');
  1044. $studentInfoProgress = !empty($studentInfo['progress']) && $studentInfo['progress'] === true;
  1045. $studentInfoScore = !empty($studentInfo['score']) && $studentInfo['score'] === true;
  1046. $studentInfoCertificate = !empty($studentInfo['certificate']) && $studentInfo['certificate'] === true;
  1047. $courseCompleteList = [];
  1048. $coursesInCategoryCount = 0;
  1049. $coursesNotInCategoryCount = 0;
  1050. // If we're not in the history view...
  1051. if (!isset($_GET['history'])) {
  1052. // Display special courses.
  1053. $specialCourses = CourseManager::returnSpecialCourses(
  1054. $user_id,
  1055. $this->load_directories_preview,
  1056. $useUserLanguageFilterIfAvailable
  1057. );
  1058. // Display courses.
  1059. $courses = CourseManager::returnCourses(
  1060. $user_id,
  1061. $this->load_directories_preview,
  1062. $useUserLanguageFilterIfAvailable
  1063. );
  1064. // Course option (show student progress)
  1065. // This code will add new variables (Progress, Score, Certificate)
  1066. if ($studentInfoProgress || $studentInfoScore || $studentInfoCertificate) {
  1067. if (!empty($specialCourses)) {
  1068. foreach ($specialCourses as $key => $specialCourseInfo) {
  1069. if ($studentInfoProgress) {
  1070. $progress = Tracking::get_avg_student_progress(
  1071. $user_id,
  1072. $specialCourseInfo['course_code']
  1073. );
  1074. $specialCourses[$key]['student_info']['progress'] = $progress === false ? null : $progress;
  1075. }
  1076. if ($studentInfoScore) {
  1077. $percentage_score = Tracking::get_avg_student_score(
  1078. $user_id,
  1079. $specialCourseInfo['course_code'],
  1080. array()
  1081. );
  1082. $specialCourses[$key]['student_info']['score'] = $percentage_score;
  1083. }
  1084. if ($studentInfoCertificate) {
  1085. $category = Category::load(
  1086. null,
  1087. null,
  1088. $specialCourseInfo['course_code'],
  1089. null,
  1090. null,
  1091. null
  1092. );
  1093. $specialCourses[$key]['student_info']['certificate'] = null;
  1094. if (isset($category[0])) {
  1095. if ($category[0]->is_certificate_available($user_id)) {
  1096. $specialCourses[$key]['student_info']['certificate'] = Display::label(
  1097. get_lang('Yes'),
  1098. 'success'
  1099. );
  1100. } else {
  1101. $specialCourses[$key]['student_info']['certificate'] = Display::label(
  1102. get_lang('No'),
  1103. 'danger'
  1104. );
  1105. }
  1106. }
  1107. }
  1108. }
  1109. }
  1110. if (isset($courses['in_category']) && isset($courses['not_category'])) {
  1111. foreach ($courses['in_category'] as $key1 => $value) {
  1112. if (isset($courses['in_category'][$key1]['courses'])) {
  1113. foreach ($courses['in_category'][$key1]['courses'] as $key2 => $courseInCatInfo) {
  1114. if ($studentInfoProgress) {
  1115. $progress = Tracking::get_avg_student_progress(
  1116. $user_id,
  1117. $courseInCatInfo['course_code']
  1118. );
  1119. $courses['in_category'][$key1]['courses'][$key2]['student_info']['progress'] = $progress === false ? null : $progress;
  1120. }
  1121. if ($studentInfoScore) {
  1122. $percentage_score = Tracking::get_avg_student_score(
  1123. $user_id,
  1124. $specialCourseInfo['course_code'],
  1125. array()
  1126. );
  1127. $courses['in_category'][$key1]['courses'][$key2]['student_info']['score'] = $percentage_score;
  1128. }
  1129. if ($studentInfoCertificate) {
  1130. $category = Category::load(
  1131. null,
  1132. null,
  1133. $courseInCatInfo['course_code'],
  1134. null,
  1135. null,
  1136. null
  1137. );
  1138. $courses['in_category'][$key1]['student_info']['certificate'] = null;
  1139. $isCertificateAvailable = $category[0]->is_certificate_available($user_id);
  1140. if (isset($category[0])) {
  1141. if ($viewGrid == 'true') {
  1142. if ($isCertificateAvailable) {
  1143. $courses['in_category'][$key1]['student_info']['certificate'] = get_lang('Yes');
  1144. } else {
  1145. $courses['in_category'][$key1]['student_info']['certificate'] = get_lang('No');
  1146. }
  1147. } else {
  1148. if ($isCertificateAvailable) {
  1149. $courses['in_category'][$key1]['student_info']['certificate'] = Display::label(
  1150. get_lang('Yes'),
  1151. 'success'
  1152. );
  1153. } else {
  1154. $courses['in_category'][$key1]['student_info']['certificate'] = Display::label(
  1155. get_lang('No'),
  1156. 'danger'
  1157. );
  1158. }
  1159. }
  1160. }
  1161. }
  1162. }
  1163. }
  1164. }
  1165. }
  1166. if (isset($courses['not_category'])) {
  1167. foreach ($courses['not_category'] as $key => $courseNotInCatInfo) {
  1168. if ($studentInfoProgress) {
  1169. $progress = Tracking::get_avg_student_progress(
  1170. $user_id,
  1171. $courseNotInCatInfo['course_code']
  1172. );
  1173. $courses['not_category'][$key]['student_info']['progress'] = $progress === false ? null : $progress;
  1174. }
  1175. if ($studentInfoScore) {
  1176. $percentage_score = Tracking::get_avg_student_score(
  1177. $user_id,
  1178. $courseNotInCatInfo['course_code'],
  1179. array()
  1180. );
  1181. $courses['not_category'][$key]['student_info']['score'] = $percentage_score;
  1182. }
  1183. if ($studentInfoCertificate) {
  1184. $category = Category::load(
  1185. null,
  1186. null,
  1187. $courseNotInCatInfo['course_code'],
  1188. null,
  1189. null,
  1190. null
  1191. );
  1192. $courses['not_category'][$key]['student_info']['certificate'] = null;
  1193. if (isset($category[0])) {
  1194. $certificateAvailable = $category[0]->is_certificate_available($user_id);
  1195. if ($viewGrid == 'true') {
  1196. if ($certificateAvailable) {
  1197. $courses['not_category'][$key]['student_info']['certificate'] = get_lang('Yes');
  1198. } else {
  1199. $courses['not_category'][$key]['student_info']['certificate'] = get_lang('No');
  1200. }
  1201. } else {
  1202. if ($certificateAvailable) {
  1203. $courses['not_category'][$key]['student_info']['certificate'] = Display::label(
  1204. get_lang('Yes'),
  1205. 'success'
  1206. );
  1207. } else {
  1208. $courses['not_category'][$key]['student_info']['certificate'] = Display::label(
  1209. get_lang('No'),
  1210. 'danger'
  1211. );
  1212. }
  1213. }
  1214. }
  1215. }
  1216. }
  1217. }
  1218. }
  1219. if ($viewGridCourses) {
  1220. $coursesWithoutCategoryTemplate = '/user_portal/grid_courses_without_category.tpl';
  1221. $coursesWithCategoryTemplate = '/user_portal/grid_courses_with_category.tpl';
  1222. }
  1223. if ($specialCourses) {
  1224. if ($categoryCodeFilter) {
  1225. $specialCourses = self::filterByCategory(
  1226. $specialCourses,
  1227. $categoryCodeFilter
  1228. );
  1229. }
  1230. $this->tpl->assign('courses', $specialCourses);
  1231. $specialCourseList = $this->tpl->fetch(
  1232. $this->tpl->get_template($coursesWithoutCategoryTemplate)
  1233. );
  1234. $courseCompleteList = array_merge($courseCompleteList, $specialCourses);
  1235. }
  1236. if ($courses['in_category'] || $courses['not_category']) {
  1237. foreach ($courses['in_category'] as $courseData) {
  1238. if (!empty($courseData['courses'])) {
  1239. $coursesInCategoryCount += count($courseData['courses']);
  1240. $courseCompleteList = array_merge($courseCompleteList, $courseData['courses']);
  1241. }
  1242. }
  1243. $coursesNotInCategoryCount += count($courses['not_category']);
  1244. $courseCompleteList = array_merge($courseCompleteList, $courses['not_category']);
  1245. if ($categoryCodeFilter) {
  1246. $courses['in_category'] = self::filterByCategory(
  1247. $courses['in_category'],
  1248. $categoryCodeFilter
  1249. );
  1250. $courses['not_category'] = self::filterByCategory(
  1251. $courses['not_category'],
  1252. $categoryCodeFilter
  1253. );
  1254. }
  1255. $this->tpl->assign('courses', $courses['not_category']);
  1256. $this->tpl->assign('categories', $courses['in_category']);
  1257. $listCourse = $this->tpl->fetch($this->tpl->get_template($coursesWithCategoryTemplate));
  1258. $listCourse .= $this->tpl->fetch($this->tpl->get_template($coursesWithoutCategoryTemplate));
  1259. }
  1260. $courseCount = count($specialCourses) + $coursesInCategoryCount + $coursesNotInCategoryCount;
  1261. }
  1262. $sessions_with_category = '';
  1263. $sessions_with_no_category = '';
  1264. if ($showSessions) {
  1265. $coursesListSessionStyle = api_get_configuration_value('courses_list_session_title_link');
  1266. $coursesListSessionStyle = $coursesListSessionStyle === false ? 1 : $coursesListSessionStyle;
  1267. if (api_is_drh()) {
  1268. $coursesListSessionStyle = 1;
  1269. }
  1270. $portalShowDescription = api_get_setting('show_session_description') === 'true';
  1271. // Declared listSession variable
  1272. $listSession = [];
  1273. $session_now = time();
  1274. if (is_array($session_categories)) {
  1275. foreach ($session_categories as $session_category) {
  1276. $session_category_id = $session_category['session_category']['id'];
  1277. // Sessions and courses that are not in a session category
  1278. if (empty($session_category_id) &&
  1279. isset($session_category['sessions'])
  1280. ) {
  1281. // Independent sessions
  1282. foreach ($session_category['sessions'] as $session) {
  1283. $session_id = $session['session_id'];
  1284. // Don't show empty sessions.
  1285. if (count($session['courses']) < 1) {
  1286. continue;
  1287. }
  1288. // Courses inside the current session.
  1289. $date_session_start = $session['access_start_date'];
  1290. $date_session_end = $session['access_end_date'];
  1291. $coachAccessStartDate = $session['coach_access_start_date'];
  1292. $coachAccessEndDate = $session['coach_access_end_date'];
  1293. $count_courses_session = 0;
  1294. // Loop course content
  1295. $html_courses_session = [];
  1296. $atLeastOneCourseIsVisible = false;
  1297. foreach ($session['courses'] as $course) {
  1298. $is_coach_course = api_is_coach($session_id, $course['real_id']);
  1299. $allowed_time = 0;
  1300. $allowedEndTime = true;
  1301. if (!empty($date_session_start)) {
  1302. if ($is_coach_course) {
  1303. $allowed_time = api_strtotime($coachAccessStartDate);
  1304. } else {
  1305. $allowed_time = api_strtotime($date_session_start);
  1306. }
  1307. if (!isset($_GET['history'])) {
  1308. if (!empty($date_session_end)) {
  1309. if ($is_coach_course) {
  1310. // if coach end date is empty we use the default end date
  1311. if (empty($coachAccessEndDate)) {
  1312. $endSessionToTms = api_strtotime($date_session_end);
  1313. if ($session_now > $endSessionToTms) {
  1314. $allowedEndTime = false;
  1315. }
  1316. } else {
  1317. $endSessionToTms = api_strtotime($coachAccessEndDate);
  1318. if ($session_now > $endSessionToTms) {
  1319. $allowedEndTime = false;
  1320. }
  1321. }
  1322. } else {
  1323. $endSessionToTms = api_strtotime($date_session_end);
  1324. if ($session_now > $endSessionToTms) {
  1325. $allowedEndTime = false;
  1326. }
  1327. }
  1328. }
  1329. }
  1330. }
  1331. if ($session_now >= $allowed_time && $allowedEndTime) {
  1332. // Read only and accessible.
  1333. $atLeastOneCourseIsVisible = true;
  1334. if (api_get_setting('hide_courses_in_sessions') == 'false') {
  1335. $courseUserHtml = CourseManager::get_logged_user_course_html(
  1336. $course,
  1337. $session_id,
  1338. 'session_course_item',
  1339. true,
  1340. $this->load_directories_preview
  1341. );
  1342. if (isset($courseUserHtml[1])) {
  1343. $course_session = $courseUserHtml[1];
  1344. $course_session['skill'] = isset($courseUserHtml['skill']) ? $courseUserHtml['skill'] : '';
  1345. // Course option (show student progress)
  1346. // This code will add new variables (Progress, Score, Certificate)
  1347. if ($studentInfoProgress || $studentInfoScore || $studentInfoCertificate) {
  1348. if ($studentInfoProgress) {
  1349. $progress = Tracking::get_avg_student_progress(
  1350. $user_id,
  1351. $course['course_code'],
  1352. array(),
  1353. $session_id
  1354. );
  1355. $course_session['student_info']['progress'] = $progress === false ? null : $progress;
  1356. }
  1357. if ($studentInfoScore) {
  1358. $percentage_score = Tracking::get_avg_student_score(
  1359. $user_id,
  1360. $course['course_code'],
  1361. array(),
  1362. $session_id
  1363. );
  1364. $course_session['student_info']['score'] = $percentage_score;
  1365. }
  1366. if ($studentInfoCertificate) {
  1367. $category = Category::load(
  1368. null,
  1369. null,
  1370. $course['course_code'],
  1371. null,
  1372. null,
  1373. $session_id
  1374. );
  1375. $course_session['student_info']['certificate'] = null;
  1376. if (isset($category[0])) {
  1377. if ($category[0]->is_certificate_available($user_id)) {
  1378. $course_session['student_info']['certificate'] = Display::label(get_lang('Yes'), 'success');
  1379. } else {
  1380. $course_session['student_info']['certificate'] = Display::label(get_lang('No'));
  1381. }
  1382. }
  1383. }
  1384. }
  1385. $html_courses_session[] = $course_session;
  1386. }
  1387. }
  1388. $count_courses_session++;
  1389. }
  1390. }
  1391. // No courses to show.
  1392. if ($atLeastOneCourseIsVisible == false) {
  1393. if (empty($html_courses_session)) {
  1394. continue;
  1395. }
  1396. }
  1397. if ($count_courses_session > 0) {
  1398. $params = array(
  1399. 'id' => $session_id
  1400. );
  1401. $session_box = Display::get_session_title_box($session_id);
  1402. $actions = api_get_path(WEB_CODE_PATH).'session/resume_session.php?id_session='.$session_id;
  1403. $coachId = $session_box['id_coach'];
  1404. $extraFieldValue = new ExtraFieldValue('session');
  1405. $imageField = $extraFieldValue->get_values_by_handler_and_field_variable(
  1406. $session_id,
  1407. 'image'
  1408. );
  1409. $params['category_id'] = $session_box['category_id'];
  1410. $params['title'] = $session_box['title'];
  1411. $params['id_coach'] = $coachId;
  1412. $params['coach_url'] = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=get_user_popup&user_id='.$coachId;
  1413. $params['coach_name'] = !empty($session_box['coach']) ? $session_box['coach'] : null;
  1414. $params['coach_avatar'] = UserManager::getUserPicture(
  1415. $coachId,
  1416. USER_IMAGE_SIZE_SMALL
  1417. );
  1418. $params['date'] = $session_box['dates'];
  1419. $params['image'] = isset($imageField['value']) ? $imageField['value'] : null;
  1420. $params['duration'] = isset($session_box['duration']) ? ' '.$session_box['duration'] : null;
  1421. $params['edit_actions'] = $actions;
  1422. $params['show_description'] = $session_box['show_description'] == 1 && $portalShowDescription;
  1423. $params['description'] = $session_box['description'];
  1424. $params['visibility'] = $session_box['visibility'];
  1425. $params['show_simple_session_info'] = $showSimpleSessionInfo;
  1426. $params['course_list_session_style'] = $coursesListSessionStyle;
  1427. $params['num_users'] = $session_box['num_users'];
  1428. $params['num_courses'] = $session_box['num_courses'];
  1429. $params['course_categories'] = CourseManager::getCourseCategoriesFromCourseList(
  1430. $html_courses_session
  1431. );
  1432. $params['courses'] = $html_courses_session;
  1433. if ($showSimpleSessionInfo) {
  1434. $params['subtitle'] = self::getSimpleSessionDetails(
  1435. $session_box['coach'],
  1436. $session_box['dates'],
  1437. isset($session_box['duration']) ? $session_box['duration'] : null
  1438. );
  1439. }
  1440. if ($gameModeIsActive) {
  1441. $params['stars'] =
  1442. GamificationUtils::getSessionStars($params['id'], $this->user_id);
  1443. $params['progress'] = GamificationUtils::getSessionProgress($params['id'],
  1444. $this->user_id);
  1445. $params['points'] =
  1446. GamificationUtils::getSessionPoints($params['id'], $this->user_id);
  1447. }
  1448. $listSession[] = $params;
  1449. $sessionCount++;
  1450. }
  1451. }
  1452. } else {
  1453. // All sessions included in
  1454. $count_courses_session = 0;
  1455. $html_sessions = '';
  1456. if (isset($session_category['sessions'])) {
  1457. foreach ($session_category['sessions'] as $session) {
  1458. $session_id = $session['session_id'];
  1459. // Don't show empty sessions.
  1460. if (count($session['courses']) < 1) {
  1461. continue;
  1462. }
  1463. $date_session_start = $session['access_start_date'];
  1464. $date_session_end = $session['access_end_date'];
  1465. $coachAccessStartDate = $session['coach_access_start_date'];
  1466. $coachAccessEndDate = $session['coach_access_end_date'];
  1467. $html_courses_session = [];
  1468. $count = 0;
  1469. foreach ($session['courses'] as $course) {
  1470. $is_coach_course = api_is_coach($session_id, $course['real_id']);
  1471. $allowed_time = 0;
  1472. $allowedEndTime = true;
  1473. if (!empty($date_session_start)) {
  1474. if ($is_coach_course) {
  1475. $allowed_time = api_strtotime($coachAccessStartDate);
  1476. } else {
  1477. $allowed_time = api_strtotime($date_session_start);
  1478. }
  1479. if (!isset($_GET['history'])) {
  1480. if (!empty($date_session_end)) {
  1481. if ($is_coach_course) {
  1482. // if coach end date is empty we use the default end date
  1483. if (empty($coachAccessEndDate)) {
  1484. $endSessionToTms = api_strtotime($date_session_end);
  1485. if ($session_now > $endSessionToTms) {
  1486. $allowedEndTime = false;
  1487. }
  1488. } else {
  1489. $endSessionToTms = api_strtotime($coachAccessEndDate);
  1490. if ($session_now > $endSessionToTms) {
  1491. $allowedEndTime = false;
  1492. }
  1493. }
  1494. } else {
  1495. $endSessionToTms = api_strtotime($date_session_end);
  1496. if ($session_now > $endSessionToTms) {
  1497. $allowedEndTime = false;
  1498. }
  1499. }
  1500. }
  1501. }
  1502. }
  1503. if ($session_now >= $allowed_time && $allowedEndTime) {
  1504. if (api_get_setting('hide_courses_in_sessions') === 'false') {
  1505. $c = CourseManager::get_logged_user_course_html(
  1506. $course,
  1507. $session_id,
  1508. 'session_course_item'
  1509. );
  1510. $html_courses_session[] = $c[1];
  1511. }
  1512. $count_courses_session++;
  1513. $count++;
  1514. }
  1515. }
  1516. $sessionParams = [];
  1517. // Category
  1518. if ($count > 0) {
  1519. $session_box = Display::get_session_title_box($session_id);
  1520. $sessionParams[0]['id'] = $session_id;
  1521. $sessionParams[0]['date'] = $session_box['dates'];
  1522. $sessionParams[0]['duration'] = isset($session_box['duration']) ? ' '.$session_box['duration'] : null;
  1523. $sessionParams[0]['course_list_session_style'] = $coursesListSessionStyle;
  1524. $sessionParams[0]['title'] = $session_box['title'];
  1525. $sessionParams[0]['subtitle'] = (!empty($session_box['coach']) ? $session_box['coach'].' | ' : '').$session_box['dates'];
  1526. $sessionParams[0]['show_actions'] = api_is_platform_admin();
  1527. $sessionParams[0]['courses'] = $html_courses_session;
  1528. $sessionParams[0]['show_simple_session_info'] = $showSimpleSessionInfo;
  1529. $sessionParams[0]['coach_name'] = !empty($session_box['coach']) ? $session_box['coach'] : null;
  1530. if ($showSimpleSessionInfo) {
  1531. $sessionParams[0]['subtitle'] = self::getSimpleSessionDetails(
  1532. $session_box['coach'],
  1533. $session_box['dates'],
  1534. isset($session_box['duration']) ? $session_box['duration'] : null
  1535. );
  1536. }
  1537. $this->tpl->assign('session', $sessionParams);
  1538. $html_sessions .= $this->tpl->fetch(
  1539. $this->tpl->get_template('user_portal/classic_session.tpl')
  1540. );
  1541. $sessionCount++;
  1542. }
  1543. }
  1544. }
  1545. if ($count_courses_session > 0) {
  1546. $categoryParams = array(
  1547. 'id' => $session_category['session_category']['id'],
  1548. 'title' => $session_category['session_category']['name'],
  1549. 'show_actions' => api_is_platform_admin(),
  1550. 'subtitle' => '',
  1551. 'sessions' => $html_sessions
  1552. );
  1553. $session_category_start_date = $session_category['session_category']['date_start'];
  1554. $session_category_end_date = $session_category['session_category']['date_end'];
  1555. if ($session_category_start_date == '0000-00-00') {
  1556. $session_category_start_date = '';
  1557. }
  1558. if ($session_category_end_date == '0000-00-00') {
  1559. $session_category_end_date = '';
  1560. }
  1561. if (!empty($session_category_start_date) &&
  1562. !empty($session_category_end_date)
  1563. ) {
  1564. $categoryParams['subtitle'] = sprintf(
  1565. get_lang('FromDateXToDateY'),
  1566. $session_category_start_date,
  1567. $session_category_end_date
  1568. );
  1569. } else {
  1570. if (!empty($session_category_start_date)) {
  1571. $categoryParams['subtitle'] = get_lang('From').' '.$session_category_start_date;
  1572. }
  1573. if (!empty($session_category_end_date)) {
  1574. $categoryParams['subtitle'] = get_lang('Until').' '.$session_category_end_date;
  1575. }
  1576. }
  1577. $this->tpl->assign('session_category', $categoryParams);
  1578. $sessions_with_category .= $this->tpl->fetch(
  1579. "{$this->tpl->templateFolder}/user_portal/session_category.tpl"
  1580. );
  1581. }
  1582. $this->tpl->assign('session_category', $categoryParams);
  1583. $sessions_with_category .= $this->tpl->fetch(
  1584. "{$this->tpl->templateFolder}/user_portal/session_category.tpl"
  1585. );
  1586. }
  1587. }
  1588. }
  1589. $allCoursesInSessions = [];
  1590. foreach ($listSession as $currentSession) {
  1591. $coursesInSessions = $currentSession['courses'];
  1592. unset($currentSession['courses']);
  1593. foreach ($coursesInSessions as $coursesInSession) {
  1594. $coursesInSession['session'] = $currentSession;
  1595. $allCoursesInSessions[] = $coursesInSession;
  1596. }
  1597. }
  1598. }
  1599. $this->tpl->assign('all_courses', $allCoursesInSessions);
  1600. $this->tpl->assign('session', $listSession);
  1601. $this->tpl->assign('show_tutor', (api_get_setting('show_session_coach') === 'true' ? true : false));
  1602. $this->tpl->assign('gamification_mode', $gameModeIsActive);
  1603. $this->tpl->assign('remove_session_url', api_get_configuration_value('remove_session_url'));
  1604. if ($viewGridCourses) {
  1605. $sessions_with_no_category = $this->tpl->fetch(
  1606. $this->tpl->get_template('/user_portal/grid_session.tpl')
  1607. );
  1608. } else {
  1609. $sessions_with_no_category = $this->tpl->fetch(
  1610. $this->tpl->get_template('user_portal/classic_session.tpl')
  1611. );
  1612. }
  1613. }
  1614. }
  1615. return [
  1616. 'courses' => $courseCompleteList,
  1617. 'sessions' => $session_categories,
  1618. 'html' => trim($specialCourseList.$sessions_with_category.$sessions_with_no_category.$listCourse),
  1619. 'session_count' => $sessionCount,
  1620. 'course_count' => $courseCount
  1621. ];
  1622. }
  1623. /**
  1624. * Shows a welcome message when the user doesn't have any content in the course list
  1625. */
  1626. public function return_welcome_to_course_block()
  1627. {
  1628. $count_courses = CourseManager::count_courses();
  1629. $tpl = $this->tpl->get_template('layout/welcome_to_course.tpl');
  1630. $course_catalog_url = api_get_path(WEB_CODE_PATH).'auth/courses.php';
  1631. $course_list_url = api_get_path(WEB_PATH).'user_portal.php';
  1632. $this->tpl->assign('course_catalog_url', $course_catalog_url);
  1633. $this->tpl->assign('course_list_url', $course_list_url);
  1634. $this->tpl->assign('course_catalog_link', Display::url(get_lang('Here'), $course_catalog_url));
  1635. $this->tpl->assign('course_list_link', Display::url(get_lang('Here'), $course_list_url));
  1636. $this->tpl->assign('count_courses', $count_courses);
  1637. return $this->tpl->fetch($tpl);
  1638. }
  1639. /**
  1640. * @return array
  1641. */
  1642. public function return_hot_courses()
  1643. {
  1644. return CourseManager::return_hot_courses(30, 6);
  1645. }
  1646. /**
  1647. * UserPortal view for session, return the HTML of the course list
  1648. * @param $user_id
  1649. * @return string
  1650. */
  1651. public function returnCoursesAndSessionsViewBySession($user_id)
  1652. {
  1653. $sessionCount = 0;
  1654. $courseCount = 0;
  1655. $load_history = (isset($_GET['history']) && intval($_GET['history']) == 1) ? true : false;
  1656. if ($load_history) {
  1657. // Load sessions in category in *history*
  1658. $session_categories = UserManager::get_sessions_by_category($user_id, true);
  1659. } else {
  1660. // Load sessions in category
  1661. $session_categories = UserManager::get_sessions_by_category($user_id, false);
  1662. }
  1663. $html = '';
  1664. $loadDirs = $this->load_directories_preview;
  1665. // If we're not in the history view...
  1666. $listCoursesInfo = array();
  1667. if (!isset($_GET['history'])) {
  1668. // Display special courses
  1669. $specialCoursesResult = CourseManager::returnSpecialCourses(
  1670. $user_id,
  1671. $loadDirs
  1672. );
  1673. $specialCourses = $specialCoursesResult;
  1674. if ($specialCourses) {
  1675. $this->tpl->assign('courses', $specialCourses);
  1676. $html = $this->tpl->fetch(
  1677. $this->tpl->get_template('/user_portal/classic_courses_without_category.tpl')
  1678. );
  1679. }
  1680. // Display courses
  1681. // [code=>xxx, real_id=>000]
  1682. $listCourses = CourseManager::get_courses_list_by_user_id(
  1683. $user_id,
  1684. false
  1685. );
  1686. foreach ($listCourses as $i => $listCourseCodeId) {
  1687. if (isset($listCourseCodeId['special_course'])) {
  1688. continue;
  1689. }
  1690. $courseCategory = CourseManager::getUserCourseCategoryForCourse(
  1691. $user_id,
  1692. $listCourseCodeId['real_id']
  1693. );
  1694. $userCatTitle = '';
  1695. $userCategoryId = 0;
  1696. if ($courseCategory) {
  1697. $userCategoryId = $courseCategory['user_course_cat'];
  1698. $userCatTitle = $courseCategory['title'];
  1699. }
  1700. $listCourse = api_get_course_info_by_id($listCourseCodeId['real_id']);
  1701. $listCoursesInfo[] = array(
  1702. 'course' => $listCourse,
  1703. 'code' => $listCourseCodeId['code'],
  1704. 'id' => $listCourseCodeId['real_id'],
  1705. 'title' => $listCourse['title'],
  1706. 'userCatId' => $userCategoryId,
  1707. 'userCatTitle' => $userCatTitle
  1708. );
  1709. $courseCount++;
  1710. }
  1711. usort($listCoursesInfo, 'self::compareByCourse');
  1712. }
  1713. if (is_array($session_categories)) {
  1714. // all courses that are in a session
  1715. $listCoursesInSession = SessionManager::getNamedSessionCourseForCoach($user_id);
  1716. }
  1717. // we got all courses
  1718. // for each user category, sorted alphabetically, display courses
  1719. $listUserCategories = CourseManager::get_user_course_categories($user_id);
  1720. $listCoursesAlreadyDisplayed = array();
  1721. uasort($listUserCategories, "self::compareListUserCategory");
  1722. $listUserCategories[0] = '';
  1723. $html .= '<div class="session-view-block">';
  1724. foreach ($listUserCategories as $userCategoryId => $userCat) {
  1725. // add user category
  1726. $userCategoryHtml = '';
  1727. if ($userCategoryId != 0) {
  1728. $userCategoryHtml = '<div class="session-view-well ">';
  1729. $userCategoryHtml .= self::getHtmlForUserCategory($userCategoryId, $userCat['title']);
  1730. }
  1731. // look for course in this userCat in session courses : $listCoursesInSession
  1732. $htmlCategory = '';
  1733. if (isset($listCoursesInSession[$userCategoryId])) {
  1734. // list of courses in this user cat
  1735. foreach ($listCoursesInSession[$userCategoryId]['courseInUserCatList'] as $i => $listCourse) {
  1736. // add course
  1737. $listCoursesAlreadyDisplayed[$listCourse['courseId']] = 1;
  1738. if ($userCategoryId == 0) {
  1739. $htmlCategory .= '<div class="panel panel-default">';
  1740. } else {
  1741. $htmlCategory .= '<div class="panel panel-default">';
  1742. }
  1743. $htmlCategory .= '<div class="panel-body">';
  1744. $coursesInfo = $listCourse['course'];
  1745. $htmlCategory .= self::getHtmlForCourse(
  1746. $coursesInfo,
  1747. $userCategoryId,
  1748. 1,
  1749. $loadDirs
  1750. );
  1751. // list of session category
  1752. $htmlSessionCategory = '<div class="session-view-row" style="display:none;" id="courseblock-'.$coursesInfo['real_id'].'">';
  1753. foreach ($listCourse['sessionCatList'] as $j => $listCategorySession) {
  1754. // add session category
  1755. $htmlSessionCategory .= self::getHtmlSessionCategory(
  1756. $listCategorySession['catSessionId'],
  1757. $listCategorySession['catSessionName']
  1758. );
  1759. // list of session
  1760. $htmlSession = ''; // start
  1761. foreach ($listCategorySession['sessionList'] as $k => $listSession) {
  1762. // add session
  1763. $htmlSession .= '<div class="session-view-row">';
  1764. $htmlSession .= self::getHtmlForSession(
  1765. $listSession['sessionId'],
  1766. $listSession['sessionName'],
  1767. $listCategorySession['catSessionId'],
  1768. $coursesInfo
  1769. );
  1770. $htmlSession .= '</div>';
  1771. $sessionCount++;
  1772. }
  1773. $htmlSession .= ''; // end session block
  1774. $htmlSessionCategory .= $htmlSession;
  1775. }
  1776. $htmlSessionCategory .= '</div>'; // end session cat block
  1777. $htmlCategory .= $htmlSessionCategory.'</div></div>';
  1778. $htmlCategory .= ''; // end course block
  1779. }
  1780. $userCategoryHtml .= $htmlCategory;
  1781. }
  1782. // look for courses in this userCat in not in session courses : $listCoursesInfo
  1783. // if course not already added
  1784. $htmlCategory = '';
  1785. foreach ($listCoursesInfo as $i => $listCourse) {
  1786. if ($listCourse['userCatId'] == $userCategoryId && !isset($listCoursesAlreadyDisplayed[$listCourse['id']])) {
  1787. if ($userCategoryId != 0) {
  1788. $htmlCategory .= '<div class="panel panel-default">';
  1789. } else {
  1790. $htmlCategory .= '<div class="panel panel-default">';
  1791. }
  1792. $htmlCategory .= '<div class="panel-body">';
  1793. $htmlCategory .= self::getHtmlForCourse(
  1794. $listCourse['course'],
  1795. $userCategoryId,
  1796. 0,
  1797. $loadDirs
  1798. );
  1799. $htmlCategory .= '</div></div>';
  1800. }
  1801. }
  1802. $htmlCategory .= '';
  1803. $userCategoryHtml .= $htmlCategory; // end user cat block
  1804. if ($userCategoryId != 0) {
  1805. $userCategoryHtml .= '</div>';
  1806. }
  1807. $html .= $userCategoryHtml; //
  1808. }
  1809. $html .= '</div>';
  1810. return [
  1811. 'html' => $html,
  1812. 'sessions' => $session_categories,
  1813. 'courses' => $listCoursesInfo,
  1814. 'session_count' => $sessionCount,
  1815. 'course_count' => $courseCount
  1816. ];
  1817. }
  1818. /**
  1819. * Return HTML code for personal user course category
  1820. * @param $id
  1821. * @param $title
  1822. * @return string
  1823. */
  1824. private static function getHtmlForUserCategory($id, $title)
  1825. {
  1826. if ($id == 0) {
  1827. return '';
  1828. }
  1829. $icon = Display::return_icon(
  1830. 'folder_yellow.png',
  1831. $title,
  1832. array('class' => 'sessionView'),
  1833. ICON_SIZE_LARGE
  1834. );
  1835. return "<div class='session-view-user-category'>$icon<span>$title</span></div>";
  1836. }
  1837. /**
  1838. * return HTML code for course display in session view
  1839. * @param array $courseInfo
  1840. * @param $userCategoryId
  1841. * @param bool $displayButton
  1842. * @param $loadDirs
  1843. * @return string
  1844. */
  1845. private static function getHtmlForCourse(
  1846. $courseInfo,
  1847. $userCategoryId,
  1848. $displayButton = false,
  1849. $loadDirs
  1850. ) {
  1851. if (empty($courseInfo)) {
  1852. return '';
  1853. }
  1854. $id = $courseInfo['real_id'];
  1855. $title = $courseInfo['title'];
  1856. $code = $courseInfo['code'];
  1857. $class = 'session-view-lvl-6';
  1858. if ($userCategoryId != 0 && !$displayButton) {
  1859. $class = 'session-view-lvl-7';
  1860. }
  1861. $class2 = 'session-view-lvl-6';
  1862. if ($displayButton || $userCategoryId != 0) {
  1863. $class2 = 'session-view-lvl-7';
  1864. }
  1865. $button = '';
  1866. if ($displayButton) {
  1867. $button = '<input id="session-view-button-'.intval($id).'" class="btn btn-default btn-sm" type="button" onclick="hideUnhide(\'courseblock-'.intval($id).'\', \'session-view-button-'.intval($id).'\', \'+\', \'-\')" value="+" />';
  1868. }
  1869. $icon = Display::return_icon(
  1870. 'blackboard.png',
  1871. $title,
  1872. array('class' => 'sessionView'),
  1873. ICON_SIZE_LARGE
  1874. );
  1875. $courseLink = $courseInfo['course_public_url'].'?id_session=0';
  1876. // get html course params
  1877. $courseParams = CourseManager::getCourseParamsForDisplay($id, $loadDirs);
  1878. $teachers = '';
  1879. $rightActions = '';
  1880. // teacher list
  1881. if (!empty($courseParams['teachers'])) {
  1882. $teachers = '<p class="'.$class2.' view-by-session-teachers">'.$courseParams['teachers'].'</p>';
  1883. }
  1884. // notification
  1885. if (!empty($courseParams['right_actions'])) {
  1886. $rightActions = '<div class="pull-right">'.$courseParams['right_actions'].'</div>';
  1887. }
  1888. $notifications = isset($courseParams['notifications']) ? $courseParams['notifications'] : '';
  1889. return "<div>
  1890. $button
  1891. <span class='$class'>$icon
  1892. <a class='sessionView' href='$courseLink'>$title</a>
  1893. </span>
  1894. $notifications
  1895. $rightActions
  1896. </div>
  1897. $teachers";
  1898. }
  1899. /**
  1900. * return HTML code for session category
  1901. * @param $id
  1902. * @param $title
  1903. * @return string
  1904. */
  1905. private static function getHtmlSessionCategory($id, $title)
  1906. {
  1907. if ($id == 0) {
  1908. return '';
  1909. }
  1910. $icon = Display::return_icon(
  1911. 'folder_blue.png',
  1912. $title,
  1913. array('class' => 'sessionView'),
  1914. ICON_SIZE_LARGE
  1915. );
  1916. return "<div class='session-view-session-category'>
  1917. <span class='session-view-lvl-2'>
  1918. $icon
  1919. <span>$title</span>
  1920. </span>
  1921. </div>";
  1922. }
  1923. /**
  1924. * return HTML code for session
  1925. * @param int $id session id
  1926. * @param string $title session title
  1927. * @param int $categorySessionId
  1928. * @param array $courseInfo
  1929. *
  1930. * @return string
  1931. */
  1932. private static function getHtmlForSession($id, $title, $categorySessionId, $courseInfo)
  1933. {
  1934. $html = '';
  1935. if ($categorySessionId == 0) {
  1936. $class1 = 'session-view-lvl-2'; // session
  1937. $class2 = 'session-view-lvl-4'; // got to course in session link
  1938. } else {
  1939. $class1 = 'session-view-lvl-3'; // session
  1940. $class2 = 'session-view-lvl-5'; // got to course in session link
  1941. }
  1942. $icon = Display::return_icon(
  1943. 'blackboard_blue.png',
  1944. $title,
  1945. array('class' => 'sessionView'),
  1946. ICON_SIZE_LARGE
  1947. );
  1948. $courseLink = $courseInfo['course_public_url'].'?id_session='.intval($id);
  1949. $html .= "<span class='$class1 session-view-session'>$icon$title</span>";
  1950. $html .= '<div class="'.$class2.' session-view-session-go-to-course-in-session">
  1951. <a class="" href="'.$courseLink.'">'.get_lang('GoToCourseInsideSession').'</a></div>';
  1952. return '<div>'.$html.'</div>';
  1953. }
  1954. /**
  1955. * @param $listA
  1956. * @param $listB
  1957. * @return int
  1958. */
  1959. private static function compareByCourse($listA, $listB)
  1960. {
  1961. if ($listA['userCatTitle'] == $listB['userCatTitle']) {
  1962. if ($listA['title'] == $listB['title']) {
  1963. return 0;
  1964. }
  1965. if ($listA['title'] > $listB['title']) {
  1966. return 1;
  1967. }
  1968. return -1;
  1969. }
  1970. if ($listA['userCatTitle'] > $listB['userCatTitle']) {
  1971. return 1;
  1972. }
  1973. return -1;
  1974. }
  1975. /**
  1976. * @param $listA
  1977. * @param $listB
  1978. * @return int
  1979. */
  1980. public static function compareListUserCategory($listA, $listB)
  1981. {
  1982. if ($listA['title'] == $listB['title']) {
  1983. return 0;
  1984. }
  1985. if ($listA['title'] > $listB['title']) {
  1986. return 1;
  1987. }
  1988. return -1;
  1989. }
  1990. /**
  1991. * @param $view
  1992. * @param $userId
  1993. */
  1994. public static function setDefaultMyCourseView($view, $userId)
  1995. {
  1996. setcookie('defaultMyCourseView'.$userId, $view);
  1997. }
  1998. /**
  1999. * Get the session coach name, duration or dates
  2000. * when $_configuration['show_simple_session_info'] is enabled
  2001. * @param string $coachName
  2002. * @param string $dates
  2003. * @param string|null $duration Optional
  2004. * @return string
  2005. */
  2006. private static function getSimpleSessionDetails($coachName, $dates, $duration = null)
  2007. {
  2008. $strDetails = [];
  2009. if (!empty($coachName)) {
  2010. $strDetails[] = $coachName;
  2011. }
  2012. $strDetails[] = !empty($duration) ? $duration : $dates;
  2013. return implode(' | ', $strDetails);
  2014. }
  2015. /**
  2016. * @param int $userId
  2017. * @return array
  2018. */
  2019. public function returnCourseCategoryListFromUser($userId)
  2020. {
  2021. $sessionCount = 0;
  2022. $courseList = CourseManager::get_courses_list_by_user_id($userId);
  2023. $categoryCodes = CourseManager::getCourseCategoriesFromCourseList($courseList);
  2024. $categories = [];
  2025. foreach ($categoryCodes as $categoryCode) {
  2026. $categories[] = CourseCategory::getCategory($categoryCode);
  2027. }
  2028. $template = new Template('', false, false, false, true, false, false);
  2029. $layout = $template->get_template('user_portal/course_categories.tpl');
  2030. $template->assign('course_categories', $categories);
  2031. return [
  2032. 'courses' => $courseList,
  2033. 'html' => $template->fetch($layout),
  2034. 'course_count' => count($courseList),
  2035. 'session_count' => $sessionCount
  2036. ];
  2037. }
  2038. /**
  2039. * Filter the course list by category code
  2040. * @param array $courseList course list
  2041. * @param string $categoryCode
  2042. * @return array
  2043. */
  2044. private static function filterByCategory($courseList, $categoryCode)
  2045. {
  2046. return array_filter($courseList, function ($courseInfo) use ($categoryCode) {
  2047. if (isset($courseInfo['category_code']) &&
  2048. $courseInfo['category_code'] === $categoryCode
  2049. ) {
  2050. return true;
  2051. }
  2052. return false;
  2053. });
  2054. }
  2055. }