ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilObjDataCollection.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
16 {
17  public function initType()
18  {
19  $this->type = "dcl";
20  }
21 
22 
23  public function doRead()
24  {
25  global $DIC;
26  $ilDB = $DIC['ilDB'];
27 
28  $result = $ilDB->query("SELECT * FROM il_dcl_data WHERE id = " . $ilDB->quote($this->getId(), "integer"));
29 
30  $data = $ilDB->fetchObject($result);
31  $this->setOnline($data->is_online);
32  $this->setRating($data->rating);
33  $this->setApproval($data->approval);
34  $this->setPublicNotes($data->public_notes);
35  $this->setNotification($data->notification);
36  }
37 
38 
39  protected function doCreate($clone_mode = false)
40  {
41  global $DIC;
42  $ilDB = $DIC['ilDB'];
43  $ilLog = $DIC['ilLog'];
44 
45  $ilLog->write('doCreate');
46 
47  if (!$clone_mode) {
48  //Create Main Table - The title of the table is per default the title of the data collection object
49  $main_table = ilDclCache::getTableCache();
50  $main_table->setObjId($this->getId());
51  $main_table->setTitle($this->getTitle());
52  $main_table->setAddPerm(1);
53  $main_table->setEditPerm(1);
54  $main_table->setDeletePerm(0);
55  $main_table->setDeleteByOwner(1);
56  $main_table->setEditByOwner(1);
57  $main_table->setLimited(0);
58  $main_table->setIsVisible(true);
59  $main_table->doCreate();
60  }
61 
62  $ilDB->insert(
63  "il_dcl_data",
64  array(
65  "id" => array("integer", $this->getId()),
66  "is_online" => array("integer", (int) $this->getOnline()),
67  "rating" => array("integer", (int) $this->getRating()),
68  "public_notes" => array("integer", (int) $this->getPublicNotes()),
69  "approval" => array("integer", (int) $this->getApproval()),
70  "notification" => array("integer", (int) $this->getNotification()),
71  )
72  );
73  }
74 
75 
79  public function doClone()
80  {
81  return false;
82  }
83 
84 
85  protected function doDelete()
86  {
87  global $DIC;
88  $ilDB = $DIC['ilDB'];
89 
90  foreach ($this->getTables() as $table) {
91  $table->doDelete(false, true);
92  }
93 
94  $query = "DELETE FROM il_dcl_data WHERE id = " . $ilDB->quote($this->getId(), "integer");
95  $ilDB->manipulate($query);
96  }
97 
98 
99  public function doUpdate()
100  {
101  global $DIC;
102  $ilDB = $DIC['ilDB'];
103 
104  $ilDB->update(
105  "il_dcl_data",
106  array(
107  "id" => array("integer", $this->getId()),
108  "is_online" => array("integer", (int) $this->getOnline()),
109  "rating" => array("integer", (int) $this->getRating()),
110  "public_notes" => array("integer", (int) $this->getPublicNotes()),
111  "approval" => array("integer", (int) $this->getApproval()),
112  "notification" => array("integer", (int) $this->getNotification()),
113  ),
114  array(
115  "id" => array("integer", $this->getId()),
116  )
117  );
118  }
119 
120 
126  public function sendNotification($a_action, $a_table_id, $a_record_id = null)
127  {
128  global $DIC;
129  $ilUser = $DIC['ilUser'];
130  $ilAccess = $DIC['ilAccess'];
131 
132  // If coming from trash, never send notifications and don't load dcl Object
133  if ($_GET['ref_id'] == SYSTEM_FOLDER_ID) {
134  return;
135  }
136 
137  if ($this->getNotification() != 1) {
138  return;
139  }
140  $obj_table = ilDclCache::getTableCache($a_table_id);
141  $obj_dcl = $obj_table->getCollectionObject();
142 
143  // recipients
145  if (!sizeof($users)) {
146  return;
147  }
148 
150 
151  //FIXME $_GET['ref_id]
152  $link = ilLink::_getLink($_GET['ref_id']);
153 
154  // prepare mail content
155  // use language of recipient to compose message
156 
157  // send mails
158  foreach (array_unique($users) as $idx => $user_id) {
159  // the user responsible for the action should not be notified
160  // FIXME $_GET['ref_id]
161  $record = ilDclCache::getRecordCache($a_record_id);
162  $ilDclTable = new ilDclTable($record->getTableId());
163  if ($user_id != $ilUser->getId() && $ilDclTable->hasPermissionToViewRecord(filter_input(INPUT_GET, 'ref_id'), $record, $user_id)) {
164  // use language of recipient to compose message
165  $ulng = ilLanguageFactory::_getLanguageOfUser($user_id);
166  $ulng->loadLanguageModule('dcl');
167 
168  $subject = sprintf($ulng->txt('dcl_change_notification_subject'), $obj_dcl->getTitle());
169  // update/delete
170  $message = $ulng->txt("dcl_hello") . " " . ilObjUser::_lookupFullname($user_id) . ",\n\n";
171  $message .= $ulng->txt('dcl_change_notification_dcl_' . $a_action) . ":\n\n";
172  $message .= $ulng->txt('obj_dcl') . ": " . $obj_dcl->getTitle() . "\n\n";
173  $message .= $ulng->txt('dcl_table') . ": " . $obj_table->getTitle() . "\n\n";
174  $message .= $ulng->txt('dcl_record') . ":\n";
175  $message .= "------------------------------------\n";
176  if ($a_record_id) {
177  if (!$record->getTableId()) {
178  $record->setTableId($a_table_id);
179  }
180  // $message .= $ulng->txt('dcl_record_id').": ".$a_record_id.":\n";
181  $t = "";
182  if ($tableview_id = $record->getTable()->getFirstTableViewId($_GET['ref_id'], $user_id)) {
183  $visible_fields = ilDclTableView::find($tableview_id)->getVisibleFields();
184  if (empty($visible_fields)) {
185  continue;
186  }
188  foreach ($visible_fields as $field) {
189  if ($field->isStandardField()) {
190  $value = $record->getStandardFieldPlainText($field->getId());
191  } elseif ($record_field = $record->getRecordField($field->getId())) {
192  $value = $record_field->getPlainText();
193  }
194 
195  if ($value) {
196  $t .= $field->getTitle() . ": " . $value . "\n";
197  }
198  }
199  }
200  $message .= $this->prepareMessageText($t);
201  }
202  $message .= "------------------------------------\n";
203  $message .= $ulng->txt('dcl_changed_by') . ": " . $ilUser->getFullname() . " " . ilUserUtil::getNamePresentation($ilUser->getId())
204  . "\n\n";
205  $message .= $ulng->txt('dcl_change_notification_link') . ": " . $link . "\n\n";
206 
207  $message .= $ulng->txt('dcl_change_why_you_receive_this_email');
208 
209  $mail_obj = new ilMail(ANONYMOUS_USER_ID);
210  $mail_obj->appendInstallationSignature(true);
211  $mail_obj->enqueue(
212  ilObjUser::_lookupLogin($user_id),
213  "",
214  "",
215  $subject,
216  $message,
217  array()
218  );
219  } else {
220  unset($users[$idx]);
221  }
222  }
223  }
224 
225 
232  public function getFirstVisibleTableId()
233  {
234  global $DIC;
236  $ilDB = $DIC['ilDB'];
237  $ilDB->setLimit(1);
238  $only_visible = ilObjDataCollectionAccess::hasWriteAccess($this->ref_id) ? '' : ' AND is_visible = 1 ';
239  $result = $ilDB->query(
240  'SELECT id
241  FROM il_dcl_table
242  WHERE obj_id = ' . $ilDB->quote($this->getId(), 'integer') .
243  $only_visible . '
244  ORDER BY -table_order DESC '
245  ); //"-table_order DESC" is ASC with NULL last
246 
247  // if there's no visible table, fetch first one not visible
248  // this is to avoid confusion, since the default of a table after creation is not visible
249  if (!$result->numRows() && $only_visible) {
250  $ilDB->setLimit(1);
251  $result = $ilDB->query(
252  'SELECT id
253  FROM il_dcl_table
254  WHERE obj_id = ' . $ilDB->quote($this->getId(), 'integer') . '
255  ORDER BY -table_order DESC '
256  );
257  }
258 
259  return $ilDB->fetchObject($result)->id;
260  }
261 
262 
266  public function reorderTables($table_order)
267  {
268  if ($table_order) {
269  $order = 10;
270  foreach ($table_order as $title) {
271  $table_id = ilDclTable::_getTableIdByTitle($title, $this->getId());
272  $table = ilDclCache::getTableCache($table_id);
273  $table->setOrder($order);
274  $table->doUpdate();
275  $order += 10;
276  }
277  }
278  }
279 
280 
290  public function doCloneObject($new_obj, $a_target_id, $a_copy_id = null, $a_omit_tree = false)
291  {
292 
293  //copy online status if object is not the root copy object
294  $cp_options = ilCopyWizardOptions::_getInstance($a_copy_id);
295 
296  if (!$cp_options->isRootNode($this->getRefId())) {
297  $new_obj->setOnline($this->getOnline());
298  }
299 
300  $new_obj->cloneStructure($this->getRefId());
301 
302  return $new_obj;
303  }
304 
305  //TODO: Find better way to copy data (including references)
306  /*public function doCloneObject(ilObjDataCollection $new_obj, $a_target_id, $a_copy_id = 0) {
307  //$new_obj->delete();
308  $created_new_id = $new_obj->getId();
309  $obj_id = $this->getId();
310 
311  $exp = new ilExport();
312  $exp->exportObject($this->getType(), $obj_id, "5.0.0");
313 
314  $file_name = substr(strrchr($exp->export_run_dir, DIRECTORY_SEPARATOR), 1);
315 
316  $import = new ilImport((int)$a_target_id);
317  $new_id = $import->importObject(null, $exp->export_run_dir.".zip", $file_name.".zip", $this->getType(), "", true);
318 
319  $new_obj->delete();
320 
321  if ($new_id > 0)
322  {
323  $obj = ilObjectFactory::getInstanceByObjId($new_id);
324  $obj->setId($created_new_id);
325 
326  $obj->createReference();
327  $obj->putInTree($a_target_id);
328  $obj->setPermissions($a_target_id);
329 
330 
331  }
332 
333  return $obj;
334  }*/
335 
341  public function cloneStructure($original_id)
342  {
343  $original = new ilObjDataCollection($original_id);
344 
345  $this->setApproval($original->getApproval());
346  $this->setNotification($original->getNotification());
347  $this->setPublicNotes($original->getPublicNotes());
348  $this->setRating($original->getRating());
349 
350  // delete old tables.
351  foreach ($this->getTables() as $table) {
352  $table->doDelete();
353  }
354 
355  // add new tables.
356  foreach ($original->getTables() as $table) {
357  $new_table = new ilDclTable();
358  $new_table->setObjId($this->getId());
359  $new_table->cloneStructure($table);
360  }
361 
362  // mandatory for all cloning functions
364 
365  foreach ($this->getTables() as $table) {
366  $table->afterClone();
367  }
368  }
369 
370 
374  public function setOnline($a_val)
375  {
376  $this->is_online = $a_val;
377  }
378 
379 
383  public function getOnline()
384  {
385  return $this->is_online;
386  }
387 
388 
392  public function setRating($a_val)
393  {
394  $this->rating = $a_val;
395  }
396 
397 
401  public function getRating()
402  {
403  return $this->rating;
404  }
405 
406 
410  public function setPublicNotes($a_val)
411  {
412  $this->public_notes = $a_val;
413  }
414 
415 
419  public function getPublicNotes()
420  {
421  return $this->public_notes;
422  }
423 
424 
428  public function setApproval($a_val)
429  {
430  $this->approval = $a_val;
431  }
432 
433 
437  public function getApproval()
438  {
439  return $this->approval;
440  }
441 
442 
446  public function setNotification($a_val)
447  {
448  $this->notification = $a_val;
449  }
450 
451 
455  public function getNotification()
456  {
457  return $this->notification;
458  }
459 
460 
467  public static function _hasWriteAccess($ref)
468  {
470  }
471 
472 
479  public static function _hasReadAccess($ref)
480  {
482  }
483 
484 
488  public function getTables()
489  {
490  global $DIC;
491  $ilDB = $DIC['ilDB'];
492 
493  $query = "SELECT id FROM il_dcl_table WHERE obj_id = " . $ilDB->quote($this->getId(), "integer") .
494  " ORDER BY -table_order DESC";
495  $set = $ilDB->query($query);
496  $tables = array();
497 
498  while ($rec = $ilDB->fetchAssoc($set)) {
499  $tables[$rec['id']] = ilDclCache::getTableCache($rec['id']);
500  }
501 
502  return $tables;
503  }
504 
505 
506  public function getTableById($table_id)
507  {
508  return ilDclCache::getTableCache($table_id);
509  }
510 
511 
515  public function getVisibleTables()
516  {
517  $tables = array();
518  foreach ($this->getTables() as $table) {
519  if ($table->getIsVisible() && $table->getVisibleTableViews($this->ref_id)) {
520  $tables[$table->getId()] = $table;
521  }
522  }
523 
524  return $tables;
525  }
526 
527 
536  public static function _hasTableByTitle($title, $obj_id)
537  {
538  global $DIC;
539  $ilDB = $DIC['ilDB'];
540  $result = $ilDB->query(
541  'SELECT * FROM il_dcl_table WHERE obj_id = ' . $ilDB->quote($obj_id, 'integer') . ' AND title = '
542  . $ilDB->quote($title, 'text')
543  );
544 
545  return ($ilDB->numRows($result)) ? true : false;
546  }
547 
548 
549  public function getStyleSheetId()
550  {
551  }
552 
553  public function prepareMessageText(string $body): string
554  {
555  if (preg_match_all('/<.*?br.*?>/', $body, $matches)) {
556  $matches = array_unique($matches[0]);
557  $brNewLineMatches = array_map(static function($match): string {
558  return $match . "\n";
559  }, $matches);
560 
561  //Remove carriage return to guarantee all new line can be properly found
562  $body = str_replace("\r", '', $body);
563  //Replace occurrence of <br> + \n with a single \n
564  $body = str_replace($brNewLineMatches, "\n", $body);
565  //Replace additional <br> with a \”
566  $body = str_replace($matches, "\n", $body);
567  //Revert removal of carriage return
568  return str_replace("\n", "\r\n", $body);
569  }
570  return $body;
571  }
572 }
static _lookupLogin($a_user_id)
lookup login
const TYPE_DATACOLLECTION
static setCloneOf($old, $new, $type)
static _getTableIdByTitle($title, $obj_id)
$data
Definition: storeScorm.php:23
const ANONYMOUS_USER_ID
Definition: constants.php:25
$result
setPublicNotes($a_val)
setPublicNotes
$_GET["client_id"]
static _lookupFullname($a_user_id)
Lookup Full Name.
static updateNotificationTime($type, $id, array $user_ids, $page_id=false)
Update the last mail timestamp for given object and users.
doCloneObject($new_obj, $a_target_id, $a_copy_id=null, $a_omit_tree=false)
Clone DCL.
static getTableCache($table_id=0)
Class ilDclBaseFieldModel.
const SYSTEM_FOLDER_ID
Definition: constants.php:33
static getNotificationsForObject($type, $id, $page_id=null, $ignore_threshold=false)
Get all users for given object.
static _getInstance($a_copy_id)
Get instance of copy wizard options.
getId()
get object id public
static getRecordCache($record_id=0)
global $DIC
Definition: goto.php:24
notification()
Definition: notification.php:2
static _hasTableByTitle($title, $obj_id)
Checks if a DataCollection has a table with a given title.
$query
static getNamePresentation( $a_user_id, $a_user_image=false, $a_profile_link=false, $a_profile_back_link="", $a_force_first_lastname=false, $a_omit_login=false, $a_sortable=true, $a_return_data_array=false, $a_ctrl_path="ilpublicuserprofilegui")
Default behaviour is:
static _getLanguageOfUser($a_usr_id)
Get language object of user.
cloneStructure($original_id)
Attention only use this for objects who have not yet been created (use like: $x = new ilObjDataCollec...
setApproval($a_val)
setApproval
global $ilDB
$message
Definition: xapiexit.php:14
$ilUser
Definition: imgupload.php:18
Class ilObjDataCollection.
setNotification($a_val)
setNotification