19 declare(strict_types=1);
31 $last_year = (
int) $last_run->format(
'Y');
32 $now_year = (
int) $now->format(
'Y');
34 if ($last_year > $now_year) {
39 $last_week = $last_run->format(
'W');
40 $now_week = $now->format(
'W');
42 if ($last_week !== $now_week) {
49 $last_month = (
int) $last_run->format(
'm');
50 $now_month = (
int) $now->format(
'm');
52 $is_within_same_week_in_same_year = ($last_year .
'-' . $last_week) === ($now_year .
'-' . $now_week);
53 if ($is_within_same_week_in_same_year) {
55 return $last_month !== $now_month && $now->diff($last_run)->d > 7;
58 if ($now_year - $last_year > 1) {
64 return $last_month === $now_month;
69 if (null === $schedule_type) {
73 if (null === $last_run) {
77 if ($this->date_time_provider === null) {
83 switch ($schedule_type) {
84 case CronJobScheduleType::SCHEDULE_TYPE_DAILY:
85 $last = $last_run->format(
'Y-m-d');
86 $ref = $now->format(
'Y-m-d');
87 return ($last !== $ref);
89 case CronJobScheduleType::SCHEDULE_TYPE_WEEKLY:
92 case CronJobScheduleType::SCHEDULE_TYPE_MONTHLY:
93 $last = $last_run->format(
'Y-n');
94 $ref = $now->format(
'Y-n');
95 return ($last !== $ref);
97 case CronJobScheduleType::SCHEDULE_TYPE_QUARTERLY:
98 $last = $last_run->format(
'Y') .
'-' . ceil(((
int) $last_run->format(
'n')) / 3);
99 $ref = $now->format(
'Y') .
'-' . ceil(((
int) $now->format(
'n')) / 3);
100 return ($last !== $ref);
103 $last = $last_run->format(
'Y');
104 $ref = $now->format(
'Y');
105 return ($last !== $ref);
107 case CronJobScheduleType::SCHEDULE_TYPE_IN_MINUTES:
108 $diff = floor(($now->getTimestamp() - $last_run->getTimestamp()) / 60);
109 return ($diff >= $schedule_value);
111 case CronJobScheduleType::SCHEDULE_TYPE_IN_HOURS:
112 $diff = floor(($now->getTimestamp() - $last_run->getTimestamp()) / (60 * 60));
113 return ($diff >= $schedule_value);
115 case CronJobScheduleType::SCHEDULE_TYPE_IN_DAYS:
116 $diff = floor(($now->getTimestamp() - $last_run->getTimestamp()) / (60 * 60 * 24));
117 return ($diff >= $schedule_value);
128 if ($date_time_provider !== null) {
130 $return_type =
$r->getReturnType();
131 if ($return_type !== null) {
132 $return_type = $return_type->getName();
134 $expected_type = DateTimeInterface::class;
135 if (!is_subclass_of($return_type, $expected_type)) {
137 'The return type of the datetime provider must be of type %s',
143 $parameters =
$r->getParameters();
144 if ($parameters !== []) {
146 'The datetime provider must not define any parameters',
157 ?
int $schedule_value,
158 bool $is_manually_executed =
false 160 if ($is_manually_executed) {
169 return $this->
checkSchedule($last_run, $schedule_type, $schedule_value);
206 $this->schedule_type = $a_type;
207 $this->schedule_value = $a_value;
217 return CronJobScheduleType::cases();
226 CronJobScheduleType::SCHEDULE_TYPE_IN_MINUTES,
227 CronJobScheduleType::SCHEDULE_TYPE_IN_HOURS,
228 CronJobScheduleType::SCHEDULE_TYPE_IN_DAYS,
272 abstract public function getId(): string;
274 abstract public function getTitle(): string;
activationWasToggled(ilDBInterface $db, ilSetting $setting, bool $a_currently_active)
Important: This method is (also) called from the setup process, where the constructor of an ilCronJob...
checkSchedule(?DateTimeImmutable $last_run, ?CronJobScheduleType $schedule_type, ?int $schedule_value)
getValidScheduleTypes()
Returns a collection of all valid schedule types for a specific job.
setDateTimeProvider(?Closure $date_time_provider)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getAllScheduleTypes()
Get all available schedule types.
isDue(?DateTimeImmutable $last_run, ?CronJobScheduleType $schedule_type, ?int $schedule_value, bool $is_manually_executed=false)
addCustomSettingsToForm(ilPropertyFormGUI $a_form)
CronJobScheduleType $schedule_type
addToExternalSettingsForm(int $a_form_id, array &$a_fields, bool $a_is_active)
getScheduleTypesWithValues()
Closure $date_time_provider
setSchedule(?CronJobScheduleType $a_type, ?int $a_value)
Update current schedule (if flexible)
getDefaultScheduleValue()
saveCustomSettings(ilPropertyFormGUI $a_form)
getScheduleValue()
Get current schedule value (if flexible)
hasAutoActivation()
Is to be activated on "installation", does only work for ILIAS core cron jobs.
getScheduleType()
Get current schedule type (if flexible)
checkWeeklySchedule(DateTimeImmutable $last_run, DateTimeImmutable $now)