| 1 |
<?php |
<?php |
| 2 |
// $Id: graphstat.module,v 1.13 2007/11/14 22:29:56 profix898 Exp $ |
// $Id: graphstat.module,v 1.13.2.1 2009/01/25 14:32:28 profix898 Exp $ |
|
|
|
|
require_once (drupal_get_path('module', 'graphstat') .'/graphstat_statistics.inc'); |
|
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* Implementation of hook_perm(). |
* Implementation of hook_perm(). |
| 23 |
} |
} |
| 24 |
|
|
| 25 |
/** |
/** |
|
* Implementation of hook_init(). |
|
|
*/ |
|
|
function graphstat_init() { |
|
|
drupal_add_css(drupal_get_path('module', 'graphstat') .'/graphstat.css'); |
|
|
} |
|
|
|
|
|
/** |
|
| 26 |
* Implementation of hook_menu(). |
* Implementation of hook_menu(). |
| 27 |
*/ |
*/ |
| 28 |
function graphstat_menu() { |
function graphstat_menu() { |
| 29 |
$items = array(); |
$items = array(); |
| 30 |
$items['admin/reports/graphs'] = array( |
$items['admin/reports/graphs'] = array( |
| 31 |
'title' => 'Graphs', |
'title' => 'Graphs', |
| 32 |
'description' => 'Displays graphs based on site statistics.', |
'description' => 'Displays graphs with statistical data.', |
| 33 |
'access callback' => 'user_access', |
'access callback' => 'user_access', |
| 34 |
'access arguments' => array('access graphs'), |
'access arguments' => array('access graphs'), |
| 35 |
'page callback' => 'graphstat_graphs', |
'page callback' => 'graphstat_graphs', |
| 39 |
$graphs = module_invoke_all('graphstat'); |
$graphs = module_invoke_all('graphstat'); |
| 40 |
foreach ($graphs as $group => $graph) { |
foreach ($graphs as $group => $graph) { |
| 41 |
$items['admin/reports/graphs/'. strtr($group, ' ', '_')] = array( |
$items['admin/reports/graphs/'. strtr($group, ' ', '_')] = array( |
| 42 |
'title' => check_plain(isset($graph['title']) ? $graph['title'] : drupal_ucfirst($group)), |
'title' => check_plain(isset($graph['#title']) ? $graph['#title'] : drupal_ucfirst($group)), |
| 43 |
'access callback' => 'user_access', |
'access callback' => 'user_access', |
| 44 |
'access arguments' => array('access graphs'), |
'access arguments' => array('access graphs'), |
| 45 |
'page callback' => 'graphstat_graphs', |
'page callback' => 'graphstat_graphs', |
| 52 |
'access arguments' => array('access graphs'), |
'access arguments' => array('access graphs'), |
| 53 |
'page callback' => 'graphstat_render_graph', |
'page callback' => 'graphstat_render_graph', |
| 54 |
'page arguments' => array($group, $key), |
'page arguments' => array($group, $key), |
| 55 |
|
'file' => 'graphstat_render.inc', |
| 56 |
'type' => MENU_CALLBACK |
'type' => MENU_CALLBACK |
| 57 |
); |
); |
| 58 |
} |
} |
| 62 |
} |
} |
| 63 |
|
|
| 64 |
/** |
/** |
| 65 |
|
* Implementation of hook_cron(). |
| 66 |
|
*/ |
| 67 |
|
function graphstat_cron() { |
| 68 |
|
_graphstat_invoke_modules('cron'); |
| 69 |
|
} |
| 70 |
|
|
| 71 |
|
/** |
| 72 |
|
* Implementation of hook_graphstat(). |
| 73 |
|
*/ |
| 74 |
|
function graphstat_graphstat() { |
| 75 |
|
return _graphstat_invoke_modules('graphstat'); |
| 76 |
|
} |
| 77 |
|
|
| 78 |
|
/** |
| 79 |
|
* Implementation of hook_graphstat_data(). |
| 80 |
|
*/ |
| 81 |
|
function graphstat_graphstat_data() { |
| 82 |
|
return _graphstat_invoke_modules('graphstat_data'); |
| 83 |
|
} |
| 84 |
|
|
| 85 |
|
/** |
| 86 |
* Function graphstat_graphs(). |
* Function graphstat_graphs(). |
| 87 |
* (menu callback: page containing multiple graphs) |
* (menu callback: page containing multiple graphs) |
| 88 |
*/ |
*/ |
| 89 |
function graphstat_graphs($group = NULL) { |
function graphstat_graphs($group = NULL) { |
| 90 |
$graphs = module_invoke_all('graphstat'); |
drupal_add_css(drupal_get_path('module', 'graphstat') .'/graphstat.css'); |
| 91 |
|
|
| 92 |
|
$graphs = module_invoke_all('graphstat'); |
| 93 |
if ($group && isset($graphs[$group])) { |
if ($group && isset($graphs[$group])) { |
| 94 |
$graphs = $graphs[$group]; |
$graphs = $graphs[$group]; |
| 95 |
$output = isset($graphs['pre']) ? $graphs['pre'] : ''; |
$output = isset($graphs['#prefix']) ? $graphs['#prefix'] : ''; |
| 96 |
// If this graph group defines a filter callback then add the filter form |
// If this graph group defines a filter callback then add the filter form |
| 97 |
if (isset($graphs['filter'])) { |
if (isset($graphs['#filter'])) { |
| 98 |
$options = $graphs['filter']['options']; |
$options = $graphs['#filter']['#options']; |
| 99 |
$filter = isset($_SESSION['graphstat_filter'][$group]) ? $_SESSION['graphstat_filter'][$group] : NULL; |
$filter = isset($_SESSION['graphstat_filter'][$group]) ? $_SESSION['graphstat_filter'][$group] : NULL; |
| 100 |
if (isset($_POST['graphstat_filter_select'])) { |
if (isset($_POST['graphstat_filter_select'])) { |
| 101 |
$filter = $_POST['graphstat_filter_select']; |
$filter = $_POST['graphstat_filter_select']; |
| 104 |
$output .= drupal_get_form('graphstat_filter_form', $options, $filter); |
$output .= drupal_get_form('graphstat_filter_form', $options, $filter); |
| 105 |
} |
} |
| 106 |
// Loop over the graphs for this graph group |
// Loop over the graphs for this graph group |
| 107 |
foreach ($graphs as $key => $graph) { |
foreach (element_children($graphs) as $key) { |
| 108 |
if (is_array($graph) && isset($graph['data']) && is_array($graph['data'])) { |
$graph = $graphs[$key]; |
| 109 |
$url = url('graphstat/'. strtr($group .'_'. $key, ' ', '_'), array('absolute' => TRUE)); |
$url = url('graphstat/'. strtr($group .'_'. $key, ' ', '_'), array('absolute' => TRUE)); |
| 110 |
$title = isset($graph['title']) ? $graph['title'] : drupal_ucfirst($group); |
$title = isset($graph['#title']) ? $graph['#title'] : drupal_ucfirst($group); |
| 111 |
$description = isset($graph['description']) ? $graph['description'] : ''; |
$description = isset($graph['#description']) ? $graph['#description'] : ''; |
| 112 |
$output .= theme('graphstat_graph', $url, $title, $description); |
$output .= theme('graphstat_graph', $url, $title, $description); |
|
} |
|
| 113 |
} |
} |
| 114 |
$output .= isset($graphs['post']) ? $graphs['post'] : ''; |
$output .= isset($graphs['#suffix']) ? $graphs['#suffix'] : ''; |
| 115 |
$output .= drupal_get_form('graphstat_size_form'); |
$output .= drupal_get_form('graphstat_size_form'); |
| 116 |
} |
} |
| 117 |
else { |
else { |
| 214 |
} |
} |
| 215 |
|
|
| 216 |
/** |
/** |
| 217 |
* Function graphstat_render_graph(). |
* Function _graphstat_invoke_modules(). |
| 218 |
* (function to render the actual graph .png images) |
* (invoke hook in internal graphstat modules) |
| 219 |
*/ |
*/ |
| 220 |
function graphstat_render_graph($group, $key) { |
function _graphstat_invoke_modules($hook = 'graphstat') { |
| 221 |
global $user; |
$return = array(); |
| 222 |
drupal_set_header('Content-type: image/png'); |
// Scan graphstat/modules/ folder for (internal) modules |
| 223 |
|
$files = file_scan_directory(drupal_get_path('module', 'graphstat') .'/modules', '\graphstat.[a-z]+.inc$'); |
| 224 |
$filter = isset($_SESSION['graphstat_filter'][$group]) ? $_SESSION['graphstat_filter'][$group] : NULL; |
foreach ($files as $file) { |
| 225 |
$cache = cache_get('graphstat:'. $user->uid); |
list( , $module) = explode('.', $file->name); |
| 226 |
$width = $cache ? $cache->data['width'] : 500; |
// If the corresponding module is installed, invoke graphstat_$module_graphstat() |
| 227 |
$height = $cache ? $cache->data['height'] : 300; |
if (module_exists($module)) { |
| 228 |
$time = format_date(time(), 'custom', 'H'); |
require_once(drupal_get_path('module', 'graphstat') .'/modules/'. $file->name .'.inc'); |
| 229 |
|
$function = str_replace('.', '_', $file->name) .'_'. $hook; |
|
$cid = 'graphstat:'. md5($group . $key . $filter) .':'. $width .':'. $time; |
|
|
if ($cache = cache_get($cid)) { |
|
|
$image = $cache->data; |
|
|
} |
|
|
else { |
|
|
// Cache Miss: we need to generate the graphs from scratch |
|
|
$graphs = module_invoke_all('graphstat'); |
|
|
$graphs = $graphs[$group]; |
|
|
// If a filter is defined then invoke the callback function |
|
|
// to fetch the filtered graph data |
|
|
if (isset($graphs['filter'])) { |
|
|
$function = $graphs['filter']['callback']; |
|
| 230 |
if (function_exists($function)) { |
if (function_exists($function)) { |
| 231 |
$function($graphs, $filter); |
$return = array_merge_recursive($return, $function()); |
|
} |
|
|
} |
|
|
$graph = $graphs[$key]; |
|
|
// If no graph data available then render a placeholder image |
|
|
if (empty($graph['data'])) { |
|
|
if (function_exists('imagepng')) { |
|
|
$img = imagecreate($width, $height); |
|
|
imagefill($img, 0, 0, imagecolorallocate($img, 240, 240, 240)); |
|
|
imageline($img, 0, 0, $width, $height, imagecolorallocate($img, 128, 128, 128)); |
|
|
imageline($img, 0, $height, $width, 0, imagecolorallocate($img, 128, 128, 128)); |
|
|
imagepng($img); |
|
|
imagedestroy($img); |
|
| 232 |
} |
} |
|
exit(); |
|
|
} |
|
|
// Include the PHPLOT library |
|
|
require_once(drupal_get_path('module', 'graphstat') .'/phplot.php'); |
|
|
// Create instance of PHPLOT and configure graph paramters |
|
|
$plot =& new PHPlot($width, $height); |
|
|
$graph['type'] = isset($graph['type']) ? $graph['type'] : 'linepoints'; |
|
|
$plot->SetPlotType($graph['type']); |
|
|
$plot->SetTitle(isset($graph['title']) ? $graph['title'] : ''); |
|
|
$plot->SetXTitle(isset($graph['xlabel']) ? $graph['xlabel'] : ''); |
|
|
$plot->SetXTickLabelPos('none'); |
|
|
$plot->SetYTitle(isset($graph['ylabel']) ? $graph['ylabel'] : ''); |
|
|
$plot->SetXLabelAngle(90); |
|
|
if (isset($graph['legend'])) { |
|
|
$plot->SetLegend($graph['legend']); |
|
|
} |
|
|
// Push graph data into the plot renderer |
|
|
$data = array(); |
|
|
if (is_array(current($graph['data']))) { |
|
|
$data = $graph['data']; |
|
| 233 |
} |
} |
|
else { |
|
|
foreach ($graph['data'] as $x => $y) { |
|
|
$data[] = array($x, $y); |
|
|
} |
|
|
} |
|
|
$plot->SetDataValues($data); |
|
|
// Draw the graph into a buffer $image |
|
|
ob_start(); |
|
|
$plot->DrawGraph(); |
|
|
$image = ob_get_contents(); |
|
|
ob_end_clean(); |
|
|
// Put $image into the cache |
|
|
cache_set($cid, $image, 'cache', CACHE_TEMPORARY); |
|
| 234 |
} |
} |
| 235 |
|
|
| 236 |
// Print out the image and make sure to exit immediatly |
return $return; |
|
print $image; |
|
|
exit(); |
|
| 237 |
} |
} |