Ejemplo n.º 1
0
        public ActionResult SubmitAssignment(AssignmentSubmissionCreateViewModel submissions)
        {
            var     assignment = _teacherRepository.GetAssignmentById(submissions.AssignmentId);
            Subject subject    = _teacherRepository.GetSubjectById(submissions.SubjectId);
            Student student    = _teacherRepository.GetStudentByName(User.Identity.Name);

            var virtualPath = string.Format("~/StudentResources/Grammar11Plus/Assignments/Submissions/{0}/{1}", subject.SubjectName, CleanseAssignmentName(assignment.AssignmentName));

            //Save File to FileSystem:
            var fileBuffer = new byte[submissions.MediaContent.ContentLength];

            var physicalPath = Server.MapPath(virtualPath);
            var dirInfo      = new DirectoryInfo(physicalPath);

            if (!dirInfo.Exists)
            {
                dirInfo.Create();
            }

            FileInfo fileInfo1 = new FileInfo(physicalPath + "\\" + student.StudentFirsName + student.StudentLastName + submissions.MediaContent.FileName);

            if (fileInfo1.Exists)
            {
                fileInfo1.Delete();
            }
            FileInfo fileInfo = new FileInfo(physicalPath + "\\" + student.StudentFirsName + student.StudentLastName + submissions.MediaContent.FileName);

            using (var fileStream = fileInfo.Create())
            {
                var sizeRead = 0;
                while ((sizeRead = submissions.MediaContent.InputStream.Read(fileBuffer, 0, fileBuffer.Length)) > 0)
                {
                    fileStream.Write(fileBuffer, 0, sizeRead);
                }
                submissions.MediaContent.InputStream.Flush();
                submissions.MediaContent.InputStream.Close();
                fileStream.Flush();
                fileStream.Close();
            }
            var actualSubmission = new AssignmentSubmission
            {
                AssignmentSubmissionId = submissions.AssignmentSubmissionId,
                AssignmentId           = submissions.AssignmentId,
                DateDue        = assignment.DateDue,
                DateSubmitted  = DateTime.Now,
                StudentId      = (int)student.StudentId,
                StudentRole    = "Grammar11Plus",
                FilePath       = Url.Content(virtualPath + "/" + student.StudentFirsName + student.StudentLastName + submissions.MediaContent.FileName),
                IsSubmitted    = true,
                SubjectId      = assignment.SubjectId,
                TeacherId      = assignment.TeacherId,
                AssignmentName = assignment.AssignmentName,
                Notes          = submissions.Notes,
                CourseId       = assignment.CourseId
            };

            _teacherRepository.SaveOrUpdateAssignmentSubmissions(actualSubmission);
            return(View("SuccessfullCreation"));
        }