Guest User

Untitled

a guest
Aug 4th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 7.26 KB | None | 0 0
  1. USE [SDS_DevSchoolDistrict]
  2. GO
  3. /****** Object:  UserDefinedFunction [dbo].[WidgetDataForStudentActivitiesAsXml]    Script Date: 05/03/2012 19:00:10 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8.  
  9.  
  10. ALTER FUNCTION [dbo].[WidgetDataForStudentActivitiesForPrintAsXml]
  11. (       @EntityId       int,
  12.         @IncludeHeader  bit = 1,
  13.         @DataViewMode varchar(200) = 'Standard',
  14.         @EncryptIds bit = 0,
  15.         @ApplicationUserId int,
  16.         @ApplicationUserPersonRoleId int = 1
  17. )
  18. RETURNS Xml
  19. AS
  20. BEGIN
  21.  
  22.         /*---------------------------------------------
  23.         -- Debugging
  24.         ---------------------------------------------
  25.         --SELECT    SEE.StudentId, A.ActivityId, A.ActivityTitle, A.SchoolYearId, COUNT(DISTINCT StudentId)
  26.         --FROM  Activities A
  27.         --JOIN  StudentEntityEnrollments SEE
  28.         --  ON  SEE.EntityId = A.ActivityId
  29.         ---------------------------------------------
  30.         --SELECT dbo.WidgetDataForStudentActivitiesForPrintAsXml(10004, 1, DEFAULT, DEFAULT, 22, 1)
  31.         ---------------------------------------------
  32.         DECLARE @EntityId           int             SET @EntityId = 10004
  33.         DECLARE @SchoolYearId       int             SET @SchoolYearId = 12
  34.         DECLARE @DataViewMode       varchar(200)
  35.         DECLARE @EncryptIds         bit
  36.         DECLARE @ApplicationUserId  int
  37.         DECLARE @ApplicationUserPersonRoleId int
  38.         ---------------------------------------------------*/
  39.  
  40.  
  41.         ---------------------------------------------
  42.         -- Prepare for performance metrics
  43.         ---------------------------------------------
  44.         DECLARE @XmlAssemblyStartTime time
  45.         DECLARE @XmlAssemblyEndTime time
  46.         DECLARE @XmlAssemblyTimeInMilliseconds int
  47.         SET @XmlAssemblyStartTime = GETDATE()
  48.         ---------------------------------------------
  49.  
  50.  
  51.  
  52.  
  53.         ---------------------------------------------
  54.         -- Declare the return variable here
  55.         ---------------------------------------------
  56.         DECLARE @Xml Xml = '<Xml/>'
  57.         ---------------------------------------------
  58.  
  59.  
  60.         ---------------------------------------------
  61.         -- Ensure access is valid
  62.         ---------------------------------------------
  63.         DECLARE @ApplicationSecurityPermissionId int
  64.  
  65.         SELECT  @ApplicationSecurityPermissionId = ApplicationSecurityPermissionId
  66.         FROM    ApplicationUserPermissionsByApplicationUser(@EntityId,8,@ApplicationUserId,@ApplicationUserPersonRoleId,1) AUP
  67.         JOIN    SecurableItems SI
  68.           ON    SI.SecurableItem = 'Student Activities'
  69.          AND    SI.MinimumApplicationSecurityPermissionId <= AUP.ApplicationSecurityPermissionId
  70.        
  71.         IF @ApplicationSecurityPermissionId IS NULL
  72.             GOTO ALLDONE
  73.  
  74.  
  75.  
  76.         ----------------------------------------------------
  77.         --
  78.         ----------------------------------------------------
  79.         DECLARE @SchoolYearId int = dbo.SchoolYearIdValue(NULL)
  80.         ----------------------------------------------------
  81.  
  82.  
  83.  
  84.         /*---------------------------------------------
  85.         DECLARE @StudentActivities Xml
  86.         ---------------------------------------------
  87.         SET @StudentActivities = (                 
  88.                 SELECT 
  89.                         CASE @EncryptIds
  90.                             WHEN 1
  91.                               THEN CAST(ActivityEncryptedIds.EncryptedId as varchar(200))
  92.                             ELSE CAST(A.ActivityId as varchar(200))
  93.                         END AS '@EntityId',
  94.                         A.ActivityTitle AS '@Title',
  95.                         A.ActivityDescription AS '@Description'
  96.                 FROM    dbo.DataViewModeActivities(NULL,@DataViewMode) A
  97.                 JOIN    StudentEntityEnrollments S_A
  98.                   ON    A.ActivityId = S_A.EntityId
  99.                  AND    S_A.StudentId = @EntityId
  100.                  AND    A.SchoolYearId = @SchoolYearId
  101.                  AND    ISNULL(S_A.StudentEntityEnrollmentStopDate,A.ActivityEndDate) >= A.ActivityEndDate
  102.                 JOIN    EncryptedIds ActivityEncryptedIds
  103.                   ON    A.ActivityId = ActivityEncryptedIds.Id
  104.                 FOR XML PATH('StudentActivity'), ROOT('StudentActivities')
  105.         )
  106.         ---------------------------------------------
  107.         --SELECT @StudentActivities AS '@StudentActivities'
  108.         */---------------------------------------------
  109.  
  110.  
  111.  
  112.         ---------------------------------------------
  113.         DECLARE @Entity Xml
  114.         DECLARE @PersonRoleId int
  115.         ---------------------------------------------
  116.         SELECT  @PersonRoleId = PersonRoleId
  117.         FROM    PersonRoles PR
  118.         WHERE   PR.PersonRole LIKE '%Student%'
  119.         ---------------------------------------------
  120.         IF(@IncludeHeader = 1)
  121.             SET @Entity = dbo.WidgetDataForEntityProfileAsXml(@EntityId, @PersonRoleId, @SchoolYearId, @DataViewMode, @EncryptIds, @ApplicationUserId, @ApplicationUserPersonRoleId)
  122.         ---------------------------------------------
  123.         --SELECT @Entity AS '@Entity'
  124.         ---------------------------------------------
  125.  
  126.  
  127.         ---------------------------------------------
  128.         DECLARE @StudentActivities Xml
  129.         ---------------------------------------------
  130.         SET @StudentActivities = (                 
  131.             SELECT  SchoolYear.*,
  132.                     ---------------
  133.                     StudentActivity.*
  134.             FROM    SchoolYears SchoolYear
  135.             JOIN    (
  136.                     SELECT  CASE @EncryptIds
  137.                                 WHEN 1
  138.                                   THEN CAST(ActivityEncryptedIds.EncryptedId as varchar(200))
  139.                                 ELSE CAST(A.ActivityId as varchar(200))
  140.                             END AS 'EntityId',
  141.                             A.ActivityTitle AS 'Title',
  142.                             A.ActivityDescription AS 'Description',
  143.                             A.SchoolYearId
  144.                     FROM    dbo.DataViewModeActivities(NULL,@DataViewMode) A
  145.                     JOIN    StudentEntityEnrollments S_A
  146.                       ON    A.ActivityId = S_A.EntityId
  147.                      AND    S_A.StudentId = @EntityId
  148.                      AND    A.SchoolYearId = @SchoolYearId
  149.                      AND    ISNULL(S_A.StudentEntityEnrollmentStopDate,A.ActivityEndDate) >= A.ActivityEndDate
  150.                     JOIN    EncryptedIds ActivityEncryptedIds
  151.                       ON    A.ActivityId = ActivityEncryptedIds.Id
  152.                     --FOR XML PATH('StudentActivity'), ROOT('StudentActivities')
  153.                     ) StudentActivity
  154.               ON    SchoolYear.SchoolYearId = StudentActivity.SchoolYearId
  155.             ORDER
  156.                BY   SchoolYear.SchoolYearId DESC
  157.             FOR XML AUTO , ROOT('SchoolYears')
  158.         )
  159.         ---------------------------------------------
  160.         --SELECT @StudentActivities AS '@StudentActivities'
  161.         ---------------------------------------------
  162.  
  163.  
  164.  
  165.  
  166.         ---------------------------------------------
  167.         --DECLARE @SchoolYears Xml
  168.         ---------------------------------------------
  169.         --SET @SchoolYears = dbo.SchoolYearsByStudentAsXml(@EntityId, @DataViewMode, @EncryptIds, @ApplicationUserId, @ApplicationUserPersonRoleId)
  170.         ---------------------------------------------
  171.         --SELECT @SchoolYears AS '@SchoolYears'
  172.         ---------------------------------------------
  173.  
  174.  
  175.  
  176.  
  177.  
  178.         ---------------------------------------------
  179.         -- Assemble output Xml
  180.         ---------------------------------------------
  181.         --SET @Xml = dbo.XmlAdopt(@Xml, @StudentActivities, 'Xml', 'StudentActivity')
  182.         --SET @Xml = dbo.XmlAdopt(@Xml, @SchoolYears, 'Xml', 'SchoolYear')
  183.         --SET @Xml = dbo.XmlNormalize(@Xml)
  184.         ---------------------------------------------
  185.         SET @Xml = dbo.XmlAdopt(@Xml, @StudentActivities, 'Xml', 'SchoolYears')
  186.         SET @Xml = dbo.XmlAdopt(@Xml, @Entity, 'Xml', 'Entity')
  187.         ---------------------------------------------
  188.  
  189.  
  190. ALLDONE:
  191.         ---------------------------------------------
  192.         -- Track Assembly time
  193.         ---------------------------------------------
  194.         SET @XmlAssemblyEndTime = GETDATE()
  195.         SET @XmlAssemblyTimeInMilliseconds = DATEDIFF(ms, @XmlAssemblyStartTime, @XmlAssemblyEndTime)
  196.         SET @Xml = dbo.XmlAssignAttributeValues(@Xml, '//Xml', 'xmlAssemblyTimeInMilliseconds', @XmlAssemblyTimeInMilliseconds)
  197.         ---------------------------------------------
  198.  
  199.  
  200.     --SELECT @Xml AS '@Xml'
  201.     RETURN @Xml
  202. END
  203. GO
  204.  
  205. SELECT dbo.WidgetDataForStudentActivitiesForPrintAsXml(10004, 1, DEFAULT, DEFAULT, 22, 1)
Add Comment
Please, Sign In to add comment