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