ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilCronJob.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
11 abstract class ilCronJob
12 {
21 
22  //
23  // SCHEDULE
24  //
25 
35  public function isActive($a_ts_last_run, $a_schedule_type, $a_schedule_value, $a_manual = false)
36  {
37  if ($a_manual) {
38  return true;
39  }
40  if (!$this->hasFlexibleSchedule()) {
41  $a_schedule_type = $this->getDefaultScheduleType();
42  $a_schedule_value = $this->getDefaultScheduleValue();
43  }
44  return $this->checkSchedule($a_ts_last_run, $a_schedule_type, $a_schedule_value);
45  }
46 
52  public function getScheduleType()
53  {
54  if ($this->hasFlexibleSchedule() && $this->schedule_type) {
55  return $this->schedule_type;
56  }
57  }
58 
64  public function getScheduleValue()
65  {
66  if ($this->hasFlexibleSchedule() && $this->schedule_value) {
67  return $this->schedule_value;
68  }
69  }
70 
77  public function setSchedule($a_type, $a_value)
78  {
79  if ($this->hasFlexibleSchedule() &&
80  in_array($a_type, $this->getValidScheduleTypes()) &&
81  $a_value) {
82  $this->schedule_type = $a_type;
83  $this->schedule_value = $a_value;
84  }
85  }
86 
91  public function getAllScheduleTypes()
92  {
93  return array(
94  self::SCHEDULE_TYPE_DAILY,
95  self::SCHEDULE_TYPE_WEEKLY,
96  self::SCHEDULE_TYPE_MONTHLY,
97  self::SCHEDULE_TYPE_QUARTERLY,
98  self::SCHEDULE_TYPE_YEARLY,
99  self::SCHEDULE_TYPE_IN_MINUTES,
100  self::SCHEDULE_TYPE_IN_HOURS,
101  self::SCHEDULE_TYPE_IN_DAYS
102  );
103  }
104 
108  public function getScheduleTypesWithValues()
109  {
110  return [
114  ];
115  }
116 
121  public function getValidScheduleTypes()
122  {
123  return $this->getAllScheduleTypes();
124  }
125 
126  /*
127  * Check if next run is due
128  *
129  * @param timestamp $a_ts_last_run
130  * @param integer $a_schedule_type
131  * @param integer $a_schedule_value
132  * @return boolean
133  */
134  protected function checkSchedule($a_ts_last_run, $a_schedule_type, $a_schedule_value)
135  {
136  if (!$a_schedule_type) {
137  return false;
138  }
139  if (!$a_ts_last_run) {
140  return true;
141  }
142 
143  $now = time();
144 
145  switch ($a_schedule_type) {
146  case self::SCHEDULE_TYPE_DAILY:
147  $last = date("Y-m-d", $a_ts_last_run);
148  $ref = date("Y-m-d", $now);
149  return ($last != $ref);
150 
151  case self::SCHEDULE_TYPE_WEEKLY:
152  $last = date("Y-W", $a_ts_last_run);
153  $ref = date("Y-W", $now);
154  return ($last != $ref);
155 
156  case self::SCHEDULE_TYPE_MONTHLY:
157  $last = date("Y-n", $a_ts_last_run);
158  $ref = date("Y-n", $now);
159  return ($last != $ref);
160 
161  case self::SCHEDULE_TYPE_QUARTERLY:
162  $last = date("Y", $a_ts_last_run) . "-" . ceil(date("n", $a_ts_last_run) / 3);
163  $ref = date("Y", $now) . "-" . ceil(date("n", $now) / 3);
164  return ($last != $ref);
165 
166  case self::SCHEDULE_TYPE_YEARLY:
167  $last = date("Y", $a_ts_last_run);
168  $ref = date("Y", $now);
169  return ($last != $ref);
170 
171  case self::SCHEDULE_TYPE_IN_MINUTES:
172  $diff = floor(($now - $a_ts_last_run) / 60);
173  return ($diff >= $a_schedule_value);
174 
175  case self::SCHEDULE_TYPE_IN_HOURS:
176  $diff = floor(($now - $a_ts_last_run) / (60 * 60));
177  return ($diff >= $a_schedule_value);
178 
179  case self::SCHEDULE_TYPE_IN_DAYS:
180  $diff = floor(($now - $a_ts_last_run) / (60 * 60 * 24));
181  return ($diff >= $a_schedule_value);
182  }
183  }
184 
185 
186  //
187  // TITLE / DESCRIPTION
188  //
189 
195  public function getTitle()
196  {
197  }
198 
204  public function getDescription()
205  {
206  }
207 
212  public function isManuallyExecutable()
213  {
214  return true;
215  }
216 
217  //
218  // SETTINGS
219  //
220 
226  public function hasCustomSettings()
227  {
228  return false;
229  }
230 
237  {
238  }
239 
246  public function saveCustomSettings(ilPropertyFormGUI $a_form)
247  {
248  return true;
249  }
250 
258  public function addToExternalSettingsForm($a_form_id, array &$a_fields, $a_is_active)
259  {
260  }
261 
262 
263  //
264  // HOOKS
265  //
266 
272  public function activationWasToggled($a_currently_active)
273  {
274  // we cannot use ilObject or any higher level construct here
275  // this may be called from setup, so it is limited to handling ilSetting/ilDB mostly
276  }
277 
278 
279  //
280  // ABSTRACT
281  //
282 
288  abstract public function getId();
289 
295  abstract public function hasAutoActivation();
296 
302  abstract public function hasFlexibleSchedule();
303 
309  abstract public function getDefaultScheduleType();
310 
316  abstract public function getDefaultScheduleValue();
317 
323  abstract public function run();
324 }
setSchedule($a_type, $a_value)
Update current schedule (if flexible)
run()
Run job.
getValidScheduleTypes()
Returns a collection of all valid schedule types for a specific job.
getAllScheduleTypes()
Get all available schedule types.
checkSchedule($a_ts_last_run, $a_schedule_type, $a_schedule_value)
This class represents a property form user interface.
addToExternalSettingsForm($a_form_id, array &$a_fields, $a_is_active)
Add external settings to form.
getId()
Get id.
Cron job application base class.
const SCHEDULE_TYPE_IN_MINUTES
getDescription()
Get description.
activationWasToggled($a_currently_active)
Cron job status was changed.
const SCHEDULE_TYPE_MONTHLY
const SCHEDULE_TYPE_WEEKLY
$a_type
Definition: workflow.php:92
getTitle()
Get title.
addCustomSettingsToForm(ilPropertyFormGUI $a_form)
Add custom settings to form.
hasFlexibleSchedule()
Can the schedule be configured?
isActive($a_ts_last_run, $a_schedule_type, $a_schedule_value, $a_manual=false)
Is job currently active?
const SCHEDULE_TYPE_IN_DAYS
getScheduleTypesWithValues()
hasCustomSettings()
Has cron job any custom setting which can be edited?
const SCHEDULE_TYPE_YEARLY
getDefaultScheduleType()
Get schedule type.
const SCHEDULE_TYPE_DAILY
getDefaultScheduleValue()
Get schedule value.
saveCustomSettings(ilPropertyFormGUI $a_form)
Save custom settings.
const SCHEDULE_TYPE_QUARTERLY
getScheduleValue()
Get current schedule value (if flexible)
const SCHEDULE_TYPE_IN_HOURS
hasAutoActivation()
Is to be activated on "installation".
isManuallyExecutable()
Defines whether or not a cron job can be started manually.
getScheduleType()
Get current schedule type (if flexible)