ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilBackgroundTaskHub.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once "Services/BackgroundTask/classes/class.ilBackgroundTask.php";
5 
14 {
15  protected $task; // [ilBackgroundTask]
16  protected $handler; // [ilBackgroundTaskHandler]
17 
23  public function __construct()
24  {
25  global $DIC;
26  $lng = $DIC['lng'];
27 
28  $lng->loadLanguageModule("bgtask");
29 
30  if ((int) $_REQUEST["tid"]) {
31  $this->task = new ilBackgroundTask((int) $_REQUEST["tid"]);
32  $this->handler = $this->task->getHandlerInstance();
33  }
34  }
35 
36 
37  //
38  // ajax
39  //
40 
44  public function executeCommand()
45  {
46  global $DIC;
47  $ilCtrl = $DIC['ilCtrl'];
48 
49  $next_class = $ilCtrl->getNextClass($this);
50  $cmd = $ilCtrl->getCmd("validate");
51 
52  switch ($next_class) {
53  default:
54  if ($cmd == "deliver" ||
55  $ilCtrl->isAsynch()) {
56  $this->$cmd();
57  break;
58  }
59  }
60 
61  // deliver file and ajax require exit
62  exit();
63  }
64 
70  protected function sendJson(stdClass $a_json)
71  {
72  echo json_encode($a_json);
73  }
74 
78  protected function validate()
79  {
80  $class = trim($_GET["hid"]);
81  $file = "Services/BackgroundTask/classes/class." . $class . ".php";
82  if (file_exists($file)) {
83  include_once $file;
84  $handler = new $class();
85  $json = $handler->init($_GET["par"]);
86 
87  $this->sendJson($json);
88  }
89  }
90 
95  protected function unblock()
96  {
97  global $DIC;
98  $ilUser = $DIC['ilUser'];
99 
100  foreach (ilBackgroundTask::getActiveByUserId($ilUser->getId()) as $task_id) {
101  // leave current task alone
102  if ($task_id != $this->task->getId()) {
103  // emit cancelling status, running processes will cancel
104  $task = new ilBackgroundTask($task_id);
106  $task->save();
107  }
108  }
109 
110  // init/start current task
111  $json = $this->handler->init();
112  $this->sendJson($json);
113  }
114 
118  protected function process()
119  {
120  $this->task->setStatus(ilBackgroundTask::STATUS_PROCESSING);
121  $this->task->save();
122 
123  if (!$this->isSOAPEnabled()) {
124  $this->handler->process();
125  } else {
126  require_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
127  $soap_client = new ilSoapClient();
128  $soap_client->setResponseTimeout(1);
129  $soap_client->enableWSDL(true);
130  $soap_client->init();
131  $soap_client->call('processBackgroundTask', array(
132  session_id() . '::' . $_COOKIE['ilClientId'],
133  $this->task->getId()
134  ));
135  }
136  }
137 
143  public function isSOAPEnabled()
144  {
145  global $DIC;
146  $ilSetting = $DIC['ilSetting'];
147 
148  // see ilMail
149  return (extension_loaded('curl') &&
150  $ilSetting->get('soap_user_administration') &&
152  }
153 
157  protected function progress()
158  {
159  include_once "Services/BackgroundTask/classes/class.ilBackgroundTaskJson.php";
160 
161  // if task has been finished, get result action
162  if ($this->task->getStatus() == ilBackgroundTask::STATUS_FINISHED) {
163  $result = $this->handler->finish();
164  $json = ilBackgroundTaskJson::getProgressJson($this->task, $result[0], $result[1]);
165  } else {
166  $json = ilBackgroundTaskJson::getProgressJson($this->task);
167  }
168 
169  $this->sendJson($json);
170  }
171 
175  protected function cancel()
176  {
177  // just emit cancelling status, (background) process will stop ASAP
178  $this->task->setStatus(ilBackgroundTask::STATUS_CANCELLING);
179  $this->task->save();
180  }
181 
185  protected function deliver()
186  {
187  // :TODO: delete task?
188 
189  $this->handler->deliver();
190  }
191 }
$_COOKIE['client_id']
Definition: server.php:9
$result
background task hub (aka ajax handler, GUI)
global $DIC
Definition: saml.php:7
$_GET["client_id"]
const CONTEXT_CRON
static getActiveByUserId($a_user_id)
cancel()
Cancel current task.
validate()
Validate given task.
global $ilCtrl
Definition: ilias.php:18
isSOAPEnabled()
Is soap enabled?
progress()
Check progress of current task.
unblock()
Cancel all other tasks, start current one.
$lng
$ilUser
Definition: imgupload.php:18
static getProgressJson(ilBackgroundTask $a_task, $a_finished_cmd=null, $a_finished_result=null)
Get json for task progress.
sendJson(stdClass $a_json)
Send Json to client.
exit
Definition: backend.php:16
process()
Process current task.
executeCommand()
Execute current command.
global $ilSetting
Definition: privfeed.php:17
static getType()
Get context type.