public void DeleteAllTagsFromQuestion(Question question) { List<Tag> tags = GetTagsForQuestion(question); foreach (Tag tag in tags) { DeleteTagFromQuestion(question, tag); } }
public void DeleteQuestion(Question question) { //Delete the answers foreach (var answer in question.Answers) { DeleteAnswer(answer); } dc.Questions.DeleteOnSubmit(question); DeleteAllTagsFromQuestion(question); }
public void AddTagToQuestion(Tag tag, Question question) { dc.sp_AddQuestionTag(tag.TagID, question.QuestionID); }
public bool QuestionHasTag(Question question, Tag tag) { QuestionTag qTag = dc.QuestionTags.SingleOrDefault(qt => qt.TagID == tag.TagID && qt.QuestionID == question.QuestionID); return (qTag != null); }
public List<Tag> GetTagsForQuestion(Question question) { var tags = from qTag in dc.QuestionTags join q in dc.Questions on qTag.QuestionID equals q.QuestionID join t in dc.Tags on qTag.TagID equals t.TagID where q.QuestionID == question.QuestionID select t; return tags.ToList(); }
public void DeleteTagFromQuestion(Question question, Tag tag) { dc.sp_DeleteTagFromQuestion(tag.TagID, question.QuestionID); }
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; } }