display.lib.php 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
  4. use Chamilo\CoreBundle\Entity\ExtraField;
  5. use Chamilo\CoreBundle\Framework\Container;
  6. use ChamiloSession as Session;
  7. use Symfony\Component\HttpFoundation\Response;
  8. /**
  9. * Class Display
  10. * Contains several public functions dealing with the display of
  11. * table data, messages, help topics, ...
  12. *
  13. * Include/require it in your code to use its public functionality.
  14. * There are also several display public functions in the main api library.
  15. *
  16. * All public functions static public functions inside a class called Display,
  17. * so you use them like this: e.g.
  18. * Display::return_message($message)
  19. *
  20. * @package chamilo.library
  21. */
  22. class Display
  23. {
  24. /** @var Template */
  25. public static $global_template;
  26. public static $preview_style = null;
  27. /**
  28. * Constructor.
  29. */
  30. public function __construct()
  31. {
  32. }
  33. /**
  34. * @return array
  35. */
  36. public static function toolList()
  37. {
  38. return [
  39. 'group',
  40. 'work',
  41. 'glossary',
  42. 'forum',
  43. 'course_description',
  44. 'gradebook',
  45. 'attendance',
  46. 'course_progress',
  47. 'notebook',
  48. ];
  49. }
  50. /**
  51. * Displays the page header.
  52. *
  53. * @param string The name of the page (will be showed in the page title)
  54. * @param string Optional help file name
  55. * @param string $page_header
  56. */
  57. public static function display_header(
  58. $tool_name = '',
  59. $help = null,
  60. $page_header = null
  61. ) {
  62. ob_start();
  63. return true;
  64. $origin = api_get_origin();
  65. $showHeader = true;
  66. if (isset($origin) && $origin == 'learnpath') {
  67. $showHeader = false;
  68. }
  69. /* USER_IN_ANON_SURVEY is defined in fillsurvey.php when survey is marked as anonymous survey */
  70. $userInAnonSurvey = defined('USER_IN_ANON_SURVEY') && USER_IN_ANON_SURVEY;
  71. self::$global_template = new Template($tool_name, $showHeader, $showHeader, false, $userInAnonSurvey);
  72. self::$global_template->assign('user_in_anon_survey', $userInAnonSurvey);
  73. // Fixing tools with any help it takes xxx part of main/xxx/index.php
  74. if (empty($help)) {
  75. $currentURL = api_get_self();
  76. preg_match('/main\/([^*\/]+)/', $currentURL, $matches);
  77. $toolList = self::toolList();
  78. if (!empty($matches)) {
  79. foreach ($matches as $match) {
  80. if (in_array($match, $toolList)) {
  81. $help = explode('_', $match);
  82. $help = array_map('ucfirst', $help);
  83. $help = implode('', $help);
  84. break;
  85. }
  86. }
  87. }
  88. }
  89. self::$global_template->setHelp($help);
  90. if (!empty(self::$preview_style)) {
  91. self::$global_template->preview_theme = self::$preview_style;
  92. self::$global_template->set_system_parameters();
  93. self::$global_template->setCssFiles();
  94. self::$global_template->set_js_files();
  95. self::$global_template->setCssCustomFiles();
  96. }
  97. if (!empty($page_header)) {
  98. self::$global_template->assign('header', $page_header);
  99. }
  100. echo self::$global_template->show_header_template();
  101. }
  102. /**
  103. * Displays the reduced page header (without banner).
  104. */
  105. public static function display_reduced_header()
  106. {
  107. global $show_learnpath, $tool_name;
  108. self::$global_template = new Template(
  109. $tool_name,
  110. false,
  111. false,
  112. $show_learnpath
  113. );
  114. echo self::$global_template->show_header_template();
  115. }
  116. /**
  117. * Display no header.
  118. */
  119. public static function display_no_header()
  120. {
  121. global $tool_name, $show_learnpath;
  122. $disable_js_and_css_files = true;
  123. self::$global_template = new Template(
  124. $tool_name,
  125. false,
  126. false,
  127. $show_learnpath
  128. );
  129. }
  130. /**
  131. * Display the page footer.
  132. */
  133. public static function display_footer()
  134. {
  135. $contents = ob_get_contents();
  136. if (ob_get_length()) {
  137. ob_end_clean();
  138. }
  139. $tpl = '@ChamiloTheme/Layout/layout_one_col.html.twig';
  140. $response = new Response();
  141. $params['content'] = $contents;
  142. global $interbreadcrumb, $htmlHeadXtra;
  143. $params['legacy_javascript'] = $htmlHeadXtra;
  144. $params['legacy_breadcrumb'] = $interbreadcrumb;
  145. $flash = Display::getFlashToString();
  146. Display::cleanFlashMessages();
  147. $params['flash_messages'] = $flash;
  148. $content = Container::getTemplating()->render($tpl, $params);
  149. $response->setContent($content);
  150. $response->send();
  151. exit;
  152. }
  153. /**
  154. * Display the page footer.
  155. */
  156. public static function display_reduced_footer()
  157. {
  158. echo self::$global_template->show_footer_js_template();
  159. echo '</body></html>';
  160. }
  161. /**
  162. * Displays the tool introduction of a tool.
  163. *
  164. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  165. *
  166. * @param string $tool these are the constants that are used for indicating the tools
  167. * @param array $editor_config Optional configuration settings for the online editor.
  168. * return: $tool return a string array list with the "define" in main_api.lib
  169. *
  170. * @return string html code for adding an introduction
  171. */
  172. public static function display_introduction_section(
  173. $tool,
  174. $editor_config = null
  175. ) {
  176. echo self::return_introduction_section($tool, $editor_config);
  177. }
  178. /**
  179. * @param string $tool
  180. * @param array $editor_config
  181. */
  182. public static function return_introduction_section(
  183. $tool,
  184. $editor_config = null
  185. ) {
  186. $moduleId = $tool;
  187. if (api_get_setting('enable_tool_introduction') == 'true' || $tool == TOOL_COURSE_HOMEPAGE) {
  188. $introduction_section = null;
  189. require api_get_path(SYS_INC_PATH).'introductionSection.inc.php';
  190. return $introduction_section;
  191. }
  192. }
  193. /**
  194. * Displays a table.
  195. *
  196. * @param array $header Titles for the table header
  197. * each item in this array can contain 3 values
  198. * - 1st element: the column title
  199. * - 2nd element: true or false (column sortable?)
  200. * - 3th element: additional attributes for
  201. * th-tag (eg for column-width)
  202. * - 4the element: additional attributes for the td-tags
  203. * @param array $content 2D-array with the tables content
  204. * @param array $sorting_options Keys are:
  205. * 'column' = The column to use as sort-key
  206. * 'direction' = SORT_ASC or SORT_DESC
  207. * @param array $paging_options Keys are:
  208. * 'per_page_default' = items per page when switching from
  209. * full- list to per-page-view
  210. * 'per_page' = number of items to show per page
  211. * 'page_nr' = The page to display
  212. * @param array $query_vars Additional variables to add in the query-string
  213. * @param array $form_actions
  214. * @param string $style The style that the table will show. You can set 'table' or 'grid'
  215. * @param string $tableName
  216. * @param string $tableId
  217. *
  218. * @author bart.mollet@hogent.be
  219. */
  220. public static function display_sortable_table(
  221. $header,
  222. $content,
  223. $sorting_options = [],
  224. $paging_options = [],
  225. $query_vars = null,
  226. $form_actions = [],
  227. $style = 'table',
  228. $tableName = 'tablename',
  229. $tableId = ''
  230. ) {
  231. $column = isset($sorting_options['column']) ? $sorting_options['column'] : 0;
  232. $default_items_per_page = isset($paging_options['per_page']) ? $paging_options['per_page'] : 20;
  233. $table = new SortableTableFromArray($content, $column, $default_items_per_page, $tableName, null, $tableId);
  234. if (is_array($query_vars)) {
  235. $table->set_additional_parameters($query_vars);
  236. }
  237. if ($style == 'table') {
  238. if (is_array($header) && count($header) > 0) {
  239. foreach ($header as $index => $header_item) {
  240. $table->set_header(
  241. $index,
  242. isset($header_item[0]) ? $header_item[0] : null,
  243. isset($header_item[1]) ? $header_item[1] : null,
  244. isset($header_item[2]) ? $header_item[2] : null,
  245. isset($header_item[3]) ? $header_item[3] : null
  246. );
  247. }
  248. }
  249. $table->set_form_actions($form_actions);
  250. $table->display();
  251. } else {
  252. $table->display_grid();
  253. }
  254. }
  255. /**
  256. * Returns an HTML table with sortable column (through complete page refresh).
  257. *
  258. * @param array $header
  259. * @param array $content Array of row arrays
  260. * @param array $sorting_options
  261. * @param array $paging_options
  262. * @param array $query_vars
  263. * @param array $form_actions
  264. * @param string $style
  265. *
  266. * @return string HTML string for array
  267. */
  268. public static function return_sortable_table(
  269. $header,
  270. $content,
  271. $sorting_options = [],
  272. $paging_options = [],
  273. $query_vars = null,
  274. $form_actions = [],
  275. $style = 'table'
  276. ) {
  277. ob_start();
  278. self::display_sortable_table(
  279. $header,
  280. $content,
  281. $sorting_options,
  282. $paging_options,
  283. $query_vars,
  284. $form_actions,
  285. $style
  286. );
  287. $content = ob_get_contents();
  288. ob_end_clean();
  289. return $content;
  290. }
  291. /**
  292. * Shows a nice grid.
  293. *
  294. * @param string grid name (important to create css)
  295. * @param array header content
  296. * @param array array with the information to show
  297. * @param array $paging_options Keys are:
  298. * 'per_page_default' = items per page when switching from
  299. * full- list to per-page-view
  300. * 'per_page' = number of items to show per page
  301. * 'page_nr' = The page to display
  302. * 'hide_navigation' = true to hide the navigation
  303. * @param array $query_vars Additional variables to add in the query-string
  304. * @param array $form actions Additional variables to add in the query-string
  305. * @param mixed An array with bool values to know which columns show.
  306. * i.e: $visibility_options= array(true, false) we will only show the first column
  307. * Can be also only a bool value. TRUE: show all columns, FALSE: show nothing
  308. */
  309. public static function display_sortable_grid(
  310. $name,
  311. $header,
  312. $content,
  313. $paging_options = [],
  314. $query_vars = null,
  315. $form_actions = [],
  316. $visibility_options = true,
  317. $sort_data = true,
  318. $grid_class = []
  319. ) {
  320. echo self::return_sortable_grid(
  321. $name,
  322. $header,
  323. $content,
  324. $paging_options,
  325. $query_vars,
  326. $form_actions,
  327. $visibility_options,
  328. $sort_data,
  329. $grid_class
  330. );
  331. }
  332. /**
  333. * Gets a nice grid in html string.
  334. *
  335. * @param string grid name (important to create css)
  336. * @param array header content
  337. * @param array array with the information to show
  338. * @param array $paging_options Keys are:
  339. * 'per_page_default' = items per page when switching from
  340. * full- list to per-page-view
  341. * 'per_page' = number of items to show per page
  342. * 'page_nr' = The page to display
  343. * 'hide_navigation' = true to hide the navigation
  344. * @param array $query_vars Additional variables to add in the query-string
  345. * @param array $form actions Additional variables to add in the query-string
  346. * @param mixed An array with bool values to know which columns show. i.e:
  347. * $visibility_options= array(true, false) we will only show the first column
  348. * Can be also only a bool value. TRUE: show all columns, FALSE: show nothing
  349. * @param bool true for sorting data or false otherwise
  350. * @param array grid classes
  351. *
  352. * @return string html grid
  353. */
  354. public static function return_sortable_grid(
  355. $name,
  356. $header,
  357. $content,
  358. $paging_options = [],
  359. $query_vars = null,
  360. $form_actions = [],
  361. $visibility_options = true,
  362. $sort_data = true,
  363. $grid_class = [],
  364. $elementCount = 0
  365. ) {
  366. $column = 0;
  367. $default_items_per_page = isset($paging_options['per_page']) ? $paging_options['per_page'] : 20;
  368. $table = new SortableTableFromArray($content, $column, $default_items_per_page, $name);
  369. $table->total_number_of_items = intval($elementCount);
  370. if (is_array($query_vars)) {
  371. $table->set_additional_parameters($query_vars);
  372. }
  373. return $table->display_simple_grid(
  374. $visibility_options,
  375. $paging_options['hide_navigation'],
  376. $default_items_per_page,
  377. $sort_data,
  378. $grid_class
  379. );
  380. }
  381. /**
  382. * Displays a table with a special configuration.
  383. *
  384. * @param array $header Titles for the table header
  385. * each item in this array can contain 3 values
  386. * - 1st element: the column title
  387. * - 2nd element: true or false (column sortable?)
  388. * - 3th element: additional attributes for th-tag (eg for column-width)
  389. * - 4the element: additional attributes for the td-tags
  390. * @param array $content 2D-array with the tables content
  391. * @param array $sorting_options Keys are:
  392. * 'column' = The column to use as sort-key
  393. * 'direction' = SORT_ASC or SORT_DESC
  394. * @param array $paging_options Keys are:
  395. * 'per_page_default' = items per page when switching from full list to per-page-view
  396. * 'per_page' = number of items to show per page
  397. * 'page_nr' = The page to display
  398. * @param array $query_vars Additional variables to add in the query-string
  399. * @param array $column_show Array of binaries 1= show columns 0. hide a column
  400. * @param array $column_order An array of integers that let us decide how the columns are going to be sort.
  401. * i.e: $column_order=array('1''4','3','4'); The 2nd column will be order like the 4th column
  402. * @param array $form_actions Set optional forms actions
  403. *
  404. * @author Julio Montoya
  405. */
  406. public static function display_sortable_config_table(
  407. $table_name,
  408. $header,
  409. $content,
  410. $sorting_options = [],
  411. $paging_options = [],
  412. $query_vars = null,
  413. $column_show = [],
  414. $column_order = [],
  415. $form_actions = []
  416. ) {
  417. $column = isset($sorting_options['column']) ? $sorting_options['column'] : 0;
  418. $default_items_per_page = isset($paging_options['per_page']) ? $paging_options['per_page'] : 20;
  419. $table = new SortableTableFromArrayConfig(
  420. $content,
  421. $column,
  422. $default_items_per_page,
  423. $table_name,
  424. $column_show,
  425. $column_order
  426. );
  427. if (is_array($query_vars)) {
  428. $table->set_additional_parameters($query_vars);
  429. }
  430. // Show or hide the columns header
  431. if (is_array($column_show)) {
  432. for ($i = 0; $i < count($column_show); $i++) {
  433. if (!empty($column_show[$i])) {
  434. $val0 = isset($header[$i][0]) ? $header[$i][0] : null;
  435. $val1 = isset($header[$i][1]) ? $header[$i][1] : null;
  436. $val2 = isset($header[$i][2]) ? $header[$i][2] : null;
  437. $val3 = isset($header[$i][3]) ? $header[$i][3] : null;
  438. $table->set_header($i, $val0, $val1, $val2, $val3);
  439. }
  440. }
  441. }
  442. $table->set_form_actions($form_actions);
  443. $table->display();
  444. }
  445. /**
  446. * Returns a div html string with.
  447. *
  448. * @param string $message
  449. * @param string $type Example: confirm, normal, warning, error
  450. * @param bool $filter Whether to XSS-filter or not
  451. *
  452. * @return string Message wrapped into an HTML div
  453. */
  454. public static function return_message(
  455. $message,
  456. $type = 'normal',
  457. $filter = true
  458. ) {
  459. if (empty($message)) {
  460. return '';
  461. }
  462. if ($filter) {
  463. $message = api_htmlentities(
  464. $message,
  465. ENT_QUOTES,
  466. api_is_xml_http_request() ? 'UTF-8' : api_get_system_encoding()
  467. );
  468. }
  469. $class = '';
  470. switch ($type) {
  471. case 'warning':
  472. $class .= 'alert alert-warning';
  473. break;
  474. case 'error':
  475. $class .= 'alert alert-danger';
  476. break;
  477. case 'confirmation':
  478. case 'confirm':
  479. case 'success':
  480. $class .= 'alert alert-success';
  481. break;
  482. case 'normal':
  483. default:
  484. $class .= 'alert alert-info';
  485. }
  486. return self::div($message, ['class' => $class]);
  487. }
  488. /**
  489. * Returns an encrypted mailto hyperlink.
  490. *
  491. * @param string e-mail
  492. * @param string clickable text
  493. * @param string optional, class from stylesheet
  494. * @param bool $addExtraContent
  495. *
  496. * @return string encrypted mailto hyperlink
  497. */
  498. public static function encrypted_mailto_link(
  499. $email,
  500. $clickable_text = null,
  501. $style_class = '',
  502. $addExtraContent = false
  503. ) {
  504. if (is_null($clickable_text)) {
  505. $clickable_text = $email;
  506. }
  507. // "mailto:" already present?
  508. if (substr($email, 0, 7) !== 'mailto:') {
  509. $email = 'mailto:'.$email;
  510. }
  511. // Class (stylesheet) defined?
  512. if ($style_class !== '') {
  513. $style_class = ' class="'.$style_class.'"';
  514. }
  515. // Encrypt email
  516. $hmail = '';
  517. for ($i = 0; $i < strlen($email); $i++) {
  518. $hmail .= '&#'.ord($email[$i]).';';
  519. }
  520. $value = api_get_configuration_value('add_user_course_information_in_mailto');
  521. if ($value) {
  522. if (api_get_setting('allow_email_editor') === 'false') {
  523. $hmail .= '?';
  524. }
  525. if (!api_is_anonymous()) {
  526. $hmail .= '&subject='.Security::remove_XSS(api_get_setting('siteName'));
  527. }
  528. if ($addExtraContent) {
  529. $content = '';
  530. if (!api_is_anonymous()) {
  531. $userInfo = api_get_user_info();
  532. $content .= get_lang('User').': '.$userInfo['complete_name']."\n";
  533. $courseInfo = api_get_course_info();
  534. if (!empty($courseInfo)) {
  535. $content .= get_lang('Course').': ';
  536. $content .= $courseInfo['name'];
  537. $sessionInfo = api_get_session_info(api_get_session_id());
  538. if (!empty($sessionInfo)) {
  539. $content .= ' '.$sessionInfo['name'].' <br />';
  540. }
  541. }
  542. }
  543. $hmail .= '&body='.rawurlencode($content);
  544. }
  545. }
  546. $hclickable_text = '';
  547. // Encrypt clickable text if @ is present
  548. if (strpos($clickable_text, '@')) {
  549. for ($i = 0; $i < strlen($clickable_text); $i++) {
  550. $hclickable_text .= '&#'.ord($clickable_text[$i]).';';
  551. }
  552. } else {
  553. $hclickable_text = @htmlspecialchars(
  554. $clickable_text,
  555. ENT_QUOTES,
  556. api_get_system_encoding()
  557. );
  558. }
  559. // Return encrypted mailto hyperlink
  560. return '<a href="'.$hmail.'"'.$style_class.' class="clickable_email_link">'.$hclickable_text.'</a>';
  561. }
  562. /**
  563. * Returns an mailto icon hyperlink.
  564. *
  565. * @param string e-mail
  566. * @param string icon source file from the icon lib
  567. * @param int icon size from icon lib
  568. * @param string optional, class from stylesheet
  569. *
  570. * @return string encrypted mailto hyperlink
  571. */
  572. public static function icon_mailto_link(
  573. $email,
  574. $icon_file = "mail.png",
  575. $icon_size = 22,
  576. $style_class = ''
  577. ) {
  578. // "mailto:" already present?
  579. if (substr($email, 0, 7) != 'mailto:') {
  580. $email = 'mailto:'.$email;
  581. }
  582. // Class (stylesheet) defined?
  583. if ($style_class != '') {
  584. $style_class = ' class="'.$style_class.'"';
  585. }
  586. // Encrypt email
  587. $hmail = '';
  588. for ($i = 0; $i < strlen($email); $i++) {
  589. $hmail .= '&#'.ord($email[
  590. $i]).';';
  591. }
  592. // icon html code
  593. $icon_html_source = self::return_icon(
  594. $icon_file,
  595. $hmail,
  596. '',
  597. $icon_size
  598. );
  599. // Return encrypted mailto hyperlink
  600. return '<a href="'.$hmail.'"'.$style_class.' class="clickable_email_link">'.$icon_html_source.'</a>';
  601. }
  602. /**
  603. * Prints an <option>-list with all letters (A-Z).
  604. *
  605. * @param string $selected_letter The letter that should be selected
  606. *
  607. * @todo This is English language specific implementation.
  608. * It should be adapted for the other languages.
  609. *
  610. * @return string
  611. */
  612. public static function get_alphabet_options($selectedLetter = '')
  613. {
  614. $result = '';
  615. for ($i = 65; $i <= 90; $i++) {
  616. $letter = chr($i);
  617. $result .= '<option value="'.$letter.'"';
  618. if ($selectedLetter == $letter) {
  619. $result .= ' selected="selected"';
  620. }
  621. $result .= '>'.$letter.'</option>';
  622. }
  623. return $result;
  624. }
  625. /**
  626. * Get the options withing a select box within the given values.
  627. *
  628. * @param int Min value
  629. * @param int Max value
  630. * @param int Default value
  631. *
  632. * @return string HTML select options
  633. */
  634. public static function get_numeric_options($min, $max, $selected_num = 0)
  635. {
  636. $result = '';
  637. for ($i = $min; $i <= $max; $i++) {
  638. $result .= '<option value="'.$i.'"';
  639. if (is_int($selected_num)) {
  640. if ($selected_num == $i) {
  641. $result .= ' selected="selected"';
  642. }
  643. }
  644. $result .= '>'.$i.'</option>';
  645. }
  646. return $result;
  647. }
  648. /**
  649. * This public function displays an icon.
  650. *
  651. * @param string The filename of the file (in the main/img/ folder
  652. * @param string The alt text (probably a language variable)
  653. * @param array additional attributes (for instance height, width, onclick, ...)
  654. * @param int The wanted width of the icon (to be looked for in the corresponding img/icons/ folder)
  655. */
  656. public static function display_icon(
  657. $image,
  658. $alt_text = '',
  659. $additional_attributes = [],
  660. $size = null
  661. ) {
  662. echo self::return_icon($image, $alt_text, $additional_attributes, $size);
  663. }
  664. /**
  665. * Gets the path of an icon.
  666. *
  667. * @param string $icon
  668. * @param int $size
  669. *
  670. * @return string
  671. */
  672. public static function returnIconPath($icon, $size = ICON_SIZE_SMALL)
  673. {
  674. return self::return_icon($icon, null, null, $size, null, true, false);
  675. }
  676. /**
  677. * This public function returns the htmlcode for an icon.
  678. *
  679. * @param string The filename of the file (in the main/img/ folder
  680. * @param string The alt text (probably a language variable)
  681. * @param array Additional attributes (for instance height, width, onclick, ...)
  682. * @param int The wanted width of the icon (to be looked for in the corresponding img/icons/ folder)
  683. *
  684. * @return string An HTML string of the right <img> tag
  685. *
  686. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University 2006
  687. * @author Julio Montoya 2010 Function improved, adding image constants
  688. * @author Yannick Warnier 2011 Added size handler
  689. *
  690. * @version Feb 2011
  691. */
  692. public static function return_icon(
  693. $image,
  694. $alt_text = '',
  695. $additional_attributes = [],
  696. $size = ICON_SIZE_SMALL,
  697. $show_text = true,
  698. $return_only_path = false,
  699. $loadThemeIcon = true
  700. ) {
  701. $code_path = api_get_path(SYS_PUBLIC_PATH);
  702. $w_code_path = api_get_path(WEB_PUBLIC_PATH);
  703. // The following path is checked to see if the file exist. It's
  704. // important to use the public path (i.e. web/css/) rather than the
  705. // internal path (/app/Resource/public/css/) because the path used
  706. // in the end must be the public path
  707. $alternateCssPath = api_get_path(SYS_PUBLIC_PATH).'css/';
  708. $alternateWebCssPath = api_get_path(WEB_PUBLIC_PATH).'css/';
  709. // Avoid issues with illegal string offset for legacy calls to this
  710. // method with an empty string rather than null or an empty array
  711. if (empty($additional_attributes)) {
  712. $additional_attributes = [];
  713. }
  714. $image = trim($image);
  715. if (isset($size)) {
  716. $size = intval($size);
  717. } else {
  718. $size = ICON_SIZE_SMALL;
  719. }
  720. $size_extra = $size.'/';
  721. $icon = $w_code_path.'img/'.$image;
  722. $theme = 'themes/chamilo/icons/';
  723. if ($loadThemeIcon) {
  724. // @todo with chamilo 2 code
  725. $theme = 'themes/'.api_get_visual_theme().'/icons/';
  726. if (is_file($alternateCssPath.$theme.$image)) {
  727. $icon = $alternateWebCssPath.$theme.$image;
  728. }
  729. // Checking the theme icons folder example: app/Resources/public/css/themes/chamilo/icons/XXX
  730. if (is_file($alternateCssPath.$theme.$size_extra.$image)) {
  731. $icon = $alternateWebCssPath.$theme.$size_extra.$image;
  732. } elseif (is_file($code_path.'img/icons/'.$size_extra.$image)) {
  733. //Checking the main/img/icons/XXX/ folder
  734. $icon = $w_code_path.'img/icons/'.$size_extra.$image;
  735. }
  736. } else {
  737. if (is_file($code_path.'img/icons/'.$size_extra.$image)) {
  738. // Checking the main/img/icons/XXX/ folder
  739. $icon = $w_code_path.'img/icons/'.$size_extra.$image;
  740. }
  741. }
  742. // Special code to enable SVG - refs #7359 - Needs more work
  743. // The code below does something else to "test out" SVG: for each icon,
  744. // it checks if there is an SVG version. If so, it uses it.
  745. // When moving this to production, the return_icon() calls should
  746. // ask for the SVG version directly
  747. $svgIcons = api_get_setting('icons_mode_svg');
  748. if ($svgIcons == 'true' && $return_only_path == false) {
  749. $svgImage = substr($image, 0, -3).'svg';
  750. if (is_file($code_path.$theme.'svg/'.$svgImage)) {
  751. $icon = $w_code_path.$theme.'svg/'.$svgImage;
  752. } elseif (is_file($code_path.'img/icons/svg/'.$svgImage)) {
  753. $icon = $w_code_path.'img/icons/svg/'.$svgImage;
  754. }
  755. if (empty($additional_attributes['height'])) {
  756. $additional_attributes['height'] = $size;
  757. }
  758. if (empty($additional_attributes['width'])) {
  759. $additional_attributes['width'] = $size;
  760. }
  761. }
  762. if ($return_only_path) {
  763. return $icon;
  764. }
  765. $img = self::img($icon, $alt_text, $additional_attributes);
  766. if (SHOW_TEXT_NEAR_ICONS == true && !empty($alt_text)) {
  767. if ($show_text) {
  768. $img = "$img $alt_text";
  769. }
  770. }
  771. return $img;
  772. }
  773. /**
  774. * Returns the htmlcode for an image.
  775. *
  776. * @param string $image_path the filename of the file (in the main/img/ folder
  777. * @param string $alt_text the alt text (probably a language variable)
  778. * @param array $additional_attributes (for instance height, width, onclick, ...)
  779. * @param bool $filterPath Optional. Whether filter the image path. Default is true
  780. *
  781. * @return string
  782. *
  783. * @author Julio Montoya 2010
  784. */
  785. public static function img(
  786. $image_path,
  787. $alt_text = '',
  788. $additional_attributes = null,
  789. $filterPath = true
  790. ) {
  791. if (empty($image_path)) {
  792. // For some reason, the call to img() happened without a proper
  793. // image. Log the error and return an empty string to avoid
  794. // breaking the HTML
  795. $trace = debug_backtrace();
  796. $caller = $trace[1];
  797. //error_log('No image provided in Display::img(). Caller info: '.print_r($caller, 1));
  798. return '';
  799. }
  800. // Sanitizing the parameter $image_path
  801. if ($filterPath) {
  802. $image_path = Security::filter_img_path($image_path);
  803. }
  804. // alt text = the image name if there is none provided (for XHTML compliance)
  805. if ($alt_text == '') {
  806. $alt_text = basename($image_path);
  807. }
  808. if (empty($additional_attributes)) {
  809. $additional_attributes = [];
  810. }
  811. $additional_attributes['src'] = $image_path;
  812. if (empty($additional_attributes['alt'])) {
  813. $additional_attributes['alt'] = $alt_text;
  814. }
  815. if (empty($additional_attributes['title'])) {
  816. $additional_attributes['title'] = $alt_text;
  817. }
  818. return self::tag('img', '', $additional_attributes);
  819. }
  820. /**
  821. * Returns the htmlcode for a tag (h3, h1, div, a, button), etc.
  822. *
  823. * @param string $tag the tag name
  824. * @param string $content the tag's content
  825. * @param array $additional_attributes (for instance height, width, onclick, ...)
  826. *
  827. * @return string
  828. *
  829. * @author Julio Montoya 2010
  830. */
  831. public static function tag($tag, $content, $additional_attributes = [])
  832. {
  833. $attribute_list = '';
  834. // Managing the additional attributes
  835. if (!empty($additional_attributes) && is_array($additional_attributes)) {
  836. $attribute_list = '';
  837. foreach ($additional_attributes as $key => &$value) {
  838. $attribute_list .= $key.'="'.$value.'" ';
  839. }
  840. }
  841. //some tags don't have this </XXX>
  842. if (in_array($tag, ['img', 'input', 'br'])) {
  843. $return_value = '<'.$tag.' '.$attribute_list.' />';
  844. } else {
  845. $return_value = '<'.$tag.' '.$attribute_list.' >'.$content.'</'.$tag.'>';
  846. }
  847. return $return_value;
  848. }
  849. /**
  850. * Creates a URL anchor.
  851. *
  852. * @param string $name
  853. * @param string $url
  854. * @param array $attributes
  855. *
  856. * @return string
  857. */
  858. public static function url($name, $url, $attributes = [])
  859. {
  860. if (!empty($url)) {
  861. $url = preg_replace('#&amp;#', '&', $url);
  862. $url = htmlspecialchars($url, ENT_QUOTES, 'UTF-8');
  863. $attributes['href'] = $url;
  864. }
  865. return self::tag('a', $name, $attributes);
  866. }
  867. /**
  868. * Creates a div tag.
  869. *
  870. * @param string $content
  871. * @param array $attributes
  872. *
  873. * @return string
  874. */
  875. public static function div($content, $attributes = [])
  876. {
  877. return self::tag('div', $content, $attributes);
  878. }
  879. /**
  880. * Creates a span tag.
  881. */
  882. public static function span($content, $attributes = [])
  883. {
  884. return self::tag('span', $content, $attributes);
  885. }
  886. /**
  887. * Displays an HTML input tag.
  888. */
  889. public static function input($type, $name, $value, $attributes = [])
  890. {
  891. if (isset($type)) {
  892. $attributes['type'] = $type;
  893. }
  894. if (isset($name)) {
  895. $attributes['name'] = $name;
  896. }
  897. if (isset($value)) {
  898. $attributes['value'] = $value;
  899. }
  900. return self::tag('input', '', $attributes);
  901. }
  902. /**
  903. * @param $name
  904. * @param $value
  905. * @param array $attributes
  906. *
  907. * @return string
  908. */
  909. public static function button($name, $value, $attributes = [])
  910. {
  911. if (!empty($name)) {
  912. $attributes['name'] = $name;
  913. }
  914. return self::tag('button', $value, $attributes);
  915. }
  916. /**
  917. * Displays an HTML select tag.
  918. *
  919. * @param string $name
  920. * @param array $values
  921. * @param int $default
  922. * @param array $extra_attributes
  923. * @param bool $show_blank_item
  924. * @param null $blank_item_text
  925. *
  926. * @return string
  927. */
  928. public static function select(
  929. $name,
  930. $values,
  931. $default = -1,
  932. $extra_attributes = [],
  933. $show_blank_item = true,
  934. $blank_item_text = ''
  935. ) {
  936. $html = '';
  937. $extra = '';
  938. $default_id = 'id="'.$name.'" ';
  939. $extra_attributes = array_merge(['class' => 'form-control'], $extra_attributes);
  940. foreach ($extra_attributes as $key => $parameter) {
  941. if ($key == 'id') {
  942. $default_id = '';
  943. }
  944. $extra .= $key.'="'.$parameter.'" ';
  945. }
  946. $html .= '<select name="'.$name.'" '.$default_id.' '.$extra.'>';
  947. if ($show_blank_item) {
  948. if (empty($blank_item_text)) {
  949. $blank_item_text = get_lang('Select');
  950. } else {
  951. $blank_item_text = Security::remove_XSS($blank_item_text);
  952. }
  953. $html .= self::tag(
  954. 'option',
  955. '-- '.$blank_item_text.' --',
  956. ['value' => '-1']
  957. );
  958. }
  959. if ($values) {
  960. foreach ($values as $key => $value) {
  961. if (is_array($value) && isset($value['name'])) {
  962. $value = $value['name'];
  963. }
  964. $html .= '<option value="'.$key.'"';
  965. if (is_array($default)) {
  966. foreach ($default as $item) {
  967. if ($item == $key) {
  968. $html .= ' selected="selected"';
  969. break;
  970. }
  971. }
  972. } else {
  973. if ($default == $key) {
  974. $html .= ' selected="selected"';
  975. }
  976. }
  977. $html .= '>'.$value.'</option>';
  978. }
  979. }
  980. $html .= '</select>';
  981. return $html;
  982. }
  983. /**
  984. * Creates a tab menu
  985. * Requirements: declare the jquery, jquery-ui libraries + the jquery-ui.css
  986. * in the $htmlHeadXtra variable before the display_header
  987. * Add this script.
  988. *
  989. * @example
  990. * <script>
  991. </script>
  992. * @param array $headers list of the tab titles
  993. * @param array $items
  994. * @param string $id id of the container of the tab in the example "tabs"
  995. * @param array $attributes for the ul
  996. * @param array $ul_attributes
  997. * @param int $selected
  998. *
  999. * @return string
  1000. */
  1001. public static function tabs(
  1002. $headers,
  1003. $items,
  1004. $id = 'tabs',
  1005. $attributes = [],
  1006. $ul_attributes = [],
  1007. $selected = ''
  1008. ) {
  1009. if (empty($headers) || count($headers) == 0) {
  1010. return '';
  1011. }
  1012. $lis = '';
  1013. $i = 1;
  1014. foreach ($headers as $item) {
  1015. $active = '';
  1016. if ($i == 1) {
  1017. $active = ' active';
  1018. }
  1019. if (!empty($selected)) {
  1020. $active = '';
  1021. if ($selected == $i) {
  1022. $active = ' active';
  1023. }
  1024. }
  1025. $item = self::tag(
  1026. 'a',
  1027. $item,
  1028. [
  1029. 'href' => '#'.$id.'-'.$i,
  1030. 'class' => 'nav-item nav-link '.$active,
  1031. 'id' => $id.$i.'-tab',
  1032. 'data-toggle' => 'tab',
  1033. 'role' => 'tab',
  1034. 'aria-controls' => $id.'-'.$i,
  1035. 'aria-selected' => $selected,
  1036. ]
  1037. );
  1038. $lis .= $item;
  1039. $i++;
  1040. }
  1041. $ul = self::tag(
  1042. 'nav',
  1043. $lis,
  1044. [
  1045. 'id' => 'ul_'.$id,
  1046. 'class' => 'nav nav-tabs',
  1047. 'role' => 'tablist',
  1048. ]
  1049. );
  1050. $i = 1;
  1051. $divs = '';
  1052. foreach ($items as $content) {
  1053. $active = '';
  1054. if ($i == 1) {
  1055. $active = ' show active';
  1056. }
  1057. if (!empty($selected)) {
  1058. $active = '';
  1059. if ($selected == $i) {
  1060. $active = ' show active';
  1061. }
  1062. }
  1063. $divs .= self::tag(
  1064. 'div',
  1065. $content,
  1066. [
  1067. 'id' => $id.'-'.$i,
  1068. 'class' => 'tab-pane fade '.$active,
  1069. 'role' => 'tabpanel',
  1070. 'aria-labelledby' => $id.$i.'-tab',
  1071. ]
  1072. );
  1073. $i++;
  1074. }
  1075. $attributes['id'] = $id;
  1076. $attributes['class'] = 'tab_wrapper';
  1077. $html = self::tag(
  1078. 'div',
  1079. $ul.
  1080. self::tag('div', $divs, ['class' => 'tab-content']),
  1081. $attributes
  1082. );
  1083. return $html;
  1084. }
  1085. /**
  1086. * @param $headers
  1087. * @param null $selected
  1088. *
  1089. * @return string
  1090. */
  1091. public static function tabsOnlyLink($headers, $selected = null)
  1092. {
  1093. $id = uniqid();
  1094. $i = 1;
  1095. $lis = null;
  1096. foreach ($headers as $item) {
  1097. $class = null;
  1098. if ($i == $selected) {
  1099. $class = 'active';
  1100. }
  1101. $item = self::tag(
  1102. 'a',
  1103. $item['content'],
  1104. [
  1105. 'id' => $id.'-'.$i,
  1106. 'href' => $item['url'],
  1107. 'class' => 'nav-link '.$class,
  1108. ]
  1109. );
  1110. $lis .= self::tag('li', $item, ['class' => 'nav-item']);
  1111. $i++;
  1112. }
  1113. return self::tag(
  1114. 'ul',
  1115. $lis,
  1116. ['class' => 'nav nav-tabs tabs-margin']
  1117. );
  1118. }
  1119. /**
  1120. * In order to display a grid using jqgrid you have to:.
  1121. *
  1122. * @example
  1123. * After your Display::display_header function you have to add the nex javascript code:
  1124. * <script>
  1125. * echo Display::grid_js('my_grid_name', $url,$columns, $column_model, $extra_params,[]);
  1126. * // for more information of this function check the grid_js() function
  1127. * </script>
  1128. * //Then you have to call the grid_html
  1129. * echo Display::grid_html('my_grid_name');
  1130. * As you can see both function use the same "my_grid_name" this is very important otherwise nothing will work
  1131. *
  1132. * @param string the div id, this value must be the same with the first parameter of Display::grid_js()
  1133. *
  1134. * @return string html
  1135. */
  1136. public static function grid_html($div_id)
  1137. {
  1138. $table = self::tag('table', '', ['id' => $div_id]);
  1139. $table .= self::tag('div', '', ['id' => $div_id.'_pager']);
  1140. return $table;
  1141. }
  1142. /**
  1143. * @param string $label
  1144. * @param string $form_item
  1145. *
  1146. * @return string
  1147. */
  1148. public static function form_row($label, $form_item)
  1149. {
  1150. $label = self::tag('label', $label, ['class' => 'col-sm-2 control-label']);
  1151. $form_item = self::div($form_item, ['class' => 'col-sm-10']);
  1152. return self::div($label.$form_item, ['class' => 'form-group']);
  1153. }
  1154. /**
  1155. * This is a wrapper to use the jqgrid in Chamilo.
  1156. * For the other jqgrid options visit http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options
  1157. * This function need to be in the ready jquery function
  1158. * example --> $(function() { <?php echo Display::grid_js('grid' ...); ?> }
  1159. * In order to work this function needs the Display::grid_html function with the same div id.
  1160. *
  1161. * @param string $div_id div id
  1162. * @param string $url url where the jqgrid will ask for data (if datatype = json)
  1163. * @param array $column_names Visible columns (you should use get_lang).
  1164. * An array in which we place the names of the columns.
  1165. * This is the text that appears in the head of the grid (Header layer).
  1166. * Example: colname {name:'date', index:'date', width:120, align:'right'},
  1167. * @param array $column_model the column model : Array which describes the parameters of the columns.
  1168. * This is the most important part of the grid.
  1169. * For a full description of all valid values see colModel API. See the url above.
  1170. * @param array $extra_params extra parameters
  1171. * @param array $data data that will be loaded
  1172. * @param string $formatter A string that will be appended to the JSON returned
  1173. * @param bool $fixed_width not implemented yet
  1174. *
  1175. * @return string the js code
  1176. */
  1177. public static function grid_js(
  1178. $div_id,
  1179. $url,
  1180. $column_names,
  1181. $column_model,
  1182. $extra_params,
  1183. $data = [],
  1184. $formatter = '',
  1185. $fixed_width = false
  1186. ) {
  1187. $obj = new stdClass();
  1188. $obj->first = 'first';
  1189. if (!empty($url)) {
  1190. $obj->url = $url;
  1191. }
  1192. // Needed it in order to render the links/html in the grid
  1193. foreach ($column_model as &$columnModel) {
  1194. if (!isset($columnModel['formatter'])) {
  1195. $columnModel['formatter'] = '';
  1196. }
  1197. }
  1198. //This line should only be used/modified in case of having characters
  1199. // encoding problems - see #6159
  1200. //$column_names = array_map("utf8_encode", $column_names);
  1201. $obj->colNames = $column_names;
  1202. $obj->colModel = $column_model;
  1203. $obj->pager = '#'.$div_id.'_pager';
  1204. $obj->datatype = 'json';
  1205. $obj->viewrecords = 'true';
  1206. $obj->guiStyle = 'bootstrap4';
  1207. $obj->iconSet = 'fontAwesomeSolid';
  1208. $all_value = 10000000;
  1209. // Sets how many records we want to view in the grid
  1210. $obj->rowNum = 20;
  1211. // Default row quantity
  1212. if (!isset($extra_params['rowList'])) {
  1213. $extra_params['rowList'] = [20, 50, 100, 500, 1000, $all_value];
  1214. $rowList = api_get_configuration_value('table_row_list');
  1215. if (!empty($rowList) && isset($rowList['options'])) {
  1216. $rowList = $rowList['options'];
  1217. $rowList[] = $all_value;
  1218. }
  1219. $extra_params['rowList'] = $rowList;
  1220. }
  1221. $defaultRow = api_get_configuration_value('table_default_row');
  1222. if (!empty($defaultRow)) {
  1223. $obj->rowNum = (int) $defaultRow;
  1224. }
  1225. $json = '';
  1226. if (!empty($extra_params['datatype'])) {
  1227. $obj->datatype = $extra_params['datatype'];
  1228. }
  1229. // Row even odd style.
  1230. $obj->altRows = true;
  1231. if (!empty($extra_params['altRows'])) {
  1232. $obj->altRows = $extra_params['altRows'];
  1233. }
  1234. if (!empty($extra_params['sortname'])) {
  1235. $obj->sortname = $extra_params['sortname'];
  1236. }
  1237. if (!empty($extra_params['sortorder'])) {
  1238. $obj->sortorder = $extra_params['sortorder'];
  1239. }
  1240. if (!empty($extra_params['rowList'])) {
  1241. $obj->rowList = $extra_params['rowList'];
  1242. }
  1243. if (!empty($extra_params['rowNum'])) {
  1244. $obj->rowNum = $extra_params['rowNum'];
  1245. } else {
  1246. // Try to load max rows from Session
  1247. $urlInfo = parse_url($url);
  1248. if (isset($urlInfo['query'])) {
  1249. parse_str($urlInfo['query'], $query);
  1250. if (isset($query['a'])) {
  1251. $action = $query['a'];
  1252. // This value is set in model.ajax.php
  1253. $savedRows = Session::read('max_rows_'.$action);
  1254. if (!empty($savedRows)) {
  1255. $obj->rowNum = $savedRows;
  1256. }
  1257. }
  1258. }
  1259. }
  1260. if (!empty($extra_params['viewrecords'])) {
  1261. $obj->viewrecords = $extra_params['viewrecords'];
  1262. }
  1263. $beforeSelectRow = null;
  1264. if (isset($extra_params['beforeSelectRow'])) {
  1265. $beforeSelectRow = 'beforeSelectRow: '.$extra_params['beforeSelectRow'].', ';
  1266. unset($extra_params['beforeSelectRow']);
  1267. }
  1268. $beforeProcessing = '';
  1269. if (isset($extra_params['beforeProcessing'])) {
  1270. $beforeProcessing = 'beforeProcessing : function() { '.$extra_params['beforeProcessing'].' },';
  1271. unset($extra_params['beforeProcessing']);
  1272. }
  1273. $beforeRequest = '';
  1274. if (isset($extra_params['beforeRequest'])) {
  1275. $beforeRequest = 'beforeRequest : function() { '.$extra_params['beforeRequest'].' },';
  1276. unset($extra_params['beforeRequest']);
  1277. }
  1278. $gridComplete = '';
  1279. if (isset($extra_params['gridComplete'])) {
  1280. $gridComplete = 'gridComplete : function() { '.$extra_params['gridComplete'].' },';
  1281. unset($extra_params['gridComplete']);
  1282. }
  1283. // Adding extra params
  1284. if (!empty($extra_params)) {
  1285. foreach ($extra_params as $key => $element) {
  1286. // the groupHeaders key gets a special treatment
  1287. if ($key != 'groupHeaders') {
  1288. $obj->$key = $element;
  1289. }
  1290. }
  1291. }
  1292. // Adding static data.
  1293. if (!empty($data)) {
  1294. $data_var = $div_id.'_data';
  1295. $json .= ' var '.$data_var.' = '.json_encode($data).';';
  1296. $obj->data = $data_var;
  1297. $obj->datatype = 'local';
  1298. $json .= "\n";
  1299. }
  1300. $obj->end = 'end';
  1301. $json_encode = json_encode($obj);
  1302. if (!empty($data)) {
  1303. //Converts the "data":"js_variable" to "data":js_variable,
  1304. // otherwise it will not work
  1305. $json_encode = str_replace('"data":"'.$data_var.'"', '"data":'.$data_var.'', $json_encode);
  1306. }
  1307. // Fixing true/false js values that doesn't need the ""
  1308. $json_encode = str_replace(':"true"', ':true', $json_encode);
  1309. // wrap_cell is not a valid jqgrid attributes is a hack to wrap a text
  1310. $json_encode = str_replace('"wrap_cell":true', 'cellattr : function(rowId, value, rowObject, colModel, arrData) { return \'class = "jqgrid_whitespace"\'; }', $json_encode);
  1311. $json_encode = str_replace(':"false"', ':false', $json_encode);
  1312. $json_encode = str_replace('"formatter":"action_formatter"', 'formatter:action_formatter', $json_encode);
  1313. $json_encode = str_replace('"formatter":"extra_formatter"', 'formatter:extra_formatter', $json_encode);
  1314. $json_encode = str_replace(['{"first":"first",', '"end":"end"}'], '', $json_encode);
  1315. if (api_get_configuration_value('allow_compilatio_tool') &&
  1316. (strpos($_SERVER['REQUEST_URI'], 'work/work.php') !== false ||
  1317. strpos($_SERVER['REQUEST_URI'], 'work/work_list_all.php') != false
  1318. )
  1319. ) {
  1320. $json_encode = str_replace('"function () { compilatioInit() }"',
  1321. 'function () { compilatioInit() }',
  1322. $json_encode
  1323. );
  1324. }
  1325. // Creating the jqgrid element.
  1326. $json .= '$("#'.$div_id.'").jqGrid({';
  1327. //$json .= $beforeSelectRow;
  1328. $json .= $gridComplete;
  1329. $json .= $beforeProcessing;
  1330. $json .= $beforeRequest;
  1331. $json .= $json_encode;
  1332. $json .= '});';
  1333. // Grouping headers option
  1334. if (isset($extra_params['groupHeaders'])) {
  1335. $groups = '';
  1336. foreach ($extra_params['groupHeaders'] as $group) {
  1337. //{ "startColumnName" : "courses", "numberOfColumns" : 1, "titleText" : "Order Info" },
  1338. $groups .= '{ "startColumnName" : "'.$group['startColumnName'].'", "numberOfColumns" : '.$group['numberOfColumns'].', "titleText" : "'.$group['titleText'].'" },';
  1339. }
  1340. $json .= '$("#'.$div_id.'").jqGrid("setGroupHeaders", {
  1341. "useColSpanStyle" : false,
  1342. "groupHeaders" : [
  1343. '.$groups.'
  1344. ]
  1345. });';
  1346. }
  1347. $all_text = addslashes(get_lang('All'));
  1348. $json .= '$("'.$obj->pager.' option[value='.$all_value.']").text("'.$all_text.'");';
  1349. $json .= "\n";
  1350. // Adding edit/delete icons.
  1351. $json .= $formatter;
  1352. return $json;
  1353. }
  1354. /**
  1355. * @param array $headers
  1356. * @param array $rows
  1357. * @param array $attributes
  1358. *
  1359. * @return string
  1360. */
  1361. public static function table($headers, $rows, $attributes = [])
  1362. {
  1363. if (empty($attributes)) {
  1364. $attributes['class'] = 'data_table';
  1365. }
  1366. $table = new HTML_Table($attributes);
  1367. $row = 0;
  1368. $column = 0;
  1369. // Course headers
  1370. if (!empty($headers)) {
  1371. foreach ($headers as $item) {
  1372. $table->setHeaderContents($row, $column, $item);
  1373. $column++;
  1374. }
  1375. $row = 1;
  1376. $column = 0;
  1377. }
  1378. if (!empty($rows)) {
  1379. foreach ($rows as $content) {
  1380. $table->setCellContents($row, $column, $content);
  1381. $row++;
  1382. }
  1383. }
  1384. return $table->toHtml();
  1385. }
  1386. /**
  1387. * Returns the "what's new" icon notifications.
  1388. *
  1389. * The general logic of this function is to track the last time the user
  1390. * entered the course and compare to what has changed inside this course
  1391. * since then, based on the item_property table inside this course. Note that,
  1392. * if the user never entered the course before, he will not see notification
  1393. * icons. This function takes session ID into account (if any) and only shows
  1394. * the corresponding notifications.
  1395. *
  1396. * @param array $courseInfo Course information array, containing at least elements 'db' and 'k'
  1397. * @param bool $loadAjax
  1398. *
  1399. * @return string The HTML link to be shown next to the course
  1400. */
  1401. public static function show_notification($courseInfo, $loadAjax = true)
  1402. {
  1403. if (empty($courseInfo)) {
  1404. return '';
  1405. }
  1406. $t_track_e_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  1407. $course_tool_table = Database::get_course_table(TABLE_TOOL_LIST);
  1408. $tool_edit_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1409. $course_code = Database::escape_string($courseInfo['code']);
  1410. $user_id = api_get_user_id();
  1411. $course_id = (int) $courseInfo['real_id'];
  1412. $sessionId = (int) $courseInfo['id_session'];
  1413. $status = (int) $courseInfo['status'];
  1414. $loadNotificationsByAjax = api_get_configuration_value('user_portal_load_notification_by_ajax');
  1415. if ($loadNotificationsByAjax) {
  1416. if ($loadAjax) {
  1417. $id = 'notification_'.$course_id.'_'.$sessionId.'_'.$status;
  1418. Session::write($id, true);
  1419. return '<span id ="'.$id.'" class="course_notification"></span>';
  1420. }
  1421. }
  1422. // Get the user's last access dates to all tools of this course
  1423. $sql = "SELECT *
  1424. FROM $t_track_e_access
  1425. WHERE
  1426. c_id = $course_id AND
  1427. access_user_id = '$user_id' AND
  1428. access_session_id ='".$sessionId."'
  1429. ORDER BY access_date DESC
  1430. LIMIT 1
  1431. ";
  1432. $result = Database::query($sql);
  1433. // latest date by default is the creation date
  1434. $latestDate = $courseInfo['creation_date'];
  1435. if (Database::num_rows($result)) {
  1436. $row = Database::fetch_array($result, 'ASSOC');
  1437. $latestDate = $row['access_date'];
  1438. }
  1439. $sessionCondition = api_get_session_condition(
  1440. $sessionId,
  1441. true,
  1442. false,
  1443. 'session_id'
  1444. );
  1445. $hideTools = [TOOL_NOTEBOOK, TOOL_CHAT];
  1446. // Get current tools in course
  1447. $sql = "SELECT name, link, image
  1448. FROM $course_tool_table
  1449. WHERE
  1450. c_id = $course_id AND
  1451. visibility = '1' AND
  1452. name NOT IN ('".implode("','", $hideTools)."')
  1453. ";
  1454. $result = Database::query($sql);
  1455. $tools = Database::store_result($result);
  1456. $group_ids = GroupManager::get_group_ids($courseInfo['real_id'], $user_id);
  1457. $group_ids[] = 0; //add group 'everyone'
  1458. $notifications = [];
  1459. if ($tools) {
  1460. foreach ($tools as $tool) {
  1461. $toolName = $tool['name'];
  1462. $toolName = Database::escape_string($toolName);
  1463. // Fix to get student publications
  1464. $toolCondition = " tool = '$toolName' AND ";
  1465. if ($toolName == 'student_publication' || $toolName == 'work') {
  1466. $toolCondition = " (tool = 'work' OR tool = 'student_publication') AND ";
  1467. }
  1468. $toolName = addslashes($toolName);
  1469. $sql = "SELECT * FROM $tool_edit_table
  1470. WHERE
  1471. c_id = $course_id AND
  1472. $toolCondition
  1473. lastedit_type NOT LIKE '%Deleted%' AND
  1474. lastedit_type NOT LIKE '%deleted%' AND
  1475. lastedit_type NOT LIKE '%DocumentInvisible%' AND
  1476. lastedit_date > '$latestDate' AND
  1477. lastedit_user_id != $user_id $sessionCondition AND
  1478. visibility != 2 AND
  1479. (to_user_id IN ('$user_id', '0') OR to_user_id IS NULL) AND
  1480. (to_group_id IN ('".implode("','", $group_ids)."') OR to_group_id IS NULL)
  1481. ORDER BY lastedit_date DESC
  1482. LIMIT 1";
  1483. $result = Database::query($sql);
  1484. $latestChange = Database::fetch_array($result, 'ASSOC');
  1485. if ($latestChange) {
  1486. $latestChange['link'] = $tool['link'];
  1487. $latestChange['image'] = $tool['image'];
  1488. $latestChange['tool'] = $tool['name'];
  1489. $notifications[$toolName] = $latestChange;
  1490. }
  1491. }
  1492. }
  1493. // Show all tool icons where there is something new.
  1494. $return = '';
  1495. foreach ($notifications as $notification) {
  1496. $toolName = $notification['tool'];
  1497. if (!(
  1498. $notification['visibility'] == '1' ||
  1499. ($status == '1' && $notification['visibility'] == '0') ||
  1500. !isset($notification['visibility'])
  1501. )
  1502. ) {
  1503. continue;
  1504. }
  1505. if ($toolName == TOOL_SURVEY) {
  1506. $survey_info = SurveyManager::get_survey($notification['ref'], 0, $course_code);
  1507. if (!empty($survey_info)) {
  1508. $invited_users = SurveyUtil::get_invited_users(
  1509. $survey_info['code'],
  1510. $course_code
  1511. );
  1512. if (!in_array($user_id, $invited_users['course_users'])) {
  1513. continue;
  1514. }
  1515. }
  1516. }
  1517. if ($notification['tool'] == TOOL_LEARNPATH) {
  1518. if (!learnpath::is_lp_visible_for_student($notification['ref'], $user_id, $courseInfo)) {
  1519. continue;
  1520. }
  1521. }
  1522. if ($notification['tool'] == TOOL_DROPBOX) {
  1523. $notification['link'] = 'dropbox/dropbox_download.php?id='.$notification['ref'];
  1524. }
  1525. if ($notification['tool'] == 'work' &&
  1526. $notification['lastedit_type'] == 'DirectoryCreated'
  1527. ) {
  1528. $notification['lastedit_type'] = 'WorkAdded';
  1529. }
  1530. $lastDate = api_get_local_time($notification['lastedit_date']);
  1531. $type = $notification['lastedit_type'];
  1532. if ($type == 'CalendareventVisible') {
  1533. $type = 'Visible';
  1534. }
  1535. $label = get_lang('Since your latest visit').": ".get_lang($type)." ($lastDate)";
  1536. if (strpos($notification['link'], '?') === false) {
  1537. $notification['link'] = $notification['link'].'?notification=1';
  1538. } else {
  1539. $notification['link'] = $notification['link'].'&notification=1';
  1540. }
  1541. $image = substr($notification['image'], 0, -4).'.png';
  1542. $return .= self::url(
  1543. self::return_icon($image, $label),
  1544. api_get_path(WEB_CODE_PATH).
  1545. $notification['link'].'&cidReq='.$course_code.
  1546. '&ref='.$notification['ref'].
  1547. '&gidReq='.$notification['to_group_id'].
  1548. '&id_session='.$sessionId
  1549. ).PHP_EOL;
  1550. }
  1551. return $return;
  1552. }
  1553. /**
  1554. * Get the session box details as an array.
  1555. *
  1556. * @param int $session_id
  1557. *
  1558. * @return array Empty array or session array
  1559. * ['title'=>'...','category'=>'','dates'=>'...','coach'=>'...','active'=>true/false,'session_category_id'=>int]
  1560. */
  1561. public static function getSessionTitleBox($session_id)
  1562. {
  1563. global $nosession;
  1564. if (!$nosession) {
  1565. global $now, $date_start, $date_end;
  1566. }
  1567. $output = [];
  1568. if (!$nosession) {
  1569. $session_info = api_get_session_info($session_id);
  1570. $coachInfo = [];
  1571. if (!empty($session['id_coach'])) {
  1572. $coachInfo = api_get_user_info($session['id_coach']);
  1573. }
  1574. $session = [];
  1575. $session['category_id'] = $session_info['session_category_id'];
  1576. $session['title'] = $session_info['name'];
  1577. $session['coach_id'] = $session['id_coach'] = $session_info['id_coach'];
  1578. $session['dates'] = '';
  1579. $session['coach'] = '';
  1580. if (api_get_setting('show_session_coach') === 'true' && isset($coachInfo['complete_name'])) {
  1581. $session['coach'] = get_lang('General coach').': '.$coachInfo['complete_name'];
  1582. }
  1583. if (($session_info['access_end_date'] == '0000-00-00 00:00:00' &&
  1584. $session_info['access_start_date'] == '0000-00-00 00:00:00') ||
  1585. (empty($session_info['access_end_date']) && empty($session_info['access_start_date']))
  1586. ) {
  1587. if (isset($session_info['duration']) && !empty($session_info['duration'])) {
  1588. $daysLeft = SessionManager::getDayLeftInSession($session_info, api_get_user_id());
  1589. $session['duration'] = $daysLeft >= 0
  1590. ? sprintf(get_lang('This session has a maximum duration. Only %s days to go.'), $daysLeft)
  1591. : get_lang('You are already registered but your allowed access time has expired.');
  1592. }
  1593. $active = true;
  1594. } else {
  1595. $dates = SessionManager::parseSessionDates($session_info, true);
  1596. $session['dates'] = $dates['access'];
  1597. if (api_get_setting('show_session_coach') === 'true' && isset($coachInfo['complete_name'])) {
  1598. $session['coach'] = $coachInfo['complete_name'];
  1599. }
  1600. $active = $date_start <= $now && $date_end >= $now;
  1601. }
  1602. $session['active'] = $active;
  1603. $session['session_category_id'] = $session_info['session_category_id'];
  1604. $session['visibility'] = $session_info['visibility'];
  1605. $session['num_users'] = $session_info['nbr_users'];
  1606. $session['num_courses'] = $session_info['nbr_courses'];
  1607. $session['description'] = $session_info['description'];
  1608. $session['show_description'] = $session_info['show_description'];
  1609. //$session['image'] = SessionManager::getSessionImage($session_info['id']);
  1610. $session['url'] = api_get_path(WEB_CODE_PATH).'session/index.php?session_id='.$session_info['id'];
  1611. $entityManager = Database::getManager();
  1612. $fieldValuesRepo = $entityManager->getRepository('ChamiloCoreBundle:ExtraFieldValues');
  1613. $extraFieldValues = $fieldValuesRepo->getVisibleValues(
  1614. ExtraField::SESSION_FIELD_TYPE,
  1615. $session_id
  1616. );
  1617. $session['extra_fields'] = [];
  1618. /** @var \Chamilo\CoreBundle\Entity\ExtraFieldValues $value */
  1619. foreach ($extraFieldValues as $value) {
  1620. if (empty($value)) {
  1621. continue;
  1622. }
  1623. $session['extra_fields'][] = [
  1624. 'field' => [
  1625. 'variable' => $value->getField()->getVariable(),
  1626. 'display_text' => $value->getField()->getDisplayText(),
  1627. ],
  1628. 'value' => $value->getValue(),
  1629. ];
  1630. }
  1631. $output = $session;
  1632. }
  1633. return $output;
  1634. }
  1635. /**
  1636. * Return the five star HTML.
  1637. *
  1638. * @param string $id of the rating ul element
  1639. * @param string $url that will be added (for jquery see hot_courses.tpl)
  1640. * @param array $point_info point info array see function CourseManager::get_course_ranking()
  1641. * @param bool $add_div_wrapper add a div wrapper
  1642. *
  1643. * @return string
  1644. */
  1645. public static function return_rating_system(
  1646. $id,
  1647. $url,
  1648. $point_info = [],
  1649. $add_div_wrapper = true
  1650. ) {
  1651. $number_of_users_who_voted = isset($point_info['users_who_voted']) ? $point_info['users_who_voted'] : null;
  1652. $percentage = isset($point_info['point_average']) ? $point_info['point_average'] : 0;
  1653. if (!empty($percentage)) {
  1654. $percentage = $percentage * 125 / 100;
  1655. }
  1656. $accesses = isset($point_info['accesses']) ? $point_info['accesses'] : 0;
  1657. $star_label = sprintf(get_lang('%s stars out of 5'), $point_info['point_average_star']);
  1658. $html = '<section class="rating-widget">';
  1659. $html .= '<div class="rating-stars"><ul id="stars">';
  1660. $html .= '<li class="star" data-link="'.$url.'&amp;star=1" title="Poor" data-value="1"><i class="fa fa-star fa-fw"></i></li>
  1661. <li class="star" data-link="'.$url.'&amp;star=2" title="Fair" data-value="2"><i class="fa fa-star fa-fw"></i></li>
  1662. <li class="star" data-link="'.$url.'&amp;star=3" title="Good" data-value="3"><i class="fa fa-star fa-fw"></i></li>
  1663. <li class="star" data-link="'.$url.'&amp;star=4" title="Excellent" data-value="4"><i class="fa fa-star fa-fw"></i></li>
  1664. <li class="star" data-link="'.$url.'&amp;star=5" title="WOW!!!" data-value="5"><i class="fa fa-star fa-fw"></i></li>
  1665. ';
  1666. $html .= '</ul></div>';
  1667. $html .= '</section>';
  1668. $labels = [];
  1669. $labels[] = $number_of_users_who_voted == 1 ? $number_of_users_who_voted.' '.get_lang('Vote') : $number_of_users_who_voted.' '.get_lang('Votes');
  1670. $labels[] = $accesses == 1 ? $accesses.' '.get_lang('Visit') : $accesses.' '.get_lang('Visits');
  1671. $labels[] = $point_info['user_vote'] ? get_lang('Your vote').' ['.$point_info['user_vote'].']' : get_lang('Your vote').' [?] ';
  1672. if (!$add_div_wrapper && api_is_anonymous()) {
  1673. $labels[] = self::tag('span', get_lang('Login to vote'), ['class' => 'error']);
  1674. }
  1675. $html .= self::div(implode(' | ', $labels), ['id' => 'vote_label_'.$id, 'class' => 'vote_label_info']);
  1676. $html .= ' '.self::span(' ', ['id' => 'vote_label2_'.$id]);
  1677. if ($add_div_wrapper) {
  1678. $html = self::div($html, ['id' => 'rating_wrapper_'.$id]);
  1679. }
  1680. return $html;
  1681. }
  1682. /**
  1683. * @param string $title
  1684. * @param string $second_title
  1685. * @param string $size
  1686. * @param bool $filter
  1687. *
  1688. * @return string
  1689. */
  1690. public static function page_header($title, $second_title = null, $size = 'h2', $filter = true)
  1691. {
  1692. if ($filter) {
  1693. $title = Security::remove_XSS($title);
  1694. }
  1695. if (!empty($second_title)) {
  1696. if ($filter) {
  1697. $second_title = Security::remove_XSS($second_title);
  1698. }
  1699. $title .= "<small> $second_title</small>";
  1700. }
  1701. return '<'.$size.' class="page-header">'.$title.'</'.$size.'>';
  1702. }
  1703. public static function page_header_and_translate($title, $second_title = null)
  1704. {
  1705. $title = get_lang($title);
  1706. return self::page_header($title, $second_title);
  1707. }
  1708. public static function page_subheader_and_translate($title, $second_title = null)
  1709. {
  1710. $title = get_lang($title);
  1711. return self::page_subheader($title, $second_title);
  1712. }
  1713. public static function page_subheader($title, $second_title = null, $size = 'h3', $attributes = [])
  1714. {
  1715. if (!empty($second_title)) {
  1716. $second_title = Security::remove_XSS($second_title);
  1717. $title .= "<small> $second_title<small>";
  1718. }
  1719. $subTitle = self::tag($size, Security::remove_XSS($title), $attributes);
  1720. return $subTitle;
  1721. }
  1722. public static function page_subheader2($title, $second_title = null)
  1723. {
  1724. return self::page_header($title, $second_title, 'h4');
  1725. }
  1726. public static function page_subheader3($title, $second_title = null)
  1727. {
  1728. return self::page_header($title, $second_title, 'h5');
  1729. }
  1730. /**
  1731. * @param array $list
  1732. *
  1733. * @return string|null
  1734. */
  1735. public static function description($list)
  1736. {
  1737. $html = null;
  1738. if (!empty($list)) {
  1739. $html = '<dl class="dl-horizontal">';
  1740. foreach ($list as $item) {
  1741. $html .= '<dt>'.$item['title'].'</dt>';
  1742. $html .= '<dd>'.$item['content'].'</dd>';
  1743. }
  1744. $html .= '</dl>';
  1745. }
  1746. return $html;
  1747. }
  1748. /**
  1749. * @param int $percentage int value between 0 and 100
  1750. * @param bool $show_percentage
  1751. * @param string $extra_info
  1752. * @param string $class danger/success/infowarning
  1753. *
  1754. * @return string
  1755. */
  1756. public static function bar_progress($percentage, $show_percentage = true, $extra_info = '', $class = '')
  1757. {
  1758. $percentage = (int) $percentage;
  1759. $class = empty($class) ? '' : "progress-bar-$class";
  1760. $div = '<div class="progress">
  1761. <div
  1762. class="progress-bar progress-bar-striped '.$class.'"
  1763. role="progressbar"
  1764. aria-valuenow="'.$percentage.'"
  1765. aria-valuemin="0"
  1766. aria-valuemax="100"
  1767. style="width: '.$percentage.'%;"
  1768. >';
  1769. if ($show_percentage) {
  1770. $div .= $percentage.'%';
  1771. } else {
  1772. if (!empty($extra_info)) {
  1773. $div .= $extra_info;
  1774. }
  1775. }
  1776. $div .= '</div></div>';
  1777. return $div;
  1778. }
  1779. /**
  1780. * @param string $count
  1781. * @param string $type
  1782. *
  1783. * @return string|null
  1784. */
  1785. public static function badge($count, $type = "warning")
  1786. {
  1787. $class = '';
  1788. switch ($type) {
  1789. case 'success':
  1790. $class = 'badge-success';
  1791. break;
  1792. case 'warning':
  1793. $class = 'badge-warning';
  1794. break;
  1795. case 'important':
  1796. $class = 'badge-important';
  1797. break;
  1798. case 'info':
  1799. $class = 'badge-info';
  1800. break;
  1801. case 'inverse':
  1802. $class = 'badge-inverse';
  1803. break;
  1804. }
  1805. if (!empty($count)) {
  1806. return ' <span class="badge '.$class.'">'.$count.'</span>';
  1807. }
  1808. return null;
  1809. }
  1810. /**
  1811. * @param array $badge_list
  1812. *
  1813. * @return string
  1814. */
  1815. public static function badge_group($badge_list)
  1816. {
  1817. $html = '<div class="badge-group">';
  1818. foreach ($badge_list as $badge) {
  1819. $html .= $badge;
  1820. }
  1821. $html .= '</div>';
  1822. return $html;
  1823. }
  1824. /**
  1825. * @param string $content
  1826. * @param string $type
  1827. *
  1828. * @return string
  1829. */
  1830. public static function label($content, $type = 'default')
  1831. {
  1832. switch ($type) {
  1833. case 'success':
  1834. $class = 'success';
  1835. break;
  1836. case 'warning':
  1837. $class = 'warning';
  1838. break;
  1839. case 'important':
  1840. case 'danger':
  1841. $class = 'danger';
  1842. break;
  1843. case 'info':
  1844. $class = 'info';
  1845. break;
  1846. case 'primary':
  1847. $class = 'primary';
  1848. break;
  1849. default:
  1850. $class = 'secondary';
  1851. break;
  1852. }
  1853. $html = '';
  1854. if (!empty($content)) {
  1855. $html = '<span class="badge badge-'.$class.'">';
  1856. $html .= $content;
  1857. $html .= '</span>';
  1858. }
  1859. return $html;
  1860. }
  1861. /**
  1862. * @param array $items
  1863. * @param string $class
  1864. *
  1865. * @return string|null
  1866. */
  1867. public static function actions($items, $class = 'new_actions')
  1868. {
  1869. $html = null;
  1870. if (!empty($items)) {
  1871. $html = '<div class="'.$class.'"><ul class="nav nav-pills">';
  1872. foreach ($items as $value) {
  1873. $class = null;
  1874. if (isset($value['active']) && $value['active']) {
  1875. $class = 'class ="active"';
  1876. }
  1877. if (basename($_SERVER['REQUEST_URI']) == basename($value['url'])) {
  1878. $class = 'class ="active"';
  1879. }
  1880. $html .= "<li $class >";
  1881. $attributes = isset($value['url_attributes']) ? $value['url_attributes'] : [];
  1882. $html .= self::url($value['content'], $value['url'], $attributes);
  1883. $html .= '</li>';
  1884. }
  1885. $html .= '</ul></div>';
  1886. $html .= '<br />';
  1887. }
  1888. return $html;
  1889. }
  1890. /**
  1891. * Prints a tooltip.
  1892. *
  1893. * @param string $text
  1894. * @param string $tip
  1895. *
  1896. * @return string
  1897. */
  1898. public static function tip($text, $tip)
  1899. {
  1900. if (empty($tip)) {
  1901. return $text;
  1902. }
  1903. return self::span(
  1904. $text,
  1905. ['class' => 'boot-tooltip', 'title' => strip_tags($tip)]
  1906. );
  1907. }
  1908. /**
  1909. * @param array $items
  1910. * @param string $type
  1911. * @param null $id
  1912. *
  1913. * @return string|null
  1914. */
  1915. public static function generate_accordion($items, $type = 'jquery', $id = null)
  1916. {
  1917. $html = null;
  1918. if (!empty($items)) {
  1919. if (empty($id)) {
  1920. $id = api_get_unique_id();
  1921. }
  1922. if ($type == 'jquery') {
  1923. $html = '<div class="accordion_jquery" id="'.$id.'">'; //using jquery
  1924. } else {
  1925. $html = '<div class="accordion" id="'.$id.'">'; //using bootstrap
  1926. }
  1927. $count = 1;
  1928. foreach ($items as $item) {
  1929. $html .= '<div class="accordion-my-group">';
  1930. $html .= '<div class="accordion-heading">
  1931. <a class="accordion-toggle" data-toggle="collapse" data-parent="#'.$id.'" href="#collapse'.$count.'">
  1932. '.$item['title'].'
  1933. </a>
  1934. </div>';
  1935. $html .= '<div id="collapse'.$count.'" class="accordion-body">';
  1936. $html .= '<div class="accordion-my-inner">
  1937. '.$item['content'].'
  1938. </div>
  1939. </div>';
  1940. }
  1941. $html .= '</div>';
  1942. }
  1943. return $html;
  1944. }
  1945. /**
  1946. * @param array $buttons
  1947. *
  1948. * @return string
  1949. */
  1950. public static function groupButton($buttons)
  1951. {
  1952. $html = '<div class="btn-group" role="group">';
  1953. foreach ($buttons as $button) {
  1954. $html .= $button;
  1955. }
  1956. $html .= '</div>';
  1957. return $html;
  1958. }
  1959. /**
  1960. * @todo use twig
  1961. *
  1962. * @param string $title
  1963. * @param array $elements
  1964. * @param bool $alignToRight
  1965. *
  1966. * @return string
  1967. */
  1968. public static function groupButtonWithDropDown($title, $elements, $alignToRight = false)
  1969. {
  1970. $id = uniqid('dropdown', true);
  1971. $html = '<div class="btn-group" role="group">
  1972. <button id = "'.$id.'" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  1973. '.$title.'
  1974. </button>
  1975. <div class="dropdown-menu aria-labelledby="'.$id.'" '.($alignToRight ? 'dropdown-menu-right' : '').'">';
  1976. foreach ($elements as $item) {
  1977. $html .= self::tag('li', self::url($item['title'], $item['href'], ['class' => 'dropdown-item']));
  1978. }
  1979. $html .= '</div>
  1980. </div>';
  1981. return $html;
  1982. }
  1983. /**
  1984. * @param string $file
  1985. * @param array $params
  1986. *
  1987. * @return string|null
  1988. */
  1989. public static function getMediaPlayer($file, $params = [])
  1990. {
  1991. $fileInfo = pathinfo($file);
  1992. $autoplay = isset($params['autoplay']) && 'true' === $params['autoplay'] ? 'autoplay' : '';
  1993. $id = isset($params['id']) ? $params['id'] : $fileInfo['basename'];
  1994. $width = isset($params['width']) ? 'width="'.$params['width'].'"' : null;
  1995. $class = isset($params['class']) ? ' class="'.$params['class'].'"' : null;
  1996. switch ($fileInfo['extension']) {
  1997. case 'mp3':
  1998. case 'webm':
  1999. $html = '<audio id="'.$id.'" '.$class.' controls '.$autoplay.' '.$width.' src="'.$params['url'].'" >';
  2000. $html .= '<object width="'.$width.'" height="50" type="application/x-shockwave-flash" data="'.api_get_path(WEB_LIBRARY_PATH).'javascript/mediaelement/flashmediaelement.swf">
  2001. <param name="movie" value="'.api_get_path(WEB_LIBRARY_PATH).'javascript/mediaelement/flashmediaelement.swf" />
  2002. <param name="flashvars" value="controls=true&file='.$params['url'].'" />
  2003. </object>';
  2004. $html .= '</audio>';
  2005. return $html;
  2006. break;
  2007. case 'wav':
  2008. case 'ogg':
  2009. $html = '<audio width="300px" controls id="'.$id.'" '.$autoplay.' src="'.$params['url'].'" >';
  2010. return $html;
  2011. break;
  2012. }
  2013. return null;
  2014. }
  2015. /**
  2016. * @param int $nextValue
  2017. * @param array $list
  2018. * @param int $current
  2019. * @param int $fixedValue
  2020. * @param array $conditions
  2021. * @param string $link
  2022. * @param bool $isMedia
  2023. * @param bool $addHeaders
  2024. * @param array $linkAttributes
  2025. *
  2026. * @return string
  2027. */
  2028. public static function progressPaginationBar(
  2029. $nextValue,
  2030. $list,
  2031. $current,
  2032. $fixedValue = null,
  2033. $conditions = [],
  2034. $link = null,
  2035. $isMedia = false,
  2036. $addHeaders = true,
  2037. $linkAttributes = []
  2038. ) {
  2039. if ($addHeaders) {
  2040. $pagination_size = 'pagination-mini';
  2041. $html = '<div class="exercise_pagination pagination '.$pagination_size.'"><ul>';
  2042. } else {
  2043. $html = null;
  2044. }
  2045. $affectAllItems = false;
  2046. if ($isMedia && isset($fixedValue) && ($nextValue + 1 == $current)) {
  2047. $affectAllItems = true;
  2048. }
  2049. $localCounter = 0;
  2050. foreach ($list as $itemId) {
  2051. $isCurrent = false;
  2052. if ($affectAllItems) {
  2053. $isCurrent = true;
  2054. } else {
  2055. if (!$isMedia) {
  2056. $isCurrent = $current == ($localCounter + $nextValue + 1) ? true : false;
  2057. }
  2058. }
  2059. $html .= self::parsePaginationItem(
  2060. $itemId,
  2061. $isCurrent,
  2062. $conditions,
  2063. $link,
  2064. $nextValue,
  2065. $isMedia,
  2066. $localCounter,
  2067. $fixedValue,
  2068. $linkAttributes
  2069. );
  2070. $localCounter++;
  2071. }
  2072. if ($addHeaders) {
  2073. $html .= '</ul></div>';
  2074. }
  2075. return $html;
  2076. }
  2077. /**
  2078. * @param int $itemId
  2079. * @param bool $isCurrent
  2080. * @param array $conditions
  2081. * @param string $link
  2082. * @param int $nextValue
  2083. * @param bool $isMedia
  2084. * @param int $localCounter
  2085. * @param int $fixedValue
  2086. * @param array $linkAttributes
  2087. *
  2088. * @return string
  2089. */
  2090. public static function parsePaginationItem(
  2091. $itemId,
  2092. $isCurrent,
  2093. $conditions,
  2094. $link,
  2095. $nextValue = 0,
  2096. $isMedia = false,
  2097. $localCounter = null,
  2098. $fixedValue = null,
  2099. $linkAttributes = []
  2100. ) {
  2101. $defaultClass = 'before';
  2102. $class = $defaultClass;
  2103. foreach ($conditions as $condition) {
  2104. $array = isset($condition['items']) ? $condition['items'] : [];
  2105. $class_to_applied = $condition['class'];
  2106. $type = isset($condition['type']) ? $condition['type'] : 'positive';
  2107. $mode = isset($condition['mode']) ? $condition['mode'] : 'add';
  2108. switch ($type) {
  2109. case 'positive':
  2110. if (in_array($itemId, $array)) {
  2111. if ($mode == 'overwrite') {
  2112. $class = " $defaultClass $class_to_applied";
  2113. } else {
  2114. $class .= " $class_to_applied";
  2115. }
  2116. }
  2117. break;
  2118. case 'negative':
  2119. if (!in_array($itemId, $array)) {
  2120. if ($mode == 'overwrite') {
  2121. $class = " $defaultClass $class_to_applied";
  2122. } else {
  2123. $class .= " $class_to_applied";
  2124. }
  2125. }
  2126. break;
  2127. }
  2128. }
  2129. if ($isCurrent) {
  2130. $class = 'before current';
  2131. }
  2132. if ($isMedia && $isCurrent) {
  2133. $class = 'before current';
  2134. }
  2135. if (empty($link)) {
  2136. $link_to_show = '#';
  2137. } else {
  2138. $link_to_show = $link.($nextValue + $localCounter);
  2139. }
  2140. $label = $nextValue + $localCounter + 1;
  2141. if ($isMedia) {
  2142. $label = ($fixedValue + 1).' '.chr(97 + $localCounter);
  2143. $link_to_show = $link.$fixedValue.'#questionanchor'.$itemId;
  2144. }
  2145. $link = self::url($label.' ', $link_to_show, $linkAttributes);
  2146. return '<li class = "'.$class.'">'.$link.'</li>';
  2147. }
  2148. /**
  2149. * @param int $current
  2150. * @param int $total
  2151. *
  2152. * @return string
  2153. */
  2154. public static function paginationIndicator($current, $total)
  2155. {
  2156. $html = null;
  2157. if (!empty($current) && !empty($total)) {
  2158. $label = sprintf(get_lang('%s of %s'), $current, $total);
  2159. $html = self::url($label, '#', ['class' => 'btn disabled']);
  2160. }
  2161. return $html;
  2162. }
  2163. /**
  2164. * @param $url
  2165. * @param $currentPage
  2166. * @param $pagesCount
  2167. * @param $totalItems
  2168. *
  2169. * @return string
  2170. */
  2171. public static function getPagination($url, $currentPage, $pagesCount, $totalItems)
  2172. {
  2173. $pagination = '';
  2174. if ($totalItems > 1 && $pagesCount > 1) {
  2175. $pagination .= '<ul class="pagination">';
  2176. for ($i = 0; $i < $pagesCount; $i++) {
  2177. $newPage = $i + 1;
  2178. if ($currentPage == $newPage) {
  2179. $pagination .= '<li class="active"><a href="'.$url.'&page='.$newPage.'">'.$newPage.'</a></li>';
  2180. } else {
  2181. $pagination .= '<li><a href="'.$url.'&page='.$newPage.'">'.$newPage.'</a></li>';
  2182. }
  2183. }
  2184. $pagination .= '</ul>';
  2185. }
  2186. return $pagination;
  2187. }
  2188. /**
  2189. * Adds a message in the queue.
  2190. *
  2191. * @param string $message
  2192. */
  2193. public static function addFlash($message)
  2194. {
  2195. $messages = Session::read('flash_messages');
  2196. if (empty($messages)) {
  2197. $messages[] = $message;
  2198. } else {
  2199. array_push($messages, $message);
  2200. }
  2201. Session::write('flash_messages', $messages);
  2202. }
  2203. /**
  2204. * @return string
  2205. */
  2206. public static function getFlashToString()
  2207. {
  2208. $messages = Session::read('flash_messages');
  2209. $messageToString = '';
  2210. if (!empty($messages)) {
  2211. foreach ($messages as $message) {
  2212. $messageToString .= $message;
  2213. }
  2214. }
  2215. return $messageToString;
  2216. }
  2217. /**
  2218. * Shows the message from the session.
  2219. */
  2220. public static function showFlash()
  2221. {
  2222. echo self::getFlashToString();
  2223. }
  2224. /**
  2225. * Destroys the message session.
  2226. */
  2227. public static function cleanFlashMessages()
  2228. {
  2229. Session::erase('flash_messages');
  2230. }
  2231. /**
  2232. * Get the profile edition link for a user.
  2233. *
  2234. * @param int $userId The user id
  2235. * @param bool $asAdmin Optional. Whether get the URL for the platform admin
  2236. *
  2237. * @return string The link
  2238. */
  2239. public static function getProfileEditionLink($userId, $asAdmin = false)
  2240. {
  2241. $editProfileUrl = api_get_path(WEB_CODE_PATH).'auth/profile.php';
  2242. if ($asAdmin) {
  2243. $editProfileUrl = api_get_path(WEB_CODE_PATH)."admin/user_edit.php?user_id=".intval($userId);
  2244. }
  2245. return $editProfileUrl;
  2246. }
  2247. /**
  2248. * Get the vCard for a user.
  2249. *
  2250. * @param int $userId The user id
  2251. *
  2252. * @return string *.*vcf file
  2253. */
  2254. public static function getVCardUserLink($userId)
  2255. {
  2256. $vCardUrl = api_get_path(WEB_PATH).'main/social/vcard_export.php?userId='.intval($userId);
  2257. return $vCardUrl;
  2258. }
  2259. /**
  2260. * @param string $content
  2261. * @param string $title
  2262. * @param string $footer
  2263. * @param string $type primary|success|info|warning|danger
  2264. * @param string $extra
  2265. * @param string $id
  2266. * @param string $customColor
  2267. * @param string $rightAction
  2268. *
  2269. * @return string
  2270. */
  2271. public static function panel(
  2272. $content,
  2273. $title = '',
  2274. $footer = '',
  2275. $type = 'default',
  2276. $extra = '',
  2277. $id = '',
  2278. $customColor = '',
  2279. $rightAction = ''
  2280. ) {
  2281. $headerStyle = '';
  2282. if (!empty($customColor)) {
  2283. $headerStyle = 'style = "color: white; background-color: '.$customColor.'" ';
  2284. }
  2285. if (!empty($rightAction)) {
  2286. $rightAction = '<span class="float-right">'.$rightAction.'</span>';
  2287. }
  2288. $title = !empty($title) ? '<h5 class="card-title">'.$title.' '.$rightAction.'</h5>'.$extra : '';
  2289. $footer = !empty($footer) ? '<p class="card-text"><small class="text-muted">'.$footer.'</small></p>' : '';
  2290. $typeList = ['primary', 'success', 'info', 'warning', 'danger'];
  2291. $style = !in_array($type, $typeList) ? 'default' : $type;
  2292. if (!empty($id)) {
  2293. $id = " id='$id'";
  2294. }
  2295. $cardBody = $title.' '.self::contentPanel($content).' '.$footer;
  2296. $panel = Display::tag('div', $cardBody, ['id' => 'card-'.$id, 'class' => 'card-body']);
  2297. return '
  2298. <div '.$id.' class="card">
  2299. '.$panel.'
  2300. </div>'
  2301. ;
  2302. }
  2303. /**
  2304. * @param string $content
  2305. *
  2306. * @return string
  2307. */
  2308. public static function contentPanel($content): string
  2309. {
  2310. if (empty($content)) {
  2311. return '';
  2312. }
  2313. return '<div class="card-text">'.$content.'</div>';
  2314. }
  2315. /**
  2316. * Get the button HTML with an Awesome Font icon.
  2317. *
  2318. * @param string $text The button content
  2319. * @param string $url The url to button
  2320. * @param string $icon The Awesome Font class for icon
  2321. * @param string $type Optional. The button Bootstrap class. Default 'default' class
  2322. * @param array $attributes The additional attributes
  2323. * @param bool $includeText
  2324. *
  2325. * @return string The button HTML
  2326. */
  2327. public static function toolbarButton(
  2328. $text,
  2329. $url,
  2330. $icon = 'check',
  2331. $type = null,
  2332. array $attributes = [],
  2333. $includeText = true
  2334. ) {
  2335. $buttonClass = "btn btn-outline-secondary";
  2336. if (!empty($type)) {
  2337. $buttonClass = "btn btn-$type";
  2338. }
  2339. $icon = self::tag('i', null, ['class' => "fa fa-$icon fa-fw", 'aria-hidden' => 'true']);
  2340. $attributes['class'] = isset($attributes['class']) ? "$buttonClass {$attributes['class']}" : $buttonClass;
  2341. $attributes['title'] = isset($attributes['title']) ? $attributes['title'] : $text;
  2342. if (!$includeText) {
  2343. $text = '<span class="sr-only">'.$text.'</span>';
  2344. }
  2345. return self::url("$icon $text", $url, $attributes);
  2346. }
  2347. /**
  2348. * @param string $id
  2349. * @param array $content
  2350. * @param array $colsWidth Optional. Columns width
  2351. *
  2352. * @return string
  2353. */
  2354. public static function toolbarAction($id, $content, $colsWidth = [])
  2355. {
  2356. $col = count($content);
  2357. if (!$colsWidth) {
  2358. $width = 12 / $col;
  2359. array_walk($content, function () use ($width, &$colsWidth) {
  2360. $colsWidth[] = $width;
  2361. });
  2362. }
  2363. $html = '<div id="'.$id.'" class="actions">';
  2364. $html .= '<div class="row">';
  2365. for ($i = 0; $i < $col; $i++) {
  2366. $class = 'col-sm-'.$colsWidth[$i];
  2367. if ($col > 1) {
  2368. if ($i > 0 && $i < count($content) - 1) {
  2369. $class .= ' text-center';
  2370. } elseif ($i === count($content) - 1) {
  2371. $class .= ' text-right';
  2372. }
  2373. }
  2374. $html .= '<div class="'.$class.'">'.$content[$i].'</div>';
  2375. }
  2376. $html .= '</div>';
  2377. $html .= '</div>';
  2378. return $html;
  2379. }
  2380. /**
  2381. * Get a HTML code for a icon by Font Awesome.
  2382. *
  2383. * @param string $name The icon name. Example: "mail-reply"
  2384. * @param int|string $size Optional. The size for the icon. (Example: lg, 2, 3, 4, 5)
  2385. * @param bool $fixWidth Optional. Whether add the fw class
  2386. * @param string $additionalClass Optional. Additional class
  2387. *
  2388. * @return string
  2389. */
  2390. public static function returnFontAwesomeIcon(
  2391. $name,
  2392. $size = '',
  2393. $fixWidth = false,
  2394. $additionalClass = ''
  2395. ) {
  2396. $className = "fa fa-$name";
  2397. if ($fixWidth) {
  2398. $className .= ' fa-fw';
  2399. }
  2400. switch ($size) {
  2401. case 'xs':
  2402. case 'sm':
  2403. case 'lg':
  2404. $className .= " fa-{$size}";
  2405. break;
  2406. case 2:
  2407. case 3:
  2408. case 4:
  2409. case 5:
  2410. $className .= " fa-{$size}x";
  2411. break;
  2412. }
  2413. if (!empty($additionalClass)) {
  2414. $className .= " $additionalClass";
  2415. }
  2416. $icon = self::tag('em', null, ['class' => $className]);
  2417. return "$icon ";
  2418. }
  2419. /**
  2420. * @param string $title
  2421. * @param string $content
  2422. * @param null $id
  2423. * @param array $params
  2424. * @param null $idAccordion
  2425. * @param null $idCollapse
  2426. * @param bool|true $open
  2427. * @param bool|false $fullClickable
  2428. *
  2429. * @return string|null
  2430. *
  2431. * @todo rework function to easy use
  2432. */
  2433. public static function panelCollapse(
  2434. $title,
  2435. $content,
  2436. $id = null,
  2437. $params = [],
  2438. $idAccordion = null,
  2439. $idCollapse = null,
  2440. $open = true,
  2441. $fullClickable = false
  2442. ) {
  2443. if (!empty($idAccordion)) {
  2444. $headerClass = '';
  2445. $headerClass .= $fullClickable ? 'center-block ' : '';
  2446. $headerClass .= $open ? '' : 'collapsed';
  2447. $contentClass = 'panel-collapse collapse ';
  2448. $contentClass .= $open ? 'in' : '';
  2449. $ariaExpanded = $open ? 'true' : 'false';
  2450. $html = <<<HTML
  2451. <div class="card" id="$id">
  2452. <div class="card-header">
  2453. $title
  2454. </div>
  2455. <div class="card-body">$content</div>
  2456. </div>
  2457. HTML;
  2458. } else {
  2459. if (!empty($id)) {
  2460. $params['id'] = $id;
  2461. }
  2462. $params['class'] = 'card';
  2463. $html = null;
  2464. if (!empty($title)) {
  2465. $html .= '<div class="card-header">'.$title.'</div>'.PHP_EOL;
  2466. }
  2467. $html .= '<div class="card-body">'.$content.'</div>'.PHP_EOL;
  2468. $html = self::div($html, $params);
  2469. }
  2470. return $html;
  2471. }
  2472. /**
  2473. * Returns the string "1 day ago" with a link showing the exact date time.
  2474. *
  2475. * @param string $dateTime in UTC or a DateTime in UTC
  2476. *
  2477. * @return string
  2478. */
  2479. public static function dateToStringAgoAndLongDate($dateTime)
  2480. {
  2481. if (empty($dateTime) || $dateTime === '0000-00-00 00:00:00') {
  2482. return '';
  2483. }
  2484. if ($dateTime instanceof \DateTime) {
  2485. $dateTime = $dateTime->format('Y-m-d H:i:s');
  2486. }
  2487. return self::tip(
  2488. date_to_str_ago($dateTime),
  2489. api_convert_and_format_date($dateTime, DATE_TIME_FORMAT_LONG)
  2490. //api_get_local_time($dateTime)
  2491. );
  2492. }
  2493. /**
  2494. * @param array $userInfo
  2495. * @param string $status
  2496. * @param string $toolbar
  2497. *
  2498. * @return string
  2499. */
  2500. public static function getUserCard($userInfo, $status = '', $toolbar = '')
  2501. {
  2502. if (empty($userInfo)) {
  2503. return '';
  2504. }
  2505. if (!empty($status)) {
  2506. $status = '<div class="items-user-status">'.$status.'</div>';
  2507. }
  2508. if (!empty($toolbar)) {
  2509. $toolbar = '<div class="btn-group pull-right">'.$toolbar.'</div>';
  2510. }
  2511. return '<div id="user_card_'.$userInfo['id'].'" class="card d-flex flex-row">
  2512. <img src="'.$userInfo['avatar'].'" class="rounded">
  2513. <h3 class="card-title">'.$userInfo['complete_name'].'</h3>
  2514. <div class="card-body">
  2515. <div class="card-title">
  2516. '.$status.'
  2517. '.$toolbar.'
  2518. </div>
  2519. </div>
  2520. <hr />
  2521. </div>';
  2522. }
  2523. /**
  2524. * @param string $fileName
  2525. * @param string $fileUrl
  2526. *
  2527. * @return string
  2528. */
  2529. public static function fileHtmlGuesser($fileName, $fileUrl)
  2530. {
  2531. $data = pathinfo($fileName);
  2532. //$content = self::url($data['basename'], $fileUrl);
  2533. $content = '';
  2534. switch ($data['extension']) {
  2535. case 'webm':
  2536. case 'mp4':
  2537. case 'ogg':
  2538. $content = '<video style="width: 400px; height:100%;" src="'.$fileUrl.'"></video>';
  2539. // Allows video to play when loading during an ajax call
  2540. $content .= "<script>jQuery('video:not(.skip), audio:not(.skip)').mediaelementplayer();</script>";
  2541. break;
  2542. case 'jpg':
  2543. case 'jpeg':
  2544. case 'gif':
  2545. case 'png':
  2546. $content = '<img class="img-responsive" src="'.$fileUrl.'" />';
  2547. break;
  2548. default:
  2549. //$html = self::url($data['basename'], $fileUrl);
  2550. break;
  2551. }
  2552. //$html = self::url($content, $fileUrl, ['ajax']);
  2553. return $content;
  2554. }
  2555. /**
  2556. * @param string $frameName
  2557. *
  2558. * @return string
  2559. */
  2560. public static function getFrameReadyBlock($frameName)
  2561. {
  2562. $webPublicPath = api_get_path(WEB_PUBLIC_PATH);
  2563. $videoFeatures = [
  2564. 'playpause',
  2565. 'current',
  2566. 'progress',
  2567. 'duration',
  2568. 'tracks',
  2569. 'volume',
  2570. 'fullscreen',
  2571. 'vrview',
  2572. 'markersrolls',
  2573. ];
  2574. $features = api_get_configuration_value('video_features');
  2575. $videoPluginsJS = [];
  2576. $videoPluginCSS = [];
  2577. if (!empty($features) && isset($features['features'])) {
  2578. foreach ($features['features'] as $feature) {
  2579. if ($feature === 'vrview') {
  2580. continue;
  2581. }
  2582. $defaultFeatures[] = $feature;
  2583. $videoPluginsJS[] = "mediaelement/plugins/$feature/$feature.js";
  2584. $videoPluginCSS[] = "mediaelement/plugins/$feature/$feature.css";
  2585. }
  2586. }
  2587. $videoPluginFiles = '';
  2588. foreach ($videoPluginsJS as $file) {
  2589. $videoPluginFiles .= '{type: "script", src: "'.$webPublicPath.'assets/'.$file.'"},';
  2590. }
  2591. $videoPluginCssFiles = '';
  2592. foreach ($videoPluginCSS as $file) {
  2593. $videoPluginCssFiles .= '{type: "stylesheet", src: "'.$webPublicPath.'assets/'.$file.'"},';
  2594. }
  2595. $translateHtml = '';
  2596. $translate = api_get_configuration_value('translate_html');
  2597. if ($translate) {
  2598. $translateHtml = '{type:"script", src:"'.api_get_path(WEB_AJAX_PATH).'lang.ajax.php?a=translate_html&'.api_get_cidreq().'"},';
  2599. }
  2600. $videoFeatures = implode("','", $videoFeatures);
  2601. $frameReady = '
  2602. $.frameReady(function() {
  2603. $(function () {
  2604. $("video:not(.skip), audio:not(.skip)").mediaelementplayer({
  2605. pluginPath: "'.$webPublicPath.'assets/mediaelement/plugins/",
  2606. features: [\''.$videoFeatures.'\'],
  2607. success: function(mediaElement, originalNode, instance) {
  2608. '.ChamiloApi::getQuizMarkersRollsJS().'
  2609. },
  2610. vrPath: "'.$webPublicPath.'assets/vrview/build/vrview.js"
  2611. });
  2612. });
  2613. },
  2614. "'.$frameName.'",
  2615. [
  2616. {type:"script", src:"'.api_get_jquery_web_path().'", deps: [
  2617. {type:"script", src:"'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.highlight.js"},
  2618. {type:"script", src:"'.api_get_path(WEB_CODE_PATH).'glossary/glossary.js.php?'.api_get_cidreq().'"},
  2619. {type:"script", src:"'.api_get_jquery_ui_js_web_path().'"},
  2620. {type:"script", src: "'.$webPublicPath.'build/libs/mediaelement/mediaelement-and-player.min.js",
  2621. deps: [
  2622. {type:"script", src: "'.$webPublicPath.'build/libs/mediaelement/plugins/vrview/vrview.js"},
  2623. {type:"script", src: "'.$webPublicPath.'build/libs/mediaelement/plugins/markersrolls/markersrolls.js"},
  2624. '.$videoPluginFiles.'
  2625. ]},
  2626. '.$translateHtml.'
  2627. ]},
  2628. '.$videoPluginCssFiles.'
  2629. {type:"script", src:"'.$webPublicPath.'build/libs/mathjax/MathJax.js?config=AM_HTMLorMML"},
  2630. {type:"stylesheet", src:"'.$webPublicPath.'assets/jquery-ui/themes/smoothness/jquery-ui.min.css"},
  2631. {type:"stylesheet", src:"'.$webPublicPath.'assets/jquery-ui/themes/smoothness/theme.css"},
  2632. ]);';
  2633. return $frameReady;
  2634. }
  2635. /**
  2636. * @param string $image
  2637. * @param int $size
  2638. *
  2639. * @return string
  2640. */
  2641. public static function get_icon_path($image, $size = ICON_SIZE_SMALL)
  2642. {
  2643. return self::return_icon($image, '', [], $size, false, true);
  2644. }
  2645. /**
  2646. * @param string $image
  2647. * @param int $size
  2648. * @param string $name
  2649. *
  2650. * @return string
  2651. */
  2652. public static function get_image($image, $size = ICON_SIZE_SMALL, $name = '')
  2653. {
  2654. return self::return_icon($image, $name, [], $size);
  2655. }
  2656. public static function dropdownMenu($items = [], array $attr = [])
  2657. {
  2658. $links = null;
  2659. $url = null;
  2660. foreach ($items as $row) {
  2661. $url = self::url($row['icon'].$row['item'], $row['url'], ['class' => 'dropdown-item']);
  2662. $links .= self::tag('li', $url);
  2663. }
  2664. $html = self::tag('ul', $links, $attr);
  2665. return $html;
  2666. }
  2667. /**
  2668. * @param $id
  2669. *
  2670. * @return array|mixed
  2671. */
  2672. public static function randomColor($id)
  2673. {
  2674. static $colors = [];
  2675. if (!empty($colors[$id])) {
  2676. return $colors[$id];
  2677. } else {
  2678. $color = substr(md5(time() * $id), 0, 6);
  2679. $c1 = hexdec(substr($color, 0, 2));
  2680. $c2 = hexdec(substr($color, 2, 2));
  2681. $c3 = hexdec(substr($color, 4, 2));
  2682. $luminosity = $c1 + $c2 + $c3;
  2683. $type = '#000000';
  2684. if ($luminosity < (255 + 255 + 255) / 2) {
  2685. $type = '#FFFFFF';
  2686. }
  2687. $result = [
  2688. 'color' => '#'.$color,
  2689. 'luminosity' => $type,
  2690. ];
  2691. $colors[$id] = $result;
  2692. return $result; // example: #fc443a
  2693. }
  2694. }
  2695. }