コード例 #1
0
ファイル: AdminController.cs プロジェクト: FaridGN/quizapp
        public async Task <IActionResult> CreateExam(Exam exam)
        {
            if (ModelState.IsValid)
            {
                _intellectDbContext.Exams.Add(exam);
                await _intellectDbContext.SaveChangesAsync();

                return(RedirectToAction(nameof(Admin)));
            }
            else
            {
                ModelState.AddModelError("", "Couldn't create");
                return(View());
            }
        }
コード例 #2
0
        public async Task <IActionResult> Enroll(TestTaker testTaker, DateTime birthdate)
        {
            if (ModelState.IsValid)
            {
                testTaker.Birth         = birthdate;
                testTaker.Result        = 0;
                testTaker.UserQuestions = await _intellectDbContext.Questions.Where(q => q.ExamId == 3).Select(q => new UserQuestion {
                    Question = q, TestTaker = testTaker
                }).ToListAsync();

                _intellectDbContext.TestTakers.Add(testTaker);
                await _intellectDbContext.SaveChangesAsync();

                return(RedirectToAction(nameof(Proceed)));
            }
            else
            {
                ModelState.AddModelError("", "Kecmedi");
                return(View());
            }
        }
コード例 #3
0
        public async Task InsertAsync(TEntity entity)
        {
            await _context.Set <TEntity>().AddAsync(entity);

            await _context.SaveChangesAsync();
        }
コード例 #4
0
        public async Task <IActionResult> Question(int Id, int count, int myanswer)
        {
            AdminViewModel admodel = new AdminViewModel();

            admodel.CurrentQuestion = await _intellectDbContext.Questions.Where(x => x.Id == Id).SingleOrDefaultAsync();

            admodel.Answers = await _intellectDbContext.Answers.Where(y => y.QuestionId == Id).ToListAsync();

            admodel.Equestions = await _intellectDbContext.Questions.Where(q => q.ExamId == exam_id).ToListAsync();

            /*  var date = HttpContext.Session.GetString("currentQuestion_1");
             *
             * return Ok(new
             * {
             *    date,
             *    Id,
             *    HttpContext.Session.Keys
             */
            //  admodel.CurrentQuestion.Remainedtime = new TimeSpan(0, 0, 60);
            //  var interval = DateTime.Now - DateTime.Parse(HttpContext.Session.GetString("currentQuestion_" + Id));

            var sesdata  = HttpContext.Session.GetString("currentQuestion_" + PreviousId);
            var interval = DateTime.Now - DateTime.Parse(sesdata);

            correctAnswer = _intellectDbContext.Answers.Where(a => a.QuestionId == PreviousId && a.Correct == true).SingleOrDefault().Id;

            if (_signInManager.IsSignedIn(User))
            {
                ExamUser examTaker = await _userManager.GetUserAsync(HttpContext.User);

                examTaker.TestTaker = await _intellectDbContext.TestTakers
                                      .Include(tt => tt.UserQuestions)
                                      .Where(t => t.Id == examTaker.TestTakerId)
                                      .FirstOrDefaultAsync();

                admodel.CurrentQuestion = examTaker.TestTaker.UserQuestions.Select(u => u.Question).Where(x => x.Id == Id).SingleOrDefault();

                if (myanswer == correctAnswer)
                {
                    admodel.CurrentQuestion.Score = 60 - (int)interval.TotalMilliseconds / 1000;
                    await _intellectDbContext.SaveChangesAsync();

                    // admodel.CurrentQuestion.Score = result;
                }
            }

            if (count > 1)
            {
                var question = RemainedQuestions.Single(r => r.Id == admodel.CurrentQuestion.Id);
                PreviousId = question.Id;
                RemainedQuestions.Remove(question);
                admodel.NextQuestion = RemainedQuestions[0];
                count -= 1;
            }
            else
            {
                admodel.NextQuestion = RemainedQuestions[0];
                count -= 1;
            }

            if (count == -1)
            {
                return(RedirectToAction(nameof(Finish)));
            }

            ViewBag.Equestions = count;

            return(RedirectToAction(nameof(Question)));
        }