ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilVerificationObject.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once ('./Services/Object/classes/class.ilObject2.php');
5 
15 abstract class ilVerificationObject extends ilObject2
16 {
17  protected $map = array();
18  protected $properties = array();
19 
20  const TYPE_STRING = 1;
21  const TYPE_BOOL = 2;
22  const TYPE_INT = 3;
23  const TYPE_DATE = 4;
24  const TYPE_RAW = 5;
25  const TYPE_ARRAY = 6;
26 
27  function __construct($a_id = 0, $a_reference = true)
28  {
29  $this->map = $this->getPropertyMap();
30  parent::__construct($a_id, $a_reference);
31  }
32 
38  abstract protected function getPropertyMap();
39 
46  public function hasProperty($a_name)
47  {
48  return array_key_exists($a_name, $this->map);
49  }
50 
57  public function getPropertyType($a_name)
58  {
59  if($this->hasProperty($a_name))
60  {
61  return $this->map[$a_name];
62  }
63  }
64 
71  public function getProperty($a_name)
72  {
73  if($this->hasProperty($a_name))
74  {
75  return $this->properties[$a_name];
76  }
77  }
78 
85  public function setProperty($a_name, $a_value)
86  {
87  if($this->hasProperty($a_name))
88  {
89  $this->properties[$a_name] = $a_value;
90  }
91  }
92 
100  protected function importProperty($a_type, $a_data = null, $a_raw_data = null)
101  {
102  $data_type = $this->getPropertyType($a_type);
103  if($data_type)
104  {
105  $value = null;
106 
107  switch($data_type)
108  {
109  case self::TYPE_STRING:
110  $value = (string)$a_data;
111  break;
112 
113  case self::TYPE_BOOL:
114  $value = (bool)$a_data;
115  break;
116 
117  case self::TYPE_INT:
118  $value = (int)$a_data;
119  break;
120 
121  case self::TYPE_DATE:
122  $value = new ilDate($a_data, IL_CAL_DATE);
123  break;
124 
125  case self::TYPE_ARRAY:
126  if($a_data)
127  {
128  $value = unserialize($a_data);
129  }
130  break;
131 
132  case self::TYPE_RAW:
133  $value = $a_raw_data;
134  break;
135  }
136 
137  $this->setProperty($a_type, $value);
138  }
139  }
140 
146  protected function exportProperty($a_name)
147  {
148  $data_type = $this->getPropertyType($a_name);
149  if($data_type)
150  {
151  $value = $this->getProperty($a_name);
152  $raw_data = null;
153 
154  switch($data_type)
155  {
156  case self::TYPE_DATE:
157  if($value)
158  {
159  $value = $value->get(IL_CAL_DATE);
160  }
161  break;
162 
163  case self::TYPE_ARRAY:
164  if($value)
165  {
166  $value = serialize($value);
167  }
168  break;
169 
170  case self::TYPE_RAW:
171  $raw_data = $value;
172  $value = null;
173  break;
174  }
175 
176  return array("parameters" => $value,
177  "raw_data" => $raw_data);
178  }
179  }
180 
186  protected function doRead()
187  {
188  global $ilDB;
189 
190  if($this->id)
191  {
192  $set = $ilDB->query("SELECT * FROM il_verification".
193  " WHERE id = ".$ilDB->quote($this->id, "integer"));
194  if($ilDB->numRows($set))
195  {
196  while($row = $ilDB->fetchAssoc($set))
197  {
198  $this->importProperty($row["type"], $row["parameters"],
199  $row["raw_data"]);
200  }
201  }
202  return true;
203  }
204  return false;
205  }
206 
207  public function doCreate()
208  {
209  return $this->saveProperties();
210  }
211 
212  public function doUpdate()
213  {
214  return $this->saveProperties();
215  }
216 
222  protected function saveProperties()
223  {
224  global $ilDB;
225 
226  if($this->id)
227  {
228  // remove all existing properties
229  $ilDB->manipulate("DELETE FROM il_verification".
230  " WHERE id = ".$ilDB->quote($this->id, "integer"));
231 
232  foreach($this->getPropertyMap() as $name => $type)
233  {
234  $property = $this->exportProperty($name);
235 
236  $fields = array("id" => array("integer", $this->id),
237  "type" => array("text", $name),
238  "parameters" => array("text", $property["parameters"]),
239  "raw_data" => array("text", $property["raw_data"]));
240 
241  $ilDB->insert("il_verification", $fields);
242  }
243 
244  $this->handleQuotaUpdate();
245 
246  return true;
247  }
248  return false;
249  }
250 
251 
257  public function doDelete()
258  {
259  global $ilDB;
260 
261  if($this->id)
262  {
263  // remove all files
264  include_once "Services/Verification/classes/class.ilVerificationStorageFile.php";
265  $storage = new ilVerificationStorageFile($this->id);
266  $storage->delete();
267 
268  $this->handleQuotaUpdate();
269 
270  $ilDB->manipulate("DELETE FROM il_verification".
271  " WHERE id = ".$ilDB->quote($this->id, "integer"));
272  return true;
273  }
274  return false;
275  }
276 
277  public static function initStorage($a_id, $a_subdir = null)
278  {
279  include_once "Services/Verification/classes/class.ilVerificationStorageFile.php";
280  $storage = new ilVerificationStorageFile($a_id);
281  $storage->create();
282 
283  $path = $storage->getAbsolutePath()."/";
284 
285  if($a_subdir)
286  {
287  $path .= $a_subdir."/";
288 
289  if(!is_dir($path))
290  {
291  mkdir($path);
292  }
293  }
294 
295  return $path;
296  }
297 
298  public function getFilePath()
299  {
300  $file = $this->getProperty("file");
301  if($file)
302  {
303  $path = $this->initStorage($this->getId(), "certificate");
304  return $path.$file;
305  }
306  }
307 
308  public function getOfflineFilename()
309  {
310  return ilUtil::getASCIIFilename($this->getTitle()).".pdf";
311  }
312 
313  protected function handleQuotaUpdate()
314  {
315  include_once "Services/DiskQuota/classes/class.ilDiskQuotaHandler.php";
317  $this->getId(),
318  ilUtil::dirsize($this->initStorage($this->getId())),
319  array($this->getId()),
320  true);
321  }
322 }
323 
324 ?>