display.lib.php 101 KB

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