system_charts.module

Tracking 6.x-1.x branch
  1. drupal
    1. 6 contributions/chart/contrib/system_charts/system_charts.module
  • Provides core Drupal chart implementations.

*

  • @author Tj Holowaychuk <http://www.350designs.com/>
  • @package Chart

Functions & methods

NameDescription
system_chartsPage callback.
system_charts_buildGather data and build a chart API structure.
system_charts_chart_color_schemesImplementation of hook_chart_color_schemes();
system_charts_displayDisplay charts and content in context to the current page.
system_charts_get_page_infoGet available report page information.
system_charts_menuImplementation of hook_menu().
system_charts_permImplementation of hook_perm();
_system_charts_user_status_labelReturn user status label
_system_charts_watchdog_severity_labelReturn the watchdog severity label

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Provides core Drupal chart implementations.
  5. *
  6. * @author Tj Holowaychuk <http://www.350designs.com/>
  7. * @package Chart
  8. */
  9. /*-----------------------------------------------------------------
  10. * Hook Implementations
  11. *------------------------------------------------------------------*/
  12. /**
  13. * Implementation of hook_perm();
  14. */
  15. function system_charts_perm(){
  16. return array('administer system charts', 'access system charts');
  17. }
  18. /**
  19. * Implementation of hook_menu().
  20. */
  21. function system_charts_menu() {
  22. $items = array();
  23. $items['admin/reports/charts'] = array(
  24. 'title' => 'System Charts',
  25. 'page callback' => 'system_charts',
  26. 'page arguments' => array('nodes'),
  27. 'access arguments' => array('access system charts'),
  28. );
  29. $items['admin/reports/charts/nodes'] = array(
  30. 'title' => 'Nodes',
  31. 'page callback' => 'system_charts',
  32. 'page arguments' => array('nodes'),
  33. 'access arguments' => array('access system charts'),
  34. 'type' => MENU_DEFAULT_LOCAL_TASK,
  35. );
  36. $items['admin/reports/charts/users'] = array(
  37. 'title' => 'Users',
  38. 'page callback' => 'system_charts',
  39. 'page arguments' => array('users'),
  40. 'access arguments' => array('access system charts'),
  41. 'type' => MENU_LOCAL_TASK,
  42. );
  43. $items['admin/reports/charts/watchdog'] = array(
  44. 'title' => 'Watchdog',
  45. 'page callback' => 'system_charts',
  46. 'page arguments' => array('watchdog'),
  47. 'access arguments' => array('access system charts'),
  48. 'type' => MENU_LOCAL_TASK,
  49. );
  50. return $items;
  51. }
  52. /**
  53. * Implementation of hook_chart_color_schemes();
  54. */
  55. function system_charts_chart_color_schemes(&$colors) {
  56. $colors['watchdog_severity'] = array(
  57. 'Error' => 'a00000',
  58. 'Warning' => 'e06000',
  59. 'Notice' => 'f0c040',
  60. );
  61. }
  62. /* -----------------------------------------------------------------
  63. General Functionality
  64. ------------------------------------------------------------------ */
  65. /**
  66. * Page callback.
  67. */
  68. function system_charts($page = 'nodes') {
  69. return '<div id="system-charts">' . system_charts_display($page) . '</div><!-- END #system-charts -->';
  70. }
  71. /**
  72. * Display charts and content in context to the current page.
  73. *
  74. * @return string
  75. * markup, chart images.
  76. */
  77. function system_charts_display($page = 'nodes') {
  78. $output = '';
  79. switch($page){
  80. case 'users':
  81. $output .= system_charts_build('users_per_role');
  82. $output .= system_charts_build('user_status');
  83. break;
  84. case 'nodes':
  85. $output .= system_charts_build('node_counts');
  86. $output .= system_charts_build('node_counts_published');
  87. $output .= system_charts_build('node_counts_unpublished');
  88. $output .= system_charts_build('node_activity');
  89. break;
  90. case 'watchdog':
  91. $output .= system_charts_build('watchdog_counts');
  92. $output .= system_charts_build('watchdog_severity');
  93. break;
  94. }
  95. return $output;
  96. }
  97. /**
  98. * Gather data and build a chart API structure.
  99. *
  100. * @return array
  101. * chart API structure.
  102. */
  103. function system_charts_build($type) {
  104. $chart = array();
  105. $now = (isset($_GET['year']) && isset($_GET['month'])) ? mktime(0, 0, 0, $_GET['month'], 30, $_GET['year']) : time();
  106. switch($type) {
  107. case 'node_counts':
  108. case 'node_counts_published':
  109. case 'node_counts_unpublished':
  110. case 'node_counts_today':
  111. $sql_where = '';
  112. switch($type){
  113. case 'node_counts':
  114. $title = t('Total');
  115. break;
  116. case 'node_counts_published':
  117. $title = t('Published');
  118. $sql_where = " WHERE status = '1' ";
  119. break;
  120. case 'node_counts_unpublished':
  121. $title = t('Unpublished');
  122. $sql_where = " WHERE status = '0' ";
  123. break;
  124. }
  125. $results = db_query("
  126. SELECT COUNT(*) as count, type
  127. FROM {node}
  128. " . $sql_where . "
  129. GROUP BY type
  130. ORDER BY type
  131. ");
  132. while ($result = db_fetch_array($results)){
  133. $chart['#data'][] = $result['count'];
  134. $chart['#labels'][] = $result['type'].': '.$result['count'];
  135. $chart['#data_colors'][] = chart_unique_color($result['type']);
  136. }
  137. $chart['#chart_id'] = $type;
  138. $chart['#title'] = chart_title($title);
  139. $chart['#type'] = CHART_TYPE_PIE;
  140. $chart['#size'] = chart_size(600, 350);
  141. break;
  142. case 'node_activity':
  143. $results = db_query("
  144. SELECT type, created
  145. FROM {node}
  146. WHERE created < %d AND created > %d
  147. ORDER BY created
  148. ", $now, mktime(0, 0, 0, date('m', $now), 1, date('Y', $now)));
  149. $max = array();
  150. $counts = array();
  151. $types = array();
  152. while ($result = db_fetch_array($results)){
  153. $day = ltrim(date('d', $result['created']), '0');
  154. $types[$result['type']] = $result['type'];
  155. $counts[$day][$result['type']]++;
  156. $max[$result['type']]++;
  157. }
  158. // Generate data and labels
  159. if (count($counts) && count($types)){
  160. for($i = 0; $i <= date('d', $now); $i++){
  161. $chart['#labels'][] = $i;
  162. foreach($types AS $type){
  163. if ($counts[$i][$type]){
  164. $chart['#data'][$type][] = $counts[$i][$type];
  165. }
  166. else{
  167. $chart['#data'][$type][] = '0';
  168. }
  169. }
  170. }
  171. }
  172. // Data colors, legends, line styles, and labels
  173. if (count($types)){
  174. foreach($types AS $type){
  175. $chart['#data_colors'][] = chart_unique_color($type);
  176. $chart['#legends'][] = $type;
  177. $chart['#line_styles'][] = chart_line_style(2);
  178. }
  179. }
  180. $max = count($max) ? max($max) : 0;
  181. $chart['#chart_id'] = 'node_activity';
  182. $chart['#title'] = chart_title(t('Node Activity for !date', array('!date' => date('F Y', $now))));
  183. $chart['#type'] = CHART_TYPE_LINE;
  184. $chart['#size'] = chart_size(620, 250);
  185. $chart['#grid_lines'] = chart_grid_lines(25, 9.5, 1, 3);
  186. $chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][0][] = chart_mixed_axis_range_label(0, $max);
  187. $chart['#adjust_resolution'] = TRUE;
  188. break;
  189. case 'watchdog_counts':
  190. $results = db_query("
  191. SELECT COUNT(*) as count, type
  192. FROM {watchdog}
  193. GROUP BY type
  194. ORDER BY type
  195. ");
  196. while ($result = db_fetch_array($results)){
  197. $chart['#data'][] = $result['count'];
  198. $chart['#labels'][] = $result['type'] . ': ' . $result['count'];
  199. $chart['#data_colors'][] = chart_unique_color($result['type']);
  200. }
  201. $chart['#chart_id'] = 'watchdog_counts';
  202. $chart['#title'] = chart_title(t('Watchdog Messages'));
  203. $chart['#type'] = CHART_TYPE_PIE;
  204. $chart['#size'] = chart_size(600, 350);
  205. break;
  206. case 'watchdog_severity':
  207. $results = db_query("
  208. SELECT COUNT(*) as count, severity
  209. FROM {watchdog}
  210. GROUP BY severity
  211. ORDER BY severity
  212. ");
  213. while ($result = db_fetch_array($results)){
  214. $severity_label = _system_charts_watchdog_severity_label($result['severity']);
  215. $chart['#data'][] = $result['count'];
  216. $chart['#labels'][] = $severity_label . ': ' . $result['count'];
  217. $chart['#data_colors'][] = chart_unique_color($severity_label, 'watchdog_severity');
  218. }
  219. $chart['#chart_id'] = 'watchdog_severity';
  220. $chart['#title'] = chart_title(t('Message Severity'));
  221. $chart['#type'] = CHART_TYPE_PIE;
  222. $chart['#size'] = chart_size(600, 350);
  223. break;
  224. case 'users_per_role':
  225. $results = db_query("
  226. SELECT COUNT(*) as count, r.*
  227. FROM {users_roles} ur
  228. LEFT JOIN {users} u ON ur.uid = u.uid
  229. LEFT JOIN {role} r ON r.rid = ur.rid
  230. GROUP BY r.rid, r.name
  231. ORDER BY r.name
  232. ");
  233. while ($result = db_fetch_array($results)){
  234. $chart['#data'][] = $result['count'];
  235. $chart['#labels'][] = $result['name'] . ': ' . $result['count'];
  236. $chart['#data_colors'][] = chart_unique_color('role_' . $result['name']);
  237. }
  238. $chart['#chart_id'] = 'users_per_role';
  239. $chart['#title'] = chart_title('Users Per Role');
  240. $chart['#type'] = CHART_TYPE_PIE;
  241. $chart['#size'] = chart_size(600, 350);
  242. break;
  243. case 'user_status':
  244. $results = db_query("
  245. SELECT COUNT(*) as count, status
  246. FROM {users}
  247. WHERE uid != 0
  248. GROUP BY status
  249. ORDER BY status
  250. ");
  251. while ($result = db_fetch_array($results)){
  252. $chart['#data'][] = $result['count'];
  253. $chart['#labels'][] = _system_charts_user_status_label($result['status']) . ': ' . $result['count'];
  254. $chart['#data_colors'][] = chart_unique_color('status_' . $result['status']);
  255. }
  256. $chart['#chart_id'] = 'user_status';
  257. $chart['#title'] = chart_title('User Status');
  258. $chart['#type'] = CHART_TYPE_PIE;
  259. $chart['#size'] = chart_size(600, 350);
  260. break;
  261. }
  262. return chart_render($chart);
  263. }
  264. /**
  265. * Get available report page information.
  266. *
  267. * @return array
  268. * report information.
  269. */
  270. function system_charts_get_page_info() {
  271. return array(
  272. 'nodes' => array(
  273. '#title' => t('Nodes'),
  274. '#type' => 'nodes',
  275. '#description' => t('Various node data reports.'),
  276. ),
  277. 'users' => array(
  278. '#title' => t('Users'),
  279. '#type' => 'users',
  280. '#description' => t('User access and information reporting.'),
  281. ),
  282. 'watchdog' => array(
  283. '#title' => t('Watchdog'),
  284. '#type' => 'watchdog',
  285. '#description' => t('Log charts.'),
  286. ),
  287. );
  288. }
  289. /*-----------------------------------------------------------------
  290. * Helpers
  291. *------------------------------------------------------------------*/
  292. /**
  293. * Return the watchdog severity label
  294. */
  295. function _system_charts_watchdog_severity_label($severity) {
  296. switch($severity){
  297. case WATCHDOG_NOTICE:
  298. return t('Notice');
  299. break;
  300. case WATCHDOG_WARNING:
  301. return t('Warning');
  302. break;
  303. case WATCHDOG_ERROR:
  304. return t('Error');
  305. break;
  306. }
  307. }
  308. /**
  309. * Return user status label
  310. */
  311. function _system_charts_user_status_label($status) {
  312. return $status ? t('Active') : t('Blocked') ;
  313. }