ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCronManagerTableGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once './Services/Table/classes/class.ilTable2GUI.php';
5 
13 {
20  public function __construct($a_parent_obj, $a_parent_cmd)
21  {
22  global $ilCtrl, $lng;
23 
24  $this->setId("crnmng"); // #14526 / #16391
25 
26  parent::__construct($a_parent_obj, $a_parent_cmd);
27 
28  $this->addColumn("", "", 1);
29  $this->addColumn($this->lng->txt("cron_job_id"), "title");
30  $this->addColumn($this->lng->txt("cron_component"), "component");
31  $this->addColumn($this->lng->txt("cron_schedule"), "schedule");
32  $this->addColumn($this->lng->txt("cron_status"), "status");
33  $this->addColumn($this->lng->txt("cron_status_info"), "");
34  $this->addColumn($this->lng->txt("cron_result"), "result");
35  $this->addColumn($this->lng->txt("cron_result_info"), "");
36  $this->addColumn($this->lng->txt("cron_last_run"), "last_run");
37  $this->addColumn($this->lng->txt("actions"), "");
38 
39  $this->setTitle($this->lng->txt("cron_jobs"));
40  $this->setDefaultOrderField("title");
41 
42  $this->setSelectAllCheckbox("mjid");
43  $this->addMultiCommand("activate", $lng->txt("cron_action_activate"));
44  $this->addMultiCommand("deactivate", $lng->txt("cron_action_deactivate"));
45 
46  $this->setRowTemplate("tpl.cron_job_row.html", "Services/Cron");
47  $this->setFormAction($ilCtrl->getFormAction($a_parent_obj, $a_parent_cmd));
48 
49  $this->getItems();
50  }
51 
52  protected function parseJobToData(array $a_item, ilCronJob $job)
53  {
54  global $lng;
55 
56  $res = $a_item;
57 
58  $res["title"] = $job->getTitle();
59  $res["description"] = $job->getDescription();
60  $res["has_settings"] = $job->hasCustomSettings();
61 
62  if(!$res["title"])
63  {
64  $res["title"] = $a_item["job_id"];
65  }
66 
67  // schedule
68  if(!$job->hasFlexibleSchedule())
69  {
70  // schedule type changed
71  if($a_item["schedule_type"])
72  {
73  ilCronManager::updateJobSchedule($job, null, null);
74  }
75 
76  $a_item["schedule_type"] = $job->getDefaultScheduleType();
77  $a_item["schedule_value"] = $job->getDefaultScheduleValue();
78  $res["editable_schedule"] = false;
79  }
80  else
81  {
82  // schedule type changed
83  if(!$a_item["schedule_type"])
84  {
85  $a_item["schedule_type"] = $job->getDefaultScheduleType();
86  $a_item["schedule_value"] = $job->getDefaultScheduleValue();
87  ilCronManager::updateJobSchedule($job, $a_item["schedule_type"],
88  $a_item["schedule_value"]);
89  }
90 
91  $res["editable_schedule"] = true;
92  }
93 
94  switch($a_item["schedule_type"])
95  {
97  $schedule = $lng->txt("cron_schedule_daily");
98  break;
99 
101  $schedule = $lng->txt("cron_schedule_weekly");
102  break;
103 
105  $schedule = $lng->txt("cron_schedule_monthly");
106  break;
107 
109  $schedule = $lng->txt("cron_schedule_quarterly");
110  break;
111 
113  $schedule = $lng->txt("cron_schedule_yearly");
114  break;
115 
117  $schedule = sprintf($lng->txt("cron_schedule_in_minutes"), $a_item["schedule_value"]);
118  break;
119 
121  $schedule = sprintf($lng->txt("cron_schedule_in_hours"), $a_item["schedule_value"]);
122  break;
123 
125  $schedule = sprintf($lng->txt("cron_schedule_in_days"), $a_item["schedule_value"]);
126  break;
127  }
128  $res["schedule"] = $schedule;
129 
130  // status
131  if($a_item["job_status"])
132  {
133  $res["status"] = $lng->txt("cron_status_active");
134  }
135  else
136  {
137  $res["status"] = $lng->txt("cron_status_inactive");
138  }
139 
140  $status_info = array();
141  if($a_item["job_status_ts"])
142  {
143  $status_info[] = ilDatePresentation::formatDate(new ilDateTime($a_item["job_status_ts"], IL_CAL_UNIX));
144  }
145  if(!$a_item["job_status_type"])
146  {
147  $status_info[] = $lng->txt("cron_changed_by_crontab");
148  }
149  else
150  {
151  $status_info[] = ilUserUtil::getNamePresentation($a_item["job_status_user_id"]);
152  }
153  $res["status_info"] = implode("<br />", $status_info);
154 
155  // result
156  $result = "-";
157  if($a_item["job_result_status"])
158  {
159  switch($a_item["job_result_status"])
160  {
162  $result = $lng->txt("cron_result_status_invalid_configuration");
163  break;
164 
166  $result = $lng->txt("cron_result_status_no_action");
167  break;
168 
170  $result = $lng->txt("cron_result_status_ok");
171  break;
172 
174  $result = $lng->txt("cron_result_status_crashed");
175  break;
176 
178  $result = $lng->txt("cron_result_status_reset");
179  break;
180 
182  $result = $lng->txt("cron_result_status_fail");
183  break;
184  }
185  }
186  $res["result"] = $result;
187 
188  $result_info = array();
189  if($a_item["job_result_dur"])
190  {
191  $result_info[] = ($a_item["job_result_dur"]/1000)." sec";
192  }
193  if($a_item["job_result_message"])
194  {
195  $result_info[] = $a_item["job_result_message"];
196  }
197  if(DEVMODE && $a_item["job_result_code"]) // #11866
198  {
199  $result_info[] = $a_item["job_result_code"];
200  }
201  if(!$a_item["job_result_type"])
202  {
203  $result_info[] = $lng->txt("cron_changed_by_crontab");
204  }
205  else
206  {
207  $result_info[] = ilUserUtil::getNamePresentation($a_item["job_result_user_id"]);
208  }
209  $res["result_info"] = implode("<br />", $result_info);
210 
211  if($a_item["running_ts"])
212  {
213  $res["last_run"] = strtotime("+1year", $a_item["running_ts"]);
214  }
215  else if($a_item["job_result_ts"])
216  {
217  $res["last_run"] = $a_item["job_result_ts"];
218  }
219  else
220  {
221  $res["last_run"] = null;
222  }
223 
224  $res['is_manually_executable'] = $job->isManuallyExecutable();
225 
226  return $res;
227  }
228 
229  protected function getItems()
230  {
231  global $ilPluginAdmin, $lng;
232 
233  include_once "Services/User/classes/class.ilUserUtil.php";
234  include_once "Services/Cron/classes/class.ilCronJobResult.php";
235 
236  // systems
238  foreach($data as $idx => $item)
239  {
240  $job = ilCronManager::getJobInstance($item["job_id"],
241  $item["component"], $item["class"], $item["path"]);
242  if($job)
243  {
244  $data[$idx] = $this->parseJobToData($item, $job);
245  }
246  }
247 
248  // plugins
249  $lng->loadLanguageModule("cmps");
250  foreach(ilCronManager::getPluginJobs() as $item)
251  {
252  $job = $item[0];
253  $item = $item[1];
254 
255  $item["job_id"] = "pl__".$item["component"]."__".$job->getId();
256  $item["component"] = $lng->txt("cmps_plugin")."/".$item["component"];
257 
258  $data[] = $this->parseJobToData($item, $job);
259  }
260 
261  $this->setData($data);
262  }
263 
264  protected function fillRow($a_set)
265  {
266  global $ilCtrl, $lng;
267 
268  $this->tpl->setVariable("VAL_ID", $a_set["title"]);
269  $this->tpl->setVariable("VAL_JID", $a_set["job_id"]);
270 
271  if($a_set["description"])
272  {
273  $this->tpl->setVariable("VAL_DESC", $a_set["description"]);
274  }
275 
276  $this->tpl->setVariable("VAL_COMPONENT", $a_set["component"]);
277  $this->tpl->setVariable("VAL_SCHEDULE", $a_set["schedule"]);
278  $this->tpl->setVariable("VAL_STATUS", $a_set["status"]);
279  $this->tpl->setVariable("VAL_STATUS_INFO", $a_set["status_info"]);
280  $this->tpl->setVariable("VAL_RESULT", $a_set["result"]);
281  $this->tpl->setVariable("VAL_RESULT_INFO", $a_set["result_info"]);
282  if($a_set["last_run"] > time())
283  {
284  $a_set["last_run"] = $lng->txt("cron_running_since")." ".
285  ilDatePresentation::formatDate(new ilDateTime($a_set["running_ts"], IL_CAL_UNIX));
286 
287  // job has pinged
288  if($a_set["alive_ts"] != $a_set["running_ts"])
289  {
290  $a_set["last_run"] .= "<br />(Ping: ".
291  ilDatePresentation::formatDate(new ilDateTime($a_set["alive_ts"], IL_CAL_UNIX)).")";
292  }
293  }
294  else if($a_set["last_run"])
295  {
296  $a_set["last_run"] = ilDatePresentation::formatDate(new ilDateTime($a_set["last_run"], IL_CAL_UNIX));
297  }
298  $this->tpl->setVariable("VAL_LAST_RUN", $a_set["last_run"] ? $a_set["last_run"] : "-");
299 
300 
301  // actions
302 
303  $actions = array();
304 
305  if(!$a_set["running_ts"])
306  {
307  // reset
308  if($a_set["job_result_status"] == ilCronJobResult::STATUS_CRASHED)
309  {
310  $actions[] = "reset";
311  }
312  // activate
313  else if(!$a_set["job_status"])
314  {
315  $actions[] = "activate";
316  }
317  // deactivate
318  else
319  {
320  if($a_set['is_manually_executable'])
321  {
322  $actions[] = 'run';
323  }
324  $actions[] = "deactivate";
325  }
326  // edit (schedule)
327  if($a_set["editable_schedule"] || $a_set["has_settings"])
328  {
329  $actions[] = "edit";
330  }
331 
332  $ilCtrl->setParameter($this->getParentObject(), "jid", $a_set["job_id"]);
333 
334  foreach($actions as $action)
335  {
336  $this->tpl->setCurrentBlock("action_bl");
337  $this->tpl->setVariable("URL_ACTION",
338  $ilCtrl->getLinkTarget($this->getParentObject(), $action));
339  $this->tpl->setVariable("TXT_ACTION", $lng->txt("cron_action_".$action));
340  $this->tpl->parseCurrentBlock();
341  }
342 
343  $ilCtrl->setParameter($this->getParentObject(), "jid", "");
344  }
345  }
346 }
347 
348 ?>