display.lib.php 86 KB

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