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 array(
  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 = array(),
  207. $paging_options = array(),
  208. $query_vars = null,
  209. $form_actions = array(),
  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 = array(),
  251. $paging_options = array(),
  252. $query_vars = null,
  253. $form_actions = array(),
  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 = array(),
  292. $query_vars = null,
  293. $form_actions = array(),
  294. $visibility_options = true,
  295. $sort_data = true,
  296. $grid_class = array()
  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 = array(),
  335. $query_vars = null,
  336. $form_actions = array(),
  337. $visibility_options = true,
  338. $sort_data = true,
  339. $grid_class = array(),
  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 = array(),
  386. $paging_options = array(),
  387. $query_vars = null,
  388. $column_show = array(),
  389. $column_order = array(),
  390. $form_actions = array()
  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, array('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 = array(),
  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 = array(),
  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 = array())
  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, array('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 = array())
  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 = array())
  903. {
  904. return self::tag('div', $content, $attributes);
  905. }
  906. /**
  907. * Creates a span tag
  908. */
  909. public static function span($content, $attributes = array())
  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 = array())
  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 = array())
  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. *
  945. */
  946. public static function select(
  947. $name,
  948. $values,
  949. $default = -1,
  950. $extra_attributes = array(),
  951. $show_blank_item = true,
  952. $blank_item_text = null
  953. ) {
  954. $html = '';
  955. $extra = '';
  956. $default_id = 'id="'.$name.'" ';
  957. foreach ($extra_attributes as $key => $parameter) {
  958. if ($key == 'id') {
  959. $default_id = '';
  960. }
  961. $extra .= $key.'="'.$parameter.'" ';
  962. }
  963. $html .= '<select name="'.$name.'" '.$default_id.' '.$extra.'>';
  964. if ($show_blank_item) {
  965. if (empty($blank_item_text)) {
  966. $blank_item_text = get_lang('Select');
  967. } else {
  968. $blank_item_text = Security::remove_XSS($blank_item_text);
  969. }
  970. $html .= self::tag(
  971. 'option',
  972. '-- '.$blank_item_text.' --',
  973. array('value' => '-1')
  974. );
  975. }
  976. if ($values) {
  977. foreach ($values as $key => $value) {
  978. if (is_array($value) && isset($value['name'])) {
  979. $value = $value['name'];
  980. }
  981. $html .= '<option value="'.$key.'"';
  982. if (is_array($default)) {
  983. foreach ($default as $item) {
  984. if ($item == $key) {
  985. $html .= ' selected="selected"';
  986. break;
  987. }
  988. }
  989. } else {
  990. if ($default == $key) {
  991. $html .= ' selected="selected"';
  992. }
  993. }
  994. $html .= '>'.$value.'</option>';
  995. }
  996. }
  997. $html .= '</select>';
  998. return $html;
  999. }
  1000. /**
  1001. * Creates a tab menu
  1002. * Requirements: declare the jquery, jquery-ui libraries + the jquery-ui.css
  1003. * in the $htmlHeadXtra variable before the display_header
  1004. * Add this script
  1005. * @example
  1006. * <script>
  1007. $(function() {
  1008. $( "#tabs" ).tabs();
  1009. });
  1010. </script>
  1011. * @param array $headers list of the tab titles
  1012. * @param array $items
  1013. * @param string $id id of the container of the tab in the example "tabs"
  1014. * @param array $attributes for the ul
  1015. * @param array $ul_attributes
  1016. *
  1017. * @return string
  1018. */
  1019. public static function tabs(
  1020. $headers,
  1021. $items,
  1022. $id = 'tabs',
  1023. $attributes = array(),
  1024. $ul_attributes = array()
  1025. ) {
  1026. if (empty($headers) || count($headers) == 0) {
  1027. return '';
  1028. }
  1029. $lis = '';
  1030. $i = 1;
  1031. foreach ($headers as $item) {
  1032. $active = '';
  1033. if ($i == 1) {
  1034. $active = ' active';
  1035. }
  1036. $item = self::tag(
  1037. 'a',
  1038. $item,
  1039. array(
  1040. 'href' => '#'.$id.'-'.$i,
  1041. 'role' => 'tab',
  1042. 'data-toggle' => 'tab',
  1043. 'id' => $id.$i,
  1044. )
  1045. );
  1046. $ul_attributes['role'] = 'presentation';
  1047. $ul_attributes['class'] = $active;
  1048. $lis .= self::tag('li', $item, $ul_attributes);
  1049. $i++;
  1050. }
  1051. $ul = self::tag(
  1052. 'ul',
  1053. $lis,
  1054. [
  1055. 'class' => 'nav nav-tabs tabs-margin',
  1056. 'role' => 'tablist',
  1057. 'id' => 'ul_'.$id
  1058. ]
  1059. );
  1060. $i = 1;
  1061. $divs = '';
  1062. foreach ($items as $content) {
  1063. $active = '';
  1064. if ($i == 1) {
  1065. $active = ' active';
  1066. }
  1067. $divs .= self::tag(
  1068. 'div',
  1069. $content,
  1070. array('id' => $id.'-'.$i, 'class' => 'tab-pane '.$active, 'role' => 'tabpanel')
  1071. );
  1072. $i++;
  1073. }
  1074. $attributes['id'] = $id;
  1075. $attributes['role'] = 'tabpanel';
  1076. $attributes['class'] = 'tab-wrapper';
  1077. $main_div = self::tag(
  1078. 'div',
  1079. $ul.self::tag('div', $divs, ['class' => 'tab-content']),
  1080. $attributes
  1081. );
  1082. return $main_div;
  1083. }
  1084. /**
  1085. * @param $headers
  1086. * @param null $selected
  1087. *
  1088. * @return string
  1089. */
  1090. public static function tabsOnlyLink($headers, $selected = null)
  1091. {
  1092. $id = uniqid();
  1093. $i = 1;
  1094. $lis = null;
  1095. foreach ($headers as $item) {
  1096. $class = null;
  1097. if ($i == $selected) {
  1098. $class = 'active';
  1099. }
  1100. $item = self::tag(
  1101. 'a',
  1102. $item['content'],
  1103. array('id' => $id.'-'.$i, 'href' => $item['url'])
  1104. );
  1105. $lis .= self::tag('li', $item, array('class' => $class));
  1106. $i++;
  1107. }
  1108. return self::tag(
  1109. 'ul',
  1110. $lis,
  1111. array('class' => 'nav nav-tabs tabs-margin')
  1112. );
  1113. }
  1114. /**
  1115. * In order to display a grid using jqgrid you have to:
  1116. * @example
  1117. * After your Display::display_header function you have to add the nex javascript code:
  1118. * <script>
  1119. * echo Display::grid_js('my_grid_name', $url,$columns, $column_model, $extra_params,array());
  1120. * // for more information of this function check the grid_js() function
  1121. * </script>
  1122. * //Then you have to call the grid_html
  1123. * echo Display::grid_html('my_grid_name');
  1124. * As you can see both function use the same "my_grid_name" this is very important otherwise nothing will work
  1125. *
  1126. * @param string the div id, this value must be the same with the first parameter of Display::grid_js()
  1127. * @return string html
  1128. *
  1129. */
  1130. public static function grid_html($div_id)
  1131. {
  1132. $table = self::tag('table', '', array('id' => $div_id));
  1133. $table .= self::tag('div', '', array('id' => $div_id.'_pager'));
  1134. return $table;
  1135. }
  1136. /**
  1137. * @param string $label
  1138. * @param string $form_item
  1139. * @return string
  1140. */
  1141. public static function form_row($label, $form_item)
  1142. {
  1143. $label = self::span($label, array('class' =>'control-label'));
  1144. $form_item = self::div($form_item, array('class' =>'controls'));
  1145. return self::div($label.$form_item, array('class'=>'control-group'));
  1146. }
  1147. /**
  1148. * This is a wrapper to use the jqgrid in Chamilo.
  1149. * For the other jqgrid options visit http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options
  1150. * This function need to be in the ready jquery function
  1151. * example --> $(function() { <?php echo Display::grid_js('grid' ...); ?> }
  1152. * In order to work this function needs the Display::grid_html function with the same div id
  1153. *
  1154. * @param string $div_id div id
  1155. * @param string $url url where the jqgrid will ask for data (if datatype = json)
  1156. * @param array $column_names Visible columns (you should use get_lang).
  1157. * An array in which we place the names of the columns.
  1158. * This is the text that appears in the head of the grid (Header layer).
  1159. * Example: colname {name:'date', index:'date', width:120, align:'right'},
  1160. * @param array $column_model the column model : Array which describes the parameters of the columns.
  1161. * This is the most important part of the grid.
  1162. * For a full description of all valid values see colModel API. See the url above.
  1163. * @param array $extra_params extra parameters
  1164. * @param array $data data that will be loaded
  1165. * @param string $formatter A string that will be appended to the JSON returned
  1166. * @param bool $fixed_width not implemented yet
  1167. * @return string the js code
  1168. *
  1169. */
  1170. public static function grid_js(
  1171. $div_id,
  1172. $url,
  1173. $column_names,
  1174. $column_model,
  1175. $extra_params,
  1176. $data = array(),
  1177. $formatter = '',
  1178. $fixed_width = false
  1179. ) {
  1180. $obj = new stdClass();
  1181. $obj->first = 'first';
  1182. if (!empty($url)) {
  1183. $obj->url = $url;
  1184. }
  1185. //This line should only be used/modified in case of having characters
  1186. // encoding problems - see #6159
  1187. //$column_names = array_map("utf8_encode", $column_names);
  1188. $obj->colNames = $column_names;
  1189. $obj->colModel = $column_model;
  1190. $obj->pager = '#'.$div_id.'_pager';
  1191. $obj->datatype = 'json';
  1192. $obj->viewrecords = 'true';
  1193. $all_value = 10000000;
  1194. // Default row quantity
  1195. if (!isset($extra_params['rowList'])) {
  1196. $extra_params['rowList'] = array(20, 50, 100, 500, 1000, $all_value);
  1197. }
  1198. $json = '';
  1199. if (!empty($extra_params['datatype'])) {
  1200. $obj->datatype = $extra_params['datatype'];
  1201. }
  1202. // Row even odd style.
  1203. $obj->altRows = true;
  1204. if (!empty($extra_params['altRows'])) {
  1205. $obj->altRows = $extra_params['altRows'];
  1206. }
  1207. if (!empty($extra_params['sortname'])) {
  1208. $obj->sortname = $extra_params['sortname'];
  1209. }
  1210. if (!empty($extra_params['sortorder'])) {
  1211. $obj->sortorder = $extra_params['sortorder'];
  1212. }
  1213. if (!empty($extra_params['rowList'])) {
  1214. $obj->rowList = $extra_params['rowList'];
  1215. }
  1216. // Sets how many records we want to view in the grid
  1217. $obj->rowNum = 20;
  1218. if (!empty($extra_params['rowNum'])) {
  1219. $obj->rowNum = $extra_params['rowNum'];
  1220. } else {
  1221. // Try to load max rows from Session
  1222. $urlInfo = parse_url($url);
  1223. if (isset($urlInfo['query'])) {
  1224. parse_str($urlInfo['query'], $query);
  1225. if (isset($query['a'])) {
  1226. $action = $query['a'];
  1227. // This value is set in model.ajax.php
  1228. $savedRows = Session::read('max_rows_'.$action);
  1229. if (!empty($savedRows)) {
  1230. $obj->rowNum = $savedRows;
  1231. }
  1232. }
  1233. }
  1234. }
  1235. if (!empty($extra_params['viewrecords'])) {
  1236. $obj->viewrecords = $extra_params['viewrecords'];
  1237. }
  1238. $beforeSelectRow = null;
  1239. if (isset($extra_params['beforeSelectRow'])) {
  1240. $beforeSelectRow = "beforeSelectRow: ".$extra_params['beforeSelectRow'].", ";
  1241. unset($extra_params['beforeSelectRow']);
  1242. }
  1243. // Adding extra params
  1244. if (!empty($extra_params)) {
  1245. foreach ($extra_params as $key => $element) {
  1246. // the groupHeaders key gets a special treatment
  1247. if ($key != 'groupHeaders') {
  1248. $obj->$key = $element;
  1249. }
  1250. }
  1251. }
  1252. // Adding static data.
  1253. if (!empty($data)) {
  1254. $data_var = $div_id.'_data';
  1255. $json .= ' var '.$data_var.' = '.json_encode($data).';';
  1256. $obj->data = $data_var;
  1257. $obj->datatype = 'local';
  1258. $json .= "\n";
  1259. }
  1260. $obj->end = 'end';
  1261. $json_encode = json_encode($obj);
  1262. if (!empty($data)) {
  1263. //Converts the "data":"js_variable" to "data":js_variable,
  1264. // otherwise it will not work
  1265. $json_encode = str_replace('"data":"'.$data_var.'"', '"data":'.$data_var.'', $json_encode);
  1266. }
  1267. // Fixing true/false js values that doesn't need the ""
  1268. $json_encode = str_replace(':"true"', ':true', $json_encode);
  1269. // wrap_cell is not a valid jqgrid attributes is a hack to wrap a text
  1270. $json_encode = str_replace('"wrap_cell":true', 'cellattr : function(rowId, value, rowObject, colModel, arrData) { return \'class = "jqgrid_whitespace"\'; }', $json_encode);
  1271. $json_encode = str_replace(':"false"', ':false', $json_encode);
  1272. $json_encode = str_replace('"formatter":"action_formatter"', 'formatter:action_formatter', $json_encode);
  1273. $json_encode = str_replace(array('{"first":"first",', '"end":"end"}'), '', $json_encode);
  1274. // Creating the jqgrid element.
  1275. $json .= '$("#'.$div_id.'").jqGrid({';
  1276. //$json .= $beforeSelectRow;
  1277. $json .= $json_encode;
  1278. $json .= '});';
  1279. // Grouping headers option
  1280. if (isset($extra_params['groupHeaders'])) {
  1281. $groups = '';
  1282. foreach ($extra_params['groupHeaders'] as $group) {
  1283. //{ "startColumnName" : "courses", "numberOfColumns" : 1, "titleText" : "Order Info" },
  1284. $groups .= '{ "startColumnName" : "'.$group['startColumnName'].'", "numberOfColumns" : '.$group['numberOfColumns'].', "titleText" : "'.$group['titleText'].'" },';
  1285. }
  1286. $json .= '$("#'.$div_id.'").jqGrid("setGroupHeaders", {
  1287. "useColSpanStyle" : false,
  1288. "groupHeaders" : [
  1289. ' . $groups.'
  1290. ]
  1291. });';
  1292. }
  1293. $all_text = addslashes(get_lang('All'));
  1294. $json .= '$("'.$obj->pager.' option[value='.$all_value.']").text("'.$all_text.'");';
  1295. $json .= "\n";
  1296. // Adding edit/delete icons.
  1297. $json .= $formatter;
  1298. return $json;
  1299. }
  1300. /**
  1301. * @param array $headers
  1302. * @param array $rows
  1303. * @param array $attributes
  1304. * @return string
  1305. */
  1306. public static function table($headers, $rows, $attributes = array())
  1307. {
  1308. if (empty($attributes)) {
  1309. $attributes['class'] = 'data_table';
  1310. }
  1311. $table = new HTML_Table($attributes);
  1312. $row = 0;
  1313. $column = 0;
  1314. // Course headers
  1315. if (!empty($headers)) {
  1316. foreach ($headers as $item) {
  1317. $table->setHeaderContents($row, $column, $item);
  1318. $column++;
  1319. }
  1320. $row = 1;
  1321. $column = 0;
  1322. }
  1323. if (!empty($rows)) {
  1324. foreach ($rows as $content) {
  1325. $table->setCellContents($row, $column, $content);
  1326. $row++;
  1327. }
  1328. }
  1329. return $table->toHtml();
  1330. }
  1331. /**
  1332. * Returns the "what's new" icon notifications
  1333. *
  1334. * The general logic of this function is to track the last time the user
  1335. * entered the course and compare to what has changed inside this course
  1336. * since then, based on the item_property table inside this course. Note that,
  1337. * if the user never entered the course before, he will not see notification
  1338. * icons. This function takes session ID into account (if any) and only shows
  1339. * the corresponding notifications.
  1340. * @param array $courseInfo Course information array, containing at least elements 'db' and 'k'
  1341. * @param bool $loadAjax
  1342. * @return string The HTML link to be shown next to the course
  1343. */
  1344. public static function show_notification($courseInfo, $loadAjax = true)
  1345. {
  1346. if (empty($courseInfo)) {
  1347. return '';
  1348. }
  1349. $t_track_e_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  1350. $course_tool_table = Database::get_course_table(TABLE_TOOL_LIST);
  1351. $tool_edit_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1352. $course_code = Database::escape_string($courseInfo['code']);
  1353. $user_id = api_get_user_id();
  1354. $course_id = (int) $courseInfo['real_id'];
  1355. $sessionId = (int) $courseInfo['id_session'];
  1356. $status = (int) $courseInfo['status'];
  1357. $loadNotificationsByAjax = api_get_configuration_value('user_portal_load_notification_by_ajax');
  1358. if ($loadNotificationsByAjax) {
  1359. if ($loadAjax) {
  1360. $id = 'notification_'.$course_id.'_'.$sessionId.'_'.$status;
  1361. Session::write($id, true);
  1362. return '<span id ="'.$id.'" class="course_notification"></span>';
  1363. }
  1364. }
  1365. // Get the user's last access dates to all tools of this course
  1366. $sql = "SELECT *
  1367. FROM $t_track_e_access
  1368. WHERE
  1369. c_id = $course_id AND
  1370. access_user_id = '$user_id' AND
  1371. access_session_id ='".$sessionId."'
  1372. ORDER BY access_date DESC
  1373. LIMIT 1
  1374. ";
  1375. $result = Database::query($sql);
  1376. // latest date by default is the creation date
  1377. $latestDate = $courseInfo['creation_date'];
  1378. if (Database::num_rows($result)) {
  1379. $row = Database::fetch_array($result, 'ASSOC');
  1380. $latestDate = $row['access_date'];
  1381. }
  1382. $sessionCondition = api_get_session_condition(
  1383. $sessionId,
  1384. true,
  1385. false,
  1386. 'session_id'
  1387. );
  1388. $hideTools = [TOOL_NOTEBOOK, TOOL_CHAT];
  1389. // Get current tools in course
  1390. $sql = "SELECT name, link, image
  1391. FROM $course_tool_table
  1392. WHERE
  1393. c_id = $course_id AND
  1394. visibility = '1' AND
  1395. name NOT IN ('".implode("','", $hideTools)."')
  1396. ";
  1397. $result = Database::query($sql);
  1398. $tools = Database::store_result($result);
  1399. $group_ids = GroupManager::get_group_ids($courseInfo['real_id'], $user_id);
  1400. $group_ids[] = 0; //add group 'everyone'
  1401. $notifications = array();
  1402. if ($tools) {
  1403. foreach ($tools as $tool) {
  1404. $toolName = $tool['name'];
  1405. $toolName = Database::escape_string($toolName);
  1406. // Fix to get student publications
  1407. $toolCondition = " tool = '$toolName' AND ";
  1408. if ($toolName == 'student_publication' || $toolName == 'work') {
  1409. $toolCondition = " (tool = 'work' OR tool = 'student_publication') AND ";
  1410. }
  1411. $toolName = addslashes($toolName);
  1412. $sql = "SELECT * FROM $tool_edit_table
  1413. WHERE
  1414. c_id = $course_id AND
  1415. $toolCondition
  1416. lastedit_type NOT LIKE '%Deleted%' AND
  1417. lastedit_type NOT LIKE '%deleted%' AND
  1418. lastedit_type NOT LIKE '%DocumentInvisible%' AND
  1419. lastedit_date > '$latestDate' AND
  1420. lastedit_user_id != $user_id $sessionCondition AND
  1421. visibility != 2 AND
  1422. (to_user_id IN ('$user_id', '0') OR to_user_id IS NULL) AND
  1423. (to_group_id IN ('".implode("','", $group_ids)."') OR to_group_id IS NULL)
  1424. ORDER BY lastedit_date DESC
  1425. LIMIT 1";
  1426. $result = Database::query($sql);
  1427. $latestChange = Database::fetch_array($result, 'ASSOC');
  1428. if ($latestChange) {
  1429. $latestChange['link'] = $tool['link'];
  1430. $latestChange['image'] = $tool['image'];
  1431. $latestChange['tool'] = $tool['name'];
  1432. $notifications[$toolName] = $latestChange;
  1433. }
  1434. }
  1435. }
  1436. // Show all tool icons where there is something new.
  1437. $return = '';
  1438. foreach ($notifications as $notification) {
  1439. $toolName = $notification['tool'];
  1440. if (!(
  1441. $notification['visibility'] == '1' ||
  1442. ($status == '1' && $notification['visibility'] == '0') ||
  1443. !isset($notification['visibility'])
  1444. )
  1445. ) {
  1446. continue;
  1447. }
  1448. if ($toolName == TOOL_SURVEY) {
  1449. $survey_info = SurveyManager::get_survey($notification['ref'], 0, $course_code);
  1450. if (!empty($survey_info)) {
  1451. $invited_users = SurveyUtil::get_invited_users(
  1452. $survey_info['code'],
  1453. $course_code
  1454. );
  1455. if (!in_array($user_id, $invited_users['course_users'])) {
  1456. continue;
  1457. }
  1458. }
  1459. }
  1460. if ($notification['tool'] == TOOL_LEARNPATH) {
  1461. if (!learnpath::is_lp_visible_for_student($notification['ref'], $user_id, $course_code)) {
  1462. continue;
  1463. }
  1464. }
  1465. if ($notification['tool'] == TOOL_DROPBOX) {
  1466. $notification['link'] = 'dropbox/dropbox_download.php?id='.$notification['ref'];
  1467. }
  1468. if ($notification['tool'] == 'work' &&
  1469. $notification['lastedit_type'] == 'DirectoryCreated'
  1470. ) {
  1471. $notification['lastedit_type'] = 'WorkAdded';
  1472. }
  1473. $lastDate = api_get_local_time($notification['lastedit_date']);
  1474. $type = $notification['lastedit_type'];
  1475. if ($type == 'CalendareventVisible') {
  1476. $type = 'Visible';
  1477. }
  1478. $label = get_lang('TitleNotification').": ".get_lang($type)." ($lastDate)";
  1479. if (strpos($notification['link'], '?') === false) {
  1480. $notification['link'] = $notification['link'].'?notification=1';
  1481. } else {
  1482. $notification['link'] = $notification['link'].'&notification=1';
  1483. }
  1484. $image = substr($notification['image'], 0, -4).'.png';
  1485. $return .= self::url(
  1486. self::return_icon($image, $label),
  1487. api_get_path(WEB_CODE_PATH).
  1488. $notification['link'].'&cidReq='.$course_code.
  1489. '&ref='.$notification['ref'].
  1490. '&gidReq='.$notification['to_group_id'].
  1491. '&id_session='.$sessionId
  1492. ).PHP_EOL;
  1493. }
  1494. return $return;
  1495. }
  1496. /**
  1497. * Get the session box details as an array
  1498. * @param int Session ID
  1499. * @return array Empty array or session array
  1500. * ['title'=>'...','category'=>'','dates'=>'...','coach'=>'...','active'=>true/false,'session_category_id'=>int]
  1501. */
  1502. public static function get_session_title_box($session_id)
  1503. {
  1504. global $nosession;
  1505. if (!$nosession) {
  1506. global $now, $date_start, $date_end;
  1507. }
  1508. $output = array();
  1509. $active = false;
  1510. if (!$nosession) {
  1511. $main_user_table = Database::get_main_table(TABLE_MAIN_USER);
  1512. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  1513. // Request for the name of the general coach
  1514. $sql = 'SELECT tu.lastname, tu.firstname, ts.*
  1515. FROM '.$tbl_session.' ts
  1516. LEFT JOIN '.$main_user_table.' tu
  1517. ON ts.id_coach = tu.user_id
  1518. WHERE ts.id = '.intval($session_id);
  1519. $rs = Database::query($sql);
  1520. $session_info = Database::store_result($rs, 'ASSOC');
  1521. $session_info = $session_info[0];
  1522. $session = array();
  1523. $session['category_id'] = $session_info['session_category_id'];
  1524. $session['title'] = $session_info['name'];
  1525. $session['id_coach'] = $session_info['id_coach'];
  1526. $session['coach'] = '';
  1527. $session['dates'] = '';
  1528. if (api_get_setting('show_session_coach') === 'true') {
  1529. $session['coach'] = get_lang('GeneralCoach').': '.api_get_person_name($session_info['firstname'], $session_info['lastname']);
  1530. }
  1531. if (
  1532. ($session_info['access_end_date'] == '0000-00-00 00:00:00' && $session_info['access_start_date'] == '0000-00-00 00:00:00') ||
  1533. (empty($session_info['access_end_date']) && empty($session_info['access_start_date']))
  1534. ) {
  1535. if (isset($session_info['duration']) && !empty($session_info['duration'])) {
  1536. $daysLeft = SessionManager::getDayLeftInSession($session_info, api_get_user_id());
  1537. $session['duration'] = $daysLeft >= 0
  1538. ? sprintf(get_lang('SessionDurationXDaysLeft'), $daysLeft)
  1539. : get_lang('YourSessionTimeHasExpired');
  1540. }
  1541. $active = true;
  1542. } else {
  1543. $dates = SessionManager::parseSessionDates($session_info, true);
  1544. $session['dates'] = $dates['access'];
  1545. if (api_get_setting('show_session_coach') === 'true') {
  1546. $session['coach'] = api_get_person_name(
  1547. $session_info['firstname'],
  1548. $session_info['lastname']
  1549. );
  1550. }
  1551. $active = $date_start <= $now && $date_end >= $now;
  1552. }
  1553. $session['active'] = $active;
  1554. $session['session_category_id'] = $session_info['session_category_id'];
  1555. $session['visibility'] = $session_info['visibility'];
  1556. $session['num_users'] = $session_info['nbr_users'];
  1557. $session['num_courses'] = $session_info['nbr_courses'];
  1558. $session['description'] = $session_info['description'];
  1559. $session['show_description'] = $session_info['show_description'];
  1560. $entityManager = Database::getManager();
  1561. $fieldValuesRepo = $entityManager->getRepository('ChamiloCoreBundle:ExtraFieldValues');
  1562. $extraFieldValues = $fieldValuesRepo->getVisibleValues(
  1563. ExtraField::SESSION_FIELD_TYPE,
  1564. $session_id
  1565. );
  1566. $session['extra_fields'] = [];
  1567. foreach ($extraFieldValues as $value) {
  1568. $session['extra_fields'][] = [
  1569. 'field' => [
  1570. 'variable' => $value->getField()->getVariable(),
  1571. 'display_text' => $value->getField()->getDisplayText()
  1572. ],
  1573. 'value' => $value->getValue()
  1574. ];
  1575. }
  1576. $output = $session;
  1577. }
  1578. return $output;
  1579. }
  1580. /**
  1581. * Return the five star HTML
  1582. *
  1583. * @param string $id of the rating ul element
  1584. * @param string $url that will be added (for jquery see hot_courses.tpl)
  1585. * @param array $point_info point info array see function CourseManager::get_course_ranking()
  1586. * @param bool $add_div_wrapper add a div wrapper
  1587. * @return string
  1588. **/
  1589. public static function return_rating_system(
  1590. $id,
  1591. $url,
  1592. $point_info = [],
  1593. $add_div_wrapper = true
  1594. ) {
  1595. $number_of_users_who_voted = isset($point_info['users_who_voted']) ? $point_info['users_who_voted'] : null;
  1596. $percentage = isset($point_info['point_average']) ? $point_info['point_average'] : 0;
  1597. if (!empty($percentage)) {
  1598. $percentage = $percentage * 125 / 100;
  1599. }
  1600. $accesses = isset($point_info['accesses']) ? $point_info['accesses'] : 0;
  1601. $star_label = sprintf(get_lang('XStarsOutOf5'), $point_info['point_average_star']);
  1602. $html = '<ul id="'.$id.'" class="star-rating">
  1603. <li class="current-rating" style="width:'.$percentage.'px;"></li>
  1604. <li><a href="javascript:void(0);" data-link="'.$url.'&amp;star=1" title="'.$star_label.'" class="one-star">1</a></li>
  1605. <li><a href="javascript:void(0);" data-link="'.$url.'&amp;star=2" title="'.$star_label.'" class="two-stars">2</a></li>
  1606. <li><a href="javascript:void(0);" data-link="'.$url.'&amp;star=3" title="'.$star_label.'" class="three-stars">3</a></li>
  1607. <li><a href="javascript:void(0);" data-link="'.$url.'&amp;star=4" title="'.$star_label.'" class="four-stars">4</a></li>
  1608. <li><a href="javascript:void(0);" data-link="'.$url.'&amp;star=5" title="'.$star_label.'" class="five-stars">5</a></li>
  1609. </ul>';
  1610. $labels = array();
  1611. $labels[] = $number_of_users_who_voted == 1 ? $number_of_users_who_voted.' '.get_lang('Vote') : $number_of_users_who_voted.' '.get_lang('Votes');
  1612. $labels[] = $accesses == 1 ? $accesses.' '.get_lang('Visit') : $accesses.' '.get_lang('Visits');
  1613. $labels[] = $point_info['user_vote'] ? get_lang('YourVote').' ['.$point_info['user_vote'].']' : get_lang('YourVote').' [?] ';
  1614. if (!$add_div_wrapper && api_is_anonymous()) {
  1615. $labels[] = self::tag('span', get_lang('LoginToVote'), array('class' => 'error'));
  1616. }
  1617. $html .= self::div(implode(' | ', $labels), array('id' => 'vote_label_'.$id, 'class' => 'vote_label_info'));
  1618. $html .= ' '.self::span(' ', array('id' => 'vote_label2_'.$id));
  1619. if ($add_div_wrapper) {
  1620. $html = self::div($html, array('id' => 'rating_wrapper_'.$id));
  1621. }
  1622. return $html;
  1623. }
  1624. /**
  1625. * @param string $title
  1626. * @param string $second_title
  1627. * @param string $size
  1628. * @param bool $filter
  1629. * @return string
  1630. */
  1631. public static function page_header($title, $second_title = null, $size = 'h2', $filter = true)
  1632. {
  1633. if ($filter) {
  1634. $title = Security::remove_XSS($title);
  1635. }
  1636. if (!empty($second_title)) {
  1637. if ($filter) {
  1638. $second_title = Security::remove_XSS($second_title);
  1639. }
  1640. $title .= "<small> $second_title</small>";
  1641. }
  1642. return '<'.$size.' class="page-header">'.$title.'</'.$size.'>';
  1643. }
  1644. public static function page_header_and_translate($title, $second_title = null)
  1645. {
  1646. $title = get_lang($title);
  1647. return self::page_header($title, $second_title);
  1648. }
  1649. public static function page_subheader_and_translate($title, $second_title = null)
  1650. {
  1651. $title = get_lang($title);
  1652. return self::page_subheader($title, $second_title);
  1653. }
  1654. public static function page_subheader($title, $second_title = null, $size = 'h3')
  1655. {
  1656. if (!empty($second_title)) {
  1657. $second_title = Security::remove_XSS($second_title);
  1658. $title .= "<small> $second_title<small>";
  1659. }
  1660. return '<'.$size.'>'.Security::remove_XSS($title).'</'.$size.'>';
  1661. }
  1662. public static function page_subheader2($title, $second_title = null)
  1663. {
  1664. return self::page_header($title, $second_title, 'h4');
  1665. }
  1666. public static function page_subheader3($title, $second_title = null)
  1667. {
  1668. return self::page_header($title, $second_title, 'h5');
  1669. }
  1670. /**
  1671. * @param array $list
  1672. * @return null|string
  1673. */
  1674. public static function description($list)
  1675. {
  1676. $html = null;
  1677. if (!empty($list)) {
  1678. $html = '<dl class="dl-horizontal">';
  1679. foreach ($list as $item) {
  1680. $html .= '<dt>'.$item['title'].'</dt>';
  1681. $html .= '<dd>'.$item['content'].'</dd>';
  1682. }
  1683. $html .= '</dl>';
  1684. }
  1685. return $html;
  1686. }
  1687. /**
  1688. * @param $percentage
  1689. * @param bool $show_percentage
  1690. * @param null $extra_info
  1691. * @return string
  1692. */
  1693. public static function bar_progress($percentage, $show_percentage = true, $extra_info = null)
  1694. {
  1695. $percentage = intval($percentage);
  1696. $div = '<div class="progress">
  1697. <div
  1698. class="progress-bar progress-bar-striped"
  1699. role="progressbar"
  1700. aria-valuenow="'.$percentage.'"
  1701. aria-valuemin="0"
  1702. aria-valuemax="100"
  1703. style="width: '.$percentage.'%;"
  1704. >';
  1705. if ($show_percentage) {
  1706. $div .= $percentage.'%';
  1707. } else {
  1708. if (!empty($extra_info)) {
  1709. $div .= $extra_info;
  1710. }
  1711. }
  1712. $div .= '</div>';
  1713. return $div;
  1714. }
  1715. /**
  1716. * @param string $count
  1717. * @param string $type
  1718. * @return null|string
  1719. */
  1720. public static function badge($count, $type = "warning")
  1721. {
  1722. $class = '';
  1723. switch ($type) {
  1724. case 'success':
  1725. $class = 'badge-success';
  1726. break;
  1727. case 'warning':
  1728. $class = 'badge-warning';
  1729. break;
  1730. case 'important':
  1731. $class = 'badge-important';
  1732. break;
  1733. case 'info':
  1734. $class = 'badge-info';
  1735. break;
  1736. case 'inverse':
  1737. $class = 'badge-inverse';
  1738. break;
  1739. }
  1740. if (!empty($count)) {
  1741. return ' <span class="badge '.$class.'">'.$count.'</span>';
  1742. }
  1743. return null;
  1744. }
  1745. /**
  1746. * @param array $badge_list
  1747. * @return string
  1748. */
  1749. public static function badge_group($badge_list)
  1750. {
  1751. $html = '<div class="badge-group">';
  1752. foreach ($badge_list as $badge) {
  1753. $html .= $badge;
  1754. }
  1755. $html .= '</div>';
  1756. return $html;
  1757. }
  1758. /**
  1759. * @param string $content
  1760. * @param string $type
  1761. * @return string
  1762. */
  1763. public static function label($content, $type = 'default')
  1764. {
  1765. switch ($type) {
  1766. case 'success':
  1767. $class = 'label-success';
  1768. break;
  1769. case 'warning':
  1770. $class = 'label-warning';
  1771. break;
  1772. case 'important':
  1773. //no break
  1774. case 'danger':
  1775. $class = 'label-danger';
  1776. break;
  1777. case 'info':
  1778. $class = 'label-info';
  1779. break;
  1780. case 'primary':
  1781. $class = 'label-primary';
  1782. break;
  1783. default:
  1784. $class = 'label-default';
  1785. break;
  1786. }
  1787. $html = '';
  1788. if (!empty($content)) {
  1789. $html = '<span class="label '.$class.'">';
  1790. $html .= $content;
  1791. $html .= '</span>';
  1792. }
  1793. return $html;
  1794. }
  1795. /**
  1796. * @param array $items
  1797. * @param string $class
  1798. * @return null|string
  1799. */
  1800. public static function actions($items, $class = 'new_actions')
  1801. {
  1802. $html = null;
  1803. if (!empty($items)) {
  1804. $html = '<div class="'.$class.'"><ul class="nav nav-pills">';
  1805. foreach ($items as $value) {
  1806. $class = null;
  1807. if (isset($value['active']) && $value['active']) {
  1808. $class = 'class ="active"';
  1809. }
  1810. if (basename($_SERVER['REQUEST_URI']) == basename($value['url'])) {
  1811. $class = 'class ="active"';
  1812. }
  1813. $html .= "<li $class >";
  1814. $attributes = isset($value['url_attributes']) ? $value['url_attributes'] : array();
  1815. $html .= self::url($value['content'], $value['url'], $attributes);
  1816. $html .= '</li>';
  1817. }
  1818. $html .= '</ul></div>';
  1819. $html .= '<br />';
  1820. }
  1821. return $html;
  1822. }
  1823. /**
  1824. * Prints a tooltip
  1825. * @param string $text
  1826. * @param string $tip
  1827. *
  1828. * @return string
  1829. */
  1830. public static function tip($text, $tip)
  1831. {
  1832. if (empty($tip)) {
  1833. return $text;
  1834. }
  1835. return self::span(
  1836. $text,
  1837. array('class' => 'boot-tooltip', 'title' => strip_tags($tip))
  1838. );
  1839. }
  1840. /**
  1841. * @param array $items
  1842. * @param string $type
  1843. * @param null $id
  1844. * @return null|string
  1845. */
  1846. public static function generate_accordion($items, $type = 'jquery', $id = null)
  1847. {
  1848. $html = null;
  1849. if (!empty($items)) {
  1850. if (empty($id)) {
  1851. $id = api_get_unique_id();
  1852. }
  1853. if ($type == 'jquery') {
  1854. $html = '<div class="accordion_jquery" id="'.$id.'">'; //using jquery
  1855. } else {
  1856. $html = '<div class="accordion" id="'.$id.'">'; //using bootstrap
  1857. }
  1858. $count = 1;
  1859. foreach ($items as $item) {
  1860. $html .= '<div class="accordion-my-group">';
  1861. $html .= '<div class="accordion-heading">
  1862. <a class="accordion-toggle" data-toggle="collapse" data-parent="#'.$id.'" href="#collapse'.$count.'">
  1863. '.$item['title'].'
  1864. </a>
  1865. </div>';
  1866. $html .= '<div id="collapse'.$count.'" class="accordion-body">';
  1867. $html .= '<div class="accordion-my-inner">
  1868. '.$item['content'].'
  1869. </div>
  1870. </div>';
  1871. }
  1872. $html .= '</div>';
  1873. }
  1874. return $html;
  1875. }
  1876. /**
  1877. * @param array $buttons
  1878. * @return string
  1879. */
  1880. public static function groupButton($buttons)
  1881. {
  1882. $html = '<div class="btn-group" role="group">';
  1883. foreach ($buttons as $button) {
  1884. $html .= $button;
  1885. }
  1886. $html .= '</div>';
  1887. return $html;
  1888. }
  1889. /**
  1890. * @todo use twig
  1891. * @param string $title
  1892. * @param array $elements
  1893. * @param bool $alignToRight
  1894. * @return string
  1895. */
  1896. public static function groupButtonWithDropDown($title, $elements, $alignToRight = false)
  1897. {
  1898. $html = '<div class="btn-group">
  1899. <button class="btn btn-default dropdown-toggle" data-toggle="dropdown">
  1900. '.$title.'
  1901. <span class="caret"></span></button>
  1902. <ul class="dropdown-menu ' . ($alignToRight ? 'dropdown-menu-right' : '').'">';
  1903. foreach ($elements as $item) {
  1904. $html .= self::tag('li', self::url($item['title'], $item['href']));
  1905. }
  1906. $html .= '</ul>
  1907. </div>';
  1908. return $html;
  1909. }
  1910. /**
  1911. * @param string $file
  1912. * @param array $params
  1913. * @return null|string
  1914. */
  1915. public static function getMediaPlayer($file, $params = array())
  1916. {
  1917. $fileInfo = pathinfo($file);
  1918. switch ($fileInfo['extension']) {
  1919. case 'mp3':
  1920. case 'webm':
  1921. $autoplay = null;
  1922. if (isset($params['autoplay']) && $params['autoplay'] == 'true') {
  1923. $autoplay = 'autoplay';
  1924. }
  1925. $width = isset($params['width']) ? 'width="'.$params['width'].'"' : null;
  1926. $id = isset($params['id']) ? $params['id'] : $fileInfo['basename'];
  1927. $class = isset($params['class']) ? ' class="'.$params['class'].'"' : null;
  1928. $html = '<audio id="'.$id.'" '.$class.' controls '.$autoplay.' '.$width.' src="'.$params['url'].'" >';
  1929. $html .= '<object width="'.$width.'" height="50" type="application/x-shockwave-flash" data="'.api_get_path(WEB_LIBRARY_PATH).'javascript/mediaelement/flashmediaelement.swf">
  1930. <param name="movie" value="'.api_get_path(WEB_LIBRARY_PATH).'javascript/mediaelement/flashmediaelement.swf" />
  1931. <param name="flashvars" value="controls=true&file='.$params['url'].'" />
  1932. </object>';
  1933. $html .= '</audio>';
  1934. return $html;
  1935. break;
  1936. case 'wav':
  1937. //no break;
  1938. case 'ogg':
  1939. $html = '<audio width="300px" controls src="'.$params['url'].'" >';
  1940. return $html;
  1941. break;
  1942. }
  1943. return null;
  1944. }
  1945. /**
  1946. * @param int $nextValue
  1947. * @param array $list
  1948. * @param int $current
  1949. * @param int $fixedValue
  1950. * @param array $conditions
  1951. * @param string $link
  1952. * @param bool $isMedia
  1953. * @param bool $addHeaders
  1954. * @param array $linkAttributes
  1955. * @return string
  1956. */
  1957. public static function progressPaginationBar(
  1958. $nextValue,
  1959. $list,
  1960. $current,
  1961. $fixedValue = null,
  1962. $conditions = [],
  1963. $link = null,
  1964. $isMedia = false,
  1965. $addHeaders = true,
  1966. $linkAttributes = []
  1967. ) {
  1968. if ($addHeaders) {
  1969. $pagination_size = 'pagination-mini';
  1970. $html = '<div class="exercise_pagination pagination '.$pagination_size.'"><ul>';
  1971. } else {
  1972. $html = null;
  1973. }
  1974. $affectAllItems = false;
  1975. if ($isMedia && isset($fixedValue) && ($nextValue + 1 == $current)) {
  1976. $affectAllItems = true;
  1977. }
  1978. $localCounter = 0;
  1979. foreach ($list as $itemId) {
  1980. $isCurrent = false;
  1981. if ($affectAllItems) {
  1982. $isCurrent = true;
  1983. } else {
  1984. if (!$isMedia) {
  1985. $isCurrent = $current == ($localCounter + $nextValue + 1) ? true : false;
  1986. }
  1987. }
  1988. $html .= self::parsePaginationItem(
  1989. $itemId,
  1990. $isCurrent,
  1991. $conditions,
  1992. $link,
  1993. $nextValue,
  1994. $isMedia,
  1995. $localCounter,
  1996. $fixedValue,
  1997. $linkAttributes
  1998. );
  1999. $localCounter++;
  2000. }
  2001. if ($addHeaders) {
  2002. $html .= '</ul></div>';
  2003. }
  2004. return $html;
  2005. }
  2006. /**
  2007. *
  2008. * @param int $itemId
  2009. * @param bool $isCurrent
  2010. * @param array $conditions
  2011. * @param string $link
  2012. * @param int $nextValue
  2013. * @param bool $isMedia
  2014. * @param int $localCounter
  2015. * @param int $fixedValue
  2016. * @param array $linkAttributes
  2017. *
  2018. * @return string
  2019. */
  2020. public static function parsePaginationItem(
  2021. $itemId,
  2022. $isCurrent,
  2023. $conditions,
  2024. $link,
  2025. $nextValue = 0,
  2026. $isMedia = false,
  2027. $localCounter = null,
  2028. $fixedValue = null,
  2029. $linkAttributes = []
  2030. ) {
  2031. $defaultClass = 'before';
  2032. $class = $defaultClass;
  2033. foreach ($conditions as $condition) {
  2034. $array = isset($condition['items']) ? $condition['items'] : array();
  2035. $class_to_applied = $condition['class'];
  2036. $type = isset($condition['type']) ? $condition['type'] : 'positive';
  2037. $mode = isset($condition['mode']) ? $condition['mode'] : 'add';
  2038. switch ($type) {
  2039. case 'positive':
  2040. if (in_array($itemId, $array)) {
  2041. if ($mode == 'overwrite') {
  2042. $class = " $defaultClass $class_to_applied";
  2043. } else {
  2044. $class .= " $class_to_applied";
  2045. }
  2046. }
  2047. break;
  2048. case 'negative':
  2049. if (!in_array($itemId, $array)) {
  2050. if ($mode == 'overwrite') {
  2051. $class = " $defaultClass $class_to_applied";
  2052. } else {
  2053. $class .= " $class_to_applied";
  2054. }
  2055. }
  2056. break;
  2057. }
  2058. }
  2059. if ($isCurrent) {
  2060. $class = 'before current';
  2061. }
  2062. if ($isMedia && $isCurrent) {
  2063. $class = 'before current';
  2064. }
  2065. if (empty($link)) {
  2066. $link_to_show = '#';
  2067. } else {
  2068. $link_to_show = $link.($nextValue + $localCounter);
  2069. }
  2070. $label = $nextValue + $localCounter + 1;
  2071. if ($isMedia) {
  2072. $label = ($fixedValue + 1).' '.chr(97 + $localCounter);
  2073. $link_to_show = $link.$fixedValue.'#questionanchor'.$itemId;
  2074. }
  2075. $link = self::url($label.' ', $link_to_show, $linkAttributes);
  2076. return '<li class = "'.$class.'">'.$link.'</li>';
  2077. }
  2078. /**
  2079. * @param int $current
  2080. * @param int $total
  2081. * @return string
  2082. */
  2083. public static function paginationIndicator($current, $total)
  2084. {
  2085. $html = null;
  2086. if (!empty($current) && !empty($total)) {
  2087. $label = sprintf(get_lang('PaginationXofY'), $current, $total);
  2088. $html = self::url($label, '#', array('class' => 'btn disabled'));
  2089. }
  2090. return $html;
  2091. }
  2092. /**
  2093. * Adds a message in the queue
  2094. * @param string $message
  2095. */
  2096. public static function addFlash($message)
  2097. {
  2098. $messages = Session::read('flash_messages');
  2099. if (empty($messages)) {
  2100. $messages[] = $message;
  2101. } else {
  2102. array_push($messages, $message);
  2103. }
  2104. Session::write('flash_messages', $messages);
  2105. }
  2106. /**
  2107. * @return string
  2108. */
  2109. public static function getFlashToString()
  2110. {
  2111. $messages = Session::read('flash_messages');
  2112. $messageToString = '';
  2113. if (!empty($messages)) {
  2114. foreach ($messages as $message) {
  2115. $messageToString .= $message;
  2116. }
  2117. }
  2118. return $messageToString;
  2119. }
  2120. /**
  2121. * Shows the message from the session
  2122. */
  2123. public static function showFlash()
  2124. {
  2125. echo self::getFlashToString();
  2126. }
  2127. /**
  2128. * Destroys the message session
  2129. */
  2130. public static function cleanFlashMessages()
  2131. {
  2132. Session::erase('flash_messages');
  2133. }
  2134. /**
  2135. * Get the profile edition link for a user
  2136. * @param int $userId The user id
  2137. * @param boolean $asAdmin Optional. Whether get the URL for the platform admin
  2138. * @return string The link
  2139. */
  2140. public static function getProfileEditionLink($userId, $asAdmin = false)
  2141. {
  2142. $editProfileUrl = api_get_path(WEB_CODE_PATH).'auth/profile.php';
  2143. if ($asAdmin) {
  2144. $editProfileUrl = api_get_path(WEB_CODE_PATH)."admin/user_edit.php?user_id=".intval($userId);
  2145. }
  2146. if (api_get_setting('sso_authentication') === 'true') {
  2147. $subSSOClass = api_get_setting('sso_authentication_subclass');
  2148. $objSSO = null;
  2149. if (!empty($subSSOClass)) {
  2150. $file = api_get_path(SYS_CODE_PATH)."auth/sso/sso.$subSSOClass.class.php";
  2151. if (file_exists($file)) {
  2152. require_once $file;
  2153. $subSSOClass = 'sso'.$subSSOClass;
  2154. $objSSO = new $subSSOClass();
  2155. } else {
  2156. throw new Exception("$subSSOClass file not set");
  2157. }
  2158. } else {
  2159. $objSSO = new sso();
  2160. }
  2161. $editProfileUrl = $objSSO->generateProfileEditingURL(
  2162. $userId,
  2163. $asAdmin
  2164. );
  2165. }
  2166. return $editProfileUrl;
  2167. }
  2168. /**
  2169. * Get the vCard for a user
  2170. * @param int $userId The user id
  2171. * @return string *.*vcf file
  2172. */
  2173. public static function getVCardUserLink($userId)
  2174. {
  2175. $vCardUrl = api_get_path(WEB_PATH).'main/social/vcard_export.php?userId='.intval($userId);
  2176. return $vCardUrl;
  2177. }
  2178. /**
  2179. * @param string $content
  2180. * @param string $title
  2181. * @param string $footer
  2182. * @param string $type primary|success|info|warning|danger
  2183. * @param string $extra
  2184. * @param string $id
  2185. * @param string $customColor
  2186. *
  2187. * @return string
  2188. */
  2189. public static function panel(
  2190. $content,
  2191. $title = '',
  2192. $footer = '',
  2193. $type = 'default',
  2194. $extra = '',
  2195. $id = '',
  2196. $customColor = ''
  2197. ) {
  2198. $headerStyle = '';
  2199. if (!empty($customColor)) {
  2200. $headerStyle = 'style = "color: white; background-color: '.$customColor.'" ';
  2201. }
  2202. $title = !empty($title) ? '<div class="panel-heading" '.$headerStyle.' ><h3 class="panel-title">'.$title.'</h3>'.$extra.'</div>' : '';
  2203. $footer = !empty($footer) ? '<div class="panel-footer">'.$footer.'</div>' : '';
  2204. $typeList = ['primary', 'success', 'info', 'warning', 'danger'];
  2205. $style = !in_array($type, $typeList) ? 'default' : $type;
  2206. if (!empty($id)) {
  2207. $id = " id = $id ";
  2208. }
  2209. return '
  2210. <div '.$id.' class="panel panel-'.$style.'">
  2211. '.$title.'
  2212. '.self::contentPanel($content).'
  2213. '.$footer.'
  2214. </div>'
  2215. ;
  2216. }
  2217. /**
  2218. * @param string $content
  2219. * @return string
  2220. */
  2221. public static function contentPanel($content)
  2222. {
  2223. if (empty($content)) {
  2224. return '';
  2225. }
  2226. return '<div class="panel-body">'.$content.'</div>';
  2227. }
  2228. /**
  2229. * Get the button HTML with an Awesome Font icon
  2230. * @param string $text The button content
  2231. * @param string $url The url to button
  2232. * @param string $icon The Awesome Font class for icon
  2233. * @param string $type Optional. The button Bootstrap class. Default 'default' class
  2234. * @param array $attributes The additional attributes
  2235. * @param bool $includeText
  2236. *
  2237. * @return string The button HTML
  2238. */
  2239. public static function toolbarButton(
  2240. $text,
  2241. $url,
  2242. $icon = 'check',
  2243. $type = 'default',
  2244. array $attributes = [],
  2245. $includeText = true
  2246. ) {
  2247. $buttonClass = "btn btn-$type";
  2248. $icon = self::tag('i', null, ['class' => "fa fa-$icon fa-fw", 'aria-hidden' => 'true']);
  2249. $attributes['class'] = isset($attributes['class']) ? "$buttonClass {$attributes['class']}" : $buttonClass;
  2250. $attributes['title'] = isset($attributes['title']) ? $attributes['title'] : $text;
  2251. if (!$includeText) {
  2252. $text = '<span class="sr-only">'.$text.'</span>';
  2253. }
  2254. return self::url("$icon $text", $url, $attributes);
  2255. }
  2256. /**
  2257. * @param string $id
  2258. * @param array $content
  2259. * @param array $colsWidth Optional. Columns width
  2260. * @return string
  2261. */
  2262. public static function toolbarAction($id, $content, $colsWidth = [])
  2263. {
  2264. $col = count($content);
  2265. if (!$colsWidth) {
  2266. $width = 12 / $col;
  2267. array_walk($content, function() use ($width, &$colsWidth) {
  2268. $colsWidth[] = $width;
  2269. });
  2270. }
  2271. $html = '<div id="'.$id.'" class="actions">';
  2272. $html .= '<div class="row">';
  2273. for ($i = 0; $i < $col; $i++) {
  2274. $class = 'col-sm-'.$colsWidth[$i];
  2275. if ($col > 1) {
  2276. if ($i > 0 && $i < count($content) - 1) {
  2277. $class .= ' text-center';
  2278. } elseif ($i === count($content) - 1) {
  2279. $class .= ' text-right';
  2280. }
  2281. }
  2282. $html .= '<div class="'.$class.'">'.$content[$i].'</div>';
  2283. }
  2284. $html .= '</div>';
  2285. $html .= '</div>';
  2286. return $html;
  2287. }
  2288. /**
  2289. * Get a HTML code for a icon by Font Awesome
  2290. * @param string $name The icon name
  2291. * @param int|string $size Optional. The size for the icon. (Example: lg, 2, 3, 4, 5)
  2292. * @param boolean $fixWidth Optional. Whether add the fw class
  2293. * @param string $additionalClass Optional. Additional class
  2294. *
  2295. * @return string
  2296. */
  2297. public static function returnFontAwesomeIcon(
  2298. $name,
  2299. $size = '',
  2300. $fixWidth = false,
  2301. $additionalClass = ''
  2302. ) {
  2303. $className = "fa fa-$name";
  2304. if ($fixWidth) {
  2305. $className .= ' fa-fw';
  2306. }
  2307. switch ($size) {
  2308. case 'lg':
  2309. $className .= ' fa-lg';
  2310. break;
  2311. case 2:
  2312. //no break
  2313. case 3:
  2314. //no break
  2315. case 4:
  2316. //no break
  2317. case 5:
  2318. $className .= " fa-{$size}x";
  2319. break;
  2320. }
  2321. if (!empty($additionalClass)) {
  2322. $className .= " $additionalClass";
  2323. }
  2324. $icon = self::tag('em', null, ['class' => $className]);
  2325. return "$icon ";
  2326. }
  2327. /**
  2328. * @param string $title
  2329. * @param string $content
  2330. * @param null $id
  2331. * @param array $params
  2332. * @param null $idAccordion
  2333. * @param null $idCollapse
  2334. * @param bool|true $open
  2335. * @param bool|false $fullClickable
  2336. * @return null|string
  2337. * @todo rework function to easy use
  2338. */
  2339. public static function panelCollapse(
  2340. $title,
  2341. $content,
  2342. $id = null,
  2343. $params = array(),
  2344. $idAccordion = null,
  2345. $idCollapse = null,
  2346. $open = true,
  2347. $fullClickable = false
  2348. ) {
  2349. if (!empty($idAccordion)) {
  2350. $headerClass = '';
  2351. $headerClass .= $fullClickable ? 'center-block ' : '';
  2352. $headerClass .= $open ? '' : 'collapsed';
  2353. $contentClass = 'panel-collapse collapse ';
  2354. $contentClass .= $open ? 'in' : '';
  2355. $ariaExpanded = $open ? 'true' : 'false';
  2356. $html = <<<HTML
  2357. <div class="panel-group" id="$idAccordion" role="tablist" aria-multiselectable="true">
  2358. <div class="panel panel-default" id="$id">
  2359. <div class="panel-heading" role="tab">
  2360. <h4 class="panel-title">
  2361. <a class="$headerClass" role="button" data-toggle="collapse" data-parent="#$idAccordion" href="#$idCollapse" aria-expanded="$ariaExpanded" aria-controls="$idCollapse">$title</a>
  2362. </h4>
  2363. </div>
  2364. <div id="$idCollapse" class="$contentClass" role="tabpanel">
  2365. <div class="panel-body">$content</div>
  2366. </div>
  2367. </div>
  2368. </div>
  2369. HTML;
  2370. } else {
  2371. if (!empty($id)) {
  2372. $params['id'] = $id;
  2373. }
  2374. $params['class'] = 'panel panel-default';
  2375. $html = null;
  2376. if (!empty($title)) {
  2377. $html .= '<div class="panel-heading">'.$title.'</div>'.PHP_EOL;
  2378. }
  2379. $html .= '<div class="panel-body">'.$content.'</div>'.PHP_EOL;
  2380. $html = self::div($html, $params);
  2381. }
  2382. return $html;
  2383. }
  2384. /**
  2385. * Returns the string "1 day ago" with a link showing the exact date time.
  2386. * @param string $dateTime in UTC or a DateTime in UTC
  2387. *
  2388. * @return string
  2389. */
  2390. public static function dateToStringAgoAndLongDate($dateTime)
  2391. {
  2392. if (empty($dateTime) || $dateTime === '0000-00-00 00:00:00') {
  2393. return '';
  2394. }
  2395. if ($dateTime instanceof \DateTime) {
  2396. $dateTime = $dateTime->format('Y-m-d H:i:s');
  2397. }
  2398. return self::tip(
  2399. date_to_str_ago($dateTime),
  2400. api_convert_and_format_date($dateTime, DATE_TIME_FORMAT_LONG)
  2401. //api_get_local_time($dateTime)
  2402. );
  2403. }
  2404. /**
  2405. * @param array $userInfo
  2406. * @param string $status
  2407. * @param string $toolbar
  2408. *
  2409. * @return string
  2410. */
  2411. public static function getUserCard($userInfo, $status = '', $toolbar = '')
  2412. {
  2413. if (empty($userInfo)) {
  2414. return '';
  2415. }
  2416. if (!empty($status)) {
  2417. $status = '<div class="items-user-status">'.$status.'</div>';
  2418. }
  2419. if (!empty($toolbar)) {
  2420. $toolbar = '<div class="btn-group pull-right">'.$toolbar.'</div>';
  2421. }
  2422. return '<div id="user_card_'.$userInfo['id'].'" class="col-md-12">
  2423. <div class="row">
  2424. <div class="col-md-2">
  2425. <img src="'.$userInfo['avatar'].'" class="img-responsive img-circle">
  2426. </div>
  2427. <div class="col-md-10">
  2428. <p>'.$userInfo['complete_name'].'</p>
  2429. <div class="row">
  2430. <div class="col-md-2">
  2431. '.$status.'
  2432. </div>
  2433. <div class="col-md-10">
  2434. '.$toolbar.'
  2435. </div>
  2436. </div>
  2437. </div>
  2438. </div>
  2439. <hr />
  2440. </div>';
  2441. }
  2442. }