ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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(ilDBConstants::FETCHMODE_OBJECT)) {
115  $obj_ids[$row->obj_id] = $row->obj_type;
116  }
117  $assigned_roles = $rbacreview->assignedRoles($a_usr_id);
118  foreach ($obj_ids as $id => $type) {
119  switch ($type) {
120  case self::TYPE_USR:
121  if ($a_usr_id == $id) {
122  return true;
123  }
124  break;
125  case self::TYPE_ROLE:
126  if (in_array($id, $assigned_roles)) {
127  return true;
128  }
129  break;
130  }
131  }
132  return false;
133  }
134 
143  public static function getSharedCalendarsForUser($a_usr_id = 0)
144  {
145  global $ilDB,$ilUser,$rbacreview;
146 
147  if (!$a_usr_id) {
148  $a_usr_id = $ilUser->getId();
149  }
150 
151  $query = "SELECT * FROM cal_shared " .
152  "WHERE obj_type = " . $ilDB->quote(self::TYPE_USR, 'integer') . " " .
153  "AND obj_id = " . $ilDB->quote($a_usr_id, 'integer') . " " .
154  "ORDER BY create_date";
155  $res = $ilDB->query($query);
156  $calendars = array();
157  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
158  $calendars[] = $row->cal_id;
159 
160  $shared[$row->cal_id]['cal_id'] = $row->cal_id;
161  $shared[$row->cal_id]['create_date'] = $row->create_date;
162  $shared[$row->cal_id]['obj_type'] = $row->obj_type;
163  }
164 
165  $assigned_roles = $rbacreview->assignedRoles($ilUser->getId());
166 
167  $query = "SELECT * FROM cal_shared " .
168  "WHERE obj_type = " . $ilDB->quote(self::TYPE_ROLE, 'integer') . " " .
169  "AND " . $ilDB->in('obj_id', $assigned_roles, false, 'integer');
170 
171  $res = $ilDB->query($query);
172  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
173  if (in_array($row->cal_id, $calendars)) {
174  continue;
175  }
176  if (ilCalendarCategories::_isOwner($ilUser->getId(), $row->cal_id)) {
177  continue;
178  }
179 
180  $shared[$row->cal_id]['cal_id'] = $row->cal_id;
181  $shared[$row->cal_id]['create_date'] = $row->create_date;
182  $shared[$row->cal_id]['obj_type'] = $row->obj_type;
183  }
184 
185 
186 
187  return $shared ? $shared : array();
188  // TODO: return also role calendars
189  }
190 
197  public function getCalendarId()
198  {
199  return $this->calendar_id;
200  }
201 
208  public function getShared()
209  {
210  return $this->shared ? $this->shared : array();
211  }
212 
219  public function getUsers()
220  {
221  return $this->shared_users ? $this->shared_users : array();
222  }
223 
230  public function getRoles()
231  {
232  return $this->shared_roles ? $this->shared_roles : array();
233  }
234 
242  public function isShared($a_obj_id)
243  {
244  return isset($this->shared[$a_obj_id]);
245  }
246 
251  public function isEditableForUser($a_user_id)
252  {
253  foreach ((array) $this->shared as $info) {
254  $GLOBALS['ilLog']->write(__METHOD__ . ': Calendar info:' . print_r($info, true));
255  $GLOBALS['ilLog']->write(__METHOD__ . ': Current user:' . $a_user_id);
256  if (!$info['writable']) {
257  continue;
258  }
259 
260  switch ($info['obj_type']) {
261  case self::TYPE_USR:
262  if ($info['obj_id'] == $a_user_id) {
263  $GLOBALS['ilLog']->write(__METHOD__ . ': Shared calendar is writable.');
264  return true;
265  }
266  break;
267 
268  case self::TYPE_ROLE:
269  if ($GLOBALS['rbacreview']->isAssigned($a_user_id, $info['obj_id'])) {
270  $GLOBALS['ilLog']->write(__METHOD__ . ': Shared calendar is writable.');
271  return true;
272  }
273  break;
274  }
275  }
276  $GLOBALS['ilLog']->write(__METHOD__ . ': Shared calendar is not writable.');
277  return false;
278  }
279 
288  public function share($a_obj_id, $a_type, $a_writable = false)
289  {
290  global $ilDB;
291 
292  if ($this->isShared($a_obj_id)) {
293  return false;
294  }
295  $query = "INSERT INTO cal_shared (cal_id,obj_id,obj_type,create_date,writable) " .
296  "VALUES ( " .
297  $this->db->quote($this->getCalendarId(), 'integer') . ", " .
298  $this->db->quote($a_obj_id, 'integer') . ", " .
299  $this->db->quote($a_type, 'integer') . ", " .
300  $ilDB->now() . ", " .
301  $this->db->quote((int) $a_writable, 'integer') . ' ' .
302  ")";
303 
304  $res = $ilDB->manipulate($query);
305 
306  $this->read();
307  return true;
308  }
309 
317  public function stopSharing($a_obj_id)
318  {
319  global $ilDB;
320 
321  if (!$this->isShared($a_obj_id)) {
322  return false;
323  }
324  $query = "DELETE FROM cal_shared WHERE cal_id = " . $this->db->quote($this->getCalendarId(), 'integer') . " " .
325  "AND obj_id = " . $this->db->quote($a_obj_id, 'integer') . " ";
326  $res = $ilDB->manipulate($query);
327 
328  include_once('./Services/Calendar/classes/class.ilCalendarSharedStatus.php');
330 
331 
332  $this->read();
333  return true;
334  }
335 
342  protected function read()
343  {
344  global $ilDB;
345 
346  $this->shared = $this->shared_users = $this->shared_roles = array();
347 
348  $query = "SELECT * FROM cal_shared WHERE cal_id = " . $this->db->quote($this->getCalendarId(), 'integer');
349  $res = $this->db->query($query);
350  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
351  switch ($row->obj_type) {
352  case self::TYPE_USR:
353  $this->shared_users[$row->obj_id]['obj_id'] = $row->obj_id;
354  $this->shared_users[$row->obj_id]['obj_type'] = $row->obj_type;
355  $this->shared_users[$row->obj_id]['create_date'] = $row->create_date;
356  $this->shared_users[$row->obj_id]['writable'] = $row->writable;
357  break;
358 
359 
360  case self::TYPE_ROLE:
361  $this->shared_roles[$row->obj_id]['obj_id'] = $row->obj_id;
362  $this->shared_roles[$row->obj_id]['obj_type'] = $row->obj_type;
363  $this->shared_roles[$row->obj_id]['create_date'] = $row->create_date;
364  $this->shared_role[$row->obj_id]['writable'] = $row->writable;
365  break;
366 
367  }
368 
369  $this->shared[$row->obj_id]['obj_id'] = $row->obj_id;
370  $this->shared[$row->obj_id]['obj_type'] = $row->obj_type;
371  $this->shared[$row->obj_id]['create_date'] = $row->create_date;
372  $this->shared[$row->obj_id]['writable'] = $row->writable;
373  }
374  return true;
375  }
376 }
isEditableForUser($a_user_id)
Check if calendar is editable for user.
static _isOwner($a_usr_id, $a_cal_id)
check if user is owner of a category
static deleteByCalendar($a_cal_id)
Delete all entries for a specific calendar id.
$type
static getSharedCalendarsForUser($a_usr_id=0)
get shared calendars of user
share($a_obj_id, $a_type, $a_writable=false)
share calendar
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
if(!array_key_exists('StateId', $_REQUEST)) $id
__construct($a_calendar_id)
constructor
Handles shared calendars.
$a_type
Definition: workflow.php:92
getCalendarId()
get calendar id
foreach($_POST as $key=> $value) $res
$ilUser
Definition: imgupload.php:18
$query
Create styles array
The data for the language used.
stopSharing($a_obj_id)
stop sharing
static deleteByUser($a_user_id)
Delete all entries for a specific user.
static deleteStatus($a_id, $a_calendar_id)
delete status
global $ilDB
isShared($a_obj_id)
Check if calendar is already shared with specific user or role.
$info
Definition: index.php:5
static isSharedWithUser($a_usr_id, $a_calendar_id)
is shared with user
read()
read shared calendars