public async Task <IActionResult> OnGetAsync(int QuestionId, string Caller) { ReturnPath = Caller; IdentityUser user = await _userManager.GetUserAsync(User); PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email); if (!PBEUser.IsValidPBEQuestionBuilder()) { return(RedirectToPage("/error", new { errorMessage = "Sorry! You do not have sufficient rights to edit a PBE question" })); } Question = await _context.QuizQuestions.FindAsync(QuestionId); if (Question == null) { return(RedirectToPage("/error", new { errorMessage = "That's Odd! We weren't able to find this Question" })); } // Setup our PBEBook Object Question.BibleId = await QuizQuestion.GetValidBibleIdAsync(_context, Question.BibleId); BibleBook PBEBook = await BibleBook.GetPBEBookAndChapterAsync(_context, Question.BibleId, Question.BookNumber, Question.Chapter); if (PBEBook == null) { return(RedirectToPage("/error", new { errorMessage = "That's Odd! We weren't able to find the PBE Book." })); } Question.PopulatePBEQuestionInfo(PBEBook); Question.Verses = await Question.GetBibleVersesAsync(_context, false); // We need an answer text, and while techincally we support multiple Answers // we are only going to allow operating on the first one in this basic edit experience. await _context.Entry(Question).Collection(Q => Q.QuizAnswers).LoadAsync(); if (Question.QuizAnswers.Count > 0) { AnswerText = Question.QuizAnswers.OrderBy(A => A.Id).First().Answer; } else { AnswerText = ""; } IsCommentary = (Question.Chapter == Bible.CommentaryChapter); if (IsCommentary == false) { ChapterQuestionCount = PBEBook.BibleChapters.Where(c => c.ChapterNumber == Question.Chapter).First().QuestionCount; } CommentaryQuestionCount = PBEBook.CommentaryQuestionCount; // and now we need a Verse Select List ViewData["VerseSelectList"] = new SelectList(Question.Verses, "Verse", "Verse"); ViewData["PointsSelectList"] = Question.GetPointsSelectList(); return(Page()); }
public async Task <IActionResult> OnGetAsync(string BibleId, int BookNumber, int Chapter, int?Verse) { IdentityUser user = await _userManager.GetUserAsync(User); PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email); // Static method not requiring an instance this.BookNumber = BookNumber; this.Chapter = Chapter; this.Verse = Verse ?? 0; // set to 0 if Verse is null this.BibleId = await QuizQuestion.GetValidBibleIdAsync(_context, BibleId); BibleBook PBEBook = await BibleBook.GetPBEBookAndChapterAsync(_context, this.BibleId, this.BookNumber, this.Chapter); if (PBEBook == null) { return(RedirectToPage("/error", new { errorMessage = "That's Odd! We weren't able to find the PBE Book." })); } // Handle the possibility that we want only one verse. var questions = from q in _context.QuizQuestions select q; if (!Verse.HasValue) { questions = questions.Where(Q => (Q.BibleId == this.BibleId || Q.BibleId == null) && Q.BookNumber == BookNumber && Q.Chapter == Chapter && Q.IsDeleted == false); } else { questions = questions.Where(Q => (Q.BibleId == this.BibleId || Q.BibleId == null) && Q.BookNumber == BookNumber && Q.Chapter == Chapter && Q.EndVerse == Verse && Q.IsDeleted == false); } Questions = await questions.Include(Q => Q.QuizAnswers) .OrderBy(Q => Q.EndVerse) .ToListAsync(); foreach (QuizQuestion Question in Questions) { Question.PopulatePBEQuestionInfo(PBEBook); Question.CheckUserCanEdit(PBEUser); } IsCommentary = (this.Chapter == Bible.CommentaryChapter); if (IsCommentary) { this.BookName = PBEBook.CommentaryTitle; } else { this.BookName = PBEBook.Name; } return(Page()); }
public async Task <ActionResult <MinQuestion> > GetQuizQuestion(int id) { var quizQuestion = await _context.QuizQuestions.FindAsync(id); if (quizQuestion == null) { return(NotFound()); } // Explicit load our answers. await _context.Entry(quizQuestion).Collection(q => q.QuizAnswers).LoadAsync(); BibleBook PBEBook = await BibleBook.GetPBEBookAndChapterAsync(_context, quizQuestion.BibleId, quizQuestion.BookNumber, quizQuestion.Chapter); quizQuestion.PopulatePBEQuestionInfo(PBEBook); MinQuestion minQuestion = new MinQuestion(quizQuestion); return(minQuestion); }
public async Task <IActionResult> OnGetAsync(string BibleId, int BookNumber, int Chapter, int?VerseNum, bool?BuildQuestion) { IdentityUser user = await _userManager.GetUserAsync(User); PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email); // Static method not requiring an instance if (!PBEUser.IsValidPBEQuestionBuilder()) { return(RedirectToPage("/error", new { errorMessage = "Sorry! You do not have sufficient rights to add a PBE question" })); } bool generateQuestion = false; if (BuildQuestion.HasValue) { generateQuestion = (bool)BuildQuestion; } Question = new QuizQuestion(); // Setup our PBEBook Object Question.BibleId = await QuizQuestion.GetValidBibleIdAsync(_context, BibleId); if (generateQuestion) { BibleVerse verse = await BibleVerse.GetVerseAsync(_context, Question.BibleId, BookNumber, Chapter, (int)VerseNum); Question = await Question.BuildQuestionForVerseAsync(_context, verse, 10, Question.BibleId); foreach (QuizAnswer Answer in Question.QuizAnswers) { AnswerText += Answer.Answer; } } else { Question.BookNumber = BookNumber; Question.Chapter = Chapter; Question.StartVerse = VerseNum ?? 1; // set to 1 if VersNum is Null. Question.EndVerse = VerseNum ?? 1; // set to 1 if VersNum is Null. Question.Points = 0; } BibleBook PBEBook = await BibleBook.GetPBEBookAndChapterAsync(_context, Question.BibleId, Question.BookNumber, Question.Chapter); if (PBEBook == null) { return(RedirectToPage("/error", new { errorMessage = "That's Odd! We weren't able to find the PBE Book." })); } Question.PopulatePBEQuestionInfo(PBEBook); Question.Verses = await Question.GetBibleVersesAsync(_context, false); IsCommentary = (Question.Chapter == Bible.CommentaryChapter); if (IsCommentary == false) { ChapterQuestionCount = PBEBook.BibleChapters.Where(c => c.ChapterNumber == Question.Chapter).First().QuestionCount; } CommentaryQuestionCount = PBEBook.CommentaryQuestionCount; // and now we need a Verse Select List ViewData["VerseSelectList"] = new SelectList(Question.Verses, "Verse", "Verse"); ViewData["PointsSelectList"] = Question.GetPointsSelectList(); return(Page()); }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { // Setup our PBEBible Object Question.BibleId = await QuizQuestion.GetValidBibleIdAsync(_context, Question.BibleId); BibleBook PBEBook = await BibleBook.GetPBEBookAndChapterAsync(_context, Question.BibleId, Question.BookNumber, Question.Chapter); if (PBEBook == null) { return(RedirectToPage("/error", new { errorMessage = "That's Odd! We weren't able to find the PBE Book." })); } Question.PopulatePBEQuestionInfo(PBEBook); Question.Verses = await Question.GetBibleVersesAsync(_context, false); IsCommentary = (Question.Chapter == Bible.CommentaryChapter); if (IsCommentary == false) { ChapterQuestionCount = PBEBook.BibleChapters.Where(c => c.ChapterNumber == Question.Chapter).First().QuestionCount; } CommentaryQuestionCount = PBEBook.CommentaryQuestionCount; // and now we need a Verse and Points Select List ViewData["VerseSelectList"] = new SelectList(Question.Verses, "Verse", "Verse"); ViewData["PointsSelectList"] = Question.GetPointsSelectList(); return(Page()); } // confirm our user is a valid PBE User. IdentityUser user = await _userManager.GetUserAsync(User); if (User != null) { PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email); if (!PBEUser.IsValidPBEQuestionBuilder()) { return(RedirectToPage("/error", new { errorMessage = "Sorry! You do not have sufficient rights to add a PBE question" })); } } else { return(RedirectToPage("/error", new { errorMessage = "Oops! We were unable to get our User Object from the UserManager, this question cannot be added!" })); } // Now let's create an empty question and put only our validated properties onto it. var emptyQuestion = new QuizQuestion { Created = DateTime.Now, Modified = DateTime.Now }; if (await TryUpdateModelAsync <QuizQuestion>( emptyQuestion, "Question", // Prefix for form value. Q => Q.BibleId, Q => Q.Points, Q => Q.BookNumber, Q => Q.Chapter, Q => Q.StartVerse, Q => Q.EndVerse, Q => Q.Question)) { emptyQuestion.Owner = PBEUser.Email; emptyQuestion.Source = "BiblePaths.Net"; _context.QuizQuestions.Add(emptyQuestion); // now we need to add the Answer if there is one. if (AnswerText.Length > 0) { QuizAnswer Answer = new QuizAnswer { Created = DateTime.Now, Modified = DateTime.Now, Question = emptyQuestion, Answer = AnswerText, IsPrimary = true }; _context.QuizAnswers.Add(Answer); // Register that this question has an answer. emptyQuestion.IsAnswered = true; } await _context.SaveChangesAsync(); return(RedirectToPage("AddQuestion", new { BibleId = emptyQuestion.BibleId, BookNumber = emptyQuestion.BookNumber, Chapter = emptyQuestion.Chapter, VerseNum = emptyQuestion.EndVerse })); } else { return(RedirectToPage("/error", new { errorMessage = "Oops! We failed to update the question model, this question cannot be added!" })); } // return RedirectToPage("Index"); }
public async Task <IActionResult> OnGetAsync(string BibleId, int QuizId, string Message) { IdentityUser user = await _userManager.GetUserAsync(User); PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email); // Static method not requiring an instance if (!PBEUser.IsValidPBEQuizHost()) { return(RedirectToPage("/error", new { errorMessage = "Sorry! You do not have sufficient rights to host a PBE Quiz" })); } this.BibleId = await Bible.GetValidPBEBibleIdAsync(_context, BibleId); // Let's grab the Quiz Object Quiz = await _context.QuizGroupStats.FindAsync(QuizId); if (Quiz == null) { return(RedirectToPage("/error", new { errorMessage = "That's Odd... We were unable to find this Quiz" })); } if (Quiz.QuizUser != PBEUser) { return(RedirectToPage("/error", new { errorMessage = "Sorry! Only a Quiz Owner can run a Quiz" })); } _ = await Quiz.AddQuizPropertiesAsync(_context, BibleId); // Now for the Question Object... we're going to take 3 swings at this for the scenario where we don't have enough questions. int iterations = 0; do { Question = await Quiz.GetNextQuizQuestionAsync(_context, BibleId); iterations++; }while (iterations < 3 && Question.QuestionSelected == false); // This is the no questions found scenario if (Question.QuestionSelected == false) { return(RedirectToPage("/error", new { errorMessage = "Sorry! We failed to find a random question after three tries... please add more questions." })); } if (Question.BibleId == null) { Question.BibleId = BibleId; } BibleBook PBEBook = await BibleBook.GetPBEBookAndChapterAsync(_context, BibleId, Question.BookNumber, Question.Chapter); if (PBEBook == null) { return(RedirectToPage("/error", new { errorMessage = "That's Odd! We weren't able to find the PBE Book." })); } Question.PopulatePBEQuestionInfo(PBEBook); Question.Verses = await Question.GetBibleVersesAsync(_context, true); Question.LegalNote = Question.GetBibleLegalNote(); // Build our Select List and set a default points value of -1 to require selection. ViewData["PointsSelectList"] = Question.GetQuestionPointsSelectList(); Question.PointsAwarded = -1; UserMessage = GetUserMessage(Message); return(Page()); }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { // Setup our PBEBible Object Question.BibleId = await QuizQuestion.GetValidBibleIdAsync(_context, Question.BibleId); BibleBook PBEBook = await BibleBook.GetPBEBookAndChapterAsync(_context, Question.BibleId, Question.BookNumber, Question.Chapter); if (PBEBook == null) { return(RedirectToPage("/error", new { errorMessage = "That's Odd! We weren't able to find the PBE Book." })); } Question.PopulatePBEQuestionInfo(PBEBook); Question.Verses = await Question.GetBibleVersesAsync(_context, false); // We should still have AnswerText IsCommentary = (Question.Chapter == Bible.CommentaryChapter); if (IsCommentary == false) { ChapterQuestionCount = PBEBook.BibleChapters.Where(c => c.ChapterNumber == Question.Chapter).First().QuestionCount; } CommentaryQuestionCount = PBEBook.CommentaryQuestionCount; // and now we need a Verse and Points Select List ViewData["VerseSelectList"] = new SelectList(Question.Verses, "Verse", "Verse"); ViewData["PointsSelectList"] = Question.GetPointsSelectList(); return(Page()); } // confirm our user is a valid PBE User. IdentityUser user = await _userManager.GetUserAsync(User); PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email); if (!PBEUser.IsValidPBEQuestionBuilder()) { return(RedirectToPage("/error", new { errorMessage = "Sorry! You do not have sufficient rights to edit a PBE question" })); } // Now let's create an empty question and put only our validated properties onto it. QuizQuestion QuestionToUpdate = await _context.QuizQuestions.FindAsync(Question.Id); if (QuestionToUpdate == null) { return(RedirectToPage("/error", new { errorMessage = "That's Odd! We weren't able to find this Question" })); } if (await TryUpdateModelAsync <QuizQuestion>( QuestionToUpdate, "Question", // Prefix for form value. Q => Q.BibleId, Q => Q.Points, Q => Q.StartVerse, Q => Q.EndVerse, Q => Q.Question, Q => Q.Challenged, Q => Q.ChallengeComment)) { QuestionToUpdate.Modified = DateTime.Now; // now we need to add the Answer if there is one. if (AnswerText.Length > 0) { // We need the Original Answer and while techincally we support multiple Answers // we are only going to allow operating on the first one in this basic edit experience. await _context.Entry(QuestionToUpdate).Collection(Q => Q.QuizAnswers).LoadAsync(); if (QuestionToUpdate.QuizAnswers.Count > 0) { QuizAnswer OriginalAnswer = QuestionToUpdate.QuizAnswers.OrderBy(A => A.Id).First(); if (OriginalAnswer.Answer != AnswerText) { _context.Attach(OriginalAnswer); OriginalAnswer.Modified = DateTime.Now; OriginalAnswer.Answer = AnswerText; QuestionToUpdate.IsAnswered = true; } } } await _context.SaveChangesAsync(); switch (ReturnPath) { case "Questions": return(RedirectToPage("Questions", new { BibleId = QuestionToUpdate.BibleId, BookNumber = QuestionToUpdate.BookNumber, Chapter = QuestionToUpdate.Chapter })); // break; not needed unreachable case "ChallengedQuestions": return(RedirectToPage("ChallengedQuestions", new { BibleId = QuestionToUpdate.BibleId })); // break; not needed unreachable default: return(RedirectToPage("AddQuestion", new { BibleId = QuestionToUpdate.BibleId, BookNumber = QuestionToUpdate.BookNumber, Chapter = QuestionToUpdate.Chapter, VerseNum = QuestionToUpdate.EndVerse })); // break; not needed unreachable } } return(RedirectToPage("Index")); }