template.lib.php 69 KB

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