display.lib.php 99 KB

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