Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 5.92 KB | None | 0 0
  1. USE [SDS_DevSchoolDistrict]
  2. GO
  3. /****** Object:  UserDefinedFunction [dbo].[SpotlightabilityBySpotlighterAndSpotlightee]    Script Date: 02/09/2011 18:11:53 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. /*
  9. */
  10. -- =================================================================================
  11. -- Copyright:   School Data Solutions, LLC
  12. -- =================================================================================
  13. ALTER FUNCTION [dbo].[SpotlightabilityBySpotlighterAndSpotlightee]
  14. (   @ApplicationUserId      int,
  15.     @SpotlighteePersonId    int,
  16.     @SchoolYearId           int = NULL
  17. )
  18. RETURNS @OutputTable TABLE
  19. (       StudentIsSpotlightable bit NULL,
  20.         TeacherIsSpotlightable bit NULL,
  21.         PrincipalIsSpotlightable bit NULL
  22. )
  23. AS
  24. BEGIN   -- =========================================================================
  25.  
  26.  
  27.         ----------------------------------------------------
  28.         -- If no School-Year passed, assume current school year
  29.         ----------------------------------------------------
  30.         IF(@SchoolYearId IS NULL) SET @SchoolYearId = dbo.SchoolYearIdValue(GetDate())
  31.         ----------------------------------------------------
  32.  
  33.  
  34.  
  35.  
  36.         --------------------------------------------------
  37.         --
  38.         --------------------------------------------------
  39.         DECLARE @SpotlighterPersonId int
  40.         --------------------------------------------------
  41.         SELECT  @SpotlighterPersonId = PersonId
  42.         FROM    ApplicationUsers U
  43.         WHERE   U.ApplicationUserId = @ApplicationUserId
  44.         --------------------------------------------------
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.         --------------------------------------------------
  52.         -- Determine whether the Spotlight-ee is a Student
  53.         --------------------------------------------------
  54.         DECLARE @SpotlighteeIsStudent bit
  55.         --------------------------------------------------
  56.         IF(EXISTS(SELECT 1 FROM Students WHERE StudentId = @SpotlighteePersonId))
  57.             SET @SpotlighteeIsStudent = 1
  58.         --------------------------------------------------
  59.  
  60.  
  61.         --------------------------------------------------
  62.         -- Determine whether the Spotlight-ee is a Teacher
  63.         --------------------------------------------------
  64.         DECLARE @SpotlighteeIsTeacher bit
  65.         --------------------------------------------------
  66.         IF(EXISTS(SELECT 1 FROM Teachers WHERE TeacherId = @SpotlighteePersonId))
  67.             SET @SpotlighteeIsTeacher = 1
  68.         --------------------------------------------------
  69.  
  70.         --------------------------------------------------
  71.         -- Determine whether the Spotlight-ee is a Principal
  72.         --------------------------------------------------
  73.         DECLARE @SpotlighteeIsPrincipal bit
  74.         --------------------------------------------------
  75.         IF(EXISTS(SELECT 1 FROM Principals WHERE PrincipalId = @SpotlighteePersonId))
  76.             SET @SpotlighteeIsPrincipal = 1
  77.         --------------------------------------------------
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.         --------------------------------------------------
  85.         -- Determine whether the Spotlight-er is an Administrator
  86.         --------------------------------------------------
  87.         DECLARE @SpotlighterIsAdministrator bit
  88.         --------------------------------------------------
  89.         IF(EXISTS(SELECT 1 FROM Administrators WHERE AdministratorId = @SpotlighterPersonId))
  90.             SET @SpotlighterIsAdministrator = 1
  91.         --------------------------------------------------
  92.  
  93.  
  94.         --------------------------------------------------
  95.         -- Determine whether the Spotlight-er is a Teacher
  96.         --------------------------------------------------
  97.         DECLARE @SpotlighterIsTeacher bit
  98.         --------------------------------------------------
  99.         IF(EXISTS(SELECT 1 FROM Teachers WHERE TeacherId = @SpotlighterPersonId))
  100.             SET @SpotlighterIsTeacher = 1
  101.         --------------------------------------------------
  102.  
  103.         --------------------------------------------------
  104.         -- Determine whether the Spotlight-er is a Principal
  105.         --------------------------------------------------
  106.         DECLARE @SpotlighterIsPrincipal bit
  107.         --------------------------------------------------
  108.         IF(EXISTS(SELECT 1 FROM Principals WHERE PrincipalId = @SpotlighterPersonId))
  109.             SET @SpotlighterIsPrincipal = 1
  110.         --------------------------------------------------
  111.  
  112.  
  113.  
  114.         DECLARE @StudentIsSpotlightable int
  115.         DECLARE @TeacherIsSpotlightable int
  116.         DECLARE @PrincipalIsSpotlightable int
  117.  
  118.  
  119.  
  120.  
  121.         IF(@SpotlighterIsAdministrator = 1)
  122.         BEGIN   -----------------------------------------------
  123.            
  124.                 IF(@SpotlighteeIsStudent = 1) SET @StudentIsSpotlightable = 1
  125.                 IF(@SpotlighteeIsTeacher = 1) SET @TeacherIsSpotlightable = 1
  126.                 IF(@SpotlighteeIsPrincipal = 1) SET @PrincipalIsSpotlightable = 1
  127.        
  128.         END     -----------------------------------------------
  129.         ELSE IF(@SpotlighteeIsPrincipal = 1)
  130.         BEGIN
  131.            
  132.                 IF(@SpotlighteeIsStudent = 1) SET @StudentIsSpotlightable = 1
  133.                 IF(@SpotlighteeIsTeacher = 1) SET @TeacherIsSpotlightable = 1
  134.                 IF(@SpotlighteeIsPrincipal = 1) SET @PrincipalIsSpotlightable = 0
  135.        
  136.         END     -----------------------------------------------
  137.         ELSE IF(@SpotlighterIsTeacher = 1)
  138.         BEGIN
  139.        
  140.                 IF(@SpotlighteeIsTeacher = 1) SET @TeacherIsSpotlightable = 1
  141.                 IF(@SpotlighteeIsPrincipal = 1) SET @PrincipalIsSpotlightable = 0
  142.                 IF(@SpotlighteeIsStudent = 1)
  143.                 BEGIN
  144.                         SET @StudentIsSpotlightable = 0
  145.  
  146.                         IF(EXISTS(  SELECT  1
  147.                                     FROM    ClassTeachingAssignments A
  148.                                     JOIN    Classes C
  149.                                       ON    A.TeacherId = @SpotlighterPersonId
  150.                                      AND    A.ClassId = C.ClassId
  151.                                      AND    C.SchoolYearId = @SchoolYearId
  152.                                     JOIN    StudentClassEnrollments E
  153.                                       ON    C.ClassId = C.ClassId
  154.                                      AND    E.StudentId = @SpotlighteePersonId  
  155.                         ))
  156.                         BEGIN
  157.                                 SET @StudentIsSpotlightable = 1
  158.                         END
  159.                
  160.                 END
  161.                
  162.            
  163.         END     -----------------------------------------------
  164.  
  165.  
  166.  
  167.  
  168.         INSERT  @OutputTable(
  169.                 StudentIsSpotlightable,
  170.                 TeacherIsSpotlightable,
  171.                 PrincipalIsSpotlightable
  172.         )
  173.         VALUES  (
  174.                 @StudentIsSpotlightable,
  175.                 @TeacherIsSpotlightable,
  176.                 @PrincipalIsSpotlightable
  177.         )              
  178.  
  179. RETURN
  180. END     -- =========================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement