ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilPaymentPrices.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
12 {
13  private $ilDB;
14 
15  private $pobject_id;
16 
17  private $price;
18  private $currency;
19  private $duration;
20  private $unlimited_duration = 0;
21 
22  // TODO later -> this is for using different currencies
24 
25  private $prices = array();
26 
27  public function ilPaymentPrices($a_pobject_id = 0)
28  {
29  global $ilDB;
30 
31  $this->db = $ilDB;
32 
33  $this->pobject_id = $a_pobject_id;
34 
35  $this->__read();
36  }
37 
38  // SET GET
39  public function getPobjectId()
40  {
41  return $this->pobject_id;
42  }
43 
44  public function getPrices()
45  {
46  return $this->prices ? $this->prices : array();
47  }
48  function getPrice($a_price_id)
49  {
50  return $this->prices[$a_price_id] ? $this->prices[$a_price_id] : array();
51  }
52 
53 
54  // STATIC
55  public static function _getPrice($a_price_id)
56  {
57  global $ilDB, $ilSettings;
58 
59  $res = $ilDB->queryf('
60  SELECT * FROM payment_prices
61  WHERE price_id = %s',
62  array('integer'), array($a_price_id));
63 
64  while($row = $ilDB->fetchObject($res))
65  {
66  $price['duration'] = $row->duration;
67  $price['unlimited_duration'] = $row->unlimited_duration;
68  $price['currency'] = $row->currency;
69  $price['price'] = $row->price;
70 
71  }
72  return count($price) ? $price : array();
73  }
74 
75  public static function _countPrices($a_pobject_id)
76  {
77  global $ilDB;
78 
79  $res = $ilDB->queryf('
80  SELECT count(price_id) FROM payment_prices
81  WHERE pobject_id = %s',
82  array('integer'),
83  array($a_pobject_id));
84 
85  $row = $res->fetchRow(DB_FETCHMODE_ARRAY);
86 
87  return ($row[0]);
88  }
89 
90  public static function _getPriceString($a_price_id)
91  {
92  #include_once './Services/Payment/classes/class.ilPaymentCurrency.php';
93 
94  global $lng;
95 
96  $price = ilPaymentPrices::_getPrice($a_price_id);
97 
98  return (float)$price['price'];
99 
100  }
101 
102  public static function _formatPriceToString($a_price)
103  {
104  include_once './Services/Payment/classes/class.ilGeneralSettings.php';
105 
106  $genSet = new ilGeneralSettings();
107  $currency_unit = $genSet->get('currency_unit');
108 
109  return $a_price . ' ' . $currency_unit;
110 
111 /* TODO: after currency implementation is finished -> replace whole function
112  * include_once './Services/Payment/classes/class.ilPaymentCurrency.php';
113 
114  $separator= ilPaymentCurrency::_getDecimalSeparator();
115  $currency_symbol = ilPaymentCurrency::_getSymbol($a_currency_id);
116  $price_string = number_format($a_price,'2',$separator,'');
117 
118  return $price_string . ' ' . $currency_symbol;
119  */
120  }
121 
122 
123  public static function _getPriceStringFromAmount($a_price)
124  {
125  include_once './Services/Payment/classes/class.ilPaymentCurrency.php';
126  include_once './Services/Payment/classes/class.ilGeneralSettings.php';
127 
128  global $lng;
129 
130  $genSet = new ilGeneralSettings();
131  $currency_unit = $genSet->get("currency_unit");
132 
133  $pr_str = '';
134 
135  $pr_str = number_format($a_price , 2, ",", ".");
136 /* TODO: CURRENCY $pr_str = number_format($a_price * $this->getCurrencyConversionRate() , 2, ",", ".");
137  * remove genset
138  * */
139 
140  return $pr_str . " " . $currency_unit;
141  }
142 
143 
144  public static function _getTotalAmount($a_price_ids)
145  {
146 
147  include_once './Services/Payment/classes/class.ilPaymentPrices.php';
148 # include_once './Services/Payment/classes/class.ilPaymentCurrency.php';
149  include_once './Services/Payment/classes/class.ilGeneralSettings.php';
150 
151  global $ilDB, $lng;
152 
153  $genSet = new ilGeneralSettings();
154  $currency_unit = $genSet->get("currency_unit");
155 
156  $amount = array();
157 
158  if (is_array($a_price_ids))
159  {
160  for ($i = 0; $i < count($a_price_ids); $i++)
161  {
162  $price_data = ilPaymentPrices::_getPrice($a_price_ids[$i]["id"]);
163 
164  $price = (float) $price_data["price"];
165  $amount[$a_price_ids[$i]["pay_method"]] += (float) $price;
166  }
167  }
168 /* TODO: CURRENCY replace 'if' & remove genset
169  if (is_array($a_price_ids))
170  {
171  $default_currency = ilPaymentCurrency::_getDefaultcurrency();
172 
173  for ($i = 0; $i < count($a_price_ids); $i++)
174  {
175  $price_data = ilPaymentPrices::_getPrice($a_price_ids[$i]["id"]);
176 
177  if($price_data['currency'] != $default_currency['currency_id'])
178  {
179  $conversion_rate = ilPaymentCurrency::_getConversionRate($price_data['currency']);
180  $price = round(((float) $price_data['price'] * $conversion_rate),2);
181  }
182  else
183  $price = (float) $price_data["price"];
184 
185  $amount[$a_price_ids[$i]["pay_method"]] += (float) $price;
186  }
187  }
188  */
189  return $amount;
190  }
191 
192 
193  public function setPrice($a_price = 0)
194  {
195  $this->price = preg_replace('/^0+/','',$a_price);
196 
197  $this->price = $a_price;
198  }
199 
200  public function setCurrency($a_currency_id)
201  {
202  $this->currency = $a_currency_id;
203  }
204  public function setDuration($a_duration)
205  {
206  if($this->unlimited_duration == '1' && ($a_duration == '' || null))
207  $a_duration = 0;
208 
209  $this->duration = (int)$a_duration;
210  }
211 
212  public function setUnlimitedDuration($a_unlimited_duration)
213  {
214  if($a_unlimited_duration)
215  $this->unlimited_duration = (int)$a_unlimited_duration;
216  else
217  $this->unlimited_duration = 0;
218  }
219 
220  public function add()
221  {
222  $next_id = $this->db->nextId('payment_prices');
223 
224  $res = $this->db->manipulateF('
225  INSERT INTO payment_prices
226  ( price_id,
227  pobject_id,
228  currency,
229  duration,
230  unlimited_duration,
231  price
232  )
233  VALUES (%s, %s, %s, %s, %s, %s)',
234 
235  array('integer','integer', 'integer', 'integer', 'integer', 'float'),
236  array( $next_id,
237  $this->getPobjectId(),
238  $this->__getCurrency(),
239  $this->__getDuration(),
240  $this->__getUnlimitedDuration(),
241  $this->__getPrice()
242  ));
243 
244  $this->__read();
245 
246  return true;
247  }
248  public function update($a_price_id)
249  {
250  $res = $this->db->manipulateF('
251  UPDATE payment_prices SET
252  currency = %s,
253  duration = %s,
254  unlimited_duration = %s,
255  price = %s
256  WHERE price_id = %s',
257 
258  array('integer', 'integer','integer', 'float', 'integer'),
259  array( $this->__getCurrency(),
260  $this->__getDuration(),
261  $this->__getUnlimitedDuration(),
262  $this->__getPrice(),
263  $a_price_id
264  ));
265 
266  $this->__read();
267 
268  return true;
269  }
270  public function delete($a_price_id)
271  {
272  $statement = $this->db->manipulateF('
273  DELETE FROM payment_prices
274  WHERE price_id = %s',
275  array('integer'), array($a_price_id));
276 
277  $this->__read();
278 
279  return true;
280  }
281  public function deleteAllPrices()
282  {
283  $statement = $this->db->manipulateF('
284  DELETE FROM payment_prices
285  WHERE pobject_id = %s',
286  array('integer'),
287  array($this->getPobjectId()));
288 
289  $this->__read();
290 
291  return true;
292  }
293 
294  public function validate()
295  {
296 
297  $duration_valid = false;
298  $price_valid = false;
299 
300  if(preg_match('/^(([1-9][0-9]{0,1})|[0])?$/',$this->__getDuration())
301  || ((int)$this->__getDuration() == 0 && $this->__getUnlimitedDuration() == 1))
302  {
303  $duration_valid = true;
304  }
305 
306  if(preg_match('/[0-9]/',$this->__getPrice()))
307  {
308 
309  $price_valid = true;
310  }
311 
312  if($duration_valid == true && $price_valid == true)
313  {
314  return true;
315 
316  }
317 
318  else return false;
319 
320  }
321  // STATIC
322  public static function _priceExists($a_price_id,$a_pobject_id)
323  {
324  global $ilDB;
325 
326  $res = $ilDB->queryf('
327  SELECT * FROM payment_prices
328  WHERE price_id = %s
329  AND pobject_id = %s',
330  array('integer', 'integer'),
331  array($a_price_id, $a_pobject_id));
332 
333  return $res->numRows() ? true : false;
334  }
335 
336  // PRIVATE
337 
338  private function __getPrice()
339  {
340  return $this->price;
341  }
342  private function __getCurrency()
343  {
344  /*TODO: CURRENCY not finished yet -> return 1 as default */
345  if($this->currency == null)
346  $this->currency = 1;
347  return $this->currency;
348  }
349  private function __getDuration()
350  {
351  return $this->duration;
352  }
353  private function __getUnlimitedDuration()
354  {
356  }
357 
358  private function __read()
359  {
360  $this->prices = array();
361 
362  $res = $this->db->queryf('
363  SELECT * FROM payment_prices
364  WHERE pobject_id = %s
365  ORDER BY duration',
366  array('integer'),
367  array($this->getPobjectId()));
368 
369 
370  while($row = $this->db->fetchObject($res))
371  {
372  $this->prices[$row->price_id]['pobject_id'] = $row->pobject_id;
373  $this->prices[$row->price_id]['price_id'] = $row->price_id;
374  $this->prices[$row->price_id]['currency'] = $row->currency;
375  $this->prices[$row->price_id]['duration'] = $row->duration;
376  $this->prices[$row->price_id]['unlimited_duration'] = $row->unlimited_duration;
377  $this->prices[$row->price_id]['price'] = $row->price;
378 //TODO: CURRENCY $this->prices[$row->price_id]['price'] = $row->price * $this->getCurrencyConversionRate();
379  }
380  }
381 
382  public function getNumberOfPrices()
383  {
384  return count($this->prices);
385  }
386 
387  public function getLowestPrice()
388  {
389  $lowest_price_id = 0;
390  $lowest_price = 0;
391 
392  foreach ($this->prices as $price_id => $data)
393  {
394  $current_price = $data['price'];
395 
396  if($lowest_price == 0||
397  $lowest_price > (float)$current_price)
398  {
399  $lowest_price = (float)$current_price;
400  $lowest_price_id = $price_id;
401  }
402  }
403 
404  return is_array($this->prices[$lowest_price_id]) ? $this->prices[$lowest_price_id] : array();
405  }
406 }
407 ?>