home *** CD-ROM | disk | FTP | other *** search
/ bombers.k12.ar.us / bombers.k12.ar.us.tar / bombers.k12.ar.us / survey_unconfigured / CopyPage.asp < prev    next >
Text File  |  2006-10-25  |  16KB  |  367 lines

  1. <!--#Include File="Include/Top_inc.asp"-->
  2. <%
  3. '***********************************************************************
  4. '   Application: SelectSurveyASP Advanced v8.1.11
  5. '   Author: Aaron Baril for ClassApps.com
  6. '   Page Description: This page copies an entire page in a survey.
  7. '
  8. '   COPYRIGHT NOTICE                                
  9. '
  10. '   See attached Software License Agreement
  11. '
  12. '   (c) Copyright 2002 - 2006 by ClassApps.com.  All rights reserved.
  13. '***********************************************************************
  14. %>
  15. <!--#Include File="Include/Config_inc.asp"-->
  16. <!--#Include File="Include/Utility_inc.asp"-->
  17. <!--#Include File="Include/adovbs_inc.asp"-->
  18. <!--#Include File="Include/ID_inc.asp"-->
  19. <!--#Include File="Include/CurrentUser_inc.asp"-->
  20. <!--#Include File="Include/SurveySecurity_inc.asp"-->
  21. <!--#Include File="Include/Constants_inc.asp"-->
  22. <!--#Include File="Include/SurveyUtility_inc.asp"-->
  23. <!--#Include File="Include/Collection_inc.asp"-->
  24. <%
  25.     'If the user does not have "Create" or "Admin" permission, redirect them to the access denied page.
  26.     If lngUserSecurityLevel <> SUR_SECURITY_LEVEL_CREATE And lngUserSecurityLevel <> SUR_SECURITY_LEVEL_ADMIN Then
  27.         Response.Redirect "AccessDenied.asp?Reason=" & SUR_ACCESS_DENIED_NOT_ADMIN_SECURITY_LEVEL
  28.     End If
  29.  
  30.     'Do not allow a user to copy a page in a survey that already has responses.  Instead, redirect the user back
  31.     'to the modify survey page
  32.     If HasResponses(Request.Form("SurveyID")) = True Then
  33.         Response.Redirect "ModifySurvey.asp?SurveyID=" & Request.Form("SurveyID")
  34.     End If
  35.  
  36.     Dim strSQL
  37.     Dim conNewPage
  38.     Dim lngSurveyID
  39.     Dim rsOriginalPage
  40.     Dim rsItemCountOnPage
  41.     Dim lngItemCountOnPage
  42.     Dim strOldToNewItemIDs
  43.     Dim strOldToNewAnswerIDs
  44.     Dim lngTempID
  45.     Dim lngNewItemID
  46.     Dim lngNewAnswerID
  47.     Dim lngPageNumber
  48.     Dim lngMinimumNumberResponses
  49.     Dim lngMaximumNumberResponses
  50.     Dim lngRowTextWidth
  51.     Dim lngAnswerTotal
  52.     Dim strItemAlias
  53.     Dim lngAnswerValue
  54.     
  55.     'Initialization
  56.     Set rsOriginalPage = Server.CreateObject("ADODB.Recordset")
  57.     Set conNewPage = Server.CreateObject("ADODB.Connection")
  58.     Set rsItemCountOnPage = Server.CreateObject("ADODB.Recordset")
  59.     conNewPage.Open SURVEY_APP_CONNECTION
  60.     lngSurveyID = Request.Form("SurveyID")
  61.     lngPageNumber = Request.Form("PageNumber")
  62.  
  63.     '1.  Get the count of items on the specified page
  64.     strSQL = "SELECT count(item_id) As ItemCountOnPage " & _
  65.              "FROM sur_survey_to_item_mapping " & _
  66.              "WHERE survey_id = " & lngSurveyID & _
  67.              " AND page_number = " & lngPageNumber
  68.     rsItemCountOnPage.Open ConvertSQL(strSQL), SURVEY_APP_CONNECTION, adOpenForwardOnly, adLockReadOnly, adCmdText
  69.     rsItemCountOnPage.MoveFirst
  70.     lngItemCountOnPage = rsItemCountOnPage("ItemCountOnPage")
  71.     rsItemCountOnPage.Close
  72.     Set rsItemCountOnPage = Nothing
  73.  
  74.     '2.  Make space in the mapping table for the copied set of items.  Increase the page number by one starting with 
  75.     'the page selected, and increase the order number by the number of items on the page.
  76.     strSQL = "UPDATE sur_survey_to_item_mapping " & _
  77.              "SET page_number = page_number + 1, " & _
  78.              "order_number = order_number + " & lngItemCountOnPage & _
  79.              " WHERE page_number >= " & lngPageNumber & _
  80.              " AND survey_id = " & lngSurveyID
  81.     conNewPage.Execute ConvertSQL(strSQL), , adCmdText
  82.  
  83.     '3.  Update the page number for any page conditions that might have been associated with the original page
  84.     strSQL = "UPDATE sur_page_condition " & _
  85.              "SET page_number = page_number + 1 " & _
  86.              "WHERE survey_id = " & lngSurveyID & _
  87.              " AND page_number = " & lngPageNumber
  88.     conNewPage.Execute ConvertSQL(strSQL), , adCmdText
  89.  
  90.     '4.  Update the page number for any page property entries that might have been associated with the original page
  91.     strSQL = "UPDATE sur_page " & _
  92.              "SET page_number = page_number + 1 " & _
  93.              "WHERE survey_id = " & lngSurveyID & _
  94.              " AND page_number >= " & lngPageNumber
  95.     conNewPage.Execute ConvertSQL(strSQL), , adCmdText
  96.  
  97.     '5.  Copy all of the data from the item table (SUR_ITEM) for the specified page
  98.     strSQL = "SELECT * FROM sur_item WHERE item_id IN (" & _
  99.              ConvertSQLInClause("SELECT item_id " & _
  100.              "FROM sur_survey_to_item_mapping " & _
  101.              "WHERE survey_id = " & lngSurveyID & _
  102.              " AND page_number = " & (lngPageNumber + 1), "item_id") & ")"
  103.     Set rsOriginalPage = conNewPage.Execute(ConvertSQL(strSQL), , adCmdText)
  104.  
  105.     'Make sure that there are items for this survey
  106.     If Not rsOriginalPage.EOF Then
  107.         rsOriginalPage.MoveFirst
  108.  
  109.         'Build the SQL statements to insert the new records into the SUR_ITEM table
  110.         Do While Not rsOriginalPage.EOF
  111.             
  112.             'Create values needed for the insert
  113.             lngMinimumNumberResponses = rsOriginalPage("minimum_number_responses")
  114.             If IsNull(lngMinimumNumberResponses) = True Then
  115.                 lngMinimumNumberResponses = "Null"
  116.             End If
  117.             lngMaximumNumberResponses = rsOriginalPage("maximum_number_responses")
  118.             If IsNull(lngMaximumNumberResponses) = True Then
  119.                 lngMaximumNumberResponses = "Null"
  120.             End If
  121.             lngRowTextWidth = rsOriginalPage("row_text_width")
  122.             If IsNull(lngRowTextWidth) = True Then
  123.                 lngRowTextWidth= "Null"
  124.             End If
  125.             lngAnswerTotal = rsOriginalPage("answer_total")
  126.             If IsNull(lngAnswerTotal) = True Or Len(lngAnswerTotal) = 0 Then
  127.                 lngAnswerTotal = "Null"
  128.             End If
  129.             strItemAlias = rsOriginalPage("item_alias")
  130.             If IsNull(strItemAlias) = True Or Len(strItemAlias) = 0 Then
  131.                 strItemAlias = ""
  132.             End If
  133.  
  134.             'Create the new ID for the new item
  135.             lngTempID = ID_GetNextAvailableID("SurveyGenerationItem")
  136.  
  137.             'Create dictionary of old item ID's to new item ID's
  138.             strOldToNewItemIDs = strOldToNewItemIDs & rsOriginalPage("item_id") & ";" & lngTempID & ";"
  139.  
  140.             strSQL = "INSERT INTO sur_item(item_id, item_type_id, item_text, item_sub_text, item_alias, required_yn, " & _
  141.                         "random_answer_order_yn, email_address_yn, other_yn, other_text, minimum_value, maximum_value, answer_total, " & _
  142.                         "minimum_number_responses, maximum_number_responses, row_text_width, display_format, image_path, " & _
  143.                         "image_width, image_height, image_alignment, default_value, database_sql, database_dsn, " & _
  144.                         "subitem_count) VALUES(" & _
  145.                         lngTempID & ", " & _
  146.                         rsOriginalPage("item_type_id") & ", " & _
  147.                         SQLEncode(rsOriginalPage("item_text")) & ", " & _
  148.                         SQLEncode(rsOriginalPage("item_sub_text")) & ", " & _
  149.                         SQLEncode(strItemAlias) & ", " & _
  150.                         SQLEncode(rsOriginalPage("required_yn")) & ", " & _
  151.                         SQLEncode(rsOriginalPage("random_answer_order_yn")) & ", " & _
  152.                         SQLEncode(rsOriginalPage("email_address_yn")) & ", " & _
  153.                         SQLEncode(rsOriginalPage("other_yn")) & ", " & _
  154.                         SQLEncode(rsOriginalPage("other_text")) & ", "
  155.             'For minimum and maximum values, make sure that Null is not used
  156.             If IsNull(rsOriginalPage("minimum_value")) = True Then
  157.                 strSQL = strSQL & "'', "
  158.             Else
  159.                 strSQL = strSQL & SQLEncode(rsOriginalPage("minimum_value")) & ", "
  160.             End If
  161.             If IsNull(rsOriginalPage("maximum_value")) = True Then
  162.                 strSQL = strSQL & "'', "
  163.             Else
  164.                 strSQL = strSQL & SQLEncode(rsOriginalPage("maximum_value")) & ", "
  165.             End If
  166.             strSQL = strSQL & lngAnswerTotal & ", " & _
  167.                         lngMinimumNumberResponses & ", " & _
  168.                         lngMaximumNumberResponses & ", " & _
  169.                         lngRowTextWidth & ", " & _
  170.                         SQLEncode(rsOriginalPage("display_format")) & ", " & _
  171.                         SQLEncode(rsOriginalPage("image_path")) & ", " & _
  172.                         SQLEncode(rsOriginalPage("image_width")) & ", "
  173.             'For image height, make sure that Null is not used
  174.             If IsNull(rsOriginalPage("image_height")) = True Then
  175.                 strSQL = strSQL & "'', "
  176.             Else
  177.                 strSQL = strSQL & SQLEncode(rsOriginalPage("image_height")) & ", "
  178.             End If
  179.             strSQL = strSQL & SQLEncode(rsOriginalPage("image_alignment")) & ", " & _
  180.                         SQLEncode(rsOriginalPage("default_value")) & ", " & _
  181.                         SQLEncode(rsOriginalPage("database_sql")) & ", " & _
  182.                         SQLEncode(rsOriginalPage("database_dsn")) & ", "
  183.             If IsNull(rsOriginalPage("subitem_count")) = True Then
  184.                 strSQL = strSQL & "''"
  185.             Else
  186.                 strSQL = strSQL & SQLEncode(rsOriginalPage("subitem_count"))
  187.             End If
  188.             strSQL = strSQL & ")"
  189.  
  190.             conNewPage.Execute ConvertSQL(strSQL), , adCmdText
  191.  
  192.             rsOriginalPage.MoveNext
  193.         Loop
  194.     End If
  195.     rsOriginalPage.Close
  196.  
  197.     '6.  Copy all of the data from the item answer table (SUR_ITEM_ANSWER)
  198.     strSQL = "SELECT * FROM sur_item_answer WHERE item_id IN (" & _
  199.              ConvertSQLInClause("SELECT item_id " & _
  200.                 "FROM sur_survey_to_item_mapping " & _
  201.                 "WHERE survey_id = " & lngSurveyID & _
  202.                 " AND page_number = " & (lngPageNumber + 1), "item_id") & ")"
  203.     Set rsOriginalPage = conNewPage.Execute(ConvertSQL(strSQL), , adCmdText)
  204.  
  205.     'Make sure that there are item answers for this survey
  206.     If Not rsOriginalPage.EOF Then
  207.         rsOriginalPage.MoveFirst
  208.  
  209.         'Build the SQL statements to insert the new records into the SUR_ITEM_ANSWER table
  210.         Do While Not rsOriginalPage.EOF
  211.             'Get the new item ID from the collection that maps old item ID's to new item ID's
  212.             lngNewItemID = GetValueFromCollection(strOldToNewItemIDs, CStr(rsOriginalPage("item_id")))
  213.  
  214.             'Create the new answer ID for the new answer
  215.             lngTempID = ID_GetNextAvailableID("SurveyGenerationAnswer")
  216.  
  217.             'Create dictionary of old item ID's to new item ID's
  218.             strOldToNewAnswerIDs = strOldToNewAnswerIDs & rsOriginalPage("answer_id") & ";" & lngTempID & ";"
  219.  
  220.             'Get the answer value
  221.             If IsNull(rsOriginalPage("answer_value")) = True Then
  222.                 lngAnswerValue = "Null"
  223.             Else
  224.                 lngAnswerValue = rsOriginalPage("answer_value")
  225.             End If
  226.  
  227.             strSQL = "INSERT INTO sur_item_answer(item_id, answer_id, answer_text, answer_value, order_number, " & _
  228.                         "default_yn) VALUES(" & _
  229.                         lngNewItemID & ", " & _
  230.                         lngTempID & ", " & _
  231.                         SQLEncode(rsOriginalPage("answer_text")) & ", " & _
  232.                         lngAnswerValue & ", " & _
  233.                         rsOriginalPage("order_number") & ", " & _
  234.                         SQLEncode(rsOriginalPage("default_yn")) & ")"
  235.             conNewPage.Execute ConvertSQL(strSQL), , adCmdText
  236.             rsOriginalPage.MoveNext
  237.         Loop
  238.     End If
  239.     rsOriginalPage.Close
  240.  
  241.     '7.  Copy all of the data from the mapping table (SUR_SURVEY_TO_ITEM_MAPPING)
  242.     strSQL = "SELECT * FROM sur_survey_to_item_mapping " & _
  243.              "WHERE survey_id = " & lngSurveyID & _
  244.              " AND page_number = " & (lngPageNumber + 1)
  245.     Set rsOriginalPage = conNewPage.Execute(ConvertSQL(strSQL), , adCmdText)
  246.  
  247.     'Make sure that there are items in the mapping table for this survey
  248.     If Not rsOriginalPage.EOF Then
  249.         rsOriginalPage.MoveFirst
  250.  
  251.         'Build the SQL statements to insert the new records into the SUR_SURVEY_TO_ITEM_MAPPING table
  252.         Do While Not rsOriginalPage.EOF
  253.             'Get the new item ID from the collection that maps old item ID's to new item ID's.
  254.             lngNewItemID = GetValueFromCollection(strOldToNewItemIDs, CStr(rsOriginalPage("item_id")))
  255.             
  256.             'Note that we subtract the number of items on the page from the order number so we can fill in the 
  257.             'spaces made earlier.  Also, we are using the page number of the original page, since the original 
  258.             'page was bumped up one page.
  259.             strSQL = "INSERT INTO sur_survey_to_item_mapping(survey_id, item_id, order_number, page_number) VALUES(" & _
  260.                         lngSurveyID & ", " & _
  261.                         lngNewItemID & ", " & _
  262.                         rsOriginalPage("order_number") - lngItemCountOnPage & ", " & _
  263.                         lngPageNumber & ")"
  264.             conNewPage.Execute ConvertSQL(strSQL), , adCmdText
  265.             rsOriginalPage.MoveNext
  266.         Loop
  267.     End If
  268.     rsOriginalPage.Close
  269.  
  270.     '8.  Copy all of the data from the subitem table (SUR_SUBITEM)
  271.     strSQL = "SELECT * FROM sur_subitem WHERE item_id IN (" & _
  272.              ConvertSQLInClause("SELECT item_id " & _
  273.                 "FROM sur_survey_to_item_mapping " & _
  274.                 "WHERE survey_id = " & lngSurveyID & _
  275.                 " AND page_number = " & (lngPageNumber + 1), "item_id") & ")"
  276.     Set rsOriginalPage = conNewPage.Execute(ConvertSQL(strSQL), , adCmdText)
  277.  
  278.     'Make sure that there are subitems for this survey
  279.     If Not rsOriginalPage.EOF Then
  280.         rsOriginalPage.MoveFirst
  281.  
  282.         'Build the SQL statements to insert the new records into the SUR_SUBITEM table
  283.         Do While Not rsOriginalPage.EOF
  284.             'Get the new item ID from the collection that maps old item ID's to new item ID's
  285.             lngNewItemID = GetValueFromCollection(strOldToNewItemIDs, CStr(rsOriginalPage("item_id")))
  286.  
  287.             'Create the new answer ID for the new answer
  288.             lngTempID = ID_GetNextAvailableID("SurveyGenerationSubitem")
  289.  
  290.             strSQL = "INSERT INTO sur_subitem(subitem_id, item_id, subitem_text, order_number) " & _
  291.                         "VALUES(" & _
  292.                         lngTempID & ", " & _
  293.                         lngNewItemID & ", " & _
  294.                         SQLEncode(rsOriginalPage("subitem_text")) & ", " & _
  295.                         rsOriginalPage("order_number") & ")"
  296.             conNewPage.Execute ConvertSQL(strSQL), , adCmdText
  297.             rsOriginalPage.MoveNext
  298.         Loop
  299.     End If
  300.     rsOriginalPage.Close
  301.  
  302.     '9.  Copy all of the data from the page condition table (SUR_PAGE_CONDITION)
  303.     strSQL = "SELECT * FROM sur_page_condition " & _
  304.              "WHERE survey_id = " & lngSurveyID & _
  305.              " AND page_number = " & lngPageNumber + 1
  306.     Set rsOriginalPage = conNewPage.Execute(ConvertSQL(strSQL), , adCmdText)
  307.  
  308.     'Make sure that there are page conditions for this survey
  309.     If Not rsOriginalPage.EOF Then
  310.         rsOriginalPage.MoveFirst
  311.  
  312.         'Build the SQL statements to insert the new records into the SUR_PAGE_CONDITION table
  313.         Do While Not rsOriginalPage.EOF
  314.             'Generate a new value for the page_condition_id column
  315.             lngTempID = ID_GetNextAvailableID("SurveyGenerationPageConditionID")
  316.  
  317.             'Get the new answer ID.  If the old answer ID equals SUR_ANSWER_ID_NULL then use SUR_ANSWER_ID_NULL. 
  318.             'Otherwise, get the answer from the collection that maps old answer ID's to new answer ID's
  319.             
  320.             If CStr(rsOriginalPage("answer_id")) = CStr(SUR_ANSWER_ID_NULL) Then
  321.                 lngNewAnswerID = SUR_ANSWER_ID_NULL
  322.             Else
  323.                 lngNewAnswerID = GetValueFromCollection(strOldToNewAnswerIDs, CStr(rsOriginalPage("answer_id")))
  324.             End If
  325.             strSQL = "INSERT INTO sur_page_condition(page_condition_id, survey_id, page_number, dependent_item_id, " & _
  326.                         "group_number, operator_id, answer_id, answer_text) VALUES(" & _
  327.                         lngTempID & ", " & _
  328.                         lngSurveyID & ", " & _
  329.                         lngPageNumber & ", " & _
  330.                         rsOriginalPage("dependent_item_id") & ", " & _
  331.                         rsOriginalPage("group_number") & ", " & _
  332.                         rsOriginalPage("operator_id") & ", " & _
  333.                         rsOriginalPage("answer_id") & ", " & _
  334.                         SQLEncode(rsOriginalPage("answer_text")) & ")"
  335.             conNewPage.Execute ConvertSQL(strSQL), , adCmdText
  336.             rsOriginalPage.MoveNext
  337.         Loop
  338.     End If
  339.     rsOriginalPage.Close
  340.  
  341.     '10.  Copy all of the data from the page property table (SUR_PAGE)
  342.     strSQL = "SELECT * FROM sur_page " & _
  343.              "WHERE survey_id = " & lngSurveyID & _
  344.              " AND page_number = " & lngPageNumber + 1
  345.     Set rsOriginalPage = conNewPage.Execute(ConvertSQL(strSQL), , adCmdText)
  346.  
  347.     'Make sure that there is a page property entry for this page
  348.     If Not rsOriginalPage.EOF Then
  349.         rsOriginalPage.MoveFirst
  350.  
  351.         strSQL = "INSERT INTO sur_page(survey_id, page_number, page_title, page_introduction) " & _
  352.                     "VALUES(" & _
  353.                     rsOriginalPage("survey_id") & ", " & _
  354.                     lngPageNumber & ", " & _
  355.                     SQLEncode(rsOriginalPage("page_title")) & ", " & _
  356.                     SQLEncode(rsOriginalPage("page_introduction")) & ")"
  357.         conNewPage.Execute ConvertSQL(strSQL), , adCmdText
  358.     End If
  359.     rsOriginalPage.Close
  360.  
  361.     'Clean up
  362.     conNewPage.Close
  363.     Set conNewPage = Nothing
  364.  
  365.     'Redirect to the modify survey page for the survey just created
  366.     Response.Redirect "ModifySurvey.asp?SurveyID=" & lngSurveyID & "&#ItemID" & lngTempID
  367. %>