dashboard.lib.php 17 KB

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