ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilSoapCourseAdministration.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 
32 {
33  public const MEMBER = 1;
34  public const TUTOR = 2;
35  public const ADMIN = 4;
36  public const OWNER = 8;
37 
41  public function addCourse(string $sid, int $target_id, string $crs_xml)
42  {
43  $this->initAuth($sid);
44  $this->initIlias();
45 
46  if (!$this->checkSession($sid)) {
47  return $this->raiseError($this->getMessage(), $this->getMessageCode());
48  }
49 
50  global $DIC;
51 
52  $rbacsystem = $DIC['rbacsystem'];
53 
54  if (!$target_obj = ilObjectFactory::getInstanceByRefId($target_id, false)) {
55  return $this->raiseError('No valid target given.', 'Client');
56  }
57 
58  if (ilObject::_isInTrash($target_id)) {
59  return $this->raiseError("Parent with ID $target_id has been deleted.", 'CLIENT_OBJECT_DELETED');
60  }
61 
62  if (!$rbacsystem->checkAccess('create', $target_id, 'crs')) {
63  return $this->raiseError('Check access failed. No permission to create courses', 'Server');
64  }
65 
66  $newObj = new ilObjCourse();
67  $newObj->setType('crs');
68  $newObj->setTitle('dummy');
69  $newObj->setDescription("");
70  $newObj->create(true); // true for upload
71  $newObj->createReference();
72  $newObj->putInTree($target_id);
73  $newObj->setPermissions($target_id);
74 
75  $xml_parser = new ilCourseXMLParser($newObj);
76  $xml_parser->setMode(ilCourseXMLParser::MODE_SOAP);
77  $xml_parser->setXMLContent($crs_xml);
78  $xml_parser->startParsing();
79  return $newObj->getRefId() ?: "0";
80  }
81 
85  public function deleteCourse(string $sid, int $course_id)
86  {
87  $this->initAuth($sid);
88  $this->initIlias();
89 
90  if (!$this->checkSession($sid)) {
91  return $this->raiseError($this->getMessage(), $this->getMessageCode());
92  }
93 
94  global $DIC;
95 
96  $rbacsystem = $DIC['rbacsystem'];
97 
98  if (ilObject::_lookupType(ilObject::_lookupObjId($course_id)) !== 'crs') {
99  $ref_ids = ilObject::_getAllReferences($course_id);
100  $course_id = end($ref_ids);
101  if (ilObject::_lookupType(ilObject::_lookupObjId($course_id)) !== 'crs') {
102  return $this->raiseError(
103  'Invalid course id. Object with id "' . $course_id . '" is not of type "course"',
104  'Client'
105  );
106  }
107  }
108 
109  if (!$rbacsystem->checkAccess('delete', $course_id)) {
110  return $this->raiseError('Check access failed. No permission to delete course', 'Server');
111  }
112 
113  global $DIC;
114  $tree = $DIC->repositoryTree();
115  $user = $DIC->user();
116  $rbacadmin = $DIC['rbacadmin'];
117  $log = $DIC['log'];
118 
119  if ($tree->isDeleted($course_id)) {
120  return $this->raiseError('Node already deleted', 'Server');
121  }
122 
123  $subnodes = $tree->getSubTree($tree->getNodeData($course_id));
124  foreach ($subnodes as $subnode) {
125  $rbacadmin->revokePermission($subnode["child"]);
126  }
127  if (!$tree->moveToTrash($course_id, true, $user->getId())) {
128  return $this->raiseError('Node already deleted', 'Client');
129  }
130 
131  $log->write("SOAP ilObjectGUI::confirmedDeleteObject(), moved ref_id " . $course_id . " to trash");
132  return true;
133  }
134 
138  public function assignCourseMember(string $sid, int $course_id, int $user_id, string $type)
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  global $DIC;
148 
149  $rbacsystem = $DIC['rbacsystem'];
150 
151  if (ilObject::_lookupType(ilObject::_lookupObjId($course_id)) !== 'crs') {
152  $ref_ids = ilObject::_getAllReferences($course_id);
153  $course_id = end($ref_ids);
154  if (ilObject::_lookupType(ilObject::_lookupObjId($course_id)) !== 'crs') {
155  return $this->raiseError(
156  'Invalid course id. Object with id "' . $course_id . '" is not of type "course"',
157  'Client'
158  );
159  }
160  }
161 
162  if (!$rbacsystem->checkAccess('manage_members', $course_id)) {
163  return $this->raiseError('Check access failed. No permission to write to course', 'Server');
164  }
165 
166  if (ilObject::_lookupType($user_id) !== 'usr') {
167  return $this->raiseError('Invalid user id. User with id "' . $user_id . ' does not exist', 'Client');
168  }
169  if ($type !== 'Admin' &&
170  $type !== 'Tutor' &&
171  $type !== 'Member') {
172  return $this->raiseError(
173  'Invalid type given. Parameter "type" must be "Admin", "Tutor" or "Member"',
174  'Client'
175  );
176  }
177 
178  if (!$tmp_course = ilObjectFactory::getInstanceByRefId($course_id, false)) {
179  return $this->raiseError('Cannot create course instance!', 'Server');
180  }
181 
182  if (!$tmp_user = ilObjectFactory::getInstanceByObjId($user_id, false)) {
183  return $this->raiseError('Cannot create user instance!', 'Server');
184  }
185 
186  $course_members = ilCourseParticipants::_getInstanceByObjId($tmp_course->getId());
187 
188  switch ($type) {
189  case 'Admin':
190  $settings = new ilSetting();
191  $course_members->add($tmp_user->getId(), ilParticipants::IL_CRS_ADMIN);
192  $course_members->updateNotification(
193  $tmp_user->getId(),
194  (bool) $settings->get('mail_crs_admin_notification', "1")
195  );
196  break;
197 
198  case 'Tutor':
199  $course_members->add($tmp_user->getId(), ilParticipants::IL_CRS_TUTOR);
200  break;
201 
202  case 'Member':
203  $course_members->add($tmp_user->getId(), ilParticipants::IL_CRS_MEMBER);
204  break;
205  }
206  return true;
207  }
208 
212  public function excludeCourseMember(string $sid, int $course_id, int $user_id)
213  {
214  $this->initAuth($sid);
215  $this->initIlias();
216 
217  if (!$this->checkSession($sid)) {
218  return $this->raiseError($this->getMessage(), $this->getMessageCode());
219  }
220 
221  global $DIC;
222 
223  $rbacsystem = $DIC['rbacsystem'];
224 
225  if (ilObject::_lookupType(ilObject::_lookupObjId($course_id)) !== 'crs') {
226  $ref_ids = ilObject::_getAllReferences($course_id);
227  $course_id = end($ref_ids);
228  if (ilObject::_lookupType(ilObject::_lookupObjId($course_id)) !== 'crs') {
229  return $this->raiseError(
230  'Invalid course id. Object with id "' . $course_id . '" is not of type "course"',
231  'Client'
232  );
233  }
234  }
235 
236  if (ilObject::_lookupType($user_id) !== 'usr') {
237  return $this->raiseError('Invalid user id. User with id "' . $user_id . ' does not exist', 'Client');
238  }
239 
240  if (!$tmp_course = ilObjectFactory::getInstanceByRefId($course_id, false)) {
241  return $this->raiseError('Cannot create course instance!', 'Server');
242  }
243 
244  if (!$rbacsystem->checkAccess('manage_members', $course_id)) {
245  return $this->raiseError('Check access failed. No permission to write to course', 'Server');
246  }
247 
248  $course_members = ilCourseParticipants::_getInstanceByObjId($tmp_course->getId());
249  if (!$course_members->checkLastAdmin(array($user_id))) {
250  return $this->raiseError('Cannot deassign last administrator from course', 'Server');
251  }
252  $course_members->delete($user_id);
253  return true;
254  }
255 
259  public function isAssignedToCourse(string $sid, int $course_id, int $user_id)
260  {
261  $this->initAuth($sid);
262  $this->initIlias();
263 
264  if (!$this->checkSession($sid)) {
265  return $this->raiseError($this->getMessage(), $this->getMessageCode());
266  }
267 
268  global $DIC;
269 
270  $rbacsystem = $DIC['rbacsystem'];
271 
272  if (ilObject::_lookupType(ilObject::_lookupObjId($course_id)) !== 'crs') {
273  $ref_ids = ilObject::_getAllReferences($course_id);
274  $course_id = end($ref_ids);
275  if (ilObject::_lookupType(ilObject::_lookupObjId($course_id)) !== 'crs') {
276  return $this->raiseError(
277  'Invalid course id. Object with id "' . $course_id . '" is not of type "course"',
278  'Client'
279  );
280  }
281  }
282 
283  if (ilObject::_lookupType($user_id) !== 'usr') {
284  return $this->raiseError('Invalid user id. User with id "' . $user_id . ' does not exist', 'Client');
285  }
286 
288  if (!$tmp_course = ilObjectFactory::getInstanceByRefId($course_id, false)) {
289  return $this->raiseError('Cannot create course instance!', 'Server');
290  }
291 
292  if (!$rbacsystem->checkAccess('manage_members', $course_id)) {
293  return $this->raiseError('Check access failed. No permission to write to course', 'Server');
294  }
295 
296  $crs_members = ilCourseParticipants::_getInstanceByObjId($tmp_course->getId());
297 
298  if ($crs_members->isAdmin($user_id)) {
300  }
301  if ($crs_members->isTutor($user_id)) {
303  }
304  if ($crs_members->isMember($user_id)) {
306  }
307  return "0";
308  }
309 
313  public function getCourseXML(string $sid, int $course_id)
314  {
315  $this->initAuth($sid);
316  $this->initIlias();
317 
318  if (!$this->checkSession($sid)) {
319  return $this->raiseError($this->getMessage(), $this->getMessageCode());
320  }
321 
322  global $DIC;
323 
324  $rbacsystem = $DIC['rbacsystem'];
325 
327  $tmp_course = $this->checkObjectAccess($course_id, ['crs'], "read", true);
328  if ($this->isFault($tmp_course)) {
329  return $tmp_course;
330  }
331 
332  $xml_writer = new ilCourseXMLWriter($tmp_course);
333  $xml_writer->start();
334  return $xml_writer->getXML();
335  }
336 
340  public function updateCourse(string $sid, int $course_id, string $xml)
341  {
342  $this->initAuth($sid);
343  $this->initIlias();
344 
345  if (!$this->checkSession($sid)) {
346  return $this->raiseError($this->getMessage(), $this->getMessageCode());
347  }
348 
349  global $DIC;
350 
351  $rbacsystem = $DIC['rbacsystem'];
352 
353  if (ilObject::_lookupType(ilObject::_lookupObjId($course_id)) !== 'crs') {
354  $ref_ids = ilObject::_getAllReferences($course_id);
355  $course_id = end($ref_ids);
356  if (ilObject::_lookupType(ilObject::_lookupObjId($course_id)) !== 'crs') {
357  return $this->raiseError(
358  'Invalid course id. Object with id "' . $course_id . '" is not of type "course"',
359  'Client'
360  );
361  }
362  }
363 
365  if (!$tmp_course = ilObjectFactory::getInstanceByRefId($course_id, false)) {
366  return $this->raiseError('Cannot create course instance!', 'Server');
367  }
368 
369  if (!$rbacsystem->checkAccess('write', $course_id)) {
370  return $this->raiseError('Check access failed. No permission to write course', 'Server');
371  }
372 
373  // First delete old meta data
374 
375  $md = new ilMD($tmp_course->getId(), 0, 'crs');
376  $md->deleteAll();
377 
378  ilCourseParticipants::_deleteAllEntries($tmp_course->getId());
379 
380  ilCourseWaitingList::_deleteAll($tmp_course->getId());
381 
382 
383  $xml_parser = new ilCourseXMLParser($tmp_course);
384  $xml_parser->setMode(ilCourseXMLParser::MODE_SOAP);
385  $xml_parser->setXMLContent($xml);
386  $xml_parser->startParsing();
387  $tmp_course->MDUpdateListener('General');
388 
389  return true;
390  }
391 
395  public function getCoursesForUser(string $sid, string $parameters)
396  {
397  $this->initAuth($sid);
398  $this->initIlias();
399 
400  if (!$this->checkSession($sid)) {
401  return $this->raiseError($this->getMessage(), $this->getMessageCode());
402  }
403 
404  global $DIC;
405 
406  $rbacreview = $DIC['rbacreview'];
407  $ilObjDataCache = $DIC['ilObjDataCache'];
408  $tree = $DIC['tree'];
409 
410  $parser = new ilXMLResultSetParser($parameters);
411  try {
412  $parser->startParsing();
413  } catch (ilSaxParserException $exception) {
414  return $this->raiseError($exception->getMessage(), "Client");
415  }
416  $xmlResultSet = $parser->getXMLResultSet();
417 
418  if (!$xmlResultSet->hasColumn("user_id")) {
419  return $this->raiseError("parameter user_id is missing", "Client");
420  }
421 
422  if (!$xmlResultSet->hasColumn("status")) {
423  return $this->raiseError("parameter status is missing", "Client");
424  }
425 
426  $user_id = (int) $xmlResultSet->getValue(0, "user_id");
427  $status = (int) $xmlResultSet->getValue(0, "status");
428 
429  $ref_ids = array();
430 
431  if (self::MEMBER == ($status & self::MEMBER) ||
432  self::TUTOR == ($status & self::TUTOR) ||
433  self::ADMIN == ($status & self::ADMIN)) {
434  foreach ($rbacreview->assignedRoles($user_id) as $role_id) {
435  if ($role = ilObjectFactory::getInstanceByObjId($role_id, false)) {
436  #echo $role->getType();
437  if ($role->getType() !== "role") {
438  continue;
439  }
440  if ($role->getParent() == ROLE_FOLDER_ID) {
441  continue;
442  }
443  $role_title = $role->getTitle();
444 
445  if ($ref_id = ilUtil::__extractRefId($role_title)) {
447  continue;
448  }
449 
450  if (self::MEMBER == ($status & self::MEMBER) && strpos(
451  $role_title,
452  "member"
453  ) !== false) {
454  $ref_ids [] = $ref_id;
455  } elseif (self::TUTOR == ($status & self::TUTOR) && strpos(
456  $role_title,
457  "tutor"
458  ) !== false) {
459  $ref_ids [] = $ref_id;
460  } elseif (self::ADMIN == ($status & self::ADMIN) && strpos(
461  $role_title,
462  "admin"
463  ) !== false) {
464  $ref_ids [] = $ref_id;
465  } elseif (($status & self::OWNER) == self::OWNER && $ilObjDataCache->lookupOwner($ilObjDataCache->lookupObjId($ref_id)) == $user_id) {
466  $ref_ids [] = $ref_id;
467  }
468  }
469  }
470  }
471  }
472  if (($status & self::OWNER) == self::OWNER) {
473  $owned_objects = ilObjectFactory::getObjectsForOwner("crs", $user_id);
474  $refs = [];
475  foreach ($owned_objects as $obj_id) {
476  $allrefs = ilObject::_getAllReferences($obj_id);
477  foreach ($allrefs as $r) {
478  if ($tree->isDeleted($r)) {
479  continue;
480  }
481  if ($tree->isInTree($r)) {
482  $refs[] = $r;
483  }
484  }
485  if (count($refs) > 0) {
486  $ref_ids[] = array_pop($refs);
487  }
488  }
489  }
490  $ref_ids = array_unique($ref_ids);
491 
492  $ref_ids = array_unique($ref_ids);
493 
494  $xmlResultSet = new ilXMLResultSet();
495  $xmlResultSet->addColumn("ref_id");
496  $xmlResultSet->addColumn("xml");
497  $xmlResultSet->addColumn("parent_ref_id");
498 
499  global $DIC;
500 
501  $ilUser = $DIC['ilUser'];
502  //#18004
503  // Enable to see own participations by reducing the needed permissions
504  $permission = $user_id === $ilUser->getId() ? 'read' : 'write';
505 
506  foreach ($ref_ids as $course_id) {
507  $course_obj = $this->checkObjectAccess($course_id, ['crs'], $permission, true);
508  if ($course_obj instanceof ilObjCourse) {
509  $row = new ilXMLResultSetRow();
510  $row->setValue("ref_id", $course_id);
511  $xmlWriter = new ilCourseXMLWriter($course_obj);
512  $xmlWriter->setAttachUsers(false);
513  $xmlWriter->start();
514  $row->setValue("xml", $xmlWriter->getXML());
515  $row->setValue("parent_ref_id", $tree->getParentId($course_id));
516  $xmlResultSet->addRow($row);
517  }
518  }
519  $xmlResultSetWriter = new ilXMLResultSetWriter($xmlResultSet);
520  $xmlResultSetWriter->start();
521  return $xmlResultSetWriter->getXML();
522  }
523 }
addCourse(string $sid, int $target_id, string $crs_xml)
XML Writer for XMLResultSet.
static _getAllReferences(int $id)
get all reference ids for object ID
deleteAll()
Definition: class.ilMD.php:302
raiseError(string $a_message, $a_code)
Row Class for XMLResultSet.
SaxParserException thrown by ilSaxParser if property throwException is set.
getCoursesForUser(string $sid, string $parameters)
static _lookupObjId(int $ref_id)
static _deleteAll(int $a_obj_id)
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 _getInstanceByObjId(int $a_obj_id)
static getObjectsForOwner(string $object_type, int $owner_id)
returns all objects of an owner, filtered by type, objects are not deleted!
deleteCourse(string $sid, int $course_id)
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 ...
$log
Definition: ltiresult.php:34
excludeCourseMember(string $sid, int $course_id, int $user_id)
const ROLE_FOLDER_ID
Definition: constants.php:34
assignCourseMember(string $sid, int $course_id, int $user_id, string $type)
static _deleteAllEntries(int $a_obj_id)
Delete all entries Normally called in case of object deletion.
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupType(int $id, bool $reference=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static __extractRefId(string $role_title)
extract ref id from role title, e.g.
$r