template.lib.php 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\SessionRelCourseRelUser;
  4. use Chamilo\UserBundle\Entity\User;
  5. /**
  6. * Class Template
  7. *
  8. * @author Julio Montoya <gugli100@gmail.com>
  9. * @todo better organization of the class, methods and variables
  10. *
  11. */
  12. class Template
  13. {
  14. /**
  15. * The Template folder name see main/template
  16. * @var string
  17. */
  18. public $templateFolder = 'default';
  19. /**
  20. * The theme that will be used: chamilo, public_admin, chamilo_red, etc
  21. * This variable is set from the database
  22. * @var string
  23. */
  24. public $theme = '';
  25. private $themeDir;
  26. /**
  27. * @var string
  28. */
  29. public $preview_theme = '';
  30. public $title = null;
  31. public $show_header;
  32. public $show_footer;
  33. public $help;
  34. public $menu_navigation = []; //Used in the userportal.lib.php function: return_navigation_course_links()
  35. public $show_learnpath = false; // This is a learnpath section or not?
  36. public $plugin = null;
  37. public $course_id = null;
  38. public $user_is_logged_in = false;
  39. public $twig = null;
  40. /* Loads chamilo plugins */
  41. public $load_plugins = false;
  42. public $params = [];
  43. public $force_plugin_load = false;
  44. public $responseCode = 0;
  45. /**
  46. * @param string $title
  47. * @param bool $show_header
  48. * @param bool $show_footer
  49. * @param bool $show_learnpath
  50. * @param bool $hide_global_chat
  51. * @param bool $load_plugins
  52. * @param int $responseCode
  53. * @param bool $sendHeaders send http headers or not
  54. */
  55. public function __construct(
  56. $title = '',
  57. $show_header = true,
  58. $show_footer = true,
  59. $show_learnpath = false,
  60. $hide_global_chat = false,
  61. $load_plugins = true,
  62. $sendHeaders = true,
  63. $responseCode = 0
  64. ) {
  65. // Page title
  66. $this->title = $title;
  67. $this->show_learnpath = $show_learnpath;
  68. $this->setResponseCode($responseCode);
  69. if (empty($this->show_learnpath)) {
  70. $origin = api_get_origin();
  71. if ($origin === 'learnpath') {
  72. $this->show_learnpath = true;
  73. $show_footer = false;
  74. $show_header = false;
  75. }
  76. }
  77. $this->hide_global_chat = $hide_global_chat;
  78. $this->load_plugins = $load_plugins;
  79. $template_paths = [
  80. api_get_path(SYS_CODE_PATH).'template/overrides', // user defined templates
  81. api_get_path(SYS_CODE_PATH).'template', //template folder
  82. api_get_path(SYS_PLUGIN_PATH) // plugin folder
  83. ];
  84. $urlId = api_get_current_access_url_id();
  85. $cache_folder = api_get_path(SYS_ARCHIVE_PATH).'twig/'.$urlId.'/';
  86. if (!is_dir($cache_folder)) {
  87. mkdir($cache_folder, api_get_permissions_for_new_directories(), true);
  88. }
  89. $loader = new Twig_Loader_Filesystem($template_paths);
  90. $isTestMode = api_get_setting('server_type') === 'test';
  91. //Setting Twig options depending on the server see http://twig.sensiolabs.org/doc/api.html#environment-options
  92. if ($isTestMode) {
  93. $options = [
  94. //'cache' => api_get_path(SYS_ARCHIVE_PATH), //path to the cache folder
  95. 'autoescape' => false,
  96. 'debug' => true,
  97. 'auto_reload' => true,
  98. 'optimizations' => 0,
  99. // turn on optimizations with -1
  100. 'strict_variables' => false,
  101. //If set to false, Twig will silently ignore invalid variables
  102. ];
  103. } else {
  104. $options = [
  105. 'cache' => $cache_folder,
  106. //path to the cache folder
  107. 'autoescape' => false,
  108. 'debug' => false,
  109. 'auto_reload' => false,
  110. 'optimizations' => -1,
  111. // turn on optimizations with -1
  112. 'strict_variables' => false
  113. //If set to false, Twig will silently ignore invalid variables
  114. ];
  115. }
  116. $this->twig = new Twig_Environment($loader, $options);
  117. if ($isTestMode) {
  118. $this->twig->addExtension(new Twig_Extension_Debug());
  119. }
  120. // Twig filters setup
  121. $filters = [
  122. 'get_plugin_lang',
  123. 'get_lang',
  124. 'api_get_path',
  125. 'api_get_local_time',
  126. 'api_convert_and_format_date',
  127. 'api_is_allowed_to_edit',
  128. 'api_get_user_info',
  129. 'api_get_configuration_value',
  130. 'api_get_setting',
  131. [
  132. 'name' => 'return_message',
  133. 'callable' => 'Display::return_message_and_translate'
  134. ],
  135. [
  136. 'name' => 'display_page_header',
  137. 'callable' => 'Display::page_header_and_translate'
  138. ],
  139. [
  140. 'name' => 'display_page_subheader',
  141. 'callable' => 'Display::page_subheader_and_translate'
  142. ],
  143. [
  144. 'name' => 'icon',
  145. 'callable' => 'Template::get_icon_path'
  146. ],
  147. [
  148. 'name' => 'img',
  149. 'callable' => 'Template::get_image'
  150. ],
  151. [
  152. 'name' => 'format_date',
  153. 'callable' => 'Template::format_date'
  154. ]
  155. ];
  156. foreach ($filters as $filter) {
  157. if (is_array($filter)) {
  158. $this->twig->addFilter(new Twig_SimpleFilter($filter['name'], $filter['callable']));
  159. } else {
  160. $this->twig->addFilter(new Twig_SimpleFilter($filter, $filter));
  161. }
  162. }
  163. $functions = [
  164. ['name' => 'get_tutors_names', 'callable' => 'Template::returnTutorsNames'],
  165. ['name' => 'get_teachers_names', 'callable' => 'Template::returnTeachersNames']
  166. ];
  167. foreach ($functions as $function) {
  168. $this->twig->addFunction(new Twig_SimpleFunction($function['name'], $function['callable']));
  169. }
  170. // Setting system variables
  171. $this->set_system_parameters();
  172. // Setting user variables
  173. $this->set_user_parameters();
  174. // Setting course variables
  175. $this->set_course_parameters();
  176. // Setting administrator variables
  177. $this->setAdministratorParams();
  178. //$this->setCSSEditor();
  179. // Header and footer are showed by default
  180. $this->set_footer($show_footer);
  181. $this->set_header($show_header);
  182. $this->set_header_parameters($sendHeaders);
  183. $this->set_footer_parameters();
  184. $defaultStyle = api_get_configuration_value('default_template');
  185. if (!empty($defaultStyle)) {
  186. $this->templateFolder = $defaultStyle;
  187. }
  188. $this->assign('template', $this->templateFolder);
  189. $this->assign('locale', api_get_language_isocode());
  190. $this->assign('login_class', null);
  191. $allow = api_get_configuration_value('show_language_selector_in_menu');
  192. if ($allow) {
  193. $this->assign('language_form', api_display_language_form());
  194. }
  195. // Chamilo plugins
  196. if ($this->show_header) {
  197. if ($this->load_plugins) {
  198. $this->plugin = new AppPlugin();
  199. //1. Showing installed plugins in regions
  200. $pluginRegions = $this->plugin->get_plugin_regions();
  201. foreach ($pluginRegions as $region) {
  202. $this->set_plugin_region($region);
  203. }
  204. //2. Loading the course plugin info
  205. global $course_plugin;
  206. if (isset($course_plugin) && !empty($course_plugin) && !empty($this->course_id)) {
  207. //Load plugin get_langs
  208. $this->plugin->load_plugin_lang_variables($course_plugin);
  209. }
  210. }
  211. }
  212. }
  213. /**
  214. * @param string $image
  215. * @param int $size
  216. *
  217. * @return string
  218. */
  219. public static function get_icon_path($image, $size = ICON_SIZE_SMALL)
  220. {
  221. return Display::return_icon($image, '', [], $size, false, true);
  222. }
  223. /**
  224. * @param string $image
  225. * @param int $size
  226. * @param string $name
  227. * @return string
  228. */
  229. public static function get_image($image, $size = ICON_SIZE_SMALL, $name = '')
  230. {
  231. return Display::return_icon($image, $name, [], $size);
  232. }
  233. /**
  234. * @param string $timestamp
  235. * @param string $format
  236. *
  237. * @return string
  238. */
  239. public static function format_date($timestamp, $format = null)
  240. {
  241. return api_format_date($timestamp, $format);
  242. }
  243. /**
  244. * Return the item's url key:
  245. *
  246. * c_id=xx&id=xx
  247. *
  248. * @param object $item
  249. * @return string
  250. */
  251. public static function key($item)
  252. {
  253. $id = isset($item->id) ? $item->id : null;
  254. $c_id = isset($item->c_id) ? $item->c_id : null;
  255. $result = '';
  256. if ($c_id) {
  257. $result = "c_id=$c_id";
  258. }
  259. if ($id) {
  260. if ($result) {
  261. $result .= "&amp;id=$id";
  262. } else {
  263. $result .= "&amp;id=$id";
  264. }
  265. }
  266. return $result;
  267. }
  268. /**
  269. * @param string $helpInput
  270. */
  271. public function setHelp($helpInput = null)
  272. {
  273. if (!empty($helpInput)) {
  274. $help = $helpInput;
  275. } else {
  276. $help = $this->help;
  277. }
  278. $content = '';
  279. if (api_get_setting('enable_help_link') == 'true') {
  280. if (!empty($help)) {
  281. $help = Security::remove_XSS($help);
  282. $content = '<div class="help">';
  283. $content .= Display::url(
  284. Display::return_icon('help.large.png', get_lang('Help')),
  285. api_get_path(WEB_CODE_PATH).'help/help.php?open='.$help,
  286. [
  287. 'class' => 'ajax',
  288. 'data-title' => get_lang('Help')
  289. ]
  290. );
  291. $content .= '</div>';
  292. }
  293. }
  294. $this->assign('help_content', $content);
  295. }
  296. /**
  297. * Use template system to parse the actions menu
  298. * @todo finish it!
  299. **/
  300. public function set_actions($actions)
  301. {
  302. $action_string = '';
  303. if (!empty($actions)) {
  304. foreach ($actions as $action) {
  305. $action_string .= $action;
  306. }
  307. }
  308. $this->assign('actions', $actions);
  309. }
  310. /**
  311. * Shortcut to display a 1 col layout (index.php)
  312. * */
  313. public function display_one_col_template()
  314. {
  315. $tpl = $this->get_template('layout/layout_1_col.tpl');
  316. $this->display($tpl);
  317. }
  318. /**
  319. * Shortcut to display a 2 col layout (userportal.php)
  320. **/
  321. public function display_two_col_template()
  322. {
  323. $tpl = $this->get_template('layout/layout_2_col.tpl');
  324. $this->display($tpl);
  325. }
  326. /**
  327. * Displays an empty template
  328. */
  329. public function display_blank_template()
  330. {
  331. $tpl = $this->get_template('layout/blank.tpl');
  332. $this->display($tpl);
  333. }
  334. /**
  335. * Displays an empty template
  336. */
  337. public function displayBlankTemplateNoHeader()
  338. {
  339. $tpl = $this->get_template('layout/blank_no_header.tpl');
  340. $this->display($tpl);
  341. }
  342. /**
  343. * Displays an empty template
  344. */
  345. public function display_no_layout_template()
  346. {
  347. $tpl = $this->get_template('layout/no_layout.tpl');
  348. $this->display($tpl);
  349. }
  350. /**
  351. * Sets the footer visibility
  352. * @param bool true if we show the footer
  353. */
  354. public function set_footer($status)
  355. {
  356. $this->show_footer = $status;
  357. $this->assign('show_footer', $status);
  358. }
  359. /**
  360. * return true if toolbar has to be displayed for user
  361. * @return bool
  362. */
  363. public static function isToolBarDisplayedForUser()
  364. {
  365. //Toolbar
  366. $show_admin_toolbar = api_get_setting('show_admin_toolbar');
  367. $show_toolbar = false;
  368. switch ($show_admin_toolbar) {
  369. case 'do_not_show':
  370. break;
  371. case 'show_to_admin':
  372. if (api_is_platform_admin()) {
  373. $show_toolbar = true;
  374. }
  375. break;
  376. case 'show_to_admin_and_teachers':
  377. if (api_is_platform_admin() || api_is_allowed_to_edit()) {
  378. $show_toolbar = true;
  379. }
  380. break;
  381. case 'show_to_all':
  382. $show_toolbar = true;
  383. break;
  384. }
  385. return $show_toolbar;
  386. }
  387. /**
  388. * Sets the header visibility
  389. * @param bool true if we show the header
  390. */
  391. public function set_header($status)
  392. {
  393. $this->show_header = $status;
  394. $this->assign('show_header', $status);
  395. $show_toolbar = 0;
  396. if (self::isToolBarDisplayedForUser()) {
  397. $show_toolbar = 1;
  398. }
  399. $this->assign('show_toolbar', $show_toolbar);
  400. //Only if course is available
  401. $show_course_shortcut = null;
  402. $show_course_navigation_menu = null;
  403. if (!empty($this->course_id) && $this->user_is_logged_in) {
  404. if (api_get_setting('show_toolshortcuts') != 'false') {
  405. //Course toolbar
  406. $show_course_shortcut = CourseHome::show_navigation_tool_shortcuts();
  407. }
  408. if (api_get_setting('show_navigation_menu') != 'false') {
  409. //Course toolbar
  410. $show_course_navigation_menu = CourseHome::show_navigation_menu();
  411. }
  412. }
  413. $this->assign('show_course_shortcut', $show_course_shortcut);
  414. $this->assign('show_course_navigation_menu', $show_course_navigation_menu);
  415. }
  416. /**
  417. * Returns the sub-folder and filename for the given tpl file.
  418. * If template not found in overrides/ or custom template folder, the
  419. * default template will be used.
  420. * @param string $name
  421. *
  422. * @return string
  423. */
  424. public function get_template($name)
  425. {
  426. // Check if the tpl file is present in the main/template/overrides/ dir
  427. // Overrides is a special directory meant for temporary template
  428. // customization. It must be taken into account before anything else
  429. $file = api_get_path(SYS_CODE_PATH).'template/overrides/'.$name;
  430. if (is_readable($file)) {
  431. return 'overrides/'.$name;
  432. }
  433. // If a template folder has been manually defined, search for the right
  434. // file, and if not found, go for the same file in the default template
  435. if ($this->templateFolder != 'default') {
  436. // Avoid missing template error, use the default file.
  437. $file = api_get_path(SYS_CODE_PATH).'template/'.$this->templateFolder.'/'.$name;
  438. if (!file_exists($file)) {
  439. return 'default/'.$name;
  440. }
  441. }
  442. return $this->templateFolder.'/'.$name;
  443. }
  444. /**
  445. * Prepare the _c array for template files. The _c array contains
  446. * information about the current course
  447. * @return void
  448. */
  449. private function set_course_parameters()
  450. {
  451. //Setting course id
  452. $course = api_get_course_info();
  453. if (empty($course)) {
  454. $this->assign('course_is_set', false);
  455. return;
  456. }
  457. $this->assign('course_is_set', true);
  458. $this->course_id = $course['id'];
  459. $_c = [
  460. 'id' => $course['real_id'],
  461. 'code' => $course['code'],
  462. 'title' => $course['name'],
  463. 'visibility' => $course['visibility'],
  464. 'language' => $course['language'],
  465. 'directory' => $course['directory'],
  466. 'session_id' => api_get_session_id(),
  467. 'user_is_teacher' => api_is_course_admin(),
  468. 'student_view' => (!empty($_GET['isStudentView']) && $_GET['isStudentView'] == 'true'),
  469. ];
  470. $this->assign('course_code', $course['code']);
  471. $this->assign('_c', $_c);
  472. }
  473. /**
  474. * Prepare the _u array for template files. The _u array contains
  475. * information about the current user, as returned by
  476. * api_get_user_info()
  477. * @return void
  478. */
  479. private function set_user_parameters()
  480. {
  481. $user_info = [];
  482. $user_info['logged'] = 0;
  483. $this->user_is_logged_in = false;
  484. if (api_user_is_login()) {
  485. $user_info = api_get_user_info(api_get_user_id(), true);
  486. $user_info['logged'] = 1;
  487. $user_info['is_admin'] = 0;
  488. if (api_is_platform_admin()) {
  489. $user_info['is_admin'] = 1;
  490. }
  491. $user_info['messages_count'] = MessageManager::getCountNewMessages();
  492. $this->user_is_logged_in = true;
  493. }
  494. // Setting the $_u array that could be use in any template
  495. $this->assign('_u', $user_info);
  496. }
  497. /**
  498. * Get CSS themes sub-directory
  499. * @param string $theme
  500. * @return string with a trailing slash, e.g. 'themes/chamilo_red/'
  501. */
  502. public static function getThemeDir($theme)
  503. {
  504. $themeDir = 'themes/'.$theme.'/';
  505. $virtualTheme = api_get_configuration_value('virtual_css_theme_folder');
  506. if (!empty($virtualTheme)) {
  507. $virtualThemeList = api_get_themes(true);
  508. $isVirtualTheme = in_array($theme, array_keys($virtualThemeList));
  509. if ($isVirtualTheme) {
  510. $themeDir = 'themes/'.$virtualTheme.'/'.$theme.'/';
  511. }
  512. }
  513. return $themeDir;
  514. }
  515. /**
  516. * Get an array of all the web paths available (e.g. 'web' => 'https://my.chamilo.site/')
  517. * @return array
  518. */
  519. private function getWebPaths()
  520. {
  521. return [
  522. 'web' => api_get_path(WEB_PATH),
  523. 'web_url' => api_get_web_url(),
  524. 'web_relative' => api_get_path(REL_PATH),
  525. 'web_course' => api_get_path(WEB_COURSE_PATH),
  526. 'web_main' => api_get_path(WEB_CODE_PATH),
  527. 'web_css' => api_get_path(WEB_CSS_PATH),
  528. 'web_css_theme' => api_get_path(WEB_CSS_PATH).$this->themeDir,
  529. 'web_ajax' => api_get_path(WEB_AJAX_PATH),
  530. 'web_img' => api_get_path(WEB_IMG_PATH),
  531. 'web_plugin' => api_get_path(WEB_PLUGIN_PATH),
  532. 'web_lib' => api_get_path(WEB_LIBRARY_PATH),
  533. 'web_upload' => api_get_path(WEB_UPLOAD_PATH),
  534. 'web_self' => api_get_self(),
  535. 'web_query_vars' => api_htmlentities($_SERVER['QUERY_STRING']),
  536. 'web_self_query_vars' => api_htmlentities($_SERVER['REQUEST_URI']),
  537. 'web_cid_query' => api_get_cidreq(),
  538. ];
  539. }
  540. /**
  541. * Set system parameters from api_get_configuration into _s array for use in TPLs
  542. * Also fills the _p array from getWebPaths()
  543. * @uses self::getWebPaths()
  544. */
  545. public function set_system_parameters()
  546. {
  547. $this->theme = api_get_visual_theme();
  548. if (!empty($this->preview_theme)) {
  549. $this->theme = $this->preview_theme;
  550. }
  551. $this->themeDir = self::getThemeDir($this->theme);
  552. // Setting app paths/URLs
  553. $this->assign('_p', $this->getWebPaths());
  554. // Here we can add system parameters that can be use in any template
  555. $_s = [
  556. 'software_name' => api_get_configuration_value('software_name'),
  557. 'system_version' => api_get_configuration_value('system_version'),
  558. 'site_name' => api_get_setting('siteName'),
  559. 'institution' => api_get_setting('Institution'),
  560. 'date' => api_format_date('now', DATE_FORMAT_LONG),
  561. 'timezone' => api_get_timezone(),
  562. 'gamification_mode' => api_get_setting('gamification_mode')
  563. ];
  564. $this->assign('_s', $_s);
  565. }
  566. /**
  567. * Set theme, include mainstream CSS files
  568. * @return void
  569. * @see setCssCustomFiles() for additional CSS sheets
  570. */
  571. public function setCssFiles()
  572. {
  573. global $disable_js_and_css_files;
  574. $css = [];
  575. // Default CSS Bootstrap
  576. $bowerCSSFiles = [
  577. 'fontawesome/css/font-awesome.min.css',
  578. 'jquery-ui/themes/smoothness/theme.css',
  579. 'jquery-ui/themes/smoothness/jquery-ui.min.css',
  580. 'mediaelement/build/mediaelementplayer.min.css',
  581. 'jqueryui-timepicker-addon/dist/jquery-ui-timepicker-addon.min.css',
  582. 'bootstrap/dist/css/bootstrap.min.css',
  583. 'jquery.scrollbar/jquery.scrollbar.css',
  584. 'bootstrap-daterangepicker/daterangepicker.css',
  585. 'bootstrap-select/dist/css/bootstrap-select.min.css',
  586. 'select2/dist/css/select2.min.css',
  587. 'flag-icon-css/css/flag-icon.min.css'
  588. ];
  589. foreach ($bowerCSSFiles as $file) {
  590. $css[] = api_get_path(WEB_PUBLIC_PATH).'assets/'.$file;
  591. }
  592. $css[] = api_get_path(WEB_LIBRARY_PATH).'javascript/chosen/chosen.css';
  593. if (api_is_global_chat_enabled()) {
  594. $css[] = api_get_path(WEB_LIBRARY_PATH).'javascript/chat/css/chat.css';
  595. }
  596. $css_file_to_string = '';
  597. foreach ($css as $file) {
  598. $css_file_to_string .= api_get_css($file);
  599. }
  600. if (!$disable_js_and_css_files) {
  601. $this->assign('css_static_file_to_string', $css_file_to_string);
  602. }
  603. }
  604. /**
  605. *
  606. */
  607. public function setCSSEditor()
  608. {
  609. $cssEditor = api_get_cdn_path(api_get_path(WEB_CSS_PATH).'editor.css');
  610. if (is_file(api_get_path(SYS_CSS_PATH).$this->themeDir.'editor.css')) {
  611. $cssEditor = api_get_path(WEB_CSS_PATH).$this->themeDir.'editor.css';
  612. }
  613. $this->assign('cssEditor', $cssEditor);
  614. }
  615. /**
  616. * Prepare custom CSS to be added at the very end of the <head> section
  617. * @return void
  618. * @see setCssFiles() for the mainstream CSS files
  619. */
  620. public function setCssCustomFiles()
  621. {
  622. global $disable_js_and_css_files;
  623. // Base CSS
  624. $css[] = api_get_cdn_path(api_get_path(WEB_CSS_PATH).'base.css');
  625. if ($this->show_learnpath) {
  626. $css[] = api_get_cdn_path(api_get_path(WEB_CSS_PATH).'scorm.css');
  627. if (is_file(api_get_path(SYS_CSS_PATH).$this->themeDir.'learnpath.css')) {
  628. $css[] = api_get_path(WEB_CSS_PATH).$this->themeDir.'learnpath.css';
  629. }
  630. }
  631. $css[] = api_get_cdn_path(api_get_path(WEB_CSS_PATH).$this->themeDir.'default.css');
  632. $css_file_to_string = null;
  633. foreach ($css as $file) {
  634. $css_file_to_string .= api_get_css($file);
  635. }
  636. // @todo move this somewhere else. Special fix when using tablets in order to see the text near icons
  637. if (SHOW_TEXT_NEAR_ICONS == true) {
  638. //hack in order to fix the actions buttons
  639. $css_file_to_string .= '<style>
  640. .td_actions a {
  641. float:left;
  642. width:100%;
  643. }
  644. .forum_message_left a {
  645. float:left;
  646. width:100%;
  647. }
  648. </style>';
  649. }
  650. $navigator_info = api_get_navigator();
  651. if ($navigator_info['name'] == 'Internet Explorer' && $navigator_info['version'] == '6') {
  652. $css_file_to_string .= 'img, div { behavior: url('.api_get_path(WEB_LIBRARY_PATH).'javascript/iepngfix/iepngfix.htc) } '."\n";
  653. }
  654. if (!$disable_js_and_css_files) {
  655. $this->assign('css_custom_file_to_string', $css_file_to_string);
  656. $style_print = '';
  657. if (is_readable(api_get_path(SYS_CSS_PATH).$this->theme.'/print.css')) {
  658. $style_print = api_get_css(
  659. api_get_cdn_path(api_get_path(WEB_CSS_PATH).$this->theme.'/print.css'),
  660. 'print'
  661. );
  662. }
  663. $this->assign('css_style_print', $style_print);
  664. }
  665. // Logo
  666. $logo = return_logo($this->theme);
  667. $this->assign('logo', $logo);
  668. $this->assign('show_media_element', 1);
  669. }
  670. /**
  671. * Declare and define the template variable that will be used to load
  672. * javascript libraries in the header.
  673. */
  674. public function set_js_files()
  675. {
  676. global $disable_js_and_css_files, $htmlHeadXtra;
  677. $isoCode = api_get_language_isocode();
  678. $selectLink = 'bootstrap-select/dist/js/i18n/defaults-'.$isoCode.'_'.strtoupper($isoCode).'.min.js';
  679. if ($isoCode == 'en') {
  680. $selectLink = 'bootstrap-select/dist/js/i18n/defaults-'.$isoCode.'_US.min.js';
  681. }
  682. // JS files
  683. $js_files = [
  684. 'chosen/chosen.jquery.min.js'
  685. ];
  686. $viewBySession = api_get_setting('my_courses_view_by_session') === 'true';
  687. if (api_is_global_chat_enabled() || $viewBySession) {
  688. // Do not include the global chat in LP
  689. if ($this->show_learnpath == false &&
  690. $this->show_footer == true &&
  691. $this->hide_global_chat == false
  692. ) {
  693. $js_files[] = 'chat/js/chat.js';
  694. }
  695. }
  696. if (api_get_setting('accessibility_font_resize') == 'true') {
  697. $js_files[] = 'fontresize.js';
  698. }
  699. $js_file_to_string = '';
  700. $bowerJsFiles = [
  701. 'modernizr/modernizr.js',
  702. 'jquery/dist/jquery.min.js',
  703. 'bootstrap/dist/js/bootstrap.min.js',
  704. 'jquery-ui/jquery-ui.min.js',
  705. 'moment/min/moment-with-locales.js',
  706. 'bootstrap-daterangepicker/daterangepicker.js',
  707. 'jquery-timeago/jquery.timeago.js',
  708. 'mediaelement/build/mediaelement-and-player.min.js',
  709. 'jqueryui-timepicker-addon/dist/jquery-ui-timepicker-addon.min.js',
  710. 'image-map-resizer/js/imageMapResizer.min.js',
  711. 'jquery.scrollbar/jquery.scrollbar.min.js',
  712. 'readmore-js/readmore.min.js',
  713. 'bootstrap-select/dist/js/bootstrap-select.min.js',
  714. $selectLink,
  715. 'select2/dist/js/select2.min.js',
  716. "select2/dist/js/i18n/$isoCode.js"
  717. ];
  718. if (CHAMILO_LOAD_WYSIWYG == true) {
  719. $bowerJsFiles[] = 'ckeditor/ckeditor.js';
  720. }
  721. if (api_get_setting('include_asciimathml_script') == 'true') {
  722. $bowerJsFiles[] = 'MathJax/MathJax.js?config=TeX-MML-AM_HTMLorMML';
  723. }
  724. if ($isoCode != 'en') {
  725. $bowerJsFiles[] = 'jqueryui-timepicker-addon/dist/i18n/jquery-ui-timepicker-'.$isoCode.'.js';
  726. $bowerJsFiles[] = 'jquery-ui/ui/minified/i18n/datepicker-'.$isoCode.'.min.js';
  727. }
  728. foreach ($bowerJsFiles as $file) {
  729. $js_file_to_string .= '<script type="text/javascript" src="'.api_get_path(WEB_PUBLIC_PATH).'assets/'.$file.'"></script>'."\n";
  730. }
  731. foreach ($js_files as $file) {
  732. $js_file_to_string .= api_get_js($file);
  733. }
  734. // Loading email_editor js
  735. if (!api_is_anonymous() && api_get_setting('allow_email_editor') == 'true') {
  736. $template = $this->get_template('mail_editor/email_link.js.tpl');
  737. $js_file_to_string .= $this->fetch($template);
  738. }
  739. if (!$disable_js_and_css_files) {
  740. $this->assign('js_file_to_string', $js_file_to_string);
  741. $extra_headers = '<script>var _p = '.json_encode($this->getWebPaths(), JSON_PRETTY_PRINT).'</script>';
  742. //Adding jquery ui by default
  743. $extra_headers .= api_get_jquery_ui_js();
  744. //$extra_headers = '';
  745. if (isset($htmlHeadXtra) && $htmlHeadXtra) {
  746. foreach ($htmlHeadXtra as & $this_html_head) {
  747. $extra_headers .= $this_html_head."\n";
  748. }
  749. }
  750. $this->assign('extra_headers', $extra_headers);
  751. }
  752. }
  753. /**
  754. * Special function to declare last-minute JS libraries which depend on
  755. * other things to be declared first. In particular, it might be useful
  756. * under IE9 with compatibility mode, which for some reason is getting
  757. * upset when a variable is used in a function (even if not used yet)
  758. * when this variable hasn't been defined yet.
  759. */
  760. public function set_js_files_post()
  761. {
  762. global $disable_js_and_css_files;
  763. $js_files = [];
  764. if (api_is_global_chat_enabled()) {
  765. //Do not include the global chat in LP
  766. if ($this->show_learnpath == false && $this->show_footer == true && $this->hide_global_chat == false) {
  767. $js_files[] = 'chat/js/chat.js';
  768. }
  769. }
  770. $js_file_to_string = null;
  771. foreach ($js_files as $js_file) {
  772. $js_file_to_string .= api_get_js($js_file);
  773. }
  774. if (!$disable_js_and_css_files) {
  775. $this->assign('js_file_to_string_post', $js_file_to_string);
  776. }
  777. }
  778. /**
  779. * Set header parameters
  780. * @param bool $sendHeaders send headers
  781. */
  782. private function set_header_parameters($sendHeaders)
  783. {
  784. global $httpHeadXtra, $interbreadcrumb, $language_file, $_configuration, $this_section;
  785. $_course = api_get_course_info();
  786. $nameTools = $this->title;
  787. $navigation = return_navigation_array();
  788. $this->menu_navigation = $navigation['menu_navigation'];
  789. $this->assign('system_charset', api_get_system_encoding());
  790. if (isset($httpHeadXtra) && $httpHeadXtra) {
  791. foreach ($httpHeadXtra as & $thisHttpHead) {
  792. header($thisHttpHead);
  793. }
  794. }
  795. $this->assign(
  796. 'online_button',
  797. Display::return_icon('statusonline.png', null, [], ICON_SIZE_ATOM)
  798. );
  799. $this->assign(
  800. 'offline_button',
  801. Display::return_icon('statusoffline.png', null, [], ICON_SIZE_ATOM)
  802. );
  803. // Get language iso-code for this page - ignore errors
  804. $this->assign('document_language', api_get_language_isocode());
  805. $course_title = isset($_course['name']) ? $_course['name'] : null;
  806. $title_list = [];
  807. $title_list[] = api_get_setting('Institution');
  808. $title_list[] = api_get_setting('siteName');
  809. if (!empty($course_title)) {
  810. $title_list[] = $course_title;
  811. }
  812. if ($nameTools != '') {
  813. $title_list[] = $nameTools;
  814. }
  815. $title_string = '';
  816. for ($i = 0; $i < count($title_list); $i++) {
  817. $title_string .= $title_list[$i];
  818. if (isset($title_list[$i + 1])) {
  819. $item = trim($title_list[$i + 1]);
  820. if (!empty($item)) {
  821. $title_string .= ' - ';
  822. }
  823. }
  824. }
  825. $this->assign('title_string', $title_string);
  826. // Setting the theme and CSS files
  827. $this->setCssFiles();
  828. $this->set_js_files();
  829. $this->setCssCustomFiles();
  830. $browser = api_browser_support('check_browser');
  831. if ($browser[0] == 'Internet Explorer' && $browser[1] >= '11') {
  832. $browser_head = '<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9" />';
  833. $this->assign('browser_specific_head', $browser_head);
  834. }
  835. // Implementation of prefetch.
  836. // See http://cdn.chamilo.org/main/img/online.png for details
  837. $prefetch = '';
  838. if (!empty($_configuration['cdn_enable'])) {
  839. $prefetch .= '<meta http-equiv="x-dns-prefetch-control" content="on">';
  840. foreach ($_configuration['cdn'] as $host => $exts) {
  841. $prefetch .= '<link rel="dns-prefetch" href="'.$host.'">';
  842. }
  843. }
  844. $this->assign('prefetch', $prefetch);
  845. $this->assign('text_direction', api_get_text_direction());
  846. $this->assign('section_name', 'section-'.$this_section);
  847. // Default root chamilo favicon
  848. $favico = '<link rel="shortcut icon" href="'.api_get_path(WEB_PATH).'favicon.ico" type="image/x-icon" />';
  849. //Added to verify if in the current Chamilo Theme exist a favicon
  850. $favicoThemeUrl = api_get_path(SYS_CSS_PATH).$this->themeDir.'images/';
  851. //If exist pick the current chamilo theme favicon
  852. if (is_file($favicoThemeUrl.'favicon.ico')) {
  853. $favico = '<link rel="shortcut icon" href="'.api_get_path(WEB_CSS_PATH).$this->themeDir.'images/favicon.ico" type="image/x-icon" />';
  854. }
  855. if (api_is_multiple_url_enabled()) {
  856. $access_url_id = api_get_current_access_url_id();
  857. if ($access_url_id != -1) {
  858. $url_info = api_get_access_url($access_url_id);
  859. $url = api_remove_trailing_slash(
  860. preg_replace('/https?:\/\//i', '', $url_info['url'])
  861. );
  862. $clean_url = api_replace_dangerous_char($url);
  863. $clean_url = str_replace('/', '-', $clean_url);
  864. $clean_url .= '/';
  865. $homep = api_get_path(REL_PATH).'home/'.$clean_url; //homep for Home Path
  866. $icon_real_homep = api_get_path(SYS_APP_PATH).'home/'.$clean_url;
  867. //we create the new dir for the new sites
  868. if (is_file($icon_real_homep.'favicon.ico')) {
  869. $favico = '<link rel="shortcut icon" href="'.$homep.'favicon.ico" type="image/x-icon" />';
  870. }
  871. }
  872. }
  873. $this->assign('favico', $favico);
  874. $this->setHelp();
  875. //@todo move this in the template
  876. $rightFloatMenu = '';
  877. $iconBug = Display::return_icon(
  878. 'bug.png',
  879. get_lang('ReportABug'),
  880. [],
  881. ICON_SIZE_LARGE
  882. );
  883. if (api_get_setting('show_link_bug_notification') == 'true' && $this->user_is_logged_in) {
  884. $rightFloatMenu = '<div class="report">
  885. <a href="https://github.com/chamilo/chamilo-lms/wiki/How-to-report-issues" target="_blank">
  886. '.$iconBug.'
  887. </a>
  888. </div>';
  889. }
  890. if (api_get_setting('show_link_ticket_notification') == 'true' && $this->user_is_logged_in) {
  891. // by default is project_id = 1
  892. $iconTicket = Display::return_icon(
  893. 'help.png',
  894. get_lang('Ticket'),
  895. [],
  896. ICON_SIZE_LARGE
  897. );
  898. $courseInfo = api_get_course_info();
  899. $courseParams = '';
  900. if (!empty($courseInfo)) {
  901. $courseParams = api_get_cidreq();
  902. }
  903. $url = api_get_path(WEB_CODE_PATH).'ticket/tickets.php?project_id=1&'.$courseParams;
  904. $rightFloatMenu .= '<div class="help">
  905. <a href="'.$url.'" target="_blank">
  906. '.$iconTicket.'
  907. </a>
  908. </div>';
  909. }
  910. $this->assign('bug_notification', $rightFloatMenu);
  911. $resize = '';
  912. if (api_get_setting('accessibility_font_resize') == 'true') {
  913. $resize .= '<div class="resize_font">';
  914. $resize .= '<div class="btn-group">';
  915. $resize .= '<a title="'.get_lang('DecreaseFontSize').'" href="#" class="decrease_font btn btn-default"><em class="fa fa-font"></em></a>';
  916. $resize .= '<a title="'.get_lang('ResetFontSize').'" href="#" class="reset_font btn btn-default"><em class="fa fa-font"></em></a>';
  917. $resize .= '<a title="'.get_lang('IncreaseFontSize').'" href="#" class="increase_font btn btn-default"><em class="fa fa-font"></em></a>';
  918. $resize .= '</div>';
  919. $resize .= '</div>';
  920. }
  921. $this->assign('accessibility', $resize);
  922. // Preparing values for the menu
  923. // Logout link
  924. $hideLogout = api_get_setting('hide_logout_button');
  925. if ($hideLogout === 'true') {
  926. $this->assign('logout_link', null);
  927. } else {
  928. $this->assign('logout_link', api_get_path(WEB_PATH).'index.php?logout=logout&uid='.api_get_user_id());
  929. }
  930. // Profile link
  931. if (api_get_setting('allow_social_tool') == 'true') {
  932. $profile_url = api_get_path(WEB_CODE_PATH).'social/home.php';
  933. } else {
  934. $profile_url = api_get_path(WEB_CODE_PATH).'auth/profile.php';
  935. }
  936. $this->assign('profile_url', $profile_url);
  937. //Message link
  938. $message_link = null;
  939. $message_url = null;
  940. if (api_get_setting('allow_message_tool') == 'true') {
  941. $message_url = api_get_path(WEB_CODE_PATH).'messages/inbox.php';
  942. $message_link = '<a href="'.api_get_path(WEB_CODE_PATH).'messages/inbox.php">'.get_lang('Inbox').'</a>';
  943. }
  944. $this->assign('message_link', $message_link);
  945. $this->assign('message_url', $message_url);
  946. // Certificate Link
  947. $allow = api_get_configuration_value('hide_my_certificate_link');
  948. if ($allow === false) {
  949. $certificateUrl = api_get_path(WEB_CODE_PATH).'gradebook/my_certificates.php';
  950. $certificateLink = Display::url(
  951. get_lang('MyCertificates'),
  952. $certificateUrl
  953. );
  954. $this->assign('certificate_link', $certificateLink);
  955. $this->assign('certificate_url', $certificateUrl);
  956. }
  957. $institution = api_get_setting('Institution');
  958. $portal_name = empty($institution) ? api_get_setting('siteName') : $institution;
  959. $this->assign('portal_name', $portal_name);
  960. //Menu
  961. $menu = menuArray();
  962. $this->assign('menu', $menu);
  963. $breadcrumb = '';
  964. // Hide breadcrumb in LP
  965. if ($this->show_learnpath == false) {
  966. $breadcrumb = return_breadcrumb(
  967. $interbreadcrumb,
  968. $language_file,
  969. $nameTools
  970. );
  971. }
  972. $this->assign('breadcrumb', $breadcrumb);
  973. //Extra content
  974. $extra_header = null;
  975. if (!api_is_platform_admin()) {
  976. $extra_header = trim(api_get_setting('header_extra_content'));
  977. }
  978. $this->assign('header_extra_content', $extra_header);
  979. if ($sendHeaders) {
  980. header('Content-Type: text/html; charset='.api_get_system_encoding());
  981. header(
  982. 'X-Powered-By: '.$_configuration['software_name'].' '.substr($_configuration['system_version'], 0, 1)
  983. );
  984. self::addHTTPSecurityHeaders();
  985. $responseCode = $this->getResponseCode();
  986. if (!empty($responseCode)) {
  987. switch ($responseCode) {
  988. case '404':
  989. header("HTTP/1.0 404 Not Found");
  990. break;
  991. }
  992. }
  993. }
  994. $socialMeta = '';
  995. $metaTitle = api_get_setting('meta_title');
  996. if (!empty($metaTitle)) {
  997. $socialMeta .= '<meta name="twitter:card" content="summary" />'."\n";
  998. $metaSite = api_get_setting('meta_twitter_site');
  999. if (!empty($metaSite)) {
  1000. $socialMeta .= '<meta name="twitter:site" content="'.$metaSite.'" />'."\n";
  1001. $metaCreator = api_get_setting('meta_twitter_creator');
  1002. if (!empty($metaCreator)) {
  1003. $socialMeta .= '<meta name="twitter:creator" content="'.$metaCreator.'" />'."\n";
  1004. }
  1005. }
  1006. // The user badge page emits its own meta tags, so if this is
  1007. // enabled, ignore the global ones
  1008. $userId = isset($_GET['user']) ? intval($_GET['user']) : 0;
  1009. $skillId = isset($_GET['skill']) ? intval($_GET['skill']) : 0;
  1010. if (!$userId && !$skillId) {
  1011. // no combination of user and skill ID has been defined,
  1012. // so print the normal OpenGraph meta tags
  1013. $socialMeta .= '<meta property="og:title" content="'.$metaTitle.'" />'."\n";
  1014. $socialMeta .= '<meta property="og:url" content="'.api_get_path(WEB_PATH).'" />'."\n";
  1015. $metaDescription = api_get_setting('meta_description');
  1016. if (!empty($metaDescription)) {
  1017. $socialMeta .= '<meta property="og:description" content="'.$metaDescription.'" />'."\n";
  1018. }
  1019. $metaImage = api_get_setting('meta_image_path');
  1020. if (!empty($metaImage)) {
  1021. if (is_file(api_get_path(SYS_PATH).$metaImage)) {
  1022. $path = api_get_path(WEB_PATH).$metaImage;
  1023. $socialMeta .= '<meta property="og:image" content="'.$path.'" />'."\n";
  1024. }
  1025. }
  1026. }
  1027. }
  1028. $this->assign('social_meta', $socialMeta);
  1029. }
  1030. /**
  1031. * Set footer parameters
  1032. */
  1033. private function set_footer_parameters()
  1034. {
  1035. if (api_get_setting('show_administrator_data') === 'true') {
  1036. $firstName = api_get_setting('administratorName');
  1037. $lastName = api_get_setting('administratorSurname');
  1038. if (!empty($firstName) && !empty($lastName)) {
  1039. $name = api_get_person_name($firstName, $lastName);
  1040. } else {
  1041. $name = $lastName;
  1042. if (empty($lastName)) {
  1043. $name = $firstName;
  1044. }
  1045. }
  1046. $adminName = '';
  1047. // Administrator name
  1048. if (!empty($name)) {
  1049. $adminName = get_lang('Manager').' : '.
  1050. Display::encrypted_mailto_link(
  1051. api_get_setting('emailAdministrator'),
  1052. $name
  1053. );
  1054. }
  1055. $this->assign('administrator_name', $adminName);
  1056. }
  1057. // Loading footer extra content
  1058. if (!api_is_platform_admin()) {
  1059. $extra_footer = trim(api_get_setting('footer_extra_content'));
  1060. if (!empty($extra_footer)) {
  1061. $this->assign('footer_extra_content', $extra_footer);
  1062. }
  1063. }
  1064. // Tutor name
  1065. if (api_get_setting('show_tutor_data') == 'true') {
  1066. // Course manager
  1067. $courseId = api_get_course_int_id();
  1068. $id_session = api_get_session_id();
  1069. if (!empty($courseId)) {
  1070. $tutor_data = '';
  1071. if ($id_session != 0) {
  1072. $coachs_email = CourseManager::get_email_of_tutor_to_session(
  1073. $id_session,
  1074. $courseId
  1075. );
  1076. $email_link = [];
  1077. foreach ($coachs_email as $coach) {
  1078. $email_link[] = Display::encrypted_mailto_link($coach['email'], $coach['complete_name']);
  1079. }
  1080. if (count($coachs_email) > 1) {
  1081. $tutor_data .= get_lang('Coachs').' : ';
  1082. $tutor_data .= array_to_string($email_link, CourseManager::USER_SEPARATOR);
  1083. } elseif (count($coachs_email) == 1) {
  1084. $tutor_data .= get_lang('Coach').' : ';
  1085. $tutor_data .= array_to_string($email_link, CourseManager::USER_SEPARATOR);
  1086. } elseif (count($coachs_email) == 0) {
  1087. $tutor_data .= '';
  1088. }
  1089. }
  1090. $this->assign('session_teachers', $tutor_data);
  1091. }
  1092. }
  1093. if (api_get_setting('show_teacher_data') == 'true') {
  1094. // course manager
  1095. $courseId = api_get_course_int_id();
  1096. if (!empty($courseId)) {
  1097. $teacher_data = '';
  1098. $mail = CourseManager::get_emails_of_tutors_to_course($courseId);
  1099. if (!empty($mail)) {
  1100. $teachers_parsed = [];
  1101. foreach ($mail as $value) {
  1102. foreach ($value as $email => $name) {
  1103. $teachers_parsed[] = Display::encrypted_mailto_link($email, $name);
  1104. }
  1105. }
  1106. $label = get_lang('Teacher');
  1107. if (count($mail) > 1) {
  1108. $label = get_lang('Teachers');
  1109. }
  1110. $teacher_data .= $label.' : '.array_to_string($teachers_parsed, CourseManager::USER_SEPARATOR);
  1111. }
  1112. $this->assign('teachers', $teacher_data);
  1113. }
  1114. }
  1115. }
  1116. /**
  1117. * Show header template.
  1118. */
  1119. public function show_header_template()
  1120. {
  1121. $tpl = $this->get_template('layout/show_header.tpl');
  1122. $this->display($tpl);
  1123. }
  1124. /**
  1125. * Show footer template.
  1126. */
  1127. public function show_footer_template()
  1128. {
  1129. $tpl = $this->get_template('layout/show_footer.tpl');
  1130. $this->display($tpl);
  1131. }
  1132. /**
  1133. * Show footer js template.
  1134. */
  1135. public function show_footer_js_template()
  1136. {
  1137. $tpl = $this->get_template('layout/footer.js.tpl');
  1138. $this->display($tpl);
  1139. }
  1140. /**
  1141. * Sets the plugin content in a template variable
  1142. * @param string $pluginRegion
  1143. * @return null
  1144. */
  1145. public function set_plugin_region($pluginRegion)
  1146. {
  1147. if (!empty($pluginRegion)) {
  1148. $regionContent = $this->plugin->load_region(
  1149. $pluginRegion,
  1150. $this,
  1151. $this->force_plugin_load
  1152. );
  1153. $pluginList = $this->plugin->get_installed_plugins();
  1154. foreach ($pluginList as $plugin_name) {
  1155. // The plugin_info variable is available inside the plugin index
  1156. $pluginInfo = $this->plugin->getPluginInfo($plugin_name);
  1157. if (isset($pluginInfo['is_course_plugin']) && $pluginInfo['is_course_plugin']) {
  1158. $courseInfo = api_get_course_info();
  1159. if (!empty($courseInfo)) {
  1160. if (isset($pluginInfo['obj']) && $pluginInfo['obj'] instanceof Plugin) {
  1161. /** @var Plugin $plugin */
  1162. $plugin = $pluginInfo['obj'];
  1163. $regionContent .= $plugin->renderRegion($pluginRegion);
  1164. }
  1165. }
  1166. } else {
  1167. continue;
  1168. }
  1169. }
  1170. if (!empty($regionContent)) {
  1171. $this->assign('plugin_'.$pluginRegion, $regionContent);
  1172. } else {
  1173. $this->assign('plugin_'.$pluginRegion, null);
  1174. }
  1175. }
  1176. return null;
  1177. }
  1178. /**
  1179. * @param string $template
  1180. * @return string
  1181. */
  1182. public function fetch($template = null)
  1183. {
  1184. $template = $this->twig->loadTemplate($template);
  1185. return $template->render($this->params);
  1186. }
  1187. /**
  1188. * @param string $variable
  1189. * @param mixed $value
  1190. */
  1191. public function assign($variable, $value = '')
  1192. {
  1193. $this->params[$variable] = $value;
  1194. }
  1195. /**
  1196. * Render the template
  1197. * @param string $template The template path
  1198. * @param boolean $clearFlashMessages Clear the $_SESSION variables for flash messages
  1199. */
  1200. public function display($template, $clearFlashMessages = true)
  1201. {
  1202. $this->assign('flash_messages', Display::getFlashToString());
  1203. if ($clearFlashMessages) {
  1204. Display::cleanFlashMessages();
  1205. }
  1206. echo $this->twig->render($template, $this->params);
  1207. }
  1208. /**
  1209. * Adds a body class for login pages
  1210. */
  1211. public function setLoginBodyClass()
  1212. {
  1213. $this->assign('login_class', 'section-login');
  1214. }
  1215. /**
  1216. * The theme that will be used if the database is not working.
  1217. * @return string
  1218. */
  1219. public static function getThemeFallback()
  1220. {
  1221. $theme = api_get_configuration_value('theme_fallback');
  1222. if (empty($theme)) {
  1223. $theme = 'chamilo';
  1224. }
  1225. return $theme;
  1226. }
  1227. /**
  1228. * @param bool|true $setLoginForm
  1229. */
  1230. public function setLoginForm($setLoginForm = true)
  1231. {
  1232. global $loginFailed;
  1233. $userId = api_get_user_id();
  1234. if (!($userId) || api_is_anonymous($userId)) {
  1235. // Only display if the user isn't logged in.
  1236. $this->assign(
  1237. 'login_language_form',
  1238. api_display_language_form(true, true)
  1239. );
  1240. if ($setLoginForm) {
  1241. $this->assign('login_form', $this->displayLoginForm());
  1242. if ($loginFailed) {
  1243. $this->assign('login_failed', $this::handleLoginFailed());
  1244. }
  1245. }
  1246. }
  1247. }
  1248. /**
  1249. * @return string
  1250. */
  1251. public function handleLoginFailed()
  1252. {
  1253. $message = get_lang('InvalidId');
  1254. if (!isset($_GET['error'])) {
  1255. if (api_is_self_registration_allowed()) {
  1256. $message = get_lang('InvalidForSelfRegistration');
  1257. }
  1258. } else {
  1259. switch ($_GET['error']) {
  1260. case '':
  1261. if (api_is_self_registration_allowed()) {
  1262. $message = get_lang('InvalidForSelfRegistration');
  1263. }
  1264. break;
  1265. case 'account_expired':
  1266. $message = get_lang('AccountExpired');
  1267. break;
  1268. case 'account_inactive':
  1269. $message = get_lang('AccountInactive');
  1270. if (api_get_setting('allow_registration') === 'confirmation') {
  1271. $message = get_lang('AccountNotConfirmed').PHP_EOL;
  1272. $message .= Display::url(
  1273. get_lang('ReSendConfirmationMail'),
  1274. api_get_path(WEB_PATH).'main/auth/resend_confirmation_mail.php',
  1275. ['class' => 'alert-link']
  1276. );
  1277. }
  1278. break;
  1279. case 'user_password_incorrect':
  1280. $message = get_lang('InvalidId');
  1281. break;
  1282. case 'access_url_inactive':
  1283. $message = get_lang('AccountURLInactive');
  1284. break;
  1285. case 'wrong_captcha':
  1286. $message = get_lang('TheTextYouEnteredDoesNotMatchThePicture');
  1287. break;
  1288. case 'blocked_by_captcha':
  1289. $message = get_lang('AccountBlockedByCaptcha');
  1290. break;
  1291. case 'multiple_connection_not_allowed':
  1292. $message = get_lang('MultipleConnectionsAreNotAllow');
  1293. break;
  1294. case 'unrecognize_sso_origin':
  1295. //$message = get_lang('SSOError');
  1296. break;
  1297. }
  1298. }
  1299. return Display::return_message($message, 'error', false);
  1300. }
  1301. /**
  1302. * @return string
  1303. */
  1304. public function displayLoginForm()
  1305. {
  1306. $form = new FormValidator(
  1307. 'form-login',
  1308. 'POST',
  1309. null,
  1310. null,
  1311. null,
  1312. FormValidator::LAYOUT_BOX_NO_LABEL
  1313. );
  1314. $params = [
  1315. 'id' => 'login',
  1316. 'autofocus' => 'autofocus',
  1317. 'icon' => 'user fa-fw',
  1318. 'placeholder' => get_lang('UserName'),
  1319. ];
  1320. $browserAutoCapitalize= false;
  1321. // Avoid showing the autocapitalize option if the browser doesn't
  1322. // support it: this attribute is against the HTML5 standard
  1323. if (api_browser_support('autocapitalize')) {
  1324. $browserAutoCapitalize = false;
  1325. $params['autocapitalize'] = 'none';
  1326. }
  1327. $form->addText(
  1328. 'login',
  1329. get_lang('UserName'),
  1330. true,
  1331. $params
  1332. );
  1333. $params = [
  1334. 'id' => 'password',
  1335. 'icon' => 'lock fa-fw',
  1336. 'placeholder' => get_lang('Pass'),
  1337. ];
  1338. if ($browserAutoCapitalize) {
  1339. $params['autocapitalize'] = 'none';
  1340. }
  1341. $form->addElement(
  1342. 'password',
  1343. 'password',
  1344. get_lang('Pass'),
  1345. $params
  1346. );
  1347. // Captcha
  1348. $captcha = api_get_setting('allow_captcha');
  1349. $allowCaptcha = $captcha === 'true';
  1350. if ($allowCaptcha) {
  1351. $useCaptcha = isset($_SESSION['loginFailed']) ? $_SESSION['loginFailed'] : null;
  1352. if ($useCaptcha) {
  1353. $ajax = api_get_path(WEB_AJAX_PATH).'form.ajax.php?a=get_captcha';
  1354. $options = [
  1355. 'width' => 250,
  1356. 'height' => 90,
  1357. 'callback' => $ajax.'&var='.basename(__FILE__, '.php'),
  1358. 'sessionVar' => basename(__FILE__, '.php'),
  1359. 'imageOptions' => [
  1360. 'font_size' => 20,
  1361. 'font_path' => api_get_path(SYS_FONTS_PATH).'opensans/',
  1362. 'font_file' => 'OpenSans-Regular.ttf',
  1363. //'output' => 'gif'
  1364. ]
  1365. ];
  1366. // Minimum options using all defaults (including defaults for Image_Text):
  1367. //$options = array('callback' => 'qfcaptcha_image.php');
  1368. $captcha_question = $form->addElement('CAPTCHA_Image', 'captcha_question', '', $options);
  1369. $form->addHtml(get_lang('ClickOnTheImageForANewOne'));
  1370. $form->addElement(
  1371. 'text',
  1372. 'captcha',
  1373. get_lang('EnterTheLettersYouSee')
  1374. );
  1375. $form->addRule(
  1376. 'captcha',
  1377. get_lang('EnterTheCharactersYouReadInTheImage'),
  1378. 'required',
  1379. null,
  1380. 'client'
  1381. );
  1382. $form->addRule(
  1383. 'captcha',
  1384. get_lang('TheTextYouEnteredDoesNotMatchThePicture'),
  1385. 'CAPTCHA',
  1386. $captcha_question
  1387. );
  1388. }
  1389. }
  1390. $form->addButton(
  1391. 'submitAuth',
  1392. get_lang('LoginEnter'),
  1393. null,
  1394. 'primary',
  1395. null,
  1396. 'btn-block'
  1397. );
  1398. $html = $form->returnForm();
  1399. if (api_get_setting('openid_authentication') == 'true') {
  1400. include_once api_get_path(SYS_CODE_PATH).'auth/openid/login.php';
  1401. $html .= '<div>'.openid_form().'</div>';
  1402. }
  1403. return $html;
  1404. }
  1405. /**
  1406. * Set administrator variables
  1407. */
  1408. private function setAdministratorParams()
  1409. {
  1410. $_admin = [
  1411. 'email' => api_get_setting('emailAdministrator'),
  1412. 'surname' => api_get_setting('administratorSurname'),
  1413. 'name' => api_get_setting('administratorName'),
  1414. 'telephone' => api_get_setting('administratorTelephone')
  1415. ];
  1416. $this->assign('_admin', $_admin);
  1417. }
  1418. /**
  1419. * Manage specific HTTP headers security
  1420. * @return void (prints headers directly)
  1421. */
  1422. private function addHTTPSecurityHeaders()
  1423. {
  1424. // Implementation of HTTP headers security, as suggested and checked
  1425. // by https://securityheaders.io/
  1426. // Enable these settings in configuration.php to use them on your site
  1427. // Strict-Transport-Security
  1428. $setting = api_get_configuration_value('security_strict_transport');
  1429. if (!empty($setting)) {
  1430. header('Strict-Transport-Security: '.$setting);
  1431. }
  1432. // Content-Security-Policy
  1433. $setting = api_get_configuration_value('security_content_policy');
  1434. if (!empty($setting)) {
  1435. header('Content-Security-Policy: '.$setting);
  1436. }
  1437. $setting = api_get_configuration_value('security_content_policy_report_only');
  1438. if (!empty($setting)) {
  1439. header('Content-Security-Policy-Report-Only: '.$setting);
  1440. }
  1441. // Public-Key-Pins
  1442. $setting = api_get_configuration_value('security_public_key_pins');
  1443. if (!empty($setting)) {
  1444. header('Public-Key-Pins: '.$setting);
  1445. }
  1446. $setting = api_get_configuration_value('security_public_key_pins_report_only');
  1447. if (!empty($setting)) {
  1448. header('Public-Key-Pins-Report-Only: '.$setting);
  1449. }
  1450. // X-Frame-Options
  1451. $setting = api_get_configuration_value('security_x_frame_options');
  1452. if (!empty($setting)) {
  1453. header('X-Frame-Options: '.$setting);
  1454. }
  1455. // X-XSS-Protection
  1456. $setting = api_get_configuration_value('security_xss_protection');
  1457. if (!empty($setting)) {
  1458. header('X-XSS-Protection: '.$setting);
  1459. }
  1460. // X-Content-Type-Options
  1461. $setting = api_get_configuration_value('security_x_content_type_options');
  1462. if (!empty($setting)) {
  1463. header('X-Content-Type-Options: '.$setting);
  1464. }
  1465. // Referrer-Policy
  1466. $setting = api_get_configuration_value('security_referrer_policy');
  1467. if (!empty($setting)) {
  1468. header('Referrer-Policy: '.$setting);
  1469. }
  1470. // end of HTTP headers security block
  1471. }
  1472. /**
  1473. * Returns the tutors names for the current course in session
  1474. * Function to use in Twig templates
  1475. * @return string
  1476. */
  1477. public static function returnTutorsNames()
  1478. {
  1479. $em = Database::getManager();
  1480. $tutors = $em
  1481. ->createQuery('
  1482. SELECT u FROM ChamiloUserBundle:User u
  1483. INNER JOIN ChamiloCoreBundle:SessionRelCourseRelUser scu WITH u.id = scu.user
  1484. WHERE scu.status = :teacher_status AND scu.session = :session AND scu.course = :course
  1485. ')
  1486. ->setParameters([
  1487. 'teacher_status' => SessionRelCourseRelUser::STATUS_COURSE_COACH,
  1488. 'session' => api_get_session_id(),
  1489. 'course' => api_get_course_int_id()
  1490. ])
  1491. ->getResult();
  1492. $names = [];
  1493. /** @var User $tutor */
  1494. foreach ($tutors as $tutor) {
  1495. $names[] = $tutor->getCompleteName();
  1496. }
  1497. return implode(CourseManager::USER_SEPARATOR, $names);
  1498. }
  1499. /**s
  1500. * Returns the teachers name for the current course
  1501. * Function to use in Twig templates
  1502. * @return string
  1503. */
  1504. public static function returnTeachersNames()
  1505. {
  1506. $em = Database::getManager();
  1507. $teachers = $em
  1508. ->createQuery('
  1509. SELECT u FROM ChamiloUserBundle:User u
  1510. INNER JOIN ChamiloCoreBundle:CourseRelUser cu WITH u.id = cu.user
  1511. WHERE cu.status = :teacher_status AND cu.course = :course
  1512. ')
  1513. ->setParameters([
  1514. 'teacher_status' => User::COURSE_MANAGER,
  1515. 'course' => api_get_course_int_id()
  1516. ])
  1517. ->getResult();
  1518. $names = [];
  1519. /** @var User $teacher */
  1520. foreach ($teachers as $teacher) {
  1521. $names[] = $teacher->getCompleteName();
  1522. }
  1523. return implode(CourseManager::USER_SEPARATOR, $names);
  1524. }
  1525. /**
  1526. * @param int $code
  1527. */
  1528. public function setResponseCode($code)
  1529. {
  1530. $this->responseCode = $code;
  1531. }
  1532. /**
  1533. * @param string $code
  1534. */
  1535. public function getResponseCode()
  1536. {
  1537. return $this->responseCode;
  1538. }
  1539. }