dashboard.lib.php 18 KB

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