ILIAS  release_8 Revision v8.24
class.ilStudyProgrammePlaceholderValues.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
28
29 public function __construct(
30 ?ilDefaultPlaceholderValues $defaultPlaceholderValues = null,
31 ?ilLanguage $language = null,
35 ) {
36 if (null === $language) {
37 global $DIC;
38 $language = $DIC->language();
39 $language->loadLanguageModule('certificate');
40 }
41 $this->language = $language;
42
43 if (null === $defaultPlaceholderValues) {
44 $defaultPlaceholderValues = new ilDefaultPlaceholderValues();
45 }
46
47 if (null === $objectHelper) {
49 }
50 $this->objectHelper = $objectHelper;
51
52 if (null === $participantsHelper) {
54 }
55 $this->participantsHelper = $participantsHelper;
56
57 if (null === $ilUtilHelper) {
59 }
60 $this->ilUtilHelper = $ilUtilHelper;
61
62 $this->defaultPlaceholderValuesObject = $defaultPlaceholderValues;
63 }
64
78 public function getPlaceholderValues(int $userId, int $objId): array
79 {
80 $object = $this->objectHelper->getInstanceByObjId($objId);
81 $placeholders = $this->defaultPlaceholderValuesObject->getPlaceholderValues($userId, $objId);
82 $assignments = $object->getAssignmentsOfSingleProgramForUser($userId);
83 $latest_progress = $this->getRelevantProgressFromAssignments($assignments);
84 $type = $object->getSubType();
85
86 $placeholders['PRG_TITLE'] = ilLegacyFormElementsUtil::prepareFormOutput($object->getTitle());
87 $placeholders['PRG_DESCRIPTION'] = ilLegacyFormElementsUtil::prepareFormOutput($object->getDescription());
88 $placeholders['PRG_TYPE'] = ilLegacyFormElementsUtil::prepareFormOutput($type ? $type->getTitle() : '');
89 $placeholders['PRG_POINTS'] = ilLegacyFormElementsUtil::prepareFormOutput(
90 $latest_progress ? (string) $latest_progress->getCurrentAmountOfPoints() : ''
91 );
92 $placeholders['PRG_COMPLETION_DATE'] = ilLegacyFormElementsUtil::prepareFormOutput(
93 $latest_progress ? $latest_progress->getCompletionDate()->format('d.m.Y') : ''
94 );
95 $placeholders['PRG_EXPIRES_AT'] = ilLegacyFormElementsUtil::prepareFormOutput(
96 $latest_progress && $latest_progress->getValidityOfQualification() ?
97 $latest_progress->getValidityOfQualification()->format('d.m.Y') : ''
98 );
99 return $placeholders;
100 }
101
110 public function getPlaceholderValuesForPreview(int $userId, int $objId): array
111 {
112 $placeholders = $this->defaultPlaceholderValuesObject->getPlaceholderValuesForPreview($userId, $objId);
113
114 $object = $this->objectHelper->getInstanceByObjId($objId);
115 $type = $object->getSubType();
116 $today = ilLegacyFormElementsUtil::prepareFormOutput((new DateTime())->format('d.m.Y'));
117 $placeholders['PRG_TITLE'] = ilLegacyFormElementsUtil::prepareFormOutput($object->getTitle());
118 $placeholders['PRG_DESCRIPTION'] = ilLegacyFormElementsUtil::prepareFormOutput($object->getDescription());
119 $placeholders['PRG_TYPE'] = ilLegacyFormElementsUtil::prepareFormOutput($type ? $type->getTitle() : '');
120 $placeholders['PRG_POINTS'] = ilLegacyFormElementsUtil::prepareFormOutput((string) $object->getPoints());
121 $placeholders['PRG_COMPLETION_DATE'] = $today;
122 $placeholders['PRG_EXPIRES_AT'] = $today;
123 return $placeholders;
124 }
125
126 protected function getRelevantProgressFromAssignments(array $assignments): ?ilPRGProgress
127 {
128 if (count($assignments) === 0) {
129 return null;
130 }
131 $latest_progress = null;
132 $latest_successful = $this->getLatestSuccessfulAssignment($assignments);
133 if ($latest_successful) {
134 $latest_progress = $latest_successful->getProgressTree();
135 }
136 return $latest_progress;
137 }
138
139 protected function getLatestSuccessfulAssignment(array $assignments): ?ilPRGAssignment
140 {
141 $successful = array_filter(
142 $assignments,
143 fn ($ass) => $ass->getProgressTree()->isSuccessful()
144 );
145 if (count($successful) === 0) {
146 return null;
147 }
148
149 //is there an unlimited validity? if so, take latest.
150 $unlimited = array_filter(
151 $successful,
152 fn ($ass) => is_null($ass->getProgressTree()->getValidityOfQualification())
153 );
154 if (count($unlimited) > 0) {
155 $successful = $unlimited;
156 usort($successful, static function (ilPRGAssignment $a, ilPRGAssignment $b): int {
157 $a_dat = $a->getProgressTree()->getCompletionDate();
158 $b_dat = $b->getProgressTree()->getCompletionDate();
159 if ($a_dat > $b_dat) {
160 return -1;
161 } elseif ($a_dat < $b_dat) {
162 return 1;
163 } else {
164 return 0;
165 }
166 });
167 } else {
168 //there are (only) limited validities: take the one that lasts the longest.
169 $limited = array_filter(
170 $successful,
171 fn ($ass) => !is_null($ass->getProgressTree()->getValidityOfQualification())
172 );
173 $successful = $limited;
174 usort($successful, static function (ilPRGAssignment $a, ilPRGAssignment $b): int {
175 $a_dat = $a->getProgressTree()->getValidityOfQualification();
176 $b_dat = $b->getProgressTree()->getValidityOfQualification();
177 if ($a_dat > $b_dat) {
178 return -1;
179 } elseif ($a_dat < $b_dat) {
180 return 1;
181 } else {
182 return 0;
183 }
184 });
185 }
186 return array_shift($successful);
187 }
188}
Just a wrapper class to create Unit Test for other classes.
Collection of basic placeholder values that can be used.
language handling
loadLanguageModule(string $a_module)
Load language module.
static prepareFormOutput($a_str, bool $a_strip=false)
Assignments are relations of users to a PRG; They hold progress-information for (sub-)nodes of the PR...
A Progress is the status of a user on a single node of an assignment; it is unique by assignment_id:u...
getPlaceholderValues(int $userId, int $objId)
This method MUST return an array that contains the actual data for the given user of the given object...
__construct(?ilDefaultPlaceholderValues $defaultPlaceholderValues=null, ?ilLanguage $language=null, ?ilCertificateObjectHelper $objectHelper=null, ?ilCertificateParticipantsHelper $participantsHelper=null, ?ilCertificateUtilHelper $ilUtilHelper=null)
getPlaceholderValuesForPreview(int $userId, int $objId)
This method is different then the 'getPlaceholderValues' method, this method is used to create a plac...
global $DIC
Definition: feed.php:28
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$type
$objId
Definition: xapitoken.php:57