ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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  public function __construct($a_id = 0, $a_reference = true)
28  {
29  global $DIC;
30 
31  $this->db = $DIC->database();
32  $this->map = $this->getPropertyMap();
33  parent::__construct($a_id, $a_reference);
34  }
35 
41  abstract protected function getPropertyMap();
42 
49  public function hasProperty($a_name)
50  {
51  return array_key_exists($a_name, $this->map);
52  }
53 
60  public function getPropertyType($a_name)
61  {
62  if ($this->hasProperty($a_name)) {
63  return $this->map[$a_name];
64  }
65  }
66 
73  public function getProperty($a_name)
74  {
75  if ($this->hasProperty($a_name)) {
76  return $this->properties[$a_name];
77  }
78  }
79 
86  public function setProperty($a_name, $a_value)
87  {
88  if ($this->hasProperty($a_name)) {
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  $value = null;
105 
106  switch ($data_type) {
107  case self::TYPE_STRING:
108  $value = (string) $a_data;
109  break;
110 
111  case self::TYPE_BOOL:
112  $value = (bool) $a_data;
113  break;
114 
115  case self::TYPE_INT:
116  $value = (int) $a_data;
117  break;
118 
119  case self::TYPE_DATE:
120  $value = new ilDate($a_data, IL_CAL_DATE);
121  break;
122 
123  case self::TYPE_ARRAY:
124  if ($a_data) {
125  $value = unserialize($a_data);
126  }
127  break;
128 
129  case self::TYPE_RAW:
130  $value = $a_raw_data;
131  break;
132  }
133 
134  $this->setProperty($a_type, $value);
135  }
136  }
137 
143  protected function exportProperty($a_name)
144  {
145  $data_type = $this->getPropertyType($a_name);
146  if ($data_type) {
147  $value = $this->getProperty($a_name);
148  $raw_data = null;
149 
150  switch ($data_type) {
151  case self::TYPE_DATE:
152  if ($value) {
153  $value = $value->get(IL_CAL_DATE);
154  }
155  break;
156 
157  case self::TYPE_ARRAY:
158  if ($value) {
159  $value = serialize($value);
160  }
161  break;
162 
163  case self::TYPE_RAW:
164  $raw_data = $value;
165  $value = null;
166  break;
167  }
168 
169  return array("parameters" => $value,
170  "raw_data" => $raw_data);
171  }
172  }
173 
179  protected function doRead()
180  {
181  $ilDB = $this->db;
182 
183  if ($this->id) {
184  $set = $ilDB->query("SELECT * FROM il_verification" .
185  " WHERE id = " . $ilDB->quote($this->id, "integer"));
186  if ($ilDB->numRows($set)) {
187  while ($row = $ilDB->fetchAssoc($set)) {
188  $this->importProperty(
189  $row["type"],
190  $row["parameters"],
191  $row["raw_data"]
192  );
193  }
194  }
195  return true;
196  }
197  return false;
198  }
199 
200  public function doCreate()
201  {
202  return $this->saveProperties();
203  }
204 
205  public function doUpdate()
206  {
207  return $this->saveProperties();
208  }
209 
215  protected function saveProperties()
216  {
217  $ilDB = $this->db;
218 
219  if ($this->id) {
220  // remove all existing properties
221  $ilDB->manipulate("DELETE FROM il_verification" .
222  " WHERE id = " . $ilDB->quote($this->id, "integer"));
223 
224  foreach ($this->getPropertyMap() as $name => $type) {
225  $property = $this->exportProperty($name);
226 
227  $fields = array("id" => array("integer", $this->id),
228  "type" => array("text", $name),
229  "parameters" => array("text", $property["parameters"]),
230  "raw_data" => array("text", $property["raw_data"]));
231 
232  $ilDB->insert("il_verification", $fields);
233  }
234 
235  $this->handleQuotaUpdate();
236 
237  return true;
238  }
239  return false;
240  }
241 
242 
248  public function doDelete()
249  {
250  $ilDB = $this->db;
251 
252  if ($this->id) {
253  // remove all files
254  include_once "Services/Verification/classes/class.ilVerificationStorageFile.php";
255  $storage = new ilVerificationStorageFile($this->id);
256  $storage->delete();
257 
258  $this->handleQuotaUpdate();
259 
260  $ilDB->manipulate("DELETE FROM il_verification" .
261  " WHERE id = " . $ilDB->quote($this->id, "integer"));
262  return true;
263  }
264  return false;
265  }
266 
267  public static function initStorage($a_id, $a_subdir = null)
268  {
269  include_once "Services/Verification/classes/class.ilVerificationStorageFile.php";
270  $storage = new ilVerificationStorageFile($a_id);
271  $storage->create();
272 
273  $path = $storage->getAbsolutePath() . "/";
274 
275  if ($a_subdir) {
276  $path .= $a_subdir . "/";
277 
278  if (!is_dir($path)) {
279  mkdir($path);
280  }
281  }
282 
283  return $path;
284  }
285 
286  public function getFilePath()
287  {
288  $file = $this->getProperty("file");
289  if ($file) {
290  $path = $this->initStorage($this->getId(), "certificate");
291  return $path . $file;
292  }
293  }
294 
295  public function getOfflineFilename()
296  {
297  return ilUtil::getASCIIFilename($this->getTitle()) . ".pdf";
298  }
299 
300  protected function handleQuotaUpdate()
301  {
302  include_once "Services/DiskQuota/classes/class.ilDiskQuotaHandler.php";
304  $this->getType(),
305  $this->getId(),
306  ilUtil::dirsize($this->initStorage($this->getId())),
307  array($this->getId()),
308  true
309  );
310  }
311 }
importProperty($a_type, $a_data=null, $a_raw_data=null)
Import property from database.
$path
Definition: aliased.php:25
exportProperty($a_name)
Export property to database.
global $DIC
Definition: saml.php:7
getProperty($a_name)
Get property value.
static getASCIIFilename($a_filename)
convert utf8 to ascii filename
setProperty($a_name, $a_value)
Set property value.
hasProperty($a_name)
Check if given property is valid.
doDelete()
Delete entry from database.
$a_type
Definition: workflow.php:92
Verification object base class.
static initStorage($a_id, $a_subdir=null)
Class for single dates.
getPropertyType($a_name)
Get property data type.
__construct($a_id=0, $a_reference=true)
$row
static dirsize($directory)
get size of a directory or a file.
const IL_CAL_DATE
static handleUpdatedSourceObject($a_src_obj_type, $a_src_obj_id, $a_src_filesize, $a_owner_obj_ids=null, $a_is_prtf=false)
Find and update/create all related entries for source object.
saveProperties()
Save current properties to database.
global $ilDB
doRead()
Read database entry.
getPropertyMap()
Return property map (name => type)