ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilPrgRestartAssignmentsCronJob.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
29 {
30  private const ID = 'prg_restart_assignments_temporal_progress';
32 
34  protected ilLanguage $lng;
37 
38  protected array $prgs = [];
39 
40  public function __construct()
41  {
42  global $DIC;
43  $this->log = $DIC['ilLog'];
44  $this->lng = $DIC['lng'];
45  $this->lng->loadLanguageModule('prg');
46 
48  $this->assignment_repo = $dic['repo.assignment'];
49  $this->adapter = $dic['cron.restart'];
50  }
51 
52  public function getTitle(): string
53  {
54  return $this->lng->txt('prg_restart_assignments_temporal_progress_title');
55  }
56 
57  public function getDescription(): string
58  {
59  return $this->lng->txt('prg_restart_assignments_temporal_progress_desc');
60  }
61 
62  public function getId(): string
63  {
64  return self::ID;
65  }
66 
67  public function hasAutoActivation(): bool
68  {
69  return true;
70  }
71 
72  public function hasFlexibleSchedule(): bool
73  {
74  return true;
75  }
76 
78  {
79  return CronJobScheduleType::SCHEDULE_TYPE_IN_DAYS;
80  }
81 
82  public function getDefaultScheduleValue(): ?int
83  {
84  return 1;
85  }
86 
87  public function run(): ilCronJobResult
88  {
89  $result = new ilCronJobResult();
90  $result->setStatus(ilCronJobResult::STATUS_NO_ACTION);
91 
92  $programmes_to_reassign = $this->adapter->getRelevantProgrammeIds();
93  if (count($programmes_to_reassign) === 0) {
94  return $result;
95  }
96 
97  $today = $this->getNow();
98  $programmes_and_due = [];
99 
100  foreach ($programmes_to_reassign as $programme_obj_id => $days_offset) {
101  $interval = new DateInterval('P' . $days_offset . 'D');
102  $due = $today->add($interval);
103  $programmes_and_due[$programme_obj_id] = $due;
104  }
105 
106  $assignments = $this->assignment_repo->getAboutToExpire($programmes_and_due, false);
107 
108  if (count($assignments) === 0) {
109  return $result;
110  }
111 
112  foreach ($assignments as $ass) {
113  if ($ass->getRestartedAssignmentId() < 0) {
114  $prg = $this->getStudyProgramme($ass->getRootId());
115 
116  $restart_settings = $prg->getSettings()->getValidityOfQualificationSettings();
117  if ($restart_settings->getRestartRecheck()
118  && !$ass->isManuallyAssigned()
119  && !$prg->getApplicableMembershipSourceForUser($ass->getUserId(), null)
120  ) {
121  continue;
122  }
123 
124  $this->log(
125  sprintf(
126  'PRG, RestartAssignments: user %s\'s assignment %s is being restarted (Programme %s)',
127  $ass->getUserId(),
128  $ass->getId(),
129  $ass->getRootId()
130  )
131  );
132 
133  $restarted = $prg->assignUser($ass->getUserId(), self::ACTING_USR_ID, false);
134  $ass = $ass->withRestarted($restarted->getId(), $today);
135  $this->assignment_repo->store($ass);
136 
137  $this->adapter->actOnSingleAssignment($restarted);
138 
139  $result->setStatus(ilCronJobResult::STATUS_OK);
140  }
141  }
142 
143  return $result;
144  }
145 
146  protected function getStudyProgramme(int $prg_obj_id): ilObjStudyProgramme
147  {
148  if (!array_key_exists($prg_obj_id, $this->prgs)) {
149  $this->prgs[$prg_obj_id] = ilObjStudyProgramme::getInstanceByObjId($prg_obj_id);
150  }
151  return $this->prgs[$prg_obj_id];
152  }
153 
154  protected function getNow(): DateTimeImmutable
155  {
156  return new DateTimeImmutable();
157  }
158 
159  protected function log(string $msg): void
160  {
161  $this->log->write($msg);
162  }
163 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Component logger with individual log levels by component id.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
final const STATUS_NO_ACTION
Re-assign users (according to restart-date).
static getInstanceByObjId(int $obj_id)
Assignments are relations of users to a PRG; They hold progress-information for (sub-)nodes of the PR...
$dic
Definition: result.php:32