ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCalendarShared.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
34 {
35  const TYPE_USR = 1;
36  const TYPE_ROLE = 2;
37 
38  private $calendar_id;
39 
40  private $shared = array();
41  private $shared_users = array();
42  private $shared_roles = array();
43 
44  protected $db;
45 
46 
53  public function __construct($a_calendar_id)
54  {
55  global $ilDB;
56 
57  $this->calendar_id = $a_calendar_id;
58  $this->db = $ilDB;
59  $this->read();
60  }
61 
70  public static function deleteByCalendar($a_cal_id)
71  {
72  global $ilDB;
73 
74  $query = "DELETE FROM cal_shared WHERE cal_id = ".$ilDB->quote($a_cal_id ,'integer')." ";
75  $res = $ilDB->manipulate($query);
76  return true;
77  }
78 
87  public static function deleteByUser($a_user_id)
88  {
89  global $ilDB;
90 
91  $query = "DELETE FROM cal_shared WHERE obj_id = ".$ilDB->quote($a_user_id ,'integer')." ";
92  $res = $ilDB->manipulate($query);
93  return true;
94 
95  // TODO: delete also cal_shared_user_status
96  }
97 
107  public static function isSharedWithUser($a_usr_id,$a_calendar_id)
108  {
109  global $ilDB,$rbacreview;
110 
111  $query = 'SELECT * FROM cal_shared '.
112  "WHERE cal_id = ".$ilDB->quote($a_calendar_id ,'integer')." ";
113  $res = $ilDB->query($query);
114  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
115  {
116  $obj_ids[$row->obj_id] = $row->obj_type;
117  }
118  $assigned_roles = $rbacreview->assignedRoles($a_usr_id);
119  foreach($obj_ids as $id => $type)
120  {
121  switch($type)
122  {
123  case self::TYPE_USR:
124  if($a_usr_id == $id)
125  {
126  return true;
127  }
128  break;
129  case self::TYPE_ROLE:
130  if(in_array($id,$assigned_roles))
131  {
132  return true;
133  }
134  break;
135  }
136  }
137  return false;
138  }
139 
148  public static function getSharedCalendarsForUser($a_usr_id = 0)
149  {
150  global $ilDB,$ilUser,$rbacreview;
151 
152  if(!$a_usr_id)
153  {
154  $a_usr_id = $ilUser->getId();
155  }
156 
157  $query = "SELECT * FROM cal_shared ".
158  "WHERE obj_type = ".$ilDB->quote(self::TYPE_USR ,'integer')." ".
159  "AND obj_id = ".$ilDB->quote($a_usr_id ,'integer')." ".
160  "ORDER BY create_date";
161  $res = $ilDB->query($query);
162  $calendars = array();
163  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
164  {
165  $calendars[] = $row->cal_id;
166 
167  $shared[$row->cal_id]['cal_id'] = $row->cal_id;
168  $shared[$row->cal_id]['create_date'] = $row->create_date;
169  $shared[$row->cal_id]['obj_type'] = $row->obj_type;
170  }
171 
172  $assigned_roles = $rbacreview->assignedRoles($ilUser->getId());
173 
174  $query = "SELECT * FROM cal_shared ".
175  "WHERE obj_type = ".$ilDB->quote(self::TYPE_ROLE ,'integer')." ".
176  "AND ".$ilDB->in('obj_id',$assigned_roles,false ,'integer');
177 
178  $res = $ilDB->query($query);
179  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
180  {
181  if(in_array($row->cal_id,$calendars))
182  {
183  continue;
184  }
185  if(ilCalendarCategories::_isOwner($ilUser->getId(),$row->cal_id))
186  {
187  continue;
188  }
189 
190  $shared[$row->cal_id]['cal_id'] = $row->cal_id;
191  $shared[$row->cal_id]['create_date'] = $row->create_date;
192  $shared[$row->cal_id]['obj_type'] = $row->obj_type;
193  }
194 
195 
196 
197  return $shared ? $shared : array();
198  // TODO: return also role calendars
199 
200  }
201 
208  public function getCalendarId()
209  {
210  return $this->calendar_id;
211  }
212 
219  public function getShared()
220  {
221  return $this->shared ? $this->shared : array();
222  }
223 
230  public function getUsers()
231  {
232  return $this->shared_users ? $this->shared_users : array();
233  }
234 
241  public function getRoles()
242  {
243  return $this->shared_roles ? $this->shared_roles : array();
244  }
245 
253  public function isShared($a_obj_id)
254  {
255  return isset($this->shared[$a_obj_id]);
256  }
257 
262  public function isEditableForUser($a_user_id)
263  {
264  foreach((array) $this->shared as $info)
265  {
266  $GLOBALS['ilLog']->write(__METHOD__.': Calendar info:' . print_r($info,true));
267  $GLOBALS['ilLog']->write(__METHOD__.': Current user:' . $a_user_id);
268  if(!$info['writable'])
269  {
270  continue;
271  }
272 
273  switch($info['obj_type'])
274  {
275  case self::TYPE_USR:
276  if($info['obj_id'] == $a_user_id)
277  {
278  $GLOBALS['ilLog']->write(__METHOD__.': Shared calendar is writable.');
279  return true;
280  }
281  break;
282 
283  case self::TYPE_ROLE:
284  if($GLOBALS['rbacreview']->isAssigned($a_user_id,$info['obj_id']))
285  {
286  $GLOBALS['ilLog']->write(__METHOD__.': Shared calendar is writable.');
287  return true;
288  }
289  break;
290  }
291  }
292  $GLOBALS['ilLog']->write(__METHOD__.': Shared calendar is not writable.');
293  return false;
294  }
295 
304  public function share($a_obj_id,$a_type, $a_writable = false)
305  {
306  global $ilDB;
307 
308  if($this->isShared($a_obj_id))
309  {
310  return false;
311  }
312  $query = "INSERT INTO cal_shared (cal_id,obj_id,obj_type,create_date,writable) ".
313  "VALUES ( ".
314  $this->db->quote($this->getCalendarId() ,'integer').", ".
315  $this->db->quote($a_obj_id ,'integer').", ".
316  $this->db->quote($a_type ,'integer').", ".
317  $ilDB->now().", ".
318  $this->db->quote((int) $a_writable,'integer').' '.
319  ")";
320 
321  $res = $ilDB->manipulate($query);
322 
323  $this->read();
324  return true;
325  }
326 
334  public function stopSharing($a_obj_id)
335  {
336  global $ilDB;
337 
338  if(!$this->isShared($a_obj_id))
339  {
340  return false;
341  }
342  $query = "DELETE FROM cal_shared WHERE cal_id = ".$this->db->quote($this->getCalendarId() ,'integer')." ".
343  "AND obj_id = ".$this->db->quote($a_obj_id ,'integer')." ";
344  $res = $ilDB->manipulate($query);
345 
346  include_once('./Services/Calendar/classes/class.ilCalendarSharedStatus.php');
348 
349 
350  $this->read();
351  return true;
352  }
353 
360  protected function read()
361  {
362  global $ilDB;
363 
364  $this->shared = $this->shared_users = $this->shared_roles = array();
365 
366  $query = "SELECT * FROM cal_shared WHERE cal_id = ".$this->db->quote($this->getCalendarId() ,'integer');
367  $res = $this->db->query($query);
368  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
369  {
370  switch($row->obj_type)
371  {
372  case self::TYPE_USR:
373  $this->shared_users[$row->obj_id]['obj_id'] = $row->obj_id;
374  $this->shared_users[$row->obj_id]['obj_type'] = $row->obj_type;
375  $this->shared_users[$row->obj_id]['create_date'] = $row->create_date;
376  $this->shared_users[$row->obj_id]['writable'] = $row->writable;
377  break;
378 
379 
380  case self::TYPE_ROLE:
381  $this->shared_roles[$row->obj_id]['obj_id'] = $row->obj_id;
382  $this->shared_roles[$row->obj_id]['obj_type'] = $row->obj_type;
383  $this->shared_roles[$row->obj_id]['create_date'] = $row->create_date;
384  $this->shared_role[$row->obj_id]['writable'] = $row->writable;
385  break;
386 
387  }
388 
389  $this->shared[$row->obj_id]['obj_id'] = $row->obj_id;
390  $this->shared[$row->obj_id]['obj_type'] = $row->obj_type;
391  $this->shared[$row->obj_id]['create_date'] = $row->create_date;
392  $this->shared[$row->obj_id]['writable'] = $row->writable;
393  }
394  return true;
395  }
396 }
397 ?>