display.lib.php 80 KB

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