use strict; use CGI qw( :standard ); # Note that this starts looking in your top-level W directory require "survey_struct.pl"; require "question_struct.pl"; require "response_struct.pl"; # Creates a new quiz and initializes it with some sample questions. # Input: none # Output: returns a handle to the new quiz (a 'survey' object) sub createDummyQuiz { # Creating a question is two parts # Step 1 -- create a new question object. $q1 is now a handle to that object. my $q1 = &questionCreateNew(); # Step 2 - We have a question, but its values are empty. Now we fill them in. &questionSetType($q1, TYPE_MULTIPLE_CHOICE()); # TYPE_MULTIPLE_CHOICE() is a constant. Notice the needed parentheses. &questionSetPrompt($q1, "When did Columbus first reach America?"); &questionSetChoices($q1, ("1094", "1492", "1776", "1781") ); &questionSetCorrectValue( $q1, 1); &questionSetPoints($q1, 4); # Create a question like the first. my $q2 = &questionCreateNew(); &questionSetType($q2, TYPE_MULTIPLE_CHOICE()); &questionSetPrompt($q2, "True/False: Java and JavaScript are very different languages"); &questionSetChoices($q2, ("True", "False") ); &questionSetCorrectValue( $q2, 0); # Here correct value is integer -- but might be a string for other questions! See below &questionSetPoints($q2, 2); # Create a "free response" question. This one does not have 'choices', because not multiple choice. my $q3 = &questionCreateNew(); &questionSetType($q3, TYPE_FREE_TEXT()); # TYPE_FREE_TEXT() is also a constant. &questionSetPrompt($q3, "What is the capital of Delaware?"); &questionSetCorrectValue( $q3, "Dover"); # Unlike before, the correct value is a string. &questionSetPoints($q3, 2); my $q4 = &questionCreateNew(); &questionSetType($q4, TYPE_FREE_TEXT()); &questionSetPrompt($q4, "What is your favorite color?"); # We've created four questions above, but they aren't linked together in any way. Now we will make a survey # and add them to it. # Step 1 -- Create a new survey object. my $survey = &surveyCreateNew(); # Step 2 -- Add those questions to the survey &surveyAddQuestion($survey, $q1); &surveyAddQuestion($survey, $q2); &surveyAddQuestion($survey, $q3); &surveyAddQuestion($survey, $q4); # Set some options on the survey. &surveySetPassword($survey, "blah1234"); &surveySetDueDate ($survey, "14-Dec-2005 09:00"); return $survey; } # Creates a new survey and initializes it with some sample questions. # Input: none # Output: returns a handle to the new survey sub createDummySurvey { # Create first question. This is a survey, not a quiz, so all of these questions are ungraded (have no CorrectValue) my $q1 = &questionCreateNew(); &questionSetType($q1, TYPE_MULTIPLE_CHOICE()); &questionSetPrompt($q1, "Was this project..."); &questionSetChoices($q1, ("Way too easy", "Little too easy", "About right", "Too hard", "Way too hard") ); my $q2 = &questionCreateNew(); &questionSetType($q2, TYPE_MULTIPLE_CHOICE()); &questionSetPrompt($q2, "How much did you learn from this project?"); &questionSetChoices($q2, ("A lot", "Good amount", "Fair amount", "A little", "Not much") ); my $q3 = &questionCreateNew(); &questionSetType($q3, TYPE_FREE_TEXT()); &questionSetPrompt($q3, "What was the best part of this project?"); my $q4 = &questionCreateNew(); &questionSetType($q4, TYPE_FREE_TEXT()); &questionSetPrompt($q4, "What was the worst part of this project?"); # Create a new survey, and add the questions to it my $survey = &surveyCreateNew(); &surveyAddQuestion($survey, $q1); &surveyAddQuestion($survey, $q2); &surveyAddQuestion($survey, $q3); &surveyAddQuestion($survey, $q4); &surveySetPassword($survey, "blah1234"); &surveySetIsAnon ($survey, "1"); return $survey; } # Prints out a survey # Input: a 'survey' object # Output: prints out info about the survey. Doesn't return anything. sub printSurvey { my ($survey) = @_; # read in the argument # Get basic info about the survey. First we need to know how many questions there are. my $numQuestions = &surveyGetNumQuestions($survey); print "There are $numQuestions questions."; # Print due date, if there is one my $dueDate = &surveyGetDueDate($survey); if ($dueDate) { print "\n
Survey due date is $dueDate."; } # Print password, if there is one my $password = &surveyGetPassword($survey); if ($password) { print "\n
Survey password: $password"; } print "\n
Anonymous: " . &surveyGetIsAnon($survey); # Loop through each question, printing out values for each. for (my $ii=0; $ii < $numQuestions; $ii++) { # Get handle to question number 'ii' my $question = &surveyGetQuestion($survey, $ii); # Now that we've got the question, print basic info on it my $type = &questionGetType($question); print "\n
Question $ii: type " . $type; # this will be a number corresponding to multiple choice or free response print "\n
Prompt: " . &questionGetPrompt($question); # Print the choices if there are any my @choices = &questionGetChoices($question); if (@choices) { print "\n
Choices:" . join (", ", @choices); } # Print the correct value if there is any my $correctValue = &questionGetCorrectValue($question); # $correctValue might be a number or a string. Let's ensure it is a # string and compare as such so we don't get a warning on comparing it. my $noCorrectAnswer = NO_CORRECT_ANSWER(); if ("$correctValue" ne "$noCorrectAnswer" ) { print "\n
Correct value: $correctValue"; } # Print the points value if there is any my $points = &questionGetPoints($question); if ($points) { print "\n
Points: $points"; } print "\n
\n"; } } # For debugging -- creates a bunch of dummy responses, and stores them in an array # Input: none # Output: returns an array of 'response' objects sub createDummyResponses { my @responses; # Create initially empty array to hold the responses. my $aResponse; # Mock response #1 to the dummy quiz $aResponse = &responseCreateNew(); &responseSetAlpha($aResponse, "051234"); &responseSetLastname($aResponse, "Bishop"); &responseSetSubmitDate($aResponse, "13-Nov-2005 17:02"); &responseSetScore($aResponse, 4); &responseAddAnswer($aResponse, 1); # q1: correct, 1492 &responseAddAnswer($aResponse, 1); # q2: incorrect, should be 0 = true &responseAddAnswer($aResponse, "Wilmington"); # q3: incorrect, should be "Dover" &responseAddAnswer($aResponse, "blue"); # q4: ungraded # Add it to the array $responses[0] = $aResponse; # Another Mock response (#2) to the dummy quiz $aResponse = &responseCreateNew(); &responseSetAlpha($aResponse, "0556784"); &responseSetLastname($aResponse, "Rook"); &responseSetSubmitDate($aResponse, "12-Nov-2005 9:02"); &responseSetScore($aResponse, 8); &responseAddAnswer($aResponse, 1); # q1: correct, 1492 &responseAddAnswer($aResponse, 0); # q2: correct &responseAddAnswer($aResponse, "Dover"); # q3: correct &responseAddAnswer($aResponse, "red"); # q4: ungraded # Add it to the array $responses[1] = $aResponse; # Return the whole array of responses. return @responses; } # Prints out a 'response' object # Input: the 'response' object to print # Output: prints details out. Doesn't return anything. sub printResponse { my ($response) = @_; print "\n
RESPONSE by Alpha: " . &responseGetAlpha($response); print "\n
Last name: " . &responseGetLastname($response); print "\n
Submit date " . &responseGetSubmitDate($response); my $score = &responseGetScore($response); if ($score) { print "\n
Score: $score"; } # We are printing out a single response (e.g. one student's submission). We need to know how many answers # they provided (one for each question). my $numAnswers = &responseGetNumAnswers($response); # Now print out their answer to each question. my $ii; for ($ii=0; $ii<$numAnswers; $ii++) { print "\n
Answer to question $ii: " . &responseGetAnswer($response, $ii); } print "\n
" } # Takes one argument, an array of 'response' objects, and prints them all out sub printResponses { my (@responses) = @_; my $ii; for ($ii = 0; $ii < @responses; $ii++) { &printResponse($responses[$ii]); } } # IMPORTANT -- Perl insists that a module (a file that you 'require' from somewhere else) must end with a 1; 1;