ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  {
50  $this->doRead($a_id);
51  }
52  }
53 
58  public function getHandlerInstance()
59  {
60  $handler_id = $this->getHandlerId();
61  $this->log->debug("Handler ID: ".$handler_id);
62  if($handler_id)
63  {
64  include_once "Services/BackgroundTask/classes/class.".$handler_id.".php";
65  return $handler_id::getInstanceFromTask($this);
66  }
67  }
68 
69  public static function getActiveByUserId($a_user_id)
70  {
71  global $ilDB;
72 
73  $res = array();
74 
75  $set = $ilDB->query("SELECT id FROM ".self::DB_NAME.
76  " WHERE user_id = ".$ilDB->quote($a_user_id, "integer").
77  " AND status = ".$ilDB->quote(self::STATUS_PROCESSING, "text"));
78  while($row = $ilDB->fetchAssoc($set))
79  {
80  $res[] = $row["id"];
81  }
82 
83  return $res;
84  }
85 
86  public function isToBeCancelled()
87  {
88  $this->doRead($this->getId());
89  return ($this->getStatus() == self::STATUS_CANCELLING);
90  }
91 
92 
93  //
94  // setter/getter
95  //
96 
102  public function getHandlerId()
103  {
104  return $this->handler;
105  }
106 
112  public function setHandlerId($a_val)
113  {
114  $this->handler = $a_val;
115  }
116 
122  public function getId()
123  {
124  return $this->id;
125  }
126 
132  protected function setId($a_val)
133  {
134  $this->id = (int)$a_val;
135  }
136 
142  public function getUserId()
143  {
144  return $this->user_id;
145  }
146 
152  public function setUserId($a_val)
153  {
154  $this->user_id = (int)$a_val;
155  }
156 
162  public function getStartDate()
163  {
164  return $this->start_date;
165  }
166 
172  protected function setStartDate(ilDateTime $a_val)
173  {
174  $this->start_date = $a_val;
175  }
176 
182  public function getStatus()
183  {
184  return $this->status;
185  }
186 
187  protected function getValidStatus()
188  {
189  return array(
190  self::STATUS_INITIALIZED,
191  self::STATUS_PROCESSING,
192  self::STATUS_CANCELLING,
193  self::STATUS_CANCELLED,
194  self::STATUS_PROCESSED,
195  self::STATUS_FINISHED,
196  self::STATUS_FAILED,
197  );
198  }
199 
205  public function setStatus($a_val)
206  {
207  if(in_array($a_val, $this->getValidStatus()))
208  {
209  $this->status = $a_val;
210  }
211  }
212 
218  public function getSteps()
219  {
220  return $this->steps;
221  }
222 
228  public function setSteps($a_val)
229  {
230  $this->steps = abs($a_val);
231  }
232 
238  public function getCurrentStep()
239  {
240  return $this->current_step;
241  }
242 
248  public function setCurrentStep($a_val)
249  {
250  $this->current_step = min(abs($a_val), $this->getSteps());
251  }
252 
258  public function getParams()
259  {
260  return $this->params;
261  }
262 
268  public function setParams(array $a_params = null)
269  {
270  $this->params = $a_params;
271  }
272 
278  public function exists()
279  {
280  return $this->exists;
281  }
282 
283 
284  //
285  // CRUD
286  //
287 
288  public function save()
289  {
290  $this->log->debug($this->getHandlerId());
291  // does not exist yet?
292  if (!$this->exists)
293  {
294  $this->doCreate();
295  }
296  else
297  {
298  $this->doUpdate();
299  }
300  }
301 
302  public function delete()
303  {
304  $this->log->debug($this->getHandlerId());
305  if(!$this->exists)
306  {
307  return;
308  }
309 
310  return $this->doDelete();
311  }
312 
313 
314  protected function doRead($a_id)
315  {
316  global $ilDB;
317 
318  $this->log->debug($this->getHandlerId()."/".$a_id);
319 
320  $set = $ilDB->queryF(
321  "SELECT * FROM " . self::DB_NAME . " WHERE id=%s",
322  array("integer"),
323  array($a_id));
324 
325  while ($rec = $ilDB->fetchAssoc($set))
326  {
327  $this->setId($a_id);
328  $this->exists = true;
329 
330  $this->setUserId($rec["user_id"]);
331  $this->setStartDate(new ilDateTime($rec["start_date"], IL_CAL_DATETIME));
332  $this->setStatus($rec["status"]);
333  $this->setSteps($rec["steps"]);
334  $this->setCurrentStep($rec["cstep"]);
335  $this->setHandlerId($rec["handler"]);
336  $this->setParams($rec["params"]
337  ? unserialize($rec["params"])
338  : null);
339  }
340  }
341 
342  protected function preparePropertiesForDB()
343  {
344  return array(
345  "user_id" => array("integer", $this->getUserId()),
346  "start_date" => array("timestamp", $this->getStartDate()->get(IL_CAL_DATETIME)),
347  "status" => array("text", $this->getStatus()),
348  "steps" => array("integer", $this->getSteps()),
349  "cstep" => array("integer", $this->getCurrentStep()),
350  "handler" => array("string", $this->getHandlerId()),
351  "params" => array("string", is_array($this->getParams())
352  ? serialize($this->getParams())
353  : null)
354  );
355  }
356 
357  protected function doCreate()
358  {
359  global $ilDB;
360 
361  $this->setId($ilDB->nextId(self::DB_NAME));
362  $this->setStartDate(new ilDateTime(time(), IL_CAL_UNIX));
363 
364  $fields = $this->preparePropertiesForDB();
365  $fields["id"] = array("integer", $this->getId());
366 
367  $ilDB->insert(self::DB_NAME, $fields);
368 
369  $this->exists = true;
370  }
371 
372  protected function doUpdate()
373  {
374  global $ilDB;
375 
376  $ilDB->update(
377  self::DB_NAME,
378  $this->preparePropertiesForDB(),
379  array("id" => array("integer", $this->getId()))
380  );
381  }
382 
386  protected function doDelete()
387  {
388  global $ilDB;
389 
390  return $ilDB->manipulateF(
391  "DELETE FROM " . self::DB_NAME . " WHERE id=%s",
392  array("integer"),
393  array($this->getId()));
394  }
395 }
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.
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
getCurrentStep()
Gets the current step.
Date and time handling
getHandlerId()
Gets the handler.
Create styles array
The data for the language used.
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.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
getStatus()
Gets the status.
doDelete()
Deletes the object from the database.
getUserId()
Gets the user id.
setUserId($a_val)
Sets the user id.