ILIAS  Release_3_10_x_branch Revision 61812
 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)." ";
75  $ilDB->query($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_usr_id)." ";
92  $ilDB->query($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)." ";
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)." ".
159  "AND obj_id = ".$ilDB->quote($a_usr_id)." ".
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)." ".
176  "AND obj_id IN (".implode(",",ilUtil::quoteArray($assigned_roles)).") ";
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  if($this->isShared($a_obj_id))
269  {
270  return false;
271  }
272  $query = "INSERT INTO cal_shared ".
273  "SET cal_id = ".$this->db->quote($this->getCalendarId()).", ".
274  "obj_id = ".$this->db->quote($a_obj_id).", ".
275  "obj_type = ".$this->db->quote($a_type).", ".
276  "create_date = NOW()";
277  $this->db->query($query);
278 
279  $this->read();
280  return true;
281  }
282 
290  public function stopSharing($a_obj_id)
291  {
292  if(!$this->isShared($a_obj_id))
293  {
294  return false;
295  }
296  $query = "DELETE FROM cal_shared WHERE cal_id = ".$this->db->quote($this->getCalendarId())." ".
297  "AND obj_id = ".$this->db->quote($a_obj_id)." ";
298  $this->db->query($query);
299 
300  include_once('./Services/Calendar/classes/class.ilCalendarSharedStatus.php');
302 
303 
304  $this->read();
305  return true;
306  }
307 
314  protected function read()
315  {
316  $this->shared = $this->shared_users = $this->shared_roles = array();
317 
318  $query = "SELECT * FROM cal_shared WHERE cal_id = ".$this->db->quote($this->getCalendarId());
319  $res = $this->db->query($query);
320  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
321  {
322  switch($row->obj_type)
323  {
324  case self::TYPE_USR:
325  $this->shared_users[$row->obj_id]['obj_id'] = $row->obj_id;
326  $this->shared_users[$row->obj_id]['obj_type'] = $row->obj_type;
327  $this->shared_users[$row->obj_id]['create_date'] = $row->create_date;
328  break;
329 
330 
331  case self::TYPE_ROLE:
332  $this->shared_roles[$row->obj_id]['obj_id'] = $row->obj_id;
333  $this->shared_roles[$row->obj_id]['obj_type'] = $row->obj_type;
334  $this->shared_roles[$row->obj_id]['create_date'] = $row->create_date;
335  break;
336 
337  }
338 
339  $this->shared[$row->obj_id]['obj_id'] = $row->obj_id;
340  $this->shared[$row->obj_id]['obj_type'] = $row->obj_type;
341  $this->shared[$row->obj_id]['create_date'] = $row->create_date;
342  }
343  return true;
344  }
345 }
346 ?>