示例#1
0
        //gets the answer options for a given question id
        private void SetAnswerOptionsForQuestion(quiz_dbContext _context)
        {
            //initialize list of answer options
            var dbAnswers = new List <Answer>();

            //get the answer options for this question from the DB
            dbAnswers = (from c in _context.Answers select c).Where(p => p.QuestionId == id).ToList();

            //throw an exception if no answers were found
            if (dbAnswers.Count == 0)
            {
                throw new Exception("No answer options could be found for question id " + id);
            }

            answers = new List <AnswerDisplay>();

            foreach (Answer a in dbAnswers)
            {
                //define the properties of each viewmodel
                AnswerDisplay currentAnswer = new AnswerDisplay();
                currentAnswer.answerText = a.AnswerText;
                currentAnswer.id         = a.Id;
                answers.Add(currentAnswer);
            }
        }
示例#2
0
        //intializes the next question object, whether there is a question left or just the score
        public void ShowNextQuestionOrScore(quiz_dbContext _context)
        {
            using (_context)
            {
                //get first question that has not been answered
                Question Question = _context.Questions.FirstOrDefault(q => q.IsComplete == false);
                //if the question exists, then fetch the corresponding answers
                if (Question != null)
                {
                    questionText = Question.QuestionText;
                    id           = Question.Id;

                    //get answers associated with question
                    SetAnswerOptionsForQuestion(_context);
                    shuffleAnswers();
                }
                //if there is no answered question, the quiz is finished, so calculate the user score
                else
                {
                    //get all user selected answers options

                    var dbAnswers     = (from c in _context.Answers select c).Where(p => p.WasSelected == true).ToList();
                    int questionCount = _context.Questions.Count(); //get number of questions answered

                    //calculate the percentage
                    CalculatePercentage(dbAnswers, questionCount);
                }
            }
            //return currentQuestion;
        }
示例#3
0
 public HomeController(quiz_dbContext context)
 {
     //make the DB context available for all methods of HomeController
     _context = context;
 }