dashboard.lib.php 18 KB

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