ILIAS  release_8 Revision v8.24
class.ilObjMediaCast.php
Go to the documentation of this file.
1<?php
2
24{
25 public const ORDER_TITLE = 1;
26 public const ORDER_CREATION_DATE_ASC = 2;
27 public const ORDER_CREATION_DATE_DESC = 3;
28 public const ORDER_MANUAL = 4;
29 public const VIEW_LIST = "";
30 public const VIEW_GALLERY = "gallery"; // legacy gallery, @todo remove
31 public const VIEW_IMG_GALLERY = "img_gallery";
32 public const VIEW_PODCAST = "podcast";
33 public const VIEW_VCAST = "video";
34 public const AUTOPLAY_NO = 0;
35 public const AUTOPLAY_ACT = 1;
36 public const AUTOPLAY_INACT = 2;
37 protected \ILIAS\MediaCast\InternalDomainService $domain;
38 protected \ILIAS\MediaObjects\Tracking\TrackingManager $mob_tracking;
39
40 protected array $itemsarray;
41 protected ilObjUser $user;
42 public static array $purposes = array("Standard");
43 protected bool $online = false;
44 protected bool $publicfiles = false;
45 protected bool $downloadable = true;
46 protected int $order = 0;
47 protected string $view_mode = self::VIEW_VCAST;
48 protected int $defaultAccess = 0; // 0 = logged in users, 1 = public access
49 protected array $mob_mapping = [];
50 protected int $nr_initial_videos = 5;
51 protected bool $new_items_in_lp = true;
53
54 public function __construct(
55 int $a_id = 0,
56 bool $a_call_by_reference = true
57 ) {
58 global $DIC;
59
60 $this->db = $DIC->database();
61 $this->user = $DIC->user();
62 $this->type = "mcst";
63 $mcst_set = new ilSetting("mcst");
64 $this->setDefaultAccess($mcst_set->get("defaultaccess") == "users" ? 0 : 1);
65 $this->setOrder(self::ORDER_CREATION_DATE_DESC);
66 $this->mob_tracking = $DIC->mediaObjects()->internal()
67 ->domain()
68 ->tracking();
69 $this->domain = $DIC->mediaCast()->internal()->domain();
70 parent::__construct($a_id, $a_call_by_reference);
71 }
72
73 public function setOnline(bool $a_online): void
74 {
75 $this->online = $a_online;
76 }
77
78 public function getOnline(): bool
79 {
80 return $this->online;
81 }
82
83 public function setPublicFiles(bool $a_publicfiles): void
84 {
85 $this->publicfiles = $a_publicfiles;
86 }
87
88 public function getPublicFiles(): bool
89 {
90 return $this->publicfiles;
91 }
92
93 public function setViewMode(string $a_val): void
94 {
95 $this->view_mode = $a_val;
96 }
97
98 public function getViewMode(): string
99 {
100 return $this->view_mode;
101 }
102
103 public function setItemsArray(array $a_itemsarray): void
104 {
105 $this->itemsarray = $a_itemsarray;
106 }
107
108 public function getItemsArray(): array
109 {
110 return $this->itemsarray;
111 }
112
113 public function setAutoplayMode(int $a_val): void
114 {
115 $this->autoplay_mode = $a_val;
116 }
117
118 public function getAutoplayMode(): int
119 {
121 }
122
123 public function setNumberInitialVideos(int $a_val): void
124 {
125 $this->nr_initial_videos = $a_val;
126 }
127
128 public function getNumberInitialVideos(): int
129 {
131 }
132
136 public function setNewItemsInLearningProgress(bool $a_val): void
137 {
138 $this->new_items_in_lp = $a_val;
139 }
140
141 public function getNewItemsInLearningProgress(): bool
142 {
144 }
145
146 public function getSortedItemsArray(): array
147 {
148 $med_items = $this->getItemsArray();
149
150 // sort by order setting
151 switch ($this->getOrder()) {
153 $med_items = ilArrayUtil::sortArray($med_items, "title", "asc", false, true);
154 break;
155
157 $med_items = ilArrayUtil::sortArray($med_items, "creation_date", "asc", false, true);
158 break;
159
161 $med_items = ilArrayUtil::sortArray($med_items, "creation_date", "desc", false, true);
162 break;
163
165 $order = array_flip($this->readOrder());
166 $pos = sizeof($order);
167 foreach (array_keys($med_items) as $idx) {
168 if (array_key_exists($idx, $order)) {
169 $med_items[$idx]["order"] = ($order[$idx] + 1) * 10;
170 }
171 // item has no order yet
172 else {
173 $med_items[$idx]["order"] = (++$pos) * 10;
174 }
175 }
176
177 $med_items = ilArrayUtil::sortArray($med_items, "order", "asc", true, true);
178 break;
179 }
180 return $med_items;
181 }
182
183 public function setDownloadable(bool $a_downloadable): void
184 {
185 $this->downloadable = $a_downloadable;
186 }
187
188 public function getDownloadable(): bool
189 {
190 return $this->downloadable;
191 }
192
193 public function getDefaultAccess(): int
194 {
196 }
197
198 public function setDefaultAccess(int $value): void
199 {
200 $this->defaultAccess = ($value === 0) ? 0 : 1;
201 }
202
203 public function setOrder(int $a_value): void
204 {
205 $this->order = $a_value;
206 }
207
208 public function getOrder(): int
209 {
210 return $this->order;
211 }
212
213
214 public function create(): int
215 {
217
218 $id = parent::create();
219
220 $query = "INSERT INTO il_media_cast_data (" .
221 " id" .
222 ", is_online" .
223 ", public_files" .
224 ", downloadable" .
225 ", def_access" .
226 ", sortmode" .
227 ", viewmode" .
228 ", autoplaymode" .
229 ", nr_initial_videos" .
230 ", new_items_in_lp" .
231 " ) VALUES (" .
232 $ilDB->quote($this->getId(), "integer")
233 . "," . $ilDB->quote((int) $this->getOnline(), "integer")
234 . "," . $ilDB->quote((int) $this->getPublicFiles(), "integer")
235 . "," . $ilDB->quote((int) $this->getDownloadable(), "integer")
236 . "," . $ilDB->quote((int) $this->getDefaultAccess(), "integer")
237 . "," . $ilDB->quote((int) $this->getOrder(), "integer")
238 . "," . $ilDB->quote($this->getViewMode(), "text")
239 . "," . $ilDB->quote((int) $this->getAutoplayMode(), "integer")
240 . "," . $ilDB->quote((int) $this->getNumberInitialVideos(), "integer")
241 . "," . $ilDB->quote((int) $this->getNewItemsInLearningProgress(), "integer")
242 . ")";
243 $ilDB->manipulate($query);
244 return $id;
245 }
246
247 public function update(): bool
248 {
250
251 if (!parent::update()) {
252 return false;
253 }
254
255 // update media cast data
256 $query = "UPDATE il_media_cast_data SET " .
257 " is_online = " . $ilDB->quote((int) $this->getOnline(), "integer") .
258 ", public_files = " . $ilDB->quote((int) $this->getPublicFiles(), "integer") .
259 ", downloadable = " . $ilDB->quote((int) $this->getDownloadable(), "integer") .
260 ", def_access = " . $ilDB->quote($this->getDefaultAccess(), "integer") .
261 ", sortmode = " . $ilDB->quote($this->getOrder(), "integer") .
262 ", viewmode = " . $ilDB->quote($this->getViewMode(), "text") .
263 ", autoplaymode = " . $ilDB->quote($this->getAutoplayMode(), "integer") .
264 ", nr_initial_videos = " . $ilDB->quote($this->getNumberInitialVideos(), "integer") .
265 ", new_items_in_lp = " . $ilDB->quote($this->getNewItemsInLearningProgress(), "integer") .
266 " WHERE id = " . $ilDB->quote($this->getId(), "integer");
267
268 $ilDB->manipulate($query);
269
270 return true;
271 }
272
273 public function read(): void
274 {
276
277 parent::read();
278 $this->readItems();
279
280 $query = "SELECT * FROM il_media_cast_data WHERE id = " .
281 $ilDB->quote($this->getId(), "integer");
282 $set = $ilDB->query($query);
283 $rec = $ilDB->fetchAssoc($set);
284
285 $this->setOnline((bool) $rec["is_online"]);
286 $this->setPublicFiles((bool) $rec["public_files"]);
287 $this->setDownloadable((bool) $rec["downloadable"]);
288 $this->setDefaultAccess((int) $rec["def_access"]);
289 $this->setOrder((int) $rec["sortmode"]);
290 $this->setViewMode((string) $rec["viewmode"]);
291 $this->setAutoplayMode((int) $rec["autoplaymode"]);
292 $this->setNumberInitialVideos((int) $rec["nr_initial_videos"]);
293 $this->setNewItemsInLearningProgress((bool) $rec["new_items_in_lp"]);
294 }
295
296
297 public function delete(): bool
298 {
300
301 // always call parent delete function first!!
302 if (!parent::delete()) {
303 return false;
304 }
305
306 // delete all items
307 $med_items = $this->getItemsArray();
308 foreach ($med_items as $item) {
309 $news_item = new ilNewsItem($item["id"]);
310 $news_item->delete();
311 }
312
313 $this->deleteOrder();
314
315 // delete record of table il_media_cast_data
316 $query = "DELETE FROM il_media_cast_data" .
317 " WHERE id = " . $ilDB->quote($this->getId(), "integer");
318 $ilDB->manipulate($query);
319
320 return true;
321 }
322
323 public function readItems(bool $a_oldest_first = false): array
324 {
325 //
326 $it = new ilNewsItem();
327 $it->setContextObjId($this->getId());
328 $it->setContextObjType($this->getType());
329 $this->itemsarray = $it->queryNewsForContext(false, 0, "", false, $a_oldest_first);
330
331 return $this->itemsarray;
332 }
333
334 public function deleteOrder(): void
335 {
337
338 if (!$this->getId()) {
339 return;
340 }
341
342 $sql = "DELETE FROM il_media_cast_data_ord" .
343 " WHERE obj_id = " . $ilDB->quote($this->getId(), "integer");
344 $ilDB->manipulate($sql);
345 }
346
347 public function readOrder(): ?array
348 {
350
351 if (!$this->getId()) {
352 return null;
353 }
354
355 $all = array();
356 $sql = "SELECT item_id FROM il_media_cast_data_ord" .
357 " WHERE obj_id = " . $ilDB->quote($this->getId(), "integer") .
358 " ORDER BY pos";
359 $res = $ilDB->query($sql);
360 while ($row = $ilDB->fetchAssoc($res)) {
361 $all[] = $row["item_id"];
362 }
363 return $all;
364 }
365
366 public function saveOrder(array $a_items): void
367 {
369
370 if (!$this->getId()) {
371 return;
372 }
373 $this->deleteOrder();
374
375 $pos = 0;
376 foreach ($a_items as $item_id) {
377 $pos++;
378
379 $sql = "INSERT INTO il_media_cast_data_ord (obj_id,item_id,pos)" .
380 " VALUES (" . $ilDB->quote($this->getId(), "integer") . "," .
381 $ilDB->quote($item_id, "integer") . "," .
382 $ilDB->quote($pos, "integer") . ")";
383 $ilDB->manipulate($sql);
384 }
385 }
386
387 protected function copyOrder(
388 ilObjMediaCast $newObj,
389 array $mapping
390 ): void {
391 $items = [];
392 foreach ($this->readOrder() as $i) {
393 if (!array_key_exists($i, $mapping)) {
394 continue;
395 }
396 $items[] = $mapping[$i];
397 }
398 $newObj->saveOrder($items);
399 }
400
401 public function cloneObject(int $a_target_id, int $a_copy_id = 0, bool $a_omit_tree = false): ?ilObject
402 {
404 $new_obj = parent::cloneObject($a_target_id, $a_copy_id, $a_omit_tree);
405
406 //copy online status if object is not the root copy object
407 $cp_options = ilCopyWizardOptions::_getInstance($a_copy_id);
408
409 if (!$cp_options->isRootNode($this->getRefId())) {
410 $new_obj->setOnline($this->getOnline());
411 }
412
413 $new_obj->setPublicFiles($this->getPublicFiles());
414 $new_obj->setDownloadable($this->getDownloadable());
415 $new_obj->setDefaultAccess($this->getDefaultAccess());
416 $new_obj->setOrder($this->getOrder());
417 $new_obj->setViewMode($this->getViewMode());
418 $new_obj->setAutoplayMode($this->getAutoplayMode());
419 $new_obj->setNumberInitialVideos($this->getNumberInitialVideos());
420 $new_obj->setNewItemsInLearningProgress($this->getNewItemsInLearningProgress());
421
422 $new_obj->update();
423
424 $pf = ilBlockSetting::_lookup("news", "public_feed", 0, $this->getId());
425 $keeprss = (int) ilBlockSetting::_lookup("news", "keep_rss_min", 0, $this->getId());
426 ilBlockSetting::_write("news", "public_feed", (string) $pf, 0, $new_obj->getId());
427 ilBlockSetting::_write("news", "keep_rss_min", (string) $keeprss, 0, $new_obj->getId());
428
429 // copy items
430 $mapping = $this->copyItems($new_obj);
431 $this->copyOrder($new_obj, $mapping);
432
433 // clone LP settings
434 $obj_settings = new ilLPObjSettings($this->getId());
435 $obj_settings->cloneSettings($new_obj->getId());
436 unset($obj_settings);
437
440 $olp = ilObjectLP::getInstance($this->getId());
441 $collection = $olp->getCollectionInstance();
442 if ($collection) {
443 $collection->cloneCollection($new_obj->getRefId(), $cp_options->getCopyId());
444 }
445
446 return $new_obj;
447 }
448
449 public function copyItems(ilObjMediaCast $a_new_obj): array
450 {
451 $ilUser = $this->user;
452
453 $item_mapping = [];
454 foreach ($this->readItems(true) as $item) {
455 // copy media object
456 $mob_id = $item["mob_id"];
457 $mob = new ilObjMediaObject($mob_id);
458 $new_mob = $mob->duplicate();
459
460 // copy news item
461 // create new media cast item
462 $mc_item = new ilNewsItem();
463 $mc_item->setMobId($new_mob->getId());
464 $mc_item->setContentType(NEWS_AUDIO);
465 $mc_item->setContextObjId($a_new_obj->getId());
466 $mc_item->setContextObjType($a_new_obj->getType());
467 $mc_item->setUserId($ilUser->getId());
468 $mc_item->setPlaytime($item["playtime"] ?? "");
469 $mc_item->setTitle($item["title"] ?? "");
470 $mc_item->setContent($item["content"] ?? "");
471 $mc_item->setVisibility($item["visibility"] ?? "users");
472 $mc_item->create();
473 $this->mob_mapping[$mob_id] = $new_mob->getId();
474 $item_mapping[$item["id"]] = $mc_item->getId();
475 }
476 return $item_mapping;
477 }
478
479 public function handleLPUpdate(
480 int $a_user_id,
481 int $a_mob_id
482 ): void {
483 $this->mob_tracking->saveCompletion(
484 $a_mob_id,
485 $this->getRefId(),
486 $a_user_id
487 );
488 }
489
493 public function addMobToCast(
494 int $mob_id,
495 int $user_id,
496 string $long_desc = "",
497 bool $extract = false
498 ): int {
499 $mob = new ilObjMediaObject($mob_id);
500 if ($extract) {
501 try {
502 $mob->getExternalMetadata();
503 } catch (Exception $e) {
504 }
505 }
506 $news_set = new ilSetting("news");
507 $enable_internal_rss = $news_set->get("enable_rss_for_internal");
508
509 // create new media cast item
510 $mc_item = new ilNewsItem();
511 $mc_item->setMobId($mob->getId());
512 $mc_item->setContentType(NEWS_AUDIO);
513 $mc_item->setContextObjId($this->getId());
514 $mc_item->setContextObjType($this->getType());
515 $mc_item->setUserId($user_id);
516 $med_item = $mob->getMediaItem("Standard");
517 $mc_item->setPlaytime($this->getPlaytimeForSeconds($med_item->getDuration()));
518 $mc_item->setTitle($mob->getTitle());
519 $mc_item->setContent($mob->getLongDescription());
520 if ($long_desc != "") {
521 $mc_item->setContent($long_desc);
522 }
523 $mc_item->setLimitation(false);
524 $mc_item->setVisibility($this->getDefaultAccess() == 0 ? "users" : "public");
525 $mc_item->create();
526
527 $lp = $this->domain->learningProgress($this);
528 $lp->addItemToLP($mob_id);
529
530 return $mc_item->getId();
531 }
532
536 public function getPlaytimeForSeconds(int $seconds): string
537 {
538 $hours = floor($seconds / 3600);
539 $minutes = floor(($seconds % 3600) / 60);
540 $seconds = $seconds % 60;
541 $duration = str_pad($hours, 2, "0", STR_PAD_LEFT) . ":" .
542 str_pad($minutes, 2, "0", STR_PAD_LEFT) . ":" .
543 str_pad($seconds, 2, "0", STR_PAD_LEFT);
544 return $duration;
545 }
546}
const NEWS_AUDIO
static sortArray(array $array, string $a_array_sortby_key, string $a_array_sortorder="asc", bool $a_numeric=false, bool $a_keep_keys=false)
static _write(string $a_type, string $a_setting, string $a_value, int $a_user=0, int $a_block_id=0)
Write setting to database.
static _lookup(string $a_type, string $a_setting, int $a_user=0, int $a_block_id=0)
Lookup setting from database.
static _getInstance(int $a_copy_id)
A news item can be created by different sources.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(int $a_id=0, bool $a_call_by_reference=true)
copyOrder(ilObjMediaCast $newObj, array $mapping)
setDownloadable(bool $a_downloadable)
setPublicFiles(bool $a_publicfiles)
saveOrder(array $a_items)
setNumberInitialVideos(int $a_val)
setItemsArray(array $a_itemsarray)
setAutoplayMode(int $a_val)
setViewMode(string $a_val)
getPlaytimeForSeconds(int $seconds)
Get playtime for seconds.
setNewItemsInLearningProgress(bool $a_val)
Set new items automatically in lp.
ILIAS MediaObjects Tracking TrackingManager $mob_tracking
ILIAS MediaCast InternalDomainService $domain
static array $purposes
create()
note: title, description and type should be set when this function is called
readItems(bool $a_oldest_first=false)
copyItems(ilObjMediaCast $a_new_obj)
setOrder(int $a_value)
handleLPUpdate(int $a_user_id, int $a_mob_id)
setDefaultAccess(int $value)
addMobToCast(int $mob_id, int $user_id, string $long_desc="", bool $extract=false)
Add media object to media cast.
setOnline(bool $a_online)
User class.
static getInstance(int $obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilDBInterface $db
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
$ilUser
Definition: imgupload.php:34
$res
Definition: ltiservices.php:69
$i
Definition: metadata.php:41
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$query