﻿<?xml version="1.0" encoding="utf-8"?>
<ReportsExport>
  <Reports>
    <Report id="5f2a8406-b269-43eb-a870-812bc19d67d5" codekey="Mecklenburgische_Basisreport_Weiterbildung" categoryCodekey="CreditPoints" name="Basisreport Weiterbildung" description="">
      <MetaData created="2015-08-24T16:41:38" createdBy="Administrator TestAllFeatures (Administrator)" createdBy_user_id="3" modified="2018-09-27T13:03:54" modifiedBy="Rolle Administrator" />
      <ExecutionDetails format="TableResult" commandType="SqlCommandOrQuery" exportHandler="" adminControl="" exportMultipleTablesToSheets="False" datesWithTime="False" extraParams="" />
      <Mandators mandatorMode="OnlyOwner" mandator_id="96ba2868-8baf-4e69-b1fb-d2cc6d6832e8" mandatorName="Mecklenburgische" isStandard="False" isUsedByMenu="False">
        <Mandator mandatorName="Mecklenburgische" />
      </Mandators>
      <Parameters>
        <Parameter id="7958af06-3e85-4dbe-b2ea-114699e66aa5" isRequired="True" allowMultiSelect="False" name="Qualification" contextName="Qualifizierung" defaultValue="" renderHint="DropDownList" disableParameter="DontDisable" />
        <Parameter id="a5485539-67e0-4912-8255-8081a54efb3c" isRequired="False" allowMultiSelect="False" name="Zielgruppe" contextName="Zielgruppe" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="c81f0416-97a6-44e7-895e-9cfe8d3bfc41" isRequired="False" allowMultiSelect="False" name="GutBeraten jahr" contextName="Kalenderjahr" defaultValue="" renderHint="DropDownList" disableParameter="DontDisable" />
      </Parameters>
      <Roles>
        <Role id="90" />
      </Roles>
      <command>
        --declare @current_mandator_id UNIQUEIDENTIFIER = '35F3DB1C-1B05-4A11-883A-2E57DAFB9A9A'; -- Mecklenburgische
        --declare @targetGroup_id INT = 189; -- Weiterbildungspunkte
        --declare @gutBeratenYear INT = 2018; -- Kalenderjahr
        --drop table #tmpRoles;

        create table #tmpRoles
        (
        role_id int, title nvarchar(256)
        )

        insert #tmpRoles (role_id, title) values (10, 'Lerner')
        insert #tmpRoles (role_id, title) values (20, 'Lerner Assistent')
        insert #tmpRoles (role_id, title) values (30, 'BD-Mitarbeiter')
        insert #tmpRoles (role_id, title) values (40, 'Assistent')
        insert #tmpRoles (role_id, title) values (50, 'Leiter')
        insert #tmpRoles (role_id, title) values (55, 'Führungskraft')
        insert #tmpRoles (role_id, title) values (60, 'FD/BD')
        insert #tmpRoles (role_id, title) values (70, 'DA12 Mitarbeiter')
        insert #tmpRoles (role_id, title) values (80, 'Trainer')
        insert #tmpRoles (role_id, title) values (90, 'Admin')
        insert #tmpRoles (role_id, title) values (100, 'Plattform-Verwaltung')

        select distinct nachname as 'Name',
        vorname as 'Vorname',
        division as 'Abteilung',
        accountExpiresOn as 'gültig bis',
        #tmpRoles.title as 'Rolle',

        case when @targetGroup_id is null then
        substring(
        (
        Select ','+title AS [text()]
        From tblUsersTargetGroups INNER JOIN tblTargetGroups ON tblUsersTargetGroups.targetGroup_id = tblTargetGroups.id
        Where UserCn = intUserCn AND defaultforlibrary = 0
        Order by title
        For XML PATH ('')
        ), 2, 1000)
        else (select title from tblTargetGroups where id=@targetGroup_id)
        end
        as 'Zielgruppe',

        tblItemVersions.title as 'Bezeichnung Weiterbildung',

        case when tblItemVersions.itemType_id=2 then 'Seminar'
        when tblItemVersions.isExternal=1 then 'externer Baustein'
        else 'e-Learning'
        end as 'Objekttyp',

        case when tblItemVersions.learningType_id = 1 then 'Präsenzveranstaltung'
        when tblItemVersions.learningType_id = 2 then 'Einzeltraining'
        when tblItemVersions.learningType_id = 3 then 'Blended Learning'
        when tblItemVersions.learningType_id = 4 then 'gesteuertes E-Learning'
        when tblItemVersions.learningType_id = 5 then 'selbstgesteuertes E-Learning'
        when tblItemVersions.learningType_id = 6 then 'Lernen am Arbeitsplatz'
        else '-'
        end as 'Lernart',

        case when tblItemVersions.itemType_id=2 then tbltrainingVersions.startDate
        when tblItemVersions.isExternal=1 then tblExternalItemVersions.startDate
        else tmpHistory.startDate end
        as 'Datum der Weiterbildung',

        (SELECT SUM(t1.educationTime) FROM tblUserItemScores t1
        WHERE tblUserItemScores.id = t1.id OR t1.referenceScore_id = tblUserItemScores.id) AS 'Weiterbildungszeit',

        CASE WHEN tblUserQualificationPeriods.startDate IS NULL THEN NULL
        WHEN @gutBeratenYear IS NOT NULL THEN (SELECT SUM(educationTime)
        FROM dbo.fn_GetUserItemScores(intUserCn,
        DATEFROMPARTS(@gutBeratenYear, 1, 1),
        DATEADD(yy, 1, DATEFROMPARTS(@gutBeratenYear, 1, 1)),
        @qualification_id))
        ELSE (SELECT SUM(educationTime)
        FROM dbo.fn_GetUserItemScores(intUserCn, null, null, @qualification_id))
        END AS 'Weiterbildungszeit gesamt Ist'

        from v_Users
        left join #tmpRoles on #tmpRoles.role_id=v_Users.SecurityID
        inner join tblStatusUserItemHistory tmpHistory on tmpHistory.user_id = intUserCn and status = 2
        inner join tblUserItemScores on tblUserItemScores.status_id = tmpHistory.id
        inner join tblUserItemScoresQualifications on tblUserItemScores.id = tblUserItemScoresQualifications.userItemScore_id
        left join tblUserQualificationPeriods on tblUserItemScoresQualifications.qualification_id = tblUserQualificationPeriods.qualification_id AND tblUserQualificationPeriods.user_id = intUserCn
        left join tblVersionMap itemVersionMap on itemVersionMap.id = tblUserItemScores.item_id
        and itemVersionMap.tableName = 'tblItemVersions' and itemVersionMap.mainVersion = tblUserItemScores.item_version
        left join tblVersionMap trainingVersionMap on trainingVersionMap.id = tblUserItemScores.item_id
        and trainingVersionMap.tableName = 'tblTrainingVersions' and trainingVersionMap.mainVersion = tblUserItemScores.item_version
        left join tblVersionMap externalItemVersionMap on externalItemVersionMap.id = tblUserItemScores.item_id
        and externalItemVersionMap.tableName = 'tblExternalItemVersions' and externalItemVersionMap.mainVersion = tblUserItemScores.item_version
        left join tblItemVersions on itemVersionMap.id = tblItemVersions.id and itemVersionMap.subVersion = tblItemVersions.version
        left join tblTrainingVersions on trainingVersionMap.id = tblTrainingVersions.id and trainingVersionMap.subVersion = tblTrainingVersions.version
        left join tblExternalItemVersions on externalItemVersionMap.id = tblExternalItemVersions.id and externalItemVersionMap.subVersion = tblExternalItemVersions.version

        where tblUserItemScoresQualifications.qualification_id = @qualification_id
        and (@targetGroup_id is null or exists(select * from tblUsersTargetGroups where UserCn = intUserCn and targetGroup_id=@targetGroup_id))
        and (@gutBeratenYear is null or @gutBeratenYear = YEAR(
        case when tblItemVersions.itemType_id=2
        then tblTrainingVersions.startDate
        when tblItemVersions.isExternal=1
        then tblExternalItemVersions.startDate
        else tmpHistory.startDate end))
        and (v_Users.mandator_id = @current_mandator_id)
        and (tblItemVersions.id  is null or tblItemVersions.itemType_id &lt;&gt; 1001)

        -- also display users who satisfy filters, but have no scores atm
        UNION ALL
        SELECT
        nachname as 'Name',
        vorname as 'Vorname',
        division as 'Abteilung',
        accountExpiresOn as 'gültig bis',
        #tmpRoles.title as 'Rolle',
        case when @targetGroup_id is null then
        substring(
        (
        Select ','+title AS [text()]
        From tblUsersTargetGroups INNER JOIN tblTargetGroups ON tblUsersTargetGroups.targetGroup_id = tblTargetGroups.id
        Where UserCn = intUserCn AND defaultforlibrary = 0
        Order by title
        For XML PATH ('')
        ), 2, 1000)
        else (select title from tblTargetGroups where id=@targetGroup_id)
        end
        as 'Zielgruppe',
        NULL AS 'Bezeichnung Weiterbildung',
        NULL AS 'Objekttyp',
        NULL AS 'Lernart',
        NULL AS 'Datum der Weiterbildung',
        NULL AS 'Weiterbildungszeit',
        NULL AS 'Weiterbildungszeit gesamt Ist'
        from v_Users
        left join #tmpRoles on #tmpRoles.role_id=v_Users.SecurityID
        inner join tblUsersTargetGroups ON tblUsersTargetGroups.UserCn = v_Users.intUserCn
        inner join tblQualificationsTargetGroups ON tblQualificationsTargetGroups.targetGroup_id = tblUsersTargetGroups.targetGroup_id
        inner join tblQualifications ON tblQualificationsTargetGroups.qualification_id = tblQualifications.id
        where tblQualifications.id = @qualification_id
        and ISNULL(@targetGroup_id, tblUsersTargetGroups.targetGroup_id) = tblUsersTargetGroups.targetGroup_id
        and not exists (select * from tblStatusUserItemHistory tmpHistory
        inner join tblUserItemScores on tblUserItemScores.status_id = tmpHistory.id
        inner join tblUserItemScoresQualifications on tblUserItemScores.id = tblUserItemScoresQualifications.userItemScore_id
        left join tblVersionMap itemVersionMap on itemVersionMap.id = tblUserItemScores.item_id
        and itemVersionMap.tableName = 'tblItemVersions' and itemVersionMap.mainVersion = tblUserItemScores.item_version
        left join tblVersionMap trainingVersionMap on trainingVersionMap.id = tblUserItemScores.item_id
        and trainingVersionMap.tableName = 'tblTrainingVersions' and trainingVersionMap.mainVersion = tblUserItemScores.item_version
        left join tblVersionMap externalItemVersionMap on externalItemVersionMap.id = tblUserItemScores.item_id
        and externalItemVersionMap.tableName = 'tblExternalItemVersions' and externalItemVersionMap.mainVersion = tblUserItemScores.item_version
        left join tblItemVersions on itemVersionMap.id = tblItemVersions.id and itemVersionMap.subVersion = tblItemVersions.version
        left join tblTrainingVersions on trainingVersionMap.id = tblTrainingVersions.id and trainingVersionMap.subVersion = tblTrainingVersions.version
        left join tblExternalItemVersions on externalItemVersionMap.id = tblExternalItemVersions.id and externalItemVersionMap.subVersion = tblExternalItemVersions.version
        where tblUserItemScoresQualifications.qualification_id = tblQualifications.id
        and tmpHistory.user_id = intUserCn and status = 2
        and (@gutBeratenYear is null or @gutBeratenYear = YEAR(
        case when tblItemVersions.itemType_id=2
        then tblTrainingVersions.startDate
        when tblItemVersions.isExternal=1
        then tblExternalItemVersions.startDate
        else tmpHistory.startDate end)))
        order by 'Name', 'Vorname', 'Datum der Weiterbildung' desc
        drop table #tmpRoles
      </command>
    </Report>
  </Reports>
  <Parameters>
    <Parameter id="7958af06-3e85-4dbe-b2ea-114699e66aa5" isSystem="True" name="Qualification" reportParameterType_id="e629bb84-0db6-4d66-8009-b781cea6a13e" queryParameterName="@qualification_id" />
    <Parameter id="a5485539-67e0-4912-8255-8081a54efb3c" isSystem="True" name="Zielgruppe" reportParameterType_id="9ae252e1-18b4-4b71-982c-e27b2d3a5287" queryParameterName="@targetGroup_id" />
    <Parameter id="c81f0416-97a6-44e7-895e-9cfe8d3bfc41" isSystem="False" name="GutBeraten jahr" reportParameterType_id="c5dfff35-86f0-4d3c-925b-e0cf8389c360" queryParameterName="@gutBeratenYear" />
  </Parameters>
  <ParameterTypes>
    <ParameterType id="e629bb84-0db6-4d66-8009-b781cea6a13e" isSystem="True" name="Qualifications" datatype="StringDDL" dataValueField="id" dataTextField="title">
      <query>
        SELECT tblObjects.id, tblObjects.title
        FROM tblQualifications
        INNER JOIN tblObjects ON tblObjects.id = tblQualifications.id
        WHERE tblObjects.mandator_id = @current_mandator_id AND tblObjects.deleted IS NULL
        ORDER BY tblObjects.title
      </query>
    </ParameterType>
    <ParameterType id="9ae252e1-18b4-4b71-982c-e27b2d3a5287" isSystem="True" name="TargetGroup" datatype="TargetGroup" dataValueField="" dataTextField="" />
    <ParameterType id="c5dfff35-86f0-4d3c-925b-e0cf8389c360" isSystem="False" name="GutBeraten jahr" datatype="IntegerDDL" dataValueField="year_id" dataTextField="year_name">
      <query>
        ; WITH yearsCTE AS
        (
        SELECT 2014 as year
        UNION ALL
        SELECT year + 1
        FROM yearsCTE
        WHERE year &lt; YEAR(GETDATE())
        )
        SELECT year AS year_id, cast(year as nvarchar(64)) AS year_name
        FROM yearsCTE
      </query>
    </ParameterType>
  </ParameterTypes>
</ReportsExport>