public async Task <IActionResult> BulkGrade(CancellationToken cancellationToken, CourseModel model)
        {
            var appFlow = new AppFlowMetadata(ClientId, ClientSecret);
            var token   = await appFlow.Flow.LoadTokenAsync(model.UserId, cancellationToken);

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

            var gradingModel = new GradingModel
            {
                PersonImage = model.PersonImage,
                PersonName  = model.PersonName,
                UserId      = model.UserId,
                CourseId    = model.CourseId,
                CourseName  = model.CourseName
            };

            try
            {
                using (var classroomService = new ClassroomService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "gc2lti"
                }))
                {
                    gradingModel.Assignments = await GetAssignments(model, classroomService, cancellationToken);

                    gradingModel.Students = await GetStudents(model, classroomService, cancellationToken);

                    gradingModel.AssignmentGrades = await GetGrades(model, gradingModel.Assignments, gradingModel.Students, classroomService, cancellationToken);
                }
            }
            catch (Exception e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, e));
            }

            return(View(gradingModel));
        }
        public async Task <IActionResult> BulkGrade(CancellationToken cancellationToken, GradingModel model)
        {
            var appFlow = new AppFlowMetadata(ClientId, ClientSecret);
            var token   = await appFlow.Flow.LoadTokenAsync(model.UserId, cancellationToken);

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

            try
            {
                using (var classroomService = new ClassroomService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "gc2lti"
                }))
                {
                    for (var assignmentIndex = 0; assignmentIndex < model.AssignmentGrades.Length; assignmentIndex++)
                    {
                        var assignmentGrades = model.AssignmentGrades[assignmentIndex];
                        var assignment       = model.Assignments[assignmentIndex];

                        // Get the students' submissions
                        var submissionsRequest = classroomService.Courses.CourseWork.StudentSubmissions.List
                                                 (
                            model.CourseId,
                            assignment.CourseWorkId
                                                 );
                        var submissionsResponse = await submissionsRequest.ExecuteAsync(cancellationToken)
                                                  .ConfigureAwait(false);

                        for (var studentIndex = 0; studentIndex < assignmentGrades.Grades.Length; studentIndex++)
                        {
                            var grade   = assignmentGrades.Grades[studentIndex];
                            var student = model.Students[studentIndex];

                            var submission = submissionsResponse.StudentSubmissions
                                             .SingleOrDefault(s => s.UserId == student.StudentId);

                            if (submission != null)
                            {
                                submission.AssignedGrade = grade;
                                submission.DraftGrade    = grade;

                                var patchRequest = classroomService.Courses.CourseWork.StudentSubmissions.Patch
                                                   (
                                    submission,
                                    submission.CourseId,
                                    submission.CourseWorkId,
                                    submission.Id
                                                   );
                                patchRequest.UpdateMask = "AssignedGrade,DraftGrade";
                                await patchRequest.ExecuteAsync(cancellationToken).ConfigureAwait(false);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, e));
            }

            var courseModel = new CourseModel
            {
                CourseId   = model.CourseId,
                CourseName = model.CourseName,
                PersonName = model.PersonName,
                UserId     = model.UserId
            };

            return(RedirectToAction(nameof(BulkGrade), courseModel));
        }