コード例 #1
0
        /// <summary>
        /// Checks if the answer is correct
        /// </summary>
        /// <param name="Answer">Answer to validate</param>
        /// <returns>Number of points for this answer depending on current valuation criteria</returns>
        public override int CheckAnswer(Answer Answer)
        {
            if (Answer == null)
            {
                throw new NullReferenceException();
            }

            var TargetAnswer = Answer as MultipleCheckBoxesAnswer;

            // The type should match but need to check it just in case
            if (TargetAnswer == null)
            {
                throw new Exception("Answer type does not match the question type");
            }

            if (TargetAnswer.UserAnswer.Count != CorrectAnswer.Count)
            {
                throw new Exception("Answers count does not match this question");
            }

            if (Answer.IsEmpty())
            {
                return(0);
            }

            var CorrectAnswersNumber = 0;

            // Get how many answers user got right
            for (var i = 0; i < Options.Count; i++)
            {
                if (TargetAnswer.UserAnswer[i] == CorrectAnswer[i])
                {
                    CorrectAnswersNumber++;
                }
            }

            // TODO: DECIDE ON ROUNDING (CEIL OR FLOOR)
            // Based on evaluation mode givem them proper ammount of points
            switch (Scoring.Mode)
            {
            case ScoringMode.FullAnswer:
                return(CorrectAnswersNumber == CorrectAnswer.Count ? Scoring.FullPointScore : 0);

            case ScoringMode.HalfTheAnswer:

                // Less than the half: 0 points
                if (CorrectAnswersNumber < CorrectAnswer.Count / 2)
                {
                    return(0);
                }

                // If the score is between the half and max: half the points
                if (CorrectAnswersNumber < CorrectAnswer.Count)
                {
                    return(Scoring.FullPointScore / 2);
                }

                // Full score: full points
                return(Scoring.FullPointScore);

            case ScoringMode.EvenParts:

                // For each asnwer give a proper ammount of poitns
                // NOTE: Can be done by ceil or floor rounding.
                return(Scoring.FullPointScore * CorrectAnswersNumber / CorrectAnswer.Count);

            default:
                Debugger.Break();
                throw new Exception("Answer check failed. No evaluating mode detected.");
            }
        }