ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
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 
32 {
36  public function addExercise(string $sid, int $target_id, string $exercise_xml)
37  {
38  $this->initAuth($sid);
39  $this->initIlias();
40 
41  if (!$this->checkSession($sid)) {
42  return $this->raiseError($this->getMessage(), $this->getMessageCode());
43  }
44  global $DIC;
45 
46  $rbacsystem = $DIC['rbacsystem'];
47  $tree = $DIC['tree'];
48  $ilLog = $DIC['ilLog'];
49 
50  if (!$target_obj = ilObjectFactory::getInstanceByRefId($target_id, false)) {
51  return $this->raiseError('No valid target given.', 'Client');
52  }
53 
54  if (ilObject::_isInTrash($target_id)) {
55  return $this->raiseError("Parent with ID $target_id has been deleted.", 'CLIENT_OBJECT_DELETED');
56  }
57 
58  $allowed_types = array('cat', 'grp', 'crs', 'fold', 'root');
59  if (!in_array($target_obj->getType(), $allowed_types)) {
60  return $this->raiseError(
61  'No valid target type. Target must be reference id of "course, group, category or folder"',
62  'Client'
63  );
64  }
65 
66  if (!$rbacsystem->checkAccess('create', $target_id, "exc")) {
67  return $this->raiseError('No permission to create exercises in target ' . $target_id . '!', 'Client');
68  }
69 
70  // create object, put it into the tree and use the parser to update the settings
71 
72  $exercise = new ilObjExercise();
73  $exercise->create();
74  $exercise->createReference();
75  $exercise->putInTree($target_id);
76  $exercise->setPermissions($target_id);
77  $exercise->saveData();
78 
79  // we need this as workaround because file and member objects need to be initialised
80  $exercise->read();
81 
82  $exerciseXMLParser = new ilExerciseXMLParser($exercise, $exercise_xml);
83  try {
84  if ($exerciseXMLParser->start()) {
85  $exerciseXMLParser->getAssignment()->update();
86  return $exercise->update() ? $exercise->getRefId() : -1;
87  }
88  throw new ilExerciseException("Could not parse XML");
89  } catch (ilExerciseException $exception) {
90  return $this->raiseError(
91  $exception->getMessage(),
92  $exception->getCode() == ilExerciseException::$ID_MISMATCH ? "Client" : "Server"
93  );
94  }
95  }
96 
100  public function updateExercise(string $sid, int $requested_ref_id, string $exercise_xml)
101  {
102  $this->initAuth($sid);
103  $this->initIlias();
104 
105  if (!$this->checkSession($sid)) {
106  return $this->raiseError($this->getMessage(), $this->getMessageCode());
107  }
108  global $DIC;
109 
110  $rbacsystem = $DIC['rbacsystem'];
111  $tree = $DIC['tree'];
112  $ilLog = $DIC['ilLog'];
113 
114  if (ilObject::_isInTrash($requested_ref_id)) {
115  return $this->raiseError(
116  'Cannot perform update since exercise has been deleted.',
117  'CLIENT_OBJECT_DELETED'
118  );
119  }
120 
121  if (!$obj_id = ilObject::_lookupObjectId($requested_ref_id)) {
122  return $this->raiseError(
123  'No exercise found for id: ' . $requested_ref_id,
124  'CLIENT_OBJECT_NOT_FOUND'
125  );
126  }
127 
128  $permission_ok = false;
129  foreach ($ref_ids = ilObject::_getAllReferences($obj_id) as $ref_id) {
130  if ($rbacsystem->checkAccess('edit', $ref_id)) {
131  $permission_ok = true;
132  break;
133  }
134  }
135 
136  if (!$permission_ok) {
137  return $this->raiseError(
138  'No permission to edit the exercise with id: ' . $requested_ref_id,
139  'Server'
140  );
141  }
142 
145 
146  if (!is_object($exercise) || $exercise->getType() !== "exc") {
147  return $this->raiseError(
148  'Wrong obj id or type for exercise with id ' . $requested_ref_id,
149  'CLIENT_OBJECT_NOI_FOUND'
150  );
151  }
152 
153  $exerciseXMLParser = new ilExerciseXMLParser($exercise, $exercise_xml, $obj_id);
154 
155  try {
156  if ($exerciseXMLParser->start()) {
157  $exerciseXMLParser->getAssignment()->update();
158  return $exercise->update();
159  }
160  throw new ilExerciseException("Could not parse XML");
161  } catch (ilExerciseException $exception) {
162  return $this->raiseError(
163  $exception->getMessage(),
164  $exception->getCode() == ilExerciseException::$ID_MISMATCH ? "Client" : "Server"
165  );
166  }
167  }
168 
172  public function getExerciseXML(string $sid, int $requested_ref_id, int $attachFileContentsMode)
173  {
174  $this->initAuth($sid);
175  $this->initIlias();
176 
177  if (!$this->checkSession($sid)) {
178  return $this->raiseError($this->getMessage(), $this->getMessageCode());
179  }
180  if (!$requested_ref_id) {
181  return $this->raiseError(
182  'No ref id given. Aborting!',
183  'Client'
184  );
185  }
186  global $DIC;
187 
188  $rbacsystem = $DIC['rbacsystem'];
189  $tree = $DIC['tree'];
190  $ilLog = $DIC['ilLog'];
191 
192  // get obj_id
193  if (!$obj_id = ilObject::_lookupObjectId($requested_ref_id)) {
194  return $this->raiseError(
195  'No exercise found for id: ' . $requested_ref_id,
196  'Client'
197  );
198  }
199 
200  if (ilObject::_isInTrash($requested_ref_id)) {
201  return $this->raiseError("Parent with ID $requested_ref_id has been deleted.", 'Client');
202  }
203 
204  $permission_ok = false;
205  $write_permission_ok = false;
206  foreach ($ref_ids = ilObject::_getAllReferences($obj_id) as $ref_id) {
207  if ($rbacsystem->checkAccess('write', $ref_id)) { // #14299
208  $write_permission_ok = true;
209  break;
210  }
211  if ($rbacsystem->checkAccess('read', $ref_id)) {
212  $permission_ok = true;
213  break;
214  }
215  }
216 
217  if (!$permission_ok && !$write_permission_ok) {
218  return $this->raiseError(
219  'No permission to edit the object with id: ' . $requested_ref_id,
220  'Server'
221  );
222  }
223 
226 
227  if (!is_object($exercise) || $exercise->getType() !== "exc") {
228  return $this->raiseError(
229  'Wrong obj id or type for exercise with id ' . $requested_ref_id,
230  'Server'
231  );
232  }
233 
234  $xmlWriter = new ilExerciseXMLWriter();
235  $xmlWriter->setExercise($exercise);
236  $xmlWriter->setAttachMembers($write_permission_ok);
237  $xmlWriter->setAttachFileContents($attachFileContentsMode);
238  $xmlWriter->start();
239 
240  return $xmlWriter->getXML();
241  }
242 }
static _getAllReferences(int $id)
get all reference ids for object ID
raiseError(string $a_message, $a_code)
Class ilObjExercise.
$ref_id
Definition: ltiauth.php:66
Soap exercise administration methods.
$requested_ref_id
Definition: feed.php:41
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
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...