template.lib.php 70 KB

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