Esempio n. 1
0
        public async Task <IActionResult> AddDragAndDropQuestion([FromBody] AddDragAndDropQuestionViewModel model)
        {
            var user = await _userManager.GetUserAsync(HttpContext.User);

            var test = await _context.Tests.SingleOrDefaultAsync(t => t.Id == (int)RouteData.Values["testId"]);

            if (test == null)
            {
                return(NotFound());
            }

            if (test.CreatedBy != user)
            {
                return(Forbid());
            }

            model.TestId = test.Id;
            TryValidateModel(model);
            if (ModelState.IsValid)
            {
                // транзакция
                using (var ts = _context.Database.BeginTransaction())
                {
                    var question = new DragAndDropQuestion
                    {
                        Title        = model.Title,
                        QuestionType = Enum.GetName(typeof(Question.QuestionTypeEnum), 4),
                        Test         = test,
                        Score        = model.Score
                    };
                    //создать в базе вопрос
                    var questionCreated = (await _context.Questions.AddAsync(question)).Entity;
                    await _context.SaveChangesAsync(); //применить изменения

                    foreach (var option in model.Options)
                    {
                        // добавить в базу Options
                        var optionCreated = (await _context.Options.AddAsync(
                                                 new Option {
                            Order = option.Order, Text = option.Text, Question = questionCreated
                        })).Entity;
                    }

                    // обновить вопрос и применить изменения
                    _context.Questions.Update(questionCreated);
                    await _context.SaveChangesAsync();

                    ts.Commit();
                }


                var redirectUrl = Url.Action("Details", "Test", new { id = test.Id });
                return(new JsonResult(redirectUrl));
            }

            var errors = new List <ModelError>();

            foreach (var modelState in ViewData.ModelState.Values)
            {
                foreach (var error in modelState.Errors)
                {
                    errors.Add(error);
                }
            }
            Response.StatusCode = StatusCodes.Status400BadRequest;
            return(new JsonResult(errors));
        }
Esempio n. 2
0
            private static void ParseQuestion(Queue <Token> tokens, Test test, TestData testData)
            {
                Question question;
                int      i = 1, checkedCount = 0, Row = 0;
                bool     textParsed = false, scoreParsed = false, optionParsed = false, codeParsed = false;

                Row = tokens.Peek().Row;
                Consume(tokens, "question");
                Consume(tokens, "{");
                var type = ParseType(tokens);

                switch (type)
                {
                case (int)Question.QuestionTypeEnum.SingleChoiceQuestion:
                    question = new SingleChoiceQuestion();
                    break;

                case (int)Question.QuestionTypeEnum.MultiChoiceQuestion:
                    question = new MultiChoiceQuestion();
                    break;

                case (int)Question.QuestionTypeEnum.TextQuestion:
                    question = new TextQuestion();
                    break;

                case (int)Question.QuestionTypeEnum.DragAndDropQuestion:
                    question = new DragAndDropQuestion();
                    break;

                case (int)Question.QuestionTypeEnum.CodeQuestion:
                    question = new CodeQuestion();
                    break;

                default:
                    question = new SingleChoiceQuestion();
                    break;
                }

                question.Test = test;
                while (tokens.Peek().Value != "}")
                {
                    switch (tokens.Peek().Value)
                    {
                    case "text":
                        if (!textParsed)
                        {
                            question.Title = ParseString(tokens, "text");
                        }
                        else
                        {
                            throw new Exception(string.Format("Text already parsed (Quesiton (Row - {0})).", Row));
                        }
                        textParsed = true;
                        break;

                    case "score":
                        if (!scoreParsed)
                        {
                            question.Score = ParseInt(tokens, "score");
                        }
                        else
                        {
                            throw new Exception(string.Format("Score already parsed (Quesiton (Row - {0})).", Row));
                        }
                        scoreParsed = true;
                        break;

                    case "option":
                        if (question is TextQuestion)
                        {
                            if (i == 1)
                            {
                                ParseOption(tokens, question, testData, i++, ref checkedCount);
                            }
                            else
                            {
                                throw new Exception(string.Format(
                                                        "TextQuestion поддерживает только один ответ (Quesiton (Row - {0})).", Row));
                            }
                        }
                        else
                        {
                            ParseOption(tokens, question, testData, i++, ref checkedCount);
                        }
                        optionParsed = true;
                        break;

                    case "code":
                        if (question is CodeQuestion)
                        {
                            if (!codeParsed)
                            {
                                ParseCode(tokens, question, testData);
                            }
                            else
                            {
                                throw new Exception(string.Format(
                                                        "CodeQuestion поддерживает только один ответ (Quesiton (Row - {0})).", Row));
                            }
                        }
                        else
                        {
                            throw new Exception(
                                      string.Format("Данный тип вопроса не поддерживает code(Quesiton (Row - {0})).",
                                                    Row));
                        }

                        codeParsed = true;
                        break;

                    default:
                        throw new Exception(string.Format("Неизвестный параметр: {0} (Row - {1}).",
                                                          tokens.Peek().Value, tokens.Peek().Row));
                    }
                }
                question.QuestionType = Enum.GetName(typeof(Question.QuestionTypeEnum), type);
                if (question is SingleChoiceQuestion && checkedCount != 1)
                {
                    throw new Exception(string.Format(
                                            "В данном типе вопроса нужно отметить верный один ответ (Quesiton (Row - {0})).", Row));
                }
                if (question is MultiChoiceQuestion && checkedCount < 1)
                {
                    throw new Exception(string.Format(
                                            "В данном типе вопроса нужно отметить хотя бы один верный ответ (Quesiton (Row - {0})).", Row));
                }
                Consume(tokens, "}");
                if (!scoreParsed)
                {
                    question.Score = 1;
                }
                if (!(question is CodeQuestion))
                {
                    if (!textParsed || !optionParsed)
                    {
                        throw new Exception(string.Format(
                                                "Заданы не все требуемые поля (Quesiton (Row - {0})). Text - {1}, Option - {2}.",
                                                Row, textParsed, optionParsed));
                    }
                }
                else if (!textParsed || !codeParsed)
                {
                    throw new Exception(string.Format(
                                            "Заданы не все требуемые поля (Quesiton (Row - {0})). Text - {1}, Code - {2}.",
                                            Row, textParsed, codeParsed));
                }

                testData.Questions.Add(question);
            }