ILIAS  release_8 Revision v8.24
class.ilECSObjectSettings.php
Go to the documentation of this file.
1<?php
2
18declare(strict_types=1);
19
25abstract class ilECSObjectSettings
26{
27 protected \ilObject $content_obj; // [ilObj]
28
31 private ilTree $tree;
33
34 public const MAIL_SENDER = 6;
35 private \ilGlobalTemplateInterface $main_tpl;
36
42 public function __construct(ilObject $a_content_object)
43 {
44 global $DIC;
45 $this->main_tpl = $DIC->ui()->mainTemplate();
46
47 $this->lng = $DIC->language();
48 $this->logger = $DIC->logger()->obj();
49 $this->tree = $DIC->repositoryTree();
50 $this->rbacAdmin = $DIC->rbac()->admin();
51
52 $this->content_obj = $a_content_object;
53 }
54
58 public static function getInstanceByObject(ilObject $a_content_obj): ?ilECSObjectSettings
59 {
60 switch ($a_content_obj->getType()) {
61 case 'crs':
62 return new ilECSCourseSettings($a_content_obj);
63
64 case 'cat':
65 return new ilECSCategorySettings($a_content_obj);
66
67 case 'file':
68 return new ilECSFileSettings($a_content_obj);
69
70 case 'glo':
71 return new ilECSGlossarySettings($a_content_obj);
72
73 case 'grp':
74 return new ilECSGroupSettings($a_content_obj);
75
76 case 'lm':
77 return new ilECSLearningModuleSettings($a_content_obj);
78
79 case 'wiki':
80 return new ilECSWikiSettings($a_content_obj);
81 }
82 return null;
83 }
84
89 public function getContentObject(): \ilObject
90 {
91 return $this->content_obj;
92 }
93
97 abstract protected function getECSObjectType(): string;
98
102 protected function isActive(): bool
103 {
104 if (ilECSServerSettings::getInstance()->activeServerExists()) {
105 // imported objects cannot be exported => why not
106 #if(!ilECSImportManager::getInstance()->lookupServerId($this->content_obj->getId()))
107 {
108 return true;
109 }
110 }
111
112 return false;
113 }
114
120 public function addSettingsToForm(ilPropertyFormGUI $a_form, $a_type): bool
121 {
122 $this->logger->debug('Show ecs settings.');
123 if (!$this->isActive()) {
124 $this->logger->debug('Object type is not active. => no settings.');
125 return false;
126 }
127
128 $obj_id = $this->content_obj->getId();
129
130 // Return if no participant is enabled for export and the current object is not released
131 if (!$this->getContentObject()->withReferences()) {
132 $this->logger->debug('Called withot references. => no settings.');
133 return true;
134 }
135 $exportManager = ilECSExportManager::getInstance();
136 $exportableParticipants = (new ilECSParticipantSettingsRepository())->getExportableParticipants($a_type);
137 if (!$exportableParticipants && !$exportManager->_isExported($obj_id)) {
138 $this->logger->debug('Object type is not exportable. => no settings.');
139 return true;
140 }
141 if (
142 $this->tree->checkForParentType($this->tree->getParentId($this->getContentObject()->getRefId()), 'crs', false) ||
143 $this->tree->checkForParentType($this->tree->getParentId($this->getContentObject()->getRefId()), 'grp', false)
144 ) {
145 $this->logger->debug('Parent crs/grp in path. => no settings.');
146 return true;
147 }
148
149 $this->lng->loadLanguageModule('ecs');
150
151 // show ecs property form section
152 $ecs = new ilFormSectionHeaderGUI();
153 $ecs->setTitle($this->lng->txt('ecs_' . $a_type . '_export'));
154 $a_form->addItem($ecs);
155
156
157 // release or not
158 $exp = new ilRadioGroupInputGUI($this->lng->txt('ecs_' . $a_type . '_export_obj_settings'), 'ecs_export');
159 $exp->setRequired(true);
160 $exp->setValue($exportManager->_isExported($obj_id) ? "1" : "0");
161 $off = new ilRadioOption($this->lng->txt('ecs_' . $a_type . '_export_disabled'), "0");
162 $exp->addOption($off);
163 $on = new ilRadioOption($this->lng->txt('ecs_' . $a_type . '_export_enabled'), "1");
164 $exp->addOption($on);
165 $a_form->addItem($exp);
166
167 // Show all exportable participants
168 $publish_for = new ilCheckboxGroupInputGUI($this->lng->txt('ecs_publish_for'), 'ecs_sid');
169
170 // @TODO: Active checkboxes for recipients
171 //$publish_for->setValue((array) $members);
172
173 // Read receivers
174 $receivers = array();
175 foreach ($exportManager->getExportServerIds($obj_id) as $sid) {
176 $exp = new ilECSExport($sid, $obj_id);
177
178 $participants = null;
180 $sid,
181 $exp->getEContentId(),
182 $this->getECSObjectType()
183 );
184 if ($details instanceof ilECSEContentDetails) {
185 $participants = $details->getReceivers();
186 }
187 if ($participants) {
188 foreach ($participants as $mid) {
189 $receivers[] = $sid . '_' . $mid;
190 }
191 }
192 }
193 $publish_for->setValue($receivers);
194
195 foreach ($exportableParticipants as $pInfo) {
196 $partSetting = new ilECSParticipantSetting($pInfo['sid'], $pInfo['mid']);
197
198 $com = new ilCheckboxOption(
199 $partSetting->getCommunityName() . ': ' . $partSetting->getTitle(),
200 'sid_mid'
201 );
202 $com->setValue($pInfo['sid'] . '_' . $pInfo['mid']);
203 $publish_for->addOption($com);
204 }
205 $on->addSubItem($publish_for);
206 return true;
207 }
208
217 public function handleSettingsUpdate(): bool
218 {
219 if (!$this->isActive()) {
220 return true;
221 }
222
223 // Parse post data
224 $mids = array();
225 foreach ((array) $_POST['ecs_sid'] as $sid_mid) {
226 $tmp = explode('_', $sid_mid);
227 $mids[$tmp[0]][] = $tmp[1];
228 }
229
230 try {
231 // Update for each server
232 foreach ((new ilECSParticipantSettingsRepository())->getServersContaingExports() as $server_id) {
234 if ($server->isEnabled()) {
235 // Export
236 $export = true;
237 if (!$_POST['ecs_export']) {
238 $export = false;
239 }
240 if (
241 !isset($mids[$server_id]) ||
242 !is_array($mids[$server_id]) ||
243 !count($mids[$server_id])) {
244 $export = false;
245 }
247 $server,
248 $export,
249 $mids[$server_id] ?? []
250 );
251 }
252 }
253 } catch (ilECSConnectorException $exc) {
254 $this->main_tpl->setOnScreenMessage('failure', 'Error exporting to ECS server: ' . $exc->getMessage());
255 return false;
256 }
257 return true;
258 }
259
266 protected function handleSettingsForServer(ilECSSetting $a_server, bool $a_export, array $a_mids): void
267 {
268 $export_settings = new ilECSExport($a_server->getServerId(), $this->content_obj->getId());
269
270 // already exported?
271 if ($export_settings->isExported()) {
272 // still exportable: update ecs
273 if ($a_export) {
274 $this->doUpdate($a_server, $export_settings, $a_mids);
275 }
276 // not exportable anymore
277 else {
278 $this->doDelete($a_server, $export_settings);
279 }
280 }
281 // not exported yet
282 elseif ($a_export) {
283 $this->doAdd($a_server, $export_settings, $a_mids);
284 }
285 }
286
292 public function handleContentUpdate(): bool
293 {
294 if (!$this->isActive()) {
295 return true;
296 }
297 $exportManager = ilECSExportManager::getInstance();
298 $export_servers = $exportManager->getExportServerIds($this->content_obj->getId());
299 foreach ($export_servers as $server_id) {
301 if ($server->isEnabled()) {
302 try {
303 $export_settings = new ilECSExport($server_id, $this->content_obj->getId());
304
305 // already exported, update ecs
306 if ($export_settings->isExported()) {
307 $this->doUpdate($server, $export_settings);
308 }
309 } catch (ilECSConnectorException $exc) {
310 $this->logger->warning(__METHOD__ . ': Cannot handle ECS content update. ' . $exc->getMessage());
311 return false;
312 }
313 }
314 }
315 return true;
316 }
317
321 protected function doAdd(ilECSSetting $a_server, ilECSExport $a_export_settings, array $a_mids): void
322 {
323 $this->logger->info(__METHOD__ . ': Starting ECS add resource...');
324
325 $json = $this->buildJson($a_server);
326
327 $connector = new ilECSConnector($a_server);
328 $connector->addHeader(ilECSConnector::HEADER_MEMBERSHIPS, implode(',', $a_mids));
329 $econtent_id = $connector->addResource(
330 $this->getECSObjectType(),
331 json_encode($json, JSON_THROW_ON_ERROR)
332 );
333
334 // status changed
335 $a_export_settings->setExported(true);
336 $a_export_settings->setEContentId($econtent_id);
337 $a_export_settings->save();
338
339 $this->handlePermissionUpdate($a_server);
340
341 // Send mail
342 $this->sendNewContentNotification($a_server, $econtent_id);
343 }
344
353 protected function doUpdate(ilECSSetting $a_server, ilECSExport $a_export_settings, array $a_mids = null): void
354 {
355 $econtent_id = $a_export_settings->getEContentId();
356 if (!$econtent_id) {
357 $this->logger->warning(__METHOD__ . ': Missing eid. Aborting.');
358 throw new ilECSConnectorException('Missing ECS content ID. Aborting.');
359 }
360 $connector = new ilECSConnector($a_server);
361
362 if (!$a_mids) {
363 $a_mids = $this->getParticipants($a_server->getServerId(), $econtent_id);
364 }
365 $this->logger->info(__METHOD__ . ': Start updating ECS content - ' . print_r($a_mids, true));
366 $connector->addHeader(ilECSConnector::HEADER_MEMBERSHIPS, implode(',', (array) $a_mids));
367
368 $json = $this->buildJson($a_server);
369 $connector->updateResource(
370 $this->getECSObjectType(),
371 $econtent_id,
372 json_encode($json, JSON_THROW_ON_ERROR)
373 );
374
375 $this->handlePermissionUpdate($a_server);
376 }
377
385 public function doDelete(ilECSSetting $a_server, ilECSExport $a_export_settings): void
386 {
387 // already exported?
388 if ($a_export_settings->isExported()) {
389 $econtent_id = $a_export_settings->getEContentId();
390 if (!$econtent_id) {
391 $this->logger->warning(__METHOD__ . ': Missing eid. Aborting.');
392 throw new ilECSConnectorException('Missing ECS content ID. Aborting.');
393 }
394 $connector = new ilECSConnector($a_server);
395
396 $this->logger->info(__METHOD__ . ': Start deleting ECS content...');
397 $connector->deleteResource(
398 $this->getECSObjectType(),
399 $econtent_id
400 );
401
402 // status changed
403 $a_export_settings->setExported(false);
404 $a_export_settings->save();
405 }
406 }
407
414 public static function _handleDelete(array $a_subbtree_nodes): void
415 {
416 // active?
417 if (!ilECSServerSettings::getInstance()->activeServerExists()) {
418 return;
419 }
420 $exportManager = ilECSExportManager::getInstance();
421 $exported = $exportManager->getExportedIds();
422 foreach ($a_subbtree_nodes as $node) {
423 if (in_array(
424 $node['obj_id'],
425 $exported,
426 true
427 ) && $content_obj = ilObjectFactory::getInstanceByRefId($node['child'], false)) {
429
430 // Read export server ids
431 foreach ($exportManager->getExportServerIds($node['obj_id']) as $sid) {
433 $export_settings = new ilECSExport($sid, $content_obj->getId());
434 if ($settings) {
435 $settings->doDelete($server, $export_settings);
436 }
437 }
438 }
439 }
440 }
441
445 protected function getParticipants(int $a_server_id, int $a_econtent_id): array
446 {
447 $receivers = array();
448 foreach ((array) $a_server_id as $sid) {
449 $participants = null;
451 if ($details instanceof ilECSEContentDetails) {
452 $participants = $details->getReceivers();
453 }
454 if ($participants) {
455 foreach ($participants as $mid) {
456 $receivers[] = $mid;
457 }
458 }
459 }
460 return $receivers;
461 }
462
466 protected function sendNewContentNotification(ilECSSetting $a_server, $a_econtent_id): bool
467 {
468 if (!count($rcps = $a_server->getApprovalRecipients())) {
469 return true;
470 }
471
473 $lang->loadLanguageModule('ecs');
474
475 // @TODO: read mail
476 $mail = new ilMail(self::MAIL_SENDER);
477 $message = $lang->txt('ecs_export_created_body_a') . "\n\n";
478 $message .= $lang->txt('title') . ': ' . $this->content_obj->getTitle() . "\n";
479 if (($desc = $this->content_obj->getDescription()) !== '') {
480 $message .= $lang->txt('desc') . ': ' . $desc . "\n";
481 }
482
483 // Participant info
484 $message .= ("\n" . $lang->txt('ecs_published_for'));
485
486 try {
487 $found = false;
488
489 $receivers = null;
491 $a_server->getServerId(),
492 $a_econtent_id,
493 $this->getECSObjectType()
494 );
495 if ($details instanceof ilECSEContentDetails) {
496 $receivers = $details->getReceivers();
497 }
498 if ($receivers) {
499 foreach ($receivers as $member) {
500 $found = true;
501
502 $part = ilECSCommunityReader::getInstanceByServerId($a_server->getServerId())->getParticipantByMID($member);
503
504 $message .= ("\n\n" . $part->getParticipantName() . "\n");
505 $message .= ($part->getDescription());
506 }
507 }
508 if ($found) {
509 $message .= "\n\n";
510 } else {
511 $message .= (' ' . $lang->txt('ecs_not_published') . "\n\n");
512 }
513 } catch (ilECSConnectorException $e) {
514 $this->logger->warning(__METHOD__ . ': Cannot read approvements.');
515 return false;
516 }
517
518 $href = ilLink::_getStaticLink($this->content_obj->getRefId(), 'crs', true);
519 $message .= $lang->txt("perma_link") . ': ' . $href . "\n\n";
521
522 $mail->enqueue(
524 '',
525 '',
526 $lang->txt('ecs_new_approval_subject'),
527 $message,
528 array()
529 );
530
531 return true;
532 }
533
538 {
539 if (
540 ($this->content_obj->getType() === 'crs') ||
541 ($this->content_obj->getType() === 'grp')
542 ) {
543 $this->logger->info(__METHOD__ . ': Permission update for courses/groups');
544 $this->rbacAdmin->grantPermission(
545 $server->getGlobalRole(),
546 ilRbacReview::_getOperationIdsByName(array('join','visible')),
547 $this->content_obj->getRefId()
548 );
549 }
550 }
551
555 protected function getJsonCore(string $a_etype): object
556 {
557 $json = new stdClass();
558 $json->lang = 'en_EN'; // :TODO: obsolet?
559 $json->id = 'il_' . IL_INST_ID . '_' . $this->getContentObject()->getType() . '_' . $this->getContentObject()->getId();
560 $json->etype = $a_etype;
561 $json->title = $this->content_obj->getTitle();
562 $json->abstract = $this->content_obj->getLongDescription();
563
564 $json->url = ilLink::_getLink($this->content_obj->getRefId(), $this->content_obj->getType());
565
566 return $json;
567 }
568
572 protected function addMetadataToJson(object $a_json, ilECSSetting $a_server, array $a_definition): void
573 {
575
576 $values = ilECSUtils::getAdvancedMDValuesForObjId($this->content_obj->getId());
577
578 foreach ($a_definition as $id => $type) {
579 if (is_array($type)) {
580 [$type , $target] = $type;
581 } else {
582 $target = $id;
583 }
584
585 if ($field = $mappings->getMappingByECSName(ilECSDataMappingSetting::MAPPING_EXPORT, $id)) {
586 $value = $values[$field] ?? '';
587
588 switch ($type) {
590 $a_json->{$target} = explode(',', $value);
591 break;
592
594 $a_json->{$target} = (int) $value;
595 break;
596
598 $a_json->{$target} = (string) $value;
599 break;
600
602 if (!isset($a_json->{$target})) {
603 $a_json->{$target} = new ilECSTimePlace();
604 }
605 $a_json->{$target}->{'set' . ucfirst($id)}($value);
606 break;
607 }
608 }
609 }
610 }
611
617 abstract protected function buildJson(ilECSSetting $a_server);
618}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
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...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getInstanceByServerId(int $a_server_id)
Get instance by server id.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getInstanceByServerId(int $a_server_id)
Get singleton instance.
Presentation of ecs content details (http://...campusconnect/courselinks/id/details)
static getInstanceFromServer(int $a_server_id, int $a_econtent_id, string $a_resource_type)
Get data from server.
static getInstance()
Get the singelton instance of this ilECSExportManager.
Storage of an ECS exported object.
getEContentId()
get econtent id
setEContentId(int $a_id)
set econtent id
isExported()
check if an object is exported or not
setExported(bool $a_status)
Set exported.
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...
Class ilECSGroupSettings.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Handles object exports to ECS.
getContentObject()
Get content object.
getParticipants(int $a_server_id, int $a_econtent_id)
Get participants for server and ecs resource.
doUpdate(ilECSSetting $a_server, ilECSExport $a_export_settings, array $a_mids=null)
Update ECS resource.
static getInstanceByObject(ilObject $a_content_obj)
Get settings handler for repository object.
getJsonCore(string $a_etype)
Build core json structure.
sendNewContentNotification(ilECSSetting $a_server, $a_econtent_id)
send notifications about new EContent
isActive()
Is ECS (for current object) active?
static _handleDelete(array $a_subbtree_nodes)
handle delete Objects that are moved to the trash call ECS-Remove
handleSettingsForServer(ilECSSetting $a_server, bool $a_export, array $a_mids)
Save ECS settings (add- update- deleteResource)
handlePermissionUpdate(ilECSSetting $server)
Handle permission update.
doAdd(ilECSSetting $a_server, ilECSExport $a_export_settings, array $a_mids)
Add resource to ECS.
addMetadataToJson(object $a_json, ilECSSetting $a_server, array $a_definition)
Add advanced metadata to json (export)
addSettingsToForm(ilPropertyFormGUI $a_form, $a_type)
Fill ECS export settings "multiple servers".
handleSettingsUpdate()
Update ECS Export Settings.
getECSObjectType()
Get ECS resource identifier, e.g.
doDelete(ilECSSetting $a_server, ilECSExport $a_export_settings)
Delete ECS resource.
ilGlobalTemplateInterface $main_tpl
buildJson(ilECSSetting $a_server)
Build resource-specific json.
handleContentUpdate()
Update ECS Content.
__construct(ilObject $a_content_object)
Constructor.
static getInstance()
Get singleton instance.
getServerId()
Get current server id.
getApprovalRecipientsAsString()
get approval recipients as string
getApprovalRecipients()
get approval recipients
static getInstanceByServerId(int $a_server_id)
Get singleton instance per server.
Representation of ECS EContent Time Place.
static getAdvancedMDValuesForObjId(int $a_obj_id)
Get advanced metadata values for object id.
const TYPE_TIMEPLACE
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...
static _getLanguage(string $a_lang_key='')
Get language object.
language handling
Component logger with individual log levels by component id.
static _getAutoGeneratedMessageString(ilLanguage $lang=null)
static getInstanceByRefId(int $ref_id, bool $stop_on_error=true)
get an instance of an Ilias object by reference id
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class represents a property form user interface.
This class represents a property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilRbacAdmin Core functions for role based access control.
static _getOperationIdsByName(array $operations)
get ops_id's by name.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const IL_INST_ID
Definition: constants.php:40
$server
global $DIC
Definition: feed.php:28
array $details
Details for error message relating to last request processed.
Definition: System.php:109
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
$type
$lang
Definition: xapiexit.php:26
$message
Definition: xapiexit.php:32