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->getInstalledPlugins(false);
  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[] = UserManager::formatUserFullName($tutor);
  1089. }
  1090. return implode(CourseManager::USER_SEPARATOR, $names);
  1091. }
  1092. /**
  1093. * Returns the teachers name for the current course
  1094. * Function to use in Twig templates.
  1095. *
  1096. * @return string
  1097. */
  1098. public static function returnTeachersNames()
  1099. {
  1100. $em = Database::getManager();
  1101. $teachers = $em
  1102. ->createQuery('
  1103. SELECT u FROM ChamiloUserBundle:User u
  1104. INNER JOIN ChamiloCoreBundle:CourseRelUser cu WITH u.id = cu.user
  1105. WHERE cu.status = :teacher_status AND cu.course = :course
  1106. ')
  1107. ->setParameters([
  1108. 'teacher_status' => User::COURSE_MANAGER,
  1109. 'course' => api_get_course_int_id(),
  1110. ])
  1111. ->getResult();
  1112. $names = [];
  1113. /** @var User $teacher */
  1114. foreach ($teachers as $teacher) {
  1115. $names[] = UserManager::formatUserFullName($teacher);
  1116. }
  1117. return implode(CourseManager::USER_SEPARATOR, $names);
  1118. }
  1119. /**
  1120. * @param int $code
  1121. */
  1122. public function setResponseCode($code)
  1123. {
  1124. $this->responseCode = $code;
  1125. }
  1126. /**
  1127. * @param string $code
  1128. */
  1129. public function getResponseCode()
  1130. {
  1131. return $this->responseCode;
  1132. }
  1133. /**
  1134. * Assign HTML code to the 'bug_notification' template variable for the side tabs to report issues.
  1135. *
  1136. * @return bool Always return true because there is always a string, even if empty
  1137. */
  1138. public function assignBugNotification()
  1139. {
  1140. //@todo move this in the template
  1141. $rightFloatMenu = '';
  1142. $iconBug = Display::return_icon(
  1143. 'bug.png',
  1144. get_lang('ReportABug'),
  1145. [],
  1146. ICON_SIZE_LARGE
  1147. );
  1148. if (api_get_setting('show_link_bug_notification') === 'true' && $this->user_is_logged_in) {
  1149. $rightFloatMenu = '<div class="report">
  1150. <a href="https://github.com/chamilo/chamilo-lms/wiki/How-to-report-issues" target="_blank">
  1151. '.$iconBug.'
  1152. </a>
  1153. </div>';
  1154. }
  1155. if (api_get_setting('show_link_ticket_notification') === 'true' &&
  1156. $this->user_is_logged_in
  1157. ) {
  1158. // by default is project_id = 1
  1159. $defaultProjectId = 1;
  1160. $iconTicket = Display::return_icon(
  1161. 'help.png',
  1162. get_lang('Ticket'),
  1163. [],
  1164. ICON_SIZE_LARGE
  1165. );
  1166. $courseInfo = api_get_course_info();
  1167. $courseParams = '';
  1168. if (!empty($courseInfo)) {
  1169. $courseParams = api_get_cidreq();
  1170. }
  1171. $url = api_get_path(WEB_CODE_PATH).'ticket/tickets.php?project_id='.$defaultProjectId.'&'.$courseParams;
  1172. $allow = TicketManager::userIsAllowInProject(api_get_user_info(), $defaultProjectId);
  1173. if ($allow) {
  1174. $rightFloatMenu .= '<div class="help">
  1175. <a href="'.$url.'" target="_blank">
  1176. '.$iconTicket.'
  1177. </a>
  1178. </div>';
  1179. }
  1180. }
  1181. $this->assign('bug_notification', $rightFloatMenu);
  1182. return true;
  1183. }
  1184. /**
  1185. * Prepare the _c array for template files. The _c array contains
  1186. * information about the current course.
  1187. */
  1188. private function set_course_parameters()
  1189. {
  1190. //Setting course id
  1191. $course = api_get_course_info();
  1192. if (empty($course)) {
  1193. $this->assign('course_is_set', false);
  1194. return;
  1195. }
  1196. $this->assign('course_is_set', true);
  1197. $this->course_id = $course['id'];
  1198. $_c = [
  1199. 'id' => $course['real_id'],
  1200. 'code' => $course['code'],
  1201. 'title' => $course['name'],
  1202. 'visibility' => $course['visibility'],
  1203. 'language' => $course['language'],
  1204. 'directory' => $course['directory'],
  1205. 'session_id' => api_get_session_id(),
  1206. 'user_is_teacher' => api_is_course_admin(),
  1207. 'student_view' => (!empty($_GET['isStudentView']) && $_GET['isStudentView'] == 'true'),
  1208. ];
  1209. $this->assign('course_code', $course['code']);
  1210. $this->assign('_c', $_c);
  1211. }
  1212. /**
  1213. * Prepare the _u array for template files. The _u array contains
  1214. * information about the current user, as returned by
  1215. * api_get_user_info().
  1216. */
  1217. private function set_user_parameters()
  1218. {
  1219. $user_info = [];
  1220. $user_info['logged'] = 0;
  1221. $this->user_is_logged_in = false;
  1222. if (api_user_is_login()) {
  1223. $user_info = api_get_user_info(api_get_user_id(), true);
  1224. $user_info['logged'] = 1;
  1225. $user_info['is_admin'] = 0;
  1226. if (api_is_platform_admin()) {
  1227. $user_info['is_admin'] = 1;
  1228. }
  1229. $user_info['messages_count'] = MessageManager::getCountNewMessages();
  1230. $this->user_is_logged_in = true;
  1231. }
  1232. // Setting the $_u array that could be use in any template
  1233. $this->assign('_u', $user_info);
  1234. }
  1235. /**
  1236. * Get an array of all the web paths available (e.g. 'web' => 'https://my.chamilo.site/').
  1237. *
  1238. * @return array
  1239. */
  1240. private function getWebPaths()
  1241. {
  1242. $queryString = empty($_SERVER['QUERY_STRING']) ? '' : $_SERVER['QUERY_STRING'];
  1243. $requestURI = empty($_SERVER['REQUEST_URI']) ? '' : $_SERVER['REQUEST_URI'];
  1244. return [
  1245. 'web' => api_get_path(WEB_PATH),
  1246. 'web_url' => api_get_web_url(),
  1247. 'web_relative' => api_get_path(REL_PATH),
  1248. 'web_course' => api_get_path(WEB_COURSE_PATH),
  1249. 'web_main' => api_get_path(WEB_CODE_PATH),
  1250. 'web_css' => api_get_path(WEB_CSS_PATH),
  1251. 'web_css_theme' => api_get_path(WEB_CSS_PATH).$this->themeDir,
  1252. 'web_ajax' => api_get_path(WEB_AJAX_PATH),
  1253. 'web_img' => api_get_path(WEB_IMG_PATH),
  1254. 'web_plugin' => api_get_path(WEB_PLUGIN_PATH),
  1255. 'web_lib' => api_get_path(WEB_LIBRARY_PATH),
  1256. 'web_upload' => api_get_path(WEB_UPLOAD_PATH),
  1257. 'web_self' => api_get_self(),
  1258. 'self_basename' => basename(api_get_self()),
  1259. 'web_query_vars' => api_htmlentities($queryString),
  1260. 'web_self_query_vars' => api_htmlentities($requestURI),
  1261. 'web_cid_query' => api_get_cidreq(),
  1262. 'web_rel_code' => api_get_path(REL_CODE_PATH),
  1263. ];
  1264. }
  1265. /**
  1266. * Set header parameters.
  1267. *
  1268. * @param bool $sendHeaders send headers
  1269. */
  1270. private function set_header_parameters($sendHeaders)
  1271. {
  1272. global $httpHeadXtra, $interbreadcrumb, $language_file, $_configuration, $this_section;
  1273. $_course = api_get_course_info();
  1274. $nameTools = $this->title;
  1275. $navigation = return_navigation_array();
  1276. $this->menu_navigation = $navigation['menu_navigation'];
  1277. $this->assign('system_charset', api_get_system_encoding());
  1278. if (isset($httpHeadXtra) && $httpHeadXtra) {
  1279. foreach ($httpHeadXtra as &$thisHttpHead) {
  1280. header($thisHttpHead);
  1281. }
  1282. }
  1283. // Get language iso-code for this page - ignore errors
  1284. $this->assign('document_language', api_get_language_isocode());
  1285. $course_title = isset($_course['name']) ? $_course['name'] : null;
  1286. $title_list = [];
  1287. $title_list[] = api_get_setting('Institution');
  1288. $title_list[] = api_get_setting('siteName');
  1289. if (!empty($course_title)) {
  1290. $title_list[] = $course_title;
  1291. }
  1292. if ($nameTools != '') {
  1293. $title_list[] = $nameTools;
  1294. }
  1295. $title_string = '';
  1296. for ($i = 0; $i < count($title_list); $i++) {
  1297. $title_string .= $title_list[$i];
  1298. if (isset($title_list[$i + 1])) {
  1299. $item = trim($title_list[$i + 1]);
  1300. if (!empty($item)) {
  1301. $title_string .= ' - ';
  1302. }
  1303. }
  1304. }
  1305. $this->assign('title_string', $title_string);
  1306. // Setting the theme and CSS files
  1307. $this->setCssFiles();
  1308. $this->set_js_files();
  1309. $this->setCssCustomFiles();
  1310. $browser = api_browser_support('check_browser');
  1311. if ($browser[0] == 'Internet Explorer' && $browser[1] >= '11') {
  1312. $browser_head = '<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9" />';
  1313. $this->assign('browser_specific_head', $browser_head);
  1314. }
  1315. // Implementation of prefetch.
  1316. // See http://cdn.chamilo.org/main/img/online.png for details
  1317. $prefetch = '';
  1318. if (!empty($_configuration['cdn_enable'])) {
  1319. $prefetch .= '<meta http-equiv="x-dns-prefetch-control" content="on">';
  1320. foreach ($_configuration['cdn'] as $host => $exts) {
  1321. $prefetch .= '<link rel="dns-prefetch" href="'.$host.'">';
  1322. }
  1323. }
  1324. $this->assign('prefetch', $prefetch);
  1325. $this->assign('text_direction', api_get_text_direction());
  1326. $this->assign('section_name', 'section-'.$this_section);
  1327. $this->assignFavIcon();
  1328. $this->setHelp();
  1329. $this->assignBugNotification(); //Prepare the 'bug_notification' var for the template
  1330. $this->assignAccessibilityBlock(); //Prepare the 'accessibility' var for the template
  1331. // Preparing values for the menu
  1332. // Logout link
  1333. $hideLogout = api_get_setting('hide_logout_button');
  1334. if ($hideLogout === 'true') {
  1335. $this->assign('logout_link', null);
  1336. } else {
  1337. $this->assign('logout_link', api_get_path(WEB_PATH).'index.php?logout=logout&uid='.api_get_user_id());
  1338. }
  1339. // Profile link
  1340. if (api_get_setting('allow_social_tool') == 'true') {
  1341. $profile_url = api_get_path(WEB_CODE_PATH).'social/home.php';
  1342. } else {
  1343. $profile_url = api_get_path(WEB_CODE_PATH).'auth/profile.php';
  1344. }
  1345. $this->assign('profile_url', $profile_url);
  1346. //Message link
  1347. $message_link = null;
  1348. $message_url = null;
  1349. if (api_get_setting('allow_message_tool') == 'true') {
  1350. $message_url = api_get_path(WEB_CODE_PATH).'messages/inbox.php';
  1351. $message_link = '<a href="'.api_get_path(WEB_CODE_PATH).'messages/inbox.php">'.get_lang('Inbox').'</a>';
  1352. }
  1353. $this->assign('message_link', $message_link);
  1354. $this->assign('message_url', $message_url);
  1355. $pendingSurveyLink = '';
  1356. $show = api_get_configuration_value('show_pending_survey_in_menu');
  1357. if ($show) {
  1358. $pendingSurveyLink = api_get_path(WEB_CODE_PATH).'survey/pending.php';
  1359. }
  1360. $this->assign('pending_survey_url', $pendingSurveyLink);
  1361. // Certificate Link
  1362. $allow = api_get_configuration_value('hide_my_certificate_link');
  1363. if ($allow === false) {
  1364. $certificateUrl = api_get_path(WEB_CODE_PATH).'gradebook/my_certificates.php';
  1365. $certificateLink = Display::url(
  1366. get_lang('MyCertificates'),
  1367. $certificateUrl
  1368. );
  1369. $this->assign('certificate_link', $certificateLink);
  1370. $this->assign('certificate_url', $certificateUrl);
  1371. }
  1372. $institution = api_get_setting('Institution');
  1373. $portal_name = empty($institution) ? api_get_setting('siteName') : $institution;
  1374. $this->assign('portal_name', $portal_name);
  1375. //Menu
  1376. $menu = menuArray();
  1377. $this->assign('menu', $menu);
  1378. $breadcrumb = '';
  1379. // Hide breadcrumb in LP
  1380. if ($this->show_learnpath == false) {
  1381. $breadcrumb = return_breadcrumb(
  1382. $interbreadcrumb,
  1383. $language_file,
  1384. $nameTools
  1385. );
  1386. }
  1387. $this->assign('breadcrumb', $breadcrumb);
  1388. //Extra content
  1389. $extra_header = null;
  1390. if (!api_is_platform_admin()) {
  1391. $extra_header = trim(api_get_setting('header_extra_content'));
  1392. }
  1393. $this->assign('header_extra_content', $extra_header);
  1394. if ($sendHeaders) {
  1395. header('Content-Type: text/html; charset='.api_get_system_encoding());
  1396. header(
  1397. 'X-Powered-By: '.$_configuration['software_name'].' '.substr($_configuration['system_version'], 0, 1)
  1398. );
  1399. self::addHTTPSecurityHeaders();
  1400. $responseCode = $this->getResponseCode();
  1401. if (!empty($responseCode)) {
  1402. switch ($responseCode) {
  1403. case '404':
  1404. header("HTTP/1.0 404 Not Found");
  1405. break;
  1406. }
  1407. }
  1408. }
  1409. $this->assignSocialMeta();
  1410. }
  1411. /**
  1412. * Set footer parameters.
  1413. */
  1414. private function set_footer_parameters()
  1415. {
  1416. if (api_get_setting('show_administrator_data') === 'true') {
  1417. $firstName = api_get_setting('administratorName');
  1418. $lastName = api_get_setting('administratorSurname');
  1419. if (!empty($firstName) && !empty($lastName)) {
  1420. $name = api_get_person_name($firstName, $lastName);
  1421. } else {
  1422. $name = $lastName;
  1423. if (empty($lastName)) {
  1424. $name = $firstName;
  1425. }
  1426. }
  1427. $adminName = '';
  1428. // Administrator name
  1429. if (!empty($name)) {
  1430. $adminName = get_lang('Manager').' : ';
  1431. $adminName .= Display::encrypted_mailto_link(
  1432. api_get_setting('emailAdministrator'),
  1433. $name,
  1434. '',
  1435. true
  1436. );
  1437. }
  1438. $this->assign('administrator_name', $adminName);
  1439. }
  1440. // Loading footer extra content
  1441. if (!api_is_platform_admin()) {
  1442. $extra_footer = trim(api_get_setting('footer_extra_content'));
  1443. if (!empty($extra_footer)) {
  1444. $this->assign('footer_extra_content', $extra_footer);
  1445. }
  1446. }
  1447. // Tutor name
  1448. if (api_get_setting('show_tutor_data') == 'true') {
  1449. // Course manager
  1450. $courseId = api_get_course_int_id();
  1451. $id_session = api_get_session_id();
  1452. if (!empty($courseId)) {
  1453. $tutor_data = '';
  1454. if ($id_session != 0) {
  1455. $users = SessionManager::getCoachesByCourseSession($id_session, $courseId);
  1456. $links = [];
  1457. if (!empty($users)) {
  1458. $coaches = [];
  1459. foreach ($users as $userId) {
  1460. $coaches[] = api_get_user_info($userId);
  1461. }
  1462. $links = array_column($coaches, 'complete_name_with_message_link');
  1463. }
  1464. $count = count($links);
  1465. if ($count > 1) {
  1466. $tutor_data .= get_lang('Coachs').' : ';
  1467. $tutor_data .= array_to_string($links, CourseManager::USER_SEPARATOR);
  1468. } elseif ($count === 1) {
  1469. $tutor_data .= get_lang('Coach').' : ';
  1470. $tutor_data .= array_to_string($links, CourseManager::USER_SEPARATOR);
  1471. } elseif ($count === 0) {
  1472. $tutor_data .= '';
  1473. }
  1474. }
  1475. $this->assign('session_teachers', $tutor_data);
  1476. }
  1477. }
  1478. if (api_get_setting('show_teacher_data') == 'true') {
  1479. // course manager
  1480. $courseId = api_get_course_int_id();
  1481. if (!empty($courseId)) {
  1482. $teacher_data = '';
  1483. $teachers = CourseManager::getTeachersFromCourse($courseId);
  1484. if (!empty($teachers)) {
  1485. $teachersParsed = [];
  1486. foreach ($teachers as $teacher) {
  1487. $userId = $teacher['id'];
  1488. $teachersParsed[] = api_get_user_info($userId);
  1489. }
  1490. $links = array_column($teachersParsed, 'complete_name_with_message_link');
  1491. $label = get_lang('Teacher');
  1492. if (count($links) > 1) {
  1493. $label = get_lang('Teachers');
  1494. }
  1495. $teacher_data .= $label.' : '.array_to_string($links, CourseManager::USER_SEPARATOR);
  1496. }
  1497. $this->assign('teachers', $teacher_data);
  1498. }
  1499. }
  1500. }
  1501. /**
  1502. * Set administrator variables.
  1503. */
  1504. private function setAdministratorParams()
  1505. {
  1506. $_admin = [
  1507. 'email' => api_get_setting('emailAdministrator'),
  1508. 'surname' => api_get_setting('administratorSurname'),
  1509. 'name' => api_get_setting('administratorName'),
  1510. 'telephone' => api_get_setting('administratorTelephone'),
  1511. ];
  1512. $this->assign('_admin', $_admin);
  1513. }
  1514. /**
  1515. * Manage specific HTTP headers security.
  1516. */
  1517. private function addHTTPSecurityHeaders()
  1518. {
  1519. // Implementation of HTTP headers security, as suggested and checked
  1520. // by https://securityheaders.io/
  1521. // Enable these settings in configuration.php to use them on your site
  1522. // Strict-Transport-Security
  1523. $setting = api_get_configuration_value('security_strict_transport');
  1524. if (!empty($setting)) {
  1525. header('Strict-Transport-Security: '.$setting);
  1526. }
  1527. // Content-Security-Policy
  1528. $setting = api_get_configuration_value('security_content_policy');
  1529. if (!empty($setting)) {
  1530. header('Content-Security-Policy: '.$setting);
  1531. }
  1532. $setting = api_get_configuration_value('security_content_policy_report_only');
  1533. if (!empty($setting)) {
  1534. header('Content-Security-Policy-Report-Only: '.$setting);
  1535. }
  1536. // Public-Key-Pins
  1537. $setting = api_get_configuration_value('security_public_key_pins');
  1538. if (!empty($setting)) {
  1539. header('Public-Key-Pins: '.$setting);
  1540. }
  1541. $setting = api_get_configuration_value('security_public_key_pins_report_only');
  1542. if (!empty($setting)) {
  1543. header('Public-Key-Pins-Report-Only: '.$setting);
  1544. }
  1545. // X-Frame-Options
  1546. $setting = api_get_configuration_value('security_x_frame_options');
  1547. if (!empty($setting)) {
  1548. header('X-Frame-Options: '.$setting);
  1549. }
  1550. // X-XSS-Protection
  1551. $setting = api_get_configuration_value('security_xss_protection');
  1552. if (!empty($setting)) {
  1553. header('X-XSS-Protection: '.$setting);
  1554. }
  1555. // X-Content-Type-Options
  1556. $setting = api_get_configuration_value('security_x_content_type_options');
  1557. if (!empty($setting)) {
  1558. header('X-Content-Type-Options: '.$setting);
  1559. }
  1560. // Referrer-Policy
  1561. $setting = api_get_configuration_value('security_referrer_policy');
  1562. if (!empty($setting)) {
  1563. header('Referrer-Policy: '.$setting);
  1564. }
  1565. // end of HTTP headers security block
  1566. }
  1567. /**
  1568. * Assign favicon to the 'favico' template variable.
  1569. *
  1570. * @return bool Always return true because there is always at least one correct favicon.ico
  1571. */
  1572. private function assignFavIcon()
  1573. {
  1574. // Default root chamilo favicon
  1575. $favico = '<link rel="shortcut icon" href="'.api_get_path(WEB_PATH).'favicon.ico" type="image/x-icon" />';
  1576. //Added to verify if in the current Chamilo Theme exist a favicon
  1577. $favicoThemeUrl = api_get_path(SYS_CSS_PATH).$this->themeDir.'images/';
  1578. //If exist pick the current chamilo theme favicon
  1579. if (is_file($favicoThemeUrl.'favicon.ico')) {
  1580. $favico = '<link rel="shortcut icon" href="'.api_get_path(WEB_CSS_PATH).$this->themeDir.'images/favicon.ico" type="image/x-icon" />';
  1581. }
  1582. if (api_is_multiple_url_enabled()) {
  1583. $access_url_id = api_get_current_access_url_id();
  1584. if ($access_url_id != -1) {
  1585. $url_info = api_get_access_url($access_url_id);
  1586. $url = api_remove_trailing_slash(
  1587. preg_replace('/https?:\/\//i', '', $url_info['url'])
  1588. );
  1589. $clean_url = api_replace_dangerous_char($url);
  1590. $clean_url = str_replace('/', '-', $clean_url);
  1591. $clean_url .= '/';
  1592. $homep = api_get_path(WEB_HOME_PATH).$clean_url; //homep for Home Path
  1593. $icon_real_homep = api_get_path(SYS_APP_PATH).'home/'.$clean_url;
  1594. //we create the new dir for the new sites
  1595. if (is_file($icon_real_homep.'favicon.ico')) {
  1596. $favico = '<link rel="shortcut icon" href="'.$homep.'favicon.ico" type="image/x-icon" />';
  1597. }
  1598. }
  1599. }
  1600. $this->assign('favico', $favico);
  1601. return true;
  1602. }
  1603. /**
  1604. * Assign HTML code to the 'accessibility' template variable (usually shown above top menu).
  1605. *
  1606. * @return bool Always return true (even if empty string)
  1607. */
  1608. private function assignAccessibilityBlock()
  1609. {
  1610. $resize = '';
  1611. if (api_get_setting('accessibility_font_resize') == 'true') {
  1612. $resize .= '<div class="resize_font">';
  1613. $resize .= '<div class="btn-group">';
  1614. $resize .= '<a title="'.get_lang('DecreaseFontSize').'" href="#" class="decrease_font btn btn-default"><em class="fa fa-font"></em></a>';
  1615. $resize .= '<a title="'.get_lang('ResetFontSize').'" href="#" class="reset_font btn btn-default"><em class="fa fa-font"></em></a>';
  1616. $resize .= '<a title="'.get_lang('IncreaseFontSize').'" href="#" class="increase_font btn btn-default"><em class="fa fa-font"></em></a>';
  1617. $resize .= '</div>';
  1618. $resize .= '</div>';
  1619. }
  1620. $this->assign('accessibility', $resize);
  1621. return true;
  1622. }
  1623. /**
  1624. * Assign HTML code to the 'social_meta' template variable (usually shown above top menu).
  1625. *
  1626. * @return bool Always return true (even if empty string)
  1627. */
  1628. private function assignSocialMeta()
  1629. {
  1630. $socialMeta = '';
  1631. $metaTitle = api_get_setting('meta_title');
  1632. if (!empty($metaTitle)) {
  1633. $socialMeta .= '<meta name="twitter:card" content="summary" />'."\n";
  1634. $metaSite = api_get_setting('meta_twitter_site');
  1635. if (!empty($metaSite)) {
  1636. $socialMeta .= '<meta name="twitter:site" content="'.$metaSite.'" />'."\n";
  1637. $metaCreator = api_get_setting('meta_twitter_creator');
  1638. if (!empty($metaCreator)) {
  1639. $socialMeta .= '<meta name="twitter:creator" content="'.$metaCreator.'" />'."\n";
  1640. }
  1641. }
  1642. // The user badge page emits its own meta tags, so if this is
  1643. // enabled, ignore the global ones
  1644. $userId = isset($_GET['user']) ? intval($_GET['user']) : 0;
  1645. $skillId = isset($_GET['skill']) ? intval($_GET['skill']) : 0;
  1646. if (!$userId && !$skillId) {
  1647. // no combination of user and skill ID has been defined,
  1648. // so print the normal or course-specific OpenGraph meta tags
  1649. // Check for a course ID
  1650. $courseId = api_get_course_int_id();
  1651. // Check session ID from session/id/about (see .htaccess)
  1652. $sessionId = isset($_GET['session_id']) ? intval($_GET['session_id']) : 0;
  1653. if ($courseId != false) {
  1654. // If we are inside a course (even if within a session), publish info about the course
  1655. $course = api_get_course_entity($courseId);
  1656. // @TODO: support right-to-left in title
  1657. $socialMeta .= '<meta property="og:title" content="'.$course->getTitle().' - '.$metaTitle.'" />'."\n";
  1658. $socialMeta .= '<meta property="twitter:title" content="'.$course->getTitle().' - '.$metaTitle.'" />'."\n";
  1659. $socialMeta .= '<meta property="og:url" content="'.api_get_course_url($course->getCode()).'" />'."\n";
  1660. $metaDescription = api_get_setting('meta_description');
  1661. if (!empty($course->getDescription())) {
  1662. $socialMeta .= '<meta property="og:description" content="'.strip_tags($course->getDescription()).'" />'."\n";
  1663. $socialMeta .= '<meta property="twitter:description" content="'.strip_tags($course->getDescription()).'" />'."\n";
  1664. } elseif (!empty($metaDescription)) {
  1665. $socialMeta .= '<meta property="og:description" content="'.$metaDescription.'" />'."\n";
  1666. $socialMeta .= '<meta property="twitter:description" content="'.$metaDescription.'" />'."\n";
  1667. }
  1668. $picture = CourseManager::getPicturePath($course, true);
  1669. if (!empty($picture)) {
  1670. $socialMeta .= '<meta property="og:image" content="'.$picture.'" />'."\n";
  1671. $socialMeta .= '<meta property="twitter:image" content="'.$picture.'" />'."\n";
  1672. $socialMeta .= '<meta property="twitter:image:alt" content="'.$course->getTitle().' - '.$metaTitle.'" />'."\n";
  1673. } else {
  1674. $socialMeta .= $this->getMetaPortalImagePath($metaTitle);
  1675. }
  1676. } elseif ($sessionId !== 0) {
  1677. // If we are on a session "about" screen, publish info about the session
  1678. $em = Database::getManager();
  1679. $session = $em->find('ChamiloCoreBundle:Session', $sessionId);
  1680. $socialMeta .= '<meta property="og:title" content="'.$session->getName().' - '.$metaTitle.'" />'."\n";
  1681. $socialMeta .= '<meta property="twitter:title" content="'.$session->getName().' - '.$metaTitle.'" />'."\n";
  1682. $socialMeta .= '<meta property="og:url" content="'.api_get_path(WEB_PATH)."session/{$session->getId()}/about/".'" />'."\n";
  1683. $sessionValues = new ExtraFieldValue('session');
  1684. $sessionImage = $sessionValues->get_values_by_handler_and_field_variable($session->getId(), 'image')['value'];
  1685. $sessionImageSysPath = api_get_path(SYS_UPLOAD_PATH).$sessionImage;
  1686. if (!empty($sessionImage) && is_file($sessionImageSysPath)) {
  1687. $sessionImagePath = api_get_path(WEB_UPLOAD_PATH).$sessionImage;
  1688. $socialMeta .= '<meta property="og:image" content="'.$sessionImagePath.'" />'."\n";
  1689. $socialMeta .= '<meta property="twitter:image" content="'.$sessionImagePath.'" />'."\n";
  1690. $socialMeta .= '<meta property="twitter:image:alt" content="'.$session->getName().' - '.$metaTitle.'" />'."\n";
  1691. } else {
  1692. $socialMeta .= $this->getMetaPortalImagePath($metaTitle);
  1693. }
  1694. } else {
  1695. // Otherwise (not a course nor a session, nor a user, nor a badge), publish portal info
  1696. $socialMeta .= '<meta property="og:title" content="'.$metaTitle.'" />'."\n";
  1697. $socialMeta .= '<meta property="twitter:title" content="'.$metaTitle.'" />'."\n";
  1698. $socialMeta .= '<meta property="og:url" content="'.api_get_path(WEB_PATH).'" />'."\n";
  1699. $metaDescription = api_get_setting('meta_description');
  1700. if (!empty($metaDescription)) {
  1701. $socialMeta .= '<meta property="og:description" content="'.$metaDescription.'" />'."\n";
  1702. $socialMeta .= '<meta property="twitter:description" content="'.$metaDescription.'" />'."\n";
  1703. }
  1704. $socialMeta .= $this->getMetaPortalImagePath($metaTitle);
  1705. }
  1706. }
  1707. }
  1708. $this->assign('social_meta', $socialMeta);
  1709. return true;
  1710. }
  1711. /**
  1712. * Get platform meta image tag (check meta_image_path setting, then use the logo).
  1713. *
  1714. * @param string $imageAlt The alt attribute for the image
  1715. *
  1716. * @return string The meta image HTML tag, or empty
  1717. */
  1718. private function getMetaPortalImagePath($imageAlt = '')
  1719. {
  1720. // Load portal meta image if defined
  1721. $metaImage = api_get_setting('meta_image_path');
  1722. $metaImageSysPath = api_get_path(SYS_PATH).$metaImage;
  1723. $metaImageWebPath = api_get_path(WEB_PATH).$metaImage;
  1724. $portalImageMeta = '';
  1725. if (!empty($metaImage)) {
  1726. if (is_file($metaImageSysPath)) {
  1727. $portalImageMeta = '<meta property="og:image" content="'.$metaImageWebPath.'" />'."\n";
  1728. $portalImageMeta .= '<meta property="twitter:image" content="'.$metaImageWebPath.'" />'."\n";
  1729. $portalImageMeta .= '<meta property="twitter:image:alt" content="'.$imageAlt.'" />'."\n";
  1730. }
  1731. } else {
  1732. $logo = ChamiloApi::getPlatformLogoPath($this->theme);
  1733. if (!empty($logo)) {
  1734. $portalImageMeta = '<meta property="og:image" content="'.$logo.'" />'."\n";
  1735. $portalImageMeta .= '<meta property="twitter:image" content="'.$logo.'" />'."\n";
  1736. $portalImageMeta .= '<meta property="twitter:image:alt" content="'.$imageAlt.'" />'."\n";
  1737. }
  1738. }
  1739. return $portalImageMeta;
  1740. }
  1741. }