19declare(strict_types=1);
21use ceLTIc\LTI\OAuth\OAuthRequest;
22use ceLTIc\LTI\OAuth\OAuthServer;
23use ceLTIc\LTI\OAuth\OAuthSignatureMethod_HMAC_SHA1;
24use ceLTIc\LTI\OAuthDataStore;
88 if ($this->availability == 0) {
102 $logger =
$DIC->logger()->root();
103 $logger->info(
'LTI Consumer Result Service: Incoming request');
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);
111 $ns = $xml->getNamespaces(
true);
112 $body = $xml->children($ns[
''])->imsx_POXBody;
114 $logger->info(
'LTI Consumer Result Service: request loaded');
115 $this->operation = str_replace(
'Request',
'', $request->getName());
117 $request = $body->replaceResultRequest;
119 $logger->info(
"LTI Consumer Result Service: operation loaded ($this->operation), user " .
$token->getUsrId() .
" and objId " .
$token->getObjId());
121 $logger->info(
"LTI Consumer Result Service: token loaded");
123 if (empty($this->result)) {
124 $logger->error(
'LTI Consumer Result Service: Incoming request');
141 if (
$result instanceof Exception) {
142 $logger->error(
'LTI Consumer Result Service: Incoming request');
147 $logger->info(
"LTI Consumer Result Service: Request signature verified, this->operation: $this->operation");
150 switch ($this->operation) {
155 case 'replaceResult':
169 }
catch (Exception $exception) {
177 protected function readResult(\SimpleXMLElement $request): void
180 $response = str_replace(
'{message_id}', md5((
string) rand(0, 999_999_999)),
$response);
185 header(
'Content-type: application/xml');
195 $logger =
$DIC->logger()->root();
197 $result = (string) $request->resultRecord->result->resultScore->textString;
198 $logger->info(
'LTI Consumer Result Service: Replace result. Result: ' .
$result);
201 $severity =
"status";
202 $description =
"The result is not a number.";
205 $severity =
"status";
206 $description =
"The result is out of range from 0 to 1.";
208 $this->result->result = (float)
$result;
209 $this->result->
save();
222 $severity =
"status";
223 $description = sprintf(
"Score for %s is now %s", $this->result->id, $this->result->result);
227 $response = str_replace(
'{message_id}', md5((
string) rand(0, 999_999_999)),
$response);
234 header(
'Content-type: application/xml');
243 $this->result->result =
null;
244 $this->result->save();
251 $severity =
"status";
254 $response = str_replace(
'{message_id}', md5((
string) rand(0, 999_999_999)),
$response);
260 header(
'Content-type: application/xml');
272 return file_get_contents(__DIR__ .
'/../responses/' . $a_name);
283 $response = str_replace(
'{message_id}', md5((
string) rand(0, 999_999_999)),
$response);
287 header(
'Content-type: application/xml');
297 $response = str_replace(
'{message_id}', md5((
string) rand(0, 999_999_999)),
$response);
301 header(
'Content-type: application/xml');
310 header(
'HTTP/1.1 400 Bad Request');
311 header(
'Content-type: text/plain');
315 echo
'This is not a well-formed LTI Basic Outcomes Service request.';
325 header(
'HTTP/1.1 401 Unauthorized');
326 header(
'Content-type: text/plain');
330 echo
'This request could not be authorized.';
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
348 $res =
$DIC->database()->queryF($query, array(
'integer'), array($a_obj_id));
350 if ($row =
$DIC->database()->fetchAssoc(
$res)) {
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
371 $res =
$DIC->database()->queryF($query, array(
'integer'), array($a_obj_id));
373 while ($row =
$DIC->database()->fetchAssoc(
$res)) {
374 if (strlen($row[
"launch_key"]) > 0) {
375 $this->fields[
"KEY"] = $row[
"launch_key"];
377 $this->fields[
"KEY"] = $row[
"provider_key"];
379 if (strlen($row[
"launch_key"]) > 0) {
380 $this->fields[
"SECRET"] = $row[
"launch_secret"];
382 $this->fields[
"SECRET"] = $row[
"provider_secret"];
395 $platform->setKey($a_key);
396 $platform->setSecret($a_secret);
398 $store =
new OAuthDataStore($platform);
400 $server =
new OAuthServer($store);
401 $method =
new OAuthSignatureMethod_HMAC_SHA1();
402 $server->add_signature_method($method);
404 $request = OAuthRequest::from_request();
407 $server->verify_request($request);
408 }
catch (Exception
$e) {
static getInstanceByToken(string $token)
static _updateStatus(int $a_obj_id, int $a_usr_id, ?object $a_obj=null, bool $a_percentage=false, bool $a_force_raise=false)
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_COMPLETED_NUM
const LP_STATUS_IN_PROGRESS_NUM
readFields(int $a_obj_id)
Read the LTI Consumer object fields.
setMasteryScore(float $mastery_score)
checkSignature(string $a_key, string $a_secret)
Check the reqest signature.
readProperties(int $a_obj_id)
Read the LTI Consumer object properties.
handleRequest()
Handle an incoming request from the LTI tool provider.
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.
loadResponse($a_name)
Load the XML template for the response.
replaceResult(\SimpleXMLElement $request)
Replace a stored result.
ilLTIConsumerResult $result
setAvailability(int $availability)
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.
save()
Save a result object.