display.lib.php 95 KB

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