ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
Database.php
Go to the documentation of this file.
1 <?php
30 if (!defined('PHPEXCEL_ROOT')) {
34  define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
35  require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
36 }
37 
38 
47 
48 
49  private static function __fieldExtract($database,$field) {
50  $field = strtoupper(PHPExcel_Calculation_Functions::flattenSingleValue($field));
51  $fieldNames = array_map('strtoupper',array_shift($database));
52 
53  if (is_numeric($field)) {
54  $keys = array_keys($fieldNames);
55  return $keys[$field-1];
56  }
57  $key = array_search($field,$fieldNames);
58  return ($key) ? $key : null;
59  }
60 
61  private static function __filter($database,$criteria) {
62  $fieldNames = array_shift($database);
63  $criteriaNames = array_shift($criteria);
64 
65  // Convert the criteria into a set of AND/OR conditions with [:placeholders]
66  $testConditions = $testValues = array();
67  $testConditionsCount = 0;
68  foreach($criteriaNames as $key => $criteriaName) {
69  $testCondition = array();
70  $testConditionCount = 0;
71  foreach($criteria as $row => $criterion) {
72  if ($criterion[$key] > '') {
73  $testCondition[] = '[:'.$criteriaName.']'.PHPExcel_Calculation_Functions::_ifCondition($criterion[$key]);
74  $testConditionCount++;
75  }
76  }
77  if ($testConditionCount > 1) {
78  $testConditions[] = 'OR('.implode(',',$testCondition).')';
79  $testConditionsCount++;
80  } elseif($testConditionCount == 1) {
81  $testConditions[] = $testCondition[0];
82  $testConditionsCount++;
83  }
84  }
85  if ($testConditionsCount > 1) {
86  $testConditionSet = 'AND('.implode(',',$testConditions).')';
87  } elseif($testConditionsCount == 1) {
88  $testConditionSet = $testConditions[0];
89  }
90 
91  // Loop through each row of the database
92  foreach($database as $dataRow => $dataValues) {
93  // Substitute actual values from the database row for our [:placeholders]
94  $testConditionList = $testConditionSet;
95  foreach($criteriaNames as $key => $criteriaName) {
96  $k = array_search($criteriaName,$fieldNames);
97  if (isset($dataValues[$k])) {
98  $dataValue = $dataValues[$k];
99  $dataValue = (is_string($dataValue)) ? PHPExcel_Calculation::_wrapResult(strtoupper($dataValue)) : $dataValue;
100  $testConditionList = str_replace('[:'.$criteriaName.']',$dataValue,$testConditionList);
101  }
102  }
103  // evaluate the criteria against the row data
104  $result = PHPExcel_Calculation::getInstance()->_calculateFormulaValue('='.$testConditionList);
105  // If the row failed to meet the criteria, remove it from the database
106  if (!$result) {
107  unset($database[$dataRow]);
108  }
109  }
110 
111  return $database;
112  }
113 
114 
119  public static function DAVERAGE($database,$field,$criteria) {
120  $field = self::__fieldExtract($database,$field);
121  if (is_null($field)) {
122  return NULL;
123  }
124 
125  // reduce the database to a set of rows that match all the criteria
126  $database = self::__filter($database,$criteria);
127  // extract an array of values for the requested column
128  $colData = array();
129  foreach($database as $row) {
130  $colData[] = $row[$field];
131  }
132 
133  // Return
135  } // function DAVERAGE()
136 
141  public static function DCOUNT($database,$field,$criteria) {
142  $field = self::__fieldExtract($database,$field);
143  if (is_null($field)) {
144  return NULL;
145  }
146 
147  // reduce the database to a set of rows that match all the criteria
148  $database = self::__filter($database,$criteria);
149  // extract an array of values for the requested column
150  $colData = array();
151  foreach($database as $row) {
152  $colData[] = $row[$field];
153  }
154 
155  // Return
157  } // function DCOUNT()
158 
163  public static function DCOUNTA($database,$field,$criteria) {
164  $field = self::__fieldExtract($database,$field);
165  if (is_null($field)) {
166  return NULL;
167  }
168 
169  // reduce the database to a set of rows that match all the criteria
170  $database = self::__filter($database,$criteria);
171  // extract an array of values for the requested column
172  $colData = array();
173  foreach($database as $row) {
174  $colData[] = $row[$field];
175  }
176 
177  // Return
179  } // function DCOUNTA()
180 
185  public static function DGET($database,$field,$criteria) {
186  $field = self::__fieldExtract($database,$field);
187  if (is_null($field)) {
188  return NULL;
189  }
190 
191  // reduce the database to a set of rows that match all the criteria
192  $database = self::__filter($database,$criteria);
193  // extract an array of values for the requested column
194  $colData = array();
195  foreach($database as $row) {
196  $colData[] = $row[$field];
197  }
198 
199  // Return
200  if (count($colData) > 1) {
202  }
203 
204  return $colData[0];
205  } // function DGET()
206 
211  public static function DMAX($database,$field,$criteria) {
212  $field = self::__fieldExtract($database,$field);
213  if (is_null($field)) {
214  return NULL;
215  }
216 
217  // reduce the database to a set of rows that match all the criteria
218  $database = self::__filter($database,$criteria);
219  // extract an array of values for the requested column
220  $colData = array();
221  foreach($database as $row) {
222  $colData[] = $row[$field];
223  }
224 
225  // Return
226  return PHPExcel_Calculation_Statistical::MAX($colData);
227  } // function DMAX()
228 
233  public static function DMIN($database,$field,$criteria) {
234  $field = self::__fieldExtract($database,$field);
235  if (is_null($field)) {
236  return NULL;
237  }
238 
239  // reduce the database to a set of rows that match all the criteria
240  $database = self::__filter($database,$criteria);
241  // extract an array of values for the requested column
242  $colData = array();
243  foreach($database as $row) {
244  $colData[] = $row[$field];
245  }
246 
247  // Return
248  return PHPExcel_Calculation_Statistical::MIN($colData);
249  } // function DMIN()
250 
255  public static function DPRODUCT($database,$field,$criteria) {
256  $field = self::__fieldExtract($database,$field);
257  if (is_null($field)) {
258  return NULL;
259  }
260 
261  // reduce the database to a set of rows that match all the criteria
262  $database = self::__filter($database,$criteria);
263  // extract an array of values for the requested column
264  $colData = array();
265  foreach($database as $row) {
266  $colData[] = $row[$field];
267  }
268 
269  // Return
271  } // function DPRODUCT()
272 
277  public static function DSTDEV($database,$field,$criteria) {
278  $field = self::__fieldExtract($database,$field);
279  if (is_null($field)) {
280  return NULL;
281  }
282 
283  // reduce the database to a set of rows that match all the criteria
284  $database = self::__filter($database,$criteria);
285  // extract an array of values for the requested column
286  $colData = array();
287  foreach($database as $row) {
288  $colData[] = $row[$field];
289  }
290 
291  // Return
293  } // function DSTDEV()
294 
299  public static function DSTDEVP($database,$field,$criteria) {
300  $field = self::__fieldExtract($database,$field);
301  if (is_null($field)) {
302  return NULL;
303  }
304 
305  // reduce the database to a set of rows that match all the criteria
306  $database = self::__filter($database,$criteria);
307  // extract an array of values for the requested column
308  $colData = array();
309  foreach($database as $row) {
310  $colData[] = $row[$field];
311  }
312 
313  // Return
315  } // function DSTDEVP()
316 
321  public static function DSUM($database,$field,$criteria) {
322  $field = self::__fieldExtract($database,$field);
323  if (is_null($field)) {
324  return NULL;
325  }
326 
327  // reduce the database to a set of rows that match all the criteria
328  $database = self::__filter($database,$criteria);
329  // extract an array of values for the requested column
330  $colData = array();
331  foreach($database as $row) {
332  $colData[] = $row[$field];
333  }
334 
335  // Return
336  return PHPExcel_Calculation_MathTrig::SUM($colData);
337  } // function DSUM()
338 
343  public static function DVAR($database,$field,$criteria) {
344  $field = self::__fieldExtract($database,$field);
345  if (is_null($field)) {
346  return NULL;
347  }
348 
349  // reduce the database to a set of rows that match all the criteria
350  $database = self::__filter($database,$criteria);
351  // extract an array of values for the requested column
352  $colData = array();
353  foreach($database as $row) {
354  $colData[] = $row[$field];
355  }
356 
357  // Return
359  } // function DVAR()
360 
365  public static function DVARP($database,$field,$criteria) {
366  $field = self::__fieldExtract($database,$field);
367  if (is_null($field)) {
368  return NULL;
369  }
370 
371  // reduce the database to a set of rows that match all the criteria
372  $database = self::__filter($database,$criteria);
373  // extract an array of values for the requested column
374  $colData = array();
375  foreach($database as $row) {
376  $colData[] = $row[$field];
377  }
378 
379  // Return
381  } // function DVARP()
382 
383 
384 } // class PHPExcel_Calculation_Database