Exemplo n.º 1
0
        public IEnumerable <ViewQuestionExam> Filter(ViewQuestionExam fillterModel)
        {
            try
            {
                var result = (from e in context.Questions
                              join c in context.Categorys on e.Category.Id equals c.Id

                              select new
                {
                    Id = e.Id,
                    CategoryName = c.Name,
                    Content = e.Content,
                    Level = e.Level,
                    Suggestion = e.Suggestion,
                    Type = e.Type,
                    Media = e.Media,
                    Status = e.Status,
                    CreateBy = e.CreatedBy,
                    CreateDate = e.CreatedDate
                }).ToList();

                if (fillterModel.Level != null && !"".Equals(fillterModel.Level))
                {
                    result = result.Where(s => s.Level.Equals(fillterModel.Level)).ToList();
                }
                if (fillterModel.Type != null && !"".Equals(fillterModel.Type))
                {
                    result = result.Where(s => s.Type.Equals(fillterModel.Type)).ToList();
                }
                if (fillterModel.CreatedBy != null && !"".Equals(fillterModel.CreatedBy))
                {
                    result = result.Where(s => s.CreateBy.Equals(fillterModel.CreatedBy)).ToList();
                }

                if (fillterModel.CategoryName != null && !"".Equals(fillterModel.CategoryName))
                {
                    result = result.Where(s => s.CategoryName.Equals(fillterModel.CategoryName)).ToList();
                }
                List <ViewQuestionExam> list = new List <ViewQuestionExam>();
                foreach (var item in result)
                {
                    ViewQuestionExam Question = new ViewQuestionExam();
                    Question.QuesId       = item.Id;
                    Question.CategoryName = item.CategoryName;
                    Question.Content      = item.Content;
                    Question.Level        = item.Level;
                    Question.Suggestion   = item.Suggestion;
                    Question.Type         = item.Type;
                    Question.Status       = item.Status;
                    Question.CreatedBy    = item.CreateBy;
                    Question.CreatedDate  = item.CreateDate;
                    list.Add(Question);
                }
                return(list);
            }
            catch (Exception e)
            {
                return(GetAll());
            }
        }
        public int RandomQuestion(ViewQuestionExam model)
        {
            List <ExamQuestion> list = new List <ExamQuestion>();
            var questions            = context.Questions.ToList();
            var examquestion         = context.ExamQuestions.Where(ex => ex.ExamId == model.ExamId).ToList();

            foreach (var item in examquestion)
            {
                questions.Remove(questions.SingleOrDefault(s => s.Id == item.QuestionId));
            }

            ExamQuestion ExamQues;

            foreach (var item in questions)
            {
                ExamQues = new ExamQuestion();


                ExamQues.QuestionId = item.Id;
                ExamQues.ExamId     = model.ExamId;
                list.Add(ExamQues);
            }

            if (model.Total > list.Count)
            {
                model.Total = list.Count;
            }
            Random random;
            int    count = questions.Count;

            for (int i = 0; i < model.Total; i++)
            {
                random = new Random();
                count--;
                int randomnumber = random.Next(0, count);



                context.ExamQuestions.Add(list.ElementAt(randomnumber));
                list.Remove(list.ElementAt(randomnumber));
            }

            return(context.SaveChanges());
        }
Exemplo n.º 3
0
        public IEnumerable <ViewQuestionExam> GetListQuestionById(int id)
        {
            var result = (from e in context.ExamQuestions
                          join q in context.Questions on e.QuestionId equals q.Id
                          join ex in context.Exams on e.ExamId equals ex.Id
                          join c in context.Categorys on q.Category.Id equals c.Id
                          where e.ExamId == id
                          select new
            {
                Id = e.QuestionId,
                NameExam = ex.NameExam,
                Content = q.Content,
                Level = q.Level,
                Suggestion = q.Suggestion,
                Type = q.Type,
                Media = q.Media,
                Status = q.Status,
                CreateBy = q.CreatedBy,
                CreateDate = q.CreatedDate,
                CategoryName = c.Name,
                Space = ex.SpaceQuestionNumber
            }).ToList();
            List <ViewQuestionExam> list = new List <ViewQuestionExam>();

            foreach (var item in result)
            {
                ViewQuestionExam ExamQuestion = new ViewQuestionExam();
                ExamQuestion.QuesId       = item.Id;
                ExamQuestion.nameExam     = item.NameExam;
                ExamQuestion.Content      = item.Content;
                ExamQuestion.Level        = item.Level;
                ExamQuestion.Suggestion   = item.Suggestion;
                ExamQuestion.Type         = item.Type;
                ExamQuestion.Status       = item.Status;
                ExamQuestion.CreatedBy    = item.CreateBy;
                ExamQuestion.CreatedDate  = item.CreateDate;
                ExamQuestion.CategoryName = item.CategoryName;
                ExamQuestion.Space        = item.Space;
                list.Add(ExamQuestion);
            }

            return(list);
        }
Exemplo n.º 4
0
        //getall
        public IEnumerable <ViewQuestionExam> GetById(int id)
        {
            var examquestion = context.ExamQuestions.Where(e => e.ExamId == id).ToList();
            //var questions = context.Questions.ToList();
            var questions = (from e in context.Questions
                             join c in context.Categorys on e.Category.Id equals c.Id

                             select new
            {
                Id = e.Id,
                CategoryName = c.Name,
                Content = e.Content,
                Level = e.Level,
                Suggestion = e.Suggestion,
                Type = e.Type,
                Media = e.Media,
                Status = e.Status,
                CreateBy = e.CreatedBy,
                CreateDate = e.CreatedDate
            }).ToList();
            List <ViewQuestionExam> list = new List <ViewQuestionExam>();

            foreach (var itemExamQuestion in examquestion)
            {
                questions.Remove(questions.SingleOrDefault(s => s.Id == itemExamQuestion.QuestionId));
            }

            foreach (var item in questions)
            {
                ViewQuestionExam Question = new ViewQuestionExam();
                Question.QuesId       = item.Id;
                Question.CategoryName = item.CategoryName;
                Question.Content      = item.Content;
                Question.Level        = item.Level;
                Question.Suggestion   = item.Suggestion;
                Question.Type         = item.Type;
                Question.Status       = item.Status;
                Question.CreatedBy    = item.CreateBy;
                Question.CreatedDate  = item.CreateDate;
                list.Add(Question);
            }
            return(list);
        }
Exemplo n.º 5
0
        public IEnumerable <ViewQuestionExam> GetAll()
        {
            var result = (from e in context.Questions
                          join c in context.Categorys on e.Category.Id equals c.Id
                          where !(from q in context.ExamQuestions select q.QuestionId).ToList().Contains(e.Id)
                          select new
            {
                Id = e.Id,
                CategoryName = c.Name,
                Content = e.Content,
                Level = e.Level,
                Suggestion = e.Suggestion,
                Type = e.Type,
                Media = e.Media,
                Status = e.Status,
                CreateBy = e.CreatedBy,
                CreateDate = e.CreatedDate
            }).ToList();
            List <ViewQuestionExam> list = new List <ViewQuestionExam>();

            foreach (var item in result)
            {
                ViewQuestionExam Question = new ViewQuestionExam();
                Question.QuesId       = item.Id;
                Question.CategoryName = item.CategoryName;
                Question.Content      = item.Content;
                Question.Level        = item.Level;
                Question.Suggestion   = item.Suggestion;
                Question.Type         = item.Type;
                Question.Status       = item.Status;
                Question.CreatedBy    = item.CreateBy;
                Question.CreatedDate  = item.CreateDate;
                list.Add(Question);
            }

            return(list);
        }
Exemplo n.º 6
0
 public IEnumerable <ViewQuestionExam> Filter(ViewQuestionExam filterModel)
 {
     return(repository.Filter(filterModel));
 }
Exemplo n.º 7
0
 public int RandomQuestion(ViewQuestionExam model)
 {
     return(repository.RandomQuestion(model));
 }
Exemplo n.º 8
0
        public int RandomQuestion(ViewQuestionExam model)
        {
            List <ExamQuestion> list = new List <ExamQuestion>();
            var exam         = context.Exams.Where(ex => ex.Id == model.ExamId).SingleOrDefault();
            var questions    = context.Questions.Where(e => e.Category.Name == model.CategoryName && e.Level == model.Type).ToList();
            var examquestion = context.ExamQuestions.Where(ex => ex.ExamId == model.ExamId).ToList();

            foreach (var item in examquestion)
            {
                questions.Remove(questions.SingleOrDefault(s => s.Id == item.QuestionId));
            }

            ExamQuestion ExamQues;

            foreach (var item in questions)
            {
                ExamQues = new ExamQuestion();


                ExamQues.QuestionId = item.Id;
                ExamQues.ExamId     = model.ExamId;
                list.Add(ExamQues);
            }

            if (model.Total >= list.Count)
            {
                model.Total = list.Count;
            }
            Random random;
            int    count = questions.Count;

            int checkSpace = exam.SpaceQuestionNumber - examquestion.Count;

            if (checkSpace < model.Total)
            {
                for (int i = 0; i < checkSpace; i++)
                {
                    random = new Random();
                    count--;
                    int randomnumber = random.Next(0, count);

                    var currentQuestion = context.Questions.Find(list.ElementAt(randomnumber).QuestionId);
                    currentQuestion.Status = -3;

                    context.Entry(currentQuestion).State = EntityState.Modified;
                    //context.SaveChanges();

                    context.ExamQuestions.Add(list.ElementAt(randomnumber));
                    list.Remove(list.ElementAt(randomnumber));
                }
            }
            else
            {
                for (int i = 0; i < model.Total; i++)
                {
                    random = new Random();
                    count--;
                    int randomnumber = random.Next(0, count);


                    var currentQuestion = context.Questions.Find(list.ElementAt(randomnumber).QuestionId);
                    currentQuestion.Status = -3;

                    context.Entry(currentQuestion).State = EntityState.Modified;
                    //context.SaveChanges();

                    context.ExamQuestions.Add(list.ElementAt(randomnumber));
                    list.Remove(list.ElementAt(randomnumber));
                }
            }


            return(context.SaveChanges());
        }
Exemplo n.º 9
0
        public IEnumerable <ViewQuestionExam> Search(string searchString)
        {
            try
            {
                if (!string.IsNullOrEmpty(searchString))
                {
                    var result = (from e in context.Questions
                                  join c in context.Categorys on e.Category.Id equals c.Id
                                  where e.Content.Contains(searchString)
                                  select new
                    {
                        Id = e.Id,
                        CategoryName = c.Name,
                        Content = e.Content,
                        Level = e.Level,
                        Suggestion = e.Suggestion,
                        Type = e.Type,
                        Media = e.Media,
                        Status = e.Status,
                        CreateBy = e.CreatedBy,
                        CreateDate = e.CreatedDate
                    }).ToList();
                    List <ViewQuestionExam> list = new List <ViewQuestionExam>();
                    foreach (var item in result)
                    {
                        ViewQuestionExam Question = new ViewQuestionExam();
                        Question.QuesId       = item.Id;
                        Question.CategoryName = item.CategoryName;
                        Question.Content      = item.Content;
                        Question.Level        = item.Level;
                        Question.Suggestion   = item.Suggestion;
                        Question.Type         = item.Type;
                        Question.Status       = item.Status;
                        Question.CreatedBy    = item.CreateBy;
                        Question.CreatedDate  = item.CreateDate;
                        list.Add(Question);
                    }
                    return(list);
                }
                var questions = (from e in context.Questions
                                 join c in context.Categorys on e.Category.Id equals c.Id

                                 select new
                {
                    Id = e.Id,
                    CategoryName = c.Name,
                    Content = e.Content,
                    Level = e.Level,
                    Suggestion = e.Suggestion,
                    Type = e.Type,
                    Media = e.Media,
                    Status = e.Status,
                    CreateBy = e.CreatedBy,
                    CreateDate = e.CreatedDate
                }).ToList();
                List <ViewQuestionExam> listQuestion = new List <ViewQuestionExam>();
                foreach (var item in questions)
                {
                    ViewQuestionExam Question = new ViewQuestionExam();
                    Question.QuesId       = item.Id;
                    Question.CategoryName = item.CategoryName;
                    Question.Content      = item.Content;
                    Question.Level        = item.Level;
                    Question.Suggestion   = item.Suggestion;
                    Question.Type         = item.Type;
                    Question.Status       = item.Status;
                    Question.CreatedBy    = item.CreateBy;
                    Question.CreatedDate  = item.CreateDate;
                    listQuestion.Add(Question);
                }
                return(listQuestion);
            }
            catch (Exception e)
            {
                return(GetAll());
            }
        }