19declare(strict_types=1);
35 $this->
id =
'Score.collection';
36 $this->
template =
'/{context_id}/lineitems/{item_id}/lineitem/scores';
37 $this->variables[] =
'Scores.url';
38 $this->formats[] =
'application/vnd.ims.lis.v1.scorecontainer+json';
39 $this->formats[] =
'application/vnd.ims.lis.v1.score+json';
40 $this->methods[] =
'POST';
49 $contextId =
$params[
'context_id'];
67 throw new Exception(
'invalid request', 401);
76 }
catch (Exception
$e) {
86 $logger =
$DIC->logger()->root();
88 $logger->info(
'checkScore');
89 $score = json_decode($requestData);
92 if ($userId ==
null) {
94 throw new Exception(
'User not available', 404);
101 !isset($score->userId) ||
102 !isset($score->gradingProgress) ||
103 !isset($score->activityProgress) ||
104 !isset($score->timestamp) ||
105 isset($score->timestamp) && !self::validate_iso8601_date($score->timestamp) ||
106 (isset($score->scoreGiven) && !is_numeric($score->scoreGiven)) ||
107 (isset($score->scoreGiven) && !isset($score->scoreMaximum)) ||
108 (isset($score->scoreMaximum) && !is_numeric($score->scoreMaximum))
112 throw new Exception(
'Incorrect score received', 400);
116 if (!isset($score->scoreMaximum)) {
117 $score->scoreMaximum = 1;
119 if (!isset($score->scoreGiven)) {
120 $score->scoreGiven = 0;
123 $result = (float) $score->scoreGiven / (
float) $score->scoreMaximum;
127 $ltiObjRes->readProperties(
$objId);
129 if (!$ltiObjRes->isAvailable()) {
130 throw new Exception(
'Tool for Object not available', 404);
136 if ($score->activityProgress ===
'InProgress') {
138 } elseif ($score->activityProgress ===
'Completed' && $score->gradingProgress ===
'FullyGraded') {
139 if ($result >= $ltiObjRes->getMasteryScore()) {
144 } elseif ($score->activityProgress ===
'Completed' && $score->gradingProgress ===
'Failed') {
148 $lp_percentage = (
int) round(100 * $result);
150 ilObjLTIConsumer::getLogger()->info(
"lp_status: $lp_status, lp_percentage: $lp_percentage, result: $result, mastery_score: " . $ltiObjRes->getMasteryScore());
153 if (empty($consRes)) {
154 $DIC->database()->insert(
155 'lti_consumer_results',
157 'id' => array(
'integer',
$DIC->database()->nextId(
'lti_consumer_results')),
158 'obj_id' => array(
'integer',
$objId),
159 'usr_id' => array(
'integer', $userId),
160 'result' => array(
'float', $result)
164 $DIC->database()->replace(
165 'lti_consumer_results',
167 'id' => array(
'integer', $consRes->id)
170 'obj_id' => array(
'integer',
$objId),
171 'usr_id' => array(
'integer', $userId),
172 'result' => array(
'float', $result)
179 $ltiTimestamp = DateTimeImmutable::createFromFormat(DateTimeInterface::RFC3339_EXTENDED, $score->timestamp);
180 if (!$ltiTimestamp) {
181 $ltiTimestamp = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, $score->timestamp);
183 if (!$ltiTimestamp) {
184 $ltiTimestamp =
new DateTime(
'now');
187 'id' => array(
'integer',
$DIC->database()->nextId(
'lti_consumer_grades')),
188 'obj_id' => array(
'integer',
$objId),
189 'usr_id' => array(
'integer', $userId),
190 'score_given' => array(
'float', $score->scoreGiven),
191 'score_maximum' => array(
'float', $score->scoreMaximum),
192 'activity_progress' => array(
'text', $score->activityProgress),
193 'grading_progress' => array(
'text', $score->gradingProgress),
194 'lti_timestamp' => array(
'timestamp',$ltiTimestamp->format(
"Y-m-d H:i:s")),
195 'stored' => array(
'timestamp', date(
"Y-m-d H:i:s"))
197 $DIC->database()->insert(
'lti_consumer_grades', $gradeValues);
206 if (preg_match(
'/^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])' .
207 '(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))' .
208 '([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)' .
209 '?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/', $date) > 0) {
static getUsrIdForObjectAndUsrIdent(int $objId, string $userIdent)
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
const LP_STATUS_NOT_ATTEMPTED_NUM
const LP_STATUS_FAILED_NUM
checkScore(string $requestData, int $objId)
static validate_iso8601_date(string $date)
execute(ilLTIConsumerServiceResponse $response)
Execute the request for this resource.
__construct(ilLTIConsumerServiceBase $service)
Class constructor.
const SCOPE_GRADESERVICE_SCORE
Scope for access to Score service.
array $params
Template variables parsed from the resource template.
checkTool(array $scopes=array())
Check to make sure the request is valid.
ilLTIConsumerServiceBase $service
Service associated with this resource.
parseTemplate()
Parse the template for variables.
static getByKeys(int $a_obj_id, int $a_usr_id, ?bool $a_create=false)
Get a result by object and user key.
getRequestData()
Get the request body.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc