ILIAS  Release_4_2_x_branch Revision 61807
 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 
266  public function share($a_obj_id,$a_type)
267  {
268  global $ilDB;
269 
270  if($this->isShared($a_obj_id))
271  {
272  return false;
273  }
274  $query = "INSERT INTO cal_shared (cal_id,obj_id,obj_type,create_date) ".
275  "VALUES ( ".
276  $this->db->quote($this->getCalendarId() ,'integer').", ".
277  $this->db->quote($a_obj_id ,'integer').", ".
278  $this->db->quote($a_type ,'integer').", ".
279  $ilDB->now()." ".
280  ")";
281  $res = $ilDB->manipulate($query);
282 
283  $this->read();
284  return true;
285  }
286 
294  public function stopSharing($a_obj_id)
295  {
296  global $ilDB;
297 
298  if(!$this->isShared($a_obj_id))
299  {
300  return false;
301  }
302  $query = "DELETE FROM cal_shared WHERE cal_id = ".$this->db->quote($this->getCalendarId() ,'integer')." ".
303  "AND obj_id = ".$this->db->quote($a_obj_id ,'integer')." ";
304  $res = $ilDB->manipulate($query);
305 
306  include_once('./Services/Calendar/classes/class.ilCalendarSharedStatus.php');
308 
309 
310  $this->read();
311  return true;
312  }
313 
320  protected function read()
321  {
322  global $ilDB;
323 
324  $this->shared = $this->shared_users = $this->shared_roles = array();
325 
326  $query = "SELECT * FROM cal_shared WHERE cal_id = ".$this->db->quote($this->getCalendarId() ,'integer');
327  $res = $this->db->query($query);
328  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
329  {
330  switch($row->obj_type)
331  {
332  case self::TYPE_USR:
333  $this->shared_users[$row->obj_id]['obj_id'] = $row->obj_id;
334  $this->shared_users[$row->obj_id]['obj_type'] = $row->obj_type;
335  $this->shared_users[$row->obj_id]['create_date'] = $row->create_date;
336  break;
337 
338 
339  case self::TYPE_ROLE:
340  $this->shared_roles[$row->obj_id]['obj_id'] = $row->obj_id;
341  $this->shared_roles[$row->obj_id]['obj_type'] = $row->obj_type;
342  $this->shared_roles[$row->obj_id]['create_date'] = $row->create_date;
343  break;
344 
345  }
346 
347  $this->shared[$row->obj_id]['obj_id'] = $row->obj_id;
348  $this->shared[$row->obj_id]['obj_type'] = $row->obj_type;
349  $this->shared[$row->obj_id]['create_date'] = $row->create_date;
350  }
351  return true;
352  }
353 }
354 ?>