display.lib.php 98 KB

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