19 declare(strict_types=1);
34 $this->
id =
'Score.collection';
35 $this->
template =
'/{context_id}/lineitems/{item_id}/lineitem/scores';
36 $this->variables[] =
'Scores.url';
37 $this->formats[] =
'application/vnd.ims.lis.v1.scorecontainer+json';
38 $this->formats[] =
'application/vnd.ims.lis.v1.score+json';
39 $this->methods[] =
'POST';
48 $contextId =
$params[
'context_id'];
66 throw new Exception(
'invalid request', 401);
74 $response->
setCode($returnCode);
76 $response->
setCode($e->getCode());
84 $score = json_decode($requestData);
87 if ($userId ==
null) {
89 throw new Exception(
'User not available', 404);
94 !isset($score->userId) ||
95 !isset($score->gradingProgress) ||
96 !isset($score->activityProgress) ||
97 !isset($score->timestamp) ||
98 isset($score->timestamp) && !self::validate_iso8601_date($score->timestamp) ||
99 (isset($score->scoreGiven) && !is_numeric($score->scoreGiven)) ||
100 (isset($score->scoreGiven) && !isset($score->scoreMaximum)) ||
101 (isset($score->scoreMaximum) && !is_numeric($score->scoreMaximum))
105 throw new Exception(
'Incorrect score received', 400);
109 if (!isset($score->scoreMaximum)) {
110 $score->scoreMaximum = 1;
112 if (isset($score->scoreGiven)) {
113 if ($score->gradingProgress !=
'FullyGraded') {
114 $score->scoreGiven =
null;
117 $result = (float)$score->scoreGiven / (
float)$score->scoreMaximum;
122 $ltiObjRes->readProperties($objId);
124 if (!$ltiObjRes->isAvailable()) {
125 throw new Exception(
'Tool for Object not available', 404);
129 if ($result >= $ltiObjRes->getMasteryScore()) {
134 $lp_percentage = (
int) round(100 * $result);
137 if (empty($consRes)) {
142 if (!isset($consRes->id)) {
143 $consRes->id = $DIC->database()->nextId(
'lti_consumer_results');
145 $DIC->database()->replace(
146 'lti_consumer_results',
148 'id' => array(
'integer', $consRes->id)
151 'obj_id' => array(
'integer', $objId),
152 'usr_id' => array(
'integer', $userId),
153 'result' => array(
'float', $result)
159 $ltiTimestamp = DateTimeImmutable::createFromFormat(DateTimeInterface::RFC3339_EXTENDED, $score->timestamp);
160 if (!$ltiTimestamp) {
161 $ltiTimestamp = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, $score->timestamp);
163 if (!$ltiTimestamp) {
164 $ltiTimestamp =
new DateTime(
'now');
167 'id' => array(
'integer', $DIC->database()->nextId(
'lti_consumer_grades')),
168 'obj_id' => array(
'integer', $objId),
169 'usr_id' => array(
'integer', $userId),
170 'score_given' => array(
'float', $score->scoreGiven),
171 'score_maximum' => array(
'float', $score->scoreMaximum),
172 'activity_progress' => array(
'text', $score->activityProgress),
173 'grading_progress' => array(
'text', $score->gradingProgress),
174 'lti_timestamp' => array(
'timestamp',$ltiTimestamp->format(
"Y-m-d H:i:s")),
175 'stored' => array(
'timestamp', date(
"Y-m-d H:i:s"))
177 $DIC->database()->insert(
'lti_consumer_grades', $gradeValues);
186 if (preg_match(
'/^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])' .
187 '(\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])))' .
188 '([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)' .
189 '?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/', $date) > 0) {
const LP_STATUS_COMPLETED_NUM
array $params
Template variables parsed from the resource template.
execute(ilLTIConsumerServiceResponse $response)
Execute the request for this resource.
static validate_iso8601_date(string $date)
parseTemplate()
Parse the template for variables.
ilLTIConsumerServiceBase $service
Service associated with this resource.
const LP_STATUS_IN_PROGRESS_NUM
static getUsrIdForObjectAndUsrIdent(int $objId, string $userIdent)
getRequestData()
Get the request body.
__construct(ilLTIConsumerServiceBase $service)
const SCOPE_GRADESERVICE_SCORE
Scope for access to Score service.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
checkTool(array $scopes=array())
Check to make sure the request is valid.
setCode(int $code)
Set the response code.
checkScore(string $requestData, int $objId)
static getByKeys(int $a_obj_id, int $a_usr_id, ?bool $a_create=false)
Get a result by object and user key.
setReason(string $reason)
Set the response reason.
__construct(Container $dic, ilPlugin $plugin)
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.