public void MultiChoiceNAnswers() { MultiChoiceQuestion q = new MultiChoiceQuestion("Multi Choice", "Texte du Multi Choice"); // q.AddAnswer("Answer 1", "aucune", 50); q.AddAnswer("Answer 2", "aucune", 50); q.AddAnswer("Answer 3", "aucune", 0); // Action act = () => q.AddAnswer("Answer 4", "aucune", 50); Assert.ThrowsException <OverflowException>(act); // Trace.WriteLine(q.MoodleXML.OuterXml); }
public void SaveAsMoodle(string filePath) { Quiz mQuiz = new Quiz(); // string push = E3CTest.NewLine; E3CTest.NewLine = "<br/>"; // foreach (Theme th in Themes) { Category cat = new Category(th.Titre); // foreach (Question q in th.Questions) { MultiChoiceQuestion mQuestion = new MultiChoiceQuestion(); mQuestion.Title = q.Titre; mQuestion.QuestionText = q.TexteQuestion; // String repJuste = q.Correction.Choix; repJuste = repJuste.Trim(); if (!String.IsNullOrWhiteSpace(repJuste)) { // Ok, on a une Correction (une Réponse Juste) foreach (Reponse rep in q.Reponses) { Answer ans = new Answer(); ans.Text = rep.Texte; if (String.Compare(repJuste, rep.Choix, true) == 0) { ans.Fraction = 100; } mQuestion.AddAnswer(ans); } mQuiz.AddQuestion(cat, mQuestion); } } } // mQuiz.Save(filePath); // E3CTest.NewLine = push; }
public MultiChoiceQuestionViewModel(MultiChoiceQuestion question) { this.question = question; }
public async Task <IActionResult> AddMultiChoiceQuestion([FromBody] AddMultiChoiceQuestionViewModel 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 MultiChoiceQuestion { Title = model.Title, QuestionType = Enum.GetName(typeof(Question.QuestionTypeEnum), 2), Test = test, Score = model.Score }; //создать в базе вопрос var questionCreated = (await _context.AddAsync(question)).Entity; await _context.SaveChangesAsync(); //применить изменения foreach (var option in model.Options) { // добавить в базу Options var optionCreated = (await _context.AddAsync( new Option { IsRight = option.IsRight, 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)); }
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); }
public void MultiChoiceFromXMLTest() { String xml = " <question type = 'multichoice' >" + " <name>" + " <text>Variables</text>" + " </name>" + " <questiontext format='html'>" + " <text><![CDATA[<p>Question</p>]]></text>" + " </questiontext>" + " <generalfeedback format='html'>" + " <text><![CDATA[<p>feedback</p>]]></text>" + " </generalfeedback>" + " <defaultgrade>1.0000000</defaultgrade>" + " <penalty>0.3333333</penalty>" + " <hidden>0</hidden>" + " <single>true</single>" + " <shuffleanswers>true</shuffleanswers>" + " <answernumbering>abc</answernumbering>" + " <correctfeedback format='html'>" + " <text><![CDATA[<p>not</p>]]></text>" + " </correctfeedback>" + " <partiallycorrectfeedback format='html'>" + " <text><![CDATA[<p>partially</p>]]></text>" + " </partiallycorrectfeedback>" + " <incorrectfeedback format='html'>" + " <text><![CDATA[<p>incorrect</p>]]></text>" + " </incorrectfeedback>" + " <shownumcorrect/>" + " <answer fraction='0' format='html'>" + " <text><![CDATA[<p>Oui</p>]]></text>" + " <feedback format='html'>" + " <text></text>" + " </feedback>" + " </answer>" + " <answer fraction='100' format='html'>" + " <text><![CDATA[<p>Non</p>]]></text>" + " <feedback format='html'>" + " <text></text>" + " </feedback>" + " </answer>" + " <answer fraction='0' format='html'>" + " <text><![CDATA[<p>Aucune réponse n'est correcte</p>]]></text>" + " <feedback format='html'>" + " <text></text>" + " </feedback>" + " </answer>" + " <answer fraction='0' format='html'>" + " <text><![CDATA[<p>Uniquement en utilisant 'strict'</p>]]></text>" + " <feedback format='html'>" + " <text></text>" + " </feedback>" + " </answer>" + " </question>"; // // XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); // XmlElement root = doc.DocumentElement; // MultiChoiceQuestion question = new MultiChoiceQuestion(); question.MoodleXML = (XmlNode)root; // Assert.AreEqual("Variables", question.Name); Assert.AreEqual("<p>Question</p>", question.QuestionText); Assert.AreEqual("<p>feedback</p>", question.GeneralFeedback); Assert.AreEqual(1, question.DefaultGrade); Assert.AreEqual(0.3333333, question.Penalty); Assert.AreEqual(true, question.Single); Assert.AreEqual(true, question.ShuffleAnswers); // Assert.AreEqual(4, question.Answers.Count); Answer ans = question.Answers[0]; Assert.AreEqual(0, ans.Fraction); Assert.AreEqual("<p>Oui</p>", ans.Text); Assert.AreEqual(true, String.IsNullOrEmpty(ans.Feedback)); // ans = question.Answers[1]; Assert.AreEqual(100, ans.Fraction); Assert.AreEqual("<p>Non</p>", ans.Text); Assert.AreEqual(true, String.IsNullOrEmpty(ans.Feedback)); }