Exemplo n.º 1
0
        public async Task <IActionResult> Edit(long id, [Bind("Id,Topic,Questions")] TestCreationViewModel model)
        {
            var test = TestCreationTransformer.TransformToTest(model);

            if (id != test.Id)
            {
                return(NotFound());
            }
            try
            {
                SaveTest(test);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TestExists(test.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            if (ModelState.IsValid)
            {
                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> AddMultipleChoiceQuestion(long id, [Bind("Id,Topic,Questions")] TestCreationViewModel model)
        {
            SaveTest(TestCreationTransformer.TransformToTest(model));
            var newQuestion = new MultipleChoiceQuestion
            {
                TestId   = Consts.backUpTestId,
                Position = _context.Questions.Where(x => x.TestId == Consts.backUpTestId).Count(),
                Points   = 1
            };

            _context.Add(newQuestion);
            await _context.SaveChangesAsync();

            newQuestion = _context.Questions.FirstOrDefault(x => x.Id == newQuestion.Id) as MultipleChoiceQuestion;
            _context.Add(new Choice
            {
                QuestionId = newQuestion.Id
            });
            _context.Add(new Choice
            {
                QuestionId = newQuestion.Id
            });
            await _context.SaveChangesAsync();

            return(RedirectToAction("Edit", "TestCreation", new { id }, newQuestion.Id.ToString()));
        }
Exemplo n.º 3
0
 public Test(Group group, TestCreationViewModel testCreationViewModel, IEnumerable <Question> questions)
 {
     Name          = testCreationViewModel.Name;
     Group         = group;
     GroupId       = group.Id;
     TimeInSeconds = testCreationViewModel.TimeInSeconds;
     Questions     = new List <Question>(questions);
 }
Exemplo n.º 4
0
        public async Task <ActionResult> CreateGroupTest
            ([FromRoute][Required] string group_id,
            [Required][FromBody] TestCreationViewModel testCreationViewModel)
        {
            try
            {
                int.TryParse(group_id, out var id);
                var group = await _db.Groups.FindAsync(id);

                if (group == null)
                {
                    return(NotFound(group_id));
                }

                if (GetCurrentUserId() != group.TeacherId)
                {
                    return(Forbid());
                }

                if (testCreationViewModel.TimeInSeconds < 60)
                {
                    ModelState.TryAddModelError("TimeInSeconds",
                                                "Время прохождения теста не может быть меньше минуты");
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var questions = new List <Question>();
                foreach (var questionModel in testCreationViewModel.Questions)
                {
                    questions.Add(_mappingFactory.Map(questionModel));
                }

                var test = new Test(group, testCreationViewModel, questions);

                foreach (Question question in test.Questions)
                {
                    if (question is TwoColumnsQuestion twoColumnsQuestion)
                    {
                        twoColumnsQuestion.RandomizeOrder();
                    }
                }

                await _db.Tests.AddAsync(test);

                await _db.SaveChangesAsync();

                return(Ok(Mapper.Map <TestExViewModel>(test)));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(StatusCode(500, e.Message));
            }
        }
Exemplo n.º 5
0
        public async Task <IActionResult> DeleteChoice(long id, [Bind("Id,Topic,Questions")] TestCreationViewModel model)
        {
            SaveTest(TestCreationTransformer.TransformToTest(model));

            var oldChoice = await _context.Choices.FindAsync(id);

            var question = await _context.Questions.FindAsync(oldChoice.QuestionId);

            _context.Remove(oldChoice);
            await _context.SaveChangesAsync();

            return(RedirectToAction("Edit", "TestCreation", new { id = Consts.backUpTestId }, question.Id.ToString()));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> AddChoice(long id, [Bind("Id,Topic,Questions")] TestCreationViewModel model)
        {
            SaveTest(TestCreationTransformer.TransformToTest(model));
            var newChoice = new Choice();

            newChoice.QuestionId = id;
            _context.Add(newChoice);
            await _context.SaveChangesAsync();

            var question = await _context.Questions.FirstOrDefaultAsync(x => x.Id == id);

            return(RedirectToAction("Edit", "TestCreation", new { id = Consts.backUpTestId }, id.ToString()));
        }
Exemplo n.º 7
0
        public async Task <IActionResult> AddMathQuestion(long id, [Bind("Id,Topic,Questions")] TestCreationViewModel model)
        {
            SaveTest(TestCreationTransformer.TransformToTest(model));
            var newQuestion = new MathQuestion();

            newQuestion.TestId   = Consts.backUpTestId;
            newQuestion.Position = _context.Questions.Where(x => x.TestId == Consts.backUpTestId).Count();
            newQuestion.Points   = 1;

            _context.Add(newQuestion);
            await _context.SaveChangesAsync();

            return(RedirectToAction("Edit", "TestCreation", new { id }, newQuestion.Id.ToString()));
        }
Exemplo n.º 8
0
        public async Task <IActionResult> PushQuestionDown(long id, [Bind("Id,Topic,Questions")] TestCreationViewModel model)
        {
            SaveTest(TestCreationTransformer.TransformToTest(model));
            var question = await _context.Questions.FindAsync(id);

            var otherQuestion = await _context.Questions.FirstOrDefaultAsync(x => x.Position == question.Position + 1 && x.TestId == question.TestId);

            question.Position++;
            otherQuestion.Position--;
            _context.Update(question);
            _context.Update(otherQuestion);
            await _context.SaveChangesAsync();

            return(RedirectToAction("Edit", "TestCreation", new { id = Consts.backUpTestId }, id.ToString()));
        }
Exemplo n.º 9
0
        public async Task <IActionResult> SaveChanges(long id, [Bind("Id,Topic,Questions")] TestCreationViewModel model)
        {
            var test = TestCreationTransformer.TransformToTest(model);

            test.Id = originalTestId;
            _context.Questions.RemoveRange(_context.Questions.Where(x => x.TestId == originalTestId));
            _context.Choices.RemoveRange(_context.Choices
                                         .Include(x => x.Question)
                                         .Where(x => x.Question.TestId == originalTestId));

            foreach (var question in test.Questions
                     .Where(x => x is MultipleChoiceQuestion)
                     .Select(x => x as MultipleChoiceQuestion)
                     .ToList())
            {
                question.TestId = test.Id;
                if (question.Choices != null)
                {
                    foreach (var choice in question.Choices)
                    {
                        choice.QuestionId = question.Id;
                    }
                }
            }

            foreach (var question in test.Questions
                     .Where(x => x is MathQuestion)
                     .Select(x => x as MathQuestion)
                     .ToList())
            {
                question.TestId = test.Id;
            }

            _context.Update(test);
            _context.SaveChanges();
            DeleteBackUp();
            return(RedirectToAction(nameof(Index)));
        }
Exemplo n.º 10
0
        public async Task <IActionResult> DeleteQuestion(long id, [Bind("Id,Topic,Questions")] TestCreationViewModel model)
        {
            SaveTest(TestCreationTransformer.TransformToTest(model));
            var oldQuestion = await _context.Questions.FindAsync(id);

            var test = await _context.Tests.FindAsync(oldQuestion.TestId);

            test.Questions = await _context.Questions.Where(x => x.TestId == test.Id).ToListAsync();

            test.Questions.Remove(oldQuestion);
            test.Questions = test.Questions.OrderBy(x => x.Position).ToList();
            int position = 0;

            foreach (var question in test.Questions)
            {
                question.Position = position;
                position++;
            }

            _context.Remove(oldQuestion);
            await _context.SaveChangesAsync();

            return(RedirectToAction("Edit", "TestCreation", new { id = Consts.backUpTestId }, id.ToString()));
        }
Exemplo n.º 11
0
 public async Task <IActionResult> BackToMainMenu(long id, [Bind("Id,Topic,Questions")] TestCreationViewModel model)
 {
     DeleteBackUp();
     return(RedirectToAction(nameof(Index)));
 }