display.lib.php 89 KB

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