ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilCertificateSettingsCourseFormRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
24 
29 {
31  private readonly ilTree $tree;
32 
33  public function __construct(
34  private readonly ilObject $object,
35  string $certificatePath,
36  bool $hasAdditionalElements,
37  private readonly ilLanguage $language,
38  ilCtrlInterface $ctrl,
39  ilAccess $access,
40  ilToolbarGUI $toolbar,
41  ilCertificatePlaceholderDescription $placeholderDescriptionObject,
42  ?ilCertificateSettingsFormRepository $settingsFormFactory = null,
43  private readonly ilCertificateObjUserTrackingHelper $trackingHelper = new ilCertificateObjUserTrackingHelper(),
44  private readonly ilCertificateObjectHelper $objectHelper = new ilCertificateObjectHelper(),
45  private readonly ilCertificateObjectLPHelper $lpHelper = new ilCertificateObjectLPHelper(),
46  ?ilTree $tree = null,
47  private readonly ilSetting $setting = new ilSetting('crs')
48  ) {
49  global $DIC;
50 
51  $this->settingsFormFactory = $settingsFormFactory ?? new ilCertificateSettingsFormRepository(
52  $object->getId(),
53  $certificatePath,
54  $hasAdditionalElements,
55  $language,
56  $ctrl,
57  $access,
58  $toolbar,
59  $placeholderDescriptionObject,
60  $DIC->ui()->factory(),
61  $DIC->ui()->renderer()
62  );
63  $this->tree = $tree ?? $DIC->repositoryTree();
64  }
65 
74  public function createForm(ilCertificateGUI $certificateGUI): ilPropertyFormGUI
75  {
76  $form = $this->settingsFormFactory->createForm($certificateGUI);
77 
78  $objectLearningProgressSettings = new ilLPObjSettings($this->object->getId());
79 
80  $mode = $objectLearningProgressSettings->getMode();
81  if (!$this->trackingHelper->enabledLearningProgress() || $mode === ilLPObjSettings::LP_MODE_DEACTIVATED) {
82  $subitems = new ilRepositorySelector2InputGUI($this->language->txt('objects'), 'subitems', true);
83 
84  $formSection = new ilFormSectionHeaderGUI();
85  $formSection->setTitle($this->language->txt('cert_form_sec_add_features'));
86  $form->addItem($formSection);
87 
88  $exp = $subitems->getExplorerGUI();
89  $exp->setSkipRootNode(true);
90  $exp->setRootId($this->object->getRefId());
91  $exp->setTypeWhiteList($this->getLPTypes($this->object->getRefId()));
92 
93  $objectHelper = $this->objectHelper;
94  $lpHelper = $this->lpHelper;
95  $subitems->setTitleModifier(function ($id) use ($objectHelper, $lpHelper): string {
96  if (null === $id) {
97  return '';
98  }
99  $obj_id = $objectHelper->lookupObjId((int) $id);
100  $olp = $lpHelper->getInstance($obj_id);
101 
102  $invalid_modes = $this->getInvalidLPModes();
103 
104  $mode = $olp->getModeText($olp->getCurrentMode());
105 
106  if (in_array($olp->getCurrentMode(), $invalid_modes, true)) {
107  $mode = '<strong>' . $mode . '</strong>';
108  }
109  return $objectHelper->lookupTitle($obj_id) . ' (' . $mode . ')';
110  });
111 
112  $subitems->setRequired(true);
113  $form->addItem($subitems);
114  }
115 
116  return $form;
117  }
118 
122  public function save(array $formFields): void
123  {
124  $invalidModes = $this->getInvalidLPModes();
125 
126  $titlesOfObjectsWithInvalidModes = [];
127  $refIds = $formFields['subitems'] ?? [];
128 
129  foreach ($refIds as $refId) {
130  $objectId = $this->objectHelper->lookupObjId((int) $refId);
131  $learningProgressObject = $this->lpHelper->getInstance($objectId);
132  $currentMode = $learningProgressObject->getCurrentMode();
133  if (in_array($currentMode, $invalidModes, true)) {
134  $titlesOfObjectsWithInvalidModes[] = $this->objectHelper->lookupTitle($objectId);
135  }
136  }
137 
138  if ($titlesOfObjectsWithInvalidModes !== []) {
139  $message = sprintf(
140  $this->language->txt('certificate_learning_progress_must_be_active'),
141  implode(', ', $titlesOfObjectsWithInvalidModes)
142  );
143  throw new ilException($message);
144  }
145 
146  $this->setting->set(
147  'cert_subitems_' . $this->object->getId(),
148  json_encode($formFields['subitems'] ?? [], JSON_THROW_ON_ERROR)
149  );
150  }
151 
155  public function fetchFormFieldData(string $content): array
156  {
157  $formFields = $this->settingsFormFactory->fetchFormFieldData($content);
158 
159  $formFields['subitems'] = json_decode($this->setting->get(
160  'cert_subitems_' . $this->object->getId(),
161  json_encode([], JSON_THROW_ON_ERROR)
162  ), true, 512, JSON_THROW_ON_ERROR);
163  if ($formFields['subitems'] === 'null' || $formFields['subitems'] === null) {
164  $formFields['subitems'] = [];
165  }
166  return $formFields;
167  }
168 
172  private function getLPTypes(int $a_parent_ref_id): array
173  {
174  $result = [];
175 
176  $root = $this->tree->getNodeData($a_parent_ref_id);
177  $sub_items = $this->tree->getSubTree($root);
178  array_shift($sub_items); // remove root
179 
180  foreach ($sub_items as $node) {
181  if ($this->lpHelper->isSupportedObjectType($node['type'])) {
182  $class = $this->lpHelper->getTypeClass($node['type']);
184  $modes = $class::getDefaultModes($this->trackingHelper->enabledLearningProgress());
185 
186  if (count($modes) > 1) {
187  $result[] = $node['type'];
188  }
189  }
190  }
191 
192  return $result;
193  }
194 
198  private function getInvalidLPModes(): array
199  {
200  $invalid_modes = [
203  ];
204 
205  // without active LP the following modes cannot be supported
206  if (!$this->trackingHelper->enabledLearningProgress()) {
207  // status cannot be set without active LP
208  $invalid_modes[] = ilLPObjSettings::LP_MODE_MANUAL;
209  $invalid_modes[] = ilLPObjSettings::LP_MODE_MANUAL_BY_TUTOR;
211 
212  // mode cannot be configured without active LP
213  $invalid_modes[] = ilLPObjSettings::LP_MODE_COLLECTION;
214  $invalid_modes[] = ilLPObjSettings::LP_MODE_COLLECTION_MOBS;
215  $invalid_modes[] = ilLPObjSettings::LP_MODE_COLLECTION_TLT;
216  $invalid_modes[] = ilLPObjSettings::LP_MODE_SCORM;
217  $invalid_modes[] = ilLPObjSettings::LP_MODE_VISITS; // ?
218  }
219 
220  return $invalid_modes;
221  }
222 }
$refId
Definition: xapitoken.php:58
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
GUI class to create PDF certificates.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$message
Definition: xapiexit.php:32
__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'))