ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilMemberViewSettings.php
Go to the documentation of this file.
1 <?php
2 
20 use ILIAS\Container;
21 
27 {
28  public const SESSION_MEMBER_VIEW_CONTAINER = 'member_view_container';
29  protected ilLogger $logger;
31  protected ilCtrl $ctrl;
32  protected ilTree $tree;
33  protected ilSetting $settings;
34  private static ?ilMemberViewSettings $instance = null;
35  private bool $active = false;
36  private bool $enabled = false;
37  private ?int $container = null;
38  private array $container_items = [];
39  private int $current_ref_id = 0;
40  protected Container\Service $container_service;
41 
42  private function __construct()
43  {
44  global $DIC;
45 
46  $this->tree = $DIC->repositoryTree();
47  $this->settings = $DIC->settings();
48  $this->request = $DIC->http()->request();
49  $this->ctrl = $DIC->ctrl();
50  $this->logger = $DIC->logger()->cont();
51  $this->read();
52  $this->container_service = $DIC->container();
53  }
54 
55  public static function getInstance(): ilMemberViewSettings
56  {
57  return self::$instance ?? (self::$instance = new ilMemberViewSettings());
58  }
59 
60  public function getContainer(): ?int
61  {
62  return $this->container;
63  }
64 
65  public function getCurrentRefId(): int
66  {
67  return $this->current_ref_id;
68  }
69 
70  public function setContainer(int $container): void
71  {
72  $this->container = $container;
73  ilSession::set(self::SESSION_MEMBER_VIEW_CONTAINER, $this->container);
74  $this->container_service
75  ->internal()
76  ->domain()
77  ->content()
78  ->view()
79  ->setContentView();
80  }
81 
85  public function isActive(): bool
86  {
87  static $mv_status;
88  if (!isset($mv_status)) {
89  if (!$this->active) {
90  // Not active
91  return $mv_status = false;
92  }
93 
94  if (!$this->getCurrentRefId()) {
95  // No ref id given => mail, search, personal desktop menu in other tab
96  return $mv_status = false;
97  }
98 
99  if (!in_array($this->getCurrentRefId(), $this->container_items) &&
100  $this->getContainer() !== $this->getCurrentRefId()
101  ) {
102  // outside of course
103  return $mv_status = false;
104  }
105  return $mv_status = true;
106  }
107 
108  return $mv_status;
109  }
110 
114  public function isActiveForRefId(int $a_ref_id): bool
115  {
116  if (!$this->active || !$a_ref_id) {
117  return false;
118  }
119 
120  if (
121  !in_array($a_ref_id, $this->container_items) &&
122  $this->getContainer() !== $a_ref_id) {
123  return false;
124  }
125  return true;
126  }
127 
131  public function activate(int $a_ref_id): void
132  {
133  $this->active = true;
134  $this->setContainer($a_ref_id);
135  }
136 
137  public function deactivate(): void
138  {
139  $this->active = false;
140  $this->container = null;
141  ilSession::clear(self::SESSION_MEMBER_VIEW_CONTAINER);
142  }
143 
147  public function toggleActivation(int $a_ref_id, bool $a_activation): void
148  {
149  if ($a_activation) {
150  $this->activate($a_ref_id);
151  } else {
152  $this->deactivate();
153  }
154  }
155 
159  public function isEnabled(): bool
160  {
161  return $this->enabled;
162  }
163 
167  protected function read(): void
168  {
169  $current_ref_id = $this->findEffectiveRefId();
170 
171  // member view is always enabled
172  // (2013-06-18, http://www.ilias.de/docu/goto_docu_wiki_1357_Reorganising_Administration.html)
173 
174  // $this->enabled = $ilSetting->get('preview_learner');
175  $this->enabled = true;
176 
177  if (ilSession::get(self::SESSION_MEMBER_VIEW_CONTAINER)) {
178  $this->active = true;
179  $this->container = (int) ilSession::get(self::SESSION_MEMBER_VIEW_CONTAINER);
180  $this->container_items = $this->tree->getSubTreeIds($this->getContainer());
181 
182  // deactivate if out of scope
183  if (
184  $this->getCurrentRefId() &&
185  !in_array($this->getCurrentRefId(), $this->container_items) &&
186  $this->getCurrentRefId() !== $this->getContainer()
187  ) {
188  $this->deactivate();
189  }
190  }
191  }
192 
196  protected function findEffectiveRefId(): int
197  {
198  if ($this->ctrl->isAsynch()) {
199  // Ignore asynchronous requests
200  return 0;
201  }
202 
203  $ref_id = (int) ($this->request->getQueryParams()['ref_id'] ?? 0);
204  if ($ref_id) {
205  return $this->current_ref_id = $ref_id;
206  }
207  $target_str = (string) ($this->request->getQueryParams()['target'] ?? '');
208  if ($target_str !== '') {
209  $target_arr = explode('_', $target_str);
210  if (isset($target_arr[1]) && (int) $target_arr[1]) {
211  $this->current_ref_id = (int) $target_arr[1];
212  }
213  }
214  return 0;
215  }
216 }
static get(string $a_var)
activate(int $a_ref_id)
Enable member view for this session and container.
$target_arr
Definition: goto.php:50
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
isActiveForRefId(int $a_ref_id)
Check if member view is currently enabled for given ref id.
Container Service $container_service
global $DIC
Definition: feed.php:28
$ref_id
Definition: ltiauth.php:67
toggleActivation(int $a_ref_id, bool $a_activation)
Toggle activation status.
isActive()
Check if member view currently enabled.
static ilMemberViewSettings $instance
isEnabled()
Check if members view is enabled in the administration.
findEffectiveRefId()
Find effective ref_id for request.
static clear(string $a_var)
static set(string $a_var, $a_val)
Set a value.