ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilBackgroundTask.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
12 {
13  protected $id; // [int]
14  protected $user_id; // [int]
15  protected $start_date; // [DateTime]
16  protected $status; // [string]
17  protected $steps; // [int]
18  protected $current_step; // [int]
19  protected $handler; // [string]
20  protected $params; // [array]
21  protected $exists; // [bool]
22 
26  protected $log;
27 
28  // :TODO:
29  const STATUS_INITIALIZED = "initialized";
30  const STATUS_PROCESSING = "processing";
31  const STATUS_PROCESSED = "processed";
32  const STATUS_CANCELLING = "cancelling";
33  const STATUS_CANCELLED = "cancelled";
34  const STATUS_FINISHED = "finished";
35  const STATUS_FAILED = "failed";
36 
37  const DB_NAME = "background_task";
38 
45  public function __construct($a_id = 0)
46  {
47  $this->log = ilLoggerFactory::getLogger('btsk');
48  if ($a_id != 0) {
49  $this->doRead($a_id);
50  }
51  }
52 
57  public function getHandlerInstance()
58  {
59  $handler_id = $this->getHandlerId();
60  $this->log->debug("Handler ID: " . $handler_id);
61  if ($handler_id) {
62  include_once "Services/BackgroundTask/classes/class." . $handler_id . ".php";
63  return $handler_id::getInstanceFromTask($this);
64  }
65  }
66 
67  public static function getActiveByUserId($a_user_id)
68  {
69  global $DIC;
70  $ilDB = $DIC['ilDB'];
71 
72  $res = array();
73 
74  $set = $ilDB->query("SELECT id FROM " . self::DB_NAME .
75  " WHERE user_id = " . $ilDB->quote($a_user_id, "integer") .
76  " AND status = " . $ilDB->quote(self::STATUS_PROCESSING, "text"));
77  while ($row = $ilDB->fetchAssoc($set)) {
78  $res[] = $row["id"];
79  }
80 
81  return $res;
82  }
83 
84  public function isToBeCancelled()
85  {
86  $this->doRead($this->getId());
87  return ($this->getStatus() == self::STATUS_CANCELLING);
88  }
89 
90 
91  //
92  // setter/getter
93  //
94 
100  public function getHandlerId()
101  {
102  return $this->handler;
103  }
104 
110  public function setHandlerId($a_val)
111  {
112  $this->handler = $a_val;
113  }
114 
120  public function getId()
121  {
122  return $this->id;
123  }
124 
130  protected function setId($a_val)
131  {
132  $this->id = (int) $a_val;
133  }
134 
140  public function getUserId()
141  {
142  return $this->user_id;
143  }
144 
150  public function setUserId($a_val)
151  {
152  $this->user_id = (int) $a_val;
153  }
154 
160  public function getStartDate()
161  {
162  return $this->start_date;
163  }
164 
170  protected function setStartDate(ilDateTime $a_val)
171  {
172  $this->start_date = $a_val;
173  }
174 
180  public function getStatus()
181  {
182  return $this->status;
183  }
184 
185  protected function getValidStatus()
186  {
187  return array(
188  self::STATUS_INITIALIZED,
189  self::STATUS_PROCESSING,
190  self::STATUS_CANCELLING,
191  self::STATUS_CANCELLED,
192  self::STATUS_PROCESSED,
193  self::STATUS_FINISHED,
194  self::STATUS_FAILED,
195  );
196  }
197 
203  public function setStatus($a_val)
204  {
205  if (in_array($a_val, $this->getValidStatus())) {
206  $this->status = $a_val;
207  }
208  }
209 
215  public function getSteps()
216  {
217  return $this->steps;
218  }
219 
225  public function setSteps($a_val)
226  {
227  $this->steps = abs($a_val);
228  }
229 
235  public function getCurrentStep()
236  {
237  return $this->current_step;
238  }
239 
245  public function setCurrentStep($a_val)
246  {
247  $this->current_step = min(abs($a_val), $this->getSteps());
248  }
249 
255  public function getParams()
256  {
257  return $this->params;
258  }
259 
265  public function setParams(array $a_params = null)
266  {
267  $this->params = $a_params;
268  }
269 
275  public function exists()
276  {
277  return $this->exists;
278  }
279 
280 
281  //
282  // CRUD
283  //
284 
285  public function save()
286  {
287  $this->log->debug($this->getHandlerId());
288  // does not exist yet?
289  if (!$this->exists) {
290  $this->doCreate();
291  } else {
292  $this->doUpdate();
293  }
294  }
295 
296  public function delete()
297  {
298  $this->log->debug($this->getHandlerId());
299  if (!$this->exists) {
300  return;
301  }
302 
303  return $this->doDelete();
304  }
305 
306 
307  protected function doRead($a_id)
308  {
309  global $DIC;
310  $ilDB = $DIC['ilDB'];
311 
312  $this->log->debug($this->getHandlerId() . "/" . $a_id);
313 
314  $set = $ilDB->queryF(
315  "SELECT * FROM " . self::DB_NAME . " WHERE id=%s",
316  array("integer"),
317  array($a_id)
318  );
319 
320  while ($rec = $ilDB->fetchAssoc($set)) {
321  $this->setId($a_id);
322  $this->exists = true;
323 
324  $this->setUserId($rec["user_id"]);
325  $this->setStartDate(new ilDateTime($rec["start_date"], IL_CAL_DATETIME));
326  $this->setStatus($rec["status"]);
327  $this->setSteps($rec["steps"]);
328  $this->setCurrentStep($rec["cstep"]);
329  $this->setHandlerId($rec["handler"]);
330  $this->setParams($rec["params"]
331  ? unserialize($rec["params"])
332  : null);
333  }
334  }
335 
336  protected function preparePropertiesForDB()
337  {
338  return array(
339  "user_id" => array("integer", $this->getUserId()),
340  "start_date" => array("timestamp", $this->getStartDate()->get(IL_CAL_DATETIME)),
341  "status" => array("text", $this->getStatus()),
342  "steps" => array("integer", $this->getSteps()),
343  "cstep" => array("integer", $this->getCurrentStep()),
344  "handler" => array("string", $this->getHandlerId()),
345  "params" => array("string", is_array($this->getParams())
346  ? serialize($this->getParams())
347  : null)
348  );
349  }
350 
351  protected function doCreate()
352  {
353  global $DIC;
354  $ilDB = $DIC['ilDB'];
355 
356  $this->setId($ilDB->nextId(self::DB_NAME));
357  $this->setStartDate(new ilDateTime(time(), IL_CAL_UNIX));
358 
359  $fields = $this->preparePropertiesForDB();
360  $fields["id"] = array("integer", $this->getId());
361 
362  $ilDB->insert(self::DB_NAME, $fields);
363 
364  $this->exists = true;
365  }
366 
367  protected function doUpdate()
368  {
369  global $DIC;
370  $ilDB = $DIC['ilDB'];
371 
372  $ilDB->update(
373  self::DB_NAME,
374  $this->preparePropertiesForDB(),
375  array("id" => array("integer", $this->getId()))
376  );
377  }
378 
382  protected function doDelete()
383  {
384  global $DIC;
385  $ilDB = $DIC['ilDB'];
386 
387  return $ilDB->manipulateF(
388  "DELETE FROM " . self::DB_NAME . " WHERE id=%s",
389  array("integer"),
390  array($this->getId())
391  );
392  }
393 }
setStatus($a_val)
Sets the status.
setStartDate(ilDateTime $a_val)
Sets the date when the download was started.
const IL_CAL_DATETIME
getSteps()
Gets the steps.
setCurrentStep($a_val)
Sets the current step.
global $DIC
Definition: saml.php:7
getParams()
Gets the params.
__construct($a_id=0)
Constructor.
static getActiveByUserId($a_user_id)
getStartDate()
Gets the date when the download was started.
setHandlerId($a_val)
Sets the handler id.
setId($a_val)
Sets the id.
const IL_CAL_UNIX
foreach($_POST as $key=> $value) $res
getCurrentStep()
Gets the current step.
Date and time handling
getHandlerId()
Gets the handler.
$row
setParams(array $a_params=null)
Sets the params.
exists()
Gets whether the download object exists.
setSteps($a_val)
Sets the steps.
global $ilDB
static getLogger($a_component_id)
Get component logger.
getStatus()
Gets the status.
doDelete()
Deletes the object from the database.
getUserId()
Gets the user id.
setUserId($a_val)
Sets the user id.