display.lib.php 87 KB

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