Example #1
0
        public IActionResult View(string formId, Guid answersSetId)
        {
            // Try to get the quiz form
            Form form = _formsRepository.GetById(formId);

            if (form == null)
            {
                return(NotFound());
            }

            // Try to get the answers set
            FormAnswersSet answerSet = _answersRepository.Get(formId, answersSetId);

            if (answerSet == null)
            {
                return(NotFound());
            }

            // Try to get the existing scores
            Dictionary <string, int> scores = _scoresRepository.GetScore(formId, answersSetId);

            // Combine the data into a extended form answers model
            ExtendedFormAnswersSet model = AnswerChecking.ConstructExtendedFormAnswersSet(answerSet, form, scores);

            return(View(model));
        }
Example #2
0
        /// <summary>
        /// Create a set with answers
        /// </summary>
        /// <param name="formId">form id</param>
        /// <param name="team">team name</param>
        /// <param name="answers">answers dictionary</param>
        /// <returns>form answers set</returns>
        public Guid Create(string formId, string team, Dictionary <string, string> answers)
        {
            // Combine the data into a model
            FormAnswersSet model = new FormAnswersSet()
            {
                Id          = Guid.NewGuid(),
                Form        = formId,
                Team        = team,
                Answers     = answers,
                DateCreated = DateTime.UtcNow
            };

            // Get the path for the answers directory of the round.
            // Create it if it does not exist yet.
            string formAnswersPath = Path.Combine(AnswersPath, formId);

            if (!Directory.Exists(formAnswersPath))
            {
                Directory.CreateDirectory(formAnswersPath);
            }

            // Serialize the data to JSON
            string json = JsonConvert.SerializeObject(model, Formatting.Indented);

            // Construct the filename for the new form answers
            string filename = Path.Combine(formAnswersPath, string.Format("{0}.json", model.Id));

            // Write the JSON to the new forms answers file
            File.WriteAllText(filename, json);

            // Return the unqiue id of the new form answers
            return(model.Id);
        }
Example #3
0
        public static ExtendedFormAnswersSet ConstructExtendedFormAnswersSet(FormAnswersSet answerSet, Form form, Dictionary <string, int> scores)
        {
            // Create the model, it will be updated later
            ExtendedFormAnswersSet model = new ExtendedFormAnswersSet(answerSet);

            // Loop through the questions of the form
            foreach (Question question in form.Questions)
            {
                // Increase the total points counter
                model.TotalPoints += question.Points;

                // Get the given answer for the question if any
                string given = null;
                if (answerSet.Answers.ContainsKey(question.Id))
                {
                    given = answerSet.Answers[question.Id];
                }

                // Try to get the existing score;
                // Try to perform an automated check otherwise.
                int?score;
                if (scores != null && scores.ContainsKey(question.Id))
                {
                    score = scores[question.Id];
                }
                else
                {
                    score = TryCheckAnswer(question, given);
                }

                // Add the score to the total points if it's set;
                // Otherwise, increase the manual checking required.
                if (score.HasValue)
                {
                    model.Points += score.Value;
                }
                else
                {
                    model.ManualCheckingRequiredCount++;
                }

                // Add the answer to the list
                model.Answers.Add(new ExtendedAnswer(question, given, score));
            }

            return(model);
        }