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