Example #1
0
    public static QuizGrade ComputeGrade(QuizRuntimeTemplate runtimeTemplate, int[] answers)
    {
        // Determine if you pass or fail important categories
        bool passImportant = PassedImportantQuestions(runtimeTemplate, answers);

        // If you pass important, go on to check if you pass unimportant too
        if (passImportant)
        {
            bool passUnimportant = PassedUnimportantQuestions(runtimeTemplate, answers);

            // If you pass both, your score is excellent
            if (passUnimportant)
            {
                return(QuizGrade.Excellent);
            }
            // If you pass only important categories your score is acceptable
            else
            {
                return(QuizGrade.Acceptable);
            }
        }
        // If you fail important categories your grade is poor
        else
        {
            return(QuizGrade.Poor);
        }
    }
Example #2
0
 private void CheckRuntimeTemplate(QuizTemplate template)
 {
     if (runtimeTemplate == null || runtimeTemplate.Template != template)
     {
         runtimeTemplate = new QuizRuntimeTemplate(template);
     }
 }
Example #3
0
    public QuizInstance(QuizTemplate template, params QuizQuestion[] additionalQuestions)
    {
        // Assign the template
        this.template = template;
        // Create a new runtime template
        runtimeTemplate = new QuizRuntimeTemplate(template, additionalQuestions);

        // Create an answer for each question
        answers = Enumerable.Repeat(-1, runtimeTemplate.Questions.Length).ToArray();
    }
Example #4
0
    public static int ComputeScoreInImportantCategories(QuizRuntimeTemplate runtimeTemplate, int[] answers)
    {
        // Get the score itemized by category
        ItemizedQuizScore itemizedScore = ComputeItemizedScore(runtimeTemplate, answers);
        int score = 0;

        // Add up the scores in the important categories
        foreach (QuizCategory category in runtimeTemplate.Template.ImportantCategories)
        {
            score += itemizedScore.GetOrElse(category, 0);
        }
        return(score);
    }
Example #5
0
    public static ItemizedQuizScore ComputeItemizedScore(QuizRuntimeTemplate runtimeTemplate, int[] answers)
    {
        ItemizedQuizScore itemizedScore = new ItemizedQuizScore();

        for (int i = 0; i < runtimeTemplate.Questions.Length; i++)
        {
            // Get the current question and answer
            QuizQuestion question = runtimeTemplate.Questions[i];
            int          answer   = answers[i];

            // If the answer is within range of the options
            // then add the option's weight to the total score
            if (answer >= 0 && answer < question.Options.Length)
            {
                QuizOption option       = question.Options[answer];
                int        currentScore = itemizedScore.GetOrElse(question.Category, 0);
                itemizedScore.Set(question.Category, currentScore + option.Weight);
            }
        }

        return(itemizedScore);
    }
Example #6
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        // Get the sub-properties
        SerializedProperty template = property.FindPropertyRelative(nameof(template));
        SerializedProperty answers  = property.FindPropertyRelative(nameof(answers));

        // Set out the foldout
        property.isExpanded = EditorGUIAuto.Foldout(ref position, property.isExpanded, label);

        if (property.isExpanded)
        {
            // Increase indent
            EditorGUI.indentLevel++;

            // Layout the template
            EditorGUIAuto.PropertyField(ref position, template);

            // If an object reference exists then layout the answers
            if (template.objectReferenceValue)
            {
                // Get the quiz template
                QuizTemplate quizTemplate = template.objectReferenceValue as QuizTemplate;
                CheckRuntimeTemplate(quizTemplate);

                // Use a button to regenerate quiz questions from the template
                GUI.enabled = quizTemplate.Dynamic;
                if (GUIAuto.Button(ref position, "Generate new questions"))
                {
                    runtimeTemplate = new QuizRuntimeTemplate(quizTemplate);
                }
                GUI.enabled = true;

                quizAnswersEditor.OnGUI(position, answers, runtimeTemplate.Questions);
                position.y += quizAnswersEditor.GetPropertyHeight(answers, runtimeTemplate.Questions);

                // Get a list of all the answers as integers
                int[] answersArray = Answers(property);
                resultsFoldout = EditorGUIAuto.Foldout(ref position, resultsFoldout, "Quiz Results");

                if (resultsFoldout)
                {
                    // Indent the next part and disable it
                    EditorGUI.indentLevel++;
                    GUI.enabled = false;

                    // Compute if the important questions passed, what the score is, and what the max score was
                    bool   passed      = QuizInstance.PassedImportantQuestions(runtimeTemplate, answersArray);
                    int    score       = QuizInstance.ComputeScoreInImportantCategories(runtimeTemplate, answersArray);
                    int    maxScore    = runtimeTemplate.GetMaximumPossibleScoreInImportantCategories();
                    string scoreString = $"{score} / {maxScore} - {(passed ? "Pass" : "Fail")}";

                    // Show the score on important questions
                    EditorGUIAuto.PrefixedLabelField(ref position, new GUIContent("Score on Important Questions"), scoreString);

                    // Compute if the unimportant questions passed, what the score is, and what the max score was
                    passed      = QuizInstance.PassedUnimportantQuestions(runtimeTemplate, answersArray);
                    score       = QuizInstance.ComputeScoreInUnimportantCategories(runtimeTemplate, answersArray);
                    maxScore    = runtimeTemplate.GetMaximumPossibleScoreInUnimportantCategories();
                    scoreString = $"{score} / {maxScore} - {(passed ? "Pass" : "Fail")}";

                    // Show the score on unimportant questions
                    EditorGUIAuto.PrefixedLabelField(ref position, new GUIContent("Score on Unimportant Questions"), scoreString);

                    // Show the final grade
                    EditorGUIAuto.PrefixedLabelField(ref position, new GUIContent("Final Grade"),
                                                     QuizInstance.ComputeGrade(runtimeTemplate, answersArray).ToString());

                    // Change the values back to before
                    EditorGUI.indentLevel--;
                    GUI.enabled = true;
                }
            }

            // Restore indent
            EditorGUI.indentLevel--;
        }
    }
Example #7
0
 public static bool PassedUnimportantQuestions(QuizRuntimeTemplate runtimeTemplate, int[] answers) => runtimeTemplate.Template.GradingRubric
 .PassedUnimportantQuestions(
     ComputeScoreInUnimportantCategories(runtimeTemplate, answers),
     runtimeTemplate.GetMaximumPossibleScoreInUnimportantCategories());
Example #8
0
 public static int ComputeScoreInUnimportantCategories(QuizRuntimeTemplate runtimeTemplate, int[] answers)
 {
     return(ComputeItemizedScore(runtimeTemplate, answers).TotalScore - ComputeScoreInImportantCategories(runtimeTemplate, answers));
 }
Example #9
0
 public static bool ComputeCompleted(QuizRuntimeTemplate runtimeTemplate, int[] answers) => ComputeQuestionsAnswered(runtimeTemplate, answers) >= runtimeTemplate.Questions.Length;
Example #10
0
 // Made these methods public static so that the property drawer can get the info to display in the inspector window
 public static int ComputeQuestionsAnswered(QuizRuntimeTemplate runtimeTemplate, int[] answers) => answers
 // Include the index in the enumeration
 .WithIndex()
 // Count every answer greater than zero and less than the number of options for the question with the same index
 .Count(answer => answer.item >= 0 && answer.item < runtimeTemplate.Questions[answer.index].Options.Length);