/[drupal]/contributions/modules/chart/contrib/system_charts/system_charts.module
ViewVC logotype

Diff of /contributions/modules/chart/contrib/system_charts/system_charts.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

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

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.10

  ViewVC Help
Powered by ViewVC 1.1.2