display.lib.php 98 KB

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