ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
CertificateSettingsCourseFormRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Course\Certificate;
22 
23 use ilTree;
24 use ilAccess;
25 use ilObject;
26 use ilSetting;
27 use ilLanguage;
28 use ilObjectLP;
29 use ilException;
30 use ilToolbarGUI;
31 use ilWACException;
32 use ilCtrlInterface;
33 use ilLPObjSettings;
48 
53 {
55  private readonly ilTree $tree;
56 
57  public function __construct(
58  private readonly ilObject $object,
59  string $certificatePath,
60  bool $hasAdditionalElements,
61  private readonly ilLanguage $language,
62  ilCtrlInterface $ctrl,
63  ilAccess $access,
64  ilToolbarGUI $toolbar,
65  ilCertificatePlaceholderDescription $placeholderDescriptionObject,
66  ?ilCertificateSettingsFormRepository $settingsFormFactory = null,
67  private readonly ilCertificateObjUserTrackingHelper $trackingHelper = new ilCertificateObjUserTrackingHelper(),
68  private readonly ilCertificateObjectHelper $objectHelper = new ilCertificateObjectHelper(),
69  private readonly ilCertificateObjectLPHelper $lpHelper = new ilCertificateObjectLPHelper(),
70  ?ilTree $tree = null,
71  private readonly ilSetting $setting = new ilSetting('crs')
72  ) {
73  global $DIC;
74 
75  $this->settingsFormFactory = $settingsFormFactory ?? new ilCertificateSettingsFormRepository(
76  $object->getId(),
77  $certificatePath,
78  $hasAdditionalElements,
79  $language,
80  $ctrl,
81  $access,
82  $toolbar,
83  $placeholderDescriptionObject,
84  $DIC->ui()->factory(),
85  $DIC->ui()->renderer()
86  );
87  $this->tree = $tree ?? $DIC->repositoryTree();
88  }
89 
98  public function createForm(ilCertificateGUI $certificateGUI): ilPropertyFormGUI
99  {
100  $form = $this->settingsFormFactory->createForm($certificateGUI);
101 
102  $objectLearningProgressSettings = new ilLPObjSettings($this->object->getId());
103 
104  $mode = $objectLearningProgressSettings->getMode();
105  if (!$this->trackingHelper->enabledLearningProgress() || $mode === ilLPObjSettings::LP_MODE_DEACTIVATED) {
106  $subitems = new ilRepositorySelector2InputGUI($this->language->txt('objects'), 'subitems', true);
107 
108  $formSection = new ilFormSectionHeaderGUI();
109  $formSection->setTitle($this->language->txt('cert_form_sec_add_features'));
110  $form->addItem($formSection);
111 
112  $exp = $subitems->getExplorerGUI();
113  $exp->setSkipRootNode(true);
114  $exp->setRootId($this->object->getRefId());
115  $exp->setTypeWhiteList($this->getLPTypes($this->object->getRefId()));
116 
117  $objectHelper = $this->objectHelper;
118  $lpHelper = $this->lpHelper;
119  $subitems->setTitleModifier(function ($id) use ($objectHelper, $lpHelper): string {
120  if (null === $id) {
121  return '';
122  }
123  $obj_id = $objectHelper->lookupObjId((int) $id);
124  $olp = $lpHelper->getInstance($obj_id);
125 
126  $invalid_modes = $this->getInvalidLPModes();
127 
128  $mode = $olp->getModeText($olp->getCurrentMode());
129 
130  if (in_array($olp->getCurrentMode(), $invalid_modes, true)) {
131  $mode = '<strong>' . $mode . '</strong>';
132  }
133 
134  return $objectHelper->lookupTitle($obj_id) . ' (' . $mode . ')';
135  });
136 
137  $subitems->setRequired(true);
138  $form->addItem($subitems);
139  }
140 
141  return $form;
142  }
143 
147  public function save(array $formFields): void
148  {
149  $invalidModes = $this->getInvalidLPModes();
150 
151  $titlesOfObjectsWithInvalidModes = [];
152  $refIds = $formFields['subitems'] ?? [];
153 
154  foreach ($refIds as $refId) {
155  $objectId = $this->objectHelper->lookupObjId((int) $refId);
156  $learningProgressObject = $this->lpHelper->getInstance($objectId);
157  $currentMode = $learningProgressObject->getCurrentMode();
158  if (in_array($currentMode, $invalidModes, true)) {
159  $titlesOfObjectsWithInvalidModes[] = $this->objectHelper->lookupTitle($objectId);
160  }
161  }
162 
163  if ($titlesOfObjectsWithInvalidModes !== []) {
164  $message = sprintf(
165  $this->language->txt('certificate_learning_progress_must_be_active'),
166  implode(', ', $titlesOfObjectsWithInvalidModes)
167  );
168 
169  throw new ilException($message);
170  }
171 
172  $this->setting->set(
173  'cert_subitems_' . $this->object->getId(),
174  json_encode($formFields['subitems'] ?? [], JSON_THROW_ON_ERROR)
175  );
176  }
177 
181  public function fetchFormFieldData(string $content): array
182  {
183  $formFields = $this->settingsFormFactory->fetchFormFieldData($content);
184 
185  $formFields['subitems'] = json_decode($this->setting->get(
186  'cert_subitems_' . $this->object->getId(),
187  json_encode([], JSON_THROW_ON_ERROR)
188  ), true, 512, JSON_THROW_ON_ERROR);
189  if ($formFields['subitems'] === 'null' || $formFields['subitems'] === null) {
190  $formFields['subitems'] = [];
191  }
192 
193  return $formFields;
194  }
195 
199  private function getLPTypes(int $a_parent_ref_id): array
200  {
201  $result = [];
202 
203  $root = $this->tree->getNodeData($a_parent_ref_id);
204  $sub_items = $this->tree->getSubTree($root);
205  array_shift($sub_items); // remove root
206 
207  foreach ($sub_items as $node) {
208  if ($this->lpHelper->isSupportedObjectType($node['type'])) {
209  $class = $this->lpHelper->getTypeClass($node['type']);
211  $modes = $class::getDefaultModes($this->trackingHelper->enabledLearningProgress());
212 
213  if (count($modes) > 1) {
214  $result[] = $node['type'];
215  }
216  }
217  }
218 
219  return $result;
220  }
221 
225  private function getInvalidLPModes(): array
226  {
227  $invalid_modes = [
230  ];
231 
232  // without active LP the following modes cannot be supported
233  if (!$this->trackingHelper->enabledLearningProgress()) {
234  // status cannot be set without active LP
235  //$invalid_modes[] = ilLPObjSettings::LP_MODE_MANUAL;
236  $invalid_modes[] = ilLPObjSettings::LP_MODE_MANUAL_BY_TUTOR;
238 
239  // mode cannot be configured without active LP
240  $invalid_modes[] = ilLPObjSettings::LP_MODE_COLLECTION;
241  $invalid_modes[] = ilLPObjSettings::LP_MODE_COLLECTION_MOBS;
242  $invalid_modes[] = ilLPObjSettings::LP_MODE_COLLECTION_TLT;
243  $invalid_modes[] = ilLPObjSettings::LP_MODE_SCORM;
244  $invalid_modes[] = ilLPObjSettings::LP_MODE_VISITS; // ?
245  }
246 
247  return $invalid_modes;
248  }
249 }
$refId
Definition: xapitoken.php:58
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
ilSetting $setting
Definition: class.ilias.php:68
__construct(private readonly ilObject $object, string $certificatePath, bool $hasAdditionalElements, private readonly ilLanguage $language, ilCtrlInterface $ctrl, ilAccess $access, ilToolbarGUI $toolbar, ilCertificatePlaceholderDescription $placeholderDescriptionObject, ?ilCertificateSettingsFormRepository $settingsFormFactory=null, private readonly ilCertificateObjUserTrackingHelper $trackingHelper=new ilCertificateObjUserTrackingHelper(), private readonly ilCertificateObjectHelper $objectHelper=new ilCertificateObjectHelper(), private readonly ilCertificateObjectLPHelper $lpHelper=new ilCertificateObjectLPHelper(), ?ilTree $tree=null, private readonly ilSetting $setting=new ilSetting('crs'))
global $DIC
Definition: shib_login.php:22
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$message
Definition: xapiexit.php:31
language()
description: > Example for rendring a language glyph.
Definition: language.php:41