ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilLTIConsumerResultService.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4
15{
16
20 protected $result = null;
21
25// protected $properties = array();
26
30 protected $availability = 0;
31
35 protected $mastery_score = 1;
36
40 protected $fields = array();
41
45 protected $message_ref_id = '';
49 protected $operation = '';
50
51
55 public function getMasteryScore() : float
56 {
58 }
59
63 public function setMasteryScore(float $mastery_score)
64 {
65 $this->mastery_score = $mastery_score;
66 }
67
71 public function getAvailability() : int
72 {
74 }
75
79 public function setAvailability(int $availability)
80 {
81 $this->availability = $availability;
82 }
83
87 public function isAvailable() : bool
88 {
89 if ($this->availability == 0) {
90 return false;
91 }
92 return true;
93 }
94
95
96
100 public function __construct()
101 {
102 }
103
107 public function handleRequest()
108 {
109 try {
110 // get the request as xml
111 $xml = simplexml_load_file('php://input');
112 $this->message_ref_id = (string) $xml->imsx_POXHeader->imsx_POXRequestHeaderInfo->imsx_messageIdentifier;
113 $request = current($xml->imsx_POXBody->children());
114 $this->operation = str_replace('Request', '', $request->getName());
115
116 $token = ilCmiXapiAuthToken::getInstanceByToken($request->resultRecord->sourcedGUID->sourcedId);
117
118 $this->result = ilLTIConsumerResult::getByKeys($token->getObjId(), $token->getUsrId(), false);
119 if (empty($this->result)) {
120 $this->respondUnauthorized("lti_consumer_results_id not found!");
121 return;
122 }
123
124
125 // check the object status
126 $this->readProperties($this->result->obj_id);
127
128 if (!$this->isAvailable()) {
129 $this->respondUnsupported();
130 return;
131 }
132
133 // Verify the signature
134 $this->readFields($this->result->obj_id);
135 $result = $this->checkSignature($this->fields['KEY'], $this->fields['SECRET']);
136 if ($result instanceof Exception) {
137 $this->respondUnauthorized($result->getMessage());
138 return;
139 }
140
141 // Dispatch the operation
142 switch ($this->operation) {
143 case 'readResult':
144 $this->readResult($request);
145 break;
146
147 case 'replaceResult':
148 $this->replaceResult($request);
149 $this->updateLP();
150 break;
151
152 case 'deleteResult':
153 $this->deleteResult($request);
154 $this->updateLP();
155 break;
156
157 default:
158 $this->respondUnknown();
159 break;
160 }
161 } catch (Exception $exception) {
162 $this->respondBadRequest($exception->getMessage());
163 }
164 }
165
170 protected function readResult($request)
171 {
172 $response = $this->loadResponse('readResult.xml');
173 $response = str_replace('{message_id}', md5(rand(0, 999999999)), $response);
174 $response = str_replace('{message_ref_id}', $this->message_ref_id, $response);
175 $response = str_replace('{operation}', $this->operation, $response);
176 $response = str_replace('{result}', $this->result->result, $response);
177
178 header('Content-type: application/xml');
179 echo $response;
180 }
181
186 protected function replaceResult($request)
187 {
188 $result = (string) $request->resultRecord->result->resultScore->textString;
189 if (!is_numeric($result)) {
190 $code = "failure";
191 $severity = "status";
192 $description = "The result is not a number.";
193 } elseif ($result < 0 or $result > 1) {
194 $code = "failure";
195 $severity = "status";
196 $description = "The result is out of range from 0 to 1.";
197 } else {
198 $this->result->result = (float) $result;
199 $this->result->save();
200
201 #if ($result >= $this->getMasteryScore())
202 #{
203 # $lp_status = ilLTIConsumerLPStatus::LP_STATUS_COMPLETED_NUM;
204 #}
205 #else
206 #{
207 # $lp_status = ilLTIConsumerLPStatus::LP_STATUS_FAILED_NUM;
208 #}
209 #$lp_percentage = 100 * $result;
210 #ilLTIConsumerLPStatus::trackResult($this->result->usr_id, $this->result->obj_id, $lp_status, $lp_percentage);
211
212 $code = "success";
213 $severity = "status";
214 $description = sprintf("Score for %s is now %s", $this->result->id, $this->result->result);
215 }
216
217 $response = $this->loadResponse('replaceResult.xml');
218 $response = str_replace('{message_id}', md5(rand(0, 999999999)), $response);
219 $response = str_replace('{message_ref_id}', $this->message_ref_id, $response);
220 $response = str_replace('{operation}', $this->operation, $response);
221 $response = str_replace('{code}', $code, $response);
222 $response = str_replace('{severity}', $severity, $response);
223 $response = str_replace('{description}', $description, $response);
224
225 header('Content-type: application/xml');
226 echo $response;
227 }
228
233 protected function deleteResult($request)
234 {
235 $this->result->result = null;
236 $this->result->save();
237
238 #$lp_status = ilLTIConsumerLPStatus::LP_STATUS_IN_PROGRESS_NUM;
239 #$lp_percentage = 0;
240 #ilLTIConsumerLPStatus::trackResult($this->result->usr_id, $this->result->obj_id, $lp_status, $lp_percentage);
241
242 $code = "success";
243 $severity = "status";
244
245 $response = $this->loadResponse('deleteResult.xml');
246 $response = str_replace('{message_id}', md5(rand(0, 999999999)), $response);
247 $response = str_replace('{message_ref_id}', $this->message_ref_id, $response);
248 $response = str_replace('{operation}', $this->operation, $response);
249 $response = str_replace('{code}', $code, $response);
250 $response = str_replace('{severity}', $severity, $response);
251
252 header('Content-type: application/xml');
253 echo $response;
254 }
255
256
262 protected function loadResponse($a_name)
263 {
264 return file_get_contents('./Modules/LTIConsumer/responses/' . $a_name);
265 }
266
267
272 protected function respondUnsupported()
273 {
274 $response = $this->loadResponse('unsupported.xml');
275 $response = str_replace('{message_id}', md5(rand(0, 999999999)), $response);
276 $response = str_replace('{message_ref_id}', $this->message_ref_id, $response);
277 $response = str_replace('{operation}', $this->operation, $response);
278
279 header('Content-type: application/xml');
280 echo $response;
281 }
282
286 protected function respondUnknown()
287 {
288 $response = $this->loadResponse('unknown.xml');
289 $response = str_replace('{message_id}', md5(rand(0, 999999999)), $response);
290 $response = str_replace('{message_ref_id}', $this->message_ref_id, $response);
291 $response = str_replace('{operation}', $this->operation, $response);
292
293 header('Content-type: application/xml');
294 echo $response;
295 }
296
297
302 protected function respondBadRequest($message = null)
303 {
304 header('HTTP/1.1 400 Bad Request');
305 header('Content-type: text/plain');
306 if (isset($message)) {
307 echo $message;
308 } else {
309 echo 'This is not a well-formed LTI Basic Outcomes Service request.';
310 }
311 }
312
313
319 protected function respondUnauthorized($message = null)
320 {
321 header('HTTP/1.1 401 Unauthorized');
322 header('Content-type: text/plain');
323 if (isset($message)) {
324 echo $message;
325 } else {
326 echo 'This request could not be authorized.';
327 }
328 }
329
330
336 private function readProperties($a_obj_id)
337 {
338 global $DIC;
339
340 $query = "
341 SELECT lti_ext_provider.availability, lti_consumer_settings.mastery_score
342 FROM lti_ext_provider, lti_consumer_settings
343 WHERE lti_ext_provider.id = lti_consumer_settings.provider_id
344 AND lti_consumer_settings.obj_id = %s
345 ";
346
347 $res = $DIC->database()->queryF($query, array('integer'), array($a_obj_id));
348
349 if ($row = $DIC->database()->fetchAssoc($res)) {
350 //$this->properties = $row;
351 $this->setAvailability((int) $row['availability']);
352 $this->setMasteryScore((float) $row['mastery_score']);
353 }
354 }
355
361 private function readFields($a_obj_id)
362 {
363 global $DIC;
364
365 $query = "
366 SELECT lti_ext_provider.provider_key, lti_ext_provider.provider_secret, lti_consumer_settings.launch_key, lti_consumer_settings.launch_secret
367 FROM lti_ext_provider, lti_consumer_settings
368 WHERE lti_ext_provider.id = lti_consumer_settings.provider_id
369 AND lti_consumer_settings.obj_id = %s
370 ";
371
372 $res = $DIC->database()->queryF($query, array('integer'), array($a_obj_id));
373
374 while ($row = $DIC->database()->fetchAssoc($res)) {
375 if (strlen($row["launch_key"] > 0)) {
376 $this->fields["KEY"] = $row["launch_key"];
377 } else {
378 $this->fields["KEY"] = $row["provider_key"];
379 }
380 if (strlen($row["launch_key"] > 0)) {
381 $this->fields["SECRET"] = $row["launch_secret"];
382 } else {
383 $this->fields["SECRET"] = $row["provider_secret"];
384 }
385 }
386 }
387
392 private function checkSignature($a_key, $a_secret)
393 {
394 require_once('./Modules/LTIConsumer/lib/OAuth.php');
395 require_once('./Modules/LTIConsumer/lib/TrivialOAuthDataStore.php');
396
398 $store->add_consumer($this->fields['KEY'], $this->fields['SECRET']);
399
401 $method = new OAuthSignatureMethod_HMAC_SHA1();
402 $server->add_signature_method($method);
403
404 $request = OAuthRequest::from_request();
405 try {
406 $server->verify_request($request);
407 } catch (Exception $e) {
408 return $e;
409 }
410 return true;
411 }
412
413 protected function updateLP()
414 {
415 if (!($this->result instanceof ilLTIConsumerResult)) {
416 return;
417 }
418
419 ilLPStatusWrapper::_updateStatus($this->result->getObjId(), $this->result->getUsrId());
420 }
421}
An exception for terminatinating execution or to throw for unit testing.
static from_request($http_method=null, $http_url=null, $parameters=null)
attempt to build up a request from what was passed to the server
Definition: OAuth.php:228
A Trivial memory-based store - no support for tokens.
static _updateStatus($a_obj_id, $a_usr_id, $a_obj=null, $a_percentage=false, $a_force_raise=false)
Update status.
deleteResult($request)
Delete a stored result.
readResult($request)
Read a stored result.
respondUnauthorized($message=null)
Send an "unauthorized" response.
handleRequest()
Handle an incoming request from the LTI tool provider.
respondBadRequest($message=null)
Send a "bad request" response.
__construct()
Constructor: general initialisations.
respondUnsupported()
Send a response that the operation is not supported This depends on the status of the object.
loadResponse($a_name)
Load the XML template for the response.
readFields($a_obj_id)
Read the LTI Consumer object fields.
readProperties($a_obj_id)
Read the LTI Consumer object properties.
replaceResult($request)
Replace a stored result.
checkSignature($a_key, $a_secret)
Check the reqest signature.
respondUnknown()
Send a "unknown operation" response.
static getByKeys($a_obj_id, $a_usr_id, $a_create=false)
Get a result by object and user key.
if(!file_exists(getcwd() . '/ilias.ini.php'))
registration confirmation script for ilias
Definition: confirmReg.php:12
$server
$store
Definition: metadata.php:90
$xml
Definition: metadata.php:332
$query
$response
foreach($_POST as $key=> $value) $res
$errors fields
Definition: imgupload.php:51
$message
Definition: xapiexit.php:14
$token
Definition: xapitoken.php:57
$DIC
Definition: xapitoken.php:46