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