USP_STUDENTCOURSEREQUEST_UPDATEFROMXML

Adds the specified student course requests.

Parameters

Parameter Parameter Type Mode Description
@CURRENTAPPUSERID uniqueidentifier IN
@ID uniqueidentifier IN
@XML xml IN
@CHANGEAGENTID uniqueidentifier IN
@CHANGEDATE datetime IN

Definition

Copy


CREATE procedure dbo.USP_STUDENTCOURSEREQUEST_UPDATEFROMXML
(
    @CURRENTAPPUSERID uniqueidentifier,
    @ID uniqueidentifier,
    @XML xml,
    @CHANGEAGENTID uniqueidentifier = null,
    @CHANGEDATE datetime = null
)
as
begin
    set nocount on;

    if @CHANGEAGENTID is null
        exec USP_CHANGEAGENT_GETORCREATECHANGEAGENT @CHANGEAGENTID output

    if @CHANGEDATE is null 
        set @CHANGEDATE = getdate()

    declare @SESSION_STARTDATE date
    declare @SESSION_ENDDATE date

    select
        @SESSION_STARTDATE = min(STARTDATE),
        @SESSION_ENDDATE = max(ENDDATE)
    from dbo.TERM
        inner join dbo.APPUSERSESSION on TERM.SESSIONID = APPUSERSESSION.SESSIONID
    where APPUSERSESSION.ID = @CURRENTAPPUSERID
    group by APPUSERSESSION.ID

    -- build a temporary table containing the values from the XML

    declare @TempTbl table (
       [ID] uniqueidentifier,
       [COURSEID] uniqueidentifier,
       [STARTTERMID] uniqueidentifier)

    insert into @TempTbl select 
        [ID],
        [COURSEID],
        [STARTTERMID] 
    from dbo.UFN_GETSTUDENTCOURSEREQUESTS_FROMITEMLISTXML(@XML)

    update @TempTbl set ID = newid() where (ID is null) or (ID = '00000000-0000-0000-0000-000000000000');

    if @@Error <> 0
        return 1;

    declare @contextCache varbinary(128);
    declare @e int;

    -- cache current context information 

    set @contextCache = CONTEXT_INFO();

    -- set CONTEXT_INFO to @CHANGEAGENTID 

    if not @CHANGEAGENTID is null
        set CONTEXT_INFO @CHANGEAGENTID;

    -- delete any items that no longer exist in the XML table

    delete from dbo.[STUDENTCOURSEREQUEST] where [STUDENTCOURSEREQUEST].ID in 
        (
            select SCR.ID
            from dbo.UFN_GETSTUDENTCOURSEREQUESTS(@CURRENTAPPUSERID, @ID) SCR
            where not exists(
                    select 1
                    from dbo.STUDENTCLASSMEETINGGROUP
                    where STUDENTCOURSEID = SCR.ID
                        and STUDENTCLASSMEETINGGROUP.STATUSCODE != 0
                )            
            EXCEPT select ID from @TempTbl
        )    

    select @e=@@error;

    -- reset CONTEXT_INFO to previous value 

    if not @contextCache is null
        set CONTEXT_INFO @contextCache;

    if @e <> 0
        return 2;

    -- update the items that exist in the XML table and the db

    update [STUDENTCOURSEREQUEST] set
        [STUDENTCOURSEREQUEST].[STARTDATE] = @SESSION_STARTDATE,
        [STUDENTCOURSEREQUEST].[ENDDATE] = @SESSION_ENDDATE,
        [STUDENTCOURSEREQUEST].[CLASSSTARTDATE] = TERM.[STARTDATE],
        [STUDENTCOURSEREQUEST].CHANGEDBYID = @CHANGEAGENTID,
        [STUDENTCOURSEREQUEST].DATECHANGED = @CHANGEDATE
    from dbo.[STUDENTCOURSEREQUEST]
        inner join @TempTbl as [temp] on [STUDENTCOURSEREQUEST].ID = [temp].ID
        left join dbo.TERM on [temp].STARTTERMID = TERM.ID
    where ([STUDENTCOURSEREQUEST].[CLASSSTARTDATE]<>TERM.[STARTDATE]) or 
        ([STUDENTCOURSEREQUEST].[CLASSSTARTDATE] is null and temp.[STARTTERMID] is not null) or 
        ([STUDENTCOURSEREQUEST].[CLASSSTARTDATE] is not null and temp.[STARTTERMID] is null)

    if @@Error <> 0
        return 3;    

    -- insert new items

    insert into [STUDENTCOURSE]
        ([ID],
        [STUDENTID],
        [COURSEID],                
        ADDEDBYID, 
        CHANGEDBYID, 
        DATEADDED, 
        DATECHANGED)
    select
        [temp].[ID],
        @ID,
        [temp].[COURSEID], 
        @CHANGEAGENTID
        @CHANGEAGENTID
        @CHANGEDATE
        @CHANGEDATE
    from @TempTbl as [temp]
    where not exists (select ID from dbo.[STUDENTCOURSE] as data where data.ID = [temp].ID)

    insert into [STUDENTCOURSEREQUEST] 
        ([ID],
        [STARTDATE],
        [ENDDATE],
        [CLASSSTARTDATE],
        ADDEDBYID, 
        CHANGEDBYID, 
        DATEADDED, 
        DATECHANGED)
    select
        [temp].[ID],
        @SESSION_STARTDATE,
        @SESSION_ENDDATE,
        TERM.STARTDATE,
        @CHANGEAGENTID
        @CHANGEAGENTID
        @CHANGEDATE
        @CHANGEDATE
    from @TempTbl as [temp]
        left join dbo.TERM on [temp].STARTTERMID = TERM.ID
    where not exists (select ID from dbo.[STUDENTCOURSEREQUEST] as data where data.ID = [temp].ID)

    if @@Error <> 0
        return 4;

    -- cache current context information 

    set @contextCache = CONTEXT_INFO();

    -- set CONTEXT_INFO to @CHANGEAGENTID 

    if not @CHANGEAGENTID is null
        set CONTEXT_INFO @CHANGEAGENTID;

    -- Remove unused Student Course Records

    delete from dbo.STUDENTCOURSE
    from dbo.STUDENTCOURSE
        left join dbo.STUDENTCOURSEREQUEST on STUDENTCOURSEREQUEST.ID = STUDENTCOURSE.ID
            and STUDENTCOURSEREQUEST.STARTDATE >= @SESSION_STARTDATE
            and STUDENTCOURSEREQUEST.ENDDATE <= @SESSION_ENDDATE
        left join dbo.STUDENTCLASSMEETINGGROUP on STUDENTCOURSE.ID = STUDENTCLASSMEETINGGROUP.STUDENTCOURSEID
    where STUDENTCOURSE.STUDENTID = @ID
        and STUDENTCOURSEREQUEST.ID is null
        and STUDENTCLASSMEETINGGROUP.ID is null

    select @e=@@error;

    -- reset CONTEXT_INFO to previous value 

    if not @contextCache is null
        set CONTEXT_INFO @contextCache;

    if @e <> 0
        return 5;

    return 0;

end