Beispiel #1
0
        public async Task <IActionResult> Edit(string id, [Bind("Id,Content,UserId,QuestionId,Checked")] OpenQuestionAnswer openQuestionAnswer)
        {
            if (id != openQuestionAnswer.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(openQuestionAnswer);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!OpenQuestionAnswerExists(openQuestionAnswer.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["QuestionId"] = new SelectList(_context.Questions, "Id", "Id", openQuestionAnswer.QuestionId);
            ViewData["UserId"]     = new SelectList(_context.Users, "Id", "Id", openQuestionAnswer.UserId);
            return(View(openQuestionAnswer));
        }
Beispiel #2
0
        public async Task <IActionResult> Create([Bind("Id,Content,UserId,QuestionId,Checked")] OpenQuestionAnswer openQuestionAnswer)
        {
            if (ModelState.IsValid)
            {
                _context.Add(openQuestionAnswer);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["QuestionId"] = new SelectList(_context.Questions, "Id", "Id", openQuestionAnswer.QuestionId);
            ViewData["UserId"]     = new SelectList(_context.Users, "Id", "Id", openQuestionAnswer.UserId);
            return(View(openQuestionAnswer));
        }
Beispiel #3
0
        public async Task <OpenQuestionAnswer> Create([FromRoute] string id, [FromBody] OpenQuestionAnswer answer)
        {
            var currentUserName = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            var user            = _apiContext.Inspectors.FirstOrDefault(elem => elem.UserAccount.UserName == currentUserName);

            answer.Id        = Guid.NewGuid().ToString("N");
            answer.Inspector = user;
            answer.Question  = await _apiContext.OpenQuestions.FindAsync(id);

            _apiContext.OpenQuestionAnswers.Add(answer);
            await _apiContext.SaveChangesAsync();

            return(answer);
        }
Beispiel #4
0
        public async Task <OpenQuestionAnswer> Edit([FromRoute] string id, [FromBody] OpenQuestionAnswer answerposted)
        {
            var currentUserName = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            var user            = _apiContext.Inspectors.FirstOrDefault(elem => elem.UserAccount.UserName == currentUserName);
            var answer          = await _apiContext.OpenQuestionAnswers.FindAsync(answerposted.Id);

            answer.Answer    = answerposted.Answer;
            answer.Inspector = user;
            answer.Question  = await _apiContext.OpenQuestions.FindAsync(id);

            await _apiContext.SaveChangesAsync();

            return(answerposted);
        }
Beispiel #5
0
        public async Task <IActionResult> Check([Bind("TestId,CourseId,Questions")] TestViewModel testViewModel, string courseId, string testId)
        {
            User currentUser = await _userManager.GetUserAsync(User);

            if (!await _userManager.IsInRoleAsync(currentUser, "Student"))
            {
                //return Challenge();
            }
            int  countCorrect   = 0;
            bool noOpenQuestion = true;

            foreach (var question in testViewModel.Questions)
            {
                switch (question.Type)
                {
                case Question.QuestionType.SingleAnswer:
                    int i = 0;
                    while (!question.Answers[i].Correct)
                    {
                        i++;
                    }
                    if (question.SelectedAnswerId == question.Answers[i].AnswerId)
                    {
                        countCorrect++;
                    }
                    break;

                case Question.QuestionType.MultipleAnswer:
                    bool allCorrect = true;
                    foreach (var answer in question.Answers)
                    {
                        if (answer.Correct != answer.Selected)
                        {
                            allCorrect = false;
                        }
                    }
                    if (allCorrect)
                    {
                        countCorrect++;
                    }
                    break;

                case Question.QuestionType.Open:
                    noOpenQuestion = false;
                    OpenQuestionAnswer openAnswer = new OpenQuestionAnswer
                    {
                        Id      = Guid.NewGuid().ToString(),
                        Content = question.OpenAnswer,
                        UserId  = currentUser.Id,
                        //User = currentUser,
                        QuestionId = question.QuestionId,
                        //Question = question,
                        Checked = false
                    };
                    OpenQuestionAnswersController openAnswersController = new OpenQuestionAnswersController(_context, _userManager);
                    await openAnswersController.Create(openAnswer);

                    break;

                default: break;
                }
            }

            UserTestResult testResult = new UserTestResult
            {
                Id     = Guid.NewGuid().ToString(),
                UserId = currentUser.Id,
                //User=
                TestId = testViewModel.TestId,
                //Test=
                PointsCount = countCorrect,
                Checked     = noOpenQuestion
            };
            UserTestResultsController resultsController = new UserTestResultsController(_context);
            await resultsController.Create(testResult);

            return(RedirectToAction("Details", "Courses", new { id = courseId }));
        }