//Jaimeiris - This adds the submission when it is added by an administrator
        public Submission addSubmissionByAdmin(usersubmission usersubTA, submission submissionToAdd, panel pannelToAdd, workshop workshopToAdd)
        {
            try
            {
                using (conferenceadminContext context = new conferenceadminContext())
                {
                    submission sub = new submission();
                    //for all types of submissions
                    //table submission
                    sub.topicID = submissionToAdd.topicID;
                    sub.submissionTypeID = submissionToAdd.submissionTypeID;
                    sub.submissionAbstract = submissionToAdd.submissionAbstract;
                    sub.title = submissionToAdd.title;
                    sub.status = "Pending";
                    sub.creationDate = DateTime.Now;
                    sub.deleted = false;
                    sub.byAdmin = true;
                    context.submissions.Add(sub);
                    context.SaveChanges();
                    //table usersubmission
                    long submissionID = sub.submissionID;
                    usersubmission usersub = new usersubmission();
                    usersub.userID = usersubTA.userID;
                    usersub.initialSubmissionID = submissionID;
                    usersub.allowFinalVersion = false;
                    usersub.deleted = false;
                    usersub.finalSubmissionID = null;
                    context.usersubmission.Add(usersub);
                    context.SaveChanges();
                    //automatically apply user
                    user user = context.users.Where(c => c.userID == usersubTA.userID).FirstOrDefault();
                    user.hasApplied = true;
                    context.SaveChanges();

                    /*/table documents submitted
                    if (submissionToAdd.submissionTypeID != 4)
                    {
                        documentssubmitted subDocs = new documentssubmitted();

                        foreach (var doc in submissionToAdd.documentssubmitteds)
                        {
                            subDocs.submissionID = submissionID;
                            subDocs.documentName = doc.documentName;
                            subDocs.document = doc.document;
                            subDocs.deleted = false;
                            context.documentssubmitteds.Add(subDocs);
                            context.SaveChanges();
                        }
                    }*/

                    //table pannels
                    if (submissionToAdd.submissionTypeID == 3 && pannelToAdd != null)
                    {
                        panel subPanel = new panel();
                        subPanel.submissionID = submissionID;
                        subPanel.panelistNames = pannelToAdd.panelistNames;
                        subPanel.plan = pannelToAdd.plan;
                        subPanel.guideQuestion = pannelToAdd.guideQuestion;
                        subPanel.formatDescription = pannelToAdd.formatDescription;
                        subPanel.necessaryEquipment = pannelToAdd.necessaryEquipment;
                        subPanel.deleted = false;
                        context.panels.Add(subPanel);
                        context.SaveChanges();
                    }
                    //table workshop
                    if (submissionToAdd.submissionTypeID == 5 && workshopToAdd != null)
                    {
                        workshop subWorkshop = new workshop();
                        subWorkshop.submissionID = submissionID;
                        subWorkshop.duration = workshopToAdd.duration;
                        subWorkshop.delivery = workshopToAdd.delivery;
                        subWorkshop.plan = workshopToAdd.plan;
                        subWorkshop.necessary_equipment = workshopToAdd.necessary_equipment;
                        subWorkshop.deleted = false;
                        context.workshops.Add(subWorkshop);
                        context.SaveChanges();
                    }

                    Submission addedSub = new Submission
                    {
                        submissionID = submissionID,
                        submissionTypeName = getSubmissionTypeName(sub.submissionTypeID),
                        submissionTypeID = sub.submissionTypeID,
                        submissionTitle = sub.title,
                        topiccategoryID = sub.topicID,
                        status = sub.status,
                        isEvaluated = false,
                        isFinalSubmission = false
                    };
                    return addedSub;
                }
            }
            catch (Exception ex)
            {
                Console.Write("SubmissionManager.addSubmission error " + ex);
                return null;
            }
        }
        //Jaimeiris - adds a final submission when the admins adds it
        public Submission postAdminFinalSubmission(usersubmission usersubTA, submission submissionToAdd, documentssubmitted submissionDocuments, panel pannelToAdd, workshop workshopToAdd)
        {
            try
            {
                using (conferenceadminContext context = new conferenceadminContext())
                {
                    submission sub = new submission();
                    //for all types of submissions
                    //table submission
                    sub.topicID = submissionToAdd.topicID;
                    sub.submissionTypeID = submissionToAdd.submissionTypeID;
                    sub.submissionAbstract = submissionToAdd.submissionAbstract;
                    sub.title = submissionToAdd.title;
                    sub.status = "Pending";
                    sub.creationDate = DateTime.Now;
                    sub.deleted = false;
                    sub.byAdmin = true;
                    context.submissions.Add(sub);
                    context.SaveChanges();
                    //table usersubmission
                    long finalSubmissionID = sub.submissionID;
                    usersubmission usersub = context.usersubmission.Where(c => c.initialSubmissionID == usersubTA.initialSubmissionID && c.deleted == false).FirstOrDefault();
                    usersub.finalSubmissionID = finalSubmissionID;
                    context.SaveChanges();

                    //table documents submitted
                    if (submissionToAdd.submissionTypeID != 4)
                    {
                        documentssubmitted subDocs = new documentssubmitted();

                        List<documentssubmitted> prevDocuments = context.documentssubmitteds.Where(d => d.submissionID == usersubTA.initialSubmissionID && d.deleted != true).Select(d => d).ToList();

                        foreach (var doc in prevDocuments)
                        {

                            int count = submissionToAdd.documentssubmitteds.Where(d => d.documentssubmittedID == doc.documentssubmittedID).Count();
                            if (count != 0)
                            {
                                var newDoc = new documentssubmitted();
                                newDoc.submissionID = finalSubmissionID;
                                newDoc.documentName = doc.documentName;
                                newDoc.document = doc.document;
                                newDoc.deleted = false;
                                context.documentssubmitteds.Add(newDoc);
                                context.SaveChanges();
                            }

                        }
                    }

                    //table pannels
                    if (submissionToAdd.submissionTypeID == 3 && pannelToAdd != null)
                    {
                        panel subPanel = new panel();
                        subPanel.submissionID = finalSubmissionID;
                        subPanel.panelistNames = pannelToAdd.panelistNames;
                        subPanel.plan = pannelToAdd.plan;
                        subPanel.guideQuestion = pannelToAdd.guideQuestion;
                        subPanel.formatDescription = pannelToAdd.formatDescription;
                        subPanel.necessaryEquipment = pannelToAdd.necessaryEquipment;
                        subPanel.deleted = false;
                        context.panels.Add(subPanel);
                        context.SaveChanges();
                    }
                    //table workshop
                    if (submissionToAdd.submissionTypeID == 5 && workshopToAdd != null)
                    {
                        workshop subWorkshop = new workshop();
                        subWorkshop.submissionID = finalSubmissionID;
                        subWorkshop.duration = workshopToAdd.duration;
                        subWorkshop.delivery = workshopToAdd.delivery;
                        subWorkshop.plan = workshopToAdd.plan;
                        subWorkshop.necessary_equipment = workshopToAdd.necessary_equipment;
                        subWorkshop.deleted = false;
                        context.workshops.Add(subWorkshop);
                        context.SaveChanges();
                    }

                    //Delete connection between previous submissions and evaluators that have not evaluated them yet,
                    //since these will not be taken into consideration for the avg score of the final submission
                    List<evaluatiorsubmission> TBD = context.evaluatiorsubmissions.Where(c => c.submissionID == usersubTA.initialSubmissionID && c.statusEvaluation != "Evaluated" && c.deleted == false).ToList();
                    foreach (var assignment in TBD)
                    {
                        assignment.deleted = true;
                    }
                    context.SaveChanges();

                    Submission addedSub = new Submission
                    {
                        submissionID = finalSubmissionID,
                        submissionTypeName = getSubmissionTypeName(sub.submissionTypeID),
                        submissionTypeID = sub.submissionTypeID,
                        submissionTitle = sub.title,
                        topiccategoryID = sub.topicID,
                        status = sub.status,
                        isEvaluated = false,
                        isFinalSubmission = true
                    };
                    return addedSub;
                }
            }
            catch (Exception ex)
            {
                Console.Write("SubmissionManager.addSubmission error " + ex);
                return null;
            }
        }
        //Jaimeiris - adds a final submission to a existing submission, this is when the submitter os not an administrator
        //Also removes the evaluator submission assignment for the relations for which the evaluations have not been added and sends an email to the evaluators informing them that they no longer have to evaluate said submission
        public Submission addFinalSubmission(usersubmission usersubTA, submission submissionToAdd, documentssubmitted submissionDocuments, panel pannelToAdd, workshop workshopToAdd)
        {
            try
            {
                String email = "";
                using (conferenceadminContext context = new conferenceadminContext())
                {
                    submission sub = new submission();
                    //for all types of submissions
                    //table submission
                    sub.topicID = submissionToAdd.topicID;
                    sub.submissionTypeID = submissionToAdd.submissionTypeID;
                    sub.submissionAbstract = submissionToAdd.submissionAbstract;
                    sub.title = submissionToAdd.title;
                    sub.status = "Pending";
                    sub.creationDate = DateTime.Now;
                    sub.deleted = false;
                    sub.byAdmin = false;
                    context.submissions.Add(sub);
                    context.SaveChanges();
                    //table usersubmission
                    long finalSubmissionID = sub.submissionID;
                    usersubmission usersub = context.usersubmission.Where(c => c.initialSubmissionID == usersubTA.initialSubmissionID && c.deleted == false).FirstOrDefault();
                    usersub.finalSubmissionID = finalSubmissionID;
                    context.SaveChanges();

                    //table documents submitted
                    if (submissionToAdd.submissionTypeID != 4)
                    {
                        documentssubmitted subDocs = new documentssubmitted();

                        List<documentssubmitted> prevDocuments = context.documentssubmitteds.Where(d => d.submissionID == usersubTA.initialSubmissionID && d.deleted != true).Select(d => d).ToList();

                        foreach (var doc in prevDocuments)
                        {

                            int count = submissionToAdd.documentssubmitteds.Where(d => d.documentssubmittedID == doc.documentssubmittedID).Count();
                            if (count != 0)
                            {
                                var newDoc = new documentssubmitted();
                                newDoc.submissionID = finalSubmissionID;
                                newDoc.documentName = doc.documentName;
                                newDoc.document = doc.document;
                                newDoc.deleted = false;
                                context.documentssubmitteds.Add(newDoc);
                                context.SaveChanges();
                            }

                        }
                    }

                    //table pannels
                    if (submissionToAdd.submissionTypeID == 3 && pannelToAdd != null)
                    {
                        panel subPanel = new panel();
                        subPanel.submissionID = finalSubmissionID;
                        subPanel.panelistNames = pannelToAdd.panelistNames;
                        subPanel.plan = pannelToAdd.plan;
                        subPanel.guideQuestion = pannelToAdd.guideQuestion;
                        subPanel.formatDescription = pannelToAdd.formatDescription;
                        subPanel.necessaryEquipment = pannelToAdd.necessaryEquipment;
                        subPanel.deleted = false;
                        context.panels.Add(subPanel);
                        context.SaveChanges();
                    }
                    //table workshop
                    if (submissionToAdd.submissionTypeID == 5 && workshopToAdd != null)
                    {
                        workshop subWorkshop = new workshop();
                        subWorkshop.submissionID = finalSubmissionID;
                        subWorkshop.duration = workshopToAdd.duration;
                        subWorkshop.delivery = workshopToAdd.delivery;
                        subWorkshop.plan = workshopToAdd.plan;
                        subWorkshop.necessary_equipment = workshopToAdd.necessary_equipment;
                        subWorkshop.deleted = false;
                        context.workshops.Add(subWorkshop);
                        context.SaveChanges();
                    }

                    //Delete connection between previous submissions and evaluators that have not evaluated them yet,
                    //since these will not be taken into consideration for the avg score of the final submission
                    List<evaluatiorsubmission> TBD = context.evaluatiorsubmissions.Where(c => c.submissionID == usersubTA.initialSubmissionID && c.statusEvaluation != "Evaluated" && c.deleted == false).ToList();
                    foreach (var assignment in TBD)
                    {
                        //quitando el assignment de submission y evaluator del intial submission a los q aun no han evaluado, se les envia un email al evaluador de que no esta asignado.
                        assignment.deleted = true;

                        try
                        {
                            email = assignment.evaluator.user.membership.email;//evaluator email
                            String subject = "Caribbean Celebration of Women in Computing Assignment Deletion";
                            String messageBody = "Greetings, \n\n " +
                                "The request to evaluate the submission with title " + assignment.submission.title + " has been removed. It is no longer required for you to evaluate said submission.  To view all of your assigned submission please login to the system through the following link: \n\n" +
                                "http://136.145.116.238/#/Login/Log" + ".";
                            sendAssignmentEmail(email, subject, messageBody); //inform evaluator of deleted assignment via email
                        }

                        catch (Exception ex)
                        {
                            Console.Write("SubmissionManager.addEvaluation error " + ex);
                            return null;
                        }

                    }
                    context.SaveChanges();

                    Submission addedSub = new Submission
                    {
                        submissionID = finalSubmissionID,
                        submissionTypeName = getSubmissionTypeName(sub.submissionTypeID),
                        submissionTypeID = sub.submissionTypeID,
                        submissionTitle = sub.title,
                        topiccategoryID = sub.topicID,
                        status = sub.status,
                        isEvaluated = false,
                        isFinalSubmission = true
                    };
                    return addedSub;
                }
            }
            catch (Exception ex)
            {
                Console.Write("SubmissionManager.addSubmission error " + ex);
                return null;
            }
        }
        //Jaimeiris - edits a submission
        public Submission editSubmission(submission submissionToEdit, panel pannelToEdit, workshop workshopToEdit)
        {
            try
            {
                using (conferenceadminContext context = new conferenceadminContext())
                {

                    submission sub = context.submissions.Where(c => c.submissionID == submissionToEdit.submissionID).FirstOrDefault();
                    //for all types of submissions
                    //table submission
                    sub.topicID = submissionToEdit.topicID;
                    sub.submissionAbstract = submissionToEdit.submissionAbstract;
                    sub.title = submissionToEdit.title;
                    context.SaveChanges();

                    //table documents submitted
                    if (submissionToEdit.submissionTypeID != 4)
                    {
                        documentssubmitted subDocs = new documentssubmitted();

                        List<documentssubmitted> prevDocuments = context.documentssubmitteds.Where(d => d.submissionID == submissionToEdit.submissionID && d.deleted != true).Select(d => d).ToList();

                        foreach (var doc in prevDocuments)
                        {

                            int count = submissionToEdit.documentssubmitteds.Where(d => d.documentssubmittedID == doc.documentssubmittedID).Count();
                            if (count == 0)
                            {
                                doc.deleted = true;
                                context.SaveChanges();
                            }

                        }
                    }

                    //table pannels
                    if (sub.submissionTypeID == 3 && pannelToEdit != null)
                    {
                        panel subPanel = context.panels.Where(c => c.submissionID == sub.submissionID).FirstOrDefault();
                        subPanel.panelistNames = pannelToEdit.panelistNames;
                        subPanel.plan = pannelToEdit.plan;
                        subPanel.guideQuestion = pannelToEdit.guideQuestion;
                        subPanel.formatDescription = pannelToEdit.formatDescription;
                        subPanel.necessaryEquipment = pannelToEdit.necessaryEquipment;
                        context.SaveChanges();
                    }
                    //table workshop
                    if (sub.submissionTypeID == 5 && workshopToEdit != null)
                    {
                        workshop subWorkshop = context.workshops.Where(c => c.submissionID == sub.submissionID).FirstOrDefault();
                        subWorkshop.duration = workshopToEdit.duration;
                        subWorkshop.delivery = workshopToEdit.delivery;
                        subWorkshop.plan = workshopToEdit.plan;
                        subWorkshop.necessary_equipment = workshopToEdit.necessary_equipment;
                        context.SaveChanges();
                    }
                    Submission editedSub = new Submission
                    {
                        submissionID = sub.submissionID,
                        submissionTypeName = getSubmissionTypeName(sub.submissionTypeID),
                        submissionTypeID = sub.submissionTypeID,
                        submissionTitle = sub.title,
                        topiccategoryID = sub.topicID
                    };

                    return editedSub;
                }
            }
            catch (Exception ex)
            {
                Console.Write("SubmissionManager.addEvaluation error " + ex);
                return null;
            }
        }