Esempio n. 1
0
        private async Task <ReadResultResponse> ReadResultAsync(ReadResultRequest request)
        {
            var response = new ReadResultResponse();

            var ltiRequest = await Request.ParseLtiRequestAsync();

            var signature = ltiRequest.GenerateSignature("secret");

            if (!ltiRequest.Signature.Equals(signature))
            {
                response.StatusCode = StatusCodes.Status401Unauthorized;
                return(response);
            }

            response.Result = _result;
            return(response);
        }
Esempio n. 2
0
        private async Task <ReadResultResponse> ReadResultAsync(ReadResultRequest arg)
        {
            var response = new ReadResultResponse();

            var ltiRequest = await Request.ParseLtiRequestAsync();

            var signature = ltiRequest.GenerateSignature("secret");

            if (!ltiRequest.Signature.Equals(signature))
            {
                response.StatusCode = StatusCodes.Status401Unauthorized;
                return(response);
            }

            // Read the grade from Google Classroom
            try
            {
                var lisResultSourcedId = JsonConvert.DeserializeObject <LisResultSourcedId>(arg.LisResultSourcedId);
                var googleUser         = await Db.GoogleUsers.FindAsync(lisResultSourcedId.TeacherId);

                var appFlow = new AppFlowMetadata(ClientId, ClientSecret, Db);
                var token   = await appFlow.Flow.LoadTokenAsync(googleUser.UserId, CancellationToken.None);

                var credential = new UserCredential(appFlow.Flow, googleUser.UserId, token);

                using (var classroomService = new ClassroomService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "gc2lti"
                }))
                {
                    var courseWorkRequest = classroomService.Courses.CourseWork.Get
                                            (
                        lisResultSourcedId.CourseId,
                        lisResultSourcedId.CourseWorkId
                                            );
                    var courseWork = await courseWorkRequest.ExecuteAsync();

                    var submissionsRequest = classroomService.Courses.CourseWork.StudentSubmissions.List
                                             (
                        lisResultSourcedId.CourseId,
                        lisResultSourcedId.CourseWorkId
                                             );
                    submissionsRequest.UserId = lisResultSourcedId.StudentId;
                    var submissionsResponse = await submissionsRequest.ExecuteAsync();

                    var submission = submissionsResponse.StudentSubmissions.FirstOrDefault();

                    if (submission == null)
                    {
                        response.StatusCode        = StatusCodes.Status404NotFound;
                        response.StatusDescription = "Submission was found.";
                    }
                    else
                    {
                        response.Result = new Result
                        {
                            SourcedId = arg.LisResultSourcedId,
                            Score     = submission.AssignedGrade / courseWork.MaxPoints
                        };
                        response.StatusDescription = $"Score={response.Result.Score}, AssignedGrade={submission.AssignedGrade}.";
                    }
                }
            }
            catch (Exception)
            {
                response.StatusCode = StatusCodes.Status500InternalServerError;
            }

            return(response);
        }