use strict;
# This file define functions to read and write different elements of a survey.
# A survey is made up of an array of questions.
# Questions are defined in a separate file, question_struct.pl
require "question_struct.pl";
# DEBUG: This magic number is stored in the first index of the question array.
# Each function will check that the magic number is valid -- if not, then something
# was gone wrong -- most likely this means you called the function wrong.
use constant SURVEY_MAGIC_NUMBER => 198675432;
# Some constants used internally. These specify the index in the main array that each
# part of the 'survey' is stored
use constant SURVEY_INDEX_MAGIC => 0;
use constant SURVEY_INDEX_QUESTIONS => 1;
use constant SURVEY_INDEX_DUEDATE => 2;
use constant SURVEY_INDEX_PASSWORD => 3;
use constant SURVEY_INDEX_ISANON => 4;
# Internal debug function -- if this complains then you probably called the original function wrong
sub surveyCheckMagic {
my ($survey, $functionName, $caller_package, $caller_filename, $caller_line) = @_;
if ($$survey[SURVEY_INDEX_MAGIC] != SURVEY_MAGIC_NUMBER) {
print "\n\n\n
ERROR! Bad survey magic number in call to $functionName from $caller_filename, line $caller_line.\n " .
"This probably means you called $functionName wrong.
\n\n\n";
exit(1);
}
}
# Inputs: none
# Returns: a new survey.
#
sub surveyCreateNew {
my @anArray = ();
my $survey = \@anArray;
$$survey[SURVEY_INDEX_MAGIC] = SURVEY_MAGIC_NUMBER; # init magic number
my @emptyQuestionArray = ();
$$survey[SURVEY_INDEX_QUESTIONS] = \@emptyQuestionArray; # ref to question array;
# By default, survey is not anonymous
surveySetIsAnon($survey, 0);
return $survey;
}
# Internal function -- returns whole array of questions
sub helperSurveyGetQuestionArray {
my ($survey) = @_;
my $ref_questionArray = $$survey[SURVEY_INDEX_QUESTIONS];
my @questionArray = @$ref_questionArray;
return @questionArray;
}
# Inputs: a survey
# Returns: number of questions in that survey
sub surveyGetNumQuestions() {
my($survey) = @_; # get the input parameter
surveyCheckMagic($survey, "surveyGetNumQuestions()", caller());
my @questionArray = helperSurveyGetQuestionArray($survey);
my $numQuestions = @questionArray;
return $numQuestions;;
}
# Inputs: (survey, question)
# where survey is the survey to add to,
# and question is the question to be added.
# Returns: nothing (but modifies the survey that was provided,
# adding the questions to the end of the survey)
sub surveyAddQuestion() {
my($survey, $question) = @_;
surveyCheckMagic ($survey, "surveyAddQuestion()", caller());
questionCheckMagic($question, "surveyAddQuestion()", caller());
my $ref_questionArray = $$survey[SURVEY_INDEX_QUESTIONS];
push @$ref_questionArray, $question;;
}
# Inputs: (survey, questionNum)
# where survey is the survey to read from,
# and questionNum is the number of the question to return
# (indexed from zero)
# Outputs:
sub surveyGetQuestion {
my($survey, $questionNum) = @_;
surveyCheckMagic ($survey, "surveyGetQuestion()", caller());
my @questionArray = helperSurveyGetQuestionArray($survey);
my $question = $questionArray[$questionNum];
questionCheckMagic($question, "surveyGetQuestion()", caller());
return $question;
}
# Inputs: a survey
# Returns: the due date of that survey, or undef
sub surveyGetDueDate {
my($survey) = @_; # get the input parameter
surveyCheckMagic($survey, "surveyGetDueDate()", caller());
return $$survey[SURVEY_INDEX_DUEDATE];
}
# Inputs: (survey, dueDate))
# where survey is the survey to modify,
# and 'dueDate' is the survey due date to be set
# Returns: nothing (but modifies the survey that was provided,
# setting the due date of that survey))
sub surveySetDueDate() {
my($survey, $dueDate) = @_;
surveyCheckMagic($survey, "surveySetDueDate()", caller());
$$survey[SURVEY_INDEX_DUEDATE] = $dueDate;
}
# Inputs: a survey
# Returns: the due date of that survey, or undef
sub surveyGetPassword {
my($survey) = @_; # get the input parameter
surveyCheckMagic($survey, "surveyGetPassword()", caller());
return $$survey[SURVEY_INDEX_PASSWORD];
}
# Inputs: (survey, password))
# where survey is the survey to modify,
# and 'password' is the survey due date to be set
# Returns: nothing (but modifies the survey that was provided,
# setting the password of that survey))
sub surveySetPassword() {
my($survey, $password) = @_;
surveyCheckMagic($survey, "surveySetPassword()", caller());
$$survey[SURVEY_INDEX_PASSWORD] = $password;
}
# Inputs: a survey
# Returns: the due date of that survey, or undef
sub surveyGetIsAnon {
my($survey) = @_; # get the input parameter
surveyCheckMagic($survey, "surveyGetIsAnon()", caller());
return $$survey[SURVEY_INDEX_ISANON];
}
# Inputs: (survey, isAnon))
# where survey is the survey to modify,
# and 'isAnon' is the survey due date to be set
# Returns: nothing (but modifies the survey that was provided,
# setting the isAnon of that survey))
sub surveySetIsAnon() {
my($survey, $isAnon) = @_;
surveyCheckMagic($survey, "surveySetIsAnon()", caller());
$$survey[SURVEY_INDEX_ISANON] = $isAnon;
}
1;