ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilSoapExerciseAdministration.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 /*
5  +-----------------------------------------------------------------------------+
6  | ILIAS open source |
7  +-----------------------------------------------------------------------------+
8  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
9  | |
10  | This program is free software; you can redistribute it and/or |
11  | modify it under the terms of the GNU General Public License |
12  | as published by the Free Software Foundation; either version 2 |
13  | of the License, or (at your option) any later version. |
14  | |
15  | This program is distributed in the hope that it will be useful, |
16  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
17  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18  | GNU General Public License for more details. |
19  | |
20  | You should have received a copy of the GNU General Public License |
21  | along with this program; if not, write to the Free Software |
22  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23  +-----------------------------------------------------------------------------+
24 */
25 
30 include_once './webservice/soap/classes/class.ilSoapAdministration.php';
31 
33 {
37  public function addExercise(string $sid, int $target_id, string $exercise_xml)
38  {
39  $this->initAuth($sid);
40  $this->initIlias();
41 
42  if (!$this->checkSession($sid)) {
43  return $this->raiseError($this->getMessage(), $this->getMessageCode());
44  }
45  global $DIC;
46 
47  $rbacsystem = $DIC['rbacsystem'];
48  $tree = $DIC['tree'];
49  $ilLog = $DIC['ilLog'];
50 
51  if (!$target_obj = ilObjectFactory::getInstanceByRefId($target_id, false)) {
52  return $this->raiseError('No valid target given.', 'Client');
53  }
54 
55  if (ilObject::_isInTrash($target_id)) {
56  return $this->raiseError("Parent with ID $target_id has been deleted.", 'CLIENT_OBJECT_DELETED');
57  }
58 
59  $allowed_types = array('cat', 'grp', 'crs', 'fold', 'root');
60  if (!in_array($target_obj->getType(), $allowed_types)) {
61  return $this->raiseError(
62  'No valid target type. Target must be reference id of "course, group, category or folder"',
63  'Client'
64  );
65  }
66 
67  if (!$rbacsystem->checkAccess('create', $target_id, "exc")) {
68  return $this->raiseError('No permission to create exercises in target ' . $target_id . '!', 'Client');
69  }
70 
71  // create object, put it into the tree and use the parser to update the settings
72  include_once './Modules/Exercise/classes/class.ilObjExercise.php';
73  include_once './Modules/Exercise/classes/class.ilExerciseXMLParser.php';
74  include_once './Modules/Exercise/exceptions/class.ilExerciseException.php';
75 
76  $exercise = new ilObjExercise();
77  $exercise->create();
78  $exercise->createReference();
79  $exercise->putInTree($target_id);
80  $exercise->setPermissions($target_id);
81  $exercise->saveData();
82 
83  // we need this as workaround because file and member objects need to be initialised
84  $exercise->read();
85 
86  $exerciseXMLParser = new ilExerciseXMLParser($exercise, $exercise_xml);
87  try {
88  if ($exerciseXMLParser->start()) {
89  $exerciseXMLParser->getAssignment()->update();
90  return $exercise->update() ? $exercise->getRefId() : -1;
91  }
92  throw new ilExerciseException("Could not parse XML");
93  } catch (ilExerciseException $exception) {
94  return $this->raiseError(
95  $exception->getMessage(),
96  $exception->getCode() == ilExerciseException::$ID_MISMATCH ? "Client" : "Server"
97  );
98  }
99  }
100 
104  public function updateExercise(string $sid, int $requested_ref_id, string $exercise_xml)
105  {
106  $this->initAuth($sid);
107  $this->initIlias();
108 
109  if (!$this->checkSession($sid)) {
110  return $this->raiseError($this->getMessage(), $this->getMessageCode());
111  }
112  global $DIC;
113 
114  $rbacsystem = $DIC['rbacsystem'];
115  $tree = $DIC['tree'];
116  $ilLog = $DIC['ilLog'];
117 
118  if (ilObject::_isInTrash($requested_ref_id)) {
119  return $this->raiseError(
120  'Cannot perform update since exercise has been deleted.',
121  'CLIENT_OBJECT_DELETED'
122  );
123  }
124 
125  if (!$obj_id = ilObject::_lookupObjectId($requested_ref_id)) {
126  return $this->raiseError(
127  'No exercise found for id: ' . $requested_ref_id,
128  'CLIENT_OBJECT_NOT_FOUND'
129  );
130  }
131 
132  $permission_ok = false;
133  foreach ($ref_ids = ilObject::_getAllReferences($obj_id) as $ref_id) {
134  if ($rbacsystem->checkAccess('edit', $ref_id)) {
135  $permission_ok = true;
136  break;
137  }
138  }
139 
140  if (!$permission_ok) {
141  return $this->raiseError(
142  'No permission to edit the exercise with id: ' . $requested_ref_id,
143  'Server'
144  );
145  }
146 
149 
150  if (!is_object($exercise) || $exercise->getType() !== "exc") {
151  return $this->raiseError(
152  'Wrong obj id or type for exercise with id ' . $requested_ref_id,
153  'CLIENT_OBJECT_NOI_FOUND'
154  );
155  }
156 
157  include_once './Modules/Exercise/classes/class.ilExerciseXMLParser.php';
158  include_once './Modules/Exercise/exceptions/class.ilExerciseException.php';
159  $exerciseXMLParser = new ilExerciseXMLParser($exercise, $exercise_xml, $obj_id);
160 
161  try {
162  if ($exerciseXMLParser->start()) {
163  $exerciseXMLParser->getAssignment()->update();
164  return $exercise->update();
165  }
166  throw new ilExerciseException("Could not parse XML");
167  } catch (ilExerciseException $exception) {
168  return $this->raiseError(
169  $exception->getMessage(),
170  $exception->getCode() == ilExerciseException::$ID_MISMATCH ? "Client" : "Server"
171  );
172  }
173  }
174 
178  public function getExerciseXML(string $sid, int $requested_ref_id, int $attachFileContentsMode)
179  {
180  $this->initAuth($sid);
181  $this->initIlias();
182 
183  if (!$this->checkSession($sid)) {
184  return $this->raiseError($this->getMessage(), $this->getMessageCode());
185  }
186  if (!$requested_ref_id) {
187  return $this->raiseError(
188  'No ref id given. Aborting!',
189  'Client'
190  );
191  }
192  global $DIC;
193 
194  $rbacsystem = $DIC['rbacsystem'];
195  $tree = $DIC['tree'];
196  $ilLog = $DIC['ilLog'];
197 
198  // get obj_id
199  if (!$obj_id = ilObject::_lookupObjectId($requested_ref_id)) {
200  return $this->raiseError(
201  'No exercise found for id: ' . $requested_ref_id,
202  'Client'
203  );
204  }
205 
206  if (ilObject::_isInTrash($requested_ref_id)) {
207  return $this->raiseError("Parent with ID $requested_ref_id has been deleted.", 'Client');
208  }
209 
210  $permission_ok = false;
211  $write_permission_ok = false;
212  foreach ($ref_ids = ilObject::_getAllReferences($obj_id) as $ref_id) {
213  if ($rbacsystem->checkAccess('write', $ref_id)) { // #14299
214  $write_permission_ok = true;
215  break;
216  }
217  if ($rbacsystem->checkAccess('read', $ref_id)) {
218  $permission_ok = true;
219  break;
220  }
221  }
222 
223  if (!$permission_ok && !$write_permission_ok) {
224  return $this->raiseError(
225  'No permission to edit the object with id: ' . $requested_ref_id,
226  'Server'
227  );
228  }
229 
232 
233  if (!is_object($exercise) || $exercise->getType() !== "exc") {
234  return $this->raiseError(
235  'Wrong obj id or type for exercise with id ' . $requested_ref_id,
236  'Server'
237  );
238  }
239 
240  include_once './Modules/Exercise/classes/class.ilExerciseXMLWriter.php';
241 
242  $xmlWriter = new ilExerciseXMLWriter();
243  $xmlWriter->setExercise($exercise);
244  $xmlWriter->setAttachMembers($write_permission_ok);
245  $xmlWriter->setAttachFileContents($attachFileContentsMode);
246  $xmlWriter->start();
247 
248  return $xmlWriter->getXML();
249  }
250 }
static _getAllReferences(int $id)
get all reference ids for object ID
raiseError(string $a_message, $a_code)
global $DIC
Definition: feed.php:28
Class ilObjExercise.
$ref_id
Definition: ltiauth.php:67
Soap exercise administration methods.
$requested_ref_id
Definition: feed.php:40
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
static _lookupObjectId(int $ref_id)
addExercise(string $sid, int $target_id, string $exercise_xml)
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...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...