display.lib.php 78 KB

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