display.lib.php 78 KB

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