display.lib.php 85 KB

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