ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilObjDataCollection.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
23  private bool $is_online = false;
24  private bool $rating = false;
25  private bool $approval = false;
26  private bool $public_notes = false;
27  private bool $notification = false;
28 
29  protected function initType(): void
30  {
31  $this->type = "dcl";
32  }
33 
34  protected function doRead(): void
35  {
36  $result = $this->db->query("SELECT * FROM il_dcl_data WHERE id = " . $this->db->quote($this->getId(), "integer"));
37 
38  $data = $this->db->fetchObject($result);
39  if ($data) {
40  $this->setOnline((bool) $data->is_online);
41  $this->setRating((bool) $data->rating);
42  $this->setApproval((bool) $data->approval);
43  $this->setPublicNotes((bool) $data->public_notes);
44  $this->setNotification((bool) $data->notification);
45  }
46  }
47 
48  protected function doCreate(bool $clone_mode = false): void
49  {
50  $this->log->write('doCreate');
51 
52  if (!$clone_mode) {
53  //Create Main Table - The title of the table is per default the title of the data collection object
54  $main_table = ilDclCache::getTableCache();
55  $main_table->setObjId($this->getId());
56  $main_table->setTitle($this->getTitle());
57  $main_table->setAddPerm(true);
58  $main_table->setEditPerm(true);
59  $main_table->setDeletePerm(false);
60  $main_table->setDeleteByOwner(true);
61  $main_table->setEditByOwner(true);
62  $main_table->setLimited(false);
63  $main_table->setIsVisible(true);
64  $main_table->doCreate();
65  }
66 
67  $this->createMetaData();
68 
69  $this->db->insert(
70  "il_dcl_data",
71  [
72  "id" => ["integer", $this->getId()],
73  "is_online" => ["integer", (int) $this->getOnline()],
74  "rating" => ["integer", (int) $this->getRating()],
75  "public_notes" => ["integer", (int) $this->getPublicNotes()],
76  "approval" => ["integer", (int) $this->getApproval()],
77  "notification" => ["integer", (int) $this->getNotification()],
78  ]
79  );
80  }
81 
82  protected function doDelete(): void
83  {
84  foreach ($this->getTables() as $table) {
85  $table->doDelete(false, true);
86  }
87 
88  $this->deleteMetaData();
89 
90  $query = "DELETE FROM il_dcl_data WHERE id = " . $this->db->quote($this->getId(), "integer");
91  $this->db->manipulate($query);
92  }
93 
94  protected function doUpdate(): void
95  {
96  $this->updateMetaData();
97 
98  $this->db->update(
99  "il_dcl_data",
100  [
101  "id" => ["integer", $this->getId()],
102  "is_online" => ["integer", (int) $this->getOnline()],
103  "rating" => ["integer", (int) $this->getRating()],
104  "public_notes" => ["integer", (int) $this->getPublicNotes()],
105  "approval" => ["integer", (int) $this->getApproval()],
106  "notification" => ["integer", (int) $this->getNotification()],
107  ],
108  [
109  "id" => ["integer", $this->getId()],
110  ]
111  );
112  }
113 
114  public function sendNotification($a_action, $a_table_id, $a_record_id = null): void
115  {
116  global $DIC;
117  $user = $DIC->user();
118 
119  // If coming from trash, never send notifications and don't load dcl Object
120  if ($this->getRefId() === SYSTEM_FOLDER_ID) {
121  return;
122  }
123 
124  if ($this->getNotification() != 1) {
125  return;
126  }
127  $obj_table = ilDclCache::getTableCache($a_table_id);
128 
129  // recipients
132  $this->getId(),
133  1
134  );
135  if (!count($users)) {
136  return;
137  }
138 
140 
141  $link = ilLink::_getLink($this->getRefId());
142 
143  // prepare mail content
144  // use language of recipient to compose message
145 
146  // send mails
147  foreach (array_unique($users) as $idx => $user_id) {
148  // the user responsible for the action should not be notified
149  $record = ilDclCache::getRecordCache($a_record_id);
150  $ilDclTable = new ilDclTable($record->getTableId());
151 
152  if ($user_id != $user->getId() && $ilDclTable->hasPermissionToViewRecord($this->getRefId(), $record, $user_id)) {
153  // use language of recipient to compose message
155  $ulng->loadLanguageModule('dcl');
156 
157  $subject = sprintf($ulng->txt('dcl_change_notification_subject'), $this->getTitle());
158  // update/delete
159  $message = $ulng->txt("dcl_hello") . " " . ilObjUser::_lookupFullname($user_id) . ",\n\n";
160  $message .= $ulng->txt('dcl_change_notification_dcl_' . $a_action) . ":\n\n";
161  $message .= $ulng->txt('obj_dcl') . ": " . $this->getTitle() . "\n\n";
162  $message .= $ulng->txt('dcl_table') . ": " . $obj_table->getTitle() . "\n\n";
163  $message .= $ulng->txt('dcl_record') . ":\n";
164  $message .= "------------------------------------\n";
165  if ($a_record_id) {
166  if (!$record->getTableId()) {
167  $record->setTableId($a_table_id);
168  }
169  // $message .= $ulng->txt('dcl_record_id').": ".$a_record_id.":\n";
170  $t = "";
171 
172  if ($tableview_id = $record->getTable()->getFirstTableViewId($this->getRefId(), $user_id)) {
173  $visible_fields = ilDclTableView::find($tableview_id)->getVisibleFields();
174  if (empty($visible_fields)) {
175  continue;
176  }
178  foreach ($visible_fields as $field) {
179  $value = null;
180  if ($field->isStandardField()) {
181  $value = $record->getStandardFieldPlainText($field->getId());
182  } elseif ($record_field = $record->getRecordField((int) $field->getId())) {
183  $value = $record_field->getPlainText();
184  }
185 
186  if ($value) {
187  $t .= $field->getTitle() . ": " . $value . "\n";
188  }
189  }
190  }
191  $message .= $this->prepareMessageText($t);
192  }
193  $message .= "------------------------------------\n";
194  $message .= $ulng->txt('dcl_changed_by') . ": " . ilUserUtil::getNamePresentation($user->getId())
195  . "\n\n";
196  $message .= $ulng->txt('dcl_change_notification_link') . ": " . $link . "\n\n";
197 
198  $message .= $ulng->txt('dcl_change_why_you_receive_this_email');
199 
200  $mail_obj = new ilMail(ANONYMOUS_USER_ID);
201  $mail_obj->appendInstallationSignature(true);
202  $mail_obj->enqueue(ilObjUser::_lookupLogin($user_id), "", "", $subject, $message, []);
203  } else {
204  unset($users[$idx]);
205  }
206  }
207  }
208 
213  public function getFirstVisibleTableId(): int
214  {
215  $this->db->setLimit(1);
216  $only_visible = ilObjDataCollectionAccess::hasWriteAccess($this->ref_id) ? '' : ' AND is_visible = 1 ';
217  $result = $this->db->query(
218  'SELECT id FROM il_dcl_table
219  WHERE obj_id = ' . $this->db->quote($this->getId(), 'integer') .
220  $only_visible . ' ORDER BY -table_order DESC'
221  ); //"-table_order DESC" is ASC with NULL last
222 
223  // if there's no visible table, fetch first one not visible
224  // this is to avoid confusion, since the default of a table after creation is not visible
225  if (!$result->numRows() && $only_visible) {
226  $this->db->setLimit(1);
227  $result = $this->db->query(
228  'SELECT id FROM il_dcl_table
229  WHERE obj_id = ' . $this->db->quote($this->getId(), 'integer') . ' ORDER BY -table_order DESC '
230  );
231  }
232 
233  return $this->db->fetchObject($result)->id;
234  }
235 
236  public function reorderTables(array $table_order): void
237  {
238  if ($table_order) {
239  $order = 10;
240  foreach ($table_order as $title) {
241  $table_id = ilDclTable::_getTableIdByTitle($title, $this->getId());
242  $table = ilDclCache::getTableCache($table_id);
243  $table->setOrder($order);
244  $table->doUpdate();
245  $order += 10;
246  }
247  }
248  }
249 
257  protected function doCloneObject(ilObject2 $new_obj, int $a_target_id, ?int $a_copy_id = null): void
258  {
259  assert($new_obj instanceof ilObjDataCollection);
260  //copy online status if object is not the root copy object
261  $cp_options = ilCopyWizardOptions::_getInstance($a_copy_id);
262 
263  if (!$cp_options->isRootNode($this->getRefId())) {
264  $new_obj->setOnline(true);
265  }
266 
267  $new_obj->cloneStructure($this->getRefId());
268 
269  $this->cloneMetaData($new_obj);
270  }
271 
276  public function cloneStructure(int $original_id): void
277  {
278  $original = new ilObjDataCollection($original_id);
279 
280  $this->setApproval($original->getApproval());
281  $this->setNotification($original->getNotification());
282  $this->setPublicNotes($original->getPublicNotes());
283  $this->setRating($original->getRating());
284 
285  // delete old tables.
286  foreach ($this->getTables() as $table) {
287  $table->doDelete();
288  }
289 
290  // add new tables.
291  foreach ($original->getTables() as $table) {
292  $new_table = new ilDclTable();
293  $new_table->setObjId($this->getId());
294  $new_table->cloneStructure($table);
295  }
296 
297  // mandatory for all cloning functions
299 
300  foreach ($this->getTables() as $table) {
301  $table->afterClone();
302  }
303  }
304 
308  public function setOnline(bool $a_val): void
309  {
310  $this->is_online = $a_val;
311  }
312 
316  public function getOnline(): bool
317  {
318  return $this->is_online;
319  }
320 
321  public function setRating(bool $a_val): void
322  {
323  $this->rating = $a_val;
324  }
325 
326  public function getRating(): bool
327  {
328  return $this->rating;
329  }
330 
331  public function setPublicNotes(bool $a_val)
332  {
333  $this->public_notes = $a_val;
334  }
335 
336  public function getPublicNotes(): bool
337  {
338  return $this->public_notes;
339  }
340 
341  public function setApproval(bool $a_val): void
342  {
343  $this->approval = $a_val;
344  }
345 
346  public function getApproval(): bool
347  {
348  return $this->approval;
349  }
350 
351  public function setNotification(bool $a_val): void
352  {
353  $this->notification = $a_val;
354  }
355 
356  public function getNotification(): bool
357  {
358  return $this->notification;
359  }
360 
366  public static function _hasWriteAccess(int $ref): bool
367  {
369  }
370 
376  public static function _hasReadAccess(int $ref): bool
377  {
379  }
380 
384  public function getTables(): array
385  {
386  $query = "SELECT id FROM il_dcl_table WHERE obj_id = " . $this->db->quote($this->getId(), "integer") .
387  " ORDER BY title ASC";
388  $set = $this->db->query($query);
389  $tables = [];
390 
391  while ($rec = $this->db->fetchAssoc($set)) {
392  $tables[$rec['id']] = ilDclCache::getTableCache($rec['id']);
393  }
394 
395  return $tables;
396  }
397 
398  public function getTableById(int $table_id): ilDclTable
399  {
400  return ilDclCache::getTableCache($table_id);
401  }
402 
403  public function getVisibleTables(): array
404  {
405  $tables = [];
406  foreach ($this->getTables() as $table) {
407  if ($table->getIsVisible() && $table->getVisibleTableViews($this->ref_id)) {
408  $tables[$table->getId()] = $table;
409  }
410  }
411 
412  return $tables;
413  }
414 
418  public static function _hasTableByTitle(string $title, int $obj_id): bool
419  {
420  global $DIC;
421  $ilDB = $DIC['ilDB'];
422  $result = $ilDB->query(
423  'SELECT * FROM il_dcl_table WHERE obj_id = ' . $ilDB->quote($obj_id, 'integer') . ' AND title = '
424  . $ilDB->quote($title, 'text')
425  );
426 
427  return (bool) $ilDB->numRows($result);
428  }
429 
430  public function getStyleSheetId(): int
431  {
432  return 0;
433  }
434 
435 
436  public function prepareMessageText(string $body): string
437  {
438  if (preg_match_all('/<.*?br.*?>/', $body, $matches)) {
439  $matches = array_unique($matches[0]);
440  $brNewLineMatches = array_map(static function ($match): string {
441  return $match . "\n";
442  }, $matches);
443 
444  //Remove carriage return to guarantee all new line can be properly found
445  $body = str_replace("\r", '', $body);
446  //Replace occurrence of <br> + \n with a single \n
447  $body = str_replace($brNewLineMatches, "\n", $body);
448  //Replace additional <br> with a \”
449  $body = str_replace($matches, "\n", $body);
450  //Revert removal of carriage return
451  return str_replace("\n", "\r\n", $body);
452  }
453  return $body;
454  }
455 }
string $title
const TYPE_DATACOLLECTION
const ANONYMOUS_USER_ID
Definition: constants.php:27
cloneMetaData(ilObject $target_obj)
static _lookupFullname(int $a_user_id)
getFirstVisibleTableId()
for users with write access, return id of table with the lowest sorting for users with no write acces...
static _getTableIdByTitle(string $title, int $obj_id)
static _hasTableByTitle(string $title, int $obj_id)
Checks if a DataCollection has a table with a given title.
doCloneObject(ilObject2 $new_obj, int $a_target_id, ?int $a_copy_id=null)
Clone DCL.
static getNotificationsForObject(int $type, int $id, ?int $page_id=null, bool $ignore_threshold=false)
Get all users/recipients for given object.
const SYSTEM_FOLDER_ID
Definition: constants.php:35
notification()
description: > Example for rendring a notification glyph.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static getNamePresentation( $a_user_id, bool $a_user_image=false, bool $a_profile_link=false, string $a_profile_back_link='', bool $a_force_first_lastname=false, bool $a_omit_login=false, bool $a_sortable=true, bool $a_return_data_array=false, $a_ctrl_path='ilpublicuserprofilegui')
Default behaviour is:
static _getLanguageOfUser(int $a_usr_id)
Get language object of user.
setOnline(bool $a_val)
setOnline
global $DIC
Definition: shib_login.php:22
static getTableCache(?int $table_id=null)
static hasWriteAccess(int $ref, ?int $user_id=0)
static setCloneOf(int $old, int $new, string $type)
static updateNotificationTime(int $type, int $id, array $user_ids, ?int $page_id=null, bool $activate_new_entries=true)
Update the last mail timestamp for given object and users.
static getRecordCache(?int $record_id)
doCreate(bool $clone_mode=false)
static hasReadAccess(int $ref, ?int $user_id=0)
$message
Definition: xapiexit.php:31
static _getInstance(int $a_copy_id)
cloneStructure(int $original_id)
Attention only use this for objects who have not yet been created (use like: $x = new ilObjDataCollec...
ilObjUser $user
reorderTables(array $table_order)
static _lookupLogin(int $a_user_id)