ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilDashboardRecommendedContentGUI.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
11 {
15  protected $user;
16 
20  protected $rec_manager;
21 
25  protected $recommendations;
26 
30  public static $list_by_type = [];
31 
32 
36  protected $ui;
37 
41  protected $lng;
42 
46  protected $ctrl;
47 
51  protected $settings;
52 
56  protected $fav_manager;
57 
61  public function __construct()
62  {
63  global $DIC;
64 
65  $this->user = $DIC->user();
66  $this->rec_manager = new ilRecommendedContentManager();
67  $this->fav_manager = new ilFavouritesManager();
68  $this->objDefinition = $DIC["objDefinition"];
69  $this->ui = $DIC->ui();
70  $this->lng = $DIC->language();
71  $this->ctrl = $DIC->ctrl();
72  $this->settings = $DIC->settings();
73 
74  $this->lng->loadLanguageModule("rep");
75 
76  $this->requested_item_ref_id = (int) $_GET["item_ref_id"];
77 
78  $this->recommendations = $this->rec_manager->getOpenRecommendationsOfUser($this->user->getId());
79  }
80 
84  public function executeCommand()
85  {
87 
88  $next_class = $ctrl->getNextClass($this);
89  $cmd = $ctrl->getCmd();
90 
91  switch ($next_class) {
92  default:
93  if (in_array($cmd, array("remove", "makeFavourite"))) {
94  $this->$cmd();
95  }
96  }
97  }
98 
104  public function render()
105  {
106  if (count($this->recommendations) == 0) {
107  return "";
108  }
109  return $this->ui->renderer()->render(
110  $this->ui->factory()->panel()->listing()->standard(
111  $this->lng->txt("rep_recommended_content"),
112  $this->getListItemGroups()
113  )
114  );
115  }
116 
122  protected function getListItemGroups() : array
123  {
124  global $DIC;
125  $factory = $DIC->ui()->factory();
126 
127  $item_groups = [];
128  $list_items = [];
129 
130  foreach ($this->recommendations as $ref_id) {
131  try {
132  if (!$DIC->access()->checkAccess('visible', '', $ref_id)) {
133  continue;
134  }
135  $list_items[] = $this->getListItemForData($ref_id);
136  } catch (ilException $e) {
137  continue;
138  }
139  }
140 
141  $item_groups[] = $factory->item()->group("", $list_items);
142 
143  return $item_groups;
144  }
145 
146 
150  protected function getListItemForData($ref_id) : \ILIAS\UI\Component\Item\Item
151  {
152  $short_desc = $this->settings->get("rep_shorten_description");
153  $short_desc_max_length = $this->settings->get("rep_shorten_description_length");
154  $ctrl = $this->ctrl;
155 
156  $obj_id = ilObject::_lookupObjectId($ref_id);
157  $type = ilObject::_lookupType($obj_id);
158  $title = ilObject::_lookupTitle($obj_id);
159  $desc = ilObject::_lookupDescription($obj_id);
160  if ($short_desc && $short_desc_max_length) {
161  $desc = ilUtil::shortenText($desc, $short_desc_max_length, true);
162  }
163  $item = [
164  "ref_id" => $ref_id,
165  "obj_id" => $obj_id,
166  "type" => $type,
167  "title" => $title,
168  "description" => $desc,
169  ];
170 
172  $item_gui = $this->byType($type);
174 
175  $ctrl->setParameter($this, "item_ref_id", $ref_id);
176 
177  $item_gui->addCustomCommand(
178  $ctrl->getLinkTarget($this, "remove"),
179  "dash_remove_from_list"
180  );
181 
182  $item_gui->addCustomCommand(
183  $ctrl->getLinkTarget($this, "makeFavourite"),
184  "dash_make_favourite"
185  );
186 
187  $ctrl->clearParameterByClass(self::class, "item_ref_id");
188 
189 
190  $list_item = $item_gui->getAsListItem(
191  (int) $ref_id,
192  (int) $obj_id,
193  (string) $type,
194  (string) $title,
195  (string) $desc
196  );
197 
198  return $list_item;
199  }
200 
206  public function byType($a_type)
207  {
209  if (!array_key_exists($a_type, self::$list_by_type)) {
210  $class = $this->objDefinition->getClassName($a_type);
211  if (!$class) {
212  throw new ilException(sprintf("Could not find a class for object type: %s", $a_type));
213  }
214 
215  $location = $this->objDefinition->getLocation($a_type);
216  if (!$location) {
217  throw new ilException(sprintf("Could not find a class location for object type: %s", $a_type));
218  }
219 
220  $full_class = 'ilObj' . $class . 'ListGUI';
221  $item_list_gui = new $full_class();
222 
223  $item_list_gui->setContainerObject($this);
224  $item_list_gui->enableNotes(false);
225  $item_list_gui->enableComments(false);
226  $item_list_gui->enableTags(false);
227 
228  $item_list_gui->enableIcon(true);
229  $item_list_gui->enableDelete(false);
230  $item_list_gui->enableCut(false);
231  $item_list_gui->enableCopy(false);
232  $item_list_gui->enableLink(false);
233  $item_list_gui->enableInfoScreen(true);
234  //$item_list_gui->enableSubscribe($this->block->getViewSettings()->enabledSelectedItems());
235 
236  $item_list_gui->enableCommands(true, true);
237 
238  self::$list_by_type[$a_type] = $item_list_gui;
239  }
240 
241  return (clone self::$list_by_type[$a_type]);
242  }
243 
247  protected function remove()
248  {
249  $ctrl = $this->ctrl;
250  $lng = $this->lng;
251  $this->rec_manager->declineObjectRecommendation($this->user->getId(), $this->requested_item_ref_id);
252  ilUtil::sendSuccess($lng->txt("dash_item_removed"), true);
253  $ctrl->returnToParent($this);
254  }
255 
256 
260  protected function makeFavourite()
261  {
262  $ctrl = $this->ctrl;
263  $lng = $this->lng;
264  $this->fav_manager->add($this->user->getId(), $this->requested_item_ref_id);
265  ilUtil::sendSuccess($lng->txt("dash_added_to_favs"), true);
266  $ctrl->returnToParent($this);
267  }
268 }
settings()
Definition: settings.php:2
static shortenText( $a_str, $a_len, $a_dots=false, $a_next_blank=false, $a_keep_extension=false)
shorten a string to given length.
$type
$_GET["client_id"]
$location
Definition: buildRTE.php:44
static _lookupTitle($a_id)
lookup object title
Manages favourites, currently the interface for other components, needs discussion.
user()
Definition: user.php:4
static _lookupObjectId($a_ref_id)
lookup object id
static _lookupDescription($a_id)
lookup object description
Common interface to all items.
Definition: Item.php:10
global $DIC
Definition: goto.php:24
ui()
Definition: ui.php:5
static addListGUIActivationProperty(ilObjectListGUI $a_list_gui, array &$a_item)
Get timing details for list gui.
static _lookupType($a_id, $a_reference=false)
lookup object type
Recommended content manager (business logic)
$factory
Definition: metadata.php:58