display.lib.php 88 KB

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