Skip to content

Instantly share code, notes, and snippets.

@Cvmcosta
Last active June 27, 2025 09:45
Show Gist options
  • Select an option

  • Save Cvmcosta/2a503dd3df6905cd635d26d188f99c13 to your computer and use it in GitHub Desktop.

Select an option

Save Cvmcosta/2a503dd3df6905cd635d26d188f99c13 to your computer and use it in GitHub Desktop.
LTIJS Send grade method example
const lti = require('ltijs').Provider
/**
* sendGrade
*/
lti.app.post('/grade', async (req, res) => {
try {
const idtoken = res.locals.token // IdToken
const score = req.body.grade // User numeric score sent in the body
// Creating Grade object
const gradeObj = {
userId: idtoken.user,
scoreGiven: score,
scoreMaximum: 100,
activityProgress: 'Completed',
gradingProgress: 'FullyGraded'
}
// Selecting linetItem ID
let lineItemId = idtoken.platformContext.endpoint.lineitem // Attempting to retrieve it from idtoken
if (!lineItemId) {
const response = await lti.Grade.getLineItems(idtoken, { resourceLinkId: true })
const lineItems = response.lineItems
if (lineItems.length === 0) {
// Creating line item if there is none
console.log('Creating new line item')
const newLineItem = {
scoreMaximum: 100,
label: 'Grade',
tag: 'grade',
resourceLinkId: idtoken.platformContext.resource.id
}
const lineItem = await lti.Grade.createLineItem(idtoken, newLineItem)
lineItemId = lineItem.id
} else lineItemId = lineItems[0].id
}
// Sending Grade
const responseGrade = await lti.Grade.submitScore(idtoken, lineItemId, gradeObj)
return res.send(responseGrade)
} catch (err) {
return res.status(500).send({ err: err.message })
}
})
@Cvmcosta
Copy link
Author

The new process consists of:

  • Creating grade object
  • Checking the ID Token for a grade line
    • Checking a list of grade lines
    • Creating a new grade line if there are none
  • Sending new grade to specific grade line

@sammed-sankonatti
Copy link

provider:gradeService Access_token retrieved for [http://localhost/moodle42] +58ms
provider:gradeService Sending score to: http://localhost/moodle42/mod/lti/services.php/2/lineitems/44/lineitem/scores?type_id=12 +0ms
provider:gradeService {
provider:gradeService userId: '2',
provider:gradeService scoreGiven: 20,
provider:gradeService scoreMaximum: 100,
provider:gradeService activityProgress: 'Completed',
provider:gradeService gradingProgress: 'FullyGraded',
provider:gradeService timestamp: '2025-06-27T09:45:14.540Z'
provider:gradeService } +0ms
error = Response code 400 (Incorrect score received"{"userId":"2","scoreGiven":20,"scoreMaximum":100,"activityProgress":"Completed","gradingProgress":"FullyGraded","timestamp":"2025-06-27T09:45:14.540Z"}")

Grade submission is throwing this error. Please help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment