Esempio n. 1
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()));
        }
Esempio n. 2
0
        public async Task <IActionResult> Edit(long?id)
        {
            if (IsNotAdmin())
            {
                return(RedirectToAction("Index", "Home"));
            }
            if (id == null)
            {
                return(NotFound());
            }
            if (_context.Tests.Any(x => x.Id == id))
            {
                if (_context.Tests.FirstOrDefault(x => x.Id == id).ReleaseStatus != TestStatus.InProgress)
                {
                    return(RedirectToAction(nameof(Index)));
                }
            }
            else if (id != Consts.backUpTestId)
            {
                return(RedirectToAction(nameof(Index)));
            }

            if (id != Consts.backUpTestId)
            {
                originalTestId = (long)id;
                return(RedirectToAction("Edit", "TestCreation", new { id = Consts.backUpTestId }, lastChangedQuestion));
            }

            var backUpTest = CreateBackUp();
            var viewModel  = TestCreationTransformer.TransformToTestCreationViewModel(backUpTest);

            viewModel.LastChangedQuestion = lastChangedQuestion;
            return(View(viewModel));
        }
Esempio n. 3
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));
        }
Esempio n. 4
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()));
        }
Esempio n. 5
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()));
        }
Esempio n. 6
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()));
        }
Esempio n. 7
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()));
        }
Esempio n. 8
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)));
        }
Esempio n. 9
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()));
        }