dashboard.lib.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file contains a class used like library, provides functions for dashboard.
  5. * @author Christian Fasanando <christian1827@gmail.com>
  6. * @package chamilo.dashboard
  7. */
  8. /**
  9. * Code
  10. */
  11. // required files
  12. /**
  13. * DashboardManager can be used to manage dashboard
  14. * @package chamilo.dashboard
  15. */
  16. class DashboardManager {
  17. /**
  18. * contructor
  19. */
  20. private function __construct() {}
  21. /**
  22. * This function allows easy activating and inactivating of dashboard plugins
  23. * @return void
  24. */
  25. public static function handle_dashboard_plugins() {
  26. /* We scan the plugin directory. Each folder is a potential plugin. */
  27. $dashboard_pluginpath = api_get_path(SYS_PLUGIN_PATH).'dashboard/';
  28. $possibleplugins = self::get_posible_dashboard_plugins_path();
  29. $table_cols = array('name', 'version', 'description');
  30. echo Display::page_subheader(get_lang('DashboardPlugins'));
  31. echo '<form name="plugins" method="post" action="'.api_get_self().'?category='.Security::remove_XSS($_GET['category']).'">';
  32. echo '<table class="data_table">';
  33. echo '<tr>';
  34. echo '<th width="50px">'.get_lang('Enabled').'</th>';
  35. echo '<th width="250px">'.get_lang('Name').'</th>';
  36. echo '<th width="100px">'.get_lang('Version').'</th>';
  37. echo '<th>'.get_lang('Description').'</th>';
  38. echo '</tr>';
  39. $disabled_blocks_data = self::get_block_data_without_plugin();
  40. // We display all the possible enabled or disabled plugins
  41. foreach ($possibleplugins as $testplugin) {
  42. $plugin_info_file = $dashboard_pluginpath.$testplugin."/$testplugin.info";
  43. $plugin_info = array();
  44. if (file_exists($plugin_info_file) && is_readable($plugin_info_file)) {
  45. $plugin_info = api_parse_info_file($plugin_info_file);
  46. // change index to lower case
  47. $plugin_info = array_change_key_case($plugin_info);
  48. echo '<tr>';
  49. self::display_dashboard_plugin_checkboxes($testplugin);
  50. for ($i = 0 ; $i < count($table_cols); $i++) {
  51. if (isset($plugin_info[strtolower($table_cols[$i])])) {
  52. echo '<td>';
  53. echo $plugin_info[$table_cols[$i]];
  54. echo '</td>';
  55. } else {
  56. echo '<td></td>';
  57. }
  58. }
  59. echo '</tr>';
  60. } else {
  61. echo Display::tag('tr', Display::tag('td', get_lang('CheckFilePermissions').' '.Security::remove_XSS($plugin_info_file) , array('colspan'=>'3')));
  62. }
  63. }
  64. // display all disabled block data
  65. if (count($disabled_blocks_data) > 0) {
  66. foreach ($disabled_blocks_data as $disabled_block) {
  67. echo '<tr style="background-color:#eee">';
  68. echo '<td><input type="checkbox" name="disabled_block" value="true" checked disabled />';
  69. for ($j = 0 ; $j < count($table_cols); $j++) {
  70. if (isset($disabled_block[strtolower($table_cols[$j])])) {
  71. if ($j == 2) {
  72. echo '<td>';
  73. echo '<font color="#aaa">'.$disabled_block[$table_cols[$j]].'</font><br />';
  74. echo '<font color="red">'.get_lang('ThisPluginHasbeenDeletedFromDashboardPluginDirectory').'</font>';
  75. echo '</td>';
  76. } else {
  77. echo '<td>';
  78. echo '<font color="#aaa">'.$disabled_block[$table_cols[$j]].'</font>';
  79. echo '</td>';
  80. }
  81. } else {
  82. echo '<td>&nbsp;</td>';
  83. }
  84. }
  85. echo '</tr>';
  86. }
  87. }
  88. echo '</table>';
  89. echo '<br />';
  90. echo '<button class="btn btn-primary" type="submit" name="submit_dashboard_plugins" value="'.get_lang('EnableDashboardPlugins').'">'.get_lang('EnableDashboardPlugins').'</button></form>';
  91. }
  92. /**
  93. * display checkboxes for dashboard plugin list
  94. * @param string plugin path
  95. * @return void
  96. */
  97. public static function display_dashboard_plugin_checkboxes($plugin_path) {
  98. $tbl_block = Database::get_main_table(TABLE_MAIN_BLOCK);
  99. $plugin_path = Database::escape_string($plugin_path);
  100. $sql = "SELECT * FROM $tbl_block WHERE path = '$plugin_path' AND active = 1";
  101. $rs = Database::query($sql);
  102. $checked = '';
  103. if (Database::num_rows($rs) > 0) {
  104. $checked = "checked";
  105. }
  106. echo "<td align=\"center\">";
  107. echo '<input type="checkbox" name="'.$plugin_path.'" value="true" '.$checked.'/>';
  108. echo "</td>";
  109. }
  110. /**
  111. * This function allows easy activating and inactivating of plugins and save them inside db
  112. * @param array dashboard plugin paths
  113. * return int affected rows
  114. */
  115. public static function store_dashboard_plugins($plugin_paths)
  116. {
  117. $tbl_block = Database :: get_main_table(TABLE_MAIN_BLOCK);
  118. $affected_rows = 0;
  119. // get all plugins path inside plugin directory
  120. $dashboard_pluginpath = api_get_path(SYS_PLUGIN_PATH).'dashboard/';
  121. $possibleplugins = self::get_posible_dashboard_plugins_path();
  122. if (count($possibleplugins) > 0) {
  123. $selected_plugins = array_intersect(array_keys($plugin_paths),$possibleplugins);
  124. $not_selected_plugins = array_diff($possibleplugins,array_keys($plugin_paths));
  125. // get blocks id from not selected path
  126. $not_selected_blocks_id = array();
  127. foreach ($not_selected_plugins as $plugin) {
  128. $block_data = self::get_enabled_dashboard_blocks($plugin);
  129. if (!empty($block_data[$plugin])) {
  130. $not_selected_blocks_id[] = $block_data[$plugin]['id'];
  131. }
  132. }
  133. /* clean not selected plugins for extra user data and block data */
  134. // clean from extra user data
  135. $field_variable = 'dashboard';
  136. $extra_user_data = UserManager::get_extra_user_data_by_field_variable($field_variable);
  137. foreach ($extra_user_data as $key => $user_data) {
  138. $user_id = $key;
  139. $user_block_data = self::get_user_block_data($user_id);
  140. $user_block_id = array_keys($user_block_data);
  141. // clean disabled block data
  142. foreach ($user_block_id as $block_id) {
  143. if (in_array($block_id, $not_selected_blocks_id)) {
  144. unset($user_block_data[$block_id]);
  145. }
  146. }
  147. // get columns and blocks id for updating extra user data
  148. $columns = array();
  149. $user_blocks_id = array();
  150. foreach ($user_block_data as $data) {
  151. $user_blocks_id[$data['block_id']] = true;
  152. $columns[$data['block_id']] = $data['column'];
  153. }
  154. // update extra user blocks data
  155. $upd_extra_field = self::store_user_blocks($user_id, $user_blocks_id, $columns);
  156. }
  157. // clean from block data
  158. if (!empty($not_selected_blocks_id)) {
  159. $sql_check = "SELECT id FROM $tbl_block WHERE id IN(".implode(',',$not_selected_blocks_id).")";
  160. $rs_check = Database::query($sql_check);
  161. if (Database::num_rows($rs_check) > 0) {
  162. $del = "DELETE FROM $tbl_block WHERE id IN(".implode(',',$not_selected_blocks_id).")";
  163. Database::query($del);
  164. }
  165. }
  166. // store selected plugins
  167. foreach ($selected_plugins as $testplugin) {
  168. $selected_path = Database::escape_string($testplugin);
  169. // check if the path already stored inside block table for updating or adding it
  170. $sql = "SELECT path FROM $tbl_block WHERE path = '$selected_path'";
  171. $rs = Database::query($sql);
  172. if (Database::num_rows($rs) > 0) {
  173. // update
  174. $upd = "UPDATE $tbl_block SET active = 1 WHERE path = '$selected_path'";
  175. Database::query($upd);
  176. } else {
  177. // insert
  178. $plugin_info_file = $dashboard_pluginpath.$testplugin."/$testplugin.info";
  179. $plugin_info = array();
  180. if (file_exists($plugin_info_file)) {
  181. $plugin_info = api_parse_info_file($plugin_info_file);
  182. }
  183. // change keys to lower case
  184. $plugin_info = array_change_key_case($plugin_info);
  185. // setting variables
  186. $plugin_name = $testplugin;
  187. $plugin_description = '';
  188. $plugin_controller = '';
  189. $plugin_path = $testplugin;
  190. if (isset($plugin_info['name'])) {
  191. $plugin_name = Database::escape_string($plugin_info['name']);
  192. }
  193. if (isset($plugin_info['description'])) {
  194. $plugin_description = Database::escape_string($plugin_info['description']);
  195. }
  196. if (isset($plugin_info['controller'])) {
  197. $plugin_controller = Database::escape_string($plugin_info['controller']);
  198. }
  199. $ins = "INSERT INTO $tbl_block(name, description, path, controller) VALUES ('$plugin_name', '$plugin_description', '$plugin_path', '$plugin_controller')";
  200. Database::query($ins);
  201. }
  202. // $affected_rows = Database::affected_rows();
  203. }
  204. }
  205. return true;
  206. }
  207. /**
  208. * Get all plugins path inside dashboard directory
  209. * @return array name plugins directories
  210. */
  211. public static function get_posible_dashboard_plugins_path() {
  212. // get all plugins path inside plugin directory
  213. /* We scan the plugin directory. Each folder is a potential plugin. */
  214. $possibleplugins = array();
  215. $dashboard_pluginpath = api_get_path(SYS_PLUGIN_PATH).'dashboard/';
  216. $handle = @opendir($dashboard_pluginpath);
  217. while (false !== ($file = readdir($handle)))
  218. {
  219. if ($file <> '.' AND $file <> '..' AND is_dir($dashboard_pluginpath.$file))
  220. {
  221. $possibleplugins[] = $file;
  222. }
  223. }
  224. @closedir($handle);
  225. return $possibleplugins;
  226. }
  227. /**
  228. * Get all blocks data without plugin directory
  229. * @return array Block data
  230. */
  231. public static function get_block_data_without_plugin() {
  232. $tbl_block = Database :: get_main_table(TABLE_MAIN_BLOCK);
  233. $possibleplugins = self::get_posible_dashboard_plugins_path();
  234. // We check if plugin exists inside directory for updating active field
  235. $sql = "SELECT * FROM $tbl_block";
  236. $rs = Database::query($sql);
  237. if (Database::num_rows($rs) > 0){
  238. while ($row = Database::fetch_array($rs)) {
  239. $path = $row['path'];
  240. if (!in_array($row['path'],$possibleplugins)) {
  241. $active = 0;
  242. } else {
  243. $active = 1;
  244. }
  245. // update active
  246. $upd = "UPDATE $tbl_block SET active = '$active' WHERE path = '".$row['path']."'";
  247. Database::query($upd);
  248. }
  249. }
  250. // get disabled block data
  251. $block_data = array();
  252. $sql = "SELECT * FROM $tbl_block WHERE active = 0";
  253. $rs_block = Database::query($sql);
  254. if (Database::num_rows($rs_block) > 0) {
  255. while ($row_block = Database::fetch_array($rs_block)) {
  256. $block_data[] = $row_block;
  257. }
  258. }
  259. return $block_data;
  260. }
  261. /**
  262. * get data about enabled dashboard block (stored insise block table)
  263. * @param string plugin path
  264. * @return array data
  265. */
  266. public static function get_enabled_dashboard_blocks($path = '') {
  267. $tbl_block = Database :: get_main_table(TABLE_MAIN_BLOCK);
  268. $condition_path = '';
  269. if (!empty($path)) {
  270. $path = Database::escape_string($path);
  271. $condition_path = ' AND path = "'.$path.'" ';
  272. }
  273. $sql = "SELECT * FROM $tbl_block WHERE active = 1 $condition_path ";
  274. $rs = Database::query($sql);
  275. $block_data = array();
  276. if (Database::num_rows($rs) > 0) {
  277. while ($row = Database::fetch_array($rs)) {
  278. $block_data[$row['path']] = $row;
  279. }
  280. }
  281. return $block_data;
  282. }
  283. /**
  284. * display user dashboard list
  285. * @param int User id
  286. * @return void
  287. */
  288. public static function display_user_dashboard_list($user_id) {
  289. $block_data_without_plugin = self::get_block_data_without_plugin();
  290. $enabled_dashboard_plugins = self::get_enabled_dashboard_blocks();
  291. $user_block_data = self::get_user_block_data($user_id);
  292. if (count($enabled_dashboard_plugins) > 0) {
  293. echo '<div style="margin-top:20px">';
  294. echo '<div><strong>'.get_lang('SelectBlockForDisplayingInsideBlocksDashboardView').'</strong></div><br />';
  295. echo '<form name="dashboard_list" method="post" action="index.php?action=store_user_block">';
  296. echo '<table class="data_table">';
  297. echo '<tr>';
  298. echo '<th width="5%">';
  299. echo get_lang('Enabled');
  300. echo '</th>';
  301. echo '<th width="30%">';
  302. echo get_lang('Name');
  303. echo '</th>';
  304. echo '<th width="40%">';
  305. echo get_lang('Description');
  306. echo '</th>';
  307. echo '<th>';
  308. echo get_lang('ColumnPosition');
  309. echo '</th>';
  310. echo '</tr>';
  311. // We display all enabled plugins and the checkboxes
  312. foreach ($enabled_dashboard_plugins as $block) {
  313. $path = $block['path'];
  314. $controller_class = $block['controller'];
  315. $filename_controller = $path.'.class.php';
  316. $dashboard_plugin_path = api_get_path(SYS_PLUGIN_PATH).'dashboard/'.$path.'/';
  317. require_once $dashboard_plugin_path.$filename_controller;
  318. if (class_exists($controller_class)) {
  319. $obj_block = new $controller_class($user_id);
  320. // check if user is allowed to see the block
  321. if (method_exists($obj_block, 'is_block_visible_for_user')) {
  322. $is_block_visible_for_user = $obj_block->is_block_visible_for_user($user_id);
  323. if (!$is_block_visible_for_user) continue;
  324. }
  325. echo '<tr>';
  326. // checkboxes
  327. self::display_user_dashboard_list_checkboxes($user_id, $block['id']);
  328. echo '<td>'.$block['name'].'</td>';
  329. echo '<td>'.$block['description'].'</td>';
  330. echo '<td><center>
  331. <select name="columns['.$block['id'].']">
  332. <option value="1" '.(isset($user_block_data[$block['id']]) && $user_block_data[$block['id']]['column']==1?'selected':'').' >1</option>
  333. <option value="2" '.(isset($user_block_data[$block['id']]) && $user_block_data[$block['id']]['column']==2?'selected':'').' >2</option>
  334. </select></center>
  335. </td>';
  336. echo '</tr>';
  337. } else {
  338. echo Display::tag('tr', Display::tag('td', get_lang('Error').' '.$controller_class, array('colspan'=>'3')));
  339. }
  340. }
  341. echo '</table>';
  342. echo '<br />';
  343. echo '<button class="save" type="submit" name="submit_dashboard_list" value="'.get_lang('EnableDashboardBlock').'">'.get_lang('EnableDashboardBlock').'</button></form>';
  344. echo '</div>';
  345. } else {
  346. echo '<div style="margin-top:20px">'.get_lang('ThereAreNoEnabledDashboardPlugins').'</div>';
  347. if (api_is_platform_admin()) {
  348. echo '<a href="'.api_get_path(WEB_CODE_PATH).'admin/settings.php?category=Plugins">'.get_lang('ConfigureDashboardPlugin').'</a>';
  349. }
  350. }
  351. }
  352. /**
  353. * display checkboxes for user dashboard list
  354. * @param int User id
  355. * @param int Block id
  356. * @return void
  357. */
  358. public static function display_user_dashboard_list_checkboxes($user_id, $block_id) {
  359. $user_id = intval($user_id);
  360. $user_block_data = self::get_user_block_data($user_id);
  361. $enabled_blocks_id = array_keys($user_block_data);
  362. $checked = '';
  363. if (in_array($block_id, $enabled_blocks_id)) {
  364. $checked = "checked";
  365. }
  366. echo "<td align=\"center\">";
  367. echo '<input type="checkbox" name="enabled_blocks['.$block_id.']" value="true" '.$checked.'/>';
  368. echo "</td>";
  369. }
  370. /**
  371. * This function store enabled blocks id with its column position (block_id1:colum;block_id2:colum; ...) inside extra user fields
  372. * @param int User id
  373. * @param array selected blocks
  374. * @param array columns position
  375. * @return bool
  376. */
  377. public static function store_user_blocks($user_id, $enabled_blocks, $columns) {
  378. $selected_blocks_id = array();
  379. if (is_array($enabled_blocks) && count($enabled_blocks) > 0) {
  380. $selected_blocks_id = array_keys($enabled_blocks);
  381. }
  382. // build data for storing inside extra user field
  383. $fname = 'dashboard';
  384. $fvalue = array();
  385. foreach ($selected_blocks_id as $block_id) {
  386. $fvalue[] = $block_id.':'.$columns[$block_id];
  387. }
  388. $upd_extra_field = UserManager::update_extra_field_value($user_id, $fname, $fvalue);
  389. return $upd_extra_field;
  390. }
  391. /**
  392. * This function get user block data (block id with its number of column) from extra user data
  393. * @param int User id
  394. * @return array data (block_id,column)
  395. */
  396. public static function get_user_block_data($user_id) {
  397. $user_id = intval($user_id);
  398. $field_variable = 'dashboard';
  399. $extra_user_data = UserManager::get_extra_user_data_by_field($user_id, $field_variable);
  400. $extra_user_data = explode(';',$extra_user_data[$field_variable]);
  401. $data = array();
  402. foreach ($extra_user_data as $extra) {
  403. $split_extra = explode(':',$extra);
  404. if (!empty($split_extra)) {
  405. $block_id = $split_extra[0];
  406. $column = isset($split_extra[1]) ? $split_extra[1] : null;
  407. $data[$block_id] = array('block_id' => $block_id, 'column' => $column);
  408. }
  409. }
  410. return $data;
  411. }
  412. /**
  413. * This function update extra user blocks data after closing a dashboard block
  414. * @param int User id
  415. * @param string plugin path
  416. * @return bool
  417. */
  418. public static function close_user_block($user_id, $path) {
  419. $enabled_dashboard_blocks = self::get_enabled_dashboard_blocks($path);
  420. $user_block_data = self::get_user_block_data($user_id);
  421. foreach ($enabled_dashboard_blocks as $enabled_block) {
  422. unset($user_block_data[$enabled_block['id']]);
  423. }
  424. // get columns and blocks id for updating extra user data
  425. $columns = array();
  426. $user_blocks_id = array();
  427. foreach ($user_block_data as $data) {
  428. $user_blocks_id[$data['block_id']] = true;
  429. $columns[$data['block_id']] = $data['column'];
  430. }
  431. // update extra user blocks data
  432. $upd_extra_field = self::store_user_blocks($user_id, $user_blocks_id, $columns);
  433. return $upd_extra_field;
  434. }
  435. /**
  436. * get links for styles from dashboard plugins
  437. * @return string links
  438. */
  439. public static function get_links_for_styles_from_dashboard_plugins() {
  440. $possible_paths = self::get_posible_dashboard_plugins_path();
  441. $enabled_blocks = self::get_enabled_dashboard_blocks();
  442. $links = '';
  443. foreach ($enabled_blocks as $block) {
  444. $path = $block['path'];
  445. $dashboard_plugin_path = api_get_path(SYS_PLUGIN_PATH).'dashboard/'.$path.'/css/';
  446. if (is_dir($dashboard_plugin_path)) {
  447. $handle = @opendir($dashboard_plugin_path);
  448. while (false !== ($file = readdir($handle))) {
  449. if ($file <> '.' AND $file <> '..') {
  450. $src = api_get_path(WEB_PLUGIN_PATH).'dashboard/'.$path.'/css/'.$file;
  451. $links .= '<link rel="stylesheet" href="'.$src.'" type="text/css" />'.PHP_EOL;
  452. }
  453. }
  454. @closedir($handle);
  455. }
  456. }
  457. return $links;
  458. }
  459. }