display.lib.php 100 KB

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