sub defineKeywords{
$done = "no";
while ($done eq "no"){
$l += 1;
$keywds = "keywords$l";
if ($call{$keywds}){
push @formkeys, $call{$keywds};
}
else {
$done = "yes";
}
}
}
sub assembleString {
# the database. takes $call{'parameters'}, makes $addstring
&defineKeywords;
$id = $count;
# Either setOrderOfDisplayQuestions has already made
# $linkid or if it hasn't need to make it here
if (!($linkid > 0)){
if ($linkid !~ m/w/){
$linkid = '1';
}
}
# cat all these variables into addstring, seperated by |
$addstring =
$id . "|" .
$datatype . "|" .
$call{'user'} . "|" .
$linkid . "|";
# as we continue building our string, go through @formkeys
# and add all the keywords seperated by :
while ($i <= $#formkeys){
$addstring = $addstring . $formkeys[$i] . ":";
$i++;
}
$i = '0';
# to finish our string, another | and the question text
$addstring = $addstring . "|" . $formdata;
}
sub getQuestionsForLocation{
#print "Debug: i: $i, matchKeywords: $#matchKeywords!
";
# First Get Questions and put them into an array
$i = 0;
$questions = 0;
while ($i <= $#matchKeywords){
# if this is a question, make a new array of questions
#print "matchKeywords[$i]{'datatype'}: $matchKeywords[$i]{'datatype'}: eq 1?
\n";
if ($matchKeywords[$i]{'datatype'} eq '1'){
push (@matchQuestions, {
ID => $matchKeywords[$i]{'ID'},
datatype => $matchKeywords[$i]{'datatype'},
user => $matchKeywords[$i]{'user'},
linkid => $matchKeywords[$i]{'linkid'},
keywords => $matchKeywords[$i]{'keywords'},
question => $matchKeywords[$i]{'question'}
});
$questions++;
# print "adding $matchKeywords[$i]{'ID'} to matchQuestions
\n"
}
$i++;
}
$i = 0;
# Then Sort the array by the order of questions
# Since this is a list of hashes, it is hard to use a sort command.
# We know haw many questions there are and we know the order which
# to display them. Therefore we'll loop for how many questions there
# are and then put them in a new array 'SortMatchKeywords' at the
# index of where they are to be displayed. The -1 places the first
# question at 0 and the second at 1 etc.. to maintain good posture
# Discussion: Is this method a misdemeanor? It's definately faster
# than a search, but what if there is a inconsistent order of Q's
# i.e. 1, 2, 4, 5. I don't think there will be those, because of the
# way the script is set up though.
# There also have been several bugs where if the linkid is the same
# the previous entry to this array is erased.
while ($i < $questions){
# print "$i < $questions $matchQuestions[$i]{'ID'}
\n";
$buf = $matchQuestions[$i]{'linkid'};
$buf -= 1;
# print "$i < $buf $matchQuestions[$i]{'ID'}
\n";
$SortMatchKeywords[$buf]{'ID'} = $matchQuestions[$i]{'ID'};
$SortMatchKeywords[$buf]{'datatype'} = $matchQuestions[$i]{'datatype'};
$SortMatchKeywords[$buf]{'user'} = $matchQuestions[$i]{'user'};
$SortMatchKeywords[$buf]{'linkid'} = $matchQuestions[$i]{'linkid'};
$SortMatchKeywords[$buf]{'keywords'} = $matchQuestions[$i]{'keywords'};
$SortMatchKeywords[$buf]{'question'} = $matchQuestions[$i]{'question'};
$SortMatchKeywordsSize += 1;
# print "buf: $buf, i: $i, $SortMatchKeywordsSize
\n";
$i++;
}
$i = 0;
@matchQuestions = '';
# print @SortMatchKeywords;
}
sub addOrderedQuestionsToArray{
# Print questions to final array, after each question is added,
# Search for answers to questions and add them too.
# for each question in SortMatchKeywords
# push question to new array
# loop through found keywords
# if datatype is 3 (answer)
# if linkid eq ID of question
# push answer to the new array
# for each question in SortMatchKeywords
# print "SortMatchKeywordsSize: $SortMatchKeywordsSize
\n";
while ($i < $SortMatchKeywordsSize){
# print "SortMatchKeywords[$i]{'ID'}: $SortMatchKeywords[$i]{'ID'}";
# push question to new array
push (@matchQuestionsAndAnswers, {
ID => $SortMatchKeywords[$i]{'ID'},
datatype => $SortMatchKeywords[$i]{'datatype'},
user => $SortMatchKeywords[$i]{'user'},
linkid => $SortMatchKeywords[$i]{'linkid'},
keywords => $SortMatchKeywords[$i]{'keywords'},
question => $SortMatchKeywords[$i]{'question'}
});
# print "AddingQuestion: $i: $SortMatchKeywords[$i]{'ID'} to matchQuestionsAndAnswers
\n";
$j = 0;
# loop through found keywords
#print "Debug: j: $j <= #matchKeywords: $#matchKeywords
\n";
while ($j <= $#matchKeywords){
# if datatype is 3 (answer)
#print "Debug: matchKeywords[$j]{'datatype'} $matchKeywords[$j]{'datatype'} eq '3'
\n";
if ($matchKeywords[$j]{'datatype'} eq '3'){
# if linkid eq ID of question
#print "Debug: matchKeywords[$j]{'linkid'}: $matchKeywords[$j]{'linkid'} eq SortMatchKeywords[$i]{'ID'}: $SortMatchKeywords[$i]{'ID'}
\n";
if ($matchKeywords[$j]{'linkid'} eq $SortMatchKeywords[$i]{'ID'}){
# push answer to the new array
push (@matchQuestionsAndAnswers, {
ID => $matchKeywords[$j]{'ID'},
datatype => $matchKeywords[$j]{'datatype'},
user => $matchKeywords[$j]{'user'},
linkid => $matchKeywords[$j]{'linkid'},
keywords => $matchKeywords[$j]{'keywords'},
question => $matchKeywords[$j]{'question'}
});
# print "AddingAnswer: $SortMatchKeywords[$j]{'ID'} to matchQuestionsAndAnswers
\n";
}
}
$j++;
}
$i++;
}
@SortMatchKeywords = '';
$i = 0;
$j = 0;
}
sub addUserNotesToArray{
# Now we need to print the user notes to array, which
# we are going to add to the end of @matchQuestionsAndAnswers
while ($i <= $#matchKeywords){
# if datatype is a note
if ($matchKeywords[$i]{'datatype'} eq '2'){
# push
push (@matchQuestionsAndAnswers, {
ID => $matchKeywords[$i]{'ID'},
datatype => $matchKeywords[$i]{'datatype'},
user => $matchKeywords[$i]{'user'},
linkid => $matchKeywords[$i]{'linkid'},
keywords => $matchKeywords[$i]{'keywords'},
question => $matchKeywords[$i]{'question'}
});
# print "Pushing Note: $matchKeywords[$i]{'ID'}
\n";
}
$i++;
}
$i = 0;
@matchKeywords = '';
}
sub assembleQuestionEntry{
# assembleQuestionEntry assembles the form parameters from
# printFinalizeAddForm, preparing a string to be added to
# the database. takes $call{'parameters'}, makes $addstring
&defineKeywords;
$id = $count;
$datatype = '1';
# Either setOrderOfDisplayQuestions has already made
# $linkid or if it hasn't need to make it here
if (!($linkid > 0)){
$linkid = '1';
}
# cat all these variables into addstring, seperated by |
$addstring =
$id . "|" .
$datatype . "|" .
$hash{'1'} . "|" .
$linkid . "|";
# as we continue building our string, go through @formkeys
# and add all the keywords seperated by :
while ($i <= $#formkeys){
$addstring = $addstring . $formkeys[$i] . ":";
$i++;
}
$i = '';
# to finish our string, another | and the question text
$addstring = $addstring . "|" . $call{'question'};
}
sub parseDatabaseString{
# parseDatabaseString assumes the database is already open
# and is degigned to handle only data of types 1,2, and 3.
# It returns @parsedstring with the data in this format:
# $parsedinfo[0]: ID#
# $parsedinfo[1]: datatype
# $parsedinfo[2]: user
# $parsedinfo[3]: link ID# if answer, Rank if question, null if note
# $parsedinfo[4]: data, question or answer
# It also returns @parsedstring with keywords
# zero out previous array
@parsedinfo = '';
@parsedstring = '';
# $val is defined when iterating through the database.
# see dbaccess.pl: sub searchForMatchQuestions.
# seperate the pipe strings
($parsedinfo[0], $parsedinfo[1], $parsedinfo[2],
$parsedinfo[3], $keywords, $parsedinfo[4]) = split(/\|/, $val);
#print "parseDatabaseString vars: $parsedinfo[0], $parsedinfo[1], $parsedinfo[2],
#$parsedinfo[3], $keywords, $parsedinfo[3]
\n";
# put keywords in their own array
@parsedstring = split(/:/, $keywords);
if (!(($parsedinfo[1] eq '1') ||
($parsedinfo[1] eq '2') ||
($parsedinfo[1] eq '3') ||
($parsedinfo[1] eq '4'))){
# We are not supposed to be parsing these here.
# zero out array and return nothing.
@parsedinfo = '';
@parsedstring = '';
}
}
# checkFormAddQuestion checks the input variables of the
# that adds questions to the database. If there are required
# elements the subroutine will return a error screen.
sub checkFormAddQuestion{
# Our form calls with these elements:
# name: database value: riverweb
# database is how we define which database to be used
# therefore it must be present at all calls to riverweb
# This element is checked in the beginning of the
# script. The routine returns a error page if the
# database does not exist.
# name: password value: keny
# The password field needs to open the database,
# and retrieve the password key, then it must verify
# that the password is correct.
&openDBhash;
# print "add
\n";
if ($call{'password'} ne $hash{'4'}){
# the password is incorrect. return error and exit
&printHtmlHeader;
print "