Esempio n. 1
0
        public AnswersViewModel GetAnswers(string examId, string UserId)
        {
            UserParticipateInExam participant = ctx.UsersParticipateInExams.Where(o => o.ExamFK == examId && o.ParticipantFK == UserId).FirstOrDefault();

            if (participant == null)
            {
                return(null);
            }

            List <Answer>          anss       = ctx.Answers.Where(o => o.ExamParticipant == participant).ToList();
            List <AnswerViewModel> RetAnswers = new List <AnswerViewModel>();

            foreach (Answer ans in anss)
            {
                RetAnswers.Add(
                    new AnswerViewModel
                {
                    AnswerText = ans.AnswerText,
                    //ExamId=ans.ExamId,
                    QuestionId = ans.QuestionId
                });
            }

            AnswersViewModel AnsVM = new AnswersViewModel();

            AnsVM.UserName = ctx.AppUsers.Where(o => o.Id == UserId).FirstOrDefault().UserName;
            AnsVM.ExamName = ctx.Exams.Where(o => o.ExamId == examId).FirstOrDefault().ExamName;
            AnsVM.Answers  = RetAnswers;

            return(AnsVM);
        }
Esempio n. 2
0
        public bool UnenrollExam(string ExamId, string UserId)
        {
            UserParticipateInExam userParticipateInExam = ctx.UsersParticipateInExams
                                                          .Where(o => o.ExamFK == ExamId && o.ParticipantFK == UserId).FirstOrDefault();

            if (userParticipateInExam == null)
            {
                return(false);
            }

            ctx.UsersParticipateInExams.Remove(userParticipateInExam);

            //delete cascading is restricted, so we implement delete ourselves
            Answer[] toDelete = ctx.Answers.Where(o => o.ExamId == ExamId && o.ExamParticipant == userParticipateInExam).ToArray();
            ctx.Answers.RemoveRange(toDelete);

            return(true);
        }
Esempio n. 3
0
        public bool FillAnswer(AnswerViewModel answerViewModel, string UserId)
        {
            UserParticipateInExam part = ctx.UsersParticipateInExams.Where(o => //o.ExamFK == answerViewModel.ExamId &&
                                                                           o.ParticipantFK == UserId).FirstOrDefault();

            if (part == null)
            {
                return(false);
            }

            ctx.Answers.Where(o =>
                              //o.ExamId == answerViewModel.ExamId &&
                              o.QuestionId == answerViewModel.QuestionId &&
                              o.ExamParticipant == part)
            .FirstOrDefault().AnswerText = answerViewModel.AnswerText;

            return(true);
        }
Esempio n. 4
0
        public bool EnrollExam(string ExamId, string UserId)
        {
            Exam e = ctx.Exams.Find(ExamId);

            if (e == null)
            {
                return(false); //not found
            }
            UserParticipateInExam userParticipateInExam = ctx.UsersParticipateInExams
                                                          .Where(o => o.ExamFK == ExamId && o.ParticipantFK == UserId).FirstOrDefault();

            if (userParticipateInExam != null)
            {
                return(false); //bad request
            }
            UserParticipateInExam AddedParticipant = ctx.UsersParticipateInExams.Add(
                new UserParticipateInExam
            {
                ExamFK        = ExamId,
                ParticipantFK = UserId,
            }).Entity;

            Question[] questions = ctx.Questions.Where(o => o.ExamId == ExamId).ToArray();

            List <Answer> EmptyAnswers = new List <Answer>();

            foreach (Question question in questions)
            {
                EmptyAnswers.Add(
                    new Answer
                {
                    AnswerText      = "",
                    Question        = question,
                    ExamParticipant = AddedParticipant
                });
            }
            ctx.Answers.AddRange(EmptyAnswers);

            return(true);
        }
Esempio n. 5
0
        public bool FillAllAnswers(AnswersViewModel answers, string ExamId, string UserId)
        {
            UserParticipateInExam part = ctx.UsersParticipateInExams.Where(o =>
                                                                           o.ExamFK == ExamId &&
                                                                           o.ParticipantFK == UserId).FirstOrDefault();

            if (part == null)
            {
                return(false);
            }

            List <Answer> anss = ctx.Answers.Where(o =>
                                                   o.ExamId == ExamId &&
                                                   o.ExamParticipant == part)
                                 .ToList();

            foreach (var ans in answers.Answers)
            {
                anss.Where(o => o.QuestionId == ans.QuestionId)
                .FirstOrDefault().AnswerText = ans.AnswerText;
            }

            return(true);
        }