﻿<?xml version="1.0" encoding="utf-8"?>
<ReportsExport>
  <Reports>
    <Report id="50cd5a4c-b1fb-41b4-8ed6-fb055e8427d1" codekey="SAP_Report" categoryCodekey="SpecialPurpose" name="SAP Report 2" description="">
      <MetaData created="2011-11-02T11:24:30" modified="2022-06-22T10:06:39" modifiedBy="ERGO Direkt, Administrator (Administrator)" modifiedBy_user_id="888" />
      <ExecutionDetails format="TableResult" commandType="SqlCommandOrQuery" exportHandler="" adminControl="" exportMultipleTablesToSheets="False" datesWithTime="False" extraParams="" />
      <Mandators mandatorMode="OnlyOwner" mandator_id="c5fd8bcc-2b1a-4f1d-b30d-9063c3f768cc" mandatorName="ERGODirekt" isStandard="False" isUsedByMenu="False" />
      <Roles>
        <Role id="90" />
      </Roles>
      <command>-- declare @current_mandator_id uniqueidentifier
        -- set @current_mandator_id = (select id from tblMandators where name='viwis')

        create table #Result
        (
        user_id int,
        title nvarchar(256),
        internal_name nvarchar(256),
        certificateReceived datetime,
        itemType int
        )

        declare @cursor cursor
        declare @theme_id uniqueidentifier
        declare @test_id uniqueidentifier
        declare @user_id int
        declare @certificateReceived datetime
        declare @passed int
        declare @title nvarchar(256)
        declare @internalName nvarchar(256)

        -- fetch state state for all themes
        set @cursor=cursor static for
        select tblStatusUserCourse.userId as [user_id], tblItems.Title as [title], tblItems.tag as [internalName], tblStatusUserCourse.theme_id as [theme_id]
        from tblStatusUserCourse
        inner join tblThemes on tblThemes.id=tblStatusUserCourse.theme_id
        INNER JOIN tblItems ON tblItems.id = tblThemes.id
        inner join v_Users on v_Users.intUserCn=tblStatusUserCourse.userId
        where tblItems.tag!=''
        AND tblItems.deleted IS NULL
        and v_Users.mandator_id=@current_mandator_id

        open @cursor
        fetch next from @cursor into @user_id, @title, @internalName, @theme_id
        while (@@fetch_status=0)
        begin
        set @certificateReceived = null
        if ( dbo.fn_GetUserItemStatus(@user_id, @theme_id)=2 )
        begin
        SET @certificateReceived = dbo.fn_GetUserModuleItemLastPassedDate (@user_id , @theme_id)
        end

        insert into #Result
        (user_id, title, internal_name, certificateReceived, itemType)
        values
        (@user_id, @title, @internalName, @certificateReceived, 0)

        fetch next from @cursor into @user_id, @title, @internalName, @theme_id
        end

        close @cursor
        deallocate @cursor

        -- fetch state state for all tests
        set @cursor=cursor static for
        select distinct
        tblTasStatsTestSessions.user_id as [user_id],
        tblItems.title as [title],
        tblItems.tag as [internal_name],
        tblTasTests.id as [test_id]
        from
        tblTasStatsTestSessions
        inner join tblTasTests on tblTasTests.id=tblTasStatsTestSessions.test_id
        inner join tblItems on tblTasTests.id=tblItems.id
        where
        tblItems.tag != ''
        and tblTasTests.mandator_id=@current_mandator_id
        and tblItems.deleted is null
        and tblTasTests.mode=1

        open @cursor
        fetch next from @cursor into @user_id, @title, @internalName, @test_id
        while (@@fetch_status=0)
        begin

        set @certificateReceived = null
        select top 1
        @certificateReceived=endDate
        from
        v_TblTasStatsTestSessions
        where
        test_id=@test_id and user_id=@user_id and passed=1
        order by endDate desc

        insert into #Result
        (user_id, title, internal_name, certificateReceived, itemType)
        values
        (@user_id, @title, @internalName, @certificateReceived, 1)

        fetch next from @cursor into @user_id, @title, @internalName, @test_id
        end

        close @cursor
        deallocate @cursor

        -- add modules which got status "green" someday
        insert into #Result
        (user_id, title, internal_name, certificateReceived, itemType)
        select
        tblStatusUserItemHistory.user_id,
        tblItems.title,
        tblItems.tag,
        tblStatusUserItemHistory.startDate,
        tblItems.itemType_id
        from tblStatusUserItemHistory
        join tblItems on tblitems.id = tblStatusUserItemHistory.item_id
        where tblitems.itemType_id = 1001
        and tblItems.mandator_id = @current_mandator_id
        and ISNULL(tblItems.tag,'') != ''
        and tblItems.deleted IS NULL
        and tblStatusUserItemHistory.status = 2

        -- add external items now
        -- Insert External Items now
        INSERT INTO #Result
        SELECT user_id,
        title,
        tag,
        tblExternalItems.enddate,
        itemType_id
        FROM tblStatusUserItemHistory
        JOIN tblExternalItems ON tblExternalItems.id = tblStatusUserItemHistory.item_id
        JOIN tblItems ON tblItems.id = tblExternalItems.id AND deleted IS NULL
        WHERE mandator_id = @current_mandator_id AND itemType_id IN(0, 1) AND tag IS NOT NULL AND LEN(tag) &gt; 0


		-- Build result

        select
        tblUsersContacts.email as 'emailadresse',
        #Result.title as 'Lernprogramm oder Test',
        #Result.internal_name as 'Interner_key',
        convert(nvarchar(32), #Result.certificateReceived, 104) as 'Datum_Zielerreichung'
        from
        #Result
        inner join v_Users on v_Users.intUserCn=#Result.user_id
        LEFT JOIN tblUsersContacts ON tblUsersContacts.user_id = v_Users.intUserCn AND tblUsersContacts.contactType_id = 1
        where not  #Result.certificateReceived is null

        order by
        [emailadresse], #Result.itemType, [Lernprogramm oder Test]

        drop table #Result</command>
    </Report>
  </Reports>
</ReportsExport>