dashboard.lib.php 21 KB

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