ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
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 protected bool $comments = false;
35 protected \ILIAS\Notes\Service $notes;
36 protected \ILIAS\MediaCast\InternalDomainService $domain;
37 protected \ILIAS\MediaObjects\Tracking\TrackingManager $mob_tracking;
38
39 protected array $itemsarray;
40 protected ilObjUser $user;
41 public static array $purposes = array("Standard");
42 protected bool $online = false;
43 protected bool $publicfiles = false;
44 protected bool $downloadable = true;
45 protected int $order = 0;
46 protected string $view_mode = self::VIEW_VCAST;
47 protected int $defaultAccess = 0; // 0 = logged in users, 1 = public access
48 protected array $mob_mapping = [];
49 protected int $nr_initial_videos = 5;
50 protected bool $new_items_in_lp = true;
51
52 public function __construct(
53 int $a_id = 0,
54 bool $a_call_by_reference = true
55 ) {
56 global $DIC;
57
58 $this->db = $DIC->database();
59 $this->user = $DIC->user();
60 $this->type = "mcst";
61 $mcst_set = new ilSetting("mcst");
62 $this->setDefaultAccess($mcst_set->get("defaultaccess") == "users" ? 0 : 1);
63 $this->setOrder(self::ORDER_CREATION_DATE_DESC);
64 $this->mob_tracking = $DIC->mediaObjects()->internal()
65 ->domain()
66 ->tracking();
67 $this->notes = $DIC->notes();
68 $this->domain = $DIC->mediaCast()->internal()->domain();
69 parent::__construct($a_id, $a_call_by_reference);
70 }
71
72 public function setOnline(bool $a_online): void
73 {
74 $this->online = $a_online;
75 }
76
77 public function getOnline(): bool
78 {
79 return $this->online;
80 }
81
82 public function setComments(bool $comments): void
83 {
84 $this->comments = $comments;
85 }
86
87 public function getComments(): bool
88 {
89 return $this->comments;
90 }
91
92 public function setPublicFiles(bool $a_publicfiles): void
93 {
94 $this->publicfiles = $a_publicfiles;
95 }
96
97 public function getPublicFiles(): bool
98 {
99 return $this->publicfiles;
100 }
101
102 public function setViewMode(string $a_val): void
103 {
104 $this->view_mode = $a_val;
105 }
106
107 public function getViewMode(): string
108 {
109 return $this->view_mode;
110 }
111
112 public function setItemsArray(array $a_itemsarray): void
113 {
114 $this->itemsarray = $a_itemsarray;
115 }
116
117 public function getItemsArray(): array
118 {
119 return $this->itemsarray;
120 }
121
122 public function setNumberInitialVideos(int $a_val): void
123 {
124 $this->nr_initial_videos = $a_val;
125 }
126
127 public function getNumberInitialVideos(): int
128 {
130 }
131
135 public function setNewItemsInLearningProgress(bool $a_val): void
136 {
137 $this->new_items_in_lp = $a_val;
138 }
139
140 public function getNewItemsInLearningProgress(): bool
141 {
143 }
144
145 public function getSortedItemsArray(): array
146 {
147 $med_items = $this->getItemsArray();
148 // sort by order setting
149 switch ($this->getOrder()) {
151 $med_items = ilArrayUtil::sortArray($med_items, "title", "asc", false, true);
152 break;
153
155 $med_items = ilArrayUtil::sortArray($med_items, "creation_date", "asc", false, true);
156 break;
157
159 $med_items = ilArrayUtil::sortArray($med_items, "creation_date", "desc", false, true);
160 break;
161
163 $order = array_flip($this->readOrder());
164 $pos = sizeof($order);
165 foreach (array_keys($med_items) as $idx) {
166 if (array_key_exists($idx, $order)) {
167 $med_items[$idx]["order"] = ($order[$idx] + 1) * 10;
168 }
169 // item has no order yet
170 else {
171 $med_items[$idx]["order"] = (++$pos) * 10;
172 }
173 }
174
175 $med_items = ilArrayUtil::sortArray($med_items, "order", "asc", true, true);
176 break;
177 }
178 return $med_items;
179 }
180
181 public function setDownloadable(bool $a_downloadable): void
182 {
183 $this->downloadable = $a_downloadable;
184 }
185
186 public function getDownloadable(): bool
187 {
188 return $this->downloadable;
189 }
190
191 public function getDefaultAccess(): int
192 {
194 }
195
196 public function setDefaultAccess(int $value): void
197 {
198 $this->defaultAccess = ($value === 0) ? 0 : 1;
199 }
200
201 public function setOrder(int $a_value): void
202 {
203 $this->order = $a_value;
204 }
205
206 public function getOrder(): int
207 {
208 return $this->order;
209 }
210
211
212 public function create(): int
213 {
215
216 $id = parent::create();
217
218 $this->createMetaData();
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(0, "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 $this->updateMetaData();
256
257 // update media cast data
258 $query = "UPDATE il_media_cast_data SET " .
259 " is_online = " . $ilDB->quote((int) $this->getOnline(), "integer") .
260 ", public_files = " . $ilDB->quote((int) $this->getPublicFiles(), "integer") .
261 ", downloadable = " . $ilDB->quote((int) $this->getDownloadable(), "integer") .
262 ", def_access = " . $ilDB->quote($this->getDefaultAccess(), "integer") .
263 ", sortmode = " . $ilDB->quote($this->getOrder(), "integer") .
264 ", viewmode = " . $ilDB->quote($this->getViewMode(), "text") .
265 ", autoplaymode = " . $ilDB->quote(0, "integer") .
266 ", nr_initial_videos = " . $ilDB->quote($this->getNumberInitialVideos(), "integer") .
267 ", new_items_in_lp = " . $ilDB->quote($this->getNewItemsInLearningProgress(), "integer") .
268 " WHERE id = " . $ilDB->quote($this->getId(), "integer");
269
270 $ilDB->manipulate($query);
271
272 $this->notes->domain()->activateComments($this->getId(), $this->getComments());
273
274 return true;
275 }
276
277 public function read(): void
278 {
280
281 parent::read();
282 $this->readItems();
283
284 $query = "SELECT * FROM il_media_cast_data WHERE id = " .
285 $ilDB->quote($this->getId(), "integer");
286 $set = $ilDB->query($query);
287 $rec = $ilDB->fetchAssoc($set);
288
289 $this->setOnline((bool) $rec["is_online"]);
290 $this->setPublicFiles((bool) $rec["public_files"]);
291 $this->setDownloadable((bool) $rec["downloadable"]);
292 $this->setDefaultAccess((int) $rec["def_access"]);
293 $this->setOrder((int) $rec["sortmode"]);
294 $this->setViewMode((string) $rec["viewmode"]);
295 $this->setNumberInitialVideos((int) $rec["nr_initial_videos"]);
296 $this->setNewItemsInLearningProgress((bool) $rec["new_items_in_lp"]);
297
298 $this->setComments($this->notes->domain()->commentsActive($this->getId()));
299 }
300
301
302 public function delete(): bool
303 {
305
306 // always call parent delete function first!!
307 if (!parent::delete()) {
308 return false;
309 }
310
311 $this->deleteMetaData();
312
313 // delete all items
314 $med_items = $this->getItemsArray();
315 foreach ($med_items as $item) {
316 $news_item = new ilNewsItem($item["id"]);
317 $news_item->delete();
318 }
319
320 $this->deleteOrder();
321
322 // delete record of table il_media_cast_data
323 $query = "DELETE FROM il_media_cast_data" .
324 " WHERE id = " . $ilDB->quote($this->getId(), "integer");
325 $ilDB->manipulate($query);
326
327 return true;
328 }
329
330 public function readItems(bool $a_oldest_first = false): array
331 {
332 //
333 $it = new ilNewsItem();
334 $it->setContextObjId($this->getId());
335 $it->setContextObjType($this->getType());
336 $this->itemsarray = $it->queryNewsForContext(false, 0, "", false, $a_oldest_first);
337
338 return $this->itemsarray;
339 }
340
341 public function deleteOrder(): void
342 {
344
345 if (!$this->getId()) {
346 return;
347 }
348
349 $sql = "DELETE FROM il_media_cast_data_ord" .
350 " WHERE obj_id = " . $ilDB->quote($this->getId(), "integer");
351 $ilDB->manipulate($sql);
352 }
353
354 public function readOrder(): ?array
355 {
357
358 if (!$this->getId()) {
359 return null;
360 }
361
362 $all = array();
363 $sql = "SELECT item_id FROM il_media_cast_data_ord" .
364 " WHERE obj_id = " . $ilDB->quote($this->getId(), "integer") .
365 " ORDER BY pos";
366 $res = $ilDB->query($sql);
367 while ($row = $ilDB->fetchAssoc($res)) {
368 $all[] = $row["item_id"];
369 }
370 return $all;
371 }
372
373 public function saveOrder(array $a_items): void
374 {
376
377 if (!$this->getId()) {
378 return;
379 }
380 $this->deleteOrder();
381
382 $pos = 0;
383 foreach ($a_items as $item_id) {
384 $pos++;
385
386 $sql = "INSERT INTO il_media_cast_data_ord (obj_id,item_id,pos)" .
387 " VALUES (" . $ilDB->quote($this->getId(), "integer") . "," .
388 $ilDB->quote($item_id, "integer") . "," .
389 $ilDB->quote($pos, "integer") . ")";
390 $ilDB->manipulate($sql);
391 }
392 }
393
394 protected function copyOrder(
395 ilObjMediaCast $newObj,
396 array $mapping
397 ): void {
398 $items = [];
399 foreach ($this->readOrder() as $i) {
400 if (!array_key_exists($i, $mapping)) {
401 continue;
402 }
403 $items[] = $mapping[$i];
404 }
405 $newObj->saveOrder($items);
406 }
407
408 public function cloneObject(int $a_target_id, int $a_copy_id = 0, bool $a_omit_tree = false): ?ilObject
409 {
411 $new_obj = parent::cloneObject($a_target_id, $a_copy_id, $a_omit_tree);
412
413 //copy online status if object is not the root copy object
414 $cp_options = ilCopyWizardOptions::_getInstance($a_copy_id);
415
416 if (!$cp_options->isRootNode($this->getRefId())) {
417 $new_obj->setOnline($this->getOnline());
418 }
419
420 $new_obj->setPublicFiles($this->getPublicFiles());
421 $new_obj->setDownloadable($this->getDownloadable());
422 $new_obj->setDefaultAccess($this->getDefaultAccess());
423 $new_obj->setOrder($this->getOrder());
424 $new_obj->setViewMode($this->getViewMode());
425 $new_obj->setNumberInitialVideos($this->getNumberInitialVideos());
426 $new_obj->setNewItemsInLearningProgress($this->getNewItemsInLearningProgress());
427
428 $new_obj->update();
429
430 $pf = ilBlockSetting::_lookup("news", "public_feed", 0, $this->getId());
431 $keeprss = (int) ilBlockSetting::_lookup("news", "keep_rss_min", 0, $this->getId());
432 ilBlockSetting::_write("news", "public_feed", (string) $pf, 0, $new_obj->getId());
433 ilBlockSetting::_write("news", "keep_rss_min", (string) $keeprss, 0, $new_obj->getId());
434
435 // copy items
436 $mapping = $this->copyItems($new_obj);
437
438 $this->copyOrder($new_obj, $mapping);
439
440 // clone LP settings
441 $obj_settings = new ilLPObjSettings($this->getId());
442 $obj_settings->cloneSettings($new_obj->getId());
443 unset($obj_settings);
444
446 $olp = ilObjectLP::getInstance($this->getId());
448 $collection = $olp->getCollectionInstance();
449 if ($collection) {
450 $collection->cloneCollection($new_obj->getRefId(), $cp_options->getCopyId(), $this->mob_mapping);
451 }
452
453 $this->cloneMetaData($new_obj);
454
455 return $new_obj;
456 }
457
458 public function copyItems(ilObjMediaCast $a_new_obj): array
459 {
460 $ilUser = $this->user;
461
462 $item_mapping = [];
463 foreach ($this->readItems(true) as $item) {
464 // copy media object
465 $mob_id = $item["mob_id"];
466 $mob = new ilObjMediaObject($mob_id);
467 $new_mob = $mob->duplicate();
468
469 // copy news item
470 // create new media cast item
471 $mc_item = new ilNewsItem();
472 $mc_item->setMobId($new_mob->getId());
473 $mc_item->setContentType(NEWS_AUDIO);
474 $mc_item->setContextObjId($a_new_obj->getId());
475 $mc_item->setContextObjType($a_new_obj->getType());
476 $mc_item->setUserId($ilUser->getId());
477 $mc_item->setPlaytime($item["playtime"] ?? "");
478 $mc_item->setTitle($item["title"] ?? "");
479 $mc_item->setContent($item["content"] ?? "");
480 $mc_item->setVisibility($item["visibility"] ?? "users");
481 $mc_item->create();
482 $this->mob_mapping[$mob_id] = $new_mob->getId();
483 $item_mapping[$item["id"]] = $mc_item->getId();
484 }
485 return $item_mapping;
486 }
487
488 public function handleLPUpdate(
489 int $a_user_id,
490 int $a_mob_id
491 ): void {
492 $this->mob_tracking->saveCompletion(
493 $a_mob_id,
494 $this->getRefId(),
495 $a_user_id
496 );
497 }
498
502 public function addMobToCast(
503 int $mob_id,
504 int $user_id,
505 string $long_desc = "",
506 bool $extract = false
507 ): int {
508 $mob = new ilObjMediaObject($mob_id);
509 if ($extract) {
510 try {
511 $mob->getExternalMetadata();
512 } catch (Exception $e) {
513 }
514 }
515 $news_set = new ilSetting("news");
516 $enable_internal_rss = $news_set->get("enable_rss_for_internal");
517
518 // create new media cast item
519 $mc_item = new ilNewsItem();
520 $mc_item->setMobId($mob->getId());
521 $mc_item->setContentType(NEWS_AUDIO);
522 $mc_item->setContextObjId($this->getId());
523 $mc_item->setContextObjType($this->getType());
524 $mc_item->setUserId($user_id);
525 $med_item = $mob->getMediaItem("Standard");
526 $mc_item->setPlaytime($this->getPlaytimeForSeconds($med_item->getDuration()));
527 $mc_item->setTitle($mob->getTitle());
528 $mc_item->setContent($mob->getLongDescription());
529 if ($long_desc != "") {
530 $mc_item->setContent($long_desc);
531 }
532 $mc_item->setLimitation(false);
533 $mc_item->setVisibility($this->getDefaultAccess() == 0 ? "users" : "public");
534 $mc_item->create();
535
536 $lp = $this->domain->learningProgress($this);
537 $lp->addItemToLP($mob_id);
538
539 return $mc_item->getId();
540 }
541
545 public function getPlaytimeForSeconds(int $seconds): string
546 {
547 $hours = floor($seconds / 3600);
548 $minutes = floor(($seconds % 3600) / 60);
549 $seconds = $seconds % 60;
550 $duration = str_pad($hours, 2, "0", STR_PAD_LEFT) . ":" .
551 str_pad($minutes, 2, "0", STR_PAD_LEFT) . ":" .
552 str_pad($seconds, 2, "0", STR_PAD_LEFT);
553 return $duration;
554 }
555}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$duration
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)
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
setComments(bool $comments)
static array $purposes
ILIAS Notes Service $notes
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)
Class ilObject Basic functions for all objects.
ilDBInterface $db
ILIAS Setting Class.
$res
Definition: ltiservices.php:69
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26