﻿<?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="Gibt die Grünstatus für Bausteine mit ED-SAP Merkmal  für alle User des ERGODirekt Imports aus">
      <MetaData created="2011-11-02T11:24:30" createdBy="Administrator, Albrecht (Administrator)" createdBy_user_id="1" modified="2024-12-19T13:26:31" modifiedBy="Kaiser, Jan-Patrick (0245527)" modifiedBy_user_id="101393" />
      <ExecutionDetails format="TableResult" commandType="SqlCommandOrQuery" exportHandler="" adminControl="" exportMultipleTablesToSheets="False" datesWithTime="False" extraParams="" />
      <Mandators mandatorMode="OnlyOwner" mandator_id="bfa70f6b-484c-49da-9ad7-1ccd8f0d684c" mandatorName="ERGO_e-Campus" 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='ERGO_e-Campus')
*/

SELECT  * 
INTO    #EDUsers
FROM    v_Users 
WHERE   importer_id = 2


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 DISTINCT tblStatusUserCourse.userId as [user_id], tblItems.Title as [title], tblItems.tag2 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 #EDUsers on #EDUsers.intUserCn=tblStatusUserCourse.userId
where tblItems.tag2!=''
AND tblItems.deleted IS NULL
and #EDUsers.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 for all trainings
set @cursor=cursor static for
SELECT  distinct suih.user_id,
        ISNULL(i.titleForLearners,i.title),
        i.tag2,
        i.id
FROM    tblStatusUserItemHistory suih
JOIN    tblItems i
        ON i.id = suih.item_id
JOIN    #EDUsers
        ON #EDUsers.intUserCn = suih.user_id
WHERE   i.tag2 != ''
AND     i.deleted is null
AND     i.itemType_id = 2

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.tag2 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
inner join #EDUsers ON tblTasStatsTestSessions.user_id = #EDUsers.intUserCn and #EDUsers.importer_id = 2 AND #EDUsers.employeeId LIKE 'ED%'
where
tblItems.tag2 != ''
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.tag2,
tblStatusUserItemHistory.startDate,
tblItems.itemType_id
from tblStatusUserItemHistory
join #EDUsers ON #EDUsers.intUserCn = tblStatusUserItemHistory.user_id AND #EDUsers.importer_id = 2 AND #EDUsers.employeeId LIKE 'ED%'
join tblItems on tblitems.id = tblStatusUserItemHistory.item_id
where tblitems.itemType_id = 1001
and tblItems.mandator_id = @current_mandator_id
and ISNULL(tblItems.tag2,'') != ''
and tblItems.deleted IS NULL
and tblStatusUserItemHistory.status = 2

-- add external items now
-- Insert External Items now
INSERT INTO #Result
SELECT user_id,
tblItems.title,
tag2,
tblExternalItems.enddate,
itemType_id
FROM tblStatusUserItemHistory
join #EDUsers ON #EDUsers.intUserCn = tblStatusUserItemHistory.user_id AND #EDUsers.importer_id = 2 AND #EDUsers.employeeId LIKE 'ED%'
JOIN tblExternalItems ON tblExternalItems.id = tblStatusUserItemHistory.item_id
JOIN tblItems ON tblItems.id = tblExternalItems.id AND tblItems.deleted IS NULL
WHERE tblItems.mandator_id = @current_mandator_id AND itemType_id IN(0, 1) AND tag2 IS NOT NULL AND LEN(tag2) &gt; 0


-- Build result

select
SUBSTRING(#EDUsers.employeeId,3,LEN(#EDUsers.employeeId)) as 'PNR',
#Result.title as 'Bausteinname',
#Result.internal_name as 'ED-SAP Schnittstelle',
convert(nvarchar(32), #Result.certificateReceived, 104) as 'Datum_Zielerreichung'
from
#Result
inner join #EDUsers on #EDUsers.intUserCn=#Result.user_id
where not  #Result.certificateReceived is null
AND YEAR(#Result.certificateReceived) >= '2025'

order by
[descUserCn], #Result.itemType, [Bausteinname]

drop table #Result
drop table #EDUsers

</command>
    </Report>
  </Reports>
</ReportsExport>