Пример #1
0
        public void SaveDataEntryQuestion(Qrator.Common.Question Q, string psCode)
        {
            using (QBankEntities context = this.CreateConnection())
            {
                PracticeTest ps = context.PracticeTests.Where(p => p.MasterCode == psCode && p.Status == 1).FirstOrDefault();

                if (Q.MasterCode == null)
                {
                    try
                    {
                        ServerData.Entities.Question qtn = new ServerData.Entities.Question();
                        qtn.Complexity     = Q.Complexity;
                        qtn.Explanation    = Q.AnswerExplanation;
                        qtn.HeaderText     = Q.Instruction;
                        qtn.QuestionText   = Q.QuestionText;
                        qtn.TopicID        = GetTopicID(ps.ExaminationID, Q.Subject, Q.Topic);
                        qtn.MasterCode     = Guid.NewGuid().ToString();
                        qtn.Status         = 1;
                        qtn.ModifiedDate   = DateTime.Now;
                        qtn.PracticeTestID = ps.ID;

                        foreach (var x in Q.OptionTexts)
                        {
                            ServerData.Entities.Answer a = new ServerData.Entities.Answer();
                            a.OptionText   = x.OptionText;
                            a.IsCorrect    = x.IsCorrect;
                            a.MasterCode   = Guid.NewGuid().ToString();
                            a.ModifiedDate = DateTime.Now;
                            a.Status       = 1;
                            qtn.Answers.Add(a);
                        }

                        context.Questions.Add(qtn);
                        context.SaveChanges();

                        //Sync back
                        Q.MasterCode = qtn.MasterCode;
                        Q.Status     = qtn.Status;
                        foreach (var x in Q.OptionTexts)
                        {
                            ServerData.Entities.Answer a = qtn.Answers.Where(y => y.OptionText == x.OptionText).FirstOrDefault();
                            x.MasterCode = a.MasterCode;
                            x.Status     = a.Status;
                        }
                    }
                    catch (DbEntityValidationException ex)
                    {
                        foreach (var validationErrors in ex.EntityValidationErrors)
                        {
                            foreach (var validationError in validationErrors.ValidationErrors)
                            {
                                Trace.TraceInformation("Property: {0} Error: {1}",
                                                       validationError.PropertyName,
                                                       validationError.ErrorMessage);
                            }
                        }
                        Q.MasterCode = string.Empty;
                        Q.Status     = 0;
                        foreach (var item in Q.OptionTexts)
                        {
                            item.MasterCode = string.Empty;
                            item.Status     = 0;
                        }
                    }
                }
                else
                {
                    if (Q.IsDeleted)
                    {
                        Qrator.ServerData.Entities.Question qt = context.Questions.Where(x => x.MasterCode == Q.MasterCode && x.Status == 1).FirstOrDefault();
                        context.Questions.Remove(qt);
                        context.SaveChanges();
                    }
                    else
                    {
                        int i = 0;
                        Qrator.ServerData.Entities.Question qt = context.Questions.Where(x => x.MasterCode == Q.MasterCode && x.Status == 1).FirstOrDefault();
                        if (qt.Complexity != Q.Complexity)
                        {
                            qt.Complexity = Q.Complexity;
                            i++;
                        }
                        if (qt.Explanation != Q.AnswerExplanation)
                        {
                            qt.Explanation = Q.AnswerExplanation;
                            i++;
                        }
                        if (qt.HeaderText != Q.Instruction)
                        {
                            qt.HeaderText = Q.Instruction;
                            i++;
                        }
                        if (qt.QuestionText != Q.QuestionText)
                        {
                            qt.QuestionText = Q.QuestionText;
                            i++;
                        }

                        if (qt.Topic.Name != Q.Topic)
                        {
                            qt.TopicID = GetTopicID(ps.ExaminationID, Q.Subject, Q.Topic);
                            i++;
                        }

                        bool answerChanged = false; //Use this variable to update last modified date of each answer option
                        //Update Answers
                        foreach (var item in Q.OptionTexts)
                        {
                            Qrator.ServerData.Entities.Answer a = qt.Answers.Where(x => x.MasterCode == item.MasterCode && item.Status == 1).FirstOrDefault();
                            if (a.IsCorrect != item.IsCorrect)
                            {
                                a.IsCorrect   = item.IsCorrect;
                                answerChanged = true;
                                i++;
                            }
                            if (a.OptionText != item.OptionText)
                            {
                                answerChanged = true;
                                a.OptionText  = item.OptionText;
                                i++;
                            }
                            if (answerChanged)
                            {
                                a.ModifiedDate = DateTime.Now;
                            }

                            answerChanged = false;
                        }
                        qt.ModifiedDate = DateTime.Now;

                        if (i > 0)
                        {
                            context.SaveChanges();
                        }
                    }
                }
            }
        }
Пример #2
0
        public List <Qrator.Common.Question> GetQuestionsForModeration(string examName, string subjectName, string topicName)
        {
            using (QBankEntities context = this.CreateConnection())
            {
                IList <QuestionModel> questions = null;

                if (topicName == null || topicName == "")
                {
                    questions = (from q in context.Questions.Include("Answer")
                                 join t in context.Topics on q.TopicID equals t.ID
                                 join s in context.Subjects on t.SubjectID equals s.ID
                                 join e in context.Examinations on s.ExaminationID equals e.ID
                                 where e.Name == examName && s.Name == subjectName
                                 select new QuestionModel
                    {
                        Answers = q.Answers,
                        Complexity = q.Complexity,
                        Explanation = q.Explanation,
                        HeaderText = q.HeaderText,
                        ID = q.ID,
                        MasterCode = q.MasterCode,
                        ModifiedDate = q.ModifiedDate,
                        QuestionText = q.QuestionText,
                        Status = q.Status,
                        SubjectName = s.Name,
                        TopicName = t.Name
                    }).ToList();
                }
                else
                {
                    questions = (from q in context.Questions.Include("Answer")
                                 join t in context.Topics on q.TopicID equals t.ID
                                 join s in context.Subjects on t.SubjectID equals s.ID
                                 join e in context.Examinations on s.ExaminationID equals e.ID
                                 where e.Name == examName && s.Name == subjectName && t.Name == topicName
                                 select new QuestionModel
                    {
                        Answers = q.Answers,
                        Complexity = q.Complexity,
                        Explanation = q.Explanation,
                        HeaderText = q.HeaderText,
                        ID = q.ID,
                        MasterCode = q.MasterCode,
                        ModifiedDate = q.ModifiedDate,
                        QuestionText = q.QuestionText,
                        Status = q.Status,
                        SubjectName = s.Name,
                        TopicName = t.Name
                    }).ToList();
                }

                List <Qrator.Common.Question> questionList = new List <Qrator.Common.Question>();
                //Translate format
                foreach (var q in questions)
                {
                    Qrator.Common.Question qm = new Qrator.Common.Question();

                    qm.AnswerExplanation = q.Explanation;
                    qm.Subject           = q.SubjectName;
                    qm.Topic             = q.TopicName;
                    qm.Complexity        = q.Complexity;
                    qm.LastModifiedDate  = q.ModifiedDate;
                    qm.ID           = q.ID.ToString();
                    qm.Status       = q.Status;
                    qm.Instruction  = q.HeaderText;
                    qm.Status       = q.Status;
                    qm.MasterCode   = q.MasterCode;
                    qm.QuestionText = q.QuestionText;
                    foreach (Qrator.ServerData.Entities.Answer a in q.Answers)
                    {
                        Qrator.Common.Answer am = new Qrator.Common.Answer();
                        am.IsCorrect  = a.IsCorrect;
                        am.MasterCode = a.MasterCode;
                        am.Status     = a.Status;
                        am.OptionText = a.OptionText;
                        qm.OptionTexts.Add(am);
                    }
                    questionList.Add(qm);
                }
                return(questionList);
            }
        }
Пример #3
0
        public void SaveModeratedQuestion(Qrator.Common.Question Q, string psCode)
        {
            using (QBankEntities context = this.CreateConnection())
            {
                if (Q.IsDeleted)
                {
                    //Delete only the one which has been moderated - those brought by moderator and deleted before sync (status = 1) will be left in the qbf file.
                    Qrator.ServerData.Entities.Question qt = context.Questions.Where(x => x.MasterCode == Q.MasterCode && x.Status == 2).FirstOrDefault();
                    context.Questions.Remove(qt);
                    context.SaveChanges();
                    return;
                }

                PracticeTest ps = context.PracticeTests.Where(p => p.MasterCode == psCode && p.Status == 2).FirstOrDefault();

                if (Q.Status == 1)
                {
                    //Save new record
                    try
                    {
                        ServerData.Entities.Question qtn = new ServerData.Entities.Question();
                        qtn.Complexity     = Q.Complexity;
                        qtn.Explanation    = Q.AnswerExplanation;
                        qtn.HeaderText     = Q.Instruction;
                        qtn.QuestionText   = Q.QuestionText;
                        qtn.TopicID        = GetTopicID(ps.ExaminationID, Q.Subject, Q.Topic);
                        qtn.Status         = 2;
                        qtn.MasterCode     = Q.MasterCode;
                        qtn.ModifiedDate   = DateTime.Now;
                        qtn.PracticeTestID = ps.ID;

                        foreach (var x in Q.OptionTexts)
                        {
                            ServerData.Entities.Answer a = new ServerData.Entities.Answer();
                            a.OptionText   = x.OptionText;
                            a.IsCorrect    = x.IsCorrect;
                            a.MasterCode   = x.MasterCode;
                            a.ModifiedDate = DateTime.Now;
                            a.Status       = 2;
                            qtn.Answers.Add(a);
                        }

                        context.Questions.Add(qtn);
                        context.SaveChanges();

                        //Sync back
                        Q.Status = qtn.Status;
                        foreach (var x in Q.OptionTexts)
                        {
                            x.Status = 2;
                        }
                    }
                    catch (DbEntityValidationException ex)
                    {
                        foreach (var validationErrors in ex.EntityValidationErrors)
                        {
                            foreach (var validationError in validationErrors.ValidationErrors)
                            {
                                Trace.TraceInformation("Property: {0} Error: {1}",
                                                       validationError.PropertyName,
                                                       validationError.ErrorMessage);
                            }
                        }
                        Q.Status = 1;
                        foreach (var x in Q.OptionTexts)
                        {
                            x.Status = 1;
                        }
                    }
                }
                else
                {
                    //Update record
                    int i = 0;
                    Qrator.ServerData.Entities.Question qt = context.Questions.Where(x => x.MasterCode == Q.MasterCode && x.Status == 2).FirstOrDefault();
                    if (qt.Complexity != Q.Complexity)
                    {
                        qt.Complexity = Q.Complexity;
                        i++;
                    }
                    if (qt.Explanation != Q.AnswerExplanation)
                    {
                        qt.Explanation = Q.AnswerExplanation;
                        i++;
                    }
                    if (qt.HeaderText != Q.Instruction)
                    {
                        qt.HeaderText = Q.Instruction;
                        i++;
                    }
                    if (qt.QuestionText != Q.QuestionText)
                    {
                        qt.QuestionText = Q.QuestionText;
                        i++;
                    }

                    if (qt.Topic.Name != Q.Topic)
                    {
                        qt.TopicID = GetTopicID(ps.ExaminationID, Q.Subject, Q.Topic);
                        i++;
                    }

                    bool answerChanged = false; //Use this variable to update last modified date of each answer option
                    //Update Answers
                    foreach (var item in Q.OptionTexts)
                    {
                        Qrator.ServerData.Entities.Answer a = qt.Answers.Where(x => x.MasterCode == item.MasterCode && x.Status == 2).FirstOrDefault();
                        if (a.IsCorrect != item.IsCorrect)
                        {
                            a.IsCorrect   = item.IsCorrect;
                            answerChanged = true;
                            i++;
                        }
                        if (a.OptionText != item.OptionText)
                        {
                            answerChanged = true;
                            a.OptionText  = item.OptionText;
                            i++;
                        }
                        if (answerChanged)
                        {
                            a.ModifiedDate = DateTime.Now;
                        }

                        answerChanged = false;
                    }
                    qt.ModifiedDate = DateTime.Now;

                    if (i > 0)
                    {
                        context.SaveChanges();
                    }
                }
            }
        }