Middlebury

Terra-Survey

The Following Code is the script for using a survey in Terra

//-------------
//Brendan Smith 5/24/09
//Middlebury College Digital Media Department
//Survey Script for Terra Project
//This Script presents the user with a sequence of ten statements asking them to respond with one of the following; strongly agree, agree, indifferent, disagree, and strongly disagree. These answers are rated on a scale from 2(strongly agree) to -2(strongly disagree) and a running average of the answers are kept for each question. The script also keeps track of anyone who has successfully taken the survey so that they cannot take it again. The script prints out the information gathered to the owner of the object upon touch.

// To use simply follow these steps with the code marked below:
// 1. change the string variables OFF_PHOTO and ON_PHOTO to the names of the pictures in the prim's inventory that you would like to be displayed on the sides of the object when the survey is(ON_PHOTO) and isn't(OFF_PHOTO) being used.

// 2. Change the ten question string variables to be the ten questions that you would like to ask in the survey

// 3. When anyone other than the owner of the object "touches" the object they will be asked to answer the survey, but they can only finish the survey once. When the owner of the object "touches" the object they will be asked to answer the survey once and from then on they will be shown the results of the survey. !!! resetting the script will also reset all of the data stored about who took the survey and how they answered!!!

//-------------

//integer used to display message box
integer CHANNEL = -853;
//list of keys of users who have already taken survey
list g_VOTERS;
//Message sent to users who have alrady completed the survey
string g_SORRY_MSG = "Sorry, but you have already taken this survey";


//Step 1//

//replace these strings with the names of the photos you would like to display when the survey box is (ON) and isn't (OFF) being used
string OFF_PHOTO = "name of OFF photo here";
string ON_PHOTO = "name of ON photo here";

//global variables used to keep current average values for each statement/question
float newavg1 = 0;
float newavg2 = 0;
float newavg3 = 0;
float newavg4 = 0;
float newavg5 = 0;
float newavg6 = 0;
float newavg7 = 0;
float newavg8 = 0;
float newavg9 = 0;
float newavg10 = 0;

//temporary variables that are wiped after each user and are only passed into the global averages if the user successfully answers all questions without timing out.
integer temp1;
integer temp2;
integer temp3;
integer temp4;
integer temp5;
integer temp6;
integer temp7;
integer temp8;
integer temp9;
integer temp10;

//integer used to display message box
integer q1;
//keeps track of the number of people who have successfully completed the survey
integer numtakers = 0;
//the key of the current user
key user;
//the key of any other users trying to touch the survey box while script is active
key otheruser;
//The current time in seconds that a person has to answer a question before the script times out
float qtimer = 60.0;
//list of possible responses to each question/statement
list l = ["Strongly Disagree", "Disagree", "Indifferent", "Agree", "Strongly Agree"];


// STEP 2.//

// Replace these strings with the ten questions you would like to ask in the survey
string questionone= "1. question 1";
string questiontwo= "2. question 2";
string questionthree= "3. question 3";
string questionfour= "4. question 4";
string questionfive= "5. question 5";
string questionsix= "6. question 6";
string questionseven= "7. question 7";
string questioneight= "8. question 8";
string questionnine= "9. question 9";
string questionten= "10. question 10";



//Computes the average of a list of integers (same as calling llListStatistics(3, list);)
//No longer used in this script
float computeAverage(list l)
{
float temp = 0.0;
integer count = llGetListLength(l);
integer i = 0;
for(i = 0; i<= count; i++)
{
temp += llList2Integer(l,i);
}
return temp/count;
}

//Computes the average without using a list. Just needs the number of entries so far and the current average as well as the newest input to be averaged in. Saves lots of memory over keeping a list of all entries.
float computeAverageLow(float oldavg, integer newvalue)
{
if(numtakers > 0)
{
float tempavg = oldavg*numtakers;
tempavg += newvalue;
return tempavg/(numtakers + 1);
} else {
return (float)newvalue;
}
}

//Updates the global result variables with the new average once all questions in the survey have been answered without a timeout.
updateResults()
{
newavg1 = computeAverageLow(newavg1, temp1);
newavg2 = computeAverageLow(newavg2, temp2);
newavg3 = computeAverageLow(newavg3, temp3);
newavg4 = computeAverageLow(newavg4, temp4);
newavg5 = computeAverageLow(newavg5, temp5);
newavg6 = computeAverageLow(newavg6, temp6);
newavg7 = computeAverageLow(newavg7, temp7);
newavg8 = computeAverageLow(newavg8, temp8);
newavg9 = computeAverageLow(newavg9, temp9);
newavg10 = computeAverageLow(newavg10, temp10);
}

//Displays the current averages for all questions
displayResults()
{
llInstantMessage(user, (string)numtakers + " people have taken this survey \n Results: \n question1=" + (string)newavg1 + "\n question2=" + (string)newavg2 + "\n question3=" + (string)newavg3 + "\n question4=" + (string)newavg4 + "\n question5=" + (string)newavg5 + "\n question6=" + (string)newavg6 + "\n question7=" + (string)newavg7 + "\n question8=" + (string)newavg8 + "\n question9=" + (string)newavg9 + "\n question10=" + (string)newavg10);

}

default
{
//When entering the default state set the prims texture to the green background, set currentquestion to 1
state_entry()
{
llSetTexture(OFF_PHOTO,ALL_SIDES);
llSetTimerEvent(0);
}
//When the prim is touched the user's UID is saved, the prim's texture is changed to the green "in progress" sign
touch_end(integer num_detected)
{
user = llDetectedKey(0);
if(user == llGetOwner() && numtakers > 0)
{
displayResults();
}
//checks the key of the current user against the list of keys of past users to see if this user has already taken the survey or not. If so then it tells you and the script remains in the default state. If they havn't taken the survey then the color of the box is changed to reflect that the survey is "in progress" and the script moves to state question1
if( llListFindList( g_VOTERS, [user]) == -1 )
{
llSetTexture(ON_PHOTO,ALL_SIDES);
//llSay(0, "Set to Red");
state question1;
} else {
llInstantMessage(user, g_SORRY_MSG);
}
}
}
//The first question of the survey. All nine additional questions in the survey follow the same pattern. Once this question has been answered and the appropriate temporary variable has been set with the result, the script moves to the state for the next question until it finally moves to the final state after question 10.
state question1
{
touch_end(integer num_detected)
{
otheruser = llDetectedKey(0);
llInstantMessage(otheruser, "Someone is currently taking the survey. Please wait until the survey box turns blue and try again");
}
state_entry()
{
//Sets the timer to return the script to the default state if the current user does not answer within one minute.
llSetTimerEvent(qtimer);
llResetTime();
q1 = llListen(CHANNEL, "", NULL_KEY, "");
llDialog(user, questionone, l, CHANNEL);
}
timer()
{
llInstantMessage(user, "The survey has timed out. Please try again.");
state default;
}
//Listens for the response to the question/statement. Different values are assigned to the appropriate temporary variable depending on the answer.
listen(integer channel, string name, key id,
string message)
{
if (llToLower(message) == "strongly disagree")
{
llListenRemove(q1);
temp1 = -2;
state question2;
}
else if (llToLower(message) == "disagree")
{
llListenRemove(q1);
temp1 = -1;
state question2;
}
else if (llToLower(message) == "indifferent")
{
llListenRemove(q1);
temp1 = 0;
state question2;
}
else if (llToLower(message) == "agree")
{
llListenRemove(q1);
temp1 = 1;
state question2;
}
else if (llToLower(message) == "strongly agree")
{
llListenRemove(q1);
temp1 = 2;
state question2;
}
}
}
state question2
{
touch_end(integer num_detected)
{
otheruser = llDetectedKey(0);
llInstantMessage(otheruser, "Someone is currently taking the survey. Please wait until the survey box turns blue and try again");
}
state_entry()
{
llSetTimerEvent(qtimer);
llResetTime();
q1 = llListen(CHANNEL, "", NULL_KEY, "");
llDialog(user, questiontwo, l, CHANNEL);
}
timer()
{
llInstantMessage(user, "The survey has timed out. Please try again.");
state default;
}
listen(integer channel, string name, key id,
string message)
{
if (llToLower(message) == "strongly disagree")
{
llListenRemove(q1);
temp2 = -2;
state question3;
}
else if (llToLower(message) == "disagree")
{
llListenRemove(q1);
temp2 = -1;
state question3;
}
else if (llToLower(message) == "indifferent")
{
llListenRemove(q1);
temp2 = 0;
state question3;
}
else if (llToLower(message) == "agree")
{
llListenRemove(q1);
temp2 = 1;
state question3;
}
else if (llToLower(message) == "strongly agree")
{
llListenRemove(q1);
temp2 = 2;
state question3;
}
}
}
state question3
{
touch_end(integer num_detected)
{
otheruser = llDetectedKey(0);
llInstantMessage(otheruser, "Someone is currently taking the survey. Please wait until the survey box turns blue and try again");
}
state_entry()
{
llSetTimerEvent(qtimer);
llResetTime();
q1 = llListen(CHANNEL, "", NULL_KEY, "");
llDialog(user, questionthree, l, CHANNEL);
}
timer()
{
llInstantMessage(user, "The survey has timed out. Please try again.");
state default;
}
listen(integer channel, string name, key id,
string message)
{
if (llToLower(message) == "strongly disagree")
{
llListenRemove(q1);
temp3 = -2;
state question4;
}
else if (llToLower(message) == "disagree")
{
llListenRemove(q1);
temp3 = -1;
state question4;
}
else if (llToLower(message) == "indifferent")
{
llListenRemove(q1);
temp3 = 0;
state question4;
}
else if (llToLower(message) == "agree")
{
llListenRemove(q1);
temp3 = 1;
state question4;
}
else if (llToLower(message) == "strongly agree")
{
llListenRemove(q1);
temp3 = 2;
state question4;
}
}
}
state question4
{
touch_end(integer num_detected)
{
otheruser = llDetectedKey(0);
llInstantMessage(otheruser, "Someone is currently taking the survey. Please wait until the survey box turns blue and try again");
}
state_entry()
{
llSetTimerEvent(qtimer);
llResetTime();
q1 = llListen(CHANNEL, "", NULL_KEY, "");
llDialog(user, questionfour, l, CHANNEL);
}
timer()
{
llInstantMessage(user, "The survey has timed out. Please try again.");
state default;
}
listen(integer channel, string name, key id,
string message)
{
if (llToLower(message) == "strongly disagree")
{
llListenRemove(q1);
temp4 = -2;
state question5;
}
else if (llToLower(message) == "disagree")
{
llListenRemove(q1);
temp4 = -1;
state question5;
}
else if (llToLower(message) == "indifferent")
{
llListenRemove(q1);
temp4 = 0;
state question5;
}
else if (llToLower(message) == "agree")
{
llListenRemove(q1);
temp4 = 1;
state question5;
}
else if (llToLower(message) == "strongly agree")
{
llListenRemove(q1);
temp4 = 2;
state question5;
}
}
}
state question5
{
touch_end(integer num_detected)
{
otheruser = llDetectedKey(0);
llInstantMessage(otheruser, "Someone is currently taking the survey. Please wait until the survey box turns blue and try again");
}
state_entry()
{
llSetTimerEvent(qtimer);
llResetTime();
q1 = llListen(CHANNEL, "", NULL_KEY, "");
llDialog(user, questionfive, l, CHANNEL);
}
timer()
{
llInstantMessage(user, "The survey has timed out. Please try again.");
state default;
}
listen(integer channel, string name, key id,
string message)
{
if (llToLower(message) == "strongly disagree")
{
llListenRemove(q1);
temp5 = -2;
state question6;
}
else if (llToLower(message) == "disagree")
{
llListenRemove(q1);
temp5 = -1;
state question6;
}
else if (llToLower(message) == "indifferent")
{
llListenRemove(q1);
temp5 = 0;
state question6;
}
else if (llToLower(message) == "agree")
{
llListenRemove(q1);
temp5 = 1;
state question6;
}
else if (llToLower(message) == "strongly agree")
{
llListenRemove(q1);
temp5 = 2;
state question6;
}
}
}
state question6
{
touch_end(integer num_detected)
{
otheruser = llDetectedKey(0);
llInstantMessage(otheruser, "Someone is currently taking the survey. Please wait until the survey box turns blue and try again");
}
state_entry()
{
llSetTimerEvent(qtimer);
llResetTime();
q1 = llListen(CHANNEL, "", NULL_KEY, "");
llDialog(user, questionsix, l, CHANNEL);
}
timer()
{
llInstantMessage(user, "The survey has timed out. Please try again.");
state default;
}
listen(integer channel, string name, key id,
string message)
{
if (llToLower(message) == "strongly disagree")
{
llListenRemove(q1);
temp6 = -2;
state question7;
}
else if (llToLower(message) == "disagree")
{
llListenRemove(q1);
temp6 = -1;
state question7;
}
else if (llToLower(message) == "indifferent")
{
llListenRemove(q1);
temp6 = 0;
state question7;
}
else if (llToLower(message) == "agree")
{
llListenRemove(q1);
temp6 = 1;
state question7;
}
else if (llToLower(message) == "strongly agree")
{
llListenRemove(q1);
temp6 = 2;
state question7;
}
}
}
state question7
{
touch_end(integer num_detected)
{
otheruser = llDetectedKey(0);
llInstantMessage(otheruser, "Someone is currently taking the survey. Please wait until the survey box turns blue and try again");
}
state_entry()
{
llSetTimerEvent(qtimer);
llResetTime();
q1 = llListen(CHANNEL, "", NULL_KEY, "");
llDialog(user, questionseven, l, CHANNEL);
}
timer()
{
llInstantMessage(user, "The survey has timed out. Please try again.");
state default;
}
listen(integer channel, string name, key id,
string message)
{
if (llToLower(message) == "strongly disagree")
{
llListenRemove(q1);
temp7 = -2;
state question8;
}
else if (llToLower(message) == "disagree")
{
llListenRemove(q1);
temp7 = -1;
state question8;
}
else if (llToLower(message) == "indifferent")
{
llListenRemove(q1);
temp7 = 0;
state question8;
}
else if (llToLower(message) == "agree")
{
llListenRemove(q1);
temp7 = 1;
state question8;
}
else if (llToLower(message) == "strongly agree")
{
llListenRemove(q1);
temp7 = 2;
state question8;
}
}
}
state question8
{
touch_end(integer num_detected)
{
otheruser = llDetectedKey(0);
llInstantMessage(otheruser, "Someone is currently taking the survey. Please wait until the survey box turns blue and try again");
}
state_entry()
{
llSetTimerEvent(qtimer);
llResetTime();
q1 = llListen(CHANNEL, "", NULL_KEY, "");
llDialog(user, questioneight, l, CHANNEL);
}
timer()
{
llInstantMessage(user, "The survey has timed out. Please try again.");
state default;
}
listen(integer channel, string name, key id,
string message)
{
if (llToLower(message) == "strongly disagree")
{
llListenRemove(q1);
temp8 = -2;
state question9;
}
else if (llToLower(message) == "disagree")
{
llListenRemove(q1);
temp8 = -1;
state question9;
}
else if (llToLower(message) == "indifferent")
{
llListenRemove(q1);
temp8 = 0;
state question9;
}
else if (llToLower(message) == "agree")
{
llListenRemove(q1);
temp8 = 1;
state question9;
}
else if (llToLower(message) == "strongly agree")
{
llListenRemove(q1);
temp8 = 2;
state question9;
}
}
}
state question9
{
touch_end(integer num_detected)
{
otheruser = llDetectedKey(0);
llInstantMessage(otheruser, "Someone is currently taking the survey. Please wait until the survey box turns blue and try again");
}
state_entry()
{
llSetTimerEvent(qtimer);
llResetTime();
q1 = llListen(CHANNEL, "", NULL_KEY, "");
llDialog(user, questionnine, l, CHANNEL);
}
timer()
{
llInstantMessage(user, "The survey has timed out. Please try again.");
state default;
}
listen(integer channel, string name, key id,
string message)
{
if (llToLower(message) == "strongly disagree")
{
llListenRemove(q1);
temp9 = -2;
state question10;
}
else if (llToLower(message) == "disagree")
{
llListenRemove(q1);
temp9 = -1;
state question10;
}
else if (llToLower(message) == "indifferent")
{
llListenRemove(q1);
temp9 = 0;
state question10;
}
else if (llToLower(message) == "agree")
{
llListenRemove(q1);
temp9 = 1;
state question10;
}
else if (llToLower(message) == "strongly agree")
{
llListenRemove(q1);
temp9 = 2;
state question10;
}
}
}
state question10
{
touch_end(integer num_detected)
{
otheruser = llDetectedKey(0);
llInstantMessage(otheruser, "Someone is currently taking the survey. Please wait until the survey box turns blue and try again");
}
state_entry()
{
llSetTimerEvent(qtimer);
llResetTime();
q1 = llListen(CHANNEL, "", NULL_KEY, "");
llDialog(user, questionten, l, CHANNEL);
}
timer()
{
llInstantMessage(user, "The survey has timed out. Please try again.");
state default;
}
listen(integer channel, string name, key id,
string message)
{
if (llToLower(message) == "strongly disagree")
{
llListenRemove(q1);
temp10 = -2;
state finished;
}
else if (llToLower(message) == "disagree")
{
llListenRemove(q1);
temp10 = -1;
state finished;
}
else if (llToLower(message) == "indifferent")
{
llListenRemove(q1);
temp10 = 0;
state finished;
}
else if (llToLower(message) == "agree")
{
llListenRemove(q1);
temp10 = 1;
state finished;
}
else if (llToLower(message) == "strongly agree")
{
llListenRemove(q1);
temp10 = 2;
state finished;
}
}
}

//The final state reached when all questions/statement are answered. The temporary variables are used to update the global averages, the number of people to take the survey is incremented, the the last user's key is added to the list of users to finish the survey. The script then returns to the default state.
state finished
{
state_entry()
{

updateResults();
numtakers++;
g_VOTERS = (g_VOTERS=[]) + g_VOTERS + [user];
state default;
}
}

Powered by MediaWiki