display.lib.php 75 KB

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