template.lib.php 50 KB

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