<%@ Page Language="C#" Debug="true"%> Code Samples- Self-scoring Quiz
Microsoft Office FrontPage 2003      
Developer's Toolkit
Resources | Links | Search
  extend design code  
             
  New Features
 
Microsoft® Windows® SharePoint Services
 
Microsoft® Office SharePoint Portal Server
 
Microsoft Office System
 
.NET Framework
 
 
Real world scenarios that fully expose great new functionality in FrontPage 2003 and Windows SharePoint Services.
 
 
  financial planning finance  
  sales & marketing sales & marketing  
  human resources human resources  
   
  FrontPage Customization Kit > Resources > Code Library


Self-scoring Quiz Documentation

Contents

Quiz without CSS styles Example
Quiz with CSS styles Example

Download this sample

 

Summary

The Self-scoring Quiz creates a series of questions and possible answers on a page, and provides a simple technique for evaluating the guesses made.

The questions can be rearranged randomly, as well as the answers for each question. You can decide whether to require that the reader must answer all questions before getting a score. The questions and answers can be automatically numbered with numbers, letters, or bullets. Upon scoring, you can reveal, in line with the questions and answers, the correct answer, as well as an explanation for correct or incorrect guesses. Finally, you can display a final evaluation based on a range of possible scores or a percentage or correct scores.

The Self-scoring Quiz is created in javascript with the Quiz object, and with three kinds of child objects: Question, Answer, and Score objects. The Quiz object contains Question and Score objects, and Question objects contain Answer objects.

Please Note:   The quizzes produced by the Self-scoring Quiz are suitable only for entertainment purposes; they are not suitable for any form of official evaluation, even with intensive customization. This is because the identities of the correct answers are sent to the browser's user in the form of javascript code and HTML. There are many ways for even a modestly technically-savvy user to discover the correct answers without guessing, or to circumvent guessing altogether and supply any score he or she wishes.

 

Usage

There is one required file: quiz.js. Include it on your page with a line like this:

	<script src="quiz.js" type="text/javascript"></script>

To use the quiz object, create a new script block that creates a new Quiz object like this:

	<script type="text/javascript">
	var oQuiz = new Quiz();

This script block is not closed, because you will need to create Question objects to add to it, like this:

	var oQ = new Question(); 
	oQ.SetQuestionHTML("What is 2 + 2 ?");
	oQ.SetQuestionType("MC");

This script accomplishes a few things. First, it creates a new Question object. Then it sets the text for the question to "What is 2 + 2 ?". Finally, it sets the question type to a multiple choice question. It does not add the Question to the Quiz yet; that will be done after you add all the Answer objects to the Question object.

You can set other properties for the Question. The Question, when evaluated, can give feedback based on whether the question was answered correctly or not. Set the text of the feedback like this:

	oQ.SetCorrectGuessHTML("Correct.");
	oQ.SetIncorrectGuessHTML("Incorrect.");

The text displayed by the Quiz contains very little built-in formatting. Read the Reference section to see how to apply CSS class styles to a question. Alternately, you can add HTML or CSS formatting with methods that accept HTML string, such as the SetCorrectGuessHTML or SetIncorrectGuessHTML methods.

Next, you will need to add Answer objects to the Question you created, like this:

	var oA = new Answer(); 
	oA.SetAnswerHTML("Five");
	oQ.AddAnswer(oA); 
	oA = new Answer(); 
	oA.SetAnswerHTML("Four");
	oA.SetCorrect(true);
	oQ.AddAnswer(oA);

This adds two Answer objects, each one created with a call to new Answer(). For each Answer, it sets the text to be displayed as a possible answer with the SetAnswerHTML method. Identify the correct answer with a call to SetCorrect, passing the value true as an argument.  Finally, each Answer is added to the Question with the AddAnswer method of the Question object.

Finally, add the Question, complete with its collection of Answers, to the Quiz, with the AddQuestion method of the Quiz, like this:

	oQuiz.AddQuestion(oQ);

Once of you have added all of the Questions to the Quiz, add Score objects so that the Quiz can automatically display an evaluation, based on the raw score or a percentage of the possible points, like this:

	var oS = new Score();
	oS.SetScoreHTML("Score: 0");
	oS.SetMinScore(0);
	oQuiz.AddScore(oS);

	oS = new Score();
	oS.SetScoreHTML("Score: 1");
	oS.SetMinScore(1);
	oQuiz.AddScore(oS);		

This creates two Score objects and adds them to the Quiz object. The text of the Score is set with the SetScoreHTML method, and the minimum number of points that it takes to get that Score is set with the SetMinScore method. Finally, each Score is added to the Quiz with the AddScore method of the Quiz object.

The Quiz is complete and ready to render, like this:

	oQuiz.Render();
	</script>

This displays a quiz on the page, ready for the user to guess the correct answers.

However, the quiz is not yet complete. The Quiz object does not render a button or any other mechanism to automatically evaluate the user's guesses. This is up to you, but it's simple. Add an HTML button element, and set its onclick method to the Quiz's Evaluate method, like this:

	<input onclick="javascript:oQuiz.Evaluate();" 
          type="button" value="Check Answers">

When the user clicks the button labeled "Check Answers", the Quiz object will evaluate the radio buttons and check boxes for the quiz, determining which ones have been checked, and whether they are the correct answers. Each correct guess will get one point. An unanswered question or an incorrect guess gets no points. Immediately below the question, the CorrectGuessHTML or the IncorrectGuessHTML (whichever is appropriate) is displayed. Below all the questions, the proper Score is displayed.

 

Reference

There are four objects for this feature. The Quiz object is the primary object. It contains a collection of Question objects and a collection of Score objects. Each Question object contains a collection of Answer objects. Score objects are used for ranges of points that are awarded for guessing correct Answers.

Quiz object
Properties

The Quiz object has no public properties.

Methods
Method Parameter Description
AddQuestion   Adds a Question object to the collection of Question objects. If you do not use the SetQuestionNumber method of the Question objects to control the order of questions, then questions will be displayed in the same order in which you added the Question objects.
oQuestion Object. Question object to add to the collection of Question objects.
AddScore   Adds a Score object to the collection of Score objects.
oScore Object. Score object to add to the collection of Score objects.
Evaluate   If the SetRequireAllGuesses method has been called, checks to make sure that the user has made a guess for each question. If not all guesses have been made, it displays the SetNeedAllGuessesHTML message in a DIV, and returns the value false.

Then this method iterates through all Question objects in the Collection collection, and calls the Evaluate method to each Question. It accumulates all the points awarded by each Question, and then uses its own GetPercentage method to calculate the percentage of awarded points out of the total possible points.

Finally, this method determines which Score object to display, displays it, and returns the value true.

GetAwardedPoints   Returns a real number with the number of points awarded for correct answers, based on the PossiblePoints value assigned to each Question or Answer.
GetPercentage   Returns an integer in the range from 0 to 100, representing the percentage of awarded points from the points that are possible.
GetPossiblePoints   Returns an integer with the total of the PossiblePoints values set with the SetPossiblePoints method applied to Question and Answer objects.
HasAllGuesses   Returns a boolean that indicates whether the user has made a guess for all of the questions on the quiz. Returns true if every question has a guess made; returns false if any question has not had a guess made.
RandomizeQuestions   Randomly rearranges all of the questions in the quiz, whether they have been numbered or not. It does not rearrange the answers to the questions.
Render   Draws all of the questions in the quiz, along with all of the possible answers, using all settings applied.
SetNeedAllGuessesCssClass   Sets the CSS class name to use for the DIV element that will be used to display a message to indicate that the user needs to guess at all of the questions before getting the score to the quiz.
sCssClass String specifies the CSS class name to use.
SetNeedAllGuessesHTML   Sets the HTML to use for the DIV element that will be used to display a message to indicate that the user needs to guess at all of the questions before getting the score to the quiz.
sHTML String specifies the HTML snippet to use.
SetQuestionNumberCssClass   Sets the CSS class name to use for the SPAN element that will be used to display the number given to each question.

If you would like to line up the first word of each question separately from the number, apply a width style attribute to the class identified in this method, like this:

<STYLE>
.qnumber { WIDTH: 40px; }
</STYLE>
<script type="text/javascript">
oQuiz.SetQuestionNumberCssClass
("qnumber");
</script>
sCssClass String specifies the CSS class name to use.
SetQuestionNumberStyle   Sets the numbering style to use for questions. The default style is an empty string ("").
sStyle String specifies the numbering style to use. Choose from one of these styles:
NumberStyle Description
1 Numbers questions like this:
1:
2:
3:
...
A Numbers questions like this:
A:
B:
C:
...
a Numbers questions like this:
a:
b:
c:
...
. Places a bullet character (•) before each question.
"" (empty string) Places nothing before a question.
SetRequireAllGuesses   Sets a flag indicating whether all questions must be answered before an evaluation is performed and a score is displayed. The default is false.

Set to true to require the user to guess all questions before evaluating and answering. If set to true, the HTML set with the SetNeedAllGuessesHTML method will be displayed when the Evaluate method is called, until all questions are answered.

Set to false to allow the evaluation to proceed even on unanswered questions. If set to false, unanswered questions are awarded zero points.
bRequireAll Boolean value to set the flag.
SetScoreByPercentage   Sets a flag indicating whether Score objects should be applied against the total AwardedPoints or the percentage of correct answers in the Quiz. The default value is false.

Set to true to match Scores based on the percentage of correct answers. If set to true, the SetMinScore method of Score objects should be set to the lowest percentage of correct answers that the user must guess in order to get that Score.

Set to false to match Scores based on the total points awarded for correct answers. If set to false, the SetMinScore method of Score objects should be set to the lowest points that must be awarded in order to get that score,

bByPercentage Boolean value to set the flag.
SetScoreCssClass   Sets the CSS class name to use for the DIV element that will be used to display the final evaluated score HTML.
sCssClass String specifies the CSS class name to use.
Question object

The Question object supports three types of questions:

  • True/False questions allow only two possible Answer objects. The user must guess exactly one Answer.
  • Multiple-Choice questions allow as many Answer objects as you want. The user must guess exactly one Answer.
  • Multiple-Select questions allow as many Answer objects as you want. The user must guess at least one Answer, but can guess as many Answer objects there are.
Properties

The Question object has no public properties.

Methods
Method Parameter Description
AddAnswer   Adds an Answer object to the collection of Answer objects. If you do not use the SetAnswerNumber method of the Answer objects to control the order of answers, then answers will be displayed in the same order in which you added the Answer objects.
oAnswer Answer object to add to the collection of Answer objects.
Evaluate   Checks the guess or guesses made by the user and sets the AwardedPoints and Percentage values. This method does not change any of the displayed elements on the page. Use the GetAwardedPoints method to get the get the result of the evaluation.
GetAwardedPoints   Returns a real number with the points awarded for the guess(es) made by the user. This number can be an integral or fractional number, between 0 and the number provided by the GetPossiblePoints method. See the GetPossiblePoints method for its description of how the total possible points for a question are determined.

For true/false and multiple-choice questions, the number returned can be either zero or the value returned by the GetPossiblePoints method.

For multiple-selection questions, the number is determined by adding all to the points assigned to the correct Answer objects, then dividing the sum of points for correctly guessed Answer objects the sum of possible points from the sum of all correct Answers, then multiplying that result by the value returned by GetPossiblePoints.

For example, suppose you have a multiple-selection question with three Answers, two of which are correct. Each correct Answer is assigned two points. However, the Question has been assigned a maximum of five points with the SetPossiblePoints method. If the user selects one correct answer, then GetAwardedPoints will return 2.5 points. Here's how it determines that:

Sum of points for all correct Answers: 2 + 2 = 4
Sum of points for correctly guessed Answers: 2
Value set by SetPossiblepoints: 5
First result: 2 / 4 = 0.5
Final result: 0.5 * 5 = 2.5

GetPossiblePoints   Returns an integer with the points that can possibly be awarded for guessing the correct answer or answers for the question.

If you call the SetPossiblePoints method to set the points possible, this method will return that number. Otherwise, this method calculates the possible points from the collection of Answer questions, depending on the type of Question.

For true/false and multiple-choice questions, this method returns the points for the correct Answer that has the most possible points assigned to it. Normally, only one Answer should be identified as the correct answer. Answers that have not had any possible points assigned will default to one possible point.

For multiple-selection questions, this method returns the sum of all points assigned to correct Answer objects.

HasGuess   Returns a boolean that indicates whether the user has made a guess at one or more of the answers for the Question. It returns true if any Answer has been selected, and it returns false if no Answer has been selected.
RandomizeAnswers   Randomly rearranges all of the answers for this Question, whether they have been numbered or not. It does not rearrange answers for any other Question.
SetAnswerNumberCssClass   Sets the CSS class name to use for the SPAN element that will be used to display the number given to each answer.

If you would like to line up the first word of each answer, apply a width style attribute to the class identified in this method, like this:

<STYLE>
.anumber { WIDTH: 40px; }
</STYLE>
<script type="text/javascript">
oQuestion.SetAnswerNumberCssClass
("anumber");
</script>
sCssClass String specifies the CSS class name to use.
SetAnswerNumberStyle   Sets the numbering style to use for answers. The default style is an empty string ("").
sStyle String specifies the numbering style to use. Choose from one of these styles:
NumberStyle Description
1 Numbers answers like this:
1:
2:
3:
...
A Numbers answers like this:
A:
B:
C:
...
a Numbers answers like this:
a:
b:
c:
...
. Places a bullet character (•) before each answer.
"" (empty string) Places nothing before an answer.
SetCorrectGuessCssClass   Sets the CSS class name to use for the DIV element that will be used to display a message to indicate that the guess made was correct.
sCssClass String specifies the CSS class name to use.
SetCorrectGuessHTML   Sets the HTML to use for the DIV element that will be used to display a message to indicate that the guess made was correct.

Often, this message not only states that the guess was correct, but provides trivia or references about the question and answer. These details are up to you to provide.

sHTML String specifies the HTML snippet to use.
SetIncorrectGuessCssClass   Sets the CSS class name to use for the DIV element that will be used to display a message to indicate that the guess made was not correct.
sCssClass String specifies the CSS class name to use.
SetIncorrectGuessHTML   Sets the HTML to use for the DIV element that will be used to display a message to indicate that the guess made was not correct.

Often, this message not only states that the guess was incorrect, but provides an explanation about which answer was correct, and why.

sHTML String specifies the HTML snippet to use.
SetPossiblePoints   Sets the maximum points that can be awarded for guessing the correct answer or answers. This can be used to scale correctly guessed Answers for the GetAwardedPoints method.
nPoints Integer specifies the maximum points possible.
SetQuestionCssClass   Sets the CSS class name to use for the DIV element that will be used to display the question itself.
sCssClass String specifies the CSS class name to use.
SetQuestionHTML   Sets the HTML to use for the DIV element that will be used to display the question.
sHTML String specifies the HTML snippet to use.
SetQuestionNumber   Sets the order of appearance for this Question when the Quiz object renders the list of questions. This ordering is ignored if the RandomizeQuestions method of the Quiz object is used. If this method is not used, Questions will be listed in the order they were added to the Quiz object with the AddQuestion method. Do not use the same number for more than one Question.
nNumber Integer specifies the position of the Question in the list.
SetQuestionType   Sets the type of question: true/false, multiple-choice, or multiple-selection.
sType String specifies a code for the type of question.
Code Description
TF True/False.
MC Multiple Choice
MS Multiple Selection
SetRequireAllOrNothing   Sets a flag that indicates whether, for multiple-selection questions only, all correct answers must be guessed in order to be awarded any points, or whether some correct guesses will count for partial points. Regardless of this setting, any guess of an incorrect answer will yield zero points awarded.
bAllOrNothing Boolean specifies whether to require that all correct answers must be guessed. Set to true to require all guesses to be made in order to be awarded any points. Set to false to allow some correct guesses to count for partial points.
Answer object
Properties

The Answer object has no public properties.

Methods
Method Parameter Description
SetAnswerCssClass   Sets the CSS class name to use for the DIV element that will be used to display the answer itself.
sCssClass String specifies the CSS class name to use.
SetAnswerHTML   Sets the HTML to use for the DIV element that will be used to display the answer.
sHTML String specifies the HTML snippet to use.
SetAnswerNumber   Sets the order of appearance for this Answer when the Question object renders the list of answers. This ordering is ignored if the RandomizeAnswers method of the Question object is used. If this method is not used, Answers will be listed in the order they were added to the Question object with the AddAnswer method. Do not use the same number for more than one Answer.
nNumber Integer specifies the position of the Answer in the list.
SetCorrect   Sets a flag indicating whether this Answer is a correct answer to the question. By default, Answer objects are not correct answers.
bCorrect Boolean specifies whether this question is correct. Set to true to indicate that this Answer is a correct answer. Set to false to indicate that this Answer is not a correct answer.
SetPointsForCorrectGuess   Sets the possible points that can be applied for correctly guessing this Answer, if it is a correct answer. This number is used by the GetAwardedPoints method of the Question object to determine the points to award for guessing the correct answers to the question.
nPoints Integer specifies the points that can be applied for this answer.
Score object

A collection of Score objects is used to group ranges of points that can be awarded for a Quiz.

Properties

The Score object has no public properties.

Methods
Method Parameter Description
SetMinScore   Sets the minimum number of awarded points or the minimum percentage of awarded points from the total possible points that the user must get in order to have this Score displayed when the Quiz is evaluated. See the SetScoreByPercentage method of the Quiz object.
rPercentageOrPoints Real number that represents the minimum awarded points or the the minimum percentage of awarded points from the total possible points.
SetScoreCssClass   Sets the CSS class name to use for the DIV element that will be used to display the score after all Question objects have been evaluated.
sCssClass String specifies the CSS class name to use.
SetScoreHTML   Sets the HTML to use for the DIV element that will be used to display the score after all Questions
sHTML String specifies the HTML snippet to use.


 

Remarks

Formatting

The default Quiz will render itself with very little formatting, and will probably space the questions and answers too close together. As a minimum, you will probably want to call the SetQuestionCssClass method to assign the question text to a CSS class with attributes that include the margin-top attribute.

Prevent multiple evaluations

The Evaluate method returns a boolean value that indicates whether the quiz was succesfully evaluated. It returns false if the user needs to answer more questions. If it returns false, then the user has answered all of the questions. If you used a button to initiate the evaluation, then you may want to disable that button, so as to prevent the user from changing her or his guesses, and re-evaluating the quiz. There are two simple ways to do this.

To make the button appear disabled, use a code snippet like this to create the button:

	<input id="btnCheck" onclick="javascript:CheckAnswers();" 
          type="button" value="Check Answers">
	<script type="text/javascript">
	function CheckAnswers()
	{
	if (oQuiz1.Evaluate())
		document.getElementById("btnCheck").disabled = 
         true;
	}
	</script>

To make the button disappear completely, use a code snippet like this to create the button:

	<input id="btnCheck" onclick="javascript:CheckAnswers();" 
          type="button" value="Check Answers">
	<script type="text/javascript">
	function CheckAnswers()
	{
	if (oQuiz1.Evaluate())
		document.getElementById("btnCheck")
         .style.display = "none";
	}
	</script>
Exact points

The Score objects do not automatically reveal the exact points awarded, or the possible points that were calculated, or the percentage. However, those values can easily be retrieved from the Quiz, and then placed inside the Score's HTML, like this:

	<script type="text/javascript">
	...
	... other code to create the Quiz objects
	...
	oScore.SetScoreHTML("Actual score: 
           <span id='actualscore'></span><br>" + 
		"Possible score: <span id='possiblescore'>
            </span><br>" +
		"Percentage: <span id='percentage'></span>");
	...
	... other code to Render the Quiz
	...
	</script>

	<input id="btnCheck" onclick="javascript:CheckAnswers();" 
         type="button" value="Check Answers">

	<script type="text/javascript">
	function CheckAnswers()
	{
	oQuiz.Evaluate();
	document.getElementById("actualscore").innerText = 
         oQuiz.GetAwardedPoints();
	document.getElementById("possiblescore").innerText = 
         oQuiz.GetPossiblePoints();
	document.getElementById("percentage").innerText = 
         oQuiz.GetPercentage();
	}
	</script>
 

Customization

You can achieve a great deal of customization by applying more sophisticated HTML and CSS attributes to the ...HTML and ...CssClass methods. Experiment.

There are no methods provided to determine which guess the user made if the guess was incorrect. This could be useful if you want to customize the Score messages to provide specific guidance based on the actual (incorrect) guess made. The function Quiz_HasGuess has code that iterates through the HTML elements that correspond to the Answer objects for a Question. The checked property for the HTML elements indicates whether the user guessed that Answer.

The function Quiz_RenderAnswer writes out the <input> elements for the Answers for each question. If you want to handle the onclick event for the radio button and checkbox controls displayed for each Answer, the Quiz_RenderAnswer function is the right place to do it.


^ Back to top