Exemple #1
0
        public ActionResult UpdateAssignment(IFormCollection collection,
                                             [FromServices] IGoogleAuthProvider auth)
        {
            string courseId     = collection["CourseId"];
            string courseWorkId = collection["CourseWorkId"];
            string answer       = collection["Answer"];

            Document document = new Document()
            {
                Title = "My Assignment " + courseWorkId
            };

            var docService = this.GetGoogleDocsService(auth);

            var docReq = docService.Documents.Create(document);
            var docRes = docReq.Execute();

            string documentId = docRes.DocumentId;
            string docUrl     = $"https://docs.google.com/document/d/{documentId}/edit";

            DocumentsResource.BatchUpdateRequest batchUpdate =
                docService
                .Documents
                .BatchUpdate(GenerateGoogleDocText(answer), documentId);

            var batchUpdateResponse = batchUpdate.Execute();

            var submission = this.GetGoogleClassRoomService(auth)
                             .Courses
                             .CourseWork
                             .StudentSubmissions
                             .List(courseId, courseWorkId).Execute();

            string submissionId = submission.StudentSubmissions[0].Id;


            List <Attachment> attachments = new List <Attachment>()
            {
                new Attachment()
                {
                    Link = new Google.Apis.Classroom.v1.Data.Link()
                    {
                        Url = docUrl
                    }
                }
            };

            ModifyAttachmentsRequest body = new ModifyAttachmentsRequest()
            {
                AddAttachments = attachments
            };

            var req = this.GetGoogleClassRoomService(auth)
                      .Courses
                      .CourseWork
                      .StudentSubmissions
                      .ModifyAttachments(body, courseId, courseWorkId, submissionId).Execute();

            return(Redirect(req.AlternateLink));
        }
        public static void AddAttachments(this StudentSubmission submission, params Attachment[] attachments)
        {
            var request = new ModifyAttachmentsRequest();

            foreach (var attachment in attachments)
            {
                request.AddAttachments.Add(attachment);
            }
            SubmissionResourceHandler.ModifyAttachments(request, submission.CourseId, submission.CourseWorkId, submission.Id).Execute();
        }
Exemple #3
0
        /// <summary>
        /// Modifies attachments of student submission. Attachments may only be added to student submissions whose type is `ASSIGNMENT`. This request must be made by the Developer Console project of the [OAuth client ID](https://support.google.com/cloud/answer/6158849) used to create the corresponding course work item. This method returns the following error codes: * `PERMISSION_DENIED` if the requesting user is not permitted to access the requested course or course work, if the user is not permitted to modify attachments on the requested student submission, or for access errors. * `INVALID_ARGUMENT` if the request is malformed. * `NOT_FOUND` if the requested course, course work, or student submission does not exist.
        /// Documentation https://developers.google.com/classroom/v1/reference/studentSubmissions/modifyAttachments
        /// Generation Note: This does not always build corectly.  Google needs to standardise things I need to figuer out which ones are wrong.
        /// </summary>
        /// <param name="service">Authenticated classroom service.</param>
        /// <param name="courseId">Identifier of the course. This identifier can be either the Classroom-assigned identifier or an alias.</param>
        /// <param name="courseWorkId">Identifier of the course work.</param>
        /// <param name="id">Identifier of the student submission.</param>
        /// <param name="body">A valid classroom v1 body.</param>
        /// <returns>StudentSubmissionResponse</returns>
        public static StudentSubmission ModifyAttachments(classroomService service, string courseId, string courseWorkId, string id, ModifyAttachmentsRequest body)
        {
            try
            {
                // Initial validation.
                if (service == null)
                {
                    throw new ArgumentNullException("service");
                }
                if (body == null)
                {
                    throw new ArgumentNullException("body");
                }
                if (courseId == null)
                {
                    throw new ArgumentNullException(courseId);
                }
                if (courseWorkId == null)
                {
                    throw new ArgumentNullException(courseWorkId);
                }
                if (id == null)
                {
                    throw new ArgumentNullException(id);
                }

                // Make the request.
                return(service.StudentSubmissions.ModifyAttachments(body, courseId, courseWorkId, id).Execute());
            }
            catch (Exception ex)
            {
                throw new Exception("Request StudentSubmissions.ModifyAttachments failed.", ex);
            }
        }