ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilSoapGroupAdministration.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 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 
31 {
32  public const MEMBER = 1;
33  public const ADMIN = 2;
34  public const OWNER = 4;
35 
39  public function addGroup(string $sid, int $target_id, string $grp_xml)
40  {
41  $this->initAuth($sid);
42  $this->initIlias();
43 
44  if (!$this->checkSession($sid)) {
45  return $this->raiseError($this->getMessage(), $this->getMessageCode());
46  }
47 
48  global $DIC;
49 
50  $rbacsystem = $DIC['rbacsystem'];
51 
52  if (!$rbacsystem->checkAccess('create', $target_id, 'grp')) {
53  return $this->raiseError('Check access failed. No permission to create groups', 'Server');
54  }
55 
56  if (ilObject::_isInTrash($target_id)) {
57  return $this->raiseError("Parent with ID $target_id has been deleted.", 'CLIENT_TARGET_DELETED');
58  }
59 
60  $newObj = new ilObjGroup();
61  $newObj->setTitle('dummy');
62  $newObj->setDescription("");
63  $newObj->create();
64 
65  $xml_parser = new ilGroupXMLParser($newObj, $grp_xml, $target_id);
66  $xml_parser->startParsing();
67  $new_ref_id = $xml_parser->getObjectRefId();
68 
69  return $new_ref_id ?: "0";
70  }
71 
75  public function updateGroup(string $sid, int $ref_id, string $grp_xml)
76  {
77  $this->initAuth($sid);
78  $this->initIlias();
79 
80  if (!$this->checkSession($sid)) {
81  return $this->raiseError($this->getMessage(), $this->getMessageCode());
82  }
83 
84  global $DIC;
85 
86  $rbacsystem = $DIC['rbacsystem'];
87 
88  if (!$rbacsystem->checkAccess('write', $ref_id, 'grp')) {
89  return $this->raiseError('Check access failed. No permission to edit groups', 'Server');
90  }
91 
93  if (!$grp = ilObjectFactory::getInstanceByRefId($ref_id, false)) {
94  return $this->raiseError('Cannot create group instance!', 'CLIENT_OBJECT_NOT_FOUND');
95  }
96 
97  if (ilObject::_isInTrash($ref_id)) {
98  return $this->raiseError("Object with ID $ref_id has been deleted.", 'CLIENT_OBJECT_DELETED');
99  }
100 
101  if (ilObjectFactory::getTypeByRefId($ref_id, false) !== "grp") {
102  return $this->raiseError('Reference id does not point to a group!', 'CLIENT_WRONG_TYPE');
103  }
104 
105  $xml_parser = new ilGroupXMLParser($grp, $grp_xml, -1);
106  $xml_parser->setMode(ilGroupXMLParser::$UPDATE);
107  $xml_parser->startParsing();
108  $new_ref_id = $xml_parser->getObjectRefId();
109 
110  return $new_ref_id ?: "0";
111  }
112 
116  public function groupExists(string $sid, string $title)
117  {
118  $this->initAuth($sid);
119  $this->initIlias();
120 
121  if (!$this->checkSession($sid)) {
122  return $this->raiseError($this->getMessage(), $this->getMessageCode());
123  }
124 
125  if (!$title) {
126  return $this->raiseError(
127  'No title given. Please choose an title for the group in question.',
128  'Client'
129  );
130  }
131 
132  return ilUtil::groupNameExists($title);
133  }
134 
138  public function getGroup(string $sid, int $ref_id)
139  {
140  $this->initAuth($sid);
141  $this->initIlias();
142 
143  if (!$this->checkSession($sid)) {
144  return $this->raiseError($this->getMessage(), $this->getMessageCode());
145  }
146 
147  if (ilObject::_isInTrash($ref_id)) {
148  return $this->raiseError("Parent with ID $ref_id has been deleted.", 'CLIENT_OBJECT_DELETED');
149  }
150 
152  if (!$grp_obj = ilObjectFactory::getInstanceByRefId($ref_id, false)) {
153  return $this->raiseError(
154  'No valid reference id given.',
155  'Client'
156  );
157  }
158 
159  $xml_writer = new ilGroupXMLWriter($grp_obj);
160  $xml_writer->start();
161 
162  return $xml_writer->getXML();
163  }
164 
168  public function assignGroupMember(string $sid, int $group_id, int $user_id, string $type)
169  {
170  $this->initAuth($sid);
171  $this->initIlias();
172 
173  if (!$this->checkSession($sid)) {
174  return $this->raiseError($this->getMessage(), $this->getMessageCode());
175  }
176 
177  global $DIC;
178 
179  $rbacsystem = $DIC['rbacsystem'];
180 
181  if (ilObject::_lookupType(ilObject::_lookupObjId($group_id)) !== 'grp') {
182  $ref_ids = ilObject::_getAllReferences($group_id);
183  $group_id = end($ref_ids);
184  if (ilObject::_lookupType(ilObject::_lookupObjId($group_id)) !== 'grp') {
185  return $this->raiseError(
186  'Invalid group id. Object with id "' . $group_id . '" is not of type "group"',
187  'Client'
188  );
189  }
190  }
191 
192  if (!$rbacsystem->checkAccess('manage_members', $group_id)) {
193  return $this->raiseError('Check access failed. No permission to write to group', 'Server');
194  }
195 
196  if (ilObject::_lookupType($user_id) !== 'usr') {
197  return $this->raiseError('Invalid user id. User with id "' . $user_id . ' does not exist', 'Client');
198  }
199  if ($type !== 'Admin' &&
200  $type !== 'Member') {
201  return $this->raiseError(
202  'Invalid type ' . $type . ' given. Parameter "type" must be "Admin","Member"',
203  'Client'
204  );
205  }
206 
208  if (!$tmp_group = ilObjectFactory::getInstanceByRefId($group_id, false)) {
209  return $this->raiseError('Cannot create group instance!', 'Server');
210  }
211 
213  if (!$tmp_user = ilObjectFactory::getInstanceByObjId($user_id, false)) {
214  return $this->raiseError('Cannot create user instance!', 'Server');
215  }
216 
217  $group_members = ilGroupParticipants::_getInstanceByObjId($tmp_group->getId());
218 
219  switch ($type) {
220  case 'Admin':
221  $group_members->add($tmp_user->getId(), ilParticipants::IL_GRP_ADMIN);
222  break;
223 
224  case 'Member':
225  $group_members->add($tmp_user->getId(), ilParticipants::IL_GRP_MEMBER);
226  break;
227  }
228  return true;
229  }
230 
234  public function excludeGroupMember(string $sid, int $group_id, int $user_id)
235  {
236  $this->initAuth($sid);
237  $this->initIlias();
238 
239  if (!$this->checkSession($sid)) {
240  return $this->raiseError($this->getMessage(), $this->getMessageCode());
241  }
242 
243  global $DIC;
244 
245  $rbacsystem = $DIC['rbacsystem'];
246 
247  if (ilObject::_lookupType(ilObject::_lookupObjId($group_id)) !== 'grp') {
248  $ref_ids = ilObject::_getAllReferences($group_id);
249  $group_id = end($ref_ids);
250  if (ilObject::_lookupType(ilObject::_lookupObjId($group_id)) !== 'grp') {
251  return $this->raiseError(
252  'Invalid group id. Object with id "' . $group_id . '" is not of type "group"',
253  'Client'
254  );
255  }
256  }
257 
258  if (ilObject::_lookupType($user_id) !== 'usr') {
259  return $this->raiseError('Invalid user id. User with id "' . $user_id . ' does not exist', 'Client');
260  }
261 
263  if (!$tmp_group = ilObjectFactory::getInstanceByRefId($group_id, false)) {
264  return $this->raiseError('Cannot create group instance!', 'Server');
265  }
266 
267  if (!$rbacsystem->checkAccess('manage_members', $group_id)) {
268  return $this->raiseError('Check access failed. No permission to write to group', 'Server');
269  }
270 
271  $tmp_group->leave($user_id);
272  return true;
273  }
274 
278  public function isAssignedToGroup(string $sid, int $group_id, int $user_id)
279  {
280  $this->initAuth($sid);
281  $this->initIlias();
282 
283  if (!$this->checkSession($sid)) {
284  return $this->raiseError($this->getMessage(), $this->getMessageCode());
285  }
286 
287  global $DIC;
288 
289  $rbacsystem = $DIC['rbacsystem'];
290 
291  if (ilObject::_lookupType(ilObject::_lookupObjId($group_id)) !== 'grp') {
292  $ref_ids = ilObject::_getAllReferences($group_id);
293  $group_id = end($ref_ids);
294  if (ilObject::_lookupType(ilObject::_lookupObjId($group_id)) !== 'grp') {
295  return $this->raiseError(
296  'Invalid group id. Object with id "' . $group_id . '" is not of type "group"',
297  'Client'
298  );
299  }
300  }
301 
302  if (ilObject::_lookupType($user_id) !== 'usr') {
303  return $this->raiseError('Invalid user id. User with id "' . $user_id . ' does not exist', 'Client');
304  }
305 
307  if (!$tmp_group = ilObjectFactory::getInstanceByRefId($group_id, false)) {
308  return $this->raiseError('Cannot create group instance!', 'Server');
309  }
310 
311  if (!$rbacsystem->checkAccess('read', $group_id)) {
312  return $this->raiseError('Check access failed. No permission to read group data', 'Server');
313  }
314 
316 
317  if ($participants->isAdmin($user_id)) {
318  return 1;
319  }
320  if ($participants->isMember($user_id)) {
321  return 2;
322  }
323  return 0;
324  }
325 
329  public function getGroupsForUser(string $sid, string $parameters)
330  {
331  $this->initAuth($sid);
332  $this->initIlias();
333 
334  if (!$this->checkSession($sid)) {
335  return $this->raiseError($this->getMessage(), $this->getMessageCode());
336  }
337  global $DIC;
338 
339  $rbacreview = $DIC['rbacreview'];
340  $ilObjDataCache = $DIC['ilObjDataCache'];
341  $tree = $DIC['tree'];
342 
343  $parser = new ilXMLResultSetParser($parameters);
344  try {
345  $parser->startParsing();
346  } catch (ilSaxParserException $exception) {
347  return $this->raiseError($exception->getMessage(), "Client");
348  }
349  $xmlResultSet = $parser->getXMLResultSet();
350 
351  if (!$xmlResultSet->hasColumn("user_id")) {
352  return $this->raiseError("parameter user_id is missing", "Client");
353  }
354 
355  if (!$xmlResultSet->hasColumn("status")) {
356  return $this->raiseError("parameter status is missing", "Client");
357  }
358 
359  $user_id = (int) $xmlResultSet->getValue(0, "user_id");
360  $status = (int) $xmlResultSet->getValue(0, "status");
361 
362  $ref_ids = array();
363 
366  foreach ($rbacreview->assignedRoles($user_id) as $role_id) {
367  if ($role = ilObjectFactory::getInstanceByObjId($role_id, false)) {
368  #echo $role->getType();
369  if ($role->getType() !== "role") {
370  continue;
371  }
372 
373  if ($role->getParent() == ROLE_FOLDER_ID) {
374  continue;
375  }
376  $role_title = $role->getTitle();
377 
378  if ($ref_id = ilUtil::__extractRefId($role_title)) {
379  if (!ilObject::_exists($ref_id, true) || ilObject::_isInTrash($ref_id)) {
380  continue;
381  }
382 
383  #echo $role_title;
384  if (ilSoapGroupAdministration::MEMBER == ($status & ilSoapGroupAdministration::MEMBER) && strpos(
385  $role_title,
386  "member"
387  ) !== false) {
388  $ref_ids [] = $ref_id;
389  } elseif (ilSoapGroupAdministration::ADMIN == ($status & ilSoapGroupAdministration::ADMIN) && strpos(
390  $role_title,
391  "admin"
392  ) !== false) {
393  $ref_ids [] = $ref_id;
394  }
395  }
396  }
397  }
398  }
399 
400  if (($status & ilSoapGroupAdministration::OWNER) == ilSoapGroupAdministration::OWNER) {
401  $owned_objects = ilObjectFactory::getObjectsForOwner("grp", $user_id);
402  foreach ($owned_objects as $obj_id) {
403  $allrefs = ilObject::_getAllReferences($obj_id);
404  $refs = array();
405  foreach ($allrefs as $r) {
406  if ($tree->isDeleted($r)) {
407  continue;
408  }
409  if ($tree->isInTree($r)) {
410  $refs[] = $r;
411  }
412  }
413  if (count($refs) > 0) {
414  $ref_ids[] = array_pop($refs);
415  }
416  }
417  }
418  $ref_ids = array_unique($ref_ids);
419 
420  $xmlResultSet = new ilXMLResultSet();
421  $xmlResultSet->addColumn("ref_id");
422  $xmlResultSet->addColumn("xml");
423  $xmlResultSet->addColumn("parent_ref_id");
424 
425  foreach ($ref_ids as $group_id) {
426  $group_obj = $this->checkObjectAccess($group_id, ['grp'], "write", true);
427  if ($group_obj instanceof ilObjGroup) {
428  $row = new ilXMLResultSetRow();
429  $row->setValue("ref_id", $group_id);
430  $xmlWriter = new ilGroupXMLWriter($group_obj);
431  $xmlWriter->setAttachUsers(false);
432  $xmlWriter->start();
433  $row->setValue("xml", $xmlWriter->getXML());
434  $row->setValue("parent_ref_id", $tree->getParentId($group_id));
435  $xmlResultSet->addRow($row);
436  }
437  }
438  $xmlResultSetWriter = new ilXMLResultSetWriter($xmlResultSet);
439  $xmlResultSetWriter->start();
440  return $xmlResultSetWriter->getXML();
441  }
442 }
XML Writer for XMLResultSet.
addGroup(string $sid, int $target_id, string $grp_xml)
static _getAllReferences(int $id)
get all reference ids for object ID
raiseError(string $a_message, $a_code)
Row Class for XMLResultSet.
SaxParserException thrown by ilSaxParser if property throwException is set.
static getTypeByRefId(int $ref_id, bool $stop_on_error=true)
get object type by reference id
static _lookupObjId(int $ref_id)
getGroupsForUser(string $sid, string $parameters)
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
$ref_id
Definition: ltiauth.php:66
static getObjectsForOwner(string $object_type, int $owner_id)
returns all objects of an owner, filtered by type, objects are not deleted!
groupExists(string $sid, string $title)
static _isInTrash(int $ref_id)
static getInstanceByRefId(int $ref_id, bool $stop_on_error=true)
get an instance of an Ilias object by reference id
global $DIC
Definition: shib_login.php:25
checkObjectAccess(int $ref_id, array $expected_type, string $permission, bool $returnObject=false)
check access for ref id: expected type, permission, return object instance if returnobject is true ...
const ROLE_FOLDER_ID
Definition: constants.php:34
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
static _getInstanceByObjId(int $a_obj_id)
Get singleton instance.
Group Import Parser.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static groupNameExists(string $a_group_name, ?int $a_id=null)
checks if group name already exists.
Class ilObjGroup.
static _lookupType(int $id, bool $reference=false)
static __extractRefId(string $role_title)
extract ref id from role title, e.g.
$r