public void DeleteAnswer(Answer answer)
 {
     dc.Answers.DeleteOnSubmit(answer);
     dc.AnswerKeys.DeleteAllOnSubmit(answer.AnswerKeys);
     dc.Responses.DeleteAllOnSubmit(answer.Responses);
     DeleteAllTagsFromAnswer(answer);
 }
Beispiel #2
0
 public void DeleteAllTagsFromAnswer(Answer answer)
 {
     List<Tag> tags = GetTagsForAnswer(answer);
     foreach (Tag tag in tags)
     {
         DeleteTagFromAnswer(answer, tag);
     }
 }
Beispiel #3
0
 public void DeleteTagFromAnswer(Answer answer, Tag tag)
 {
     dc.sp_DeleteTagFromAnswer(tag.TagID, answer.AnswerID);
 }
Beispiel #4
0
 public bool AnswerHasTag(Answer answer, Tag tag)
 {
     AnswerTag aTag = dc.AnswerTags.SingleOrDefault(at => at.TagID == tag.TagID && at.AnswerID == answer.AnswerID);
     return (aTag != null);
 }
Beispiel #5
0
 public List<Tag> GetTagsForAnswer(Answer answer)
 {
     var tags = from ansTag in dc.AnswerTags
                join a in dc.Answers on ansTag.AnswerID equals a.AnswerID
                join t in dc.Tags on ansTag.TagID equals t.TagID
                where a.AnswerID == answer.AnswerID
                select t;
     return tags.ToList();
 }
Beispiel #6
0
 public void AddTagToAnswer(Tag tag, Answer answer)
 {
     dc.sp_AddAnswerTag(tag.TagID, answer.AnswerID);
 }
        public void SaveAssessment(Assessment assessment, bool isNew)
        {
            try
            {
                using (TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue))
                {
                    if (isNew)
                    {
                        dc.Assessments.InsertOnSubmit(assessment);
                        dc.SubmitChanges();
                    }

                    XElement markup = XElement.Parse(assessment.Data);
                    //TODO: Remove this code and replace it with code to notify if related tag is missing from tag list
                    // after the ability to delete tags from an assessment is added to the UI
                    DeleteAllTagsFromAssessment(assessment);

                    XAttribute tags = markup.Attribute("tags");
                    if (tags != null && !string.IsNullOrEmpty(tags.Value))
                    {
                        string[] tagList = tags.Value.Split(new char[] { ',' });
                        foreach (string tagname in tagList)
                        {
                            string cleanName = tagname.Trim();
                            if (string.IsNullOrEmpty(cleanName))
                            {
                                continue;
                            }
                            Tag tag = GetTagByName(assessment.CourseTerm, cleanName);
                            if (tag == null)
                            {
                                throw new Exception("Tag not found.");
                                tag = new Tag()
                                {
                                    Profile = GetLoggedInProfile(),
                                    Name = cleanName,
                                    CourseTerm = assessment.CourseTerm
                                };
                                //TODO find a way to defer saving tags to db in order
                                //to link tags to questions
                                dc.Tags.InsertOnSubmit(tag);
                                dc.SubmitChanges();
                                //throw new Exception(string.Format("Invalid TagID in Assessments 'tags' attribute. ID = {{{0}}}", tagid));
                            }
                            if (!AssessmentHasTag(assessment, tag))
                            {
                                AddTagToAssessment(tag, assessment);
                            }
                        }
                    }
                    foreach (XElement questionNode in markup.Elements("question"))
                    {
                        Question question;
                        if (questionNode.Attribute("id") != null && isNew)
                        {
                            throw new InvalidOperationException("Do not use 'id' Attribute when creating new Assessments");
                        }
                        if (isNew || questionNode.Attribute("id") == null)
                        {
                            question = new Question();
                            assessment.Questions.Add(question);
                        }
                        else
                        {
                            question = dc.Questions.SingleOrDefault(q => q.QuestionID.ToString() == questionNode.Attribute("id").Value);
                        }
                        question.Weight = questionNode.Elements("answer").Sum(a => Convert.ToDouble(a.Attribute("weight").Value));
                        question.Data = questionNode.ToString();

                        if (isNew || questionNode.Attribute("id") == null)
                        {

                            dc.SubmitChanges();
                            questionNode.SetAttributeValue("id", question.QuestionID);
                        }
                        question.Data = questionNode.ToString();
                        //TODO: Remove this line
                        DeleteAllTagsFromQuestion(question);
                        tags = questionNode.Attribute("tags");
                        if (tags != null && !string.IsNullOrEmpty(tags.Value))
                        {
                            string[] tagList = tags.Value.Split(new char[] { ',' });
                            foreach (string tagname in tagList)
                            {
                                string cleanName = tagname.Trim();
                                if (string.IsNullOrEmpty(cleanName))
                                {
                                    continue;
                                }
                                Tag tag = GetTagByName(assessment.CourseTerm, cleanName);
                                if (tag == null)
                                {
                                    throw new Exception("Tag not found.");
                                    tag = new Tag()
                                    {
                                        Profile = GetLoggedInProfile(),
                                        Name = cleanName,
                                        CourseTerm = assessment.CourseTerm
                                    };
                                    //TODO find a way to defer saving tags to db in order
                                    //to link tags to questions
                                    dc.Tags.InsertOnSubmit(tag);
                                    dc.SubmitChanges();
                                    //throw new Exception(string.Format("Invalid TagID in Question 'tags' attribute. ID = {{{0}}}, Question ID = {{{1}}}", tagname, question.QuestionID));
                                }
                                if (!QuestionHasTag(question, tag))
                                {
                                    AddTagToQuestion(tag, question);
                                }
                            }
                        }

                        foreach (XElement answerNode in questionNode.Elements("answer"))
                        {
                            Answer answer;
                            if (answerNode.Attribute("id") != null && isNew)
                            {
                                throw new InvalidOperationException("Do not use 'id' Attribute when creating new Assessments");
                            }
                            if (isNew || answerNode.Attribute("id") == null)
                            {
                                answer = new Answer();
                            }
                            else
                            {
                                answer = dc.Answers.SingleOrDefault(a => a.AnswerID.ToString() == answerNode.Attribute("id").Value);
                            }

                            //answer.AnswerKey = answerNode.Attribute("key").Value;
                            dc.AnswerKeys.DeleteAllOnSubmit(answer.AnswerKeys.ToList());
                            dc.SubmitChanges();
                            XElement keysNode = answerNode.Element("AnswerKeys");
                            if (keysNode != null)
                            {
                                answer.AnswerKeyText = keysNode.ToString();
                                foreach (XElement key in keysNode.Elements("AnswerKey"))
                                {
                                    AnswerKey newKey = new AnswerKey();
                                    XAttribute keyWeight = key.Attribute("weight");
                                    // It is assumed that this required attribute will always
                                    // be present
                                    newKey.Weight = Convert.ToDouble(keyWeight.Value);
                                    newKey.Value = key.Value;
                                    answer.AnswerKeys.Add(newKey);
                                }
                            }

                            answer.Type = answerNode.Attribute("type").Value;
                            answer.Weight = Convert.ToDouble(answerNode.Attribute("weight").Value);
                            answer.Question = question;

                            XElement stdin = answerNode.Element("Stdin");
                            if (stdin != null)
                            {
                                answer.Stdin = stdin.Value;
                            }

                            XElement fstream = answerNode.Element("Fstream");
                            if (fstream != null)
                            {
                                answer.Fstream = fstream.Value;
                            }

                            if (isNew || answerNode.Attribute("id") == null)
                            {
                                answer.Assessment = assessment;
                                dc.Answers.InsertOnSubmit(answer);
                                dc.SubmitChanges();
                                answerNode.SetAttributeValue("id", answer.AnswerID);
                            }

                            //TODO: Remove this line
                            DeleteAllTagsFromAnswer(answer);
                            tags = answerNode.Attribute("tags");
                            if (tags != null && !string.IsNullOrEmpty(tags.Value))
                            {
                                string[] tagList = tags.Value.Split(new char[] { ',' });
                                foreach (string tagname in tagList)
                                {
                                    string cleanName = tagname.Trim();
                                    if (string.IsNullOrEmpty(cleanName))
                                    {
                                        continue;
                                    }
                                    Tag tag = GetTagByName(assessment.CourseTerm, cleanName);
                                    if (tag == null)
                                    {
                                        throw new Exception("Tag not found.");
                                        tag = new Tag()
                                        {
                                            Profile = GetLoggedInProfile(),
                                            Name = cleanName,
                                            CourseTerm = assessment.CourseTerm
                                        };
                                        //TODO find a way to defer saving tags to db in order
                                        //to link tags to questions
                                        dc.Tags.InsertOnSubmit(tag);
                                        dc.SubmitChanges();
                                        //throw new Exception(string.Format("Invalid TagID in Answer 'tags' attribute. ID = {{{0}}}, Answer ID = {{{1}}}", tagname, answer.AnswerID));
                                    }
                                    if (!AnswerHasTag(answer, tag))
                                    {
                                        AddTagToAnswer(tag, answer);
                                    }
                                }
                            }
                        }
                        question.Data = questionNode.ToString();
                        dc.SubmitChanges();
                    }
                    assessment.Data = markup.ToString();
                    //Ensure that we aren't deleting an answer and leaving dangling responses
                    foreach (Answer answer in assessment.Answers)
                    {
                        XElement answerNode = markup.XPathSelectElement(string.Format("//answer[@id='{0}']", answer.AnswerID));
                        if (answerNode == null)
                        {
                            DeleteAnswer(answer);
                        }
                    }
                    //Do the same for questions
                    foreach (Question question in assessment.Questions)
                    {
                        XElement questionNode = markup.XPathSelectElement(string.Format("//question[@id='{0}']", question.QuestionID));
                        if (questionNode == null)
                        {
                            DeleteQuestion(question);
                        }
                    }
                    dc.SubmitChanges();
                    transaction.Complete();
                }
            }
            catch
            {
                throw;
            }
        }