ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
23  protected $datehandlerFile;
24  protected $datehandlerTick;
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),
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_intra = $this->timeresconfig->getValue('dateformat-intra');
187  $axislabelint = $this->timeresconfig->getValue('axislabelint');
188 
189  $axis = array();
190  $axispos = array();
191  $xentries = count($this->results);
192  $lastslot = 0;
193  $i = 0;
194 
195  foreach ($this->results as $slot => $res) {
196  // check if there should be an axis here...
197  if ($slot % $axislabelint == 0) {
198  $axis[] = $this->datehandlerTick->prettyDateSlot($slot, $slotsize, $dateformat_intra);
199  $axispos[] = (($i) / ($xentries - 1));
200  }
201  $lastslot = $slot;
202  $i++;
203  }
204 
205  $axis[] = $this->datehandlerTick->prettyDateSlot($lastslot + 1, $slotsize, $dateformat_intra);
206 
207  return array('axis' => $axis, 'axispos' => $axispos);
208  }
209 
210  /*
211  * Walk through dataset to get percent values from max into dataset[].
212  */
213  public function getPercentValues()
214  {
215  $i = 0;
216  $dataset = array();
217  foreach ($this->results as $slot => $res) {
218  if (array_key_exists($this->delimiter, $res)) {
219  if ($res[$this->delimiter] === null) {
220  $dataset[] = -1;
221  } else {
222  $dataset[] = number_format(100 * $res[$this->delimiter] / $this->max, 2);
223  }
224  } else {
225  $dataset[] = '0';
226  }
227  $i++;
228  }
229 
230  return $dataset;
231  }
232 
233  public function getDelimiterPresentation()
234  {
236  $t = new SimpleSAML_XHTML_Template($config, 'statistics:statistics.tpl.php');
237 
238  $availdelimiters = $this->availDelimiters();
239 
240  // create a delimiter presentation filter for this rule...
241  if ($this->ruleconfig->hasValue('fieldPresentation')) {
242  $fieldpresConfig = $this->ruleconfig->getConfigItem('fieldPresentation');
243  $classname = SimpleSAML\Module::resolveClass(
244  $fieldpresConfig->getValue('class'),
245  'Statistics_FieldPresentation'
246  );
247  if (!class_exists($classname)) {
248  throw new Exception('Could not find field presentation plugin ['.$classname.']: No class found');
249  }
250  $presentationHandler = new $classname($availdelimiters, $fieldpresConfig->getValue('config'), $t);
251 
252  return $presentationHandler->getPresentation();
253  }
254 
255  return array();
256  }
257 
258  public function getDelimiterPresentationPie()
259  {
260  $topdelimiters = $this->getTopDelimiters();
261  $delimiterPresentation = $this->getDelimiterPresentation();
262 
263  $pieaxis = array();
264  foreach ($topdelimiters as $key) {
265  $keyName = $key;
266  if (array_key_exists($key, $delimiterPresentation)) {
267  $keyName = $delimiterPresentation[$key];
268  }
269  $pieaxis[] = $keyName;
270  }
271  $pieaxis[] = 'Others';
272  return $pieaxis;
273  }
274 
275  public function loadData()
276  {
277  $statdir = $this->statconfig->getValue('statdir');
278  $resarray = array();
279  $rules = SimpleSAML\Utils\Arrays::arrayize($this->ruleid);
280  foreach ($rules as $rule) {
281  // Get file and extract results.
282  $resultFileName = $statdir.'/'.$rule.'-'.$this->timeres.'-'.$this->fileslot.'.stat';
283  if (!file_exists($resultFileName)) {
284  throw new Exception('Aggregated statitics file ['.$resultFileName.'] not found.');
285  }
286  if (!is_readable($resultFileName)) {
287  throw new Exception('Could not read statitics file ['.$resultFileName.']. Bad file permissions?');
288  }
289  $resultfile = file_get_contents($resultFileName);
290  $newres = unserialize($resultfile);
291  if (empty($newres)) {
292  throw new Exception('Aggregated statistics in file ['.$resultFileName.'] was empty.');
293  }
294  $resarray[] = $newres;
295  }
296 
297  $combined = $resarray[0];
298  $count = count($resarray);
299  if ($count > 1) {
300  for ($i = 1; $i < $count; $i++) {
301  $combined = $this->combine($combined, $resarray[$i]);
302  }
303  }
304  $this->results = $combined;
305  }
306 
307 }
308 
$piedata
Definition: showstats.php:83
$config
Definition: bootstrap.php:15
static roof($max)
Takes a input value, and generates a value that suits better as a max value on the Y-axis...
static arrayize($data, $index=0)
Put a non-array variable into an array.
Definition: Arrays.php:24
foreach($_POST as $key=> $value) $res
$rule
Definition: showstats.php:43
__construct($statconfig, $ruleconfig, $ruleid, $timeres, $fileslot)
Constructor.
Definition: StatDataset.php:30
$i
Definition: disco.tpl.php:19
static resolveClass($id, $type, $subclass=null)
Resolve module class.
Definition: Module.php:169
$axis
Definition: showstats.php:88
$key
Definition: croninfo.php:18
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
setDelimiter($delimiter='_')
Definition: StatDataset.php:67