dashboard.lib.php 21 KB

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