template.lib.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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. /**
  24. * @var string
  25. */
  26. public $preview_theme = '';
  27. public $title = null;
  28. public $show_header;
  29. public $show_footer;
  30. public $help;
  31. public $menu_navigation = array(); //Used in the userportal.lib.php function: return_navigation_course_links()
  32. public $show_learnpath = false; // This is a learnpath section or not?
  33. public $plugin = null;
  34. public $course_id = null;
  35. public $user_is_logged_in = false;
  36. public $twig = null;
  37. /* Loads chamilo plugins */
  38. public $load_plugins = false;
  39. public $params = array();
  40. public $force_plugin_load = false;
  41. /**
  42. * @param string $title
  43. * @param bool $show_header
  44. * @param bool $show_footer
  45. * @param bool $show_learnpath
  46. * @param bool $hide_global_chat
  47. * @param bool $load_plugins
  48. */
  49. public function __construct(
  50. $title = '',
  51. $show_header = true,
  52. $show_footer = true,
  53. $show_learnpath = false,
  54. $hide_global_chat = false,
  55. $load_plugins = true
  56. ) {
  57. // Page title
  58. $this->title = $title;
  59. $this->show_learnpath = $show_learnpath;
  60. $this->hide_global_chat = $hide_global_chat;
  61. $this->load_plugins = $load_plugins;
  62. $template_paths = array(
  63. api_get_path(SYS_CODE_PATH).'template', //template folder
  64. api_get_path(SYS_PLUGIN_PATH) //plugin folder
  65. );
  66. $cache_folder = api_get_path(SYS_ARCHIVE_PATH).'twig';
  67. if (!is_dir($cache_folder)) {
  68. mkdir($cache_folder, api_get_permissions_for_new_directories());
  69. }
  70. $loader = new Twig_Loader_Filesystem($template_paths);
  71. //Setting Twig options depending on the server see http://twig.sensiolabs.org/doc/api.html#environment-options
  72. if (api_get_setting('server_type') == 'test') {
  73. $options = array(
  74. //'cache' => api_get_path(SYS_ARCHIVE_PATH), //path to the cache folder
  75. 'autoescape' => false,
  76. 'debug' => true,
  77. 'auto_reload' => true,
  78. 'optimizations' => 0, // turn on optimizations with -1
  79. 'strict_variables' => false, //If set to false, Twig will silently ignore invalid variables
  80. );
  81. } else {
  82. $options = array(
  83. 'cache' => $cache_folder, //path to the cache folder
  84. 'autoescape' => false,
  85. 'debug' => false,
  86. 'auto_reload' => false,
  87. 'optimizations' => -1, // turn on optimizations with -1
  88. 'strict_variables' => false //If set to false, Twig will silently ignore invalid variables
  89. );
  90. }
  91. $this->twig = new Twig_Environment($loader, $options);
  92. $this->twig->addFilter('get_plugin_lang', new Twig_Filter_Function('get_plugin_lang'));
  93. $this->twig->addFilter('get_lang', new Twig_Filter_Function('get_lang'));
  94. $this->twig->addFilter('get_path', new Twig_Filter_Function('api_get_path'));
  95. $this->twig->addFilter('get_setting', new Twig_Filter_Function('api_get_setting'));
  96. $this->twig->addFilter('var_dump', new Twig_Filter_Function('var_dump'));
  97. $this->twig->addFilter('return_message', new Twig_Filter_Function('Display::return_message_and_translate'));
  98. $this->twig->addFilter('display_page_header', new Twig_Filter_Function('Display::page_header_and_translate'));
  99. $this->twig->addFilter(
  100. 'display_page_subheader',
  101. new Twig_Filter_Function('Display::page_subheader_and_translate')
  102. );
  103. $this->twig->addFilter('icon', new Twig_Filter_Function('Template::get_icon_path'));
  104. $this->twig->addFilter('format_date', new Twig_Filter_Function('Template::format_date'));
  105. $this->twig->addFilter('api_get_local_time', new Twig_Filter_Function('api_get_local_time'));
  106. /*
  107. $lexer = new Twig_Lexer($this->twig, array(
  108. //'tag_comment' => array('{*', '*}'),
  109. //'tag_comment' => array('{#', '#}'),
  110. //'tag_block' => array('{', '}'),
  111. //'tag_variable' => array('{$', '}'),
  112. ));
  113. $this->twig->setLexer($lexer); */
  114. //Setting system variables
  115. $this->set_system_parameters();
  116. //Setting user variables
  117. $this->set_user_parameters();
  118. //Setting course variables
  119. $this->set_course_parameters();
  120. //header and footer are showed by default
  121. $this->set_footer($show_footer);
  122. $this->set_header($show_header);
  123. $this->set_header_parameters();
  124. $this->set_footer_parameters();
  125. $defaultStyle = api_get_configuration_value('default_template');
  126. if (!empty($defaultStyle)) {
  127. $this->templateFolder = $defaultStyle;
  128. }
  129. $this->assign('template', $this->templateFolder);
  130. $this->assign('locale', api_get_language_isocode());
  131. $this->assign('css_styles', $this->theme);
  132. $this->assign('login_class', null);
  133. // Chamilo plugins
  134. if ($this->show_header) {
  135. if ($this->load_plugins) {
  136. $this->plugin = new AppPlugin();
  137. //1. Showing installed plugins in regions
  138. $plugin_regions = $this->plugin->get_plugin_regions();
  139. foreach ($plugin_regions as $region) {
  140. $this->set_plugin_region($region);
  141. }
  142. //2. Loading the course plugin info
  143. global $course_plugin;
  144. if (isset($course_plugin) && !empty($course_plugin) && !empty($this->course_id)) {
  145. //Load plugin get_langs
  146. $this->plugin->load_plugin_lang_variables($course_plugin);
  147. }
  148. }
  149. }
  150. }
  151. public static function get_icon_path($image, $size = ICON_SIZE_SMALL)
  152. {
  153. return Display:: return_icon($image, '', array(), $size, false, true);
  154. }
  155. public static function format_date($timestamp, $format = null)
  156. {
  157. return api_format_date($timestamp, $format);
  158. }
  159. /**
  160. * Return the item's url key:
  161. *
  162. * c_id=xx&id=xx
  163. *
  164. * @param object $item
  165. * @return string
  166. */
  167. public static function key($item)
  168. {
  169. $id = isset($item->id) ? $item->id : null;
  170. $c_id = isset($item->c_id) ? $item->c_id : null;
  171. $result = '';
  172. if ($c_id) {
  173. $result = "c_id=$c_id";
  174. }
  175. if ($id) {
  176. if ($result) {
  177. $result .= "&amp;id=$id";
  178. } else {
  179. $result .= "&amp;id=$id";
  180. }
  181. }
  182. return $result;
  183. }
  184. /**
  185. * @deprecated
  186. * @param null $helpInput
  187. */
  188. public function set_help($helpInput = null)
  189. {
  190. $this->setHelp($helpInput);
  191. }
  192. /**
  193. * @param string $helpInput
  194. */
  195. public function setHelp($helpInput = null)
  196. {
  197. if (!empty($helpInput)) {
  198. $help = $helpInput;
  199. } else {
  200. $help = $this->help;
  201. }
  202. $content = '';
  203. if (api_get_setting('enable_help_link') == 'true') {
  204. if (!empty($help)) {
  205. $help = Security::remove_XSS($help);
  206. $content = '<li class="help">';
  207. $content .= Display::url(
  208. Display::return_icon('help.large.png', get_lang('Help')),
  209. api_get_path(WEB_CODE_PATH).'help/help.php?open='.$help.'&height=400&width=600', array('class' => 'ajax'));
  210. $content .= '</li>';
  211. }
  212. }
  213. $this->assign('help_content', $content);
  214. }
  215. /**
  216. * Use template system to parse the actions menu
  217. * @todo finish it!
  218. **/
  219. public function set_actions($actions)
  220. {
  221. $action_string = '';
  222. if (!empty($actions)) {
  223. foreach ($actions as $action) {
  224. $action_string .= $action;
  225. }
  226. }
  227. $this->assign('actions', $actions);
  228. }
  229. /**
  230. * Shortcut to display a 1 col layout (index.php)
  231. * */
  232. public function display_one_col_template()
  233. {
  234. $tpl = $this->get_template('layout/layout_1_col.tpl');
  235. $this->display($tpl);
  236. }
  237. /**
  238. * Shortcut to display a 2 col layout (userportal.php)
  239. **/
  240. public function display_two_col_template()
  241. {
  242. $tpl = $this->get_template('layout/layout_2_col.tpl');
  243. $this->display($tpl);
  244. }
  245. /**
  246. * Displays an empty template
  247. */
  248. public function display_blank_template()
  249. {
  250. $tpl = $this->get_template('layout/blank.tpl');
  251. $this->display($tpl);
  252. }
  253. /**
  254. * Displays an empty template
  255. */
  256. public function display_no_layout_template()
  257. {
  258. $tpl = $this->get_template('layout/no_layout.tpl');
  259. $this->display($tpl);
  260. }
  261. /**
  262. * Sets the footer visibility
  263. * @param bool true if we show the footer
  264. */
  265. public function set_footer($status)
  266. {
  267. $this->show_footer = $status;
  268. $this->assign('show_footer', $status);
  269. }
  270. /**
  271. * return true if toolbar has to be displayed for user
  272. * @return bool
  273. */
  274. public static function isToolBarDisplayedForUser()
  275. {
  276. //Toolbar
  277. $show_admin_toolbar = api_get_setting('show_admin_toolbar');
  278. $show_toolbar = false;
  279. switch ($show_admin_toolbar) {
  280. case 'do_not_show':
  281. break;
  282. case 'show_to_admin':
  283. if (api_is_platform_admin()) {
  284. $show_toolbar = true;
  285. }
  286. break;
  287. case 'show_to_admin_and_teachers':
  288. if (api_is_platform_admin() || api_is_allowed_to_edit()) {
  289. $show_toolbar = true;
  290. }
  291. break;
  292. case 'show_to_all':
  293. $show_toolbar = true;
  294. break;
  295. }
  296. return $show_toolbar;
  297. }
  298. /**
  299. * Sets the header visibility
  300. * @param bool true if we show the header
  301. */
  302. public function set_header($status)
  303. {
  304. $this->show_header = $status;
  305. $this->assign('show_header', $status);
  306. $show_toolbar = 0;
  307. if (self::isToolBarDisplayedForUser()) {
  308. $show_toolbar = 1;
  309. }
  310. $this->assign('show_toolbar', $show_toolbar);
  311. //Only if course is available
  312. $show_course_shortcut = null;
  313. $show_course_navigation_menu = null;
  314. if (!empty($this->course_id) && $this->user_is_logged_in) {
  315. if (api_get_setting('show_toolshortcuts') != 'false') {
  316. //Course toolbar
  317. $show_course_shortcut = CourseHome::show_navigation_tool_shortcuts();
  318. }
  319. if (api_get_setting('show_navigation_menu') != 'false') {
  320. //Course toolbar
  321. $show_course_navigation_menu = CourseHome::show_navigation_menu();
  322. }
  323. }
  324. $this->assign('show_course_shortcut', $show_course_shortcut);
  325. $this->assign('show_course_navigation_menu', $show_course_navigation_menu);
  326. }
  327. /**
  328. * @param string $name
  329. *
  330. * @return string
  331. */
  332. public function get_template($name)
  333. {
  334. return $this->templateFolder.'/'.$name;
  335. }
  336. /**
  337. * Set course parameters
  338. */
  339. private function set_course_parameters()
  340. {
  341. //Setting course id
  342. $course = api_get_course_info();
  343. if (empty($course)) {
  344. $this->assign('course_is_set', false);
  345. return;
  346. }
  347. $this->assign('course_is_set', true);
  348. $this->course_id = $course['id'];
  349. $_c = array(
  350. 'id' => $course['id'],
  351. 'code' => $course['code'],
  352. 'title' => $course['name'],
  353. 'visibility' => $course['visibility'],
  354. 'language' => $course['language'],
  355. 'directory' => $course['directory'],
  356. 'session_id' => api_get_session_id(),
  357. 'user_is_teacher' => api_is_course_admin(),
  358. 'student_view' => (!empty($_GET['isStudentView']) && $_GET['isStudentView'] == 'true'),
  359. );
  360. $this->assign('course_code', $course['code']);
  361. $this->assign('_c', $_c);
  362. }
  363. /**
  364. * Set user parameters
  365. */
  366. private function set_user_parameters()
  367. {
  368. $user_info = array();
  369. $user_info['logged'] = 0;
  370. $this->user_is_logged_in = false;
  371. if (api_user_is_login()) {
  372. $user_info = api_get_user_info(api_get_user_id(), true);
  373. $user_info['logged'] = 1;
  374. $user_info['is_admin'] = 0;
  375. if (api_is_platform_admin()) {
  376. $user_info['is_admin'] = 1;
  377. }
  378. $user_info['messages_count'] = MessageManager::get_new_messages();
  379. $this->user_is_logged_in = true;
  380. }
  381. // Setting the $_u array that could be use in any template
  382. $this->assign('_u', $user_info);
  383. }
  384. /**
  385. * Set system parameters
  386. */
  387. private function set_system_parameters()
  388. {
  389. global $_configuration;
  390. $this->theme = api_get_visual_theme();
  391. //Setting app paths/URLs
  392. $_p = array(
  393. 'web' => api_get_path(WEB_PATH),
  394. 'web_relative' => api_get_path(REL_PATH),
  395. 'web_course' => api_get_path(WEB_COURSE_PATH),
  396. 'web_main' => api_get_path(WEB_CODE_PATH),
  397. 'web_css' => api_get_path(WEB_CSS_PATH),
  398. 'web_css_theme' => api_get_path(WEB_CSS_PATH) . 'themes/' . $this->theme . '/',
  399. 'web_ajax' => api_get_path(WEB_AJAX_PATH),
  400. 'web_img' => api_get_path(WEB_IMG_PATH),
  401. 'web_plugin' => api_get_path(WEB_PLUGIN_PATH),
  402. 'web_lib' => api_get_path(WEB_LIBRARY_PATH),
  403. 'web_upload' => api_get_path(WEB_UPLOAD_PATH),
  404. 'web_self' => api_get_self(),
  405. 'web_query_vars' => api_htmlentities($_SERVER['QUERY_STRING']),
  406. 'web_self_query_vars' => api_htmlentities($_SERVER['REQUEST_URI']),
  407. 'web_cid_query' => api_get_cidreq(),
  408. );
  409. $this->assign('_p', $_p);
  410. //Here we can add system parameters that can be use in any template
  411. $_s = array(
  412. 'software_name' => $_configuration['software_name'],
  413. 'system_version' => $_configuration['system_version'],
  414. 'site_name' => api_get_setting('siteName'),
  415. 'institution' => api_get_setting('Institution'),
  416. 'date' => api_format_date('now', DATE_FORMAT_LONG),
  417. 'timezone' => _api_get_timezone()
  418. );
  419. $this->assign('_s', $_s);
  420. }
  421. /**
  422. * Set theme, include mainstream CSS files
  423. * @return void
  424. * @see setCssCustomFiles() for additional CSS sheets
  425. */
  426. public function setCssFiles()
  427. {
  428. global $disable_js_and_css_files;
  429. $css = array();
  430. $this->theme = api_get_visual_theme();
  431. if (!empty($this->preview_theme)) {
  432. $this->theme = $this->preview_theme;
  433. }
  434. // Default CSS Bootstrap
  435. $bowerCSSFiles = [
  436. 'bootstrap/dist/css/bootstrap.min.css',
  437. 'bootstrap-daterangepicker/daterangepicker-bs3.css',
  438. 'fontawesome/css/font-awesome.min.css',
  439. 'jquery-ui/themes/smoothness/jquery-ui.min.css',
  440. 'jquery-ui/themes/smoothness/theme.css',
  441. 'mediaelement/build/mediaelementplayer.min.css',
  442. 'jqueryui-timepicker-addon/dist/jquery-ui-timepicker-addon.min.css'
  443. ];
  444. foreach ($bowerCSSFiles as $file) {
  445. $css[] = api_get_path(WEB_PATH).'web/assets/'.$file;
  446. }
  447. $css[] = api_get_path(WEB_LIBRARY_PATH) . 'javascript/chosen/chosen.css';
  448. $css[] = api_get_path(WEB_LIBRARY_PATH) . 'javascript/tag/style.css';
  449. if (api_is_global_chat_enabled()) {
  450. $css[] = api_get_path(WEB_LIBRARY_PATH) . 'javascript/chat/css/chat.css';
  451. }
  452. //THEME CSS STYLE
  453. // $css[] = api_get_cdn_path(api_get_path(WEB_CSS_PATH).'responsive.css');
  454. $css_file_to_string = null;
  455. foreach ($css as $file) {
  456. $css_file_to_string .= api_get_css($file);
  457. }
  458. if (!$disable_js_and_css_files) {
  459. $this->assign('css_static_file_to_string', $css_file_to_string);
  460. }
  461. }
  462. /**
  463. * Prepare custom CSS to be added at the very end of the <head> section
  464. * @return void
  465. * @see setCssFiles() for the mainstream CSS files
  466. */
  467. public function setCssCustomFiles()
  468. {
  469. global $disable_js_and_css_files;
  470. // Base CSS
  471. $css[] = api_get_cdn_path(api_get_path(WEB_CSS_PATH).'base.css');
  472. if ($this->show_learnpath) {
  473. $css[] = api_get_cdn_path(api_get_path(WEB_CSS_PATH).'scorm.css');
  474. if (is_file(api_get_path(SYS_CSS_PATH).'themes/'.$this->theme.'/learnpath.css')) {
  475. $css[] = api_get_path(WEB_CSS_PATH).'themes/'.$this->theme.'/learnpath.css';
  476. }
  477. }
  478. $css[] = api_get_cdn_path(api_get_path(WEB_CSS_PATH).'themes/'.$this->theme.'/default.css');
  479. $css_file_to_string = null;
  480. foreach ($css as $file) {
  481. $css_file_to_string .= api_get_css($file);
  482. }
  483. // @todo move this somewhere else. Special fix when using tablets in order to see the text near icons
  484. if (SHOW_TEXT_NEAR_ICONS == true) {
  485. //hack in order to fix the actions buttons
  486. $css_file_to_string .= '<style>
  487. .td_actions a {
  488. float:left;
  489. width:100%;
  490. }
  491. .forum_message_left a {
  492. float:left;
  493. width:100%;
  494. }
  495. </style>';
  496. }
  497. $navigator_info = api_get_navigator();
  498. if ($navigator_info['name'] == 'Internet Explorer' && $navigator_info['version'] == '6') {
  499. $css_file_to_string .= 'img, div { behavior: url('.api_get_path(WEB_LIBRARY_PATH).'javascript/iepngfix/iepngfix.htc) } '."\n";
  500. }
  501. if (!$disable_js_and_css_files) {
  502. $this->assign('css_custom_file_to_string', $css_file_to_string);
  503. $style_print = '';
  504. if (is_readable(api_get_path(SYS_CSS_PATH).$this->theme.'/print.css')) {
  505. $style_print = api_get_css(api_get_cdn_path(api_get_path(WEB_CSS_PATH) . $this->theme . '/print.css'),
  506. 'print');
  507. }
  508. $this->assign('css_style_print', $style_print);
  509. }
  510. // Logo
  511. $logo = return_logo($this->theme);
  512. $this->assign('logo', $logo);
  513. }
  514. /**
  515. * Declare and define the template variable that will be used to load
  516. * javascript libraries in the header.
  517. */
  518. public function set_js_files()
  519. {
  520. global $disable_js_and_css_files, $htmlHeadXtra;
  521. //JS files
  522. $js_files = array(
  523. 'chosen/chosen.jquery.min.js',
  524. );
  525. if (api_is_global_chat_enabled()) {
  526. //Do not include the global chat in LP
  527. if ($this->show_learnpath == false &&
  528. $this->show_footer == true &&
  529. $this->hide_global_chat == false
  530. ) {
  531. $js_files[] = 'chat/js/chat.js';
  532. }
  533. }
  534. if (api_get_setting('accessibility_font_resize') == 'true') {
  535. $js_files[] = 'fontresize.js';
  536. }
  537. if (api_get_setting('include_asciimathml_script') == 'true') {
  538. $js_files[] = 'asciimath/ASCIIMathML.js';
  539. }
  540. $js_files[] = 'tag/jquery.fcbkcomplete.js';
  541. $js_file_to_string = null;
  542. $isoCode = api_get_language_isocode();
  543. $bowerJsFiles = [
  544. 'modernizr/modernizr.js',
  545. 'jquery/dist/jquery.min.js',
  546. 'moment/min/moment-with-locales.min.js',
  547. 'jquery-ui/jquery-ui.min.js',
  548. 'bootstrap/dist/js/bootstrap.min.js',
  549. 'ckeditor/ckeditor.js',
  550. 'bootstrap-daterangepicker/daterangepicker.js',
  551. 'jquery-timeago/jquery.timeago.js',
  552. 'mediaelement/build/mediaelement-and-player.min.js',
  553. 'jqueryui-timepicker-addon/dist/jquery-ui-timepicker-addon.min.js',
  554. 'imagemap-resizer/js/imageMapResizer.min.js'
  555. ];
  556. if ($isoCode != 'en') {
  557. $bowerJsFiles[] = 'jqueryui-timepicker-addon/dist/i18n/jquery-ui-timepicker-' . $isoCode . '.js';
  558. $bowerJsFiles[] = 'jquery-ui/ui/minified/i18n/datepicker-' . $isoCode . '.min.js';
  559. }
  560. foreach ($bowerJsFiles as $file) {
  561. $js_file_to_string .= '<script type="text/javascript" src="'.api_get_path(WEB_PATH).'web/assets/'.$file.'"></script>'."\n";
  562. }
  563. foreach ($js_files as $file) {
  564. $js_file_to_string .= api_get_js($file);
  565. }
  566. // Loading email_editor js
  567. if (!api_is_anonymous() && api_get_setting('allow_email_editor') == 'true') {
  568. $js_file_to_string .= $this->fetch('default/mail_editor/email_link.js.tpl');
  569. }
  570. if (!$disable_js_and_css_files) {
  571. $this->assign('js_file_to_string', $js_file_to_string);
  572. //Adding jquery ui by default
  573. $extra_headers = api_get_jquery_ui_js();
  574. //$extra_headers = '';
  575. if (isset($htmlHeadXtra) && $htmlHeadXtra) {
  576. foreach ($htmlHeadXtra as & $this_html_head) {
  577. $extra_headers .= $this_html_head."\n";
  578. }
  579. }
  580. $this->assign('extra_headers', $extra_headers);
  581. }
  582. }
  583. /**
  584. * Special function to declare last-minute JS libraries which depend on
  585. * other things to be declared first. In particular, it might be useful
  586. * under IE9 with compatibility mode, which for some reason is getting
  587. * upset when a variable is used in a function (even if not used yet)
  588. * when this variable hasn't been defined yet.
  589. */
  590. public function set_js_files_post()
  591. {
  592. global $disable_js_and_css_files, $htmlHeadXtra;
  593. $js_files = array();
  594. if (api_is_global_chat_enabled()) {
  595. //Do not include the global chat in LP
  596. if ($this->show_learnpath == false && $this->show_footer == true && $this->hide_global_chat == false) {
  597. $js_files[] = 'chat/js/chat.js';
  598. }
  599. }
  600. $js_file_to_string = null;
  601. foreach ($js_files as $js_file) {
  602. $js_file_to_string .= api_get_js($js_file);
  603. }
  604. if (!$disable_js_and_css_files) {
  605. $this->assign('js_file_to_string_post', $js_file_to_string);
  606. }
  607. }
  608. /**
  609. * Set header parameters
  610. */
  611. private function set_header_parameters()
  612. {
  613. global $httpHeadXtra, $_course, $interbreadcrumb, $language_file, $noPHP_SELF, $_configuration, $this_section;
  614. $help = $this->help;
  615. $nameTools = $this->title;
  616. $navigation = return_navigation_array();
  617. $this->menu_navigation = $navigation['menu_navigation'];
  618. $this->assign('system_charset', api_get_system_encoding());
  619. if (isset($httpHeadXtra) && $httpHeadXtra) {
  620. foreach ($httpHeadXtra as & $thisHttpHead) {
  621. header($thisHttpHead);
  622. }
  623. }
  624. $this->assign('online_button', Display::return_icon('online.png'));
  625. $this->assign('offline_button',Display::return_icon('offline.png'));
  626. // Get language iso-code for this page - ignore errors
  627. $this->assign('document_language', api_get_language_isocode());
  628. $course_title = isset($_course['name']) ? $_course['name'] : null;
  629. $title_list = array();
  630. $title_list[] = api_get_setting('Institution');
  631. $title_list[] = api_get_setting('siteName');
  632. if (!empty($course_title)) {
  633. $title_list[] = $course_title;
  634. }
  635. if ($nameTools != '') {
  636. $title_list[] = $nameTools;
  637. }
  638. $title_string = '';
  639. for ($i = 0; $i < count($title_list); $i++) {
  640. $title_string .= $title_list[$i];
  641. if (isset($title_list[$i + 1])) {
  642. $item = trim($title_list[$i + 1]);
  643. if (!empty($item)) {
  644. $title_string .= ' - ';
  645. }
  646. }
  647. }
  648. $this->assign('title_string', $title_string);
  649. //Setting the theme and CSS files
  650. $css = $this->setCssFiles();
  651. $this->set_js_files();
  652. $this->setCssCustomFiles($css);
  653. //$this->set_js_files_post();
  654. $browser = api_browser_support('check_browser');
  655. if ($browser[0] == 'Internet Explorer' && $browser[1] >= '11') {
  656. $browser_head = '<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9" />';
  657. $this->assign('browser_specific_head', $browser_head);
  658. }
  659. // Implementation of prefetch.
  660. // See http://cdn.chamilo.org/main/img/online.png for details
  661. $prefetch = '';
  662. if (!empty($_configuration['cdn_enable'])) {
  663. $prefetch .= '<meta http-equiv="x-dns-prefetch-control" content="on">';
  664. foreach ($_configuration['cdn'] as $host => $exts) {
  665. $prefetch .= '<link rel="dns-prefetch" href="'.$host.'">';
  666. }
  667. }
  668. $this->assign('prefetch', $prefetch);
  669. $this->assign('text_direction', api_get_text_direction());
  670. $this->assign('section_name', 'section-'.$this_section);
  671. $favico = '<link rel="shortcut icon" href="'.api_get_path(WEB_PATH).'favicon.ico" type="image/x-icon" />';
  672. if (api_is_multiple_url_enabled()) {
  673. $access_url_id = api_get_current_access_url_id();
  674. if ($access_url_id != -1) {
  675. $url_info = api_get_access_url($access_url_id);
  676. $url = api_remove_trailing_slash(preg_replace('/https?:\/\//i', '', $url_info['url']));
  677. $clean_url = api_replace_dangerous_char($url);
  678. $clean_url = str_replace('/', '-', $clean_url);
  679. $clean_url .= '/';
  680. $homep = api_get_path(REL_PATH).'home/'.$clean_url; //homep for Home Path
  681. $icon_real_homep = api_get_path(SYS_APP_PATH).'home/'.$clean_url;
  682. //we create the new dir for the new sites
  683. if (is_file($icon_real_homep.'favicon.ico')) {
  684. $favico = '<link rel="shortcut icon" href="'.$homep.'favicon.ico" type="image/x-icon" />';
  685. }
  686. }
  687. }
  688. $this->assign('favico', $favico);
  689. $this->set_help();
  690. //@todo move this in the template
  691. $bug_notification_link = '';
  692. if (api_get_setting('show_link_bug_notification') == 'true' && $this->user_is_logged_in) {
  693. $bug_notification_link = '<li class="report">
  694. <a href="http://support.chamilo.org/projects/chamilo-18/wiki/How_to_report_bugs" target="_blank">
  695. <img src="'.api_get_path(WEB_IMG_PATH).'bug.large.png" style="vertical-align: middle;" alt="'.get_lang('ReportABug').'" title="'.get_lang(
  696. 'ReportABug'
  697. ).'"/></a>
  698. </li>';
  699. }
  700. $this->assign('bug_notification_link', $bug_notification_link);
  701. $notification = return_notification_menu();
  702. $this->assign('notification_menu', $notification);
  703. //Preparing values for the menu
  704. //Logout link
  705. $hideLogout = api_get_setting('hide_logout_button');
  706. if ($hideLogout === 'true') {
  707. $this->assign('logout_link', null);
  708. } else {
  709. $this->assign('logout_link', api_get_path(WEB_PATH).'index.php?logout=logout&uid='.api_get_user_id());
  710. }
  711. //Profile link
  712. if (api_get_setting('allow_social_tool') == 'true') {
  713. $profile_url = api_get_path(WEB_CODE_PATH).'social/home.php';
  714. $profile_link = Display::url(get_lang('Profile'), $profile_url);
  715. } else {
  716. $profile_url = api_get_path(WEB_CODE_PATH).'auth/profile.php';
  717. $profile_link = Display::url(get_lang('Profile'), $profile_url);
  718. }
  719. $this->assign('profile_link', $profile_link);
  720. $this->assign('profile_url', $profile_url);
  721. //Message link
  722. $message_link = null;
  723. $message_url = null;
  724. if (api_get_setting('allow_message_tool') == 'true') {
  725. $message_url = api_get_path(WEB_CODE_PATH).'messages/inbox.php';
  726. $message_link = '<a href="'.api_get_path(WEB_CODE_PATH).'messages/inbox.php">'.get_lang('Inbox').'</a>';
  727. }
  728. $this->assign('message_link', $message_link);
  729. $this->assign('message_url', $message_url);
  730. $institution = api_get_setting('Institution');
  731. $portal_name = empty($institution) ? api_get_setting('siteName') : $institution;
  732. $this->assign('portal_name', $portal_name);
  733. //Menu
  734. $menu = return_menu();
  735. $this->assign('menu', $menu);
  736. //Setting notifications
  737. $count_unread_message = 0;
  738. if (api_get_setting('allow_message_tool') == 'true') {
  739. // get count unread message and total invitations
  740. $count_unread_message = MessageManager::get_number_of_messages(true);
  741. }
  742. $total_invitations = 0;
  743. if (api_get_setting('allow_social_tool') == 'true') {
  744. $number_of_new_messages_of_friend = SocialManager::get_message_number_invitation_by_user_id(
  745. api_get_user_id()
  746. );
  747. $usergroup = new UserGroup();
  748. $group_pending_invitations = $usergroup->get_groups_by_user(
  749. api_get_user_id(),
  750. GROUP_USER_PERMISSION_PENDING_INVITATION,
  751. false
  752. );
  753. $group_pending_invitations = 0;
  754. if (!empty($group_pending_invitations)) {
  755. $group_pending_invitations = count($group_pending_invitations);
  756. }
  757. $total_invitations = intval($number_of_new_messages_of_friend) + $group_pending_invitations + intval($count_unread_message);
  758. }
  759. $total_invitations = (!empty($total_invitations) ? Display::badge($total_invitations) : null);
  760. $this->assign('user_notifications', $total_invitations);
  761. // Block Breadcrumb
  762. $breadcrumb = return_breadcrumb($interbreadcrumb, $language_file, $nameTools);
  763. $this->assign('breadcrumb', $breadcrumb);
  764. //Extra content
  765. $extra_header = null;
  766. if (!api_is_platform_admin()) {
  767. $extra_header = trim(api_get_setting('header_extra_content'));
  768. }
  769. $this->assign('header_extra_content', $extra_header);
  770. //if ($this->show_header == 1) {
  771. header('Content-Type: text/html; charset='.api_get_system_encoding());
  772. header(
  773. 'X-Powered-By: '.$_configuration['software_name'].' '.substr($_configuration['system_version'], 0, 1)
  774. );
  775. //}
  776. $socialMeta = '';
  777. $metaTitle = api_get_setting('meta_title');
  778. if (!empty($metaTitle)) {
  779. $socialMeta .= '<meta name="twitter:card" content="summary" />' . "\n";
  780. $socialMeta .= '<meta property="og:title" content="' . $metaTitle . '" />' . "\n";
  781. $socialMeta .= '<meta property="og:url" content="' . api_get_path(WEB_PATH) . '" />' . "\n";
  782. $metaDescription = api_get_setting('meta_description');
  783. if (!empty($metaDescription)) {
  784. $socialMeta .= '<meta property="og:description" content="' . $metaDescription . '" />' . "\n";
  785. }
  786. $metaSite = api_get_setting('meta_twitter_site');
  787. if (!empty($metaSite)) {
  788. $socialMeta .= '<meta name="twitter:site" content="' . $metaSite . '" />' . "\n";
  789. $metaCreator = api_get_setting('meta_twitter_creator');
  790. if (!empty($metaCreator)) {
  791. $socialMeta .= '<meta name="twitter:creator" content="' . $metaCreator . '" />' . "\n";
  792. }
  793. }
  794. $metaImage = api_get_setting('meta_image_path');
  795. if (!empty($metaImage)) {
  796. if (is_file(api_get_path(SYS_PATH) . $metaImage)) {
  797. $path = api_get_path(WEB_PATH) . $metaImage;
  798. $socialMeta .= '<meta property="og:image" content="' . $path . '" />' . "\n";
  799. }
  800. }
  801. }
  802. $this->assign('social_meta', $socialMeta);
  803. }
  804. /**
  805. * Set footer parameters
  806. */
  807. private function set_footer_parameters()
  808. {
  809. global $_configuration;
  810. //Show admin data
  811. //$this->assign('show_administrator_data', api_get_setting('show_administrator_data'));
  812. if (api_get_setting('show_administrator_data') == 'true') {
  813. //Administrator name
  814. $administrator_data = get_lang('Manager').' : '.Display::encrypted_mailto_link(
  815. api_get_setting('emailAdministrator'),
  816. api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'))
  817. );
  818. $this->assign('administrator_name', $administrator_data);
  819. }
  820. //Loading footer extra content
  821. if (!api_is_platform_admin()) {
  822. $extra_footer = trim(api_get_setting('footer_extra_content'));
  823. if (!empty($extra_footer)) {
  824. $this->assign('footer_extra_content', $extra_footer);
  825. }
  826. }
  827. //Tutor name
  828. if (api_get_setting('show_tutor_data') == 'true') {
  829. // Course manager
  830. $courseId = api_get_course_int_id();
  831. $id_session = api_get_session_id();
  832. if (!empty($courseId)) {
  833. $tutor_data = '';
  834. if ($id_session != 0) {
  835. $coachs_email = CourseManager::get_email_of_tutor_to_session($id_session, $courseId);
  836. $email_link = array();
  837. foreach ($coachs_email as $coach) {
  838. $email_link[] = Display::encrypted_mailto_link($coach['email'], $coach['complete_name']);
  839. }
  840. if (count($coachs_email) > 1) {
  841. $tutor_data .= get_lang('Coachs').' : ';
  842. $tutor_data .= array_to_string($email_link, CourseManager::USER_SEPARATOR);
  843. } elseif (count($coachs_email) == 1) {
  844. $tutor_data .= get_lang('Coach').' : ';
  845. $tutor_data .= array_to_string($email_link, CourseManager::USER_SEPARATOR);
  846. } elseif (count($coachs_email) == 0) {
  847. $tutor_data .= '';
  848. }
  849. }
  850. $this->assign('session_teachers', $tutor_data);
  851. }
  852. }
  853. if (api_get_setting('show_teacher_data') == 'true') {
  854. // course manager
  855. $courseId = api_get_course_int_id();
  856. if (!empty($courseId)) {
  857. $teacher_data = '';
  858. $mail= CourseManager::get_emails_of_tutors_to_course($courseId);
  859. if (!empty($mail)) {
  860. $teachers_parsed = array();
  861. foreach ($mail as $value) {
  862. foreach ($value as $email => $name) {
  863. $teachers_parsed[] = Display::encrypted_mailto_link($email, $name);
  864. }
  865. }
  866. $label = get_lang('Teacher');
  867. if (count($mail) > 1) {
  868. $label = get_lang('Teachers');
  869. }
  870. $teacher_data .= $label.' : '.array_to_string($teachers_parsed, CourseManager::USER_SEPARATOR);
  871. }
  872. $this->assign('teachers', $teacher_data);
  873. }
  874. }
  875. /* $stats = '';
  876. $this->assign('execution_stats', $stats); */
  877. }
  878. /**
  879. * Show header template.
  880. */
  881. public function show_header_template()
  882. {
  883. $tpl = $this->get_template('layout/show_header.tpl');
  884. $this->display($tpl);
  885. }
  886. /**
  887. * Show footer template.
  888. */
  889. public function show_footer_template()
  890. {
  891. $tpl = $this->get_template('layout/show_footer.tpl');
  892. $this->display($tpl);
  893. }
  894. /**
  895. * Sets the plugin content in a template variable
  896. * @param string $plugin_region
  897. * @return null
  898. */
  899. public function set_plugin_region($plugin_region)
  900. {
  901. if (!empty($plugin_region)) {
  902. $region_content = $this->plugin->load_region($plugin_region, $this, $this->force_plugin_load);
  903. if (!empty($region_content)) {
  904. $this->assign('plugin_'.$plugin_region, $region_content);
  905. } else {
  906. $this->assign('plugin_'.$plugin_region, null);
  907. }
  908. }
  909. return null;
  910. }
  911. /**
  912. * @param string $template
  913. * @return string
  914. */
  915. public function fetch($template = null)
  916. {
  917. $template = $this->twig->loadTemplate($template);
  918. return $template->render($this->params);
  919. }
  920. /**
  921. * @param $tpl_var
  922. * @param null $value
  923. */
  924. public function assign($tpl_var, $value = null)
  925. {
  926. $this->params[$tpl_var] = $value;
  927. }
  928. /**
  929. * @param string $template
  930. */
  931. public function display($template)
  932. {
  933. $this->assign('flash_messages', Display::getFlashToString());
  934. Display::cleanFlashMessages();
  935. echo $this->twig->render($template, $this->params);
  936. }
  937. /**
  938. * Adds a body class for login pages
  939. */
  940. public function setLoginBodyClass()
  941. {
  942. $this->assign('login_class', 'section-login');
  943. }
  944. /**
  945. * The theme that will be used if the database is not working.
  946. * @return string
  947. */
  948. public static function getThemeFallback()
  949. {
  950. $theme = api_get_configuration_value('theme_fallback');
  951. if (empty($theme)) {
  952. $theme = 'chamilo';
  953. }
  954. return $theme;
  955. }
  956. }