-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLtiAssignmentsGradesControllers.cs
More file actions
84 lines (74 loc) · 2.71 KB
/
LtiAssignmentsGradesControllers.cs
File metadata and controls
84 lines (74 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using HwProj.APIGateway.API.Lti.Services;
using HwProj.CoursesService.Client;
using HwProj.Models.SolutionsService;
using HwProj.SolutionsService.Client;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using LtiAdvantage.AssignmentGradeServices;
namespace HwProj.APIGateway.API.Lti.Controllers;
[Route("api/lti")]
[ApiController]
[Authorize(AuthenticationSchemes = "LtiScheme")]
public class LtiAssignmentsGradesControllers(
ICoursesServiceClient coursesServiceClient,
ISolutionsServiceClient solutionsClient,
ILtiToolService toolService)
: ControllerBase
{
[HttpPost("lineItem/{taskId}/scores")]
[Consumes("application/json", "application/vnd.ims.lis.v1.score+json")]
public async Task<IActionResult> UpdateTaskScore(long taskId, [FromBody] Score score)
{
var scopeClaim = User.FindFirst("scope")?.Value;
if (string.IsNullOrEmpty(scopeClaim) || !scopeClaim.Contains("https://purl.imsglobal.org/spec/lti-ags/scope/score"))
{
return Forbid();
}
var toolClientId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value
?? User.FindFirst("sub")?.Value;
if (string.IsNullOrEmpty(toolClientId))
{
return Unauthorized("Unknown tool client id.");
}
var tool = toolService.GetByClientId(toolClientId);
if (tool == null)
{
return BadRequest("Tool not found.");
}
var course = await coursesServiceClient.GetCourseByTaskForLti(taskId, score.UserId);
if (course == null)
{
return BadRequest("The task does not belong to any course.");
}
if (course.LtiToolName != tool.Name)
{
return BadRequest("This tool does not apply to this course.");
}
if (score.ScoreGiven < 0 || score.ScoreGiven > score.ScoreMaximum)
{
return BadRequest("ScoreGiven must be between 0 and ScoreMaximum.");
}
try
{
await solutionsClient.PostAndRateSolutionForLti(
taskId: taskId,
userId: score.UserId,
scoreGiven: score.ScoreGiven,
scoreMaximum: score.ScoreMaximum,
comment: $"Результат: {score.ScoreGiven}/{score.ScoreMaximum}\n\n" + score.Comment);
return Ok(new { message = "Score updated successfully" });
}
catch (KeyNotFoundException ex)
{
return NotFound(ex.Message);
}
catch (Exception)
{
return StatusCode(500, "Internal Server Error");
}
}
}