home *** CD-ROM | disk | FTP | other *** search
/ bombers.k12.ar.us / bombers.k12.ar.us.tar / bombers.k12.ar.us / survey_unconfigured / DeleteItem.asp < prev    next >
Text File  |  2006-10-25  |  7KB  |  172 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 deletes an item from a survey, and then
  7. '                     redirects the user back to the modify survey screen.
  8. '
  9. '   COPYRIGHT NOTICE                                
  10. '
  11. '   See attached Software License Agreement
  12. '
  13. '   (c) Copyright 2002 - 2006 by ClassApps.com.  All rights reserved.
  14. '***********************************************************************
  15. %>
  16. <!--#Include File="Include/Config_inc.asp"-->
  17. <!--#Include File="Include/Utility_inc.asp"-->
  18. <!--#Include File="Include/adovbs_inc.asp"-->
  19. <!--#Include File="Include/CurrentUser_inc.asp"-->
  20. <!--#Include File="Include/SurveySecurity_inc.asp"-->
  21. <!--#Include File="Include/SurveyUtility_inc.asp"-->
  22. <!--#Include File="Include/Constants_inc.asp"-->
  23. <%
  24.     Dim strSQL
  25.     Dim conDelete
  26.     Dim lngItemID
  27.     Dim lngOrderNumber
  28.     Dim lngSurveyID
  29.     Dim lngLibraryID
  30.     Dim lngPageNumber
  31.     Dim rsOrderNumber
  32.     Dim rsItemCount
  33.     Dim lngItemCount
  34.     Dim flgLibrary
  35.  
  36.     'Initialization
  37.     lngItemID = Request.QueryString("ItemID")
  38.     lngOrderNumber = Request.QueryString("OrderNumber")
  39.     Set conDelete = Server.CreateObject("ADODB.Connection")
  40.     Set rsItemCount = Server.CreateObject("ADODB.Recordset")
  41.     conDelete.Open SURVEY_APP_CONNECTION
  42.     
  43.     'Check to see if this is an item for a survey or a library
  44.     If Len(Request.QueryString("SurveyID")) > 0 Then
  45.         lngSurveyID = Request.QueryString("SurveyID")
  46.         lngPageNumber = Request.QueryString("PageNumber")
  47.         flgLibrary = False
  48.  
  49.         'In order to delete an item, the user must be the survey owner or an admin.
  50.         If IsUserOwnerOrAdmin(lngSurveyID) = False Then
  51.             Response.Redirect "AccessDenied.asp?SurveyID=" & lngSurveyID & "&Reason=" & SUR_ACCESS_DENIED_NOT_OWNER
  52.         End If
  53.  
  54.         'Do not allow a user to delete an item from a survey that already has responses.  Instead, redirect the user back
  55.         'to the modify survey page
  56.         If HasResponses(lngSurveyID) = True Then
  57.             Response.Redirect "ModifySurvey.asp?SurveyID=" & lngSurveyID
  58.         End If
  59.  
  60.         'Get the number of items on the page that contains the item to be deleted
  61.         strSQL = "SELECT count(item_id) As ItemCount " & _
  62.                     "FROM sur_survey_to_item_mapping " & _
  63.                     "WHERE survey_id = " & lngSurveyID & _
  64.                     " AND page_number = " & lngPageNumber
  65.         rsItemCount.Open ConvertSQL(strSQL), conDelete, adOpenForwardOnly, adLockReadOnly, adCmdText
  66.         rsItemCount.MoveFirst
  67.         lngItemCount = CLng(rsItemCount("ItemCount"))
  68.         rsItemCount.Close
  69.         Set rsItemCount = Nothing
  70.  
  71.         'If the order number was not passed in on the URL, get it from the database.
  72.         If Len(Request.QueryString("OrderNumber")) = 0 Then
  73.             Set rsOrderNumber = Server.CreateObject("ADODB.Recordset")
  74.             strSQL = "SELECT order_number FROM sur_survey_to_item_mapping WHERE item_id = " & lngItemID & " AND survey_id = " & lngSurveyID
  75.             rsOrderNumber.Open ConvertSQL(strSQL), conDelete, adOpenDynamic, , adCmdText
  76.             rsOrderNumber.MoveFirst
  77.             lngOrderNumber = rsOrderNumber("order_number")
  78.             rsOrderNumber.Close
  79.             Set rsOrderNumber = Nothing
  80.         End If
  81.     Else 'Library
  82.         lngLibraryID = Request.QueryString("LibraryID")
  83.         flgLibrary = True
  84.     End If
  85.  
  86.     'For surveys, if any page conditions were dependent on this item, delete those page conditions
  87.     If flgLibrary = False Then
  88.         strSQL = "DELETE FROM sur_page_condition WHERE dependent_item_id = " & lngItemID
  89.         conDelete.Execute ConvertSQL(strSQL), , adCmdText
  90.     End If
  91.     
  92.     'Delete the item from the SUR_ITEM table
  93.     strSQL = "DELETE FROM sur_item WHERE item_id = " & lngItemID
  94.     conDelete.Execute ConvertSQL(strSQL), , adCmdText
  95.  
  96.     'Delete all of the answer for the item from the item answer table (SUR_ITEM_ANSWER)
  97.     strSQL = "DELETE FROM sur_item_answer WHERE item_id = " & lngItemID
  98.     conDelete.Execute ConvertSQL(strSQL), , adCmdText
  99.  
  100.     'Delete all of the row names for the item from the subitem table (SUR_SUBITEM)
  101.     strSQL = "DELETE FROM sur_subitem WHERE item_id = " & lngItemID
  102.     conDelete.Execute ConvertSQL(strSQL), , adCmdText
  103.     
  104.     'Delete the entry for this item in the mapping table (SUR_SURVEY_TO_ITEM_MAPPING or SUR_LIBRARY_TO_ITEM_MAPPING)
  105.     If flgLibrary = True Then
  106.         strSQL =  "DELETE FROM sur_library_to_item_mapping WHERE item_id = " & lngItemID & " AND library_id = " & lngLibraryID
  107.     Else
  108.         strSQL =  "DELETE FROM sur_survey_to_item_mapping WHERE item_id = " & lngItemID & " AND survey_id = " & lngSurveyID
  109.     End If
  110.     conDelete.Execute ConvertSQL(strSQL), , adCmdText
  111.  
  112.     'Move all the items below the one just deleted up one
  113.     If flgLibrary = True Then
  114.         strSQL = "UPDATE sur_library_to_item_mapping SET order_number = order_number - 1" & _
  115.                     " WHERE library_id = " & lngLibraryID & _
  116.                     " AND order_number > " & lngOrderNumber
  117.     Else
  118.         strSQL = "UPDATE sur_survey_to_item_mapping SET order_number = order_number - 1" & _
  119.                     " WHERE survey_id = " & lngSurveyID & _
  120.                     " AND order_number > " & lngOrderNumber
  121.     End If    
  122.     conDelete.Execute ConvertSQL(strSQL), , adCmdText
  123.  
  124.     'For surveys, if the item deleted was the only item on the page, move all of the other pages up one.
  125.     If flgLibrary = False Then
  126.         If lngItemCount = 1 Then
  127.             'Delete any page conditions for the page
  128.             strSQL = "DELETE from sur_page_condition " & _
  129.                         "WHERE survey_id = " & lngSurveyID & _
  130.                         " AND page_number = " & lngPageNumber
  131.             conDelete.Execute ConvertSQL(strSQL), , adCmdText
  132.  
  133.             'Drop the page number for all page conditions for this survey that are greater than the page just deleted
  134.             strSQL = "UPDATE sur_page_condition " & _
  135.                         "SET page_number = page_number - 1 " & _
  136.                         "WHERE survey_id = " & lngSurveyID & _
  137.                         " AND page_number > " & lngPageNumber
  138.             conDelete.Execute ConvertSQL(strSQL), , adCmdText
  139.                             
  140.             'Delete any page property entries
  141.             strSQL = "DELETE from sur_page " & _
  142.                         "WHERE survey_id = " & lngSurveyID & _
  143.                         " AND page_number = " & lngPageNumber
  144.             conDelete.Execute ConvertSQL(strSQL), , adCmdText
  145.  
  146.             'Drop the page number for all page property entries for this survey that are greater than the page just deleted
  147.             strSQL = "UPDATE sur_page " & _
  148.                         "SET page_number = page_number - 1 " & _
  149.                         "WHERE survey_id = " & lngSurveyID & _
  150.                         " AND page_number > " & lngPageNumber
  151.             conDelete.Execute ConvertSQL(strSQL), , adCmdText
  152.  
  153.             'Move all the items below the ones on the page just deleted up.  These items should be moved up the same number of 
  154.             'positions as the number of items deleted
  155.             strSQL = "UPDATE sur_survey_to_item_mapping SET page_number = page_number - " & lngItemCount & _
  156.                         " WHERE survey_id = " & lngSurveyID & _
  157.                         " AND page_number > " & lngPageNumber
  158.             conDelete.Execute ConvertSQL(strSQL), , adCmdText
  159.         End If
  160.     End If
  161.         
  162.     'Clean up
  163.     conDelete.Close
  164.     Set conDelete = Nothing
  165.  
  166.     'Redirect to either the modify survey page for the survey or the modify library page for the library.
  167.     If flgLibrary = True Then
  168.         Response.Redirect "ModifyLibrary.asp?LibraryID=" & lngLibraryID
  169.     Else
  170.         Response.Redirect "ModifySurvey.asp?SurveyID=" & lngSurveyID
  171.     End If
  172. %>