% '*********************************************************************** ' Application: SelectSurveyASP Advanced v8.1.1 ' Author: Aaron Baril for ClassApps.com ' Page Description: This is the page that displays the actual survey for ' users to take. ' ' COPYRIGHT NOTICE ' ' See attached Software License Agreement ' ' (c) Copyright 2002 - 2006 by ClassApps.com. All rights reserved. '*********************************************************************** %> <% Dim strSQL Dim lngSurveyID Dim rsSurvey Dim rsResponseID Dim rsTemplate Dim rsPage Dim rsLastActualPage Dim lngQuestionNumber Dim flgUpdatingSurvey Dim flgUpdateableSurvey Dim flgHasStartedSurvey Dim flgHasCompletedSurvey Dim lngResponseID Dim lngActualPageNumber Dim lngDisplayPageNumber Dim lngPageCount Dim strQuestionNumberingFormat Dim flgResponseIDFound Dim lngItemID Dim lngTemplateID Dim strEditValue Dim strTitle Dim strLogoPath Dim strLogoAlignment Dim strSurveyAlignment Dim strDisplayBorderYN Dim strBorderColor Dim lngBorderWidth Dim strProgressFirstColor Dim strProgressSecondColor Dim strSurveyBackgroundColor Dim strPageBackgroundColor Dim strHTMLTop Dim strHTMLBottom Dim strNavigationBackButtonPath Dim strNavigationBackLinkText Dim strNavigationNextButtonPath Dim strNavigationNextLinkText Dim strNavigationDoneButtonPath Dim strNavigationDoneLinkText Dim strNavigationCancelButtonPath Dim strNavigationCancelLinkText Dim strPageTitle Dim strPageIntroduction Dim strBackButtonDisplay Dim strCancelButtonDisplay Dim strCancelButtonRedirectURL Dim strButtonPath Dim strRedirectURL Dim strEncryptedIDs Dim lngEmailAddressID Dim lngEmailMessageID Dim strResponseType Dim strTempDate Dim strTempValue Dim strItemIDValues 'Initialization If Len(Request.QueryString("SurveyID")) > 0 Then lngSurveyID = DecryptSurveyID(Request.QueryString("SurveyID")) 'The situation where you have both a survey ID and an EID in the querystring comes up when 'there is a survey with no answerable questions on the first page If Len(Request.QueryString("EID")) > 0 Then lngEmailAddressID = GetEncryptedEmailAddressID(Request.QueryString("EID")) lngEmailMessageID = GetEncryptedEmailMessageID(Request.QueryString("EID")) End If Else strEncryptedIDs = Request.QueryString("EID") lngSurveyID = GetEncryptedSurveyID(strEncryptedIDs) lngEmailAddressID = GetEncryptedEmailAddressID(strEncryptedIDs) lngEmailMessageID = GetEncryptedEmailMessageID(strEncryptedIDs) 'Check to make sure that the user has not already responded to this message If HasRespondedToMessage(strEncryptedIDs) = True Then If HasUserCompletedSurvey(lngSurveyID) = True And IsUpdateableSurvey(lngSurveyID) = False And IsMultipleResponseSurvey(lngSurveyID) = False Then Response.Redirect "TakeSurveyError.asp?SurveyID=" & EncryptSurveyID(GetEncryptedSurveyID(Request.QueryString("EID"))) & "&Reason=" & SUR_ACCESS_DENIED_ALREADY_RESPONDED_TO_MESSAGE End If End If End If lngPageCount = GetPageCount(lngSurveyID) lngItemID = -1 Set rsSurvey = Server.CreateObject("ADODB.Recordset") Set rsResponseID = Server.CreateObject("ADODB.Recordset") Set rsTemplate = Server.CreateObject("ADODB.Recordset") Set rsPage = Server.CreateObject("ADODB.Recordset") Set rsLastActualPage = Server.CreateObject("ADODB.Recordset") 'Make sure that the survey ID is a valid survey. This is an important check because it's possible that 'the survey was deleted since the time it was deployed. If IsValidSurveyID(lngSurveyID) = False Then Response.Redirect "AccessDenied.asp?Reason=" & SUR_ACCESS_DENIED_INVALID_SURVEY_ID End If 'If the survey is in manual response entry mode, this is a straight insert If IsManualResponseEntryMode(lngSurveyID) = True Then If IsUserOwnerOrAdmin(lngSurveyID) = True Then strResponseType = SUR_RESPONSE_TYPE_MANUAL flgUpdatingSurvey = False 'If the response ID is in the querystring, the user has already started this survey. Otherwise, the user 'is just starting this survey, so put them on the first page. If Len(Request.QueryString("ResponseID")) > 0 Then lngDisplayPageNumber = Request.QueryString("DisplayPageNumber") lngActualPageNumber = Request.QueryString("ActualPageNumber") lngResponseID = Request.QueryString("ResponseID") Else If Request.QueryString("DisplayPageNumber") <> "" And Request.QueryString("DisplayPageNumber") <> "1" Then lngDisplayPageNumber = Request.QueryString("DisplayPageNumber") lngActualPageNumber = Request.QueryString("ActualPageNumber") Else lngDisplayPageNumber = 1 lngActualPageNumber = 1 End If End If Else Response.Redirect "TakeSurveyError.asp?SurveyID=" & EncryptSurveyID(lngSurveyID) & "&Reason=" & SUR_ACCESS_DENIED_MANUAL_ENTRY_MODE End If Else 'The user must be logged in before attempting to take a survey, unless the survey allows 'anonymous access If IsLoggedIn() = False And IsSurveyAnonymous(lngSurveyID) = False Then 'If the SurveyID or EID is in the querystring, be sure to add it to the page before redirecting If Len(strEncryptedIDs) > 0 Then Response.Redirect "Login.asp?EID=" & strEncryptedIDs ElseIf Len(lngSurveyID) > 0 Then Response.Redirect "Login.asp?SurveyID=" & Request.QueryString("SurveyID") Else Response.Redirect "Login.asp" End If End If 'Make sure the survey is "LIVE" based on both its status and its start and end dates, if specified, before a 'user takes it. If it's not "LIVE", redirect. If IsSurveyLive(lngSurveyID) = False Then Response.Redirect "TakeSurveyError.asp?SurveyID=" & EncryptSurveyID(lngSurveyID) & "&Reason=" & SUR_ACCESS_DENIED_NOT_LIVE End If 'Check to see if the survey is being updated by a survey owner or an admin. If so, extract the Response ID, and set 'the page numbers appropriately If Request.Form("AdminOrOwnerEditResponse") = SUR_BOOLEAN_POSITIVE_DISPLAY Or Request.QueryString("Edit") = SUR_BOOLEAN_POSITIVE_DISPLAY Then If Request.Form("AdminOrOwnerEditResponse") = SUR_BOOLEAN_POSITIVE_DISPLAY Then lngResponseID = Request.Form("AdminOrOwnerResponseID") Else lngResponseID = Request.QueryString("ResponseID") End If flgUpdatingSurvey = True 'If the display page number is not specified, it's the first page If Len(Request.QueryString("DisplayPageNumber")) = 0 Then lngDisplayPageNumber = 1 lngActualPageNumber = 1 Else lngDisplayPageNumber = Request.QueryString("DisplayPageNumber") lngActualPageNumber = Request.QueryString("ActualPageNumber") End If Else 'Look at the session tracking variable to see if the user has already taken this page in the survey. If Len(Session(SUR_SESSION_TAKING_SURVEY & lngSurveyID)) > 0 And InStr(Session(SUR_SESSION_TAKING_SURVEY & lngSurveyID), "||Page" & Request.QueryString("ActualPageNumber") & "||") > 0 Then flgUpdatingSurvey = True lngDisplayPageNumber = Request.QueryString("DisplayPageNumber") lngActualPageNumber = Request.QueryString("ActualPageNumber") lngQuestionNumber = Request.QueryString("QuestionNumber") Else 'If the survey allows multiple responses, then it is not being updated. Also, if the survey allows multiple responses, 'make sure that the current user has not reached the maximum response count, if specified If IsMultipleResponseSurvey(lngSurveyID) = True Then flgUpdatingSurvey = False 'For multiple response surveys, make sure that the current user has not reached the response count limit If IsMaxResponseCountForUserReached(lngSurveyID) = True And Len(Request.QueryString("ResponseID")) = 0 Then Response.Redirect "TakeSurveyError.asp?SurveyID=" & EncryptSurveyID(lngSurveyID) & "&Reason=" & SUR_ACCESS_DENIED_MAX_RESPONSE_COUNT_PER_USER_EXCEEDED End If 'If the display page number is not specified, it's the first page If Len(Request.QueryString("DisplayPageNumber")) = 0 Then lngDisplayPageNumber = 1 lngActualPageNumber = 1 Else lngDisplayPageNumber = Request.QueryString("DisplayPageNumber") lngActualPageNumber = Request.QueryString("ActualPageNumber") End If Else 'The survey does not allow multiple responses from a single user, so check to see if a prior response can be updated 'Update flags indicating a) whether or not the survey is updateable, b) whether or not the current user has 'already started to take this survey, c) whether or not the current user has completed this survey, and d) whether 'or not the current user is updating a survey they've already taken flgUpdateableSurvey = IsUpdateableSurvey(lngSurveyID) flgHasStartedSurvey = HasUserStartedSurvey(lngSurveyID) flgHasCompletedSurvey = HasUserCompletedSurvey(lngSurveyID) If flgUpdateableSurvey = True Then 'For updateable surveys, check to see whether or not the current user has started the survey If flgHasStartedSurvey = True Then 'Since this is an updateable survey and the user has already responded to the survey, check to see 'if the number of days the survey can be updated has been reached. If IsMaxDaysForUpdatedReached(lngSurveyID) = True Then Response.Redirect "TakeSurveyError.asp?SurveyID=" & EncryptSurveyID(lngSurveyID) & "&Reason=" & SUR_ACCESS_DENIED_MAX_DAYS_FOR_UPDATE_REACHED End If flgUpdatingSurvey = True Else flgUpdatingSurvey = False End If 'If the display page number is not specified, it's the first page If Len(Request.QueryString("DisplayPageNumber")) = 0 Then lngDisplayPageNumber = 1 lngActualPageNumber = 1 Else lngDisplayPageNumber = Request.QueryString("DisplayPageNumber") lngActualPageNumber = Request.QueryString("ActualPageNumber") End If 'If the question number is not specified, it's the first question on the first page If Len(Request.QueryString("QuestionNumber")) = 0 Then lngQuestionNumber = 1 Else lngQuestionNumber = Request.QueryString("QuestionNumber") End If Else 'The survey is not updateable 'If the current user has completed the survey, redirect to an error page If (flgHasCompletedSurvey = True) Or (flgHasStartedSurvey = True And Len(Session(SUR_SESSION_TAKING_SURVEY & lngSurveyID)) = 0) Then Response.Redirect "TakeSurveyError.asp?SurveyID=" & EncryptSurveyID(lngSurveyID) & "&Reason=" & SUR_ACCESS_DENIED_ALREADY_TAKEN Else 'The survey is not updateable, and the user has not completed it. If they have started it, they are doing 'an update. If flgHasStartedSurvey = True Then flgUpdatingSurvey = True Else flgUpdatingSurvey = False End If 'Get the response ID for the survey that the user is completing. There will only be a response ID if 'the user has previously started this survey. strSQL = "SELECT response_id " & _ "FROM sur_response r " & _ "WHERE survey_id = " & lngSurveyID 'If the user is anonymous, query using the anonymous id If GetUsername() = SUR_ANONYMOUS_USER Then strSQL = strSQL & " AND anonymous_id = " & GetAnonymousUserID() Else strSQL = strSQL & " AND username = " & SQLEncode(GetUsername()) End If rsResponseID.Open ConvertSQL(strSQL), SURVEY_APP_CONNECTION, adOpenDynamic, , adCmdText 'If the user is updating a survey, at least one record must be returned If flgUpdatingSurvey = True Then rsResponseID.MoveFirst lngResponseID = rsResponseID("response_id") 'If the display page number is not specified, this is a user who started the survey earlier, and 'is now coming back to complete it If Len(Request.QueryString("DisplayPageNumber")) = 0 Then 'Get the last actual page answered strSQL = "SELECT max(page_number) As LastActualPage " & _ "FROM sur_survey_to_item_mapping m, sur_response_answer ra " & _ "WHERE m.item_id = ra.item_id " & _ "AND response_id = " & lngResponseID & _ " AND survey_id = " & lngSurveyID rsLastActualPage.Open ConvertSQL(strSQL), SURVEY_APP_CONNECTION, adOpenDynamic, , adCmdText rsLastActualPage.MoveFirst lngActualPageNumber = rsLastActualPage("LastActualPage") rsLastActualPage.Close Set rsLastActualPage = Nothing 'Get the page to display next lngDisplayPageNumber = GetNextPageInSurvey(lngSurveyID, lngResponseID, lngActualPageNumber) Else 'This is a user who just took the prior page in the survey lngDisplayPageNumber = Request.QueryString("DisplayPageNumber") lngActualPageNumber = Request.QueryString("ActualPageNumber") End If Else 'The user has not yet started this survey 'If the display page number is not specified, it's the first page If Len(Request.QueryString("DisplayPageNumber")) = 0 Then lngDisplayPageNumber = 1 lngActualPageNumber = 1 Else lngDisplayPageNumber = Request.QueryString("DisplayPageNumber") lngActualPageNumber = Request.QueryString("ActualPageNumber") End If End If 'If the question number is not specified, it's the first question on the first page If Len(Request.QueryString("QuestionNumber")) = 0 Then lngQuestionNumber = 1 Else lngQuestionNumber = Request.QueryString("QuestionNumber") End If 'Close the recordset rsResponseID.Close End If 'If flgHasCompletedSurvey = True Then End If 'If flgUpdateableSurvey = True Then End If 'If IsMultipleResponseSurvey(lngSurveyID) = True Then 'Make sure that the maximum response count has not been reached. Check the HasStartedSurvey flag because if the 'the user has already started this survey, it's OK for them to be the last response (the response count has been 'reached). If IsMaxResponseCountReached(lngSurveyID) = True And Len(Request.QueryString("ResponseID")) = 0 Then Response.Redirect "TakeSurveyError.asp?SurveyID=" & EncryptSurveyID(lngSurveyID) & "&Reason=" & SUR_ACCESS_DENIED_MAX_RESPONSE_COUNT_EXCEEDED End If 'If the ResponseID has been passed in, save it in a local variable If Len(Request.QueryString("ResponseID")) > 0 Then lngResponseID = Request.QueryString("ResponseID") End If End If End If 'Set the response type If IsLoggedIn() = True Then strResponseType = SUR_RESPONSE_TYPE_LOGGED_IN Else strResponseType = SUR_RESPONSE_TYPE_ANONYMOUS End If End If 'If this is the first page of the survey, check to make sure that the IP address is in a valid range If IsIPAddressInRange(lngSurveyID) = False Then Response.Redirect "TakeSurveyError.asp?SurveyID=" & EncryptSurveyID(lngSurveyID) & "&Reason=" & SUR_ACCESS_DENIED_IP_ADDRESS_OUT_OF_RANGE End If 'If the user is updating the survey, query for all the items on the current page along with the previous answers If flgUpdatingSurvey = True Then strSQL = "SELECT title, survey_language, template_id, i.item_type_id As item_type_id, " & _ "required_yn, email_address_yn, it.item_type_name As ItemType, other_yn, database_dsn, " & _ "m.order_number As order_number, maximum_length, i.item_id As item_id, random_answer_order_yn, " & _ "display_format, image_path, image_height, image_width, image_alignment, minimum_value, " & _ "maximum_value, answer_total, minimum_number_responses, maximum_number_responses, row_text_width, other_display_type, " & _ "default_value, subitem_count, back_button_display, cancel_button_display, cancel_button_redirect_url, " & _ "r.response_id As ResponseID, ra.answer_id As AnswerID, page_numbering_format, " & _ "question_numbering_format, item_text, ra.other_text As OtherTextAnswer, " & _ "i.other_text As OtherTextQuestion, ra.answer_text As AnswerText, database_sql, item_sub_text " & _ "FROM sur_response AS r RIGHT JOIN ((((sur_survey AS s RIGHT JOIN sur_survey_to_item_mapping AS m " & _ "ON s.survey_id = m.survey_id) INNER JOIN sur_item AS i " & _ "ON m.item_id = i.item_id) INNER JOIN sur_item_type AS it " & _ "ON i.item_type_id = it.item_type_id) LEFT JOIN sur_response_answer AS ra " & _ "ON i.item_id = ra.item_id) ON r.response_id = ra.response_id " & _ "WHERE m.page_number = " & lngActualPageNumber & _ " AND m.survey_id = " & lngSurveyID 'If the survey is being edited by a survey owner or an admin, we know the response ID, so use it in the query If Request.Form("AdminOrOwnerEditResponse") = SUR_BOOLEAN_POSITIVE_DISPLAY Then strSQL = strSQL & " AND r.response_id = " & Request.Form("AdminOrOwnerResponseID") ElseIf Request.QueryString("Edit") = SUR_BOOLEAN_POSITIVE_DISPLAY And Len(Request.QueryString("ResponseID")) > 0 Then strSQL = strSQL & " AND r.response_id = " & Request.QueryString("ResponseID") Else 'If the user is anonymous, query using the anonymous id If GetUsername() = SUR_ANONYMOUS_USER Then strSQL = strSQL & " AND r.anonymous_id = " & GetAnonymousUserID() & _ " AND r.response_id = (SELECT MAX(response_id) FROM sur_response WHERE " & _ " r.anonymous_id = " & GetAnonymousUserID() & _ " AND survey_id = " & lngSurveyID & ")" Else strSQL = strSQL & " AND (r.username = " & SQLEncode(GetUsername()) & " OR r.username IS NULL) " & _ " AND r.response_id = (SELECT MAX(response_id) FROM sur_response WHERE " & _ "(username = " & SQLEncode(GetUsername()) & " OR username IS NULL) " & _ "AND survey_id = " & lngSurveyID & ")" End If End If 'Complete the SQL statement and execute it strSQL = strSQL & " ORDER BY m.order_number" rsSurvey.Open ConvertSQL(strSQL), SURVEY_APP_CONNECTION, adOpenDynamic, , adCmdText 'If the survey is being edited by an admin or owner, we already know the response ID, so there's no need 'to search for it. If Request.Form("AdminOrOwnerEditResponse") = SUR_BOOLEAN_POSITIVE_DISPLAY Or Request.QueryString("Edit") = SUR_BOOLEAN_POSITIVE_DISPLAY Then flgResponseIDFound = True Else 'Check to see if all the response ID's in the recordset are Null. If so, there were no prior answers for 'this page. Therefore, the user is not updating the current page of the survey, but rather entering it for 'the first time. flgResponseIDFound = False If Not rsSurvey.EOF Then rsSurvey.MoveFirst Do While Not rsSurvey.EOF If IsNull(rsSurvey("ResponseID")) = False Then flgResponseIDFound = True lngResponseID = rsSurvey("ResponseID") End If rsSurvey.MoveNext Loop End If End If 'If a response ID was not found, the user is not updating the survey, but rather is on a page other than the first 'page of the survey. Therefore, find out the response ID. If flgResponseIDFound = False Then 'Set the updating flag to false and close the survey recordset flgUpdatingSurvey = False rsSurvey.Close 'Get the response ID for the survey that the user is completing. There will only be a response ID if 'the user has previously started this survey. strSQL = "SELECT response_id " & _ "FROM sur_response r " & _ "WHERE survey_id = " & lngSurveyID 'If the user is anonymous, query using the anonymous id If GetUsername() = SUR_ANONYMOUS_USER Then strSQL = strSQL & " AND anonymous_id = " & GetAnonymousUserID() Else strSQL = strSQL & " AND username = " & SQLEncode(GetUsername()) End If rsResponseID.Open ConvertSQL(strSQL), SURVEY_APP_CONNECTION, adOpenDynamic, , adCmdText rsResponseID.MoveFirst lngResponseID = rsResponseID("response_id") 'Clean up rsResponseID.Close Set rsResponseID = Nothing Else 'If a response ID was found, the user is updating the survey, so move back to the beginning of the recordset to render it below If Not rsSurvey.EOF Then rsSurvey.MoveFirst ElseIf Request.Form("AdminOrOwnerEditResponse") = SUR_BOOLEAN_POSITIVE_DISPLAY Or Request.QueryString("Edit") = SUR_BOOLEAN_POSITIVE_DISPLAY Then 'Set the updating flag to false and close the survey recordset flgUpdatingSurvey = False rsSurvey.Close End If End If End If 'The user is not updating the survey. This means either it's the first time the user has taken the survey, or the 'user started the survey, and is now completing it. If flgUpdatingSurvey = False Then strSQL = "SELECT title, survey_language, template_id, i.item_type_id As item_type_id, " & _ "required_yn, email_address_yn, it.item_type_name As ItemType, random_answer_order_yn, " & _ "other_yn, m.order_number As order_number, i.item_id As item_id, database_dsn, " & _ "display_format, maximum_length, image_path, image_height, image_width, image_alignment, " & _ "minimum_value, maximum_value, answer_total, minimum_number_responses, maximum_number_responses, " & _ "row_text_width, subitem_count, page_numbering_format, question_numbering_format, back_button_display, " & _ "cancel_button_display, cancel_button_redirect_url, default_value, item_text, " & _ "other_text As OtherTextQuestion, database_sql, item_sub_text, other_display_type " & _ "FROM sur_survey s, sur_item i, sur_survey_to_item_mapping m, sur_item_type it " & _ "WHERE i.item_id = m.item_id " & _ "AND m.page_number = " & lngActualPageNumber & _ " AND s.survey_id = m.survey_id " & _ "AND it.item_type_id = i.item_type_id " & _ "AND m.survey_id = " & lngSurveyID & _ " ORDER BY m.order_number" rsSurvey.Open ConvertSQL(strSQL), SURVEY_APP_CONNECTION, adOpenDynamic, , adCmdText End If 'Get the survey title and template ID for reuse later rsSurvey.MoveFirst strTitle = rsSurvey("title") strBackButtonDisplay = rsSurvey("back_button_display") strCancelButtonDisplay = rsSurvey("cancel_button_display") strCancelButtonRedirectURL = rsSurvey("cancel_button_redirect_url") lngTemplateID = rsSurvey("template_id") %> ">
| <% If strDisplayBorderYN = SUR_BOOLEAN_NEGATIVE Then Response.Write " " End If %><%=strTitle%> | |
|
|
<%=strTitle%> |
| <%=strTitle%> |
|
|
|
|
| <%=strTitle%> |