ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
StatDataset.php
Go to the documentation of this file.
1<?php
2
3
9{
10 protected $statconfig;
11 protected $ruleconfig;
12 protected $timeresconfig;
13 protected $ruleid;
14
15 protected $fileslot;
16 protected $timeres;
17
18 protected $delimiter;
19 protected $results;
20 protected $summary;
21 protected $max;
22
25
26
31 {
32 assert('$statconfig instanceof SimpleSAML_Configuration');
33 assert('$ruleconfig instanceof SimpleSAML_Configuration');
34 $this->statconfig = $statconfig;
35 $this->ruleconfig = $ruleconfig;
36
37 $timeresconfigs = $statconfig->getConfigItem('timeres');
38 $this->timeresconfig = $timeresconfigs->getConfigItem($timeres);
39
40 $this->ruleid = $ruleid;
41 $this->fileslot = $fileslot;
42 $this->timeres = $timeres;
43
44 $this->delimiter = '_';
45 $this->max = 0;
46
47 $this->datehandlerTick = new sspmod_statistics_DateHandler($this->statconfig->getValue('offset', 0));
48 if ($this->timeresconfig->getValue('customDateHandler', 'default') === 'month') {
49 $this->datehandlerFile = new sspmod_statistics_DateHandlerMonth(0);
50 } else {
51 $this->datehandlerFile = $this->datehandlerTick;
52 }
53
54 $this->loadData();
55 }
56
57 public function getFileSlot()
58 {
59 return $this->fileslot;
60 }
61
62 public function getTimeRes()
63 {
64 return $this->timeres;
65 }
66
67 public function setDelimiter($delimiter = '_')
68 {
69 if (empty($delimiter)) {
70 $delimiter = '_';
71 }
72 $this->delimiter = $delimiter;
73 }
74
75 public function getDelimiter()
76 {
77 if ($this->delimiter === '_') {
78 return null;
79 }
80 return $this->delimiter;
81 }
82
83 public function calculateMax()
84 {
85 $maxvalue = 0;
86 foreach ($this->results as $slot => &$res) {
87 if (!array_key_exists($this->delimiter, $res)) {
89 }
90 $maxvalue = max($res[$this->delimiter], $maxvalue);
91 }
92 $this->max = sspmod_statistics_Graph_GoogleCharts::roof($maxvalue);
93 }
94
95 public function getDebugData()
96 {
97 $debugdata = array();
98
99 $slotsize = $this->timeresconfig->getValue('slot');
100 $dateformat_intra = $this->timeresconfig->getValue('dateformat-intra');
101
102 foreach ($this->results as $slot => &$res) {
103 $debugdata[$slot] = array(
104 $this->datehandlerTick->prettyDateSlot($slot, $slotsize, $dateformat_intra),
105 $res[$this->delimiter]
106 );
107 }
108 return $debugdata;
109 }
110
111 public function aggregateSummary()
112 {
113 // aggregate summary table from dataset. To be used in the table view
114 $this->summary = array();
115 foreach ($this->results as $slot => $res) {
116 foreach ($res as $key => $value) {
117 if (array_key_exists($key, $this->summary)) {
118 $this->summary[$key] += $value;
119 } else {
120 $this->summary[$key] = $value;
121 }
122 }
123 }
124 asort($this->summary);
125 $this->summary = array_reverse($this->summary, true);
126 }
127
128 public function getTopDelimiters()
129 {
130 // create a list of delimiter keys that has the highest total summary in this period
131 $topdelimiters = array();
132 $maxdelimiters = 4;
133 $i = 0;
134 foreach ($this->summary as $key => $value) {
135 if ($key !== '_') {
136 $topdelimiters[] = $key;
137 }
138 if ($i++ >= $maxdelimiters) {
139 break;
140 }
141 }
142 return $topdelimiters;
143 }
144
145 public function availDelimiters()
146 {
147 $availDelimiters = array();
148 foreach ($this->summary as $key => $value) {
149 $availDelimiters[$key] = 1;
150 }
151 return array_keys($availDelimiters);
152 }
153
154 public function getPieData()
155 {
156 $piedata = array();
157 $sum = 0;
158 $topdelimiters = $this->getTopDelimiters();
159
160 foreach ($topdelimiters as $td) {
161 $sum += $this->summary[$td];
162 $piedata[] = number_format(100 * $this->summary[$td] / $this->summary['_'], 2);
163 }
164 $piedata[] = number_format(100 - 100 * ($sum / $this->summary['_']), 2);
165 return $piedata;
166 }
167
168 public function getMax()
169 {
170 return $this->max;
171 }
172
173 public function getSummary()
174 {
175 return $this->summary;
176 }
177
178 public function getResults()
179 {
180 return $this->results;
181 }
182
183 public function getAxis()
184 {
185 $slotsize = $this->timeresconfig->getValue('slot');
186 $dateformat_period = $this->timeresconfig->getValue('dateformat-period');
187 $dateformat_intra = $this->timeresconfig->getValue('dateformat-intra');
188 $axislabelint = $this->timeresconfig->getValue('axislabelint');
189
190 $axis = array();
191 $axispos = array();
192 $xentries = count($this->results);
193 $lastslot = 0;
194 $i = 0;
195
196 foreach ($this->results as $slot => $res) {
197 // check if there should be an axis here...
198 if ($slot % $axislabelint == 0) {
199 $axis[] = $this->datehandlerTick->prettyDateSlot($slot, $slotsize, $dateformat_intra);
200 $axispos[] = (($i) / ($xentries - 1));
201 }
202 $lastslot = $slot;
203 $i++;
204 }
205
206 $axis[] = $this->datehandlerTick->prettyDateSlot($lastslot + 1, $slotsize, $dateformat_intra);
207
208 return array('axis' => $axis, 'axispos' => $axispos);
209 }
210
211 /*
212 * Walk through dataset to get percent values from max into dataset[].
213 */
214 public function getPercentValues()
215 {
216 $i = 0;
217 $dataset = array();
218 foreach ($this->results as $slot => $res) {
219 if (array_key_exists($this->delimiter, $res)) {
220 if ($res[$this->delimiter] === null) {
221 $dataset[] = -1;
222 } else {
223 $dataset[] = number_format(100 * $res[$this->delimiter] / $this->max, 2);
224 }
225 } else {
226 $dataset[] = '0';
227 }
228 $i++;
229 }
230
231 return $dataset;
232 }
233
234 public function getDelimiterPresentation()
235 {
237 $t = new SimpleSAML_XHTML_Template($config, 'statistics:statistics.tpl.php');
238
239 $availdelimiters = $this->availDelimiters();
240
241 // create a delimiter presentation filter for this rule...
242 if ($this->ruleconfig->hasValue('fieldPresentation')) {
243 $fieldpresConfig = $this->ruleconfig->getConfigItem('fieldPresentation');
245 $fieldpresConfig->getValue('class'),
246 'Statistics_FieldPresentation'
247 );
248 if (!class_exists($classname)) {
249 throw new Exception('Could not find field presentation plugin ['.$classname.']: No class found');
250 }
251 $presentationHandler = new $classname($availdelimiters, $fieldpresConfig->getValue('config'), $t);
252
253 return $presentationHandler->getPresentation();
254 }
255
256 return array();
257 }
258
260 {
261 $topdelimiters = $this->getTopDelimiters();
262 $delimiterPresentation = $this->getDelimiterPresentation();
263
264 $pieaxis = array();
265 foreach ($topdelimiters as $key) {
266 $keyName = $key;
267 if (array_key_exists($key, $delimiterPresentation)) {
268 $keyName = $delimiterPresentation[$key];
269 }
270 $pieaxis[] = $keyName;
271 }
272 $pieaxis[] = 'Others';
273 return $pieaxis;
274 }
275
276 public function loadData()
277 {
278 $statdir = $this->statconfig->getValue('statdir');
279 $resarray = array();
280 $rules = SimpleSAML\Utils\Arrays::arrayize($this->ruleid);
281 foreach ($rules as $rule) {
282 // Get file and extract results.
283 $resultFileName = $statdir.'/'.$rule.'-'.$this->timeres.'-'.$this->fileslot.'.stat';
284 if (!file_exists($resultFileName)) {
285 throw new Exception('Aggregated statitics file ['.$resultFileName.'] not found.');
286 }
287 if (!is_readable($resultFileName)) {
288 throw new Exception('Could not read statitics file ['.$resultFileName.']. Bad file permissions?');
289 }
290 $resultfile = file_get_contents($resultFileName);
291 $newres = unserialize($resultfile);
292 if (empty($newres)) {
293 throw new Exception('Aggregated statistics in file ['.$resultFileName.'] was empty.');
294 }
295 $resarray[] = $newres;
296 }
297
298 $combined = $resarray[0];
299 $count = count($resarray);
300 if ($count > 1) {
301 for ($i = 1; $i < $count; $i++) {
302 $combined = $this->combine($combined, $resarray[$i]);
303 }
304 }
305 $this->results = $combined;
306 }
307
308}
309
An exception for terminatinating execution or to throw for unit testing.
static resolveClass($id, $type, $subclass=null)
Resolve module class.
Definition: Module.php:252
static arrayize($data, $index=0)
Put a non-array variable into an array.
Definition: Arrays.php:24
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
static roof($max)
Takes a input value, and generates a value that suits better as a max value on the Y-axis.
__construct($statconfig, $ruleconfig, $ruleid, $timeres, $fileslot)
Constructor.
Definition: StatDataset.php:30
setDelimiter($delimiter='_')
Definition: StatDataset.php:67
$key
Definition: croninfo.php:18
$i
Definition: disco.tpl.php:19
foreach($_POST as $key=> $value) $res
$dataset
Definition: showstats.php:45
$axis
Definition: showstats.php:64
$piedata
Definition: showstats.php:59
$rule
Definition: showstats.php:43