Beispiel #1
0
        /// <summary>
        /// Download a submission
        /// </summary>
        public void DownloadSubmission(string userid, int submissionId)
        {
            var user = _db.AspNetUsers.SingleOrDefault(x => x.Id == userid);

            if (user != null)
            {
                var submission = _db.Submissions.SingleOrDefault(x => x.Id == submissionId);
                if (submission != null)
                {
                    var  collaborators = new List <CollaboratorViewModel>();
                    bool isTeacher     = submission.Assignment.CourseInstance.Teachers.Any(x => x.AspNetUser == user);

                    if (!isTeacher)
                    {
                        collaborators = _studentService.GetCollaborators(submission.AssignmentId, userid);
                    }

                    if (collaborators.Count != 0 || isTeacher) // check if user is related to the submission group
                    {
                        var path = GetSubmissionsPath() +
                                   submission.AssignmentId + "\\" +
                                   submission.ProblemId + "\\" +
                                   submission.Id + "\\" +
                                   submission.Id + submission.Problem.Filetype;

                        DownloadFile(path, submission.OriginalFileName);
                    }
                }
            }
        }
        /// <summary>
        /// Used to calculate new total grade for assignment based on one submissionId, called right after grading that submission in the teacher controller.
        /// Updates the totalGrade for all collaborators in the AssignmentGroups table.
        /// </summary>
        public bool UpdateAssignmentGradeBySubmissionId(int submissionId)
        {
            // Get the initial submission
            var initialSubmission = _db.Submissions.SingleOrDefault(x => x.Id == submissionId);

            if (initialSubmission != null)
            {
                //Gets all collaborators in the assignment
                var studentService = new StudentService();
                var collaborators  = studentService.GetCollaborators(initialSubmission.AssignmentId, initialSubmission.StudentId);

                // Gets all problems in the assignment
                var problemsInAssignemnt = _db.AssignmentProblems.Where(x => x.AssignmentId == initialSubmission.AssignmentId);

                // Initializes the totalGrade to 0.0
                var totalGrade = 0.0;

                if (collaborators != null && problemsInAssignemnt != null)
                {
                    foreach (var problem in problemsInAssignemnt)
                    {
                        var problemWeight = problem.Weight / 100;
                        var problemGrade  = 0.0;
                        foreach (var student in collaborators)
                        {
                            var submission = _db.Submissions.Where(x => x.StudentId == student.Id && x.AssignmentId == initialSubmission.AssignmentId && x.ProblemId == initialSubmission.ProblemId).OrderByDescending(y => y.SubmissionGrade.Grade).FirstOrDefault();
                            // Checks if the best submission from the student is the best amongst his collaborators. If it is it is assigned to the problemGrade variable
                            if (submission != null && submission.SubmissionGrade != null && submission.SubmissionGrade.Grade.Value * problemWeight > problemGrade)
                            {
                                problemGrade = submission.SubmissionGrade.Grade.Value * problemWeight;
                            }
                        }
                        // Update totalGrade with the best problemGrade
                        totalGrade += problemGrade;
                    }
                }

                // The database update is made here, assigning the totalGrade to all groupMembers in the assignment
                var group = initialSubmission.Assignment.AssignmentGroups.SingleOrDefault(x => x.AssignmentId == initialSubmission.AssignmentId && x.UserId == initialSubmission.StudentId);
                if (group != null)
                {
                    var groupMembers = _db.AssignmentGroups.Where(x => x.AssignmentId == initialSubmission.AssignmentId && x.GroupNumber == group.GroupNumber);
                    foreach (var member in groupMembers)
                    {
                        member.AssignmentGrade = totalGrade;
                    }
                }
            }
            try {
                _db.SaveChanges();
                return(true);
            }
            catch (Exception e) {
                return(false);
            }
        }