ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups 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 
5 require_once "Services/Object/classes/class.ilObject2.php";
6 require_once "class.ilDataCollectionTable.php";
7 require_once "class.ilDataCollectionCache.php";
8 
18 {
19 
20  /*
21  * initType
22  */
23  public function initType()
24  {
25  $this->type = "dcl";
26  }
27 
28  /*
29  * doRead
30  */
31  public function doRead()
32  {
33  global $ilDB;
34 
35  $result = $ilDB->query("SELECT * FROM il_dcl_data WHERE id = ".$ilDB->quote($this->getId(), "integer"));
36 
37  $data = $ilDB->fetchObject($result);
38  $this->setMainTableId($data->main_table_id);
39  $this->setOnline($data->is_online);
40  $this->setRating($data->rating);
41  $this->setApproval($data->approval);
42  $this->setPublicNotes($data->public_notes);
43  $this->setNotification($data->notification);
44  }
45 
46 
47  /*
48  * doCreate
49  * Ceate a New DataCollection Object
50  */
51  protected function doCreate()
52  {
53  global $ilDB;
54 
55  //Create Main Table - The title of the table is per default the title of the data collection object
56  include_once("./Modules/DataCollection/classes/class.ilDataCollectionTable.php");
58  $main_table->setObjId($this->getId());
59  $main_table->setTitle($this->getTitle());
60  $main_table->setAddPerm(1);
61  $main_table->setEditPerm(1);
62  $main_table->setDeletePerm(1);
63  $main_table->setEditByOwner(1);
64  $main_table->setLimited(0);
65  $main_table->doCreate();
66 
67  $ilDB->insert("il_dcl_data", array(
68  "id" => array("integer", $this->getId()),
69  "main_table_id" => array("integer", (int) $main_table->getId()),
70  "is_online" => array("integer", (int) $this->getOnline()),
71  "rating" => array("integer", (int) $this->getRating()),
72  "public_notes" => array("integer", (int) $this->getPublicNotes()),
73  "approval" => array("integer", (int) $this->getApproval()),
74  "notification" => array("integer", (int) $this->getNotification()),
75  ));
76  }
77 
82  public function doClone()
83  {
84  global $x;
85 
86 
87 
88  return true;
89  }
90 
91  /*
92  * doDelete
93  */
94  protected function doDelete()
95  {
96  global $ilDB;
97 
98  foreach($this->getTables() as $table)
99  {
100  $table->doDelete(true);
101  }
102 
103  $query = "DELETE FROM il_dcl_data WHERE id = ".$ilDB->quote($this->getId(), "integer");
104  $ilDB->manipulate($query);
105  }
106 
107  /*
108  * doUpdate
109  */
110  public function doUpdate()
111  {
112  global $ilDB;
113 
114  $ilDB->update("il_dcl_data", array(
115  "id" => array("integer", $this->getId()),
116  "main_table_id" => array("integer", (int) $this->getMainTableId()),
117  "is_online" => array("integer", (int) $this->getOnline()),
118  "rating" => array("integer", (int) $this->getRating()),
119  "public_notes" => array("integer", (int) $this->getPublicNotes()),
120  "approval" => array("integer", (int) $this->getApproval()),
121  "notification" => array("integer", (int) $this->getNotification()),
122  ),
123  array(
124  "id" => array("integer", $this->getId())
125  )
126  );
127  }
128 
129 
130  /*
131  * sendNotification
132  */
133  static function sendNotification($a_action, $a_table_id, $a_record_id = NULL)
134  {
135  global $ilUser, $ilAccess;
136 
137  // If coming from trash, never send notifications and don't load dcl Object
138  if ($_GET['ref_id'] == SYSTEM_FOLDER_ID) {
139  return;
140  }
141 
142  $dclObj = new ilObjDataCollection($_GET['ref_id']);
143 
144  if($dclObj->getNotification() != 1)
145  {
146  return;
147  }
148  $obj_table = ilDataCollectionCache::getTableCache($a_table_id);
149  $obj_dcl = $obj_table->getCollectionObject();
150 
151  // recipients
152  include_once "./Services/Notification/classes/class.ilNotification.php";
154  if(!sizeof($users))
155  {
156  return;
157  }
158 
160 
161  //FIXME $_GET['ref_id]
162  include_once "./Services/Link/classes/class.ilLink.php";
163  $link = ilLink::_getLink($_GET['ref_id']);
164 
165  // prepare mail content
166  // use language of recipient to compose message
167  include_once "./Services/Language/classes/class.ilLanguageFactory.php";
168 
169 
170  // send mails
171  include_once "./Services/Mail/classes/class.ilMail.php";
172  include_once "./Services/User/classes/class.ilObjUser.php";
173  include_once "./Services/Language/classes/class.ilLanguageFactory.php";
174  include_once("./Services/User/classes/class.ilUserUtil.php");
175  include_once("./Services/User/classes/class.ilUserUtil.php");
176  include_once("./Modules/DataCollection/classes/class.ilDataCollectionTable.php");
177 
178  foreach(array_unique($users) as $idx => $user_id)
179  {
180  // the user responsible for the action should not be notified
181  // FIXME $_GET['ref_id]
182  if($user_id != $ilUser->getId() && $ilAccess->checkAccessOfUser($user_id, 'read', '', $_GET['ref_id']))
183  {
184  // use language of recipient to compose message
185  $ulng = ilLanguageFactory::_getLanguageOfUser($user_id);
186  $ulng->loadLanguageModule('dcl');
187 
188  $subject = sprintf($ulng->txt('dcl_change_notification_subject'), $obj_dcl->getTitle());
189  // update/delete
190  $message = $ulng->txt("dcl_hello")." ".ilObjUser::_lookupFullname($user_id).",\n\n";
191  $message .= $ulng->txt('dcl_change_notification_dcl_'.$a_action).":\n\n";
192  $message .= $ulng->txt('obj_dcl').": ".$obj_dcl->getTitle()."\n\n";
193  $message .= $ulng->txt('dcl_table').": ".$obj_table->getTitle()."\n\n";
194  $message .= $ulng->txt('dcl_record').":\n";
195  $message .= "------------------------------------\n";
196  if($a_record_id)
197  {
198  $record = ilDataCollectionCache::getRecordCache($a_record_id);
199  if(!$record->getTableId())
200  $record->setTableId($a_table_id);
201 // $message .= $ulng->txt('dcl_record_id').": ".$a_record_id.":\n";
202  $t = "";
203  foreach($record->getTable()->getVisibleFields() as $field){
204  if($record->getRecordField($field->getId())){
205  $t .= $field->getTitle().": ".$record->getRecordField($field->getId())->getPlainText()."\n";
206  }
207  }
208  $message .= $t."\n";
209  }
210  $message .= "------------------------------------\n";
211  $message .= $ulng->txt('dcl_changed_by').": ".$ilUser->getFullname()." ".ilUserUtil::getNamePresentation($ilUser->getId())."\n\n";
212  $message .= $ulng->txt('dcl_change_notification_link').": ".$link."\n\n";
213 
214  $message .= $ulng->txt('dcl_change_why_you_receive_this_email');
215 
216  $mail_obj = new ilMail(ANONYMOUS_USER_ID);
217  $mail_obj->appendInstallationSignature(true);
218  $mail_obj->sendMail(ilObjUser::_lookupLogin($user_id), "", "", $subject, $message, array(), array("system"));
219  }
220  else
221  {
222  unset($users[$idx]);
223  }
224  }
225  }
226 
230  public function setMainTableId($a_val)
231  {
232  $this->main_table_id = $a_val;
233  }
234 
238  public function getMainTableId()
239  {
240  return $this->main_table_id;
241  }
242 
243 
252  public function doCloneObject(ilObjDataCollection $new_obj, $a_target_id, $a_copy_id = 0)
253  {
254  $new_obj->cloneStructure($this->getRefId());
255 
256  return $new_obj;
257  }
258 
259 
260 
261 
266  public function cloneStructure($original_id)
267  {
268  $original = new ilObjDataCollection($original_id);
269 
270  $this->setApproval($original->getApproval());
271  $this->setNotification($original->getNotification());
272  $this->setOnline($original->getOnline());
273  $this->setPublicNotes($original->getPublicNotes());
274  $this->setRating($original->getRating());
275 
276  //delete old tables.
277  foreach($this->getTables() as $table)
278  {
279  $table->doDelete(true);
280  }
281 
282  //add new tables.
283  foreach($original->getTables() as $table)
284  {
286  $new_table->setObjId($this->getId());
287  $new_table->cloneStructure($table->getId());
288 
289  if($table->getId() == $original->getMainTableId())
290  {
291  $this->setMainTableId($new_table->getId());
292  }
293  }
294 
295  // update because maintable id is now set.
296  $this->doUpdate();
297 
298  // Set new field-ID of referenced fields
299  foreach ($original->getTables() as $origTable) {
300  foreach ($origTable->getRecordFields() as $origField) {
301  if ($origField->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_REFERENCE) {
302  $newRefId = null;
303  $origFieldRefObj = ilDataCollectionCache::getFieldCache($origField->getFieldRef());
304  $origRefTable = ilDataCollectionCache::getTableCache($origFieldRefObj->getTableId());
305  // Lookup the new ID of the referenced field in the actual DC
306  $tableId = ilDataCollectionTable::_getTableIdByTitle($origRefTable->getTitle(), $this->getId());
307  $fieldId = ilDataCollectionField::_getFieldIdByTitle($origFieldRefObj->getTitle(), $tableId);
308  $field = ilDataCollectionCache::getFieldCache($fieldId);
309  $newRefId = $field->getId();
310  // Set the new refID in the actual DC
311  $tableId = ilDataCollectionTable::_getTableIdByTitle($origTable->getTitle(), $this->getId());
312  $fieldId = ilDataCollectionField::_getFieldIdByTitle($origField->getTitle(), $tableId);
313  $field = ilDataCollectionCache::getFieldCache($fieldId);
314  $field->setPropertyvalue($newRefId, ilDataCollectionField::PROPERTYID_REFERENCE);
315  $field->doUpdate();
316  }
317  }
318  }
319 
320  }
321 
322 
326  public function setOnline($a_val)
327  {
328  $this->is_online = $a_val;
329  }
330 
334  public function getOnline()
335  {
336  return $this->is_online;
337  }
338 
342  public function setRating($a_val)
343  {
344  $this->rating = $a_val;
345  }
346 
350  public function getRating()
351  {
352  return $this->rating;
353  }
354 
358  public function setPublicNotes($a_val)
359  {
360  $this->public_notes = $a_val;
361  }
362 
366  public function getPublicNotes()
367  {
368  return $this->public_notes;
369  }
370 
374  public function setApproval($a_val)
375  {
376  $this->approval = $a_val;
377  }
378 
382  public function getApproval()
383  {
384  return $this->approval;
385  }
386 
390  public function setNotification($a_val)
391  {
392  $this->notification = $a_val;
393  }
394 
398  public function getNotification()
399  {
400  return $this->notification;
401  }
402 
403  /*
404  * hasPermissionToAddTable
405  */
406  public function hasPermissionToAddTable()
407  {
408  return self::_checkAccess($this->getId());
409  }
410 
411  /*
412  * _checkAccess
413  */
414  public static function _checkAccess($data_collection_id)
415  {
416  global $ilAccess;
417 
418  $perm = false;
419  $references = self::_getAllReferences($data_collection_id);
420 
421  if($ilAccess->checkAccess("add_entry", "", array_shift($references)))
422  {
423  $perm = true;
424  }
425 
426  return $perm;
427  }
428 
433  public static function _hasWriteAccess($ref)
434  {
435  global $ilAccess;
436 
437  return $ilAccess->checkAccess("write", "", $ref);
438  }
439 
444  public static function _hasReadAccess($ref)
445  {
446  global $ilAccess;
447 
448  return $ilAccess->checkAccess("add_entry", "", $ref);
449  }
450 
455  public function _hasRecords()
456  {
457  /*foreach($this->getTables() as $table)
458  {
459  if($table->_hasRecords())
460  {
461  return true;
462  }
463  }
464  return false;
465  */
466  return true;
467  }
468 
472  public function getTables()
473  {
474  global $ilDB;
475 
476  $query = "SELECT id FROM il_dcl_table WHERE obj_id = ".$ilDB->quote($this->getId(), "integer");
477  $set = $ilDB->query($query);
478  $tables = array();
479 
480  while($rec = $ilDB->fetchAssoc($set))
481  {
482  $tables[$rec['id']] = ilDataCollectionCache::getTableCache($rec['id']);
483  }
484 
485  return $tables;
486  }
487 
488  public function getVisibleTables(){
489  $tables = array();
490  foreach($this->getTables() as $table){
491  if($table->getIsVisible())
492  $tables[$table->getId()] = $table;
493  }
494  return $tables;
495  }
496 
503  public static function _hasTableByTitle($title, $obj_id) {
504  global $ilDB;
505  $result = $ilDB->query('SELECT * FROM il_dcl_table WHERE obj_id = ' . $ilDB->quote($obj_id, 'integer') . ' AND title = ' . $ilDB->quote($title, 'text'));
506  return ($ilDB->numRows($result)) ? true : false;
507  }
508 }
509 
510 ?>