Ejemplo n.º 1
0
        public static string[] GetFileExtensions(DeliverableType deliverableType)
        {
            var type = deliverableType.GetType();

            var fi = type.GetField(deliverableType.ToString());

            //we get the attributes of the selected language
            var attrs = (fi.GetCustomAttributes(typeof(FileExtensions), false) as FileExtensions[]);

            //make sure we have more than (should be exactly 1)
            if (attrs != null && (attrs.Length > 0 && attrs[0] != null))
            {
                return(attrs[0].Extensions);
            }

            //throw and exception if not decorated with any attrs because it is a requirement
            throw new Exception("Languages must have be decorated with a FileExtensionAttribute");
        }
Ejemplo n.º 2
0
        public ActionResult Create(int?id, IEnumerable <HttpPostedFileBase> files, int?authorTeamID = null)
        {
            if (id != null)
            {
                Assignment assignment = db.Assignments.Find(id);

                //submit event to the eventlog
                try
                {
                    if (assignment != null)
                    {
                        var sub = new SubmitEvent
                        {
                            AssignmentId = id.Value,
                            SenderId     = CurrentUser.ID,
                            SolutionName = assignment.AssignmentName,
                            CourseId     = assignment.CourseID,
                        };
                        int eventLogId = Posts.SaveEvent(sub);
                        DBHelper.AddToSubmitEventProperties(eventLogId);
                        if (eventLogId == -1)
                        {
                            throw new Exception("Failed to log submit event to the eventlog table -- Posts.SaveEvent returned -1");
                        }
                        else
                        {
                            if (DBHelper.InterventionEnabledForCourse(assignment.CourseID ?? -1))
                            {
                                //process suggestions if interventions are enabled for this course.
                                using (EventCollectionController ecc = new EventCollectionController())
                                {
                                    ecc.ProcessLogForInterventionSync((ActivityEvent)sub);

                                    string authKey = Request.Cookies["AuthKey"].Value.Split('=').Last();
                                    ecc.NotifyNewSuggestion(CurrentUser.ID, assignment.CourseID ?? 0, authKey);
                                }
                            }
                        }
                    }
                    else
                    {
                        var sub = new SubmitEvent
                        {
                            AssignmentId = id.Value,
                            SenderId     = CurrentUser.ID,
                            SolutionName = "NULL ASSIGNMENT",
                        };
                        int eventLogId = Posts.SaveEvent(sub);

                        if (eventLogId == -1)
                        {
                            throw new Exception("Failed to log submit event to the eventlog table -- Posts.SaveEvent returned -1 -- Assignment is null");
                        }
                    }
                }
                catch (Exception e)
                {
                    throw new Exception("Failed to log submit event to the eventlog table: ", e);
                }

                if (assignment != null && (assignment.HasDeliverables == true ||
                                           assignment.Type == AssignmentTypes.CriticalReview ||
                                           assignment.Type == AssignmentTypes.AnchoredDiscussion))
                {
                    List <Deliverable> deliverables;
                    if (assignment.Type == AssignmentTypes.CriticalReview)
                    {
                        deliverables = new List <Deliverable>((assignment.PreceedingAssignment).Deliverables);
                    }
                    else if (assignment.Type == AssignmentTypes.AnchoredDiscussion)
                    {
                        //TODO: need to keep deliverables if no changes have been made.
                        //need to remove old deliverables
                        assignment.Deliverables.Clear();
                        db.SaveChanges();
                        deliverables = new List <Deliverable>((assignment).Deliverables);
                        List <string> deliverableNames = new List <string>();

                        foreach (var file in files)
                        {
                            deliverables.Add(new Deliverable
                            {
                                Assignment      = assignment,
                                AssignmentID    = assignment.ID,
                                DeliverableType = DeliverableType.PDF,
                                Name            = Path.GetFileNameWithoutExtension(file.FileName),
                            });
                        }

                        foreach (Deliverable d in deliverables)
                        {
                            assignment.Deliverables.Add(d);
                        }
                        db.Entry(assignment).State = System.Data.EntityState.Modified;
                        db.SaveChanges();
                    }
                    else
                    {
                        deliverables = new List <Deliverable>((assignment).Deliverables);
                    }


                    if (assignment.CourseID == ActiveCourseUser.AbstractCourseID && (ActiveCourseUser.AbstractRole.CanSubmit == true || ActiveCourseUser.AbstractRole.CanUploadFiles == true))
                    {
                        AssignmentTeam assignmentTeam = GetAssignmentTeam(assignment, ActiveCourseUser);

                        int i = 0;

                        //the files variable is null when submitting an in-browser text submission
                        if (files != null)
                        {
                            int anchoredDiscussionDocumentCount = 0;
                            foreach (var file in files)
                            {
                                anchoredDiscussionDocumentCount++;
                                if (file != null && file.ContentLength > 0)
                                {
                                    DeliverableType type = (DeliverableType)deliverables[i].Type;

                                    //jump over all DeliverableType.InBrowserText as they will be handled separately
                                    while (type == DeliverableType.InBrowserText)
                                    {
                                        i++;
                                        type = (DeliverableType)deliverables[i].Type;
                                    }
                                    string fileName        = Path.GetFileName(file.FileName);
                                    string extension       = Path.GetExtension(file.FileName).ToLower();
                                    string deliverableName = string.Format("{0}{1}", deliverables[i].Name, extension);

                                    string[] allowFileExtensions = GetFileExtensions(type);

                                    if (allowFileExtensions.Contains(extension))
                                    {
                                        if (assignment.Type == AssignmentTypes.CriticalReview || assignment.Type == AssignmentTypes.AnchoredDiscussion)
                                        {
                                            //TODO: clean this up
                                            AssignmentTeam authorTeam = new AssignmentTeam();
                                            ReviewTeam     reviewTeam = new ReviewTeam();

                                            if (assignment.Type == AssignmentTypes.AnchoredDiscussion)
                                            {
                                                authorTeam = new AssignmentTeam
                                                {
                                                    Assignment   = assignment,
                                                    AssignmentID = assignment.ID,
                                                    Team         = null,
                                                    TeamID       = anchoredDiscussionDocumentCount,
                                                };

                                                reviewTeam = new ReviewTeam
                                                {
                                                    Assignment   = assignment,
                                                    AssignmentID = assignment.ID,
                                                    //AuthorTeam = null,
                                                    AuthorTeamID = anchoredDiscussionDocumentCount,
                                                    //ReviewingTeam = null,
                                                    ReviewTeamID = ActiveCourseUser.AbstractCourse.ID,
                                                };
                                                assignment.ReviewTeams.Add(reviewTeam);
                                                //db.Entry(assignment).State = System.Data.EntityState.Modified;
                                                db.SaveChanges();
                                            }
                                            else
                                            {
                                                authorTeam = (from at in db.AssignmentTeams
                                                              where at.TeamID == authorTeamID &&
                                                              at.AssignmentID == assignment.PrecededingAssignmentID
                                                              select at).FirstOrDefault();

                                                reviewTeam = (from tm in db.TeamMembers
                                                              join t in db.Teams on tm.TeamID equals t.ID
                                                              join rt in db.ReviewTeams on t.ID equals rt.ReviewTeamID
                                                              where tm.CourseUserID == ActiveCourseUser.ID &&
                                                              rt.AssignmentID == assignment.ID
                                                              select rt).FirstOrDefault();
                                            }

                                            //MG&MK: file system for critical review assignments is laid out a bit differently, so
                                            //critical review assignments must use different file system functions

                                            //remove all prior files
                                            OSBLE.Models.FileSystem.AssignmentFilePath fs =
                                                Models.FileSystem.Directories.GetAssignment(
                                                    ActiveCourseUser.AbstractCourseID, assignment.ID);
                                            fs.Review(authorTeam.TeamID, reviewTeam.ReviewTeamID)
                                            .File(deliverableName)
                                            .Delete();

                                            if (assignment.Type != AssignmentTypes.AnchoredDiscussion) // handle assignments that are not anchored discussion
                                            {
                                                //We need to remove the zipfile corresponding to the authorTeamId being sent in as well as the regularly cached zip.
                                                AssignmentTeam precedingAuthorAssignmentTeam = (from at in assignment.PreceedingAssignment.AssignmentTeams
                                                                                                where at.TeamID == authorTeamID
                                                                                                select at).FirstOrDefault();
                                                FileSystem.RemoveZipFile(ActiveCourseUser.AbstractCourse as Course, assignment, precedingAuthorAssignmentTeam);
                                                FileSystem.RemoveZipFile(ActiveCourseUser.AbstractCourse as Course, assignment, assignmentTeam);
                                            }
                                            else //anchored discussion type TODO: this does nothing right now, fix!
                                            {
                                                //We need to remove the zipfile corresponding to the authorTeamId being sent in as well as the regularly cached zip.
                                                AssignmentTeam precedingAuthorAssignmentTeam = (from at in assignment.AssignmentTeams
                                                                                                where at.TeamID == authorTeamID
                                                                                                select at).FirstOrDefault();
                                                FileSystem.RemoveZipFile(ActiveCourseUser.AbstractCourse as Course, assignment, precedingAuthorAssignmentTeam);
                                                FileSystem.RemoveZipFile(ActiveCourseUser.AbstractCourse as Course, assignment, assignmentTeam);
                                            }
                                            //add in the new file
                                            //authorTeamID is the deliverable file counter, and reviewTeamID is the courseID
                                            fs.Review(authorTeam.TeamID, reviewTeam.ReviewTeamID)
                                            .AddFile(deliverableName, file.InputStream);

                                            //unzip and rezip xps files because some XPS generators don't do it right
                                            if (extension.ToLower().CompareTo(".xps") == 0)
                                            {
                                                //XPS documents require the actual file path, so get that.
                                                OSBLE.Models.FileSystem.FileCollection fileCollection =
                                                    OSBLE.Models.FileSystem.Directories.GetAssignment(
                                                        ActiveCourseUser.AbstractCourseID, assignment.ID)
                                                    .Review(authorTeam.TeamID, reviewTeam.ReviewTeamID)
                                                    .File(deliverables[i].Name);
                                                string path = fileCollection.FirstOrDefault();

                                                string extractPath = Path.Combine(FileSystem.GetTeamUserSubmissionFolderForAuthorID(true, ActiveCourseUser.AbstractCourse as Course, (int)id, assignmentTeam, authorTeam.Team), "extract");
                                                using (ZipFile oldZip = ZipFile.Read(path))
                                                {
                                                    oldZip.ExtractAll(extractPath, ExtractExistingFileAction.OverwriteSilently);
                                                }
                                                using (ZipFile newZip = new ZipFile())
                                                {
                                                    newZip.AddDirectory(extractPath);
                                                    newZip.Save(path);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            //If a submission of any extension exists delete it.  This is needed because they could submit a .c file and then a .cs file and the teacher would not know which one is the real one.
                                            string submission = FileSystem.GetDeliverable(ActiveCourseUser.AbstractCourse as Course, assignment.ID, assignmentTeam, deliverables[i].Name, allowFileExtensions);
                                            if (submission != null)
                                            {
                                                FileInfo oldSubmission = new FileInfo(submission);

                                                if (oldSubmission.Exists)
                                                {
                                                    oldSubmission.Delete();
                                                }
                                            }
                                            FileSystem.RemoveZipFile(ActiveCourseUser.AbstractCourse as Course, assignment, assignmentTeam);
                                            string path = Path.Combine(FileSystem.GetTeamUserSubmissionFolder(true, ActiveCourseUser.AbstractCourse as Course, (int)id, assignmentTeam), deliverables[i].Name + extension);
                                            file.SaveAs(path);

                                            //unzip and rezip xps files because some XPS generators don't do it right
                                            if (extension.ToLower().CompareTo(".xps") == 0)
                                            {
                                                string extractPath = Path.Combine(FileSystem.GetTeamUserSubmissionFolder(true, ActiveCourseUser.AbstractCourse as Course, (int)id, assignmentTeam), "extract");
                                                using (ZipFile oldZip = ZipFile.Read(path))
                                                {
                                                    oldZip.ExtractAll(extractPath, ExtractExistingFileAction.OverwriteSilently);
                                                }
                                                using (ZipFile newZip = new ZipFile())
                                                {
                                                    newZip.AddDirectory(extractPath);
                                                    newZip.Save(path);
                                                }
                                            }
                                        }

                                        DateTime?dueDate = assignment.DueDate;
                                        if (dueDate != null)
                                        {   //TODO: add case for anchored discussion assignment
                                            (new NotificationController()).SendFilesSubmittedNotification(assignment, assignmentTeam, deliverables[i].Name);
                                        }
                                    }
                                    else
                                    {
                                        //The submission view handles incorrect extension types, so this area of code is unlikely to be reached. In the case that it does a occur,
                                        //we will ineloquently redirect them to assignment index without feedback.
                                        Cache["SubmissionReceived"] = false;
                                        return(RedirectToAction("Index", "Assignment"));
                                    }
                                }
                                i++;
                            }
                        }

                        // Creates the text files from text boxes
                        int    j = 0;
                        string delName;
                        do
                        {
                            if (Request != null)
                            {
                                //delName = Request.Params["desiredName[" + j + "]"];
                                delName = Request.Unvalidated.Form["desiredName[" + j + "]"];
                            }
                            else //TODO: change this to releveant string
                            {
                                delName = null;
                            }

                            if (delName != null)
                            {
                                string inbrowser;
                                if (Request != null)
                                {
                                    //inbrowser = Request.Params["inBrowserText[" + j + "]"];
                                    inbrowser = Request.Unvalidated.Form["inBrowserText[" + j + "]"];

                                    if (inbrowser.Length > 0)
                                    {
                                        var path = Path.Combine(FileSystem.GetTeamUserSubmissionFolder(true, ActiveCourseUser.AbstractCourse as Course, (int)id, assignmentTeam), CurrentUser.LastName + "_" + CurrentUser.FirstName + "_" + delName + ".txt");
                                        System.IO.File.WriteAllText(path, inbrowser);
                                    }
                                }
                            }
                            j++;
                        } while (delName != null);
                        Cache["SubmissionReceived"]             = true;
                        Cache["SubmissionReceivedAssignmentID"] = assignment.ID;
                        if (authorTeamID != null)
                        {
                            Cache["SubmissionForAuthorTeamID"] = authorTeamID;
                        }
                        if (assignment.Type == AssignmentTypes.AnchoredDiscussion)
                        {
                            return(RedirectToAction("Index", "AnchoredDiscussionController"));
                        }
                        else
                        {
                            return(Redirect(Request.UrlReferrer.ToString()));
                        }
                    }
                }
            }

            return(Create(id));
        }
Ejemplo n.º 3
0
 public override string ToString()
 {
     return(string.Format("{0}.{1}", Name, DeliverableType.ToString()));
 }
Ejemplo n.º 4
0
        public void ContractRequestsGrid_Tests()
        {
            #region Data
            Deliverable deliverable1 = new Deliverable()
            {
                Id                    = 1403256,
                CreatedBy             = 556,
                Name                  = "Test",
                ProducingDepartmentId = 1
            };
            Department dept1 = new Department()
            {
                Id   = 1,
                Code = "PROG"
            };
            DeliverableGroup deliverablegroup1 = new DeliverableGroup()
            {
                Id   = 1,
                Code = "CR"
            };
            DeliverableType deliverabletype1 = new DeliverableType()
            {
                Name = "CR",
                Id   = 1
            };
            DeliverableStatus status = new DeliverableStatus()
            {
                Name = "Draft",
                Id   = 1
            };
            DeliverableBudget delbudget = new DeliverableBudget()
            {
                CreatedBy = 556,
                EstimateCompleteAmount = 100,
                ActualAmount           = 500,
                Id             = 285,
                MasterVendorId = 61
            };
            MasterVendor msvendor = new MasterVendor()
            {
                Id        = 61,
                CreatedBy = 556
            };

            string SAPVendorName = "SAP Vendor Name";
            Vendor vendors       = new Vendor()
            {
                Id   = 61,
                Name = SAPVendorName
            };
            msvendor.Vendor        = vendors;
            delbudget.MasterVendor = msvendor;
            InvoiceLine invoice1 = new InvoiceLine()
            {
                Id     = 1,
                Amount = 100
            };
            List <InvoiceLine> invoicelist = new List <InvoiceLine>();
            invoicelist.Add(invoice1);
            delbudget.InvoiceLine = invoicelist;
            ActualsReconciliation arc = new ActualsReconciliation()
            {
                ActualAmount        = 100,
                CreatedBy           = 556,
                DeliverableBudgetId = 285
            };
            List <ActualsReconciliation> arclist = new List <ActualsReconciliation>();
            arclist.Add(arc);
            delbudget.ActualsReconciliation = arclist;
            List <DeliverableBudget> delbudgetlist = new List <DeliverableBudget>();
            delbudgetlist.Add(delbudget);
            DeliverableDate deldate = new DeliverableDate()
            {
                Id            = 1,
                DeliverableId = 1403256
            };
            DeliverableDateType deldatetype = new DeliverableDateType()
            {
                Id   = 1,
                Code = "DEL"
            };
            deldate.DeliverableDateType = deldatetype;
            List <DeliverableDate> deldatelist = new List <DeliverableDate>();
            deldatelist.Add(deldate);
            ContractRequest ctrreq = new ContractRequest()
            {
                ContractRequestProject = "CR",
                CreatedBy = 556
            };
            deliverable1.DeliverableGroup  = deliverablegroup1;
            deliverable1.Department        = dept1;
            deliverable1.DeliverableType   = deliverabletype1;
            deliverable1.DeliverableStatus = status;
            deliverable1.DeliverableDate   = deldatelist;
            deliverable1.ContractRequest   = ctrreq;
            deliverable1.DeliverableBudget = delbudgetlist;
            List <Deliverable> deliverablelist = new List <Deliverable>();
            deliverablelist.Add(deliverable1);
            List <CRDeliverableViewModel> viewModel = new List <CRDeliverableViewModel>();
            #endregion

            #region Mock
            mockdeliverablerepository.Setup(x => x.GetDeliverablesByDeliverableGroup(It.IsAny <int>())).Returns(deliverablelist);
            mockbudgetService.Setup(x => x.GetDeliverablesByDeliverableGroup(It.IsAny <int>())).Returns(deliverablelist);

            #endregion
            var budgetservice   = new BudgetServiceMock(_deliverableRepository: mockdeliverablerepository.Object);
            var contractservice = new ContractRequestControllerMock(budgetservice: mockbudgetService.Object);

            var results = budgetservice.GetDeliverablesByDeliverableGroup(MRM_USER_ID);
            viewModel = ContractRequestMapper.CRDeliverableMapper(deliverablelist);


            #region Assets
            Assert.IsNotNull(results);
            Assert.IsNotNull(viewModel);
            Assert.IsTrue(results.Count > 0);
            Assert.AreEqual(viewModel[0].Vendor, SAPVendorName);
            #endregion
        }