ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilLTIConsumerResultService Class Reference
+ Collaboration diagram for ilLTIConsumerResultService:

Public Member Functions

 getMasteryScore ()
 
 setMasteryScore (float $mastery_score)
 
 getAvailability ()
 
 setAvailability (int $availability)
 
 isAvailable ()
 
 handleRequest ()
 Handle an incoming request from the LTI tool provider. More...
 
 readProperties (int $a_obj_id)
 Read the LTI Consumer object properties. More...
 

Protected Member Functions

 readResult (\SimpleXMLElement $request)
 Read a stored result. More...
 
 replaceResult (\SimpleXMLElement $request)
 Replace a stored result. More...
 
 deleteResult (\SimpleXMLElement $request)
 Delete a stored result. More...
 
 loadResponse ($a_name)
 Load the XML template for the response. More...
 
 respondUnsupported ()
 Send a response that the operation is not supported This depends on the status of the object. More...
 
 respondUnknown ()
 Send a "unknown operation" response. More...
 
 respondBadRequest (?string $message=null)
 Send a "bad request" response. More...
 
 respondUnauthorized (?string $message=null)
 Send an "unauthorized" response. More...
 
 updateLP ()
 

Protected Attributes

ilLTIConsumerResult $result = null
 
int $availability = 0
 
float $mastery_score = 1
 
array $fields = array()
 
string $message_ref_id = ''
 
string $operation = ''
 

Private Member Functions

 readFields (int $a_obj_id)
 Read the LTI Consumer object fields. More...
 
 checkSignature (string $a_key, string $a_secret)
 Check the reqest signature. More...
 

Detailed Description

Definition at line 34 of file class.ilLTIConsumerResultService.php.

Member Function Documentation

◆ checkSignature()

ilLTIConsumerResultService::checkSignature ( string  $a_key,
string  $a_secret 
)
private

Check the reqest signature.

Returns
bool Exception or true

Definition at line 391 of file class.ilLTIConsumerResultService.php.

391 : bool
392 {
393 $platform = new ilLTIPlatform();
394
395 $platform->setKey($a_key);
396 $platform->setSecret($a_secret);
397
398 $store = new OAuthDataStore($platform);
399
400 $server = new OAuthServer($store);
401 $method = new OAuthSignatureMethod_HMAC_SHA1();
402 $server->add_signature_method($method);
403
404 $request = OAuthRequest::from_request();
405
406 try {
407 $server->verify_request($request);
408 } catch (Exception $e) {
409 return false;
410 }
411 return true;
412 }
LTI provider for LTI launch.
$server
Definition: shib_login.php:28

References Vendor\Package\$e, and $server.

Referenced by handleRequest().

+ Here is the caller graph for this function:

◆ deleteResult()

ilLTIConsumerResultService::deleteResult ( \SimpleXMLElement  $request)
protected

Delete a stored result.

Definition at line 241 of file class.ilLTIConsumerResultService.php.

241 : void
242 {
243 $this->result->result = null;
244 $this->result->save();
245
247 $lp_percentage = 0;
248 ilLPStatus::writeStatus($this->result->obj_id, $this->result->usr_id, $lp_status, $lp_percentage, true);
249
250 $code = "success";
251 $severity = "status";
252
253 $response = $this->loadResponse('deleteResult.xml');
254 $response = str_replace('{message_id}', md5((string) rand(0, 999_999_999)), $response);
255 $response = str_replace('{message_ref_id}', $this->message_ref_id, $response);
256 $response = str_replace('{operation}', $this->operation, $response);
257 $response = str_replace('{code}', $code, $response);
258 $response = str_replace('{severity}', $severity, $response);
259
260 header('Content-type: application/xml');
261 echo $response;
262 }
static writeStatus(int $a_obj_id, int $a_user_id, int $a_status, int $a_percentage=0, bool $a_force_per=false, ?int &$a_old_status=self::LP_STATUS_NOT_ATTEMPTED_NUM)
Write status for user and object.
const LP_STATUS_IN_PROGRESS_NUM
loadResponse($a_name)
Load the XML template for the response.
$response
Definition: xapitoken.php:93

References $response, ILIAS\UI\examples\Symbol\Glyph\Header\header(), loadResponse(), ilLPStatus\LP_STATUS_IN_PROGRESS_NUM, and ilLPStatus\writeStatus().

Referenced by handleRequest().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getAvailability()

ilLTIConsumerResultService::getAvailability ( )

Definition at line 76 of file class.ilLTIConsumerResultService.php.

References $availability.

◆ getMasteryScore()

ilLTIConsumerResultService::getMasteryScore ( )

Definition at line 66 of file class.ilLTIConsumerResultService.php.

66 : float
67 {
69 }

References $mastery_score.

Referenced by replaceResult().

+ Here is the caller graph for this function:

◆ handleRequest()

ilLTIConsumerResultService::handleRequest ( )

Handle an incoming request from the LTI tool provider.

Definition at line 97 of file class.ilLTIConsumerResultService.php.

97 : void
98 {
99 try {
100
101 global $DIC;
102 $logger = $DIC->logger()->root();
103 $logger->info('LTI Consumer Result Service: Incoming request');
104 // get the request as xml
105 $xml = simplexml_load_file('php://input');
106 $logger->info('LTI Consumer Result Service: xml loaded');
107 $this->message_ref_id = (string) $xml->imsx_POXHeader->imsx_POXRequestHeaderInfo->imsx_messageIdentifier;
108 $children = (array) $xml->imsx_POXBody->children();
109 $request = current($children);
110
111 $ns = $xml->getNamespaces(true);
112 $body = $xml->children($ns[''])->imsx_POXBody;
113
114 $logger->info('LTI Consumer Result Service: request loaded');
115 $this->operation = str_replace('Request', '', $request->getName());
116
117 $request = $body->replaceResultRequest;
118 $token = ilCmiXapiAuthToken::getInstanceByToken((string) $request->resultRecord->sourcedGUID->sourcedId);
119 $logger->info("LTI Consumer Result Service: operation loaded ($this->operation), user " . $token->getUsrId() . " and objId " . $token->getObjId());
120
121 $logger->info("LTI Consumer Result Service: token loaded");
122 $this->result = ilLTIConsumerResult::getByKeys($token->getObjId(), $token->getUsrId(), false);
123 if (empty($this->result)) {
124 $logger->error('LTI Consumer Result Service: Incoming request');
125 $this->respondUnauthorized("lti_consumer_results_id not found!");
126 return;
127 }
128
129
130 // check the object status
131 $this->readProperties($this->result->obj_id);
132
133 if (!$this->isAvailable()) {
134 $this->respondUnsupported();
135 return;
136 }
137
138 // Verify the signature
139 $this->readFields($this->result->obj_id);
140 $result = $this->checkSignature($this->fields['KEY'], $this->fields['SECRET']);
141 if ($result instanceof Exception) {
142 $logger->error('LTI Consumer Result Service: Incoming request');
143 $this->respondUnauthorized($result->getMessage());
144 return;
145 }
146
147 $logger->info("LTI Consumer Result Service: Request signature verified, this->operation: $this->operation");
148
149 // Dispatch the operation
150 switch ($this->operation) {
151 case 'readResult':
152 $this->readResult($request);
153 break;
154
155 case 'replaceResult':
156 $this->replaceResult($request);
157 $this->updateLP();
158 break;
159
160 case 'deleteResult':
161 $this->deleteResult($request);
162 $this->updateLP();
163 break;
164
165 default:
166 $this->respondUnknown();
167 break;
168 }
169 } catch (Exception $exception) {
170 $this->respondBadRequest($exception->getMessage());
171 }
172 }
static getInstanceByToken(string $token)
readFields(int $a_obj_id)
Read the LTI Consumer object fields.
checkSignature(string $a_key, string $a_secret)
Check the reqest signature.
readProperties(int $a_obj_id)
Read the LTI Consumer object properties.
respondBadRequest(?string $message=null)
Send a "bad request" response.
respondUnsupported()
Send a response that the operation is not supported This depends on the status of the object.
respondUnauthorized(?string $message=null)
Send an "unauthorized" response.
replaceResult(\SimpleXMLElement $request)
Replace a stored result.
readResult(\SimpleXMLElement $request)
Read a stored result.
respondUnknown()
Send a "unknown operation" response.
deleteResult(\SimpleXMLElement $request)
Delete a stored result.
static getByKeys(int $a_obj_id, int $a_usr_id, ?bool $a_create=false)
Get a result by object and user key.
global $DIC
Definition: shib_login.php:26
$token
Definition: xapitoken.php:70

References $DIC, $result, $token, checkSignature(), deleteResult(), ilLTIConsumerResult\getByKeys(), ilCmiXapiAuthToken\getInstanceByToken(), isAvailable(), readFields(), readProperties(), readResult(), replaceResult(), respondBadRequest(), respondUnauthorized(), respondUnknown(), respondUnsupported(), and updateLP().

+ Here is the call graph for this function:

◆ isAvailable()

ilLTIConsumerResultService::isAvailable ( )

Definition at line 86 of file class.ilLTIConsumerResultService.php.

86 : bool
87 {
88 if ($this->availability == 0) {
89 return false;
90 }
91 return true;
92 }

Referenced by handleRequest().

+ Here is the caller graph for this function:

◆ loadResponse()

ilLTIConsumerResultService::loadResponse (   $a_name)
protected

Load the XML template for the response.

Parameters
stringfile name
Returns
string file content

Definition at line 270 of file class.ilLTIConsumerResultService.php.

270 : string
271 {
272 return file_get_contents(__DIR__ . '/../responses/' . $a_name);
273 }

Referenced by deleteResult(), readResult(), replaceResult(), respondUnknown(), and respondUnsupported().

+ Here is the caller graph for this function:

◆ readFields()

ilLTIConsumerResultService::readFields ( int  $a_obj_id)
private

Read the LTI Consumer object fields.

Definition at line 360 of file class.ilLTIConsumerResultService.php.

360 : void
361 {
362 global $DIC;
363
364 $query = "
365 SELECT lti_ext_provider.provider_key, lti_ext_provider.provider_secret, lti_consumer_settings.launch_key, lti_consumer_settings.launch_secret
366 FROM lti_ext_provider, lti_consumer_settings
367 WHERE lti_ext_provider.id = lti_consumer_settings.provider_id
368 AND lti_consumer_settings.obj_id = %s
369 ";
370
371 $res = $DIC->database()->queryF($query, array('integer'), array($a_obj_id));
372
373 while ($row = $DIC->database()->fetchAssoc($res)) {
374 if (strlen($row["launch_key"]) > 0) {
375 $this->fields["KEY"] = $row["launch_key"];
376 } else {
377 $this->fields["KEY"] = $row["provider_key"];
378 }
379 if (strlen($row["launch_key"]) > 0) {
380 $this->fields["SECRET"] = $row["launch_secret"];
381 } else {
382 $this->fields["SECRET"] = $row["provider_secret"];
383 }
384 }
385 }
$res
Definition: ltiservices.php:69

References $DIC, and $res.

Referenced by handleRequest().

+ Here is the caller graph for this function:

◆ readProperties()

ilLTIConsumerResultService::readProperties ( int  $a_obj_id)

Read the LTI Consumer object properties.

Definition at line 337 of file class.ilLTIConsumerResultService.php.

337 : void
338 {
339 global $DIC;
340
341 $query = "
342 SELECT lti_ext_provider.availability, lti_consumer_settings.mastery_score
343 FROM lti_ext_provider, lti_consumer_settings
344 WHERE lti_ext_provider.id = lti_consumer_settings.provider_id
345 AND lti_consumer_settings.obj_id = %s
346 ";
347
348 $res = $DIC->database()->queryF($query, array('integer'), array($a_obj_id));
349
350 if ($row = $DIC->database()->fetchAssoc($res)) {
351 //$this->properties = $row;
352 $this->setAvailability((int) $row['availability']);
353 $this->setMasteryScore((float) $row['mastery_score']);
354 }
355 }

References $DIC, $res, setAvailability(), and setMasteryScore().

Referenced by handleRequest().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ readResult()

ilLTIConsumerResultService::readResult ( \SimpleXMLElement  $request)
protected

Read a stored result.

Definition at line 177 of file class.ilLTIConsumerResultService.php.

177 : void
178 {
179 $response = $this->loadResponse('readResult.xml');
180 $response = str_replace('{message_id}', md5((string) rand(0, 999_999_999)), $response);
181 $response = str_replace('{message_ref_id}', $this->message_ref_id, $response);
182 $response = str_replace('{operation}', $this->operation, $response);
183 $response = str_replace('{result}', (string) $this->result->result, $response);
184
185 header('Content-type: application/xml');
186 echo $response;
187 }

References $response, ILIAS\UI\examples\Symbol\Glyph\Header\header(), and loadResponse().

Referenced by handleRequest().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ replaceResult()

ilLTIConsumerResultService::replaceResult ( \SimpleXMLElement  $request)
protected

Replace a stored result.

Definition at line 192 of file class.ilLTIConsumerResultService.php.

192 : void
193 {
194 global $DIC;
195 $logger = $DIC->logger()->root();
196
197 $result = (string) $request->resultRecord->result->resultScore->textString;
198 $logger->info('LTI Consumer Result Service: Replace result. Result: ' . $result);
199 if (!is_numeric($result)) {
200 $code = "failure";
201 $severity = "status";
202 $description = "The result is not a number.";
203 } elseif ($result > 1) {
204 $code = "failure";
205 $severity = "status";
206 $description = "The result is out of range from 0 to 1.";
207 } else {
208 $this->result->result = (float) $result;
209 $this->result->save();
210
211 if ($result >= $this->getMasteryScore()) {
213 } else {
215 }
216 $lp_percentage = (int) round(100 * $result);
217
218 // Mantis #37080
219 ilLPStatus::writeStatus($this->result->obj_id, $this->result->usr_id, $lp_status, $lp_percentage, true);
220
221 $code = "success";
222 $severity = "status";
223 $description = sprintf("Score for %s is now %s", $this->result->id, $this->result->result);
224 }
225
226 $response = $this->loadResponse('replaceResult.xml');
227 $response = str_replace('{message_id}', md5((string) rand(0, 999_999_999)), $response);
228 $response = str_replace('{message_ref_id}', $this->message_ref_id, $response);
229 $response = str_replace('{operation}', $this->operation, $response);
230 $response = str_replace('{code}', $code, $response);
231 $response = str_replace('{severity}', $severity, $response);
232 $response = str_replace('{description}', $description, $response);
233
234 header('Content-type: application/xml');
235 echo $response;
236 }
const LP_STATUS_COMPLETED_NUM
save()
Save a result object.

References $DIC, $response, $result, getMasteryScore(), ILIAS\UI\examples\Symbol\Glyph\Header\header(), ILIAS\Repository\int(), loadResponse(), ilLPStatus\LP_STATUS_COMPLETED_NUM, ilLPStatus\LP_STATUS_IN_PROGRESS_NUM, ilLTIConsumerResult\save(), and ilLPStatus\writeStatus().

Referenced by handleRequest().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ respondBadRequest()

ilLTIConsumerResultService::respondBadRequest ( ?string  $message = null)
protected

Send a "bad request" response.

Definition at line 308 of file class.ilLTIConsumerResultService.php.

308 : void
309 {
310 header('HTTP/1.1 400 Bad Request');
311 header('Content-type: text/plain');
312 if (isset($message)) {
313 echo $message;
314 } else {
315 echo 'This is not a well-formed LTI Basic Outcomes Service request.';
316 }
317 }
$message
Definition: xapiexit.php:31

References $message, and ILIAS\UI\examples\Symbol\Glyph\Header\header().

Referenced by handleRequest().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ respondUnauthorized()

ilLTIConsumerResultService::respondUnauthorized ( ?string  $message = null)
protected

Send an "unauthorized" response.

Parameters
string | null$messageresponse message

Definition at line 323 of file class.ilLTIConsumerResultService.php.

323 : void
324 {
325 header('HTTP/1.1 401 Unauthorized');
326 header('Content-type: text/plain');
327 if (isset($message)) {
328 echo $message;
329 } else {
330 echo 'This request could not be authorized.';
331 }
332 }

References $message, and ILIAS\UI\examples\Symbol\Glyph\Header\header().

Referenced by handleRequest().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ respondUnknown()

ilLTIConsumerResultService::respondUnknown ( )
protected

Send a "unknown operation" response.

Definition at line 294 of file class.ilLTIConsumerResultService.php.

294 : void
295 {
296 $response = $this->loadResponse('unknown.xml');
297 $response = str_replace('{message_id}', md5((string) rand(0, 999_999_999)), $response);
298 $response = str_replace('{message_ref_id}', $this->message_ref_id, $response);
299 $response = str_replace('{operation}', $this->operation, $response);
300
301 header('Content-type: application/xml');
302 echo $response;
303 }

References $response, ILIAS\UI\examples\Symbol\Glyph\Header\header(), and loadResponse().

Referenced by handleRequest().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ respondUnsupported()

ilLTIConsumerResultService::respondUnsupported ( )
protected

Send a response that the operation is not supported This depends on the status of the object.

Definition at line 280 of file class.ilLTIConsumerResultService.php.

280 : void
281 {
282 $response = $this->loadResponse('unsupported.xml');
283 $response = str_replace('{message_id}', md5((string) rand(0, 999_999_999)), $response);
284 $response = str_replace('{message_ref_id}', $this->message_ref_id, $response);
285 $response = str_replace('{operation}', $this->operation, $response);
286
287 header('Content-type: application/xml');
288 echo $response;
289 }

References $response, ILIAS\UI\examples\Symbol\Glyph\Header\header(), and loadResponse().

Referenced by handleRequest().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setAvailability()

ilLTIConsumerResultService::setAvailability ( int  $availability)

Definition at line 81 of file class.ilLTIConsumerResultService.php.

81 : void
82 {
83 $this->availability = $availability;
84 }

References $availability.

Referenced by readProperties().

+ Here is the caller graph for this function:

◆ setMasteryScore()

ilLTIConsumerResultService::setMasteryScore ( float  $mastery_score)

Definition at line 71 of file class.ilLTIConsumerResultService.php.

71 : void
72 {
73 $this->mastery_score = $mastery_score;
74 }

References $mastery_score.

Referenced by readProperties().

+ Here is the caller graph for this function:

◆ updateLP()

ilLTIConsumerResultService::updateLP ( )
protected

Definition at line 414 of file class.ilLTIConsumerResultService.php.

414 : void
415 {
416 if (!($this->result instanceof ilLTIConsumerResult)) {
417 return;
418 }
419
420 ilLPStatusWrapper::_updateStatus($this->result->getObjId(), $this->result->getUsrId());
421 }
static _updateStatus(int $a_obj_id, int $a_usr_id, ?object $a_obj=null, bool $a_percentage=false, bool $a_force_raise=false)

References ilLPStatusWrapper\_updateStatus().

Referenced by handleRequest().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $availability

int ilLTIConsumerResultService::$availability = 0
protected

Definition at line 44 of file class.ilLTIConsumerResultService.php.

Referenced by getAvailability(), and setAvailability().

◆ $fields

array ilLTIConsumerResultService::$fields = array()
protected

Definition at line 54 of file class.ilLTIConsumerResultService.php.

◆ $mastery_score

float ilLTIConsumerResultService::$mastery_score = 1
protected

Definition at line 49 of file class.ilLTIConsumerResultService.php.

Referenced by getMasteryScore(), and setMasteryScore().

◆ $message_ref_id

string ilLTIConsumerResultService::$message_ref_id = ''
protected

Definition at line 59 of file class.ilLTIConsumerResultService.php.

◆ $operation

string ilLTIConsumerResultService::$operation = ''
protected

Definition at line 63 of file class.ilLTIConsumerResultService.php.

◆ $result

ilLTIConsumerResult ilLTIConsumerResultService::$result = null
protected

Definition at line 39 of file class.ilLTIConsumerResultService.php.

Referenced by handleRequest(), and replaceResult().


The documentation for this class was generated from the following file: