dashboard.lib.php 17 KB

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