dashboard.lib.php 17 KB

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