ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilObjLinkResource.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27{
28 protected LOMServices $lom_services;
30
31 public function __construct(int $a_id = 0, bool $a_call_by_reference = true)
32 {
33 global $DIC;
34
35 $this->type = "webr";
36 parent::__construct($a_id, $a_call_by_reference);
37
38 $this->lom_services = $DIC->learningObjectMetadata();
39 }
40
41 protected function getWebLinkRepo(): ilWebLinkRepository
42 {
43 if (isset($this->repo)) {
44 return $this->repo;
45 }
46 return $this->repo = new ilWebLinkDatabaseRepository($this->getId());
47 }
48
49 public function create($a_upload = false): int
50 {
51 $new_id = parent::create();
52 $this->createMetaData();
53 return $new_id;
54 }
55
56 public function update(): bool
57 {
58 $this->updateMetaData();
59 return parent::update();
60 }
61
62 protected function doMDUpdateListener(string $a_element): void
63 {
64 if ($a_element !== 'General') {
65 return;
66 }
67
68 $paths = $this->lom_services->paths();
69 $reader = $this->lom_services->read(
70 $this->getId(),
71 0,
72 $this->getType(),
73 $paths->custom()->withNextStep('general')->get()
74 );
75
76 $title = $reader->firstData($paths->title())->value();
77 $description = $reader->firstData($paths->firstDescription())->value();
78
79 if (
80 !$this->getWebLinkRepo()->doesListExist() &&
81 $this->getWebLinkRepo()->doesOnlyOneItemExist()
82 ) {
83 $item = $this->getWebLinkRepo()->getAllItemsAsContainer()->getFirstItem();
84 $draft = new ilWebLinkDraftItem(
85 $item->isInternal(),
86 $title,
87 $description,
88 $item->getTarget(),
89 $item->isActive(),
90 $item->getParameters()
91 );
92 $this->getWebLinkRepo()->updateItem($item, $draft);
93 }
94 if ($this->getWebLinkRepo()->doesListExist()) {
95 $list = $this->getWebLinkRepo()->getList();
96 $draft = new ilWebLinkDraftList(
97 $title,
98 $description
99 );
100 $this->getWebLinkRepo()->updateList($list, $draft);
101 }
102 }
103
104 public function delete(): bool
105 {
106 // always call parent delete function first!!
107 if (!parent::delete()) {
108 return false;
109 }
110
111 // delete items and list
112 $this->getWebLinkRepo()->deleteAllItems();
113 if ($this->getWebLinkRepo()->doesListExist()) {
114 $this->getWebLinkRepo()->deleteList();
115 }
116
117 // delete meta data
118 $this->deleteMetaData();
119
120 return true;
121 }
122
123 public function cloneObject(
124 int $target_id,
125 int $copy_id = 0,
126 bool $omit_tree = false
127 ): ?ilObject {
128 $new_obj = parent::cloneObject($target_id, $copy_id, $omit_tree);
129 $this->cloneMetaData($new_obj);
130
131 // object created, now copy items and parameters
132 $items = $this->getWebLinkRepo()->getAllItemsAsContainer()->getItems();
134
135 foreach ($items as $item) {
136 $draft = new ilWebLinkDraftItem(
137 $item->isInternal(),
138 $item->getTitle(),
139 $item->getDescription(),
140 $item->getTarget(),
141 $item->isActive(),
142 $item->getParameters()
143 );
144
145 $container->addItem($draft);
146 }
147
148 $new_web_link_repo = new ilWebLinkDatabaseRepository($new_obj->getId());
149 $new_web_link_repo->createAllItemsInDraftContainer($container);
150
151 // append copy info weblink title
152 if ($new_web_link_repo->doesOnlyOneItemExist(true)) {
153 $item = ilObjLinkResourceAccess::_getFirstLink($new_obj->getId());
154 $draft = new ilWebLinkDraftItem(
155 $item->isInternal(),
156 $new_obj->getTitle(),
157 $new_obj->getDescription(),
158 $item->getTarget(),
159 $item->isActive(),
160 $item->getParameters()
161 );
162 $new_web_link_repo->updateItem($item, $draft);
163 }
164
165 // make the new object a list if needed
166 if ($this->getWebLinkRepo()->doesListExist()) {
167 $draft_list = new ilWebLinkDraftList(
168 $new_obj->getTitle(),
169 $new_obj->getDescription()
170 );
171 $new_web_link_repo->createList($draft_list);
172 }
173
174 return $new_obj;
175 }
176
177 public function toXML(ilXmlWriter $writer): void
178 {
179 $attribs = array("obj_id" => "il_" . IL_INST_ID . "_webr_" . $this->getId(
180 )
181 );
182
183 $writer->xmlStartTag('WebLinks', $attribs);
184
185 // Sorting
188 $writer->xmlElement(
189 'Sorting',
190 array('type' => 'Manual')
191 );
192 break;
193
195 default:
196 $writer->xmlElement(
197 'Sorting',
198 array('type' => 'Title')
199 );
200 break;
201 }
202
203 if ($this->getWebLinkRepo()->doesListExist()) {
204 $writer->xmlStartTag('ListSettings');
205 $writer->xmlElement('ListTitle', [], $this->getTitle());
206 $writer->xmlElement('ListDescription', [], $this->getDescription());
207 $writer->xmlEndTag('ListSettings');
208 }
209
210 // All items
211 $items = $this->getWebLinkRepo()->getAllItemsAsContainer()
212 ->sort()
213 ->getItems();
214
215 $position = 0;
216 foreach ($items as $item) {
217 ++$position;
218 $item->toXML($writer, $position);
219 }
220
221 $writer->xmlEndTag('WebLinks');
222 }
223}
static _getFirstLink(int $a_webr_id)
Get first link item Check before with _isSingular() if there is more or less than one.
cloneObject(int $target_id, int $copy_id=0, bool $omit_tree=false)
__construct(int $a_id=0, bool $a_call_by_reference=true)
toXML(ilXmlWriter $writer)
doMDUpdateListener(string $a_element)
ilWebLinkDatabaseRepository $repo
Class ilObject Basic functions for all objects.
string $title
cloneMetaData(ilObject $target_obj)
Copy meta data.
Draft class for creating and updating a Web Link item.
Container class for drafted Web Link items.
Draft class for creating or updating a Web Link list.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
xmlEndTag(string $tag)
Writes an endtag.
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
const IL_INST_ID
Definition: constants.php:40
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26
$container
@noRector
Definition: wac.php:37