template.lib.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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_url' => api_get_web_url(),
  477. 'web' => api_get_path(WEB_PATH),
  478. 'web_relative' => api_get_path(REL_PATH),
  479. 'web_course' => api_get_path(WEB_COURSE_PATH),
  480. 'web_main' => api_get_path(WEB_CODE_PATH),
  481. 'web_css' => api_get_path(WEB_CSS_PATH),
  482. 'web_css_theme' => api_get_path(WEB_CSS_PATH).$this->themeDir,
  483. 'web_ajax' => api_get_path(WEB_AJAX_PATH),
  484. 'web_img' => api_get_path(WEB_IMG_PATH),
  485. 'web_plugin' => api_get_path(WEB_PLUGIN_PATH),
  486. 'web_lib' => api_get_path(WEB_LIBRARY_PATH),
  487. 'web_upload' => api_get_path(WEB_UPLOAD_PATH),
  488. 'web_self' => api_get_self(),
  489. 'web_query_vars' => api_htmlentities($_SERVER['QUERY_STRING']),
  490. 'web_self_query_vars' => api_htmlentities($_SERVER['REQUEST_URI']),
  491. 'web_cid_query' => api_get_cidreq(),
  492. 'basename' => basename(api_get_self())
  493. );
  494. $this->assign('_p', $_p);
  495. // Here we can add system parameters that can be use in any template
  496. $_s = array(
  497. 'software_name' => api_get_configuration_value('software_name'),
  498. 'system_version' => api_get_configuration_value('system_version'),
  499. 'site_name' => api_get_setting('siteName'),
  500. 'institution' => api_get_setting('Institution'),
  501. 'date' => api_format_date('now', DATE_FORMAT_LONG),
  502. 'timezone' => api_get_timezone(),
  503. 'gamification_mode' => api_get_setting('gamification_mode')
  504. );
  505. $this->assign('_s', $_s);
  506. }
  507. /**
  508. * Set theme, include mainstream CSS files
  509. * @return void
  510. * @see setCssCustomFiles() for additional CSS sheets
  511. */
  512. public function setCssFiles()
  513. {
  514. global $disable_js_and_css_files;
  515. $css = array();
  516. $this->theme = api_get_visual_theme();
  517. if (!empty($this->preview_theme)) {
  518. $this->theme = $this->preview_theme;
  519. }
  520. // Default CSS Bootstrap
  521. $bowerCSSFiles = [
  522. 'fontawesome/css/font-awesome.min.css',
  523. 'jquery-ui/themes/smoothness/theme.css',
  524. 'jquery-ui/themes/smoothness/jquery-ui.min.css',
  525. 'mediaelement/build/mediaelementplayer.min.css',
  526. 'jqueryui-timepicker-addon/dist/jquery-ui-timepicker-addon.min.css',
  527. 'bootstrap/dist/css/bootstrap.min.css',
  528. 'jquery.scrollbar/jquery.scrollbar.css',
  529. 'bootstrap-daterangepicker/daterangepicker.css',
  530. 'bootstrap-select/dist/css/bootstrap-select.min.css',
  531. 'select2/dist/css/select2.min.css'
  532. ];
  533. foreach ($bowerCSSFiles as $file) {
  534. $css[] = api_get_path(WEB_PATH).'web/assets/'.$file;
  535. }
  536. $css[] = api_get_path(WEB_LIBRARY_PATH).'javascript/chosen/chosen.css';
  537. if (api_is_global_chat_enabled()) {
  538. $css[] = api_get_path(WEB_LIBRARY_PATH).'javascript/chat/css/chat.css';
  539. }
  540. $css_file_to_string = '';
  541. foreach ($css as $file) {
  542. $css_file_to_string .= api_get_css($file);
  543. }
  544. if (!$disable_js_and_css_files) {
  545. $this->assign('css_static_file_to_string', $css_file_to_string);
  546. }
  547. }
  548. /**
  549. *
  550. */
  551. public function setCSSEditor()
  552. {
  553. $cssEditor = api_get_cdn_path(api_get_path(WEB_CSS_PATH).'editor.css');
  554. if (is_file(api_get_path(SYS_CSS_PATH).$this->themeDir.'editor.css')) {
  555. $cssEditor = api_get_path(WEB_CSS_PATH).$this->themeDir.'editor.css';
  556. }
  557. $this->assign('cssEditor', $cssEditor);
  558. }
  559. /**
  560. * Prepare custom CSS to be added at the very end of the <head> section
  561. * @return void
  562. * @see setCssFiles() for the mainstream CSS files
  563. */
  564. public function setCssCustomFiles()
  565. {
  566. global $disable_js_and_css_files;
  567. // Base CSS
  568. $css[] = api_get_cdn_path(api_get_path(WEB_CSS_PATH).'base.css');
  569. if ($this->show_learnpath) {
  570. $css[] = api_get_cdn_path(api_get_path(WEB_CSS_PATH).'scorm.css');
  571. if (is_file(api_get_path(SYS_CSS_PATH).$this->themeDir.'learnpath.css')) {
  572. $css[] = api_get_path(WEB_CSS_PATH).$this->themeDir.'learnpath.css';
  573. }
  574. }
  575. if (is_file(api_get_path(SYS_CSS_PATH).$this->themeDir.'editor.css')) {
  576. $css[] = api_get_path(WEB_CSS_PATH).$this->themeDir.'editor.css';
  577. } else {
  578. $css[] = api_get_cdn_path(api_get_path(WEB_CSS_PATH).'editor.css');
  579. }
  580. $css[] = api_get_cdn_path(api_get_path(WEB_CSS_PATH).$this->themeDir.'default.css');
  581. $css_file_to_string = null;
  582. foreach ($css as $file) {
  583. $css_file_to_string .= api_get_css($file);
  584. }
  585. // @todo move this somewhere else. Special fix when using tablets in order to see the text near icons
  586. if (SHOW_TEXT_NEAR_ICONS == true) {
  587. //hack in order to fix the actions buttons
  588. $css_file_to_string .= '<style>
  589. .td_actions a {
  590. float:left;
  591. width:100%;
  592. }
  593. .forum_message_left a {
  594. float:left;
  595. width:100%;
  596. }
  597. </style>';
  598. }
  599. $navigator_info = api_get_navigator();
  600. if ($navigator_info['name'] == 'Internet Explorer' && $navigator_info['version'] == '6') {
  601. $css_file_to_string .= 'img, div { behavior: url('.api_get_path(WEB_LIBRARY_PATH).'javascript/iepngfix/iepngfix.htc) } '."\n";
  602. }
  603. if (!$disable_js_and_css_files) {
  604. $this->assign('css_custom_file_to_string', $css_file_to_string);
  605. $style_print = '';
  606. if (is_readable(api_get_path(SYS_CSS_PATH).$this->theme.'/print.css')) {
  607. $style_print = api_get_css(
  608. api_get_cdn_path(api_get_path(WEB_CSS_PATH).$this->theme.'/print.css'),
  609. 'print'
  610. );
  611. }
  612. $this->assign('css_style_print', $style_print);
  613. }
  614. // Logo
  615. $logo = return_logo($this->theme);
  616. $this->assign('logo', $logo);
  617. $this->assign('show_media_element', 1);
  618. }
  619. /**
  620. * Declare and define the template variable that will be used to load
  621. * javascript libraries in the header.
  622. */
  623. public function set_js_files()
  624. {
  625. global $disable_js_and_css_files, $htmlHeadXtra;
  626. $isoCode = api_get_language_isocode();
  627. $selectLink = 'bootstrap-select/dist/js/i18n/defaults-'.$isoCode.'_'.strtoupper($isoCode).'.min.js';
  628. if ($isoCode == 'en') {
  629. $selectLink = 'bootstrap-select/dist/js/i18n/defaults-'.$isoCode.'_US.min.js';
  630. }
  631. // JS files
  632. $js_files = array(
  633. 'chosen/chosen.jquery.min.js'
  634. );
  635. $viewBySession = api_get_setting('my_courses_view_by_session') === 'true';
  636. if (api_is_global_chat_enabled() || $viewBySession) {
  637. // Do not include the global chat in LP
  638. if ($this->show_learnpath == false &&
  639. $this->show_footer == true &&
  640. $this->hide_global_chat == false
  641. ) {
  642. $js_files[] = 'chat/js/chat.js';
  643. }
  644. }
  645. if (api_get_setting('accessibility_font_resize') == 'true') {
  646. $js_files[] = 'fontresize.js';
  647. }
  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. 'select2/dist/js/select2.min.js',
  665. "select2/dist/js/i18n/$isoCode.js"
  666. ];
  667. if (CHAMILO_LOAD_WYSIWYG == true) {
  668. $bowerJsFiles[] = 'ckeditor/ckeditor.js';
  669. }
  670. if (api_get_setting('include_asciimathml_script') == 'true') {
  671. $bowerJsFiles[] = 'MathJax/MathJax.js?config=TeX-AMS_HTML';
  672. }
  673. if ($isoCode != 'en') {
  674. $bowerJsFiles[] = 'jqueryui-timepicker-addon/dist/i18n/jquery-ui-timepicker-'.$isoCode.'.js';
  675. $bowerJsFiles[] = 'jquery-ui/ui/minified/i18n/datepicker-'.$isoCode.'.min.js';
  676. }
  677. foreach ($bowerJsFiles as $file) {
  678. $js_file_to_string .= '<script type="text/javascript" src="'.api_get_path(WEB_PATH).'web/assets/'.$file.'"></script>'."\n";
  679. }
  680. foreach ($js_files as $file) {
  681. $js_file_to_string .= api_get_js($file);
  682. }
  683. // Loading email_editor js
  684. if (!api_is_anonymous() && api_get_setting('allow_email_editor') == 'true') {
  685. $template = $this->get_template('mail_editor/email_link.js.tpl');
  686. $js_file_to_string .= $this->fetch($template);
  687. }
  688. if (!$disable_js_and_css_files) {
  689. $this->assign('js_file_to_string', $js_file_to_string);
  690. //Adding jquery ui by default
  691. $extra_headers = api_get_jquery_ui_js();
  692. //$extra_headers = '';
  693. if (isset($htmlHeadXtra) && $htmlHeadXtra) {
  694. foreach ($htmlHeadXtra as & $this_html_head) {
  695. $extra_headers .= $this_html_head."\n";
  696. }
  697. }
  698. $this->assign('extra_headers', $extra_headers);
  699. }
  700. }
  701. /**
  702. * Special function to declare last-minute JS libraries which depend on
  703. * other things to be declared first. In particular, it might be useful
  704. * under IE9 with compatibility mode, which for some reason is getting
  705. * upset when a variable is used in a function (even if not used yet)
  706. * when this variable hasn't been defined yet.
  707. */
  708. public function set_js_files_post()
  709. {
  710. global $disable_js_and_css_files;
  711. $js_files = array();
  712. if (api_is_global_chat_enabled()) {
  713. //Do not include the global chat in LP
  714. if ($this->show_learnpath == false && $this->show_footer == true && $this->hide_global_chat == false) {
  715. $js_files[] = 'chat/js/chat.js';
  716. }
  717. }
  718. $js_file_to_string = null;
  719. foreach ($js_files as $js_file) {
  720. $js_file_to_string .= api_get_js($js_file);
  721. }
  722. if (!$disable_js_and_css_files) {
  723. $this->assign('js_file_to_string_post', $js_file_to_string);
  724. }
  725. }
  726. /**
  727. * Set header parameters
  728. * @param bool $sendHeaders send headers
  729. */
  730. private function set_header_parameters($sendHeaders)
  731. {
  732. global $httpHeadXtra, $interbreadcrumb, $language_file, $_configuration, $this_section;
  733. $_course = api_get_course_info();
  734. $help = $this->help;
  735. $nameTools = $this->title;
  736. $navigation = return_navigation_array();
  737. $this->menu_navigation = $navigation['menu_navigation'];
  738. $locale = api_get_language_isocode();
  739. $categories = Database::getManager()->getRepository('ChamiloFaqBundle:Category')->retrieveActive();
  740. $faqCategories = [];
  741. if ($categories) {
  742. /** @var \Chamilo\FaqBundle\Entity\Category $cat */
  743. foreach ($categories as $cat) {
  744. $sql = "SELECT * FROM faq_category_translation
  745. WHERE locale = '$locale' AND translatable_id = ".$cat->getId();
  746. $result = Database::query($sql);
  747. $category = Database::fetch_array($result, 'ASSOC');
  748. $faqCategories[] = $category;
  749. }
  750. }
  751. $this->assign('faq_categories', $faqCategories);
  752. $this->assign('system_charset', api_get_system_encoding());
  753. if (isset($httpHeadXtra) && $httpHeadXtra) {
  754. foreach ($httpHeadXtra as & $thisHttpHead) {
  755. header($thisHttpHead);
  756. }
  757. }
  758. $this->assign(
  759. 'online_button',
  760. Display::return_icon('statusonline.png', null, [], ICON_SIZE_ATOM)
  761. );
  762. $this->assign(
  763. 'offline_button',
  764. Display::return_icon('statusoffline.png', null, [], ICON_SIZE_ATOM)
  765. );
  766. // Get language iso-code for this page - ignore errors
  767. $this->assign('document_language', api_get_language_isocode());
  768. $this->assign('current_language', api_get_interface_language());
  769. $course_title = isset($_course['name']) ? $_course['name'] : null;
  770. $title_list = array();
  771. $title_list[] = api_get_setting('Institution');
  772. $title_list[] = api_get_setting('siteName');
  773. if (!empty($course_title)) {
  774. $title_list[] = $course_title;
  775. }
  776. if ($nameTools != '') {
  777. $title_list[] = $nameTools;
  778. }
  779. $title_string = '';
  780. for ($i = 0; $i < count($title_list); $i++) {
  781. $title_string .= $title_list[$i];
  782. if (isset($title_list[$i + 1])) {
  783. $item = trim($title_list[$i + 1]);
  784. if (!empty($item)) {
  785. $title_string .= ' - ';
  786. }
  787. }
  788. }
  789. $this->assign('title_string', $title_string);
  790. //Setting the theme and CSS files
  791. $css = $this->setCssFiles();
  792. $this->set_js_files();
  793. $this->setCssCustomFiles($css);
  794. //$this->set_js_files_post();
  795. $browser = api_browser_support('check_browser');
  796. if ($browser[0] == 'Internet Explorer' && $browser[1] >= '11') {
  797. $browser_head = '<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9" />';
  798. $this->assign('browser_specific_head', $browser_head);
  799. }
  800. // Implementation of prefetch.
  801. // See http://cdn.chamilo.org/main/img/online.png for details
  802. $prefetch = '';
  803. if (!empty($_configuration['cdn_enable'])) {
  804. $prefetch .= '<meta http-equiv="x-dns-prefetch-control" content="on">';
  805. foreach ($_configuration['cdn'] as $host => $exts) {
  806. $prefetch .= '<link rel="dns-prefetch" href="'.$host.'">';
  807. }
  808. }
  809. $this->assign('prefetch', $prefetch);
  810. $this->assign('text_direction', api_get_text_direction());
  811. $this->assign('section_name', 'section-'.$this_section);
  812. // Default root chamilo favicon
  813. $favico = '<link rel="shortcut icon" href="'.api_get_path(WEB_PATH).'favicon.ico" type="image/x-icon" />';
  814. //Added to verify if in the current Chamilo Theme exist a favicon
  815. $favicoThemeUrl = api_get_path(SYS_CSS_PATH).$this->themeDir.'images/';
  816. //If exist pick the current chamilo theme favicon
  817. if (is_file($favicoThemeUrl.'favicon.ico')) {
  818. $favico = '<link rel="shortcut icon" href="'.api_get_path(WEB_CSS_PATH).$this->themeDir.'images/favicon.ico" type="image/x-icon" />';
  819. }
  820. if (api_is_multiple_url_enabled()) {
  821. $access_url_id = api_get_current_access_url_id();
  822. if ($access_url_id != -1) {
  823. $url_info = api_get_access_url($access_url_id);
  824. $url = api_remove_trailing_slash(
  825. preg_replace('/https?:\/\//i', '', $url_info['url'])
  826. );
  827. $clean_url = api_replace_dangerous_char($url);
  828. $clean_url = str_replace('/', '-', $clean_url);
  829. $clean_url .= '/';
  830. $homep = api_get_path(REL_PATH).'home/'.$clean_url; //homep for Home Path
  831. $icon_real_homep = api_get_path(SYS_APP_PATH).'home/'.$clean_url;
  832. //we create the new dir for the new sites
  833. if (is_file($icon_real_homep.'favicon.ico')) {
  834. $favico = '<link rel="shortcut icon" href="'.$homep.'favicon.ico" type="image/x-icon" />';
  835. }
  836. }
  837. }
  838. $this->assign('favico', $favico);
  839. $this->setHelp();
  840. //@todo move this in the template
  841. $rightFloatMenu = '';
  842. $iconBug = Display::return_icon(
  843. 'bug.png',
  844. get_lang('ReportABug'),
  845. [],
  846. ICON_SIZE_LARGE
  847. );
  848. if (api_get_setting('show_link_bug_notification') == 'true' && $this->user_is_logged_in) {
  849. $rightFloatMenu = '<div class="report">
  850. <a href="https://github.com/chamilo/chamilo-lms/wiki/How-to-report-issues" target="_blank">
  851. '.$iconBug.'
  852. </a>
  853. </div>';
  854. }
  855. if (api_get_setting('show_link_ticket_notification') == 'true' && $this->user_is_logged_in) {
  856. // by default is project_id = 1
  857. $iconTicket = Display::return_icon(
  858. 'bug.png',
  859. get_lang('Ticket'),
  860. [],
  861. ICON_SIZE_LARGE
  862. );
  863. $courseInfo = api_get_course_info();
  864. $courseParams = '';
  865. if (!empty($courseInfo)) {
  866. $courseParams = api_get_cidreq();
  867. }
  868. $url = api_get_path(WEB_CODE_PATH).'ticket/tickets.php?project_id=1&'.$courseParams;
  869. $rightFloatMenu .= '<div class="report">
  870. <a href="'.$url.'" target="_blank">
  871. '.$iconTicket.'
  872. </a>
  873. </div>';
  874. }
  875. $this->assign('bug_notification', $rightFloatMenu);
  876. $resize = '';
  877. if (api_get_setting('accessibility_font_resize') == 'true') {
  878. $resize .= '<div class="resize_font">';
  879. $resize .= '<div class="btn-group">';
  880. $resize .= '<a title="'.get_lang('DecreaseFontSize').'" href="#" class="decrease_font btn btn-default"><em class="fa fa-font"></em></a>';
  881. $resize .= '<a title="'.get_lang('ResetFontSize').'" href="#" class="reset_font btn btn-default"><em class="fa fa-font"></em></a>';
  882. $resize .= '<a title="'.get_lang('IncreaseFontSize').'" href="#" class="increase_font btn btn-default"><em class="fa fa-font"></em></a>';
  883. $resize .= '</div>';
  884. $resize .= '</div>';
  885. }
  886. $this->assign('accessibility', $resize);
  887. // Preparing values for the menu
  888. // Logout link
  889. $hideLogout = api_get_setting('hide_logout_button');
  890. if ($hideLogout === 'true') {
  891. $this->assign('logout_link', null);
  892. } else {
  893. $this->assign('logout_link', api_get_path(WEB_PATH).'index.php?logout=logout&uid='.api_get_user_id());
  894. }
  895. //Profile link
  896. if (api_get_setting('allow_social_tool') == 'true') {
  897. $profile_url = api_get_path(WEB_CODE_PATH).'social/home.php';
  898. } else {
  899. $profile_url = api_get_path(WEB_CODE_PATH).'auth/profile.php';
  900. }
  901. $this->assign('profile_url', $profile_url);
  902. //Message link
  903. $message_link = null;
  904. $message_url = null;
  905. if (api_get_setting('allow_message_tool') == 'true') {
  906. $message_url = api_get_path(WEB_CODE_PATH).'messages/inbox.php';
  907. $message_link = '<a href="'.api_get_path(WEB_CODE_PATH).'messages/inbox.php">'.get_lang('Inbox').'</a>';
  908. }
  909. $this->assign('message_link', $message_link);
  910. $this->assign('message_url', $message_url);
  911. // Certificate Link
  912. $allow = api_get_configuration_value('hide_my_certificate_link');
  913. if ($allow === false) {
  914. $certificateUrl = api_get_path(WEB_CODE_PATH).'gradebook/my_certificates.php';
  915. $certificateLink = Display::url(
  916. get_lang('MyCertificates'),
  917. $certificateUrl
  918. );
  919. $this->assign('certificate_link', $certificateLink);
  920. $this->assign('certificate_url', $certificateUrl);
  921. }
  922. $institution = api_get_setting('Institution');
  923. $portal_name = empty($institution) ? api_get_setting('siteName') : $institution;
  924. $this->assign('portal_name', $portal_name);
  925. //Menu
  926. $menu = menuArray();
  927. $this->assign('menu', $menu);
  928. $breadcrumb = '';
  929. // Hide breadcrumb in LP
  930. if ($this->show_learnpath == false) {
  931. $breadcrumb = return_breadcrumb(
  932. $interbreadcrumb,
  933. $language_file,
  934. $nameTools
  935. );
  936. }
  937. $this->assign('breadcrumb', $breadcrumb);
  938. //Extra content
  939. $extra_header = null;
  940. if (!api_is_platform_admin()) {
  941. $extra_header = trim(api_get_setting('header_extra_content'));
  942. }
  943. $this->assign('header_extra_content', $extra_header);
  944. if ($sendHeaders) {
  945. header('Content-Type: text/html; charset='.api_get_system_encoding());
  946. header(
  947. 'X-Powered-By: '.$_configuration['software_name'].' '.substr($_configuration['system_version'], 0, 1)
  948. );
  949. }
  950. $socialMeta = '';
  951. $metaTitle = api_get_setting('meta_title');
  952. if (!empty($metaTitle)) {
  953. $socialMeta .= '<meta name="twitter:card" content="summary" />'."\n";
  954. $metaSite = api_get_setting('meta_twitter_site');
  955. if (!empty($metaSite)) {
  956. $socialMeta .= '<meta name="twitter:site" content="'.$metaSite.'" />'."\n";
  957. $metaCreator = api_get_setting('meta_twitter_creator');
  958. if (!empty($metaCreator)) {
  959. $socialMeta .= '<meta name="twitter:creator" content="'.$metaCreator.'" />'."\n";
  960. }
  961. }
  962. // The user badge page emits its own meta tags, so if this is
  963. // enabled, ignore the global ones
  964. $userId = isset($_GET['user']) ? intval($_GET['user']) : 0;
  965. $skillId = isset($_GET['skill']) ? intval($_GET['skill']) : 0;
  966. if (!$userId && !$skillId) {
  967. // no combination of user and skill ID has been defined,
  968. // so print the normal OpenGraph meta tags
  969. $socialMeta .= '<meta property="og:title" content="'.$metaTitle.'" />'."\n";
  970. $socialMeta .= '<meta property="og:url" content="'.api_get_path(WEB_PATH).'" />'."\n";
  971. $metaDescription = api_get_setting('meta_description');
  972. if (!empty($metaDescription)) {
  973. $socialMeta .= '<meta property="og:description" content="'.$metaDescription.'" />'."\n";
  974. }
  975. $metaImage = api_get_setting('meta_image_path');
  976. if (!empty($metaImage)) {
  977. if (is_file(api_get_path(SYS_PATH).$metaImage)) {
  978. $path = api_get_path(WEB_PATH).$metaImage;
  979. $socialMeta .= '<meta property="og:image" content="'.$path.'" />'."\n";
  980. }
  981. }
  982. }
  983. }
  984. $this->assign('social_meta', $socialMeta);
  985. }
  986. /**
  987. * Set footer parameters
  988. */
  989. private function set_footer_parameters()
  990. {
  991. if (api_get_setting('show_administrator_data') === 'true') {
  992. $firstName = api_get_setting('administratorName');
  993. $lastName = api_get_setting('administratorSurname');
  994. if (!empty($firstName) && !empty($lastName)) {
  995. $name = api_get_person_name($firstName, $lastName);
  996. } else {
  997. $name = $lastName;
  998. if (empty($lastName)) {
  999. $name = $firstName;
  1000. }
  1001. }
  1002. $adminName = '';
  1003. // Administrator name
  1004. if (!empty($name)) {
  1005. $adminName = get_lang('Manager').' : '.
  1006. Display::encrypted_mailto_link(
  1007. api_get_setting('emailAdministrator'),
  1008. $name
  1009. );
  1010. }
  1011. $this->assign('administrator_name', $adminName);
  1012. }
  1013. // Loading footer extra content
  1014. if (!api_is_platform_admin()) {
  1015. $extra_footer = trim(api_get_setting('footer_extra_content'));
  1016. if (!empty($extra_footer)) {
  1017. $this->assign('footer_extra_content', $extra_footer);
  1018. }
  1019. }
  1020. // Tutor name
  1021. if (api_get_setting('show_tutor_data') == 'true') {
  1022. // Course manager
  1023. $courseId = api_get_course_int_id();
  1024. $id_session = api_get_session_id();
  1025. if (!empty($courseId)) {
  1026. $tutor_data = '';
  1027. if ($id_session != 0) {
  1028. $coachs_email = CourseManager::get_email_of_tutor_to_session(
  1029. $id_session,
  1030. $courseId
  1031. );
  1032. $email_link = array();
  1033. foreach ($coachs_email as $coach) {
  1034. $email_link[] = Display::encrypted_mailto_link($coach['email'], $coach['complete_name']);
  1035. }
  1036. if (count($coachs_email) > 1) {
  1037. $tutor_data .= get_lang('Coachs').' : ';
  1038. $tutor_data .= array_to_string($email_link, CourseManager::USER_SEPARATOR);
  1039. } elseif (count($coachs_email) == 1) {
  1040. $tutor_data .= get_lang('Coach').' : ';
  1041. $tutor_data .= array_to_string($email_link, CourseManager::USER_SEPARATOR);
  1042. } elseif (count($coachs_email) == 0) {
  1043. $tutor_data .= '';
  1044. }
  1045. }
  1046. $this->assign('session_teachers', $tutor_data);
  1047. }
  1048. }
  1049. if (api_get_setting('show_teacher_data') == 'true') {
  1050. // course manager
  1051. $courseId = api_get_course_int_id();
  1052. if (!empty($courseId)) {
  1053. $teacher_data = '';
  1054. $mail = CourseManager::get_emails_of_tutors_to_course($courseId);
  1055. if (!empty($mail)) {
  1056. $teachers_parsed = array();
  1057. foreach ($mail as $value) {
  1058. foreach ($value as $email => $name) {
  1059. $teachers_parsed[] = Display::encrypted_mailto_link($email, $name);
  1060. }
  1061. }
  1062. $label = get_lang('Teacher');
  1063. if (count($mail) > 1) {
  1064. $label = get_lang('Teachers');
  1065. }
  1066. $teacher_data .= $label.' : '.array_to_string($teachers_parsed, CourseManager::USER_SEPARATOR);
  1067. }
  1068. $this->assign('teachers', $teacher_data);
  1069. }
  1070. }
  1071. }
  1072. /**
  1073. * Show header template.
  1074. */
  1075. public function show_header_template()
  1076. {
  1077. $tpl = $this->get_template('layout/show_header.tpl');
  1078. $this->display($tpl);
  1079. }
  1080. /**
  1081. * Show footer template.
  1082. */
  1083. public function show_footer_template()
  1084. {
  1085. $tpl = $this->get_template('layout/show_footer.tpl');
  1086. $this->display($tpl);
  1087. }
  1088. /**
  1089. * Show footer js template.
  1090. */
  1091. public function show_footer_js_template()
  1092. {
  1093. $tpl = $this->get_template('layout/footer.js.tpl');
  1094. $this->display($tpl);
  1095. }
  1096. /**
  1097. * Sets the plugin content in a template variable
  1098. * @param string $pluginRegion
  1099. * @return null
  1100. */
  1101. public function set_plugin_region($pluginRegion)
  1102. {
  1103. if (!empty($pluginRegion)) {
  1104. $regionContent = $this->plugin->load_region($pluginRegion, $this, $this->force_plugin_load);
  1105. $pluginList = $this->plugin->get_installed_plugins();
  1106. foreach ($pluginList as $plugin_name) {
  1107. // The plugin_info variable is available inside the plugin index
  1108. $pluginInfo = $this->plugin->getPluginInfo($plugin_name);
  1109. if (isset($pluginInfo['is_course_plugin']) && $pluginInfo['is_course_plugin']) {
  1110. $courseInfo = api_get_course_info();
  1111. if (!empty($courseInfo)) {
  1112. if (isset($pluginInfo['obj']) && $pluginInfo['obj'] instanceof Plugin) {
  1113. /** @var Plugin $plugin */
  1114. $plugin = $pluginInfo['obj'];
  1115. $regionContent .= $plugin->renderRegion($pluginRegion);
  1116. }
  1117. }
  1118. } else {
  1119. continue;
  1120. }
  1121. }
  1122. if (!empty($regionContent)) {
  1123. $this->assign('plugin_'.$pluginRegion, $regionContent);
  1124. } else {
  1125. $this->assign('plugin_'.$pluginRegion, null);
  1126. }
  1127. }
  1128. return null;
  1129. }
  1130. /**
  1131. * @param string $template
  1132. * @return string
  1133. */
  1134. public function fetch($template = null)
  1135. {
  1136. $template = $this->twig->loadTemplate($template);
  1137. return $template->render($this->params);
  1138. }
  1139. /**
  1140. * @param string $variable
  1141. * @param mixed $value
  1142. */
  1143. public function assign($variable, $value = '')
  1144. {
  1145. $this->params[$variable] = $value;
  1146. }
  1147. /**
  1148. * Render the template
  1149. * @param string $template The template path
  1150. * @param boolean $clearFlashMessages Clear the $_SESSION variables for flash messages
  1151. */
  1152. public function display($template, $clearFlashMessages = true)
  1153. {
  1154. $this->assign('flash_messages', Display::getFlashToString());
  1155. if ($clearFlashMessages) {
  1156. Display::cleanFlashMessages();
  1157. }
  1158. echo $this->twig->render($template, $this->params);
  1159. }
  1160. /**
  1161. * Adds a body class for login pages
  1162. */
  1163. public function setLoginBodyClass()
  1164. {
  1165. $this->assign('login_class', 'section-login');
  1166. }
  1167. /**
  1168. * The theme that will be used if the database is not working.
  1169. * @return string
  1170. */
  1171. public static function getThemeFallback()
  1172. {
  1173. $theme = api_get_configuration_value('theme_fallback');
  1174. if (empty($theme)) {
  1175. $theme = 'chamilo';
  1176. }
  1177. return $theme;
  1178. }
  1179. /**
  1180. * @param bool|true $setLoginForm
  1181. */
  1182. public function setLoginForm($setLoginForm = true)
  1183. {
  1184. global $loginFailed;
  1185. $userId = api_get_user_id();
  1186. if (!($userId) || api_is_anonymous($userId)) {
  1187. // Only display if the user isn't logged in.
  1188. $this->assign(
  1189. 'login_language_form',
  1190. api_display_language_form(true)
  1191. );
  1192. if ($setLoginForm) {
  1193. $this->assign('login_form', $this->displayLoginForm());
  1194. if ($loginFailed) {
  1195. $this->assign('login_failed', $this::handleLoginFailed());
  1196. }
  1197. }
  1198. }
  1199. }
  1200. /**
  1201. * @return string
  1202. */
  1203. public function handleLoginFailed()
  1204. {
  1205. $message = get_lang('InvalidId');
  1206. if (!isset($_GET['error'])) {
  1207. if (api_is_self_registration_allowed()) {
  1208. $message = get_lang('InvalidForSelfRegistration');
  1209. }
  1210. } else {
  1211. switch ($_GET['error']) {
  1212. case '':
  1213. if (api_is_self_registration_allowed()) {
  1214. $message = get_lang('InvalidForSelfRegistration');
  1215. }
  1216. break;
  1217. case 'account_expired':
  1218. $message = get_lang('AccountExpired');
  1219. break;
  1220. case 'account_inactive':
  1221. $message = get_lang('AccountInactive');
  1222. break;
  1223. case 'user_password_incorrect':
  1224. $message = get_lang('InvalidId');
  1225. break;
  1226. case 'access_url_inactive':
  1227. $message = get_lang('AccountURLInactive');
  1228. break;
  1229. case 'wrong_captcha':
  1230. $message = get_lang('TheTextYouEnteredDoesNotMatchThePicture');
  1231. break;
  1232. case 'blocked_by_captcha':
  1233. $message = get_lang('AccountBlockedByCaptcha');
  1234. break;
  1235. case 'multiple_connection_not_allowed':
  1236. $message = get_lang('MultipleConnectionsAreNotAllow');
  1237. break;
  1238. case 'unrecognize_sso_origin':
  1239. //$message = get_lang('SSOError');
  1240. break;
  1241. }
  1242. }
  1243. return Display::return_message($message, 'error');
  1244. }
  1245. /**
  1246. * @return string
  1247. */
  1248. public function displayLoginForm()
  1249. {
  1250. $form = new FormValidator(
  1251. 'formLogin',
  1252. 'POST',
  1253. null,
  1254. null,
  1255. null,
  1256. FormValidator::LAYOUT_BOX_NO_LABEL
  1257. );
  1258. $form->addText(
  1259. 'login',
  1260. get_lang('UserName'),
  1261. true,
  1262. array(
  1263. 'id' => 'login',
  1264. 'autofocus' => 'autofocus',
  1265. 'icon' => 'user fa-fw',
  1266. 'placeholder' => get_lang('UserName'),
  1267. 'autocapitalize' => 'none'
  1268. )
  1269. );
  1270. $form->addElement(
  1271. 'password',
  1272. 'password',
  1273. get_lang('Pass'),
  1274. array(
  1275. 'id' => 'password',
  1276. 'icon' => 'lock fa-fw',
  1277. 'placeholder' => get_lang('Pass'),
  1278. 'autocapitalize' => 'none',
  1279. )
  1280. );
  1281. // Captcha
  1282. $captcha = api_get_setting('allow_captcha');
  1283. $allowCaptcha = $captcha === 'true';
  1284. if ($allowCaptcha) {
  1285. $useCaptcha = isset($_SESSION['loginFailed']) ? $_SESSION['loginFailed'] : null;
  1286. if ($useCaptcha) {
  1287. $ajax = api_get_path(WEB_AJAX_PATH).'form.ajax.php?a=get_captcha';
  1288. $options = array(
  1289. 'width' => 250,
  1290. 'height' => 90,
  1291. 'callback' => $ajax.'&var='.basename(__FILE__, '.php'),
  1292. 'sessionVar' => basename(__FILE__, '.php'),
  1293. 'imageOptions' => array(
  1294. 'font_size' => 20,
  1295. 'font_path' => api_get_path(SYS_FONTS_PATH).'opensans/',
  1296. 'font_file' => 'OpenSans-Regular.ttf',
  1297. //'output' => 'gif'
  1298. ),
  1299. );
  1300. // Minimum options using all defaults (including defaults for Image_Text):
  1301. //$options = array('callback' => 'qfcaptcha_image.php');
  1302. $captcha_question = $form->addElement('CAPTCHA_Image', 'captcha_question', '', $options);
  1303. $form->addHtml(get_lang('ClickOnTheImageForANewOne'));
  1304. $form->addElement('text', 'captcha', get_lang('EnterTheLettersYouSee'));
  1305. $form->addRule('captcha', get_lang('EnterTheCharactersYouReadInTheImage'), 'required', null, 'client');
  1306. $form->addRule('captcha', get_lang('TheTextYouEnteredDoesNotMatchThePicture'), 'CAPTCHA', $captcha_question);
  1307. }
  1308. }
  1309. $form->addButton('submitAuth', get_lang('LoginEnter'), null, 'primary', null, 'btn-block');
  1310. $html = $form->returnForm();
  1311. if (api_get_setting('openid_authentication') == 'true') {
  1312. include_once 'main/auth/openid/login.php';
  1313. $html .= '<div>'.openid_form().'</div>';
  1314. }
  1315. return $html;
  1316. }
  1317. /**
  1318. * Set administrator variables
  1319. */
  1320. private function setAdministratorParams()
  1321. {
  1322. $_admin = [
  1323. 'email' => api_get_setting('emailAdministrator'),
  1324. 'surname' => api_get_setting('administratorSurname'),
  1325. 'name' => api_get_setting('administratorName'),
  1326. 'telephone' => api_get_setting('administratorTelephone'),
  1327. ];
  1328. $this->assign('_admin', $_admin);
  1329. }
  1330. }