diff --git a/classes/headers/RollupHeader.php b/classes/headers/RollupHeader.php index a00653b4..2fb328a4 100644 --- a/classes/headers/RollupHeader.php +++ b/classes/headers/RollupHeader.php @@ -5,6 +5,10 @@ class RollupHeader extends HeaderBase { 'required'=>true, 'type'=>'object', 'default'=>array() + ), + 'dataset'=>array( + 'required'=>false, + 'default'=>0 ) ); @@ -12,48 +16,68 @@ public static function init($params, &$report) { //make sure at least 1 column is defined if(empty($params['columns'])) throw new Exception("Rollup header needs at least 1 column defined"); - //store all the rollup rows in the report if(!isset($report->options['Rollup'])) $report->options['Rollup'] = array(); - $report->options['Rollup'][] = $params; + + // If more than one dataset is defined, add the rollup header multiple times + if(is_array($params['dataset'])) { + $new_params = $params; + foreach($params['dataset'] as $dataset) { + $new_params['dataset'] = $dataset; + $report->options['Rollup'][] = $new_params; + } + } + // Otherwise, just add one rollup header + else { + $report->options['Rollup'][] = $params; + } } - public static function beforeRender(&$report) { - //cache for Twig parameters for each column + public static function beforeRender(&$report) { + //cache for Twig parameters for each dataset/column $twig_params = array(); - foreach($report->options['Rows'] as $row) { - foreach($row['values'] as $value) { - if(!isset($twig_params[$value->key])) $twig_params[$value->key] = array('values'=>array()); - $twig_params[$value->key]['values'][] = $value->getValue(); + + // First get all the values + foreach($report->options['Rollup'] as $rollup) { + // If we already got twig parameters for this dataset, skip it + if(isset($twig_params[$rollup['dataset']])) continue; + $twig_params[$rollup['dataset']] = array(); + foreach($report->options['DataSets'][$rollup['dataset']]['rows'] as $row) { + foreach($row['values'] as $value) { + if(!isset($twig_params[$rollup['dataset']][$value->key])) $twig_params[$rollup['dataset']][$value->key] = array('values'=>array()); + $twig_params[$rollup['dataset']][$value->key]['values'][] = $value->getValue(); + } } } - foreach($twig_params as $column=>&$params) { - //get non-null values and sort them - $real_values = array_filter($params['values'],function($a) {if($a === null || $a==='') return false; return true; }); - sort($real_values); - - $params['sum'] = array_sum($real_values); - $params['count'] = count($real_values); - $params['mean'] = $params['average'] = $params['sum'] / $params['count']; - $params['median'] = ($params['count']%2)? ($real_values[$params['count']/2] + $real_values[$params['count']/2+1])/2 : $real_values[ceil($params['count']/2)]; - $params['min'] = $real_values[0]; - $params['max'] = $real_values[$params['count']-1]; - - $devs = array(); - foreach($real_values as $v) $devs[] = pow($v - $params['mean'], 2); - $params['stdev'] = sqrt(array_sum($devs) / (count($devs) - 1)); + // Then, calculate other statistical properties + foreach($twig_params as $dataset=>&$tp) { + foreach($tp as $column=>&$params) { + //get non-null values and sort them + $real_values = array_filter($params['values'],function($a) {if($a === null || $a==='') return false; return true; }); + sort($real_values); + + $params['sum'] = array_sum($real_values); + $params['count'] = count($real_values); + $params['mean'] = $params['average'] = $params['sum'] / $params['count']; + $params['median'] = ($params['count']%2)? ($real_values[$params['count']/2] + $real_values[$params['count']/2+1])/2 : $real_values[ceil($params['count']/2)]; + $params['min'] = $real_values[0]; + $params['max'] = $real_values[$params['count']-1]; + + $devs = array(); + foreach($real_values as $v) $devs[] = pow($v - $params['mean'], 2); + $params['stdev'] = sqrt(array_sum($devs) / (count($devs) - 1)); + } } - - if(!isset($report->options['FooterRows'])) $report->options['FooterRows'] = array(); //render each rollup row foreach($report->options['Rollup'] as $rollup) { + if(!isset($report->options['DataSets'][$rollup['dataset']]['footer'])) $report->options['DataSets'][$rollup['dataset']]['footer'] = array(); $columns = $rollup['columns']; $row = array( 'values'=>array(), 'rollup'=>true ); - foreach($twig_params as $column=>$p) { + foreach($twig_params[$rollup['dataset']] as $column=>$p) { if(isset($columns[$column])) { $row['values'][] = new ReportValue(-1,$column,PhpReports::renderString($columns[$column],$p)); } @@ -61,7 +85,7 @@ public static function beforeRender(&$report) { $row['values'][] = new ReportValue(-1,$column,null); } } - $report->options['FooterRows'][] = $row; + $report->options['DataSets'][$rollup['dataset']]['footer'][] = $row; } } } diff --git a/sample_reports/php/timezones_multiple.php b/sample_reports/php/timezones_multiple.php index b83758fb..534d30ab 100644 --- a/sample_reports/php/timezones_multiple.php +++ b/sample_reports/php/timezones_multiple.php @@ -1,6 +1,5 @@ 'AVERAGE', - 'Offset'=>$stats['mean'] - ); - $datasets[] = $dataset; }