Esempio n. 1
0
        // 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)
            {
                ViewData["BookSelectList"] = await BibleBook.GetBookAndBookListSelectListAsync(_context, BibleId);

                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 add a PBE Quiz Template" }));
            }

            // Now let's create an empty template
            var emptyTemplate = new PredefinedQuiz
            {
                Created  = DateTime.Now,
                Modified = DateTime.Now,
                QuizUser = PBEUser
            };

            if (await TryUpdateModelAsync <PredefinedQuiz>(
                    emptyTemplate,
                    "Template", // Prefix for form value.
                    t => t.QuizName, t => t.BookNumber, t => t.NumQuestions))
            {
                emptyTemplate.QuizName  = Name; // Name is handled seperately for remote validation to work.
                emptyTemplate.IsDeleted = false;
                _context.PredefinedQuizzes.Add(emptyTemplate);
                await _context.SaveChangesAsync();

                return(RedirectToPage("./ConfigureTemplate", new { Id = emptyTemplate.Id, BibleId = this.BibleId }));
            }

            return(RedirectToPage("./Templates", new { Message = String.Format("Quiz Template {0} successfully created...", emptyTemplate.QuizName) }));
        }
        public async Task <IActionResult> OnGetAsync(string BibleId)
        {
            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 add a PBE Quiz" }));
            }

            this.BibleId = await Bible.GetValidPBEBibleIdAsync(_context, BibleId);

            //Initialize Select Lists
            ViewData["BookSelectList"] = await BibleBook.GetBookAndBookListSelectListAsync(_context, BibleId);

            ViewData["TemplateSelectList"] = await PredefinedQuiz.GetTemplateSelectListAsync(_context, PBEUser);

            return(Page());
        }
Esempio n. 3
0
        // 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(int Id)
        {
            IdentityUser user = await _userManager.GetUserAsync(User);

            PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email);

            PredefinedQuiz TemplateToUpdate = await _context.PredefinedQuizzes.FindAsync(Id);

            if (TemplateToUpdate == null)
            {
                return(RedirectToPage("/error", new { errorMessage = "Thats Odd! We were unable to find this Quiz Template" }));
            }

            if (!PBEUser.IsValidPBEQuestionBuilder() || PBEUser != TemplateToUpdate.QuizUser)
            {
                return(RedirectToPage("/error", new { errorMessage = "Sorry! You do not have sufficient rights to configure this Quiz Template" }));
            }
            if (TemplateToUpdate.QuizUser != PBEUser)
            {
                return(RedirectToPage("/error", new { errorMessage = "Sorry! Only a Template Owner may edit a Template" }));
            }

            this.BibleId = await Bible.GetValidPBEBibleIdAsync(_context, BibleId);

            _context.Entry(TemplateToUpdate).Collection(T => T.PredefinedQuizQuestions).Load();

            // We need a copy of the Questions list to compare to while the origonal is being updated.
            List <PredefinedQuizQuestion> CompareQuestions = TemplateToUpdate.PredefinedQuizQuestions.ToList();

            if (!ModelState.IsValid)
            {
                //Initialize Our Template Questions
                Questions = TemplateToUpdate.IntiQuestionListForAddEdit();
                // Initialize Template Books
                TemplateBooks = await Template.GetTemplateBooksAsync(_context, this.BibleId);

                JSONBooks = JsonSerializer.Serialize(TemplateBooks);
                // Build Select Lists
                foreach (PredefinedQuizQuestion Question in Questions)
                {
                    Question.AddChapterSelectList(TemplateBooks);
                }
                ViewData["BookSelectList"] = MinBook.GetMinBookSelectListFromList(TemplateBooks);
                return(Page());
            }

            _context.Attach(TemplateToUpdate);
            TemplateToUpdate.Modified = DateTime.Now;

            // Iterate through each of our Questions and make appropriate changes.
            foreach (PredefinedQuizQuestion Question in Questions)
            {
                // See if this is one of our existing Question objects.
                bool ExistingQuestion = true;
                PredefinedQuizQuestion OriginalQuestion = new PredefinedQuizQuestion();
                try
                {
                    OriginalQuestion = CompareQuestions.Where(Q => Q.QuestionNumber == Question.QuestionNumber).Single();
                }
                catch
                {
                    ExistingQuestion = false;
                    // New Question Scenario let's add the Question.
                    if (Question.BookNumber != 0)
                    {
                        PredefinedQuizQuestion QuestionToAdd = new PredefinedQuizQuestion();
                        QuestionToAdd.PredefinedQuiz = TemplateToUpdate;
                        QuestionToAdd.QuestionNumber = Question.QuestionNumber;
                        QuestionToAdd.BookNumber     = Question.BookNumber;
                        QuestionToAdd.Chapter        = Question.Chapter;
                        _context.PredefinedQuizQuestions.Add(QuestionToAdd);
                    }
                }
                if (ExistingQuestion)
                {
                    // Do we need to update OriginalQuestion?
                    if (OriginalQuestion.BookNumber != Question.BookNumber ||
                        OriginalQuestion.Chapter != Question.Chapter)
                    {
                        if (Question.BookNumber != 0)
                        {
                            // this is the update scenario
                            _context.Attach(OriginalQuestion);
                            OriginalQuestion.BookNumber = Question.BookNumber;
                            OriginalQuestion.Chapter    = Question.Chapter;
                        }
                        else
                        {
                            // this is the delete scenario
                            _context.PredefinedQuizQuestions.Remove(OriginalQuestion);
                        }
                    }
                }
                await _context.SaveChangesAsync();
            }
            return(RedirectToPage("./Quizzes", "", new { BibleId = this.BibleId, Message = String.Format("Quiz Template {0} configured successfuly.", TemplateToUpdate.QuizName) }, "Templates"));
        }