home *** CD-ROM | disk | FTP | other *** search
/ bombers.k12.ar.us / bombers.k12.ar.us.tar / bombers.k12.ar.us / survey_unconfigured / CopyItemAction.asp < prev    next >
Text File  |  2006-10-25  |  14KB  |  346 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 works with CopyItem.asp, and process the
  7. '                     form that allows users to create a new item by copying
  8. '                      an existing item.  After creating the new item, the 
  9. '                      user is redirect to the page Item.asp to edit the item.
  10. '
  11. '   COPYRIGHT NOTICE                                
  12. '
  13. '   See attached Software License Agreement
  14. '
  15. '   (c) Copyright 2002 - 2006 by ClassApps.com.  All rights reserved.
  16. '***********************************************************************
  17. %>
  18. <!--#Include File="Include/Config_inc.asp"-->
  19. <!--#Include File="Include/Utility_inc.asp"-->
  20. <!--#Include File="Include/adovbs_inc.asp"-->
  21. <!--#Include File="Include/ID_inc.asp"-->
  22. <!--#Include File="Include/CurrentUser_inc.asp"-->
  23. <!--#Include File="Include/SurveySecurity_inc.asp"-->
  24. <!--#Include File="Include/SurveyUtility_inc.asp"-->
  25. <!--#Include File="Include/Constants_inc.asp"-->
  26. <%
  27.     Dim strSQL
  28.     Dim conItem
  29.     Dim rsOriginal
  30.     Dim lngNewItemID
  31.     Dim lngOriginalItemID
  32.     Dim lngSurveyID
  33.     Dim lngLibraryID
  34.     Dim lngPageNumber
  35.     Dim lngItemTypeID
  36.     Dim strMinimumValue
  37.     Dim strMaximumValue
  38.     Dim lngMinimumNumberResponses
  39.     Dim lngMaximumNumberResponses
  40.     Dim lngRowTextWidth
  41.     Dim lngMaximumLength
  42.     Dim lngAnswerTotal
  43.     Dim lngSubitemCount
  44.     Dim strImageHeight
  45.     Dim strItemAlias
  46.     Dim strOtherText
  47.     Dim strOtherDisplayType
  48.     Dim lngNewAnswerID
  49.     Dim lngNewSubitemID
  50.     Dim lngAnswerValue
  51.     Dim flgLibrary
  52.     Dim flgHasResponses
  53.     
  54.     'Determine whether the item being copied is in a library or a survey
  55.     If Len(Request.Form("LibraryID")) > 0 Then
  56.         lngLibraryID = Request.Form("LibraryID")
  57.         flgLibrary = True
  58.         flgHasResponses = False
  59.     ElseIf Len(Request.Form("cboLibrary")) > 0 Then
  60.         'This is the case when inserting an item from a library
  61.         lngSurveyID = Request.Form("OriginalSurveyID")
  62.         flgLibrary = False
  63.         flgHasResponses = HasResponses(lngSurveyID)
  64.     Else
  65.         'Get the survey ID
  66.         If Len(Request.Form("OriginalSurveyID")) > 0 Then
  67.             lngSurveyID = Request.Form("OriginalSurveyID")
  68.         Else
  69.             lngSurveyID = Request.Form("SurveyID")
  70.         End If
  71.         flgLibrary = False
  72.         flgHasResponses = HasResponses(lngSurveyID)
  73.     End If
  74.  
  75.     'Do not allow a user to copy an item in a survey that already has responses.  Instead, redirect the user back
  76.     'to the modify survey page
  77.     If flgHasResponses = True Then
  78.         Response.Redirect "ModifySurvey.asp?SurveyID=" & lngSurveyID
  79.     End If
  80.  
  81.     'Initialization
  82.     Set rsOriginal = Server.CreateObject("ADODB.Recordset")
  83.     Set conItem = Server.CreateObject("ADODB.Connection")
  84.     conItem.Open SURVEY_APP_CONNECTION
  85.     
  86.     'If the survey is being copied from the ModifySurvey.asp page, the item ID will be in a form variable called ItemID. 
  87.     'Otherwise, the item was selected from a combo box.
  88.     If Len(Request.Form("cboItem")) = 0 Then
  89.         lngOriginalItemID = Request.Form("ItemID")
  90.     Else
  91.         lngOriginalItemID = Request.Form("cboItem")
  92.     End If
  93.     
  94.     'Get ID for a new item
  95.     lngNewItemID = ID_GetNextAvailableID("SurveyGenerationItem")
  96.  
  97.     'Copy all of the data from the item table (SUR_ITEM)
  98.     strSQL = "SELECT * FROM sur_item WHERE item_id = " & lngOriginalItemID
  99.     rsOriginal.Open ConvertSQL(strSQL), SURVEY_APP_CONNECTION, adOpenDynamic, adLockReadOnly, adCmdText
  100.     rsOriginal.MoveFirst
  101.     lngItemTypeID = rsOriginal("item_type_id")
  102.     strOtherText = rsOriginal("other_text")
  103.     If IsNull(strOtherText) = True Then
  104.         strOtherText = ""
  105.     End If
  106.     strOtherDisplayType = rsOriginal("other_display_type")
  107.     If IsNull(strOtherDisplayType) = True Then
  108.         strOtherDisplayType = ""
  109.     End If
  110.  
  111.     If Len(rsOriginal("minimum_value")) = 0 Or IsNull(rsOriginal("minimum_value")) = True Then
  112.         strMinimumValue = ""
  113.     Else
  114.         strMinimumValue = rsOriginal("minimum_value")
  115.     End If
  116.     
  117.     If Len(rsOriginal("maximum_value")) = 0 Or IsNull(rsOriginal("maximum_value")) = True Then
  118.         strMaximumValue = ""
  119.     Else
  120.         strMaximumValue = rsOriginal("maximum_value")
  121.     End If
  122.  
  123.     If Len(rsOriginal("answer_total")) = 0 Or IsNull(rsOriginal("answer_total")) = True Then
  124.         lngAnswerTotal = "Null"
  125.     Else
  126.         lngAnswerTotal = rsOriginal("answer_total")
  127.     End If
  128.  
  129.     If Len(rsOriginal("subitem_count")) = 0 Or IsNull(rsOriginal("subitem_count")) = True Then
  130.         lngSubitemCount = "Null"
  131.     Else
  132.         lngSubitemCount = rsOriginal("subitem_count")
  133.     End If
  134.  
  135.     If Len(rsOriginal("minimum_number_responses")) = 0 Or IsNull(rsOriginal("minimum_number_responses")) = True Then
  136.         lngMinimumNumberResponses = "Null"
  137.     Else
  138.         lngMinimumNumberResponses = rsOriginal("minimum_number_responses")
  139.     End If
  140.     
  141.     If Len(rsOriginal("maximum_number_responses")) = 0 Or IsNull(rsOriginal("maximum_number_responses")) = True Then
  142.         lngMaximumNumberResponses = "Null"
  143.     Else
  144.         lngMaximumNumberResponses = rsOriginal("maximum_number_responses")
  145.     End If
  146.  
  147.     If Len(rsOriginal("row_text_width")) = 0 Or IsNull(rsOriginal("row_text_width")) = True Then
  148.         lngRowTextWidth = "Null"
  149.     Else
  150.         lngRowTextWidth = rsOriginal("row_text_width")
  151.     End If
  152.     
  153.     If Len(rsOriginal("maximum_length")) = 0 Or IsNull(rsOriginal("maximum_length")) = True Then
  154.         lngMaximumLength = "Null"
  155.     Else
  156.         lngMaximumLength = rsOriginal("maximum_length")
  157.     End If
  158.     
  159.     If Len(rsOriginal("image_height")) = 0 Or IsNull(rsOriginal("image_height")) = True Then
  160.         strImageHeight = ""
  161.     Else
  162.         strImageHeight = rsOriginal("image_height")
  163.     End If
  164.     
  165.     If Len(rsOriginal("item_alias")) = 0 Or IsNull(rsOriginal("item_alias")) = True Then
  166.         strItemAlias = ""
  167.     Else
  168.         strItemAlias = rsOriginal("item_alias")
  169.     End If
  170.  
  171.     'Create the item in the SUR_ITEM table
  172.     strSQL = "INSERT INTO sur_item(item_id, item_type_id, item_alias, required_yn, random_answer_order_yn, " & _
  173.                 "email_address_yn, other_yn, other_text, other_display_type, minimum_value, maximum_value, " & _
  174.                 "answer_total, minimum_number_responses, maximum_number_responses, row_text_width, maximum_length, " & _
  175.                 "display_format, image_path, image_width, image_height, image_alignment, default_value, " & _
  176.                 "database_dsn, subitem_count, item_sub_text, database_sql, item_text) VALUES(" & _
  177.                 lngNewItemID & ", " & _
  178.                 rsOriginal("item_type_id") & ", " & _
  179.                 SQLEncode(strItemAlias) & ", " & _
  180.                 SQLEncode(rsOriginal("required_yn")) & ", " & _
  181.                 SQLEncode(rsOriginal("random_answer_order_yn")) & ", " & _
  182.                 SQLEncode(rsOriginal("email_address_yn")) & ", " & _
  183.                 SQLEncode(rsOriginal("other_yn")) & ", " & _
  184.                 SQLEncode(strOtherText) & ", " & _
  185.                 SQLEncode(strOtherDisplayType) & ", " & _
  186.                 SQLEncode(strMinimumValue) & ", " & _
  187.                 SQLEncode(strMaximumValue) & ", " & _
  188.                 lngAnswerTotal & ", " & _
  189.                 lngMinimumNumberResponses & ", " & _
  190.                 lngMaximumNumberResponses & ", " & _
  191.                 lngRowTextWidth & ", " & _
  192.                 lngMaximumLength & ", " & _
  193.                 SQLEncode(rsOriginal("display_format")) & ", " & _
  194.                 SQLEncode(rsOriginal("image_path")) & ", " & _
  195.                 SQLEncode(rsOriginal("image_width")) & ", " & _
  196.                 SQLEncode(strImageHeight) & ", " & _
  197.                 SQLEncode(rsOriginal("image_alignment")) & ", " & _
  198.                 SQLEncode(rsOriginal("default_value")) & ", " & _
  199.                 SQLEncode(rsOriginal("database_dsn")) & ", " & _
  200.                 lngSubitemCount & ", " & _
  201.                 SQLEncode(rsOriginal("item_sub_text")) & ", " & _
  202.                 SQLEncode(rsOriginal("database_sql")) & ", " & _
  203.                 SQLEncode(rsOriginal("item_text")) & ")"
  204.     conItem.Execute ConvertSQL(strSQL), , adCmdText
  205.     rsOriginal.Close
  206.     
  207.     'Move all the other items down one, so that we can insert the new item in the right location.  Note that
  208.     'new items are always inserted AT the order number indicated, which means that all existing items
  209.     'with that order number or higher have to have their order numbers incremented by 1.
  210.     If flgLibrary = True Then
  211.         strSQL = "UPDATE sur_library_to_item_mapping SET order_number = order_number + 1 WHERE order_number >= " & Request.Form("OrderNumber") & " AND library_id = " & lngLibraryID
  212.     Else
  213.         strSQL = "UPDATE sur_survey_to_item_mapping SET order_number = order_number + 1 WHERE order_number >= " & Request.Form("OrderNumber") & " AND survey_id = " & lngSurveyID
  214.     End If
  215.     conItem.Execute ConvertSQL(strSQL), , adCmdText
  216.             
  217.     'If the "NewPage" form value is set to "Yes", then the new item should be inserted on a new page.  The new page 
  218.     'number will be one greater than the page number passed in.  All pages after this page must be bumped down.
  219.     If Request.Form("NewPage") = "Yes" Then
  220.         strSQL = "UPDATE sur_survey_to_item_mapping " & _
  221.                     "SET page_number = page_number + 1 " & _
  222.                     "WHERE survey_id = " & lngSurveyID & _
  223.                     " AND page_number > " & Request.Form("PageNumber")
  224.         conItem.Execute ConvertSQL(strSQL), , adCmdText
  225.  
  226.         'Increment the page number for all page conditions for this survey that are greater than the page just created
  227.         strSQL = "UPDATE sur_page_condition " & _
  228.                     "SET page_number = page_number + 1 " & _
  229.                     "WHERE survey_id = " & lngSurveyID & _
  230.                     " AND page_number > " & Request.Form("PageNumber")
  231.         conItem.Execute ConvertSQL(strSQL), , adCmdText
  232.  
  233.         'Increment the page number for all page properties for this survey that are greater than the page just created
  234.         strSQL = "UPDATE sur_page " & _
  235.                     "SET page_number = page_number + 1 " & _
  236.                     "WHERE survey_id = " & lngSurveyID & _
  237.                     " AND page_number > " & Request.Form("PageNumber")
  238.         conItem.Execute ConvertSQL(strSQL), , adCmdText
  239.                 
  240.         lngPageNumber = CLng(Request.Form("PageNumber")) + 1
  241.     Else
  242.         lngPageNumber = Request.Form("PageNumber")
  243.     End If
  244.             
  245.     'Insert an entry into the mapping table (either SUR_SURVEY_TO_ITEM_MAPPING or SUR_LIBRARY_TO_ITEM_MAPPING).  
  246.     If flgLibrary = True Then
  247.         strSQL = "INSERT INTO sur_library_to_item_mapping(library_id, item_id, order_number) VALUES(" & _
  248.                     lngLibraryID & ", " & lngNewItemID & ", " & Request.Form("OrderNumber") & ")"
  249.     Else
  250.         strSQL = "INSERT INTO sur_survey_to_item_mapping(survey_id, item_id, order_number, page_number) VALUES(" & _
  251.                     lngSurveyID & ", " & lngNewItemID & ", " & Request.Form("OrderNumber") & ", " & lngPageNumber & ")"
  252.     End If
  253.     conItem.Execute ConvertSQL(strSQL), , adCmdText
  254.  
  255.     'If the item type contains multiple answers, record the answers in the database.  
  256.     If CLng(lngItemTypeID) = SUR_ITEM_SINGLE_SELECT_DROPDOWN Or CLng(lngItemTypeID) = SUR_ITEM_MULTISELECT_CHECKBOXES Or CLng(lngItemTypeID) = SUR_ITEM_SINGLE_SELECT_OPTIONS Or CLng(lngItemTypeID) = SUR_ITEM_MATRIX_SINGLE_SELECT_OPTIONS Or CLng(lngItemTypeID) = SUR_ITEM_MATRIX_MULTISELECT_CHECKBOXES Or CLng(lngItemTypeID) = SUR_ITEM_MATRIX_TEXT_BOXES Then
  257.         'Copy all of the answers from the item table (SUR_ITEM_ANSWER)
  258.         strSQL = "SELECT * FROM sur_item_answer WHERE item_id = " & lngOriginalItemID
  259.         rsOriginal.Open ConvertSQL(strSQL), SURVEY_APP_CONNECTION, adOpenForwardOnly, adLockReadOnly, adCmdText
  260.         rsOriginal.MoveFirst
  261.  
  262.         'Loop through the answers, and insert each into the SUR_ITEM_ANSWER table as answers to the new question
  263.         Do While Not rsOriginal.EOF
  264.             'Get the next available answer ID
  265.             lngNewAnswerID = ID_GetNextAvailableID("SurveyGenerationAnswer")
  266.  
  267.             'Get the answer value
  268.             If IsNull(rsOriginal("answer_value")) = True Then
  269.                 lngAnswerValue = "Null"
  270.             Else
  271.                 lngAnswerValue = rsOriginal("answer_value")
  272.             End If
  273.             
  274.             strSQL = "INSERT INTO sur_item_answer(item_id, answer_id, answer_text, answer_value, order_number, " & _
  275.                         "default_yn) VALUES(" & _
  276.                         lngNewItemID & ", " & _
  277.                         lngNewAnswerID & ", " & _  
  278.                         SQLEncode(rsOriginal("answer_text")) & ", " & _
  279.                         lngAnswerValue & ", " & _
  280.                         rsOriginal("order_number") & ", " & _
  281.                         SQLEncode(rsOriginal("default_yn")) & ")"
  282.             conItem.Execute ConvertSQL(strSQL), , adCmdText
  283.             rsOriginal.MoveNext
  284.         Loop
  285.  
  286.         'Clean up
  287.         rsOriginal.Close
  288.     End If
  289.  
  290.     'If the item type contains subitems, record the subitems in the database.  
  291.     If CLng(lngItemTypeID) = SUR_ITEM_CONSTANT_SUM Or CLng(lngItemTypeID) = SUR_ITEM_RANKING Or CLng(lngItemTypeID) = SUR_ITEM_OPEN_ENDED_ONE_OR_MORE_LINES Or CLng(lngItemTypeID) = SUR_ITEM_MATRIX_SINGLE_SELECT_OPTIONS Or CLng(lngItemTypeID) = SUR_ITEM_MATRIX_RATING_SCALE Or CLng(lngItemTypeID) = SUR_ITEM_MATRIX_MULTISELECT_CHECKBOXES Or CLng(lngItemTypeID) = SUR_ITEM_MATRIX_TEXT_BOXES Then
  292.         'Copy all of subitems from the submitem table (SUR_SUBITEM)
  293.         strSQL = "SELECT * FROM sur_subitem WHERE item_id = " & lngOriginalItemID
  294.         rsOriginal.Open ConvertSQL(strSQL), SURVEY_APP_CONNECTION, adOpenForwardOnly, adLockReadOnly, adCmdText
  295.         rsOriginal.MoveFirst
  296.  
  297.         'Loop through the submitems, and insert each into the SUR_SUBITEM table as subitems for the new question
  298.         Do While Not rsOriginal.EOF
  299.             'Get the next available subitem ID
  300.             lngNewSubitemID = ID_GetNextAvailableID("SurveyGenerationSubitem")
  301.  
  302.             strSQL = "INSERT INTO sur_subitem(subitem_id, item_id, subitem_text, order_number) VALUES(" & _
  303.                         lngNewSubitemID & ", " & _
  304.                         lngNewItemID & ", " & _  
  305.                         SQLEncode(rsOriginal("subitem_text")) & ", " & _
  306.                         rsOriginal("order_number") & ")"
  307.             conItem.Execute ConvertSQL(strSQL), , adCmdText
  308.             rsOriginal.MoveNext
  309.         Loop
  310.  
  311.         'Clean up
  312.         rsOriginal.Close
  313.     End If
  314.  
  315.     'Clean up
  316.     conItem.Close
  317.     Set conItem = Nothing
  318.     Set rsOriginal = Nothing
  319.  
  320.     'For libraries, redirect back to the modify library page
  321.     If flgLibrary = True Then
  322.         Response.Redirect "ModifyLibrary.asp?LibraryID=" & lngLibraryID & "&#ItemID" & lngNewItemID
  323.     Else    
  324.         'If the item was created by copying from the ModifySurvey.asp page, redirect back to that page; otherwise, 
  325.         'redirect to the new item so that it can be modified.
  326.         If Len(Request.Form("ReturnToModifySurvey")) > 0 Then
  327.             Response.Redirect "ModifySurvey.asp?SurveyID=" & lngSurveyID & "&#ItemID" & lngNewItemID
  328.         Else
  329. %>
  330.             <html>
  331.             <body onload="javascript:document.forms['frmItem'].submit();">
  332.                 <form name="frmItem" action="Item.asp" method="post">
  333.                     <input type="hidden" name="State" value="<%=SUR_STATE_EDIT%>">
  334.                     <input type="hidden" name="ItemID" value="<%=lngNewItemID%>">
  335.                     <input type="hidden" name="SurveyID" value="<%=lngSurveyID%>">
  336.                 </form>
  337.             </body>
  338.             </html>
  339. <%
  340.         End If
  341.     End If
  342.     
  343.     'Ensure that the web server returns the page
  344.     Response.Flush
  345. %>
  346.