display.lib.php 86 KB

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