ILIAS  Release_4_4_x_branch Revision 61816
 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 
296  // update because maintable id is now set.
297  $this->doUpdate();
298 
299  // Set new field-ID of referenced fields
300  foreach ($original->getTables() as $origTable) {
301  foreach ($origTable->getRecordFields() as $origField) {
302  if ($origField->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_REFERENCE) {
303  $newRefId = null;
304  $origFieldRefObj = ilDataCollectionCache::getFieldCache($origField->getFieldRef());
305  $origRefTable = ilDataCollectionCache::getTableCache($origFieldRefObj->getTableId());
306  // Lookup the new ID of the referenced field in the actual DC
307  $tableId = ilDataCollectionTable::_getTableIdByTitle($origRefTable->getTitle(), $this->getId());
308  $fieldId = ilDataCollectionField::_getFieldIdByTitle($origFieldRefObj->getTitle(), $tableId);
309  $field = ilDataCollectionCache::getFieldCache($fieldId);
310  $newRefId = $field->getId();
311  // Set the new refID in the actual DC
312  $tableId = ilDataCollectionTable::_getTableIdByTitle($origTable->getTitle(), $this->getId());
313  $fieldId = ilDataCollectionField::_getFieldIdByTitle($origField->getTitle(), $tableId);
314  $field = ilDataCollectionCache::getFieldCache($fieldId);
315  $field->setPropertyvalue($newRefId, ilDataCollectionField::PROPERTYID_REFERENCE);
316  $field->doUpdate();
317  }
318  }
319  }
320 
321  }
322 
323 
327  public function setOnline($a_val)
328  {
329  $this->is_online = $a_val;
330  }
331 
335  public function getOnline()
336  {
337  return $this->is_online;
338  }
339 
343  public function setRating($a_val)
344  {
345  $this->rating = $a_val;
346  }
347 
351  public function getRating()
352  {
353  return $this->rating;
354  }
355 
359  public function setPublicNotes($a_val)
360  {
361  $this->public_notes = $a_val;
362  }
363 
367  public function getPublicNotes()
368  {
369  return $this->public_notes;
370  }
371 
375  public function setApproval($a_val)
376  {
377  $this->approval = $a_val;
378  }
379 
383  public function getApproval()
384  {
385  return $this->approval;
386  }
387 
391  public function setNotification($a_val)
392  {
393  $this->notification = $a_val;
394  }
395 
399  public function getNotification()
400  {
401  return $this->notification;
402  }
403 
404  /*
405  * hasPermissionToAddTable
406  */
407  public function hasPermissionToAddTable()
408  {
409  return self::_checkAccess($this->getId());
410  }
411 
412  /*
413  * _checkAccess
414  */
415  public static function _checkAccess($data_collection_id)
416  {
417  global $ilAccess;
418 
419  $perm = false;
420  $references = self::_getAllReferences($data_collection_id);
421 
422  if($ilAccess->checkAccess("add_entry", "", array_shift($references)))
423  {
424  $perm = true;
425  }
426 
427  return $perm;
428  }
429 
434  public static function _hasWriteAccess($ref)
435  {
436  global $ilAccess;
437 
438  return $ilAccess->checkAccess("write", "", $ref);
439  }
440 
445  public static function _hasReadAccess($ref)
446  {
447  global $ilAccess;
448 
449  return $ilAccess->checkAccess("add_entry", "", $ref);
450  }
451 
456  public function _hasRecords()
457  {
458  /*foreach($this->getTables() as $table)
459  {
460  if($table->_hasRecords())
461  {
462  return true;
463  }
464  }
465  return false;
466  */
467  return true;
468  }
469 
473  public function getTables()
474  {
475  global $ilDB;
476 
477  $query = "SELECT id FROM il_dcl_table WHERE obj_id = ".$ilDB->quote($this->getId(), "integer");
478  $set = $ilDB->query($query);
479  $tables = array();
480 
481  while($rec = $ilDB->fetchAssoc($set))
482  {
483  $tables[$rec['id']] = ilDataCollectionCache::getTableCache($rec['id']);
484  }
485 
486  return $tables;
487  }
488 
489  public function getVisibleTables(){
490  $tables = array();
491  foreach($this->getTables() as $table){
492  if($table->getIsVisible())
493  $tables[$table->getId()] = $table;
494  }
495  return $tables;
496  }
497 
504  public static function _hasTableByTitle($title, $obj_id) {
505  global $ilDB;
506  $result = $ilDB->query('SELECT * FROM il_dcl_table WHERE obj_id = ' . $ilDB->quote($obj_id, 'integer') . ' AND title = ' . $ilDB->quote($title, 'text'));
507  return ($ilDB->numRows($result)) ? true : false;
508  }
509 }
510 
511 ?>