ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilPCVerificationGUI.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
27 {
28  private const SUPPORTED_TYPES = ['excv', 'tstv', 'crsv', 'cmxv', 'ltiv', 'scov'];
29  protected ilObjUser $user;
30 
31  public function __construct(
32  ilPageObject $a_pg_obj,
33  ?ilPCVerification $a_content_obj,
34  string $a_hier_id = '0',
35  string $a_pc_id = ""
36  ) {
37  global $DIC;
38 
39  $this->user = $DIC->user();
40  parent::__construct($a_pg_obj, $a_content_obj, $a_hier_id, $a_pc_id);
41  }
42 
43  public function executeCommand(): void
44  {
45  $cmd = $this->ctrl->getCmd();
46  $this->$cmd();
47  }
48 
49  public function insert(?ilPropertyFormGUI $a_form = null): void
50  {
51  $this->displayValidationError();
52 
53  if (!$a_form) {
54  $a_form = $this->initForm(true);
55  }
56  $this->tpl->setContent($a_form->getHTML());
57  }
58 
59  public function edit(?ilPropertyFormGUI $a_form = null): void
60  {
61  $this->displayValidationError();
62 
63  if (!$a_form) {
64  $a_form = $this->initForm();
65  }
66  $this->tpl->setContent($a_form->getHTML());
67  }
68 
72  private function getValidWorkspaceCertificateNodeByIdMap(): array
73  {
74  $nodes = [];
75 
76  $tree = new ilWorkspaceTree($this->user->getId());
77  $root = $tree->getRootId();
78  if ($root) {
79  $root = $tree->getNodeData($root);
80  foreach ($tree->getSubTree($root) as $node) {
81  if (in_array($node['type'], self::SUPPORTED_TYPES, true)) {
82  $nodes[$node['obj_id']] = $node;
83  }
84  }
85  }
86 
87  return $nodes;
88  }
89 
94  private function getValidCertificateByIdMap(): array
95  {
96  $certificates = [];
97 
98  $repository = new ilUserCertificateRepository();
99  $activeCertificates = $repository->fetchActiveCertificates($this->user->getId());
100  foreach ($activeCertificates as $certificate) {
101  $certificates[$certificate->getObjId()] = $certificate;
102  }
103 
104  return $certificates;
105  }
106 
107  protected function initForm(bool $a_insert = false): ilPropertyFormGUI
108  {
109  $this->lng->loadLanguageModule('wsp');
110 
111  $form = new ilPropertyFormGUI();
112  $form->setFormAction($this->ctrl->getFormAction($this));
113 
114  $data = [];
115  if ($a_insert) {
116  $form->setTitle($this->lng->txt('cont_insert_verification'));
117  $form->addCommandButton('create_verification', $this->lng->txt('save'));
118  $form->addCommandButton('cancelCreate', $this->lng->txt('cancel'));
119  } else {
120  $form->setTitle($this->lng->txt('cont_update_verification'));
121  $form->addCommandButton('update', $this->lng->txt('save'));
122  $form->addCommandButton('cancelUpdate', $this->lng->txt('cancel'));
123  $data = $this->content_obj->getData();
124  }
125 
126  $certificateOptions = [];
127  foreach ($this->getValidCertificateByIdMap() as $certificate) {
128  $userCertificate = $certificate->getUserCertificate();
130  $userCertificate->getAcquiredTimestamp(),
132  ));
133 
134  $type = $this->lng->txt('wsp_type_' . $userCertificate->getObjType() . 'v');
135  if ('sahs' === $userCertificate->getObjType()) {
136  $type = $this->lng->txt('wsp_type_scov');
137  }
138  $additionalInformation = ' (' . $type . ' / ' . $dateTime . ')';
139  $certificateOptions[$userCertificate->getObjId()] = $certificate->getObjectTitle() . $additionalInformation;
140  }
141 
142  if ($a_insert || (isset($data['type']) && 'crta' === $data['type'])) {
143  $certificate = new ilSelectInputGUI($this->lng->txt('certificate'), 'persistent_object');
144  $certificate->setRequired(true);
145  $certificate->setOptions($certificateOptions);
146  $form->addItem($certificate);
147  if (isset($data['id'])) {
148  $certificate->setValue($data['id']);
149  }
150 
151  return $form;
152  }
153 
154  $certificateSource = new ilRadioGroupInputGUI(
155  $this->lng->txt('certificate_selection'),
156  'certificate_selection'
157  );
158 
159  $workspaceRadioButton = new ilRadioOption(
160  $this->lng->txt('certificate_workspace_option'),
161  'certificate_workspace_option'
162  );
163  $persistentRadioButton = new ilRadioOption(
164  $this->lng->txt('certificate_persistent_option'),
165  'certificate_persistent_option'
166  );
167 
168  $workspaceCertificates = new ilSelectInputGUI($this->lng->txt('cont_verification_object'), 'object');
169  $workspaceCertificates->setRequired(true);
170  $workspaceOptions = [];
171  foreach ($this->getValidWorkspaceCertificateNodeByIdMap() as $node) {
172  $workspaceOptions[$node['obj_id']] = $node['title'] . ' (' . $this->lng->txt('wsp_type_' . $node['type']) . ')';
173  }
174  asort($workspaceOptions);
175  $workspaceCertificates->setOptions($workspaceOptions);
176 
177  $certificate = new ilSelectInputGUI($this->lng->txt('cont_verification_object'), 'persistent_object');
178  $certificate->setRequired(true);
179  $certificate->setOptions($certificateOptions);
180  $persistentRadioButton->addSubItem($certificate);
181  $workspaceRadioButton->addSubItem($workspaceCertificates);
182 
183  $certificateSource->addOption($persistentRadioButton);
184  $certificateSource->addOption($workspaceRadioButton);
185 
186  $form->addItem($certificateSource);
187 
188  $certificateSource->setValue('certificate_workspace_option');
189  $workspaceCertificates->setValue($data['id']);
190 
191  return $form;
192  }
193 
194  public function create(): void
195  {
196  $form = $this->initForm(true);
197  if ($form->checkInput()) {
198  $objectId = (int) $form->getInput('persistent_object');
199  $userId = $this->user->getId();
200 
201  $certificateFileService = new ilPortfolioCertificateFileService();
202  try {
203  $certificateFileService->createCertificateFile($userId, $objectId);
204  } catch (\ILIAS\Filesystem\Exception\FileAlreadyExistsException $e) {
205  $this->tpl->setOnScreenMessage('info', $this->lng->txt('certificate_file_not_found_error'), true);
206  $this->log->warning($e->getMessage());
207  } catch (\ILIAS\Filesystem\Exception\IOException $e) {
208  $this->tpl->setOnScreenMessage('info', $this->lng->txt('certificate_file_input_output_error'), true);
209  $this->log->error($e->getMessage());
210  $this->ctrl->redirect($this, 'initForm');
211  } catch (ilException $e) {
212  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('error_creating_certificate_pdf'), true);
213  $this->log->error($e->getMessage());
214  $this->ctrl->redirect($this, 'initForm');
215  }
216 
217  $this->content_obj = new ilPCVerification($this->getPage());
218  $this->content_obj->create($this->pg_obj, $this->hier_id, $this->pc_id);
219  $this->content_obj->setData('crta', $objectId);
220 
221  $this->updated = $this->pg_obj->update();
222  if ($this->updated === true) {
223  $this->ctrl->returnToParent($this, 'jump' . $this->hier_id);
224  }
225 
226  $this->log->info('File could not be created');
227  }
228 
229  $this->insert($form);
230  }
231 
236  public function update(): void
237  {
238  $form = $this->initForm();
239  if ($form->checkInput()) {
240  $option = 'certificate_persistent_option';
241  if ($form->getItemByPostVar('certificate_selection')) {
242  $option = $form->getInput('certificate_selection');
243  }
244 
245  $oldContentData = $this->content_obj->getData();
246 
247  if ('certificate_workspace_option' === $option) {
248  $objectId = (int) $form->getInput('object');
249  $validWorkSpaceCertificates = $this->getValidWorkspaceCertificateNodeByIdMap();
250 
251  if (isset($validWorkSpaceCertificates[$objectId])) {
252  $this->content_obj->setData($validWorkSpaceCertificates[$objectId]['type'], $objectId);
253  }
254  } elseif ('certificate_persistent_option' === $option) {
255  try {
256  $objectId = (int) $form->getInput('persistent_object');
257  $validCertificates = $this->getValidCertificateByIdMap();
258 
259  if (isset($validCertificates[$objectId])) {
260  $certificateFileService = new ilPortfolioCertificateFileService();
261  $certificateFileService->createCertificateFile(
262  $this->user->getId(),
263  $objectId
264  );
265  $this->content_obj->setData('crta', $objectId);
266  }
267  } catch (\ILIAS\Filesystem\Exception\FileNotFoundException | \ILIAS\Filesystem\Exception\FileAlreadyExistsException $e) {
268  $this->tpl->setOnScreenMessage('info', $this->lng->txt('certificate_file_not_found_error'), true);
269  $this->log->warning($e->getMessage());
270  } catch (\ILIAS\Filesystem\Exception\IOException $e) {
271  $this->tpl->setOnScreenMessage('info', $this->lng->txt('certificate_file_input_output_error'), true);
272  $this->log->warning($e->getMessage());
273  } catch (ilException $e) {
274  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('error_creating_certificate_pdf'), true);
275  $this->log->error($e->getMessage());
276  $this->ctrl->redirect($this, 'initForm');
277  }
278  }
279 
280  if ('crta' === $oldContentData['type']) {
281  try {
282  $certificateFileService = new ilPortfolioCertificateFileService();
283  $certificateFileService->deleteCertificateFile(
284  $this->user->getId(),
285  (int) $oldContentData['id']
286  );
287  } catch (\ILIAS\Filesystem\Exception\FileNotFoundException $e) {
288  $this->tpl->setOnScreenMessage('info', $this->lng->txt('certificate_file_not_found_error'));
289  $this->log->warning($e->getMessage());
290  } catch (\ILIAS\Filesystem\Exception\IOException $e) {
291  $this->tpl->setOnScreenMessage('info', $this->lng->txt('certificate_file_input_output_error'));
292  $this->log->warning($e->getMessage());
293  }
294  }
295 
296  $this->updated = $this->pg_obj->update();
297  if ($this->updated === true) {
298  $this->ctrl->returnToParent($this, 'jump' . $this->hier_id);
299  }
300  }
301 
302  $this->pg_obj->addHierIDs();
303  $this->edit($form);
304  }
305 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getNodeData(int $a_node_id, ?int $a_tree_pk=null)
get all information of a node.
$type
Class ChatMainBarProvider .
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
__construct(ilPageObject $a_pg_obj, ?ilPCVerification $a_content_obj, string $a_hier_id='0', string $a_pc_id="")
const IL_CAL_UNIX
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
initForm(bool $a_insert=false)
global $DIC
Definition: feed.php:28
User Interface for Editing of Page Content Objects (Paragraphs, Tables, ...)
insert(?ilPropertyFormGUI $a_form=null)
This class represents a property in a property form.
edit(?ilPropertyFormGUI $a_form=null)
setValue(string $a_value)
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(Container $dic, ilPlugin $plugin)
Class FlySystemFileAccessTest disabled disabled disabled.