display.lib.php 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This is a display library for Chamilo.
  5. *
  6. * Include/require it in your code to use its public functionality.
  7. * There are also several display public functions in the main api library.
  8. *
  9. * All public functions static public functions inside a class called Display,
  10. * so you use them like this: e.g.
  11. * Display::display_normal_message($message)
  12. *
  13. * @package chamilo.library
  14. */
  15. /**
  16. * Code
  17. */
  18. /**
  19. * Display class
  20. * contains several public functions dealing with the display of
  21. * table data, messages, help topics, ...
  22. *
  23. * @package chamilo.library
  24. */
  25. class Display
  26. {
  27. /* The main template*/
  28. public static $global_template;
  29. public static $preview_style = null;
  30. public function __construct()
  31. {
  32. }
  33. /**
  34. * @return array
  35. */
  36. public static function toolList()
  37. {
  38. return array(
  39. 'group',
  40. 'work',
  41. 'glossary',
  42. 'forum',
  43. 'course_description',
  44. 'gradebook',
  45. 'attendance',
  46. 'course_progress',
  47. 'notebook'
  48. );
  49. }
  50. /**
  51. * Displays the page header
  52. * @param string The name of the page (will be showed in the page title)
  53. * @param string Optional help file name
  54. * @param string $page_header
  55. */
  56. public static function display_header($tool_name ='', $help = null, $page_header = null)
  57. {
  58. $origin = api_get_origin();
  59. $showHeader = true;
  60. if (isset($origin) && $origin == 'learnpath') {
  61. $showHeader = false;
  62. }
  63. self::$global_template = new Template($tool_name, $showHeader, $showHeader);
  64. // Fixing tools with any help it takes xxx part of main/xxx/index.php
  65. if (empty($help)) {
  66. $currentURL = api_get_self();
  67. preg_match('/main\/([^*\/]+)/', $currentURL, $matches);
  68. $toolList = self::toolList();
  69. if (!empty($matches)) {
  70. foreach ($matches as $match) {
  71. if (in_array($match, $toolList)) {
  72. $help = explode('_', $match);
  73. $help = array_map('ucfirst', $help);
  74. $help = implode('', $help);
  75. break;
  76. }
  77. }
  78. }
  79. }
  80. self::$global_template->set_help($help);
  81. if (!empty(self::$preview_style)) {
  82. self::$global_template->preview_theme = self::$preview_style;
  83. self::$global_template->set_css_files();
  84. self::$global_template->set_js_files();
  85. }
  86. if (!empty($page_header)) {
  87. self::$global_template->assign('header', $page_header);
  88. }
  89. self::$global_template->assign('course_code', api_get_course_id());
  90. echo self::$global_template->show_header_template();
  91. }
  92. /**
  93. * Displays the reduced page header (without banner)
  94. */
  95. public static function display_reduced_header()
  96. {
  97. global $show_learnpath, $tool_name;
  98. self::$global_template = new Template($tool_name, false, false, $show_learnpath);
  99. echo self::$global_template ->show_header_template();
  100. }
  101. public static function display_no_header()
  102. {
  103. global $tool_name, $show_learnpath;
  104. $disable_js_and_css_files = true;
  105. self::$global_template = new Template($tool_name, false, false, $show_learnpath);
  106. //echo self::$global_template->show_header_template();
  107. }
  108. /**
  109. * Displays the reduced page header (without banner)
  110. */
  111. public static function set_header() {
  112. global $show_learnpath;
  113. self::$global_template = new Template($tool_name, false, false, $show_learnpath);
  114. }
  115. /**
  116. * Display the page footer
  117. */
  118. public static function display_footer() {
  119. echo self::$global_template ->show_footer_template();
  120. }
  121. public static function page()
  122. {
  123. return new Page();
  124. }
  125. /**
  126. * Displays the tool introduction of a tool.
  127. *
  128. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  129. * @param string $tool These are the constants that are used for indicating the tools.
  130. * @param array $editor_config Optional configuration settings for the online editor.
  131. * return: $tool return a string array list with the "define" in main_api.lib
  132. * @return html code for adding an introduction
  133. */
  134. public static function display_introduction_section($tool, $editor_config = null) {
  135. echo self::return_introduction_section($tool, $editor_config);
  136. }
  137. public static function return_introduction_section($tool, $editor_config = null) {
  138. $is_allowed_to_edit = api_is_allowed_to_edit();
  139. $moduleId = $tool;
  140. if (api_get_setting('enable_tool_introduction') == 'true' || $tool == TOOL_COURSE_HOMEPAGE) {
  141. $introduction_section = null;
  142. require api_get_path(INCLUDE_PATH).'introductionSection.inc.php';
  143. return $introduction_section;
  144. }
  145. }
  146. /**
  147. * Displays a localised html file
  148. * tries to show the file "$full_file_name"."_".$language_interface.".html"
  149. * and if this does not exist, shows the file "$full_file_name".".html"
  150. * warning this public function defines a global
  151. * @param $full_file_name, the (path) name of the file, without .html
  152. * @return return a string with the path
  153. */
  154. public static function display_localised_html_file($full_file_name) {
  155. global $language_interface;
  156. $localised_file_name = $full_file_name.'_'.$language_interface.'.html';
  157. $default_file_name = $full_file_name.'.html';
  158. if (file_exists($localised_file_name)) {
  159. include $localised_file_name;
  160. } else {
  161. include ($default_file_name);
  162. }
  163. }
  164. /**
  165. * Displays a table
  166. * @param array $header Titles for the table header
  167. * each item in this array can contain 3 values
  168. * - 1st element: the column title
  169. * - 2nd element: true or false (column sortable?)
  170. * - 3th element: additional attributes for
  171. * th-tag (eg for column-width)
  172. * - 4the element: additional attributes for the td-tags
  173. * @param array $content 2D-array with the tables content
  174. * @param array $sorting_options Keys are:
  175. * 'column' = The column to use as sort-key
  176. * 'direction' = SORT_ASC or SORT_DESC
  177. * @param array $paging_options Keys are:
  178. * 'per_page_default' = items per page when switching from
  179. * full- list to per-page-view
  180. * 'per_page' = number of items to show per page
  181. * 'page_nr' = The page to display
  182. * @param array $query_vars Additional variables to add in the query-string
  183. * @param string The style that the table will show. You can set 'table' or 'grid'
  184. * @author bart.mollet@hogent.be
  185. */
  186. public static function display_sortable_table($header, $content, $sorting_options = array(), $paging_options = array(), $query_vars = null, $form_actions = array(), $style = 'table') {
  187. global $origin;
  188. $column = isset($sorting_options['column']) ? $sorting_options['column'] : 0;
  189. $default_items_per_page = isset($paging_options['per_page']) ? $paging_options['per_page'] : 20;
  190. $table = new SortableTableFromArray($content, $column, $default_items_per_page);
  191. if (is_array($query_vars)) {
  192. $table->set_additional_parameters($query_vars);
  193. }
  194. if ($style == 'table') {
  195. if (is_array($header) && count($header) > 0) {
  196. foreach ($header as $index => $header_item) {
  197. $table->set_header($index, $header_item[0], $header_item[1], $header_item[2], $header_item[3]);
  198. }
  199. }
  200. $table->set_form_actions($form_actions);
  201. $table->display();
  202. } else {
  203. $table->display_grid();
  204. }
  205. }
  206. /**
  207. * Shows a nice grid
  208. * @param string grid name (important to create css)
  209. * @param array header content
  210. * @param array array with the information to show
  211. * @param array $paging_options Keys are:
  212. * 'per_page_default' = items per page when switching from
  213. * full- list to per-page-view
  214. * 'per_page' = number of items to show per page
  215. * 'page_nr' = The page to display
  216. * 'hide_navigation' = true to hide the navigation
  217. * @param array $query_vars Additional variables to add in the query-string
  218. * @param array $form actions Additional variables to add in the query-string
  219. * @param mixed An array with bool values to know which columns show. i.e: $visibility_options= array(true, false) we will only show the first column
  220. * Can be also only a bool value. TRUE: show all columns, FALSE: show nothing
  221. */
  222. public static function display_sortable_grid($name, $header, $content, $paging_options = array(), $query_vars = null, $form_actions = array(), $visibility_options = true, $sort_data = true, $grid_class = array()) {
  223. echo self::return_sortable_grid($name, $header, $content, $paging_options, $query_vars, $form_actions, $visibility_options, $sort_data, $grid_class);
  224. }
  225. /**
  226. * Gets a nice grid in html string
  227. * @param string grid name (important to create css)
  228. * @param array header content
  229. * @param array array with the information to show
  230. * @param array $paging_options Keys are:
  231. * 'per_page_default' = items per page when switching from
  232. * full- list to per-page-view
  233. * 'per_page' = number of items to show per page
  234. * 'page_nr' = The page to display
  235. * 'hide_navigation' = true to hide the navigation
  236. * @param array $query_vars Additional variables to add in the query-string
  237. * @param array $form actions Additional variables to add in the query-string
  238. * @param mixed An array with bool values to know which columns show. i.e: $visibility_options= array(true, false) we will only show the first column
  239. * Can be also only a bool value. TRUE: show all columns, FALSE: show nothing
  240. * @param bool true for sorting data or false otherwise
  241. * @param array grid classes
  242. * @return string html grid
  243. */
  244. public static function return_sortable_grid(
  245. $name,
  246. $header,
  247. $content,
  248. $paging_options = array(),
  249. $query_vars = null,
  250. $form_actions = array(),
  251. $visibility_options = true,
  252. $sort_data = true,
  253. $grid_class = array(),
  254. $elementCount = 0
  255. ) {
  256. $column = 0;
  257. $default_items_per_page = isset($paging_options['per_page']) ? $paging_options['per_page'] : 20;
  258. $table = new SortableTableFromArray($content, $column, $default_items_per_page, $name);
  259. $table->total_number_of_items = intval($elementCount);
  260. if (is_array($query_vars)) {
  261. $table->set_additional_parameters($query_vars);
  262. }
  263. return $table->display_simple_grid($visibility_options, $paging_options['hide_navigation'], $default_items_per_page, $sort_data, $grid_class);
  264. }
  265. /**
  266. * Displays a table with a special configuration
  267. * @param array $header Titles for the table header
  268. * each item in this array can contain 3 values
  269. * - 1st element: the column title
  270. * - 2nd element: true or false (column sortable?)
  271. * - 3th element: additional attributes for
  272. * th-tag (eg for column-width)
  273. * - 4the element: additional attributes for the td-tags
  274. * @param array $content 2D-array with the tables content
  275. * @param array $sorting_options Keys are:
  276. * 'column' = The column to use as sort-key
  277. * 'direction' = SORT_ASC or SORT_DESC
  278. * @param array $paging_options Keys are:
  279. * 'per_page_default' = items per page when switching from
  280. * full- list to per-page-view
  281. * 'per_page' = number of items to show per page
  282. * 'page_nr' = The page to display
  283. * @param array $query_vars Additional variables to add in the query-string
  284. * @param array $column_show Array of binaries 1= show columns 0. hide a column
  285. * @param array $column_order An array of integers that let us decide how the columns are going to be sort.
  286. * i.e: $column_order=array('1''4','3','4'); The 2nd column will be order like the 4th column
  287. * @param array $form_actions Set optional forms actions
  288. *
  289. * @author Julio Montoya
  290. */
  291. public static function display_sortable_config_table($table_name, $header, $content, $sorting_options = array(), $paging_options = array(), $query_vars = null, $column_show = array(), $column_order = array(), $form_actions = array()) {
  292. global $origin;
  293. $column = isset($sorting_options['column']) ? $sorting_options['column'] : 0;
  294. $default_items_per_page = isset($paging_options['per_page']) ? $paging_options['per_page'] : 20;
  295. $table = new SortableTableFromArrayConfig($content, $column, $default_items_per_page, $table_name, $column_show, $column_order);
  296. if (is_array($query_vars)) {
  297. $table->set_additional_parameters($query_vars);
  298. }
  299. // Show or hide the columns header
  300. if (is_array($column_show)) {
  301. for ($i = 0; $i < count($column_show); $i++) {
  302. if (!empty($column_show[$i])) {
  303. $val0 = isset($header[$i][0]) ? $header[$i][0] : null;
  304. $val1 = isset($header[$i][1]) ? $header[$i][1] : null;
  305. $val2 = isset($header[$i][2]) ? $header[$i][2] : null;
  306. $val3 = isset($header[$i][3]) ? $header[$i][3] : null;
  307. $table->set_header($i, $val0, $val1, $val2, $val3);
  308. }
  309. }
  310. }
  311. $table->set_form_actions($form_actions);
  312. $table->display();
  313. }
  314. /**
  315. * Displays a normal message. It is recommended to use this public function
  316. * to display any normal information messages.
  317. *
  318. * @param bool Filter (true) or not (false)
  319. * @return void
  320. */
  321. public static function display_normal_message($message, $filter = true, $returnValue = false) {
  322. $message = self::return_message($message, 'normal', $filter);
  323. if ($returnValue) {
  324. return $message;
  325. } else {
  326. echo $message;
  327. }
  328. }
  329. /**
  330. * Displays an warning message. Use this if you want to draw attention to something
  331. * This can also be used for instance with the hint in the exercises
  332. *
  333. */
  334. public static function display_warning_message($message, $filter = true, $returnValue = false)
  335. {
  336. $message = self::return_message($message, 'warning', $filter);
  337. if ($returnValue) {
  338. return $message;
  339. } else {
  340. echo $message;
  341. }
  342. }
  343. /**
  344. * Displays an confirmation message. Use this if something has been done successfully
  345. * @param bool Filter (true) or not (false)
  346. * @return void
  347. */
  348. public static function display_confirmation_message ($message, $filter = true, $returnValue = false)
  349. {
  350. $message = self::return_message($message, 'confirm', $filter);
  351. if ($returnValue) {
  352. return $message;
  353. } else {
  354. echo $message;
  355. }
  356. }
  357. /**
  358. * Displays an error message. It is recommended to use this public function if an error occurs
  359. * @param string $message - include any additional html
  360. * tags if you need them
  361. * @param bool Filter (true) or not (false)
  362. * @return void
  363. */
  364. public static function display_error_message ($message, $filter = true, $returnValue = false)
  365. {
  366. $message = self::return_message($message, 'error', $filter);
  367. if ($returnValue) {
  368. return $message;
  369. } else {
  370. echo $message;
  371. }
  372. }
  373. /**
  374. * @param string $message
  375. * @param string $type
  376. * @param bool $filter
  377. */
  378. public static function return_message_and_translate($message, $type='normal', $filter = true)
  379. {
  380. $message = get_lang($message);
  381. echo self::return_message($message, $type, $filter);
  382. }
  383. /**
  384. * Returns a div html string with
  385. * @param string The message
  386. * @param string The message type (confirm,normal,warning,error)
  387. * @param bool Whether to XSS-filter or not
  388. * @return string Message wrapped into an HTML div
  389. */
  390. public static function return_message($message, $type='normal', $filter = true)
  391. {
  392. if ($filter) {
  393. $message = api_htmlentities($message, ENT_QUOTES, api_is_xml_http_request() ? 'UTF-8' : api_get_system_encoding());
  394. //$message = Security::remove_XSS($message);
  395. }
  396. $class = "";
  397. switch($type) {
  398. case 'warning':
  399. $class .= 'warning-message';
  400. break;
  401. case 'error':
  402. $class .= 'error-message';
  403. break;
  404. case 'confirmation':
  405. case 'confirm':
  406. case 'success':
  407. $class .= 'confirmation-message';
  408. break;
  409. case 'normal':
  410. default:
  411. $class .= 'normal-message';
  412. }
  413. return self::div($message, array('class'=> $class));
  414. }
  415. /**
  416. * Returns an encrypted mailto hyperlink
  417. *
  418. * @param string e-mail
  419. * @param string clickable text
  420. * @param string optional, class from stylesheet
  421. * @return string encrypted mailto hyperlink
  422. */
  423. public static function encrypted_mailto_link ($email, $clickable_text = null, $style_class = '')
  424. {
  425. if (is_null($clickable_text)) {
  426. $clickable_text = $email;
  427. }
  428. // "mailto:" already present?
  429. if (substr($email, 0, 7) != 'mailto:') {
  430. $email = 'mailto:'.$email;
  431. }
  432. // Class (stylesheet) defined?
  433. if ($style_class != '') {
  434. $style_class = ' class="'.$style_class.'"';
  435. }
  436. // Encrypt email
  437. $hmail = '';
  438. for ($i = 0; $i < strlen($email); $i ++) {
  439. $hmail .= '&#'.ord($email {
  440. $i }).';';
  441. }
  442. $hclickable_text = null;
  443. // Encrypt clickable text if @ is present
  444. if (strpos($clickable_text, '@')) {
  445. for ($i = 0; $i < strlen($clickable_text); $i ++) {
  446. $hclickable_text .= '&#'.ord($clickable_text {
  447. $i }).';';
  448. }
  449. } else {
  450. $hclickable_text = @htmlspecialchars($clickable_text, ENT_QUOTES, api_get_system_encoding());
  451. }
  452. // Return encrypted mailto hyperlink
  453. return '<a href="'.$hmail.'"'.$style_class.' class="clickable_email_link">'.$hclickable_text.'</a>';
  454. }
  455. /**
  456. * Returns an mailto icon hyperlink
  457. *
  458. * @param string e-mail
  459. * @param string icon source file from the icon lib
  460. * @param integer icon size from icon lib
  461. * @param string optional, class from stylesheet
  462. * @return string encrypted mailto hyperlink
  463. */
  464. public static function icon_mailto_link ($email, $icon_file = "mail.png", $icon_size = 22, $style_class = '') {
  465. // "mailto:" already present?
  466. if (substr($email, 0, 7) != 'mailto:') {
  467. $email = 'mailto:'.$email;
  468. }
  469. // Class (stylesheet) defined?
  470. if ($style_class != '') {
  471. $style_class = ' class="'.$style_class.'"';
  472. }
  473. // Encrypt email
  474. $hmail = '';
  475. for ($i = 0; $i < strlen($email); $i ++) {
  476. $hmail .= '&#'.ord($email {
  477. $i }).';';
  478. }
  479. // icon html code
  480. $icon_html_source = self::return_icon($icon_file, $hmail, '', $icon_size);
  481. // Return encrypted mailto hyperlink
  482. return '<a href="'.$hmail.'"'.$style_class.' class="clickable_email_link">'.$icon_html_source.'</a>';
  483. }
  484. /**
  485. * Creates a hyperlink to the platform homepage.
  486. * @param string $name, the visible name of the hyperlink, default is sitename
  487. * @return string with html code for hyperlink
  488. */
  489. public static function get_platform_home_link_html($name = '') {
  490. if ($name == '') {
  491. $name = api_get_setting('siteName');
  492. }
  493. return '<a href="'.api_get_path(WEB_PATH).'index.php">'.$name.'</a>';
  494. }
  495. /**
  496. * Prints an <option>-list with all letters (A-Z).
  497. * @param char $selected_letter The letter that should be selected
  498. * @todo This is English language specific implementation. It should be adapted for the other languages.
  499. */
  500. public static function get_alphabet_options($selected_letter = '') {
  501. $result = '';
  502. for ($i = 65; $i <= 90; $i ++) {
  503. $letter = chr($i);
  504. $result .= '<option value="'.$letter.'"';
  505. if ($selected_letter == $letter) {
  506. $result .= ' selected="selected"';
  507. }
  508. $result .= '>'.$letter.'</option>';
  509. }
  510. return $result;
  511. }
  512. /**
  513. * Get the options withing a select box within the given values
  514. * @param int Min value
  515. * @param int Max value
  516. * @param int Default value
  517. * @return string HTML select options
  518. */
  519. public static function get_numeric_options($min, $max, $selected_num = 0) {
  520. $result = '';
  521. for ($i = $min; $i <= $max; $i ++) {
  522. $result .= '<option value="'.$i.'"';
  523. if (is_int($selected_num))
  524. if ($selected_num == $i) {
  525. $result .= ' selected="selected"';
  526. }
  527. $result .= '>'.$i.'</option>';
  528. }
  529. return $result;
  530. }
  531. /**
  532. * Shows the so-called "left" menu for navigating
  533. */
  534. public static function show_course_navigation_menu($isHidden = false) {
  535. global $output_string_menu;
  536. global $_setting;
  537. // Check if the $_SERVER['REQUEST_URI'] contains already url parameters (thus a questionmark)
  538. if (strpos($_SERVER['REQUEST_URI'], '?') === false) {
  539. $sourceurl = api_get_self().'?';
  540. } else {
  541. $sourceurl = $_SERVER['REQUEST_URI'];
  542. }
  543. $output_string_menu = '';
  544. if ($isHidden == 'true' and $_SESSION['hideMenu']) {
  545. $_SESSION['hideMenu'] = 'hidden';
  546. $sourceurl = str_replace('&isHidden=true', '', $sourceurl);
  547. $sourceurl = str_replace('&isHidden=false', '', $sourceurl);
  548. $output_string_menu .= ' <a href="'.$sourceurl.'&isHidden=false"><img src="../../main/img/expand.gif" alt="'.'Show menu1'.'" padding:"2px"/></a>';
  549. } elseif ($isHidden == 'false' && $_SESSION['hideMenu']) {
  550. $sourceurl = str_replace('&isHidden=true', '', $sourceurl);
  551. $sourceurl = str_replace('&isHidden=false', '', $sourceurl);
  552. $_SESSION['hideMenu'] = 'shown';
  553. $output_string_menu .= '<div id="leftimg"><a href="'.$sourceurl.'&isHidden=true"><img src="../../main/img/collapse.gif" alt="'.'Hide menu2'.'" padding:"2px"/></a></div>';
  554. } elseif ($_SESSION['hideMenu']) {
  555. if ($_SESSION['hideMenu'] == 'shown') {
  556. $output_string_menu .= '<div id="leftimg"><a href="'.$sourceurl.'&isHidden=true"><img src="../../main/img/collapse.gif" alt="'.'Hide menu3'.' padding:"2px"/></a></div>';
  557. }
  558. if ($_SESSION['hideMenu'] == 'hidden') {
  559. $sourceurl = str_replace('&isHidden=true', '', $sourceurl);
  560. $output_string_menu .= '<a href="'.$sourceurl.'&isHidden=false"><img src="../../main/img/expand.gif" alt="'.'Hide menu4'.' padding:"2px"/></a>';
  561. }
  562. } elseif (!$_SESSION['hideMenu']) {
  563. $_SESSION['hideMenu'] = 'shown';
  564. if (isset($_cid)) {
  565. $output_string_menu .= '<div id="leftimg"><a href="'.$sourceurl.'&isHidden=true"><img src="main/img/collapse.gif" alt="'.'Hide menu5'.' padding:"2px"/></a></div>';
  566. }
  567. }
  568. }
  569. /**
  570. * This public function displays an icon
  571. * @param string The filename of the file (in the main/img/ folder
  572. * @param string The alt text (probably a language variable)
  573. * @param array additional attributes (for instance height, width, onclick, ...)
  574. * @param integer The wanted width of the icon (to be looked for in the corresponding img/icons/ folder)
  575. * @return void
  576. */
  577. public static function display_icon($image, $alt_text = '', $additional_attributes = array(), $size=null) {
  578. echo self::return_icon($image, $alt_text, $additional_attributes, $size);
  579. }
  580. /**
  581. * This public function returns the htmlcode for an icon
  582. *
  583. * @param string The filename of the file (in the main/img/ folder
  584. * @param string The alt text (probably a language variable)
  585. * @param array Additional attributes (for instance height, width, onclick, ...)
  586. * @param integer The wanted width of the icon (to be looked for in the corresponding img/icons/ folder)
  587. * @return string An HTML string of the right <img> tag
  588. *
  589. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University 2006
  590. * @author Julio Montoya 2010 Function improved, adding image constants
  591. * @author Yannick Warnier 2011 Added size handler
  592. * @version Feb 2011
  593. */
  594. public static function return_icon($image, $alt_text = '', $additional_attributes = array(), $size = ICON_SIZE_SMALL, $show_text = true, $return_only_path = false) {
  595. $code_path = api_get_path(SYS_CODE_PATH);
  596. $w_code_path = api_get_path(WEB_CODE_PATH);
  597. $image = trim($image);
  598. $theme = 'css/'.api_get_visual_theme().'/icons/';
  599. $icon = '';
  600. $size_extra = '';
  601. if (isset($size)) {
  602. $size = intval($size);
  603. $size_extra = $size.'/';
  604. } else {
  605. $size = ICON_SIZE_SMALL;
  606. }
  607. //Checking the theme icons folder example: main/css/chamilo/icons/XXX
  608. if (is_file($code_path.$theme.$size_extra.$image)) {
  609. $icon = $w_code_path.$theme.$size_extra.$image;
  610. } elseif (is_file($code_path.'img/icons/'.$size_extra.$image)) {
  611. //Checking the main/img/icons/XXX/ folder
  612. $icon = $w_code_path.'img/icons/'.$size_extra.$image;
  613. } else {
  614. //Checking the img/ folder
  615. $icon = $w_code_path.'img/'.$image;
  616. }
  617. $icon = api_get_cdn_path($icon);
  618. if ($return_only_path) {
  619. return $icon;
  620. }
  621. $img = self::img($icon, $alt_text, $additional_attributes);
  622. if (SHOW_TEXT_NEAR_ICONS == true and !empty($alt_text)) {
  623. if ($show_text) {
  624. $img = "$img $alt_text";
  625. }
  626. }
  627. return $img;
  628. }
  629. /**
  630. * Returns the htmlcode for an image
  631. *
  632. * @param string $image the filename of the file (in the main/img/ folder
  633. * @param string $alt_text the alt text (probably a language variable)
  634. * @param array additional attributes (for instance height, width, onclick, ...)
  635. * @author Julio Montoya 2010
  636. */
  637. public static function img($image_path, $alt_text = '', $additional_attributes = array()) {
  638. // Sanitizing the parameter $image_path
  639. $image_path = Security::filter_img_path($image_path);
  640. // alt text = the image name if there is none provided (for XHTML compliance)
  641. if ($alt_text == '') {
  642. $alt_text = basename($image_path);
  643. }
  644. $additional_attributes['src'] = $image_path;
  645. if (empty($additional_attributes['alt'])) {
  646. $additional_attributes['alt'] = $alt_text;
  647. }
  648. if (empty($additional_attributes['title'])) {
  649. $additional_attributes['title'] = $alt_text;
  650. }
  651. //return '<img src="'.$image_path.'" alt="'.$alt_text.'" title="'.$alt_text.'" '.$attribute_list.' />';
  652. return self::tag('img', '', $additional_attributes);
  653. }
  654. /**
  655. * Returns the htmlcode for a tag (h3, h1, div, a, button), etc
  656. *
  657. * @param string $image the filename of the file (in the main/img/ folder
  658. * @param string $alt_text the alt text (probably a language variable)
  659. * @param array additional attributes (for instance height, width, onclick, ...)
  660. * @author Julio Montoya 2010
  661. */
  662. public static function tag($tag, $content, $additional_attributes = array()) {
  663. $attribute_list = '';
  664. // Managing the additional attributes
  665. if (!empty($additional_attributes) && is_array($additional_attributes)) {
  666. $attribute_list = '';
  667. foreach ($additional_attributes as $key => & $value) {
  668. $attribute_list .= $key.'="'.$value.'" ';
  669. }
  670. }
  671. //some tags don't have this </XXX>
  672. if (in_array($tag, array('img','input','br'))) {
  673. $return_value = '<'.$tag.' '.$attribute_list.' />';
  674. } else {
  675. $return_value = '<'.$tag.' '.$attribute_list.' > '.$content.'</'.$tag.'>';
  676. }
  677. return $return_value;
  678. }
  679. /**
  680. * Creates a URL anchor
  681. */
  682. public static function url($name, $url, $extra_attributes = array()) {
  683. if (!empty($url)) {
  684. $extra_attributes['href']= $url;
  685. }
  686. return self::tag('a', $name, $extra_attributes);
  687. }
  688. /**
  689. * Creates a div tag
  690. */
  691. public static function div($content, $extra_attributes = array()) {
  692. return self::tag('div', $content, $extra_attributes);
  693. }
  694. /**
  695. * Creates a span tag
  696. */
  697. public static function span($content, $extra_attributes = array()) {
  698. return self::tag('span', $content, $extra_attributes);
  699. }
  700. /**
  701. * Displays an HTML input tag
  702. *
  703. */
  704. public static function input($type, $name, $value, $extra_attributes = array()) {
  705. if (isset($type)) {
  706. $extra_attributes['type']= $type;
  707. }
  708. if (isset($name)) {
  709. $extra_attributes['name']= $name;
  710. }
  711. if (isset($value)) {
  712. $extra_attributes['value']= $value;
  713. }
  714. return self::tag('input', '', $extra_attributes);
  715. }
  716. public static function button($name, $value, $extra_attributes = array()) {
  717. if (!empty($name)) {
  718. $extra_attributes['name']= $name;
  719. }
  720. return self::tag('button', $value, $extra_attributes);
  721. }
  722. /**
  723. * Displays an HTML select tag
  724. *
  725. */
  726. public static function select($name, $values, $default = -1, $extra_attributes = array(), $show_blank_item = true, $blank_item_text = null) {
  727. $html = '';
  728. $extra = '';
  729. $default_id = 'id="'.$name.'" ';
  730. foreach($extra_attributes as $key=>$parameter) {
  731. if ($key == 'id') {
  732. $default_id = '';
  733. }
  734. $extra .= $key.'="'.$parameter.'"';
  735. }
  736. $html .= '<select name="'.$name.'" '.$default_id.' '.$extra.'>';
  737. if ($show_blank_item) {
  738. if (empty($blank_item_text)) {
  739. $blank_item_text = get_lang('Select');
  740. } else {
  741. $blank_item_text = Security::remove_XSS($blank_item_text);
  742. }
  743. $html .= self::tag('option', '-- '.$blank_item_text.' --', array('value'=>'-1'));
  744. }
  745. if ($values) {
  746. foreach ($values as $key => $value) {
  747. if(is_array($value) && isset($value['name'])) {
  748. $value = $value['name'];
  749. }
  750. $html .= '<option value="'.$key.'"';
  751. if (is_array($default)) {
  752. foreach($default as $item) {
  753. if ($item == $key) {
  754. $html .= 'selected="selected"';
  755. break;
  756. }
  757. }
  758. } else {
  759. if ($default == $key) {
  760. $html .= 'selected="selected"';
  761. }
  762. }
  763. $html .= '>'.$value.'</option>';
  764. }
  765. }
  766. $html .= '</select>';
  767. return $html;
  768. }
  769. /**
  770. * Creates a tab menu
  771. * Requirements: declare the jquery, jquery-ui libraries + the jquery-ui.css in the $htmlHeadXtra variable before the display_header
  772. * Add this script
  773. * @example
  774. * <script>
  775. $(function() {
  776. $( "#tabs" ).tabs();
  777. });
  778. </script>
  779. * @param array list of the tab titles
  780. * @param array content that will be showed
  781. * @param string the id of the container of the tab in the example "tabs"
  782. * @param array attributes for the ul
  783. *
  784. */
  785. public static function tabs($header_list, $content_list, $id = 'tabs', $attributes = array(), $ul_attributes = array()) {
  786. if (empty($header_list) || count($header_list) == 0 ) {
  787. return '';
  788. }
  789. $lis = '';
  790. $i = 1;
  791. foreach ($header_list as $item) {
  792. $item =self::tag('a', $item, array('href'=>'#'.$id.'-'.$i));
  793. $lis .=self::tag('li', $item, $ul_attributes);
  794. $i++;
  795. }
  796. $ul = self::tag('ul',$lis);
  797. $i = 1;
  798. $divs = '';
  799. foreach ($content_list as $content) {
  800. $content = self::tag('p',$content);
  801. $divs .=self::tag('div', $content, array('id'=>$id.'-'.$i));
  802. $i++;
  803. }
  804. $attributes['id'] = $id;
  805. $main_div = self::tag('div',$ul.$divs, $attributes);
  806. return $main_div ;
  807. }
  808. public static function tabs_only_link($header_list, $selected = null) {
  809. $id = uniqid();
  810. $i = 1;
  811. $lis = null;
  812. foreach ($header_list as $item) {
  813. $class = null;
  814. if ($i == $selected) {
  815. $class = 'active';
  816. }
  817. $item =self::tag('a', $item['content'], array('id'=>$id.'-'.$i, 'href' => $item['url']));
  818. $lis .=self::tag('li', $item, array('class' => $class));
  819. $i++;
  820. }
  821. return self::tag('ul',$lis, array('class' => 'nav nav-tabs'));
  822. }
  823. /**
  824. * In order to display a grid using jqgrid you have to:
  825. * @example
  826. * After your Display::display_header function you have to add the nex javascript code: *
  827. * <script>
  828. * echo Display::grid_js('my_grid_name', $url,$columns, $column_model, $extra_params,array()); // for more information of this function check the grid_js() function
  829. * </script>
  830. * //Then you have to call the grid_html
  831. * echo Display::grid_html('my_grid_name');
  832. * As you can see both function use the same "my_grid_name" this is very important otherwise nothing will work
  833. *
  834. * @param string the div id, this value must be the same with the first parameter of Display::grid_js()
  835. * @return string html
  836. *
  837. */
  838. public static function grid_html($div_id)
  839. {
  840. $table = self::tag('table','', array('id' => $div_id));
  841. $table .= self::tag('div','', array('id' => $div_id.'_pager'));
  842. return $table;
  843. }
  844. /**
  845. * @param string $label
  846. * @param string $form_item
  847. * @return string
  848. */
  849. public static function form_row($label, $form_item)
  850. {
  851. $label = self::span($label, array('class' =>'control-label'));
  852. $form_item = self::div($form_item, array('class' =>'controls'));
  853. return self::div($label.$form_item, array('class'=>'control-group'));
  854. }
  855. /**
  856. * This is a wrapper to use the jqgrid in Chamilo. For the other jqgrid options visit http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options
  857. * This function need to be in the ready jquery function example --> $(function() { <?php echo Display::grid_js('grid' ...); ?> }
  858. * In order to work this function needs the Display::grid_html function with the same div id
  859. *
  860. * @param string div id
  861. * @param string url where the jqgrid will ask for data (if datatype = json)
  862. * @param array Visible columns (you should use get_lang). An array in which we place the names of the columns.
  863. * This is the text that appears in the head of the grid (Header layer).
  864. * Example: colname {name:'date', index:'date', width:120, align:'right'},
  865. * @param array the column model : Array which describes the parameters of the columns.This is the most important part of the grid.
  866. * For a full description of all valid values see colModel API. See the url above.
  867. * @param array extra parameters
  868. * @param array data that will be loaded
  869. * @return string the js code
  870. *
  871. */
  872. public static function grid_js(
  873. $div_id,
  874. $url,
  875. $column_names,
  876. $column_model,
  877. $extra_params,
  878. $data = array(),
  879. $formatter = '',
  880. $width_fix = false
  881. ) {
  882. $obj = new stdClass();
  883. $obj->first = 'first';
  884. if (!empty($url)) {
  885. $obj->url = $url;
  886. }
  887. //This line should only be used/modified in case of having characters
  888. // encoding problems - see #6159
  889. //$column_names = array_map("utf8_encode", $column_names);
  890. $obj->colNames = $column_names;
  891. $obj->colModel = $column_model;
  892. $obj->pager = '#'.$div_id.'_pager';
  893. $obj->datatype = 'json';
  894. $obj->viewrecords = 'true';
  895. $all_value = 10000000;
  896. // Default row quantity
  897. if (!isset($extra_params['rowList'])) {
  898. $extra_params['rowList'] = array(20, 50, 100, 500, 1000, $all_value);
  899. }
  900. $json = '';
  901. if (!empty($extra_params['datatype'])) {
  902. $obj->datatype = $extra_params['datatype'];
  903. }
  904. // Row even odd style.
  905. $obj->altRows = true;
  906. if (!empty($extra_params['altRows'])) {
  907. $obj->altRows = $extra_params['altRows'];
  908. }
  909. if (!empty($extra_params['sortname'])) {
  910. $obj->sortname = $extra_params['sortname'];
  911. }
  912. if (!empty($extra_params['sortorder'])) {
  913. $obj->sortorder = $extra_params['sortorder'];
  914. }
  915. if (!empty($extra_params['rowList'])) {
  916. $obj->rowList = $extra_params['rowList'];
  917. }
  918. //Sets how many records we want to view in the grid
  919. $obj->rowNum = 20;
  920. if (!empty($extra_params['rowNum'])) {
  921. $obj->rowNum = $extra_params['rowNum'];
  922. }
  923. if (!empty($extra_params['viewrecords'])) {
  924. $obj->viewrecords = $extra_params['viewrecords'];
  925. }
  926. $beforeSelectRow = null;
  927. if (isset($extra_params['beforeSelectRow'])) {
  928. $beforeSelectRow = "beforeSelectRow: ".$extra_params['beforeSelectRow'].", ";
  929. unset($extra_params['beforeSelectRow']);
  930. }
  931. // Adding extra params
  932. if (!empty($extra_params)) {
  933. foreach ($extra_params as $key => $element) {
  934. $obj->$key = $element;
  935. }
  936. }
  937. // Adding static data.
  938. if (!empty($data)) {
  939. $data_var = $div_id.'_data';
  940. $json.=' var '.$data_var.' = '.json_encode($data).';';
  941. $obj->data = $data_var;
  942. $obj->datatype = 'local';
  943. $json.="\n";
  944. }
  945. $obj->end = 'end';
  946. $json_encode = json_encode($obj);
  947. if (!empty($data)) {
  948. //Converts the "data":"js_variable" to "data":js_variable othersiwe it will not work
  949. $json_encode = str_replace('"data":"'.$data_var.'"', '"data":'.$data_var.'', $json_encode);
  950. }
  951. // Fixing true/false js values that doesn't need the ""
  952. $json_encode = str_replace(':"true"',':true',$json_encode);
  953. // wrap_cell is not a valid jqgrid attributes is a hack to wrap a text
  954. $json_encode = str_replace('"wrap_cell":true', 'cellattr : function(rowId, value, rowObject, colModel, arrData) { return \'class = "jqgrid_whitespace"\'; }', $json_encode);
  955. $json_encode = str_replace(':"false"',':false',$json_encode);
  956. $json_encode = str_replace('"formatter":"action_formatter"', 'formatter:action_formatter', $json_encode);
  957. $json_encode = str_replace(array('{"first":"first",','"end":"end"}'), '', $json_encode);
  958. // Creating the jqgrid element.
  959. $json .= '$("#'.$div_id.'").jqGrid({';
  960. //$json .= $beforeSelectRow;
  961. $json .= $json_encode;
  962. $json .= '});';
  963. $all_text = addslashes(get_lang('All'));
  964. $json .= '$("'.$obj->pager.' option[value='.$all_value.']").text("'.$all_text.'");';
  965. $json.= "\n";
  966. // Adding edit/delete icons.
  967. $json.= $formatter;
  968. return $json;
  969. }
  970. /**
  971. * @param array $headers
  972. * @param array $rows
  973. * @param array $attributes
  974. * @return string
  975. */
  976. public static function table($headers, $rows, $attributes = array()) {
  977. if (empty($attributes)) {
  978. $attributes['class'] = 'data_table';
  979. }
  980. $table = new HTML_Table($attributes);
  981. $row = 0;
  982. $column = 0;
  983. //Course headers
  984. if (!empty($headers)) {
  985. foreach ($headers as $item) {
  986. $table->setHeaderContents($row, $column, $item);
  987. $column++;
  988. }
  989. $row = 1;
  990. $column = 0;
  991. }
  992. if (!empty($rows)) {
  993. foreach($rows as $content) {
  994. $table->setCellContents($row, $column, $content);
  995. $row++;
  996. //$column++;
  997. }
  998. }
  999. return $table->toHtml();
  1000. }
  1001. /**
  1002. * Returns the "what's new" icon notifications
  1003. *
  1004. * The general logic of this function is to track the last time the user
  1005. * entered the course and compare to what has changed inside this course
  1006. * since then, based on the item_property table inside this course. Note that,
  1007. * if the user never entered the course before, he will not see notification
  1008. * icons. This function takes session ID into account (if any) and only shows
  1009. * the corresponding notifications.
  1010. * @param array Course information array, containing at least elements 'db' and 'k'
  1011. * @return string The HTML link to be shown next to the course
  1012. */
  1013. public static function show_notification($course_info)
  1014. {
  1015. $t_track_e_access = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_LASTACCESS);
  1016. $course_tool_table = Database::get_course_table(TABLE_TOOL_LIST);
  1017. $tool_edit_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1018. $course_code = Database::escape_string($course_info['code']);
  1019. $user_id = api_get_user_id();
  1020. $course_id = $course_info['real_id'];
  1021. $course_info['id_session'] = intval($course_info['id_session']);
  1022. // Get the user's last access dates to all tools of this course
  1023. $sql = "SELECT *
  1024. FROM $t_track_e_access USE INDEX (access_cours_code, access_user_id)
  1025. WHERE
  1026. access_cours_code = '".$course_code."' AND
  1027. access_user_id = '$user_id' AND
  1028. access_session_id ='".$course_info['id_session']."'";
  1029. $resLastTrackInCourse = Database::query($sql);
  1030. $oldestTrackDate = $oldestTrackDateOrig = '3000-01-01 00:00:00';
  1031. while ($lastTrackInCourse = Database::fetch_array($resLastTrackInCourse)) {
  1032. $lastTrackInCourseDate[$lastTrackInCourse['access_tool']] = $lastTrackInCourse['access_date'];
  1033. if ($oldestTrackDate > $lastTrackInCourse['access_date']) {
  1034. $oldestTrackDate = $lastTrackInCourse['access_date'];
  1035. }
  1036. }
  1037. if ($oldestTrackDate == $oldestTrackDateOrig) {
  1038. //if there was no connexion to the course ever, then take the
  1039. // course creation date as a reference
  1040. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  1041. $sql = "SELECT course.creation_date ".
  1042. "FROM $course_table course ".
  1043. "WHERE course.code = '".$course_code."'";
  1044. $res = Database::query($sql);
  1045. if ($res && Database::num_rows($res)>0) {
  1046. $row = Database::fetch_array($res);
  1047. }
  1048. $oldestTrackDate = $row['creation_date'];
  1049. }
  1050. // Get the last edits of all tools of this course.
  1051. $sql = "SELECT
  1052. tet.*,
  1053. tet.lastedit_date last_date,
  1054. tet.tool tool,
  1055. tet.ref ref,
  1056. tet.lastedit_type type,
  1057. tet.to_group_id group_id,
  1058. ctt.image image,
  1059. ctt.link link
  1060. FROM $tool_edit_table tet, $course_tool_table ctt
  1061. WHERE
  1062. tet.c_id = $course_id AND
  1063. ctt.c_id = $course_id AND
  1064. tet.lastedit_date > '$oldestTrackDate' ".
  1065. // Special hack for work tool, which is called student_publication in c_tool and work in c_item_property :-/ BT#7104
  1066. " AND (ctt.name = tet.tool OR (ctt.name = 'student_publication' AND tet.tool = 'work')) ".
  1067. " AND ctt.visibility = '1' ".
  1068. " AND tet.lastedit_user_id != $user_id AND tet.id_session = '".$course_info['id_session']."'
  1069. ORDER BY tet.lastedit_date";
  1070. $res = Database::query($sql);
  1071. // Get the group_id's with user membership.
  1072. $group_ids = GroupManager :: get_group_ids($course_info['real_id'], $user_id);
  1073. $group_ids[] = 0; //add group 'everyone'
  1074. $notifications = array();
  1075. // Filter all last edits of all tools of the course
  1076. while ($res && ($item_property = Database::fetch_array($res))) {
  1077. // First thing to check is if the user never entered the tool
  1078. // or if his last visit was earlier than the last modification.
  1079. if ((!isset($lastTrackInCourseDate[$item_property['tool']])
  1080. || $lastTrackInCourseDate[$item_property['tool']] < $item_property['lastedit_date'])
  1081. // Drop the tool elements that are part of a group that the
  1082. // user is not part of.
  1083. && ((in_array($item_property['to_group_id'], $group_ids)
  1084. // Drop the dropbox, notebook and chat tools (we don't care)
  1085. && ($item_property['tool'] != TOOL_DROPBOX
  1086. && $item_property['tool'] != TOOL_NOTEBOOK
  1087. && $item_property['tool'] != TOOL_CHAT)
  1088. )
  1089. )
  1090. // Take only what's visible or "invisible but where the user is a teacher" or where the visibility is unset.
  1091. && ($item_property['visibility'] == '1'
  1092. || ($course_info['status'] == '1' && $item_property['visibility'] == '0')
  1093. || !isset($item_property['visibility'])))
  1094. {
  1095. // Also drop announcements and events that are not for the user or his group.
  1096. if (($item_property['tool'] == TOOL_ANNOUNCEMENT
  1097. || $item_property['tool'] == TOOL_CALENDAR_EVENT)
  1098. && (($item_property['to_user_id'] != $user_id )
  1099. && (!isset($item_property['to_group_id'])
  1100. || !in_array($item_property['to_group_id'], $group_ids)))) {
  1101. continue;
  1102. }
  1103. // If it's a survey, make sure the user's invited. Otherwise drop it.
  1104. if ($item_property['tool'] == TOOL_SURVEY) {
  1105. $survey_info = survey_manager::get_survey($item_property['ref'], 0, $course_code);
  1106. $invited_users = SurveyUtil::get_invited_users($survey_info['code'], $course_code);
  1107. if (!in_array($user_id, $invited_users['course_users'])) continue;
  1108. }
  1109. // If it's a learning path, ensure it is currently visible to the user
  1110. if ($item_property['tool'] == TOOL_LEARNPATH) {
  1111. require_once api_get_path(SYS_CODE_PATH).'newscorm/learnpath.class.php';
  1112. if (!learnpath::is_lp_visible_for_student($item_property['ref'], $user_id, $course_code)) {
  1113. continue;
  1114. }
  1115. }
  1116. if ($item_property['tool'] == 'work' && $item_property['type'] == 'DirectoryCreated') {
  1117. $item_property['lastedit_type'] = 'WorkAdded';
  1118. }
  1119. $notifications[$item_property['tool']] = $item_property;
  1120. }
  1121. }
  1122. // Show all tool icons where there is something new.
  1123. $retvalue = '&nbsp;';
  1124. while (list($key, $notification) = each($notifications)) {
  1125. $lastDate = date('d/m/Y H:i', convert_sql_date($notification['lastedit_date']));
  1126. $type = $notification['lastedit_type'];
  1127. if (empty($course_info['id_session'])) {
  1128. $my_course['id_session'] = 0;
  1129. } else {
  1130. $my_course['id_session'] = $course_info['id_session'];
  1131. }
  1132. $label = get_lang('TitleNotification').": ".get_lang($type)." ($lastDate)";
  1133. $retvalue .= '<a href="'.api_get_path(WEB_CODE_PATH).$notification['link'].'?cidReq='.$course_code.'&amp;ref='.$notification['ref'].'&amp;gidReq='.$notification['to_group_id'].'&amp;id_session='.$my_course['id_session'].'">'.
  1134. Display::return_icon($notification['image'], $label).'</a>&nbsp;';
  1135. }
  1136. return $retvalue;
  1137. }
  1138. /**
  1139. * Displays a digest e.g. short summary of new agenda and announcements items.
  1140. * This used to be displayed in the right hand menu, but is now
  1141. * disabled by default (see config settings in this file) because most people like
  1142. * the what's new icons better.
  1143. *
  1144. * @version 1.0
  1145. */
  1146. public static function display_digest($toolsList, $digest, $orderKey, $courses) {
  1147. $html = '';
  1148. if (is_array($digest) && (CONFVAL_showExtractInfo == SCRIPTVAL_UnderCourseList || CONFVAL_showExtractInfo == SCRIPTVAL_Both)) {
  1149. // // // LEVEL 1 // // //
  1150. reset($digest);
  1151. $html .= "<br /><br />\n";
  1152. while (list($key1) = each($digest)) {
  1153. if (is_array($digest[$key1])) {
  1154. // // // Title of LEVEL 1 // // //
  1155. $html .= "<strong>\n";
  1156. if ($orderKey[0] == 'keyTools') {
  1157. $tools = $key1;
  1158. $html .= $toolsList[$key1]['name'];
  1159. } elseif ($orderKey[0] == 'keyCourse') {
  1160. $courseSysCode = $key1;
  1161. $html .= "<a href=\"".api_get_path(WEB_COURSE_PATH). $courses[$key1]['coursePath']. "\">".$courses[$key1]['courseCode']. "</a>\n";
  1162. } elseif ($orderKey[0] == 'keyTime') {
  1163. $html .= api_convert_and_format_date($digest[$key1], DATE_FORMAT_LONG, date_default_timezone_get());
  1164. }
  1165. $html .= "</strong>\n";
  1166. // // // End Of Title of LEVEL 1 // // //
  1167. // // // LEVEL 2 // // //
  1168. reset($digest[$key1]);
  1169. while (list ($key2) = each($digest[$key1])) {
  1170. // // // Title of LEVEL 2 // // //
  1171. $html .= "<p>\n". "\n";
  1172. if ($orderKey[1] == 'keyTools') {
  1173. $tools = $key2;
  1174. $html .= $toolsList[$key2][name];
  1175. } elseif ($orderKey[1] == 'keyCourse') {
  1176. $courseSysCode = $key2;
  1177. $html .= "<a href=\"". api_get_path(WEB_COURSE_PATH). $courses[$key2]['coursePath']. "\">". $courses[$key2]['courseCode']. "</a>\n";
  1178. } elseif ($orderKey[1] == 'keyTime') {
  1179. $html .= api_convert_and_format_date($key2, DATE_FORMAT_LONG, date_default_timezone_get());
  1180. }
  1181. $html .= "\n";
  1182. $html .= "</p>";
  1183. // // // End Of Title of LEVEL 2 // // //
  1184. // // // LEVEL 3 // // //
  1185. reset($digest[$key1][$key2]);
  1186. while (list ($key3, $dataFromCourse) = each($digest[$key1][$key2])) {
  1187. // // // Title of LEVEL 3 // // //
  1188. if ($orderKey[2] == 'keyTools') {
  1189. $level3title = "<a href=\"".$toolsList[$key3]["path"].$courseSysCode."\">".$toolsList[$key3]['name']."</a>";
  1190. } elseif ($orderKey[2] == 'keyCourse') {
  1191. $level3title = "&#8226; <a href=\"".$toolsList[$tools]["path"].$key3."\">".$courses[$key3]['courseCode']."</a>\n";
  1192. } elseif ($orderKey[2] == 'keyTime') {
  1193. $level3title = "&#8226; <a href=\"".$toolsList[$tools]["path"].$courseSysCode."\">".api_convert_and_format_date($key3, DATE_FORMAT_LONG, date_default_timezone_get())."</a>";
  1194. }
  1195. // // // End Of Title of LEVEL 3 // // //
  1196. // // // LEVEL 4 (data) // // //
  1197. reset($digest[$key1][$key2][$key3]);
  1198. while (list ($key4, $dataFromCourse) = each($digest[$key1][$key2][$key3])) {
  1199. $html .= $level3title. ' &ndash; '. api_substr(strip_tags($dataFromCourse), 0, CONFVAL_NB_CHAR_FROM_CONTENT);
  1200. //adding ... (three dots) if the texts are too large and they are shortened
  1201. if (api_strlen($dataFromCourse) >= CONFVAL_NB_CHAR_FROM_CONTENT) {
  1202. $html .= '...';
  1203. }
  1204. }
  1205. $html .= "<br />\n";
  1206. }
  1207. }
  1208. }
  1209. }
  1210. return $html;
  1211. }
  1212. } // End function display_digest
  1213. /**
  1214. * Get the session box details as an array
  1215. * @param int Session ID
  1216. * @return array Empty array or session array ['title'=>'...','category'=>'','dates'=>'...','coach'=>'...','active'=>true/false,'session_category_id'=>int]
  1217. */
  1218. public static function get_session_title_box($session_id)
  1219. {
  1220. global $nosession;
  1221. if (!$nosession) {
  1222. global $now, $date_start, $date_end;
  1223. }
  1224. $output = array();
  1225. if (!$nosession) {
  1226. $main_user_table = Database :: get_main_table(TABLE_MAIN_USER);
  1227. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  1228. $active = false;
  1229. // Request for the name of the general coach
  1230. $sql ='SELECT tu.lastname, tu.firstname, ts.*
  1231. FROM '.$tbl_session.' ts LEFT JOIN '.$main_user_table .' tu ON ts.id_coach = tu.user_id
  1232. WHERE ts.id = '.intval($session_id);
  1233. $rs = Database::query($sql);
  1234. $session_info = Database::store_result($rs, 'ASSOC');
  1235. $session_info = $session_info[0];
  1236. $session = array();
  1237. $session['title'] = $session_info['name'];
  1238. $session['coach'] = '';
  1239. $session['dates'] = '';
  1240. if ($session_info['date_end'] == '0000-00-00' && $session_info['date_start'] == '0000-00-00') {
  1241. if (api_get_setting('show_session_coach') === 'true') {
  1242. $session['coach'] = get_lang('GeneralCoach').': '.api_get_person_name($session_info['firstname'], $session_info['lastname']);
  1243. }
  1244. $active = true;
  1245. } else {
  1246. $start = $stop = false;
  1247. $start_buffer = $stop_buffer = '';
  1248. if ($session_info['date_start'] == '0000-00-00') {
  1249. $session_info['date_start'] = '';
  1250. } else {
  1251. $start = true;
  1252. $start_buffer = $session_info['date_start'];
  1253. $session_info['date_start'] = get_lang('From').' '.$session_info['date_start'];
  1254. }
  1255. if ($session_info['date_end'] == '0000-00-00') {
  1256. $session_info['date_end'] = '';
  1257. } else {
  1258. $stop = true;
  1259. $stop_buffer = $session_info['date_end'];
  1260. $session_info['date_end'] = get_lang('Until').' '.$session_info['date_end'];
  1261. }
  1262. if ($start && $stop) {
  1263. $session['dates'] = Display::tag('i', sprintf(get_lang('FromDateXToDateY'), $start_buffer, $stop_buffer));
  1264. } else {
  1265. $session['dates'] = Display::tag('i', $session_info['date_start'].' '.$session_info['date_end']);
  1266. }
  1267. if ( api_get_setting('show_session_coach') === 'true' ) {
  1268. $session['coach'] = get_lang('GeneralCoach').': '.api_get_person_name($session_info['firstname'], $session_info['lastname']);
  1269. }
  1270. $active = ($date_start <= $now && $date_end >= $now);
  1271. }
  1272. $session['active'] = $active;
  1273. $session['session_category_id'] = $session_info['session_category_id'];
  1274. if (array_key_exists('show_description', $session_info)) {
  1275. if (!empty($session_info['show_description'])) {
  1276. $session['description'] = $session_info['description'];
  1277. }
  1278. }
  1279. $output = $session;
  1280. }
  1281. return $output;
  1282. }
  1283. /**
  1284. * Return the five star HTML
  1285. *
  1286. * @param string id of the rating ul element
  1287. * @param string url that will be added (for jquery see hot_courses.tpl)
  1288. * @param string point info array see function CourseManager::get_course_ranking()
  1289. * @param bool add a div wrapper
  1290. * @todo use templates
  1291. **/
  1292. public static function return_rating_system($id, $url, $point_info = array(), $add_div_wrapper = true) {
  1293. $number_of_users_who_voted = isset($point_info['users_who_voted']) ? $point_info['users_who_voted'] : null;
  1294. $percentage = isset($point_info['point_average']) ? $point_info['point_average'] : 0;
  1295. if (!empty($percentage)) {
  1296. $percentage = $percentage*125/100;
  1297. }
  1298. $accesses = isset($point_info['accesses']) ? $point_info['accesses'] : 0;
  1299. $star_label = sprintf(get_lang('XStarsOutOf5'), $point_info['point_average_star']);
  1300. $html = '<ul id="'.$id.'" class="star-rating">
  1301. <li class="current-rating" style="width:'.$percentage.'px;"></li>
  1302. <li><a href="javascript:void(0);" data-link="'.$url.'&amp;star=1" title="'.$star_label.'" class="one-star">1</a></li>
  1303. <li><a href="javascript:void(0);" data-link="'.$url.'&amp;star=2" title="'.$star_label.'" class="two-stars">2</a></li>
  1304. <li><a href="javascript:void(0);" data-link="'.$url.'&amp;star=3" title="'.$star_label.'" class="three-stars">3</a></li>
  1305. <li><a href="javascript:void(0);" data-link="'.$url.'&amp;star=4" title="'.$star_label.'" class="four-stars">4</a></li>
  1306. <li><a href="javascript:void(0);" data-link="'.$url.'&amp;star=5" title="'.$star_label.'" class="five-stars">5</a></li>
  1307. </ul>';
  1308. $labels = array();
  1309. $labels[]= $number_of_users_who_voted == 1 ? $number_of_users_who_voted.' '.get_lang('Vote') : $number_of_users_who_voted.' '.get_lang('Votes');
  1310. $labels[]= $accesses == 1 ? $accesses.' '.get_lang('Visit') : $accesses.' '.get_lang('Visits');
  1311. if (!empty($number_of_users_who_voted)) {
  1312. $labels[]= get_lang('Average').' '.$point_info['point_average_star'].'/5';
  1313. }
  1314. $labels[]= $point_info['user_vote'] ? get_lang('YourVote').' ['.$point_info['user_vote'].']' : get_lang('YourVote'). ' [?] ';
  1315. if (!$add_div_wrapper && api_is_anonymous()) {
  1316. $labels[]= Display::tag('span', get_lang('LoginToVote'), array('class' => 'error'));
  1317. }
  1318. $html .= Display::span(implode(' | ', $labels) , array('id' => 'vote_label_'.$id, 'class' => 'vote_label_info'));
  1319. $html .= ' '.Display::span(' ', array('id' => 'vote_label2_'.$id));
  1320. if ($add_div_wrapper) {
  1321. $html = Display::div($html, array('id' => 'rating_wrapper_'.$id));
  1322. }
  1323. return $html;
  1324. }
  1325. public static function return_default_table_class() {
  1326. return 'data_table';
  1327. }
  1328. public static function page_header($title, $second_title = null, $size = 'h2') {
  1329. $title = Security::remove_XSS($title);
  1330. if (!empty($second_title)) {
  1331. $second_title = Security::remove_XSS($second_title);
  1332. $title .= "<small> $second_title<small>";
  1333. }
  1334. return '<div class="page-header"><'.$size.'>'.$title.'</'.$size.'></div>';
  1335. }
  1336. public static function page_header_and_translate($title, $second_title = null) {
  1337. $title = get_lang($title);
  1338. return self::page_header($title, $second_title);
  1339. }
  1340. public static function page_subheader_and_translate($title, $second_title = null) {
  1341. $title = get_lang($title);
  1342. return self::page_subheader($title, $second_title);
  1343. }
  1344. public static function page_subheader($title, $second_title = null) {
  1345. if (!empty($second_title)) {
  1346. $second_title = Security::remove_XSS($second_title);
  1347. $title .= "<small> $second_title<small>";
  1348. }
  1349. return '<div class="page-header"><h2>'.Security::remove_XSS($title).'</h2></div>';
  1350. }
  1351. public static function page_subheader2($title, $second_title = null) {
  1352. return self::page_header($title, $second_title, 'h3');
  1353. }
  1354. public static function page_subheader3($title, $second_title = null) {
  1355. return self::page_header($title, $second_title, 'h4');
  1356. }
  1357. public static function description($list) {
  1358. $html = null;
  1359. if (!empty($list)) {
  1360. $html = '<dl class="dl-horizontal">';
  1361. foreach ($list as $item) {
  1362. $html .= '<dt>'.$item['title'].'</dd>';
  1363. $html .= '<dd>'.$item['content'].'</dt>';
  1364. }
  1365. $html .= '</dl>';
  1366. }
  1367. return $html;
  1368. }
  1369. public static function bar_progress($percentage, $show_percentage = true, $extra_info = null) {
  1370. $percentage = intval($percentage);
  1371. $div = '<div class="progress progress-striped">
  1372. <div class="bar" style="width: '.$percentage.'%;"></div>
  1373. </div>';
  1374. if ($show_percentage) {
  1375. $div .= '<div class="progresstext">'.$percentage.'%</div>';
  1376. } else {
  1377. if (!empty($extra_info)) {
  1378. $div .= '<div class="progresstext">'.$extra_info.'</div>';
  1379. }
  1380. }
  1381. return $div;
  1382. }
  1383. public static function badge($count, $type ="warning") {
  1384. $class = '';
  1385. switch ($type) {
  1386. case 'success':
  1387. $class = 'badge-success';
  1388. break;
  1389. case 'warning':
  1390. $class = 'badge-warning';
  1391. break;
  1392. case 'important':
  1393. $class = 'badge-important';
  1394. break;
  1395. case 'info':
  1396. $class = 'badge-info';
  1397. break;
  1398. case 'inverse':
  1399. $class = 'badge-inverse';
  1400. break;
  1401. }
  1402. if (!empty($count)) {
  1403. return ' <span class="badge '.$class.'">'.$count.'</span>';
  1404. }
  1405. return null;
  1406. }
  1407. public static function badge_group($badge_list) {
  1408. $html = '<div class="badge-group">';
  1409. foreach ($badge_list as $badge) {
  1410. $html .= $badge;
  1411. }
  1412. $html .= '</div>';
  1413. return $html;
  1414. }
  1415. public static function label($content, $type = null) {
  1416. $class = '';
  1417. switch ($type) {
  1418. case 'success':
  1419. $class = 'label-success';
  1420. break;
  1421. case 'warning':
  1422. $class = 'label-warning';
  1423. break;
  1424. case 'important':
  1425. $class = 'label-important';
  1426. break;
  1427. case 'info':
  1428. $class = 'label-info';
  1429. break;
  1430. case 'inverse':
  1431. $class = 'label-inverse';
  1432. break;
  1433. }
  1434. $html = '';
  1435. if (!empty($content)) {
  1436. $html = '<span class="label '.$class.'">';
  1437. $html .= $content;
  1438. $html .='</span>';
  1439. }
  1440. return $html;
  1441. }
  1442. public static function actions($items) {
  1443. $html = null;
  1444. if (!empty($items)) {
  1445. $html = '<div class="new_actions"><ul class="nav nav-pills">';
  1446. foreach ($items as $value) {
  1447. $class = null;
  1448. if (isset($value['active']) && $value['active']) {
  1449. $class = 'class ="active"';
  1450. }
  1451. $html .= "<li $class >";
  1452. $html .= self::url($value['content'], $value['url']);
  1453. $html .= '</li>';
  1454. }
  1455. $html .= '</ul></div>';
  1456. }
  1457. return $html;
  1458. }
  1459. /**
  1460. * Prints a tooltip
  1461. */
  1462. public static function tip($text, $tip) {
  1463. if (empty($tip)) {
  1464. return $text;
  1465. }
  1466. return self::span($text, array('class' => 'boot-tooltip', 'title' => strip_tags($tip)));
  1467. }
  1468. public static function generate_accordion($items, $type = 'jquery', $id = null) {
  1469. $html = null;
  1470. if (!empty($items)) {
  1471. if (empty($id)) {
  1472. $id = api_get_unique_id();
  1473. }
  1474. if ($type == 'jquery') {
  1475. $html = '<div class="accordion_jquery" id="'.$id.'">'; //using jquery
  1476. } else {
  1477. $html = '<div class="accordion" id="'.$id.'">'; //using bootstrap
  1478. }
  1479. $count = 1;
  1480. foreach ($items as $item) {
  1481. $html .= '<div class="accordion-my-group">';
  1482. $html .= '<div class="accordion-heading">
  1483. <a class="accordion-toggle" data-toggle="collapse" data-parent="#'.$id.'" href="#collapse'.$count.'">
  1484. '.$item['title'].'
  1485. </a>
  1486. </div>';
  1487. $html .= '<div id="collapse'.$count.'" class="accordion-body">';
  1488. $html .= '<div class="accordion-my-inner">
  1489. '.$item['content'].'
  1490. </div>
  1491. </div>';
  1492. }
  1493. $html .= '</div>';
  1494. }
  1495. return $html;
  1496. }
  1497. /**
  1498. * @todo use twig
  1499. */
  1500. public static function group_button($title, $elements) {
  1501. $html = '<div class="btn-toolbar">
  1502. <div class="btn-group">
  1503. <button class="btn dropdown-toggle" data-toggle="dropdown">'.$title.' <span class="caret"></span></button>
  1504. <ul class="dropdown-menu">';
  1505. foreach ($elements as $item) {
  1506. $html .= Display::tag('li', Display::url($item['title'], $item['href']));
  1507. }
  1508. $html .= '</ul>
  1509. </div> </div>';
  1510. return $html;
  1511. }
  1512. /**
  1513. * @param string $file
  1514. * @param array $params
  1515. * @return null|string
  1516. */
  1517. public static function getMediaPlayer($file, $params = array())
  1518. {
  1519. $fileInfo = pathinfo($file);
  1520. switch ($fileInfo['extension']) {
  1521. case 'wav':
  1522. if (isset($params['url'])) {
  1523. return DocumentManager::readNanogongFile($params['url']);
  1524. }
  1525. break;
  1526. case 'mp3':
  1527. case 'webm':
  1528. $autoplay = null;
  1529. if (isset($params['autoplay']) && $params['autoplay'] == 'true') {
  1530. $autoplay = 'autoplay';
  1531. }
  1532. $width = isset($params['width']) ? 'width="'.$params['width'].'"' : null;
  1533. $id = isset($params['id']) ? $params['id'] : $fileInfo['basename'];
  1534. $class = isset($params['class']) ? ' class="'.$params['class'].'"' : null;
  1535. $html = '<audio id="'.$id.'" '.$class.' controls '.$autoplay.' '.$width.' src="'.$params['url'].'" >';
  1536. $html .= '<object width="'.$width.'" height="50" type="application/x-shockwave-flash" data="'.api_get_path(WEB_LIBRARY_PATH).'javascript/mediaelement/flashmediaelement.swf">
  1537. <param name="movie" value="'.api_get_path(WEB_LIBRARY_PATH).'javascript/mediaelement/flashmediaelement.swf" />
  1538. <param name="flashvars" value="controls=true&file='.$params['url'].'" />
  1539. </object>';
  1540. $html .= '</audio>';
  1541. return $html;
  1542. break;
  1543. }
  1544. return null;
  1545. }
  1546. }