/// <summary> /// Get a random question linked to the quiz. /// </summary> /// <param name="id">The quiz id.</param> /// <returns>A random question.</returns> private async Task <DisplayGameQuestionModel> GetRandomQuestionFromQuiz(string id) { Random random = new Random(); // Database related helpers LinkQuizQuestionData linkQuizQuestionData = new LinkQuizQuestionData(_db); QuestionData questionData = new QuestionData(_db); QuestionTypeData questionTypeData = new QuestionTypeData(_db); // Get Quiz questions and select one randomly List <DataLinkQuizQuestionModel> dataLinkQuizQuestionModels = await linkQuizQuestionData.GetLinkedQuestions(id); DataLinkQuizQuestionModel randomlySelectedlink = dataLinkQuizQuestionModels[random.Next(dataLinkQuizQuestionModels.Count)]; DataQuestionModel randomlySelectedQuestion = await questionData.GetQuestionById(randomlySelectedlink.QuestionId).ContinueWith((x) => { return(x.Result[0]); }); // Get Question Type to get the real name DataQuestionTypeModel dataQuestionTypeModel = await questionTypeData.GetQuestionTypeById(randomlySelectedQuestion.QuestionTypeId).ContinueWith((x) => { return(x.Result[0]); }); // Result the translated object return(new DisplayGameQuestionModel { Id = randomlySelectedQuestion.Id, Question = randomlySelectedQuestion.Question, QuestionType = dataQuestionTypeModel.QuestionType, }); }
/// <summary> /// Get all the question's linked answers and display them. /// </summary> /// <param name="id">The question id.</param> public async void OnGetAsync(string id) { DisplayAnswers = new List <DisplayAnswerModel>(); // Getting the current question QuestionData questionData = new QuestionData(_db); var dataQuestionModels = await questionData.GetQuestionById(id); DisplayQuestionModel = new DisplayQuestionModel { DomainId = dataQuestionModels[0].DomainId, Id = dataQuestionModels[0].Id, Question = dataQuestionModels[0].Question, QuestionTypeId = dataQuestionModels[0].QuestionTypeId, }; // Getting Question type QuestionTypeData questionTypeData = new QuestionTypeData(_db); var dataQuestionTypeModels = await questionTypeData.GetQuestionTypeById(DisplayQuestionModel.QuestionTypeId); DisplayQuestionTypeModel = new DisplayQuestionTypeModel { Id = dataQuestionTypeModels[0].Id, QuestionType = dataQuestionTypeModels[0].QuestionType, }; // Getting links List <DisplayLinkQuestionAnswerModel> displayLinkQuestionAnswerModels = new List <DisplayLinkQuestionAnswerModel>(); LinkQuestionAnswerData linkQuestionAnswerData = new LinkQuestionAnswerData(_db); List <DataLinkQuestionAnswerModel> dataAccessLinkQuestionAnswerModels = await linkQuestionAnswerData.GetLinkedAnswers(id); foreach (DataLinkQuestionAnswerModel dataAccessLinkQuestionAnswerModel in dataAccessLinkQuestionAnswerModels) { displayLinkQuestionAnswerModels.Add(new DisplayLinkQuestionAnswerModel { AnswerId = dataAccessLinkQuestionAnswerModel.AnswerId, Id = dataAccessLinkQuestionAnswerModel.Id, QuestionId = dataAccessLinkQuestionAnswerModel.QuestionId, }); } DisplayLinks = displayLinkQuestionAnswerModels; // Getting answers for the dropdown list. List <DisplayAnswerModel> answers = await GetAnswers(); Answers = new SelectList( answers.Select(x => new { Id = x.Id, Answer = $"{x.Answer} ({(x.Type == true ? "Réponse correcte" : "Réponse incorrecte")})", }), "Id", "Answer"); }
/// <summary> /// Add a new answer to the question. /// </summary> /// <returns>Refresh the page.</returns> public async Task <IActionResult> OnPost() { if (!ModelState.IsValid) { return(Page()); } LinkQuestionAnswerData linkQuestionAnswerData = new LinkQuestionAnswerData(_db); DataLinkQuestionAnswerModel newLinkQuestionAnswerModel = new DataLinkQuestionAnswerModel { AnswerId = DisplayLink.AnswerId, Id = DisplayLink.Id, QuestionId = DisplayLink.QuestionId, }; // Getting the question type QuestionData questionData = new QuestionData(_db); var dataQuestionModel = await questionData.GetQuestionById(newLinkQuestionAnswerModel.QuestionId); QuestionTypeData questionTypeData = new QuestionTypeData(_db); var dataQuestionTypeModel = await questionTypeData.GetQuestionTypeById(dataQuestionModel[0].QuestionTypeId); // Getting the question answers List <DisplayAnswerModel> questionAnswers = await GetQuestionAnswers(newLinkQuestionAnswerModel.QuestionId); // Getting the user answer AnswerData answerData = new AnswerData(_db); var dataAnswerModel = await answerData.GetAnswerById(newLinkQuestionAnswerModel.AnswerId); if (dataAnswerModel[0].Type == true) { // If the answer to add is correct and it already exists a correct response in simple and boolean types, we shouldn't add the answer if ((dataQuestionTypeModel[0].QuestionType == "Simple" || dataQuestionTypeModel[0].QuestionType == "Boolean") && questionAnswers.Any(x => x.Type == true)) { return(RedirectToPage("./linkquestiontoanswer", new { id = DisplayLink.QuestionId })); } } await linkQuestionAnswerData.InsertLink(newLinkQuestionAnswerModel); return(RedirectToPage("./linkquestiontoanswer", new { id = DisplayLink.QuestionId })); }