ILIAS  trunk Revision v11.0_alpha-1689-g66c127b4ae8
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilStudyProgrammeMailTemplateContext.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 {
25  public const ID = 'prg_context_manual';
26 
27  private const TITLE = "prg_title";
28  private const DESCRIPTION = "prg_description";
29  private const TYPE = "prg_type";
30  private const LINK = "prg_link";
31  private const ORG_UNIT = "prg_orgus";
32  private const STATUS = "prg_status";
33  private const COMPLETION_DATE = "prg_completion_date";
34  private const COMPLETED_BY = "prg_completion_by";
35  private const POINTS_REQUIRED = "prg_points_required";
36  private const POINTS_CURRENT = "prg_points_current";
37  private const DEADLINE = "prg_deadline";
38  private const EXPIRE_DATE = "prg_expire_date";
39  private const VALIDITY = "prg_validity";
40 
41  private const DATE_FORMAT = 'd.m.Y';
42 
43  protected ilLanguage $lng;
44 
45  public function __construct(
48  ?ilMailUserHelper $usernameHelper = null,
50  ) {
53  $envHelper,
54  $usernameHelper,
56  );
57 
58  global $DIC;
59 
60  $this->lng = $DIC['lng'];
61  $this->lng->loadLanguageModule('prg');
62  }
63 
64  public function getId(): string
65  {
66  return self::ID;
67  }
68 
69  public function getTitle(): string
70  {
71  return $this->lng->txt('prg_mail_context_title');
72  }
73 
74  public function getDescription(): string
75  {
76  return $this->lng->txt('prg_mail_context_info');
77  }
78 
82  public function getSpecificPlaceholders(): array
83  {
84  $placeholders = [];
85 
86  $placeholders[self::TITLE] = [
87  'placeholder' => 'STUDY_PROGRAMME_TITLE',
88  'label' => $this->lng->txt(self::TITLE)
89  ];
90 
91  $placeholders[self::DESCRIPTION] = [
92  'placeholder' => 'STUDY_PROGRAMME_DESCRIPTION',
93  'label' => $this->lng->txt(self::DESCRIPTION)
94  ];
95 
96  $placeholders[self::TYPE] = [
97  'placeholder' => 'STUDY_PROGRAMME_TYPE',
98  'label' => $this->lng->txt(self::TYPE)
99  ];
100 
101  $placeholders[self::LINK] = [
102  'placeholder' => 'STUDY_PROGRAMME_LINK',
103  'label' => $this->lng->txt(self::LINK)
104  ];
105 
106  $placeholders[self::ORG_UNIT] = [
107  'placeholder' => 'STUDY_PROGRAMME_ORG_UNITS',
108  'label' => $this->lng->txt(self::ORG_UNIT)
109  ];
110 
111  $placeholders[self::STATUS] = [
112  'placeholder' => 'STUDY_PROGRAMME_STATUS',
113  'label' => $this->lng->txt(self::STATUS)
114  ];
115 
116  $placeholders[self::COMPLETION_DATE] = [
117  'placeholder' => 'STUDY_PROGRAMME_COMPLETION_DATE',
118  'label' => $this->lng->txt(self::COMPLETION_DATE)
119  ];
120 
121  $placeholders[self::COMPLETED_BY] = [
122  'placeholder' => 'STUDY_PROGRAMME_COMPLETED_BY',
123  'label' => $this->lng->txt(self::COMPLETED_BY)
124  ];
125 
126  $placeholders[self::POINTS_REQUIRED] = [
127  'placeholder' => 'STUDY_PROGRAMME_POINTS_REQUIRED',
128  'label' => $this->lng->txt(self::POINTS_REQUIRED)
129  ];
130 
131  $placeholders[self::POINTS_CURRENT] = [
132  'placeholder' => 'STUDY_PROGRAMME_POINTS_CURRENT',
133  'label' => $this->lng->txt(self::POINTS_CURRENT)
134  ];
135 
136  $placeholders[self::DEADLINE] = [
137  'placeholder' => 'STUDY_PROGRAMME_DEADLINE',
138  'label' => $this->lng->txt(self::DEADLINE)
139  ];
140 
141  $placeholders[self::EXPIRE_DATE] = [
142  'placeholder' => 'STUDY_PROGRAMME_EXPIRE_DATE',
143  'label' => $this->lng->txt(self::EXPIRE_DATE)
144  ];
145 
146  $placeholders[self::VALIDITY] = [
147  'placeholder' => 'STUDY_PROGRAMME_VALIDITY',
148  'label' => $this->lng->txt(self::VALIDITY)
149  ];
150 
151  return $placeholders;
152  }
153 
157  public function resolveSpecificPlaceholder(
158  string $placeholder_id,
159  array $context_parameters,
160  ?ilObjUser $recipient = null
161  ): string {
162  if (is_null($recipient)) {
163  return '';
164  }
165 
166  if (!in_array($placeholder_id, [
167  self::TITLE,
168  self::DESCRIPTION,
169  self::TYPE,
170  self::LINK,
171  self::ORG_UNIT,
172  self::STATUS,
173  self::COMPLETION_DATE,
174  self::COMPLETED_BY,
175  self::POINTS_REQUIRED,
176  self::POINTS_CURRENT,
177  self::DEADLINE,
178  self::EXPIRE_DATE,
179  self::VALIDITY
180  ])) {
181  return '';
182  }
183 
185  $prg = ilObjectFactory::getInstanceByRefId((int) $context_parameters['ref_id']);
186  $assignments = $prg->getAssignmentsOfSingleProgramForUser($recipient->getId());
187  $latest = $this->getLatestAssignment($assignments);
188  $latest_successful = $this->getLatestSuccessfulAssignment($assignments);
189 
190  switch ($placeholder_id) {
191  case self::TITLE:
192  $string = $prg->getTitle();
193  break;
194  case self::DESCRIPTION:
195  $string = $prg->getDescription();
196  break;
197  case self::TYPE:
198  $string = '';
199  if (!is_null($prg->getSubType())) {
200  $string = $prg->getSubType()->getTitle();
201  }
202  break;
203  case self::LINK:
204  $string = ilLink::_getLink((int) $context_parameters['ref_id'], 'prg') . ' ';
205  break;
206  case self::ORG_UNIT:
207  $string = ilObjUser::lookupOrgUnitsRepresentation($recipient->getId());
208  break;
209  case self::STATUS:
210  $string = $this->statusToRepr($latest->getProgressTree()->getStatus(), $recipient->getLanguage());
211  break;
212  case self::COMPLETION_DATE:
213  $string = $this->date2String($latest->getProgressTree()->getCompletionDate(), $recipient);
214  break;
215  case self::COMPLETED_BY:
216  $string = '';
217  $id = $latest->getProgressTree()->getCompletionBy();
218  if (!is_null($id) && ilObject::_exists($id)) {
220  if ($obj->getType() === 'usr') {
221  $string = ilObjUser::_lookupLogin($id);
223  if (
224  ilObject::_exists($ref_id, true) &&
226  ) {
228  }
229  }
230  }
231  break;
232  case self::POINTS_REQUIRED:
233  $string = (string) $latest->getProgressTree()->getAmountOfPoints();
234  break;
235  case self::POINTS_CURRENT:
236  $string = (string) $latest->getProgressTree()->getCurrentAmountOfPoints();
237  break;
238  case self::DEADLINE:
239  $string = $latest->getProgressTree()->isInProgress() ?
240  $this->date2String($latest->getProgressTree()->getDeadline(), $recipient) : '';
241 
242  break;
243  case self::VALIDITY:
244  $string = '-';
245  if ($latest_successful) {
246  $langvar = $latest_successful->getProgressTree()->isInvalidated() ? 'prg_not_valid' : 'prg_still_valid';
247  $string = $this->lng->txtlng('prg', $langvar, $recipient->getLanguage());
248  }
249  break;
250 
251  case self::EXPIRE_DATE:
252  $string = '-';
253  if ($latest_successful) {
254  $string = $this->date2String($latest_successful->getProgressTree()->getValidityOfQualification(), $recipient);
255  }
256  break;
257  default:
258  throw new \Exception("cannot resolve placeholder: " . $placeholder_id);
259  $string = '';
260  }
261 
262  return $string;
263  }
264 
265  protected function getLatestAssignment(array $assignments): ilPRGAssignment
266  {
267  usort($assignments, static function (ilPRGAssignment $a, ilPRGAssignment $b): int {
268  $a_dat = $a->getProgressTree()->getAssignmentDate();
269  $b_dat = $b->getProgressTree()->getAssignmentDate();
270  if ($a_dat > $b_dat) {
271  return -1;
272  } elseif ($a_dat < $b_dat) {
273  return 1;
274  } else {
275  return 0;
276  }
277  });
278  return array_shift($assignments);
279  }
280 
281  protected function getLatestSuccessfulAssignment(array $assignments): ?ilPRGAssignment
282  {
283  $successful = array_filter(
284  $assignments,
285  fn($ass) => $ass->getProgressTree()->isSuccessful()
286  );
287  if (count($successful) === 0) {
288  return null;
289  }
290 
291  usort($successful, static function (ilPRGAssignment $a, ilPRGAssignment $b): int {
292  $a_dat = $a->getProgressTree()->getCompletionDate();
293  $b_dat = $b->getProgressTree()->getCompletionDate();
294  if ($a_dat > $b_dat) {
295  return -1;
296  } elseif ($a_dat < $b_dat) {
297  return 1;
298  } else {
299  return 0;
300  }
301  });
302  return array_shift($successful);
303  }
304 
305 
306 
307  protected function statusToRepr(int $status, string $lang): string
308  {
309  if ($status === ilPRGProgress::STATUS_IN_PROGRESS) {
310  return $this->lng->txtlng('prg', 'prg_status_in_progress', $lang);
311  }
312  if ($status === ilPRGProgress::STATUS_COMPLETED) {
313  return $this->lng->txtlng('prg', 'prg_status_completed', $lang);
314  }
315  if ($status === ilPRGProgress::STATUS_ACCREDITED) {
316  return $this->lng->txtlng('prg', 'prg_status_accredited', $lang);
317  }
318  if ($status === ilPRGProgress::STATUS_NOT_RELEVANT) {
319  return $this->lng->txtlng('prg', 'prg_status_not_relevant', $lang);
320  }
321  if ($status === ilPRGProgress::STATUS_FAILED) {
322  return $this->lng->txtlng('prg', 'prg_status_failed', $lang);
323  }
324 
325  throw new ilException("Unknown status: '$status'");
326  }
327 
328  protected function date2String(
329  ?DateTimeImmutable $date_time = null,
330  ?ilObjUser $user = null
331  ): string {
332  if (is_null($date_time)) {
333  return '';
334  }
335  if (is_null($user)) {
336  return $date_time->format(self::DATE_FORMAT);
337  }
338  return $user->getDateFormat()->applyTo($date_time);
339  }
340 }
static _lookupTitle(int $obj_id)
OrgUnitUserService $orgUnitUserService
date2String(?DateTimeImmutable $date_time=null, ?ilObjUser $user=null)
Class ilMailEnvironmentHelper.
static lookupOrgUnitsRepresentation(int $a_usr_id)
lookup org unit representation
Class ilMailUserHelper.
ilMailLanguageHelper $languageHelper
__construct(?OrgUnitUserService $orgUnitUserService=null, ?ilMailEnvironmentHelper $envHelper=null, ?ilMailUserHelper $usernameHelper=null, ?ilMailLanguageHelper $languageHelper=null)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
$ref_id
Definition: ltiauth.php:65
Class ilMailLanguageHelper.
static getInstanceByRefId(int $ref_id, bool $stop_on_error=true)
get an instance of an Ilias object by reference id
global $DIC
Definition: shib_login.php:22
$lang
Definition: xapiexit.php:25
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
resolveSpecificPlaceholder(string $placeholder_id, array $context_parameters, ?ilObjUser $recipient=null)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
__construct(Container $dic, ilPlugin $plugin)
static _lookupTargetRefId(int $a_obj_id)
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
ilMailEnvironmentHelper $envHelper
Assignments are relations of users to a PRG; They hold progress-information for (sub-)nodes of the PR...
static _lookupDeletedDate(int $ref_id)
static _lookupLogin(int $a_user_id)