home *** CD-ROM | disk | FTP | other *** search
/ bombers.k12.ar.us / bombers.k12.ar.us.tar / bombers.k12.ar.us / survey_unconfigured / ItemAction.asp < prev    next >
Text File  |  2006-10-25  |  32KB  |  701 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 Item.asp, and process the
  7. '                     form that allows users to create or edit items.
  8. '                     Once the data has been written to the database, the
  9. '                     user is redirected back to the page ModifySurvey.asp.
  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 lngSurveyID
  30.     Dim lngLibraryID
  31.     Dim lngOrderNumber
  32.     Dim lngItemID
  33.     Dim strState
  34.     Dim lngItemTypeID
  35.     Dim strItemText
  36.     Dim strItemSubText
  37.     Dim strItemAlias
  38.     Dim strRequiredYesNo
  39.     Dim strRandomOrderYesNo
  40.     Dim strEmailAddressYesNo
  41.     Dim strOtherYesNo
  42.     Dim strOtherText
  43.     Dim strOtherDisplayType
  44.     Dim strDefaultValue
  45.     Dim strMinimumValue
  46.     Dim strMaximumValue
  47.     Dim lngMaximumLength
  48.     Dim lngMinimumNumberResponses
  49.     Dim lngMaximumNumberResponses
  50.     Dim lngRowTextWidth
  51.     Dim lngAnswerTotal
  52.     Dim strDisplayFormat
  53.     Dim flgDefaultValue
  54.     Dim lngAnswerID
  55.     Dim i
  56.     Dim rsPreviousAnswerCount
  57.     Dim lngPreviousAnswerCount
  58.     Dim strImageHeight
  59.     Dim strImageWidth
  60.     Dim strImagePath
  61.     Dim strImageAlignment
  62.     Dim strDatabaseSQL
  63.     Dim strDatabaseDSN
  64.     Dim lngPageNumber
  65.     Dim arrRowNames
  66.     Dim strRowName
  67.     Dim lngSubitemID
  68.     Dim lngAnswerValue
  69.     Dim flgHasResponses
  70.     Dim lngSurveyOrLibraryID
  71.     Dim flgLibrary
  72.     Dim lngSubItemCount
  73.                 
  74.     'Initialization
  75.     Set conItem = Server.CreateObject("ADODB.Connection")
  76.     Set rsPreviousAnswerCount = Server.CreateObject("ADODB.Recordset")
  77.     strState = Request.Form("State")
  78.     lngItemTypeID = Request.Form("cboItemType")
  79.     conItem.Open SURVEY_APP_CONNECTION
  80.     lngOrderNumber = Request.Form("OrderNumber")
  81.     lngSubItemCount = 0
  82.     
  83.     'Check to see if this is an item for a library or a survey
  84.     If Len(Request.Form("SurveyID")) > 0 Then
  85.         lngSurveyID = Request.Form("SurveyID")
  86.         lngSurveyOrLibraryID = lngSurveyID
  87.         flgHasResponses = HasResponses(lngSurveyID)
  88.         flgLibrary = False
  89.     Else 'Library
  90.         flgHasResponses = False
  91.         lngLibraryID = Request.Form("LibraryID")
  92.         lngSurveyOrLibraryID = lngLibraryID
  93.         flgLibrary = True
  94.     End If
  95.     
  96.     'If the survey has responses, the item is in edit mode, and only limited editing is allowed.
  97.     If flgHasResponses = True Then
  98.         strImagePath = ""
  99.         strItemText = ""
  100.         strItemSubText = ""
  101.         strItemAlias = ""
  102.  
  103.         'For images, only update the image path
  104.         If CLng(lngItemTypeID) = SUR_ITEM_IMAGE Then
  105.             strImagePath = Request.Form("txtImagePath")
  106.         Else
  107.             'For all non-image items, update the item text
  108.             strItemText = Request.Form("txtItemText")
  109.  
  110.             'For all items except imgages, headings, HTML, and messages, update the sub-text and the question alias
  111.             If CLng(lngItemTypeID) <> SUR_ITEM_HEADING And CLng(lngItemTypeID) <> SUR_ITEM_MESSAGE And CLng(lngItemTypeID) <> SUR_ITEM_HTML Then
  112.                 strItemSubText = Trim(Request.Form("txtItemSubText"))
  113.                 strItemAlias = Trim(Request.Form("txtItemAlias"))
  114.             End If
  115.         End If
  116.         
  117.         'Update the current item
  118.         strSQL = "UPDATE sur_item SET " & _
  119.                     "item_text = " & SQLEncode(strItemText) & ", " & _
  120.                     "item_sub_text = " & SQLEncode(strItemSubText) & ", " & _
  121.                     "item_alias = " & SQLEncode(strItemAlias) & ", " & _
  122.                     "image_path = " & SQLEncode(strImagePath) & _
  123.                     " WHERE item_id = " & Request.Form("ItemID")
  124.         conItem.Execute ConvertSQL(strSQL), , adCmdText
  125.     Else
  126.         'If this is an item type with multiple responses, and it's in EDIT mode, determine the number 
  127.         'of previous answers associated with this question
  128.         If CLng(lngItemTypeID) = SUR_ITEM_MATRIX_SINGLE_SELECT_OPTIONS Or CLng(lngItemTypeID) = SUR_ITEM_MATRIX_MULTISELECT_CHECKBOXES Or CLng(lngItemTypeID) = SUR_ITEM_MATRIX_TEXT_BOXES Or CLng(lngItemTypeID) = SUR_ITEM_SINGLE_SELECT_DROPDOWN Or CLng(lngItemTypeID) = SUR_ITEM_MULTISELECT_CHECKBOXES Or CLng(lngItemTypeID) = SUR_ITEM_SINGLE_SELECT_OPTIONS Then
  129.             If strState = SUR_STATE_EDIT Or strState = SUR_STATE_EDIT_REFRESH Then
  130.                 lngItemID = Request.Form("ItemID")
  131.  
  132.                 'Obtain the count of answers previously associated with this question
  133.                 strSQL = "SELECT count(answer_id) As PreviousAnswerCount FROM sur_item_answer WHERE item_id = " & lngItemID
  134.                 rsPreviousAnswerCount.Open ConvertSQL(strSQL), SURVEY_APP_CONNECTION, adOpenForwardOnly, , adCmdText
  135.                 rsPreviousAnswerCount.MoveFirst
  136.                 lngPreviousAnswerCount = CLng(rsPreviousAnswerCount("PreviousAnswerCount"))
  137.                 rsPreviousAnswerCount.Close
  138.                 Set rsPreviousAnswerCount = Nothing
  139.             End If
  140.         End If
  141.             
  142.         'Initialize all of the variables to empty or Null, since not all of them will be used for each question type
  143.         strItemText = ""
  144.         strItemSubText = ""
  145.         strItemAlias = ""
  146.         strRequiredYesNo = SUR_BOOLEAN_NEGATIVE
  147.         strRandomOrderYesNo = SUR_BOOLEAN_NEGATIVE
  148.         strEmailAddressYesNo = SUR_BOOLEAN_NEGATIVE
  149.         strOtherYesNo = ""
  150.         strOtherText = ""
  151.         strOtherDisplayType = ""
  152.         strDefaultValue = ""
  153.         strMinimumValue = ""
  154.         lngMaximumLength = "Null"
  155.         strMaximumValue = ""
  156.         lngMinimumNumberResponses = "Null"
  157.         lngMaximumNumberResponses = "Null"
  158.         lngRowTextWidth = "Null"
  159.         lngAnswerTotal = "Null"
  160.         strDisplayFormat = ""
  161.         strImageHeight = ""
  162.         strImageWidth = ""
  163.         strImagePath = ""
  164.         strImageAlignment = ""
  165.         strDatabaseSQL = ""
  166.         strDatabaseDSN = ""
  167.  
  168.         'Depending on the item type, set the appropriate values
  169.         Select Case CLng(lngItemTypeID)
  170.             Case SUR_ITEM_HEADING, SUR_ITEM_MESSAGE, SUR_ITEM_HTML
  171.                 strItemText = Request.Form("txtItemText")
  172.             Case SUR_ITEM_SINGLE_SELECT_DROPDOWN
  173.                 strItemText = Trim(Request.Form("txtItemText"))
  174.                 strItemSubText = Trim(Request.Form("txtItemSubText"))
  175.                 strItemAlias = Trim(Request.Form("txtItemAlias"))
  176.                 If Request.Form("chkRequired") = "on" Then
  177.                     strRequiredYesNo = SUR_BOOLEAN_POSITIVE
  178.                 Else
  179.                     strRequiredYesNo = SUR_BOOLEAN_NEGATIVE
  180.                 End If
  181.                 If Request.Form("chkRandomOrder") = "on" Then
  182.                     strRandomOrderYesNo = SUR_BOOLEAN_POSITIVE
  183.                 Else
  184.                     strRandomOrderYesNo = SUR_BOOLEAN_NEGATIVE
  185.                 End If
  186.             Case SUR_ITEM_SINGLE_SELECT_OPTIONS
  187.                 strItemText = Trim(Request.Form("txtItemText"))
  188.                 strItemSubText = Trim(Request.Form("txtItemSubText"))
  189.                 strItemAlias = Trim(Request.Form("txtItemAlias"))
  190.                 If Request.Form("chkRequired") = "on" Then
  191.                     strRequiredYesNo = SUR_BOOLEAN_POSITIVE
  192.                 Else
  193.                     strRequiredYesNo = SUR_BOOLEAN_NEGATIVE
  194.                 End If
  195.                 If Request.Form("chkRandomOrder") = "on" Then
  196.                     strRandomOrderYesNo = SUR_BOOLEAN_POSITIVE
  197.                 Else
  198.                     strRandomOrderYesNo = SUR_BOOLEAN_NEGATIVE
  199.                 End If
  200.                 strDisplayFormat = Request.Form("cboDisplayType")
  201.                 If Request.Form("chkOther") = "on" Then
  202.                     strOtherYesNo = SUR_BOOLEAN_POSITIVE
  203.                 Else
  204.                     strOtherYesNo = SUR_BOOLEAN_NEGATIVE
  205.                 End If
  206.                 strOtherText = Request.Form("txtOther")
  207.                 strOtherDisplayType = Request.Form("cboOtherDisplayType")
  208.             Case SUR_ITEM_MULTISELECT_CHECKBOXES
  209.                 strItemText = Trim(Request.Form("txtItemText"))
  210.                 strItemSubText = Trim(Request.Form("txtItemSubText"))
  211.                 strItemAlias = Trim(Request.Form("txtItemAlias"))
  212.                 If Request.Form("chkRequired") = "on" Then
  213.                     strRequiredYesNo = SUR_BOOLEAN_POSITIVE
  214.                 Else
  215.                     strRequiredYesNo = SUR_BOOLEAN_NEGATIVE
  216.                 End If
  217.                 If Request.Form("chkRandomOrder") = "on" Then
  218.                     strRandomOrderYesNo = SUR_BOOLEAN_POSITIVE
  219.                 Else
  220.                     strRandomOrderYesNo = SUR_BOOLEAN_NEGATIVE
  221.                 End If
  222.                 strDisplayFormat = Request.Form("cboDisplayType")
  223.                 If Len(Request.Form("txtMinimumNumberResponses")) > 0 Then
  224.                     lngMinimumNumberResponses = Request.Form("txtMinimumNumberResponses")
  225.                 End If
  226.                 
  227.                 If Len(Request.Form("txtMaximumNumberResponses")) > 0 Then
  228.                     lngMaximumNumberResponses = Request.Form("txtMaximumNumberResponses")
  229.                 End If
  230.  
  231.                 If Request.Form("chkOther") = "on" Then
  232.                     strOtherYesNo = SUR_BOOLEAN_POSITIVE
  233.                 Else
  234.                     strOtherYesNo = SUR_BOOLEAN_NEGATIVE
  235.                 End If
  236.                 strOtherText = Request.Form("txtOther")
  237.                 strOtherDisplayType = Request.Form("cboOtherDisplayType")
  238.             Case SUR_ITEM_DATABASE_DROPDOWN
  239.                 strItemText = Trim(Request.Form("txtItemText"))
  240.                 strItemSubText = Trim(Request.Form("txtItemSubText"))
  241.                 strItemAlias = Trim(Request.Form("txtItemAlias"))
  242.                 strDatabaseSQL = Trim(Request.Form("txtDatabaseSQL"))
  243.                 strDatabaseDSN = Trim(Request.Form("txtDatabaseDSN"))
  244.                 If Request.Form("chkRequired") = "on" Then
  245.                     strRequiredYesNo = SUR_BOOLEAN_POSITIVE
  246.                 Else
  247.                     strRequiredYesNo = SUR_BOOLEAN_NEGATIVE
  248.                 End If
  249.             Case SUR_ITEM_OPEN_ENDED_ONE_LINE
  250.                 strItemText = Trim(Request.Form("txtItemText"))
  251.                 strItemSubText = Trim(Request.Form("txtItemSubText"))
  252.                 strItemAlias = Trim(Request.Form("txtItemAlias"))
  253.                 If Request.Form("chkRequired") = "on" Then
  254.                     strRequiredYesNo = SUR_BOOLEAN_POSITIVE
  255.                 Else
  256.                     strRequiredYesNo = SUR_BOOLEAN_NEGATIVE
  257.                 End If
  258.                 If Request.Form("chkEmailAddress") = "on" Then
  259.                     strEmailAddressYesNo = SUR_BOOLEAN_POSITIVE
  260.                 Else
  261.                     strEmailAddressYesNo = SUR_BOOLEAN_NEGATIVE
  262.                 End If
  263.                 strDefaultValue = Request.Form("txtDefaultValue")
  264.                 If Len(Request.Form("txtMaximumLength")) > 0 Then
  265.                     lngMaximumLength = Request.Form("txtMaximumLength")
  266.                 End If
  267.             Case SUR_ITEM_OPEN_ENDED_COMMENTS_BOX
  268.                 strItemText = Trim(Request.Form("txtItemText"))
  269.                 strItemSubText = Trim(Request.Form("txtItemSubText"))
  270.                 strItemAlias = Trim(Request.Form("txtItemAlias"))
  271.                 If Request.Form("chkRequired") = "on" Then
  272.                     strRequiredYesNo = SUR_BOOLEAN_POSITIVE
  273.                 Else
  274.                     strRequiredYesNo = SUR_BOOLEAN_NEGATIVE
  275.                 End If
  276.                 strDefaultValue = Request.Form("txtDefaultValue")
  277.                 If Len(Request.Form("txtMaximumLength")) > 0 Then
  278.                     lngMaximumLength = Request.Form("txtMaximumLength")
  279.                 End If
  280.             Case SUR_ITEM_NUMBER
  281.                 strItemText = Trim(Request.Form("txtItemText"))
  282.                 strItemSubText = Trim(Request.Form("txtItemSubText"))
  283.                 strItemAlias = Trim(Request.Form("txtItemAlias"))
  284.                 If Request.Form("chkRequired") = "on" Then
  285.                     strRequiredYesNo = SUR_BOOLEAN_POSITIVE
  286.                 Else
  287.                     strRequiredYesNo = SUR_BOOLEAN_NEGATIVE
  288.                 End If
  289.                 strDefaultValue = Request.Form("txtDefaultValue")
  290.                 strMinimumValue = Request.Form("txtMinimumValue")
  291.                 strMaximumValue = Request.Form("txtMaximumValue")
  292.                 If Len(Request.Form("txtMaximumLength")) > 0 Then
  293.                     lngMaximumLength = Request.Form("txtMaximumLength")
  294.                 End If
  295.             Case SUR_ITEM_DATE
  296.                 strItemText = Trim(Request.Form("txtItemText"))
  297.                 strItemSubText = Trim(Request.Form("txtItemSubText"))
  298.                 strItemAlias = Trim(Request.Form("txtItemAlias"))
  299.                 If Request.Form("chkRequired") = "on" Then
  300.                     strRequiredYesNo = SUR_BOOLEAN_POSITIVE
  301.                 Else
  302.                     strRequiredYesNo = SUR_BOOLEAN_NEGATIVE
  303.                 End If
  304.                 strDefaultValue = Request.Form("txtDefaultValue")
  305.                 strMinimumValue = Request.Form("txtMinimumValue")
  306.                 strMaximumValue = Request.Form("txtMaximumValue")
  307.             Case SUR_ITEM_YES_NO, SUR_ITEM_TRUE_FALSE
  308.                 strItemText = Trim(Request.Form("txtItemText"))
  309.                 strItemSubText = Trim(Request.Form("txtItemSubText"))
  310.                 strItemAlias = Trim(Request.Form("txtItemAlias"))
  311.                 If Request.Form("chkRequired") = "on" Then
  312.                     strRequiredYesNo = SUR_BOOLEAN_POSITIVE
  313.                 Else
  314.                     strRequiredYesNo = SUR_BOOLEAN_NEGATIVE
  315.                 End If
  316.                 strDefaultValue = Request.Form("cboDefaultValue")
  317.                 strDisplayFormat = Request.Form("cboDisplayType")
  318.             Case SUR_ITEM_HORIZONTAL_RULE
  319.                 'Nothing to set
  320.             Case SUR_ITEM_IMAGE
  321.                 strImageHeight = Request.Form("txtImageHeight")
  322.                 strImageWidth = Request.Form("txtImageWidth")
  323.                 strImagePath = Request.Form("txtImagePath")
  324.                 strImageAlignment = Request.Form("cboImageAlignment")
  325.             Case SUR_ITEM_CONSTANT_SUM
  326.                 strItemText = Trim(Request.Form("txtItemText"))
  327.                 strItemSubText = Trim(Request.Form("txtItemSubText"))
  328.                 strItemAlias = Trim(Request.Form("txtItemAlias"))
  329.                 If Request.Form("chkRequired") = "on" Then
  330.                     strRequiredYesNo = SUR_BOOLEAN_POSITIVE
  331.                 Else
  332.                     strRequiredYesNo = SUR_BOOLEAN_NEGATIVE
  333.                 End If
  334.                 If Request.Form("chkRandomOrder") = "on" Then
  335.                     strRandomOrderYesNo = SUR_BOOLEAN_POSITIVE
  336.                 Else
  337.                     strRandomOrderYesNo = SUR_BOOLEAN_NEGATIVE
  338.                 End If
  339.                 If Len(Request.Form("txtAnswerTotal")) > 0 Then
  340.                     lngAnswerTotal = Request.Form("txtAnswerTotal")
  341.                 End If
  342.                 If Len(Request.Form("txtRowTextWidth")) > 0 Then
  343.                     lngRowTextWidth = Request.Form("txtRowTextWidth")
  344.                 End If
  345.             Case SUR_ITEM_RANKING
  346.                 strItemText = Trim(Request.Form("txtItemText"))
  347.                 strItemSubText = Trim(Request.Form("txtItemSubText"))
  348.                 strItemAlias = Trim(Request.Form("txtItemAlias"))
  349.                 If Request.Form("chkRequired") = "on" Then
  350.                     strRequiredYesNo = SUR_BOOLEAN_POSITIVE
  351.                 Else
  352.                     strRequiredYesNo = SUR_BOOLEAN_NEGATIVE
  353.                 End If
  354.                 If Request.Form("chkRandomOrder") = "on" Then
  355.                     strRandomOrderYesNo = SUR_BOOLEAN_POSITIVE
  356.                 Else
  357.                     strRandomOrderYesNo = SUR_BOOLEAN_NEGATIVE
  358.                 End If
  359.                 If Len(Request.Form("txtRowTextWidth")) > 0 Then
  360.                     lngRowTextWidth = Request.Form("txtRowTextWidth")
  361.                 End If
  362.             Case SUR_ITEM_OPEN_ENDED_ONE_OR_MORE_LINES
  363.                 strItemText = Trim(Request.Form("txtItemText"))
  364.                 strItemSubText = Trim(Request.Form("txtItemSubText"))
  365.                 strItemAlias = Trim(Request.Form("txtItemAlias"))
  366.                 If Request.Form("chkRequired") = "on" Then
  367.                     strRequiredYesNo = SUR_BOOLEAN_POSITIVE
  368.                 Else
  369.                     strRequiredYesNo = SUR_BOOLEAN_NEGATIVE
  370.                 End If
  371.  
  372.                 If Request.Form("chkRandomOrder") = "on" Then
  373.                     strRandomOrderYesNo = SUR_BOOLEAN_POSITIVE
  374.                 Else
  375.                     strRandomOrderYesNo = SUR_BOOLEAN_NEGATIVE
  376.                 End If
  377.  
  378.                 If Len(Request.Form("txtMinimumNumberResponses")) > 0 Then
  379.                     lngMinimumNumberResponses = Request.Form("txtMinimumNumberResponses")
  380.                 End If
  381.                 
  382.                 If Len(Request.Form("txtMaximumNumberResponses")) > 0 Then
  383.                     lngMaximumNumberResponses = Request.Form("txtMaximumNumberResponses")
  384.                 End If
  385.                 If Len(Request.Form("txtMaximumLength")) > 0 Then
  386.                     lngMaximumLength = Request.Form("txtMaximumLength")
  387.                 End If
  388.                 If Len(Request.Form("txtRowTextWidth")) > 0 Then
  389.                     lngRowTextWidth = Request.Form("txtRowTextWidth")
  390.                 End If
  391.             Case SUR_ITEM_MATRIX_SINGLE_SELECT_OPTIONS, SUR_ITEM_MATRIX_MULTISELECT_CHECKBOXES, SUR_ITEM_MATRIX_TEXT_BOXES
  392.                 strItemText = Trim(Request.Form("txtItemText"))
  393.                 strItemSubText = Trim(Request.Form("txtItemSubText"))
  394.                 strItemAlias = Trim(Request.Form("txtItemAlias"))
  395.                 If Request.Form("chkRequired") = "on" Then
  396.                     strRequiredYesNo = SUR_BOOLEAN_POSITIVE
  397.                 Else
  398.                     strRequiredYesNo = SUR_BOOLEAN_NEGATIVE
  399.                 End If
  400.                 If Request.Form("chkRandomOrder") = "on" Then
  401.                     strRandomOrderYesNo = SUR_BOOLEAN_POSITIVE
  402.                 Else
  403.                     strRandomOrderYesNo = SUR_BOOLEAN_NEGATIVE
  404.                 End If
  405.                 If Len(Request.Form("txtRowTextWidth")) > 0 Then
  406.                     lngRowTextWidth = Request.Form("txtRowTextWidth")
  407.                 End If
  408.             Case SUR_ITEM_MATRIX_RATING_SCALE
  409.                 strItemText = Trim(Request.Form("txtItemText"))
  410.                 strItemSubText = Trim(Request.Form("txtItemSubText"))
  411.                 strItemAlias = Trim(Request.Form("txtItemAlias"))
  412.                 If Request.Form("chkRequired") = "on" Then
  413.                     strRequiredYesNo = SUR_BOOLEAN_POSITIVE
  414.                 Else
  415.                     strRequiredYesNo = SUR_BOOLEAN_NEGATIVE
  416.                 End If
  417.                 strMinimumValue = Request.Form("txtMinimumValue")
  418.                 strMaximumValue = Request.Form("txtMaximumValue")
  419.                 If Request.Form("chkRandomOrder") = "on" Then
  420.                     strRandomOrderYesNo = SUR_BOOLEAN_POSITIVE
  421.                 Else
  422.                     strRandomOrderYesNo = SUR_BOOLEAN_NEGATIVE
  423.                 End If
  424.                 If Len(Request.Form("txtRowTextWidth")) > 0 Then
  425.                     lngRowTextWidth = Request.Form("txtRowTextWidth")
  426.                 End If
  427.         End Select    
  428.  
  429.         'Depending on the state (INSERT OR EDIT), create and execute the appropriate SQL statement(s)
  430.         Select Case strState
  431.             Case SUR_STATE_EDIT, SUR_STATE_EDIT_REFRESH 'UPDATES
  432.                 lngItemID = Request.Form("ItemID")
  433.  
  434.                 'Update the current item
  435.                 strSQL = "UPDATE sur_item SET " & _
  436.                             "item_type_id = " & lngItemTypeID & ", " & _
  437.                             "item_text = " & SQLEncode(strItemText) & ", " & _
  438.                             "item_sub_text = " & SQLEncode(strItemSubText) & ", " & _
  439.                             "item_alias = " & SQLEncode(strItemAlias) & ", " & _
  440.                             "required_yn = " & SQLEncode(strRequiredYesNo) & ", " & _
  441.                             "random_answer_order_yn = " & SQLEncode(strRandomOrderYesNo) & ", " & _
  442.                             "email_address_yn = " & SQLEncode(strEmailAddressYesNo) & ", " & _
  443.                             "other_yn = " & SQLEncode(strOtherYesNo) & ", " & _
  444.                             "other_text = " & SQLEncode(strOtherText) & ", " & _
  445.                             "other_display_type = " & SQLEncode(strOtherDisplayType) & ", " & _
  446.                             "maximum_length = " & lngMaximumLength & ", " & _
  447.                             "minimum_value = " & SQLEncode(strMinimumValue) & ", " & _
  448.                             "maximum_value = " & SQLEncode(strMaximumValue) & ", " & _
  449.                             "answer_total = " & lngAnswerTotal & ", " & _
  450.                             "minimum_number_responses = " & lngMinimumNumberResponses & ", " & _
  451.                             "maximum_number_responses = " & lngMaximumNumberResponses & ", " & _
  452.                             "row_text_width = " & lngRowTextWidth & ", " & _
  453.                             "display_format = " & SQLEncode(strDisplayFormat) & ", " & _
  454.                             "default_value = " & SQLEncode(strDefaultValue) & ", " & _
  455.                             "image_path = " & SQLEncode(strImagePath) & ", " & _
  456.                             "image_height = " & SQLEncode(strImageHeight) & ", " & _
  457.                             "image_width = " & SQLEncode(strImageWidth) & ", " & _
  458.                             "database_sql = " & SQLEncode(strDatabaseSQL) & ", " & _
  459.                             "database_dsn = " & SQLEncode(strDatabaseDSN) & ", " & _
  460.                             "image_alignment = " & SQLEncode(strImageAlignment) & _
  461.                             " WHERE item_id = " & lngItemID
  462.                 conItem.Execute ConvertSQL(strSQL), , adCmdText
  463.  
  464.             Case SUR_STATE_INSERT, SUR_STATE_INSERT_REFRESH, SUR_STATE_COPY_EXISTING_QUESTION 'INSERTS
  465.  
  466.                 'Get ID for a new item
  467.                 lngItemID = ID_GetNextAvailableID("SurveyGenerationItem")
  468.  
  469.                 'Create the item in the SUR_ITEM table
  470.                 strSQL = "INSERT INTO sur_item(item_id, item_type_id, item_text, item_sub_text, item_alias, required_yn, " & _
  471.                             "random_answer_order_yn, email_address_yn, other_yn, other_text, other_display_type, maximum_length, minimum_value, maximum_value, answer_total, " & _
  472.                             "minimum_number_responses, maximum_number_responses, row_text_width, display_format, image_path, image_width, " & _
  473.                             "image_height, image_alignment, database_sql, database_dsn, default_value) VALUES(" & _
  474.                             lngItemID & ", " & lngItemTypeID & ", " & SQLEncode(strItemText) & ", " & _
  475.                             SQLEncode(strItemSubText) & ", " & SQLEncode(strItemAlias) & ", " & SQLEncode(strRequiredYesNo) & ", " & _
  476.                             SQLEncode(strRandomOrderYesNo) & ", " & SQLEncode(strEmailAddressYesNo) & ", " & _
  477.                             SQLEncode(strOtherYesNo) & ", " & SQLEncode(strOtherText) & ", " & SQLEncode(strOtherDisplayType) & ", " & _
  478.                             lngMaximumLength & ", " & SQLEncode(strMinimumValue) & ", " & SQLEncode(strMaximumValue) & ", " & _
  479.                             lngAnswerTotal & ", " & lngMinimumNumberResponses & ", " & lngMaximumNumberResponses & ", " & _
  480.                             lngRowTextWidth & ", " & SQLEncode(strDisplayFormat) & ", " & SQLEncode(strImagePath) & ", " & _
  481.                             SQLEncode(strImageWidth) & ", " & SQLEncode(strImageHeight) & ", " & _
  482.                             SQLEncode(strImageAlignment) & ", " & _ 
  483.                             SQLEncode(strDatabaseSQL) & ", " & _ 
  484.                             SQLEncode(strDatabaseDSN) & ", " & _ 
  485.                             SQLEncode(strDefaultValue) & ")"
  486.                 conItem.Execute ConvertSQL(strSQL), , adCmdText
  487.                 
  488.                 'Move all the other items down one, so that we can insert the new item in the right location.  Note that
  489.                 'new items are always inserted AT the order number indicated, which means that all existing items
  490.                 'with that order number or higher have to have their order numbers incremented by 1.
  491.                 If flgLibrary = True Then
  492.                     strSQL = "UPDATE sur_library_to_item_mapping SET order_number = order_number + 1 WHERE order_number >= " & lngOrderNumber & " AND library_id = " & lngLibraryID
  493.                 Else
  494.                     strSQL = "UPDATE sur_survey_to_item_mapping SET order_number = order_number + 1 WHERE order_number >= " & lngOrderNumber & " AND survey_id = " & lngSurveyID
  495.                 End If
  496.                 conItem.Execute ConvertSQL(strSQL), , adCmdText
  497.                 
  498.                 'If the "NewPage" form value is set to "Yes", then the new item should be inserted on a new page.  The new page 
  499.                 'number will be one greater than the page number passed in.  All pages after this page must be bumped down.
  500.                 If Request.Form("NewPage") = "Yes" Then
  501.                     strSQL = "UPDATE sur_survey_to_item_mapping " & _
  502.                                 "SET page_number = page_number + 1 " & _
  503.                                 "WHERE survey_id = " & lngSurveyID & _
  504.                                 " AND page_number > " & Request.Form("PageNumber")
  505.                     conItem.Execute ConvertSQL(strSQL), , adCmdText
  506.  
  507.                     'Increment the page number for all page conditions for this survey that are greater than the page 
  508.                     'just created.
  509.                     strSQL = "UPDATE sur_page_condition " & _
  510.                                 "SET page_number = page_number + 1 " & _
  511.                                 "WHERE survey_id = " & lngSurveyID & _
  512.                                 " AND page_number > " & Request.Form("PageNumber")
  513.                     conItem.Execute ConvertSQL(strSQL), , adCmdText
  514.                     
  515.                     'Increment the page number for all page properties for this survey that are greater than the page 
  516.                     'just created.
  517.                     strSQL = "UPDATE sur_page " & _
  518.                                 "SET page_number = page_number + 1 " & _
  519.                                 "WHERE survey_id = " & lngSurveyID & _
  520.                                 " AND page_number > " & Request.Form("PageNumber")
  521.                     conItem.Execute ConvertSQL(strSQL), , adCmdText
  522.                     
  523.                     lngPageNumber = CLng(Request.Form("PageNumber")) + 1
  524.                 Else
  525.                     lngPageNumber = Request.Form("PageNumber")
  526.                 End If
  527.                 
  528.                 'Insert an entry into the mapping table, (either SUR_SURVEY_TO_ITEM_MAPPING or SUR_LIBRARY_TO_ITEM_MAPPING).  
  529.                 If flgLibrary = True Then
  530.                     strSQL = "INSERT INTO sur_library_to_item_mapping(library_id, item_id, order_number) VALUES(" & _
  531.                                 lngLibraryID & ", " & lngItemID & ", " & lngOrderNumber & ")"
  532.                 Else 'Survey
  533.                     strSQL = "INSERT INTO sur_survey_to_item_mapping(survey_id, item_id, order_number, page_number) VALUES(" & _
  534.                                 lngSurveyID & ", " & lngItemID & ", " & lngOrderNumber & ", " & lngPageNumber & ")"
  535.                 End If
  536.                 conItem.Execute ConvertSQL(strSQL), , adCmdText
  537.         End Select
  538.  
  539.         'If this is EDIT mode, delete the subitems for this item.  If the item will still have subitems, they will be 
  540.         'recreated below.  If the item had subitems but no longer will have them, this will clean out the old, unused 
  541.         'subitems.
  542.         If strState = SUR_STATE_EDIT Or strState = SUR_STATE_EDIT_REFRESH Then
  543.             strSQL = "DELETE FROM sur_subitem " & _
  544.                         " WHERE item_id = " & lngItemID
  545.             conItem.Execute ConvertSQL(strSQL), , adCmdText
  546.         End If
  547.  
  548.         'If the item type contains row names, record the answers in the database.  
  549.         If CLng(lngItemTypeID) = SUR_ITEM_CONSTANT_SUM Or CLng(lngItemTypeID) = SUR_ITEM_RANKING 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 Or CLng(lngItemTypeID) = SUR_ITEM_OPEN_ENDED_ONE_OR_MORE_LINES Then
  550.             'Split the list of row names on the linefeed character.
  551.             arrRowNames = Split(Trim(Request.Form("txtRowNames")), vbCrLf)
  552.                                         
  553.             'Loop through the row names, and save each to the database.
  554.             For i = 0 To UBound(arrRowNames)
  555.                 'Make sure that data was entered on each line before processing
  556.                 If Len(arrRowNames(i)) > 0 Then
  557.                     'Trim the row name
  558.                     strRowName = Trim(arrRowNames(i))
  559.  
  560.                     'Get the next available subitem ID
  561.                     lngSubitemID = ID_GetNextAvailableID("SurveyGenerationSubitem")
  562.  
  563.                     'Create and execute the INSERT statement to create a new subitem
  564.                     strSQL = "INSERT INTO sur_subitem(subitem_id, item_id, subitem_text, order_number) VALUES(" & _
  565.                              lngSubitemID & ", " & _
  566.                              lngItemID & ", " & _
  567.                              SQLEncode(strRowName) & ", " & _
  568.                              i + 1 & ")"
  569.                     conItem.Execute ConvertSQL(strSQL), , adCmdText
  570.                     
  571.                     'Count the number of subitems
  572.                     lngSubItemCount = lngSubItemCount + 1
  573.                 End If
  574.             Next
  575.  
  576.             'Update the SUR_ITEM table with the count of subitems
  577.             strSQL = "UPDATE sur_item SET subitem_count = " & lngSubItemCount & " WHERE item_id = " & lngItemID
  578.             conItem.Execute ConvertSQL(strSQL), , adCmdText
  579.         End If
  580.     
  581.         'If the item type contains multiple answers, record the answers in the database.  
  582.         If CLng(lngItemTypeID) = SUR_ITEM_MATRIX_SINGLE_SELECT_OPTIONS Or CLng(lngItemTypeID) = SUR_ITEM_MATRIX_MULTISELECT_CHECKBOXES Or CLng(lngItemTypeID) = SUR_ITEM_MATRIX_TEXT_BOXES Or CLng(lngItemTypeID) = SUR_ITEM_SINGLE_SELECT_DROPDOWN Or CLng(lngItemTypeID) = SUR_ITEM_MULTISELECT_CHECKBOXES Or CLng(lngItemTypeID) = SUR_ITEM_SINGLE_SELECT_OPTIONS Then
  583.             'Initalize the order number variable, and iterate through all of the answer text boxes.  
  584.             lngOrderNumber = 1
  585.             For i = 1 To Request.Form("AnswerCount")
  586.                 'Only record an answer where text has actually been inserted
  587.                 If Len(Trim(Request.Form("txtAnswer" & i))) > 0 Then
  588.                     
  589.                     lngAnswerID = ID_GetNextAvailableID("SurveyGenerationAnswer")
  590.                     If CLng(lngItemTypeID) = SUR_ITEM_MULTISELECT_CHECKBOXES Then
  591.                         If Request.Form("txtDefaultAnswerYN" & i) = "on" Then
  592.                             flgDefaultValue = SUR_BOOLEAN_POSITIVE
  593.                         Else
  594.                             flgDefaultValue = SUR_BOOLEAN_NEGATIVE
  595.                         End If
  596.                     Else 'Must be either a drop down or radio buttons
  597.                         If Request.Form("txtDefaultAnswerYN") = "txtAnswer" & i Then
  598.                             flgDefaultValue = SUR_BOOLEAN_POSITIVE
  599.                         Else
  600.                             flgDefaultValue = SUR_BOOLEAN_NEGATIVE
  601.                         End If
  602.                     End If
  603.  
  604.                     'For checkboxes, dropdowns, and option buttons, get the points value from the form; otherwise, set it to 0
  605.                     If CLng(lngItemTypeID) = SUR_ITEM_SINGLE_SELECT_DROPDOWN Or CLng(lngItemTypeID) = SUR_ITEM_MULTISELECT_CHECKBOXES Or CLng(lngItemTypeID) = SUR_ITEM_SINGLE_SELECT_OPTIONS Then
  606.                         If Len(Trim(Request.Form("txtAnswerValue" & i))) = 0 Then
  607.                             lngAnswerValue = "Null"
  608.                         Else
  609.                             lngAnswerValue = Request.Form("txtAnswerValue" & i)
  610.                         End If
  611.                     Else
  612.                         lngAnswerValue = "Null"
  613.                     End If
  614.  
  615.                     'Generate the correct SQL depending on the case.  If this is an INSERT, always do a straight insert.  
  616.                     If strState = SUR_STATE_INSERT Or strState = SUR_STATE_INSERT_REFRESH Then
  617.                         'Insert the question into the SUR_ITEM_ANSWER table
  618.                         strSQL = "INSERT INTO sur_item_answer(item_id, answer_id, answer_text, answer_value, order_number, " & _
  619.                                     "default_yn) VALUES(" & _
  620.                                     lngItemID & ", " & lngAnswerID & ", " & SQLEncode(Request.Form("txtAnswer" & i)) & ", " & _ 
  621.                                     lngAnswerValue & ", " & lngOrderNumber & ", " & SQLEncode(flgDefaultValue) & ")"
  622.                     Else 
  623.                         'In EDIT mode, update the previous entry if it's being changed, delete extra entries if 
  624.                         'there are any, and insert any new entries
  625.  
  626.                         'If the current order number is less than or equal to the previous answer count, update the 
  627.                         'previous answer based on the order number
  628.                         If lngOrderNumber <= lngPreviousAnswerCount Then
  629.                             'In EDIT mode, if the counter (i) is bigger than the the current order number (lngOrderNumber), 
  630.                             'the user deleted an answer from the middle of the answer list.  Therefore, the other answers
  631.                             'need to be updated based on an adjusted order number
  632.                             If i > lngOrderNumber Then
  633.                                 strSQL = "UPDATE sur_item_answer " & _
  634.                                             "SET answer_text = " & SQLEncode(Request.Form("txtAnswer" & i)) & ", " & _
  635.                                             "answer_value = " & lngAnswerValue & ", " & _
  636.                                             "default_yn = " & SQLEncode(flgDefaultValue) & ", " & _
  637.                                             "order_number = order_number - " & i - lngOrderNumber & _
  638.                                             " WHERE item_id = " & lngItemID & _
  639.                                             " AND order_number = " & i
  640.                             Else
  641.                                 strSQL = "UPDATE sur_item_answer " & _
  642.                                             "SET answer_text = " & SQLEncode(Request.Form("txtAnswer" & i)) & ", " & _
  643.                                             "answer_value = " & lngAnswerValue & ", " & _
  644.                                             "default_yn = " & SQLEncode(flgDefaultValue) & _
  645.                                             " WHERE item_id = " & lngItemID & _
  646.                                             " AND order_number = " & lngOrderNumber
  647.                             End If
  648.                         Else 'This is an additional (new) answer, so do an insert
  649.                             strSQL = "INSERT INTO sur_item_answer(item_id, answer_id, answer_text, answer_value, order_number, " & _
  650.                                         "default_yn) VALUES(" & _
  651.                                         lngItemID & ", " & lngAnswerID & ", " & SQLEncode(Request.Form("txtAnswer" & i)) & ", " & _ 
  652.                                         lngAnswerValue & ", " & lngOrderNumber & ", " & SQLEncode(flgDefaultValue) & ")"
  653.                         End If                    
  654.                     End If
  655.                     conItem.Execute ConvertSQL(strSQL), , adCmdText
  656.                     
  657.                     'Increment the order number
  658.                     lngOrderNumber = lngOrderNumber + 1
  659.                 Else
  660.                     'If this is EDIT mode, and an answer was removed from the middle of the answers, delete the answer
  661.                     strSQL = "DELETE FROM sur_item_answer " & _
  662.                                 " WHERE item_id = " & lngItemID & _
  663.                                 " AND order_number = " & i
  664.                     conItem.Execute ConvertSQL(strSQL), , adCmdText
  665.                 End If
  666.             Next
  667.             
  668.             'If this is EDIT mode, and the current answer count (determined by the lngOrderNumber variable) is less than
  669.             'the previous answer count (determined by the lngPreviousAnswerCount variable), delete the extra answers
  670.             If strState = SUR_STATE_EDIT Or strState = SUR_STATE_EDIT_REFRESH Then
  671.                 'Subtract 1 from the lngOrderNumber variable because it was already incremented, above, in anticipation of 
  672.                 'the next question
  673.                 If (lngOrderNumber - 1) < lngPreviousAnswerCount Then
  674.                     strSQL = "DELETE FROM sur_item_answer " & _
  675.                                 " WHERE item_id = " & lngItemID & _
  676.                                 " AND order_number >= " & lngOrderNumber
  677.                     conItem.Execute ConvertSQL(strSQL), , adCmdText
  678.                 End If
  679.             End If
  680.         Else 'Item is not a combo, radio buttons, or checkboxes
  681.             'If the page is in edit mode, and the question does not have multiple answers, it's possible that the 
  682.             'user changed from a question that did have multiple answers.  It's faster and easier just to delete these
  683.             'answers, if they exist, than it is to check and see if they actually exist
  684.             If strState = SUR_STATE_EDIT Or strState = SUR_STATE_EDIT_REFRESH Then
  685.                 strSQL = "DELETE FROM sur_item_answer WHERE item_id = " & lngItemID
  686.                 conItem.Execute ConvertSQL(strSQL), , adCmdText
  687.             End If
  688.         End If
  689.     End If
  690.     
  691.     'Clean up
  692.     conItem.Close
  693.     Set conItem = Nothing
  694.  
  695.     'Redirect to either the modify survey or modify library page for the survey or library.
  696.     If flgLibrary = True Then
  697.         Response.Redirect "ModifyLibrary.asp?LibraryID=" & lngLibraryID & "&#ItemID" & lngItemID    
  698.     Else
  699.         Response.Redirect "ModifySurvey.asp?SurveyID=" & lngSurveyID & "&#ItemID" & lngItemID    
  700.     End If
  701. %>