ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
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 25 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 Exception or true

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

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

Referenced by handleRequest().

361  {
363  $store->add_consumer($a_key, $a_secret);
364 
365  $server = new \ILIAS\LTIOAuth\OAuthServer($store);
366  $method = new \ILIAS\LTIOAuth\OAuthSignatureMethod_HMAC_SHA1();
367  $server->add_signature_method($method);
368 
369  $request = \ILIAS\LTIOAuth\OAuthRequest::from_request();
370  try {
371  $server->verify_request($request);
372  } catch (Exception $e) {
373  return $e;
374  }
375  return true;
376  }
$store
Definition: metadata.php:107
A Trivial memory-based store - no support for tokens.
$server
+ Here is the caller graph for this function:

◆ deleteResult()

ilLTIConsumerResultService::deleteResult ( \SimpleXMLElement  $request)
protected

Delete a stored result.

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

References $response, loadResponse(), ilLPStatus\LP_STATUS_IN_PROGRESS_NUM, and ilLPStatus\writeStatus().

Referenced by handleRequest().

210  : void
211  {
212  $this->result->result = null;
213  $this->result->save();
214 
216  $lp_percentage = 0;
217  ilLPStatus::writeStatus($this->result->obj_id, $this->result->usr_id, $lp_status, $lp_percentage, true);
218 
219  $code = "success";
220  $severity = "status";
221 
222  $response = $this->loadResponse('deleteResult.xml');
223  $response = str_replace('{message_id}', md5((string) rand(0, 999_999_999)), $response);
224  $response = str_replace('{message_ref_id}', $this->message_ref_id, $response);
225  $response = str_replace('{operation}', $this->operation, $response);
226  $response = str_replace('{code}', $code, $response);
227  $response = str_replace('{severity}', $severity, $response);
228 
229  header('Content-type: application/xml');
230  echo $response;
231  }
const LP_STATUS_IN_PROGRESS_NUM
loadResponse($a_name)
Load the XML template for the response.
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.
$response
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getAvailability()

ilLTIConsumerResultService::getAvailability ( )

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

References $availability.

67  : int
68  {
69  return $this->availability;
70  }

◆ getMasteryScore()

ilLTIConsumerResultService::getMasteryScore ( )

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

References $mastery_score.

Referenced by replaceResult().

57  : float
58  {
59  return $this->mastery_score;
60  }
+ Here is the caller graph for this function:

◆ handleRequest()

ilLTIConsumerResultService::handleRequest ( )

Handle an incoming request from the LTI tool provider.

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

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

88  : void
89  {
90  try {
91  // get the request as xml
92  $xml = simplexml_load_file('php://input');
93  $this->message_ref_id = (string) $xml->imsx_POXHeader->imsx_POXRequestHeaderInfo->imsx_messageIdentifier;
94  $request = current($xml->imsx_POXBody->children());
95  $this->operation = str_replace('Request', '', $request->getName());
96 
97  $token = ilCmiXapiAuthToken::getInstanceByToken((string) $request->resultRecord->sourcedGUID->sourcedId);
98 
99  $this->result = ilLTIConsumerResult::getByKeys($token->getObjId(), $token->getUsrId(), false);
100  if (empty($this->result)) {
101  $this->respondUnauthorized("lti_consumer_results_id not found!");
102  return;
103  }
104 
105 
106  // check the object status
107  $this->readProperties($this->result->obj_id);
108 
109  if (!$this->isAvailable()) {
110  $this->respondUnsupported();
111  return;
112  }
113 
114  // Verify the signature
115  $this->readFields($this->result->obj_id);
116  $result = $this->checkSignature($this->fields['KEY'], $this->fields['SECRET']);
117  if ($result instanceof Exception) {
118  $this->respondUnauthorized($result->getMessage());
119  return;
120  }
121 
122  // Dispatch the operation
123  switch ($this->operation) {
124  case 'readResult':
125  $this->readResult($request);
126  break;
127 
128  case 'replaceResult':
129  $this->replaceResult($request);
130  $this->updateLP();
131  break;
132 
133  case 'deleteResult':
134  $this->deleteResult($request);
135  $this->updateLP();
136  break;
137 
138  default:
139  $this->respondUnknown();
140  break;
141  }
142  } catch (Exception $exception) {
143  $this->respondBadRequest($exception->getMessage());
144  }
145  }
readFields(int $a_obj_id)
Read the LTI Consumer object fields.
respondUnknown()
Send a "unknown operation" response.
respondBadRequest(?string $message=null)
Send a "bad request" response.
checkSignature(string $a_key, string $a_secret)
Check the reqest signature.
$errors fields
Definition: imgupload.php:67
static getInstanceByToken(string $token)
deleteResult(\SimpleXMLElement $request)
Delete a stored result.
respondUnauthorized(?string $message=null)
Send an "unauthorized" response.
readProperties(int $a_obj_id)
Read the LTI Consumer object properties.
$token
Definition: xapitoken.php:70
respondUnsupported()
Send a response that the operation is not supported This depends on the status of the object...
$xml
Definition: metadata.php:351
static getByKeys(int $a_obj_id, int $a_usr_id, ?bool $a_create=false)
Get a result by object and user key.
replaceResult(\SimpleXMLElement $request)
Replace a stored result.
readResult(\SimpleXMLElement $request)
Read a stored result.
+ Here is the call graph for this function:

◆ isAvailable()

ilLTIConsumerResultService::isAvailable ( )

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

Referenced by handleRequest().

77  : bool
78  {
79  if ($this->availability == 0) {
80  return false;
81  }
82  return true;
83  }
+ 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 239 of file class.ilLTIConsumerResultService.php.

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

239  : string
240  {
241  return file_get_contents('./Modules/LTIConsumer/responses/' . $a_name);
242  }
+ 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 329 of file class.ilLTIConsumerResultService.php.

References $DIC, $query, $res, and fields.

Referenced by handleRequest().

329  : void
330  {
331  global $DIC;
332 
333  $query = "
334  SELECT lti_ext_provider.provider_key, lti_ext_provider.provider_secret, lti_consumer_settings.launch_key, lti_consumer_settings.launch_secret
335  FROM lti_ext_provider, lti_consumer_settings
336  WHERE lti_ext_provider.id = lti_consumer_settings.provider_id
337  AND lti_consumer_settings.obj_id = %s
338  ";
339 
340  $res = $DIC->database()->queryF($query, array('integer'), array($a_obj_id));
341 
342  while ($row = $DIC->database()->fetchAssoc($res)) {
343  if (strlen($row["launch_key"]) > 0) {
344  $this->fields["KEY"] = $row["launch_key"];
345  } else {
346  $this->fields["KEY"] = $row["provider_key"];
347  }
348  if (strlen($row["launch_key"]) > 0) {
349  $this->fields["SECRET"] = $row["launch_secret"];
350  } else {
351  $this->fields["SECRET"] = $row["provider_secret"];
352  }
353  }
354  }
$res
Definition: ltiservices.php:69
$errors fields
Definition: imgupload.php:67
global $DIC
Definition: feed.php:28
$query
+ Here is the caller graph for this function:

◆ readProperties()

ilLTIConsumerResultService::readProperties ( int  $a_obj_id)

Read the LTI Consumer object properties.

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

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

Referenced by handleRequest().

306  : void
307  {
308  global $DIC;
309 
310  $query = "
311  SELECT lti_ext_provider.availability, lti_consumer_settings.mastery_score
312  FROM lti_ext_provider, lti_consumer_settings
313  WHERE lti_ext_provider.id = lti_consumer_settings.provider_id
314  AND lti_consumer_settings.obj_id = %s
315  ";
316 
317  $res = $DIC->database()->queryF($query, array('integer'), array($a_obj_id));
318 
319  if ($row = $DIC->database()->fetchAssoc($res)) {
320  //$this->properties = $row;
321  $this->setAvailability((int) $row['availability']);
322  $this->setMasteryScore((float) $row['mastery_score']);
323  }
324  }
$res
Definition: ltiservices.php:69
global $DIC
Definition: feed.php:28
$query
+ 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 150 of file class.ilLTIConsumerResultService.php.

References $response, and loadResponse().

Referenced by handleRequest().

150  : void
151  {
152  $response = $this->loadResponse('readResult.xml');
153  $response = str_replace('{message_id}', md5((string) rand(0, 999_999_999)), $response);
154  $response = str_replace('{message_ref_id}', $this->message_ref_id, $response);
155  $response = str_replace('{operation}', $this->operation, $response);
156  $response = str_replace('{result}', (string) $this->result->result, $response);
157 
158  header('Content-type: application/xml');
159  echo $response;
160  }
loadResponse($a_name)
Load the XML template for the response.
$response
+ 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 165 of file class.ilLTIConsumerResultService.php.

References $response, getMasteryScore(), if, ILIAS\Repository\int(), loadResponse(), ilLPStatus\LP_STATUS_COMPLETED_NUM, ilLPStatus\LP_STATUS_IN_PROGRESS_NUM, ilLTIConsumerResult\save(), and ilLPStatus\writeStatus().

Referenced by handleRequest().

165  : void
166  {
167  $result = (string) $request->resultRecord->result->resultScore->textString;
168  if (!is_numeric($result)) {
169  $code = "failure";
170  $severity = "status";
171  $description = "The result is not a number.";
172  } elseif ($result < 0 or $result > 1) {
173  $code = "failure";
174  $severity = "status";
175  $description = "The result is out of range from 0 to 1.";
176  } else {
177  $this->result->result = (float) $result;
178  $this->result->save();
179 
180  if ($result >= $this->getMasteryScore()) {
182  } else {
184  }
185  $lp_percentage = (int) round(100 * $result);
186 
187 // Mantis #37080
188  ilLPStatus::writeStatus($this->result->obj_id, $this->result->usr_id, $lp_status, $lp_percentage, true);
189 
190  $code = "success";
191  $severity = "status";
192  $description = sprintf("Score for %s is now %s", $this->result->id, $this->result->result);
193  }
194 
195  $response = $this->loadResponse('replaceResult.xml');
196  $response = str_replace('{message_id}', md5((string) rand(0, 999_999_999)), $response);
197  $response = str_replace('{message_ref_id}', $this->message_ref_id, $response);
198  $response = str_replace('{operation}', $this->operation, $response);
199  $response = str_replace('{code}', $code, $response);
200  $response = str_replace('{severity}', $severity, $response);
201  $response = str_replace('{description}', $description, $response);
202 
203  header('Content-type: application/xml');
204  echo $response;
205  }
const LP_STATUS_COMPLETED_NUM
const LP_STATUS_IN_PROGRESS_NUM
save()
Save a result object.
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
loadResponse($a_name)
Load the XML template for the response.
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.
$response
+ 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 277 of file class.ilLTIConsumerResultService.php.

References $message.

Referenced by handleRequest().

277  : void
278  {
279  header('HTTP/1.1 400 Bad Request');
280  header('Content-type: text/plain');
281  if (isset($message)) {
282  echo $message;
283  } else {
284  echo 'This is not a well-formed LTI Basic Outcomes Service request.';
285  }
286  }
$message
Definition: xapiexit.php:32
+ 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 292 of file class.ilLTIConsumerResultService.php.

References $message.

Referenced by handleRequest().

292  : void
293  {
294  header('HTTP/1.1 401 Unauthorized');
295  header('Content-type: text/plain');
296  if (isset($message)) {
297  echo $message;
298  } else {
299  echo 'This request could not be authorized.';
300  }
301  }
$message
Definition: xapiexit.php:32
+ Here is the caller graph for this function:

◆ respondUnknown()

ilLTIConsumerResultService::respondUnknown ( )
protected

Send a "unknown operation" response.

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

References $response, and loadResponse().

Referenced by handleRequest().

263  : void
264  {
265  $response = $this->loadResponse('unknown.xml');
266  $response = str_replace('{message_id}', md5((string) rand(0, 999_999_999)), $response);
267  $response = str_replace('{message_ref_id}', $this->message_ref_id, $response);
268  $response = str_replace('{operation}', $this->operation, $response);
269 
270  header('Content-type: application/xml');
271  echo $response;
272  }
loadResponse($a_name)
Load the XML template for the response.
$response
+ 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 249 of file class.ilLTIConsumerResultService.php.

References $response, and loadResponse().

Referenced by handleRequest().

249  : void
250  {
251  $response = $this->loadResponse('unsupported.xml');
252  $response = str_replace('{message_id}', md5((string) rand(0, 999_999_999)), $response);
253  $response = str_replace('{message_ref_id}', $this->message_ref_id, $response);
254  $response = str_replace('{operation}', $this->operation, $response);
255 
256  header('Content-type: application/xml');
257  echo $response;
258  }
loadResponse($a_name)
Load the XML template for the response.
$response
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setAvailability()

ilLTIConsumerResultService::setAvailability ( int  $availability)

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

References $availability.

Referenced by readProperties().

72  : void
73  {
74  $this->availability = $availability;
75  }
+ Here is the caller graph for this function:

◆ setMasteryScore()

ilLTIConsumerResultService::setMasteryScore ( float  $mastery_score)

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

References $mastery_score.

Referenced by readProperties().

62  : void
63  {
64  $this->mastery_score = $mastery_score;
65  }
+ Here is the caller graph for this function:

◆ updateLP()

ilLTIConsumerResultService::updateLP ( )
protected

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

References ilLPStatusWrapper\_updateStatus().

Referenced by handleRequest().

378  : void
379  {
380  if (!($this->result instanceof ilLTIConsumerResult)) {
381  return;
382  }
383 
384  ilLPStatusWrapper::_updateStatus($this->result->getObjId(), $this->result->getUsrId());
385  }
static _updateStatus(int $a_obj_id, int $a_usr_id, ?object $a_obj=null, bool $a_percentage=false, bool $a_force_raise=false)
+ 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 35 of file class.ilLTIConsumerResultService.php.

Referenced by getAvailability(), and setAvailability().

◆ $fields

array ilLTIConsumerResultService::$fields = array()
protected

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

◆ $mastery_score

float ilLTIConsumerResultService::$mastery_score = 1
protected

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

Referenced by getMasteryScore(), and setMasteryScore().

◆ $message_ref_id

string ilLTIConsumerResultService::$message_ref_id = ''
protected

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

◆ $operation

string ilLTIConsumerResultService::$operation = ''
protected

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

◆ $result

ilLTIConsumerResult ilLTIConsumerResultService::$result = null
protected

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


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