﻿<?xml version="1.0" encoding="utf-8"?>
<ReportsExport>
  <Reports>
    <Report id="5409e73f-da4b-4fe0-8834-39dc176a89b6" codekey="export_perbit_daterange" categoryCodekey="SpecialPurpose" name="Exporte für Perbit (Zeitraum von... bis...)" description="">
      <MetaData created="2020-02-11T10:37:30" createdBy="Debeka, Administrator (Administrator)" createdBy_user_id="760" modified="2020-09-04T13:22:15" modifiedBy="Debeka, Administrator (Administrator)" modifiedBy_user_id="760" />
      <ExecutionDetails format="TableResult" commandType="SqlCommandOrQuery" exportHandler="" adminControl="" exportMultipleTablesToSheets="False" datesWithTime="False" extraParams="" />
      <Mandators mandatorMode="OnlyOwner" mandator_id="6279bb24-f218-49e0-9155-7c1cb1dba226" mandatorName="Debeka" isStandard="False" isUsedByMenu="False" />
      <Parameters>
        <Parameter id="d8e4595f-8bdd-4a54-9298-8f378450e6a0" isRequired="True" allowMultiSelect="False" name="Zeitraum" contextName="Zeitraum" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="7a721121-2d2e-4a3d-b9e6-c6aa238e7ab2" isRequired="False" allowMultiSelect="False" name="Auswahl (ja/nein)" contextName="Auswahl (ja/nein)" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="aee9472c-2d96-4e67-b86b-e4c2cbff8ca3" isRequired="True" allowMultiSelect="False" name="BausteinTyp (nur mit Status, schöne Namen)" contextName="BausteinTyp (nur mit Status, schöne Namen)" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
      </Parameters>
      <Roles />
      <command>
        /*
        DECLARE @current_mandator_id UNIQUEIDENTIFIER = '6279BB24-F218-49E0-9155-7C1CB1DBA226'
        DECLARE @itemType NVARCHAR(200) = 'TrainingTitle'
        DECLARE @selection_yesno BIT = 1
        DECLARE @dateRange_start datetime = '2018-01-01 00:00:00.000'
        DECLARE @dateRange_end datetime ='2018-12-31 00:00:00.000'
        */

        DECLARE @itemType_helper_id INT
        DECLARE @compareDate DATETIME
        DECLARE @existingItemTypes TABLE
        (itemType_id INT, title NVARCHAR(200), codeKey_mapping NVARCHAR(200))
        INSERT INTO @existingItemTypes VALUES (0, 'Test', 'Test')
        INSERT INTO @existingItemTypes VALUES (1, 'WBT', 'Theme')
        INSERT INTO @existingItemTypes VALUES (2, 'Seminar','TrainingTitle')
        INSERT INTO @existingItemTypes VALUES (6, 'Dokument','Document')

        DECLARE @learningTypes TABLE
        (learningType_id INT, title NVARCHAR(200))
        INSERT INTO @learningTypes VALUES(1,'Präsenzveranstaltung')
        INSERT INTO @learningTypes VALUES(2,'Einzeltraining')
        INSERT INTO @learningTypes VALUES(3,'Blended Learning')
        INSERT INTO @learningTypes VALUES(4,'gesteuertes E-Learning')
        INSERT INTO @learningTypes VALUES(5,'selbstgesteuertes E-Learning')
        INSERT INTO @learningTypes VALUES(6,'Lernen am Arbeitsplatz')

        SELECT @itemType_helper_id = itemtype_id FROM @existingItemTypes WHERE codeKey_mapping = @itemType

        -- if we are using test only exams of the type
        -- we only export green status information
        CREATE TABLE #hlp
        (
        Name nvarchar(255),
        Vorname nvarchar(255),
        descUserCn nvarchar(255),
        item_id UNIQUEIDENTIFIER,
        itemType_id INT,
        title NVARCHAR(200),
        session_id UNIQUEIDENTIFIER NULL,
        startDate DATETIME NULL,
        endDate DATETIME,
        learningType_id INT,
        learningContent NVARCHAR(200),
        educationTime INT
        )
        ;WITH HLP AS
        (
        SELECT
        Nachname AS Name,
        Vorname AS Vorname,
        descUserCn,
        i.id AS item_id, -- GUID of item
        ISNULL(NULLIF(LTRIM(RTRIM(i.titleForLearners)),''), i.title) AS title,
        suih.startDate AS endDate,
        suih.session_id AS session_id,
        i.learningType_id AS learningType_id,
        i.itemType_id,
        i.learningContent,
        CASE WHEN uis.id IS NOT NULL THEN uis.educationTime ELSE NULL END AS educationTime
        FROM v_Users
        INNER JOIN tblStatusUserItemHistory AS suih
        ON suih.user_id = v_Users.intUserCn
        AND suih.status = 2
        AND suih.startDate &gt;= @dateRange_start
        AND ISNULL(suih.endDate,suih.startDate) &lt;= @dateRange_end
        INNER JOIN tblItems AS i ON i.id = suih.item_id AND i.itemType_id = @itemType_helper_id
        INNER JOIN @existingItemTypes AS eit ON eit.itemType_id = i.itemType_id
        LEFT JOIN tblTasTests AS t ON t.id = i.id AND t.mode = 1 -- only exams if it's a test item
        LEFT JOIN tblUserItemScores AS uis ON uis.status_id = suih.id
        WHERE v_Users.mandator_id = @current_mandator_id
        AND (@selection_yesno IS NULL OR @selection_yesno = CASE WHEN uis.status_id IS NULL THEN 0 ELSE 1 END) -- only education time if available
        )
        INSERT INTO #hlp
        SELECT
        Name ,
        Vorname,
        descUserCn,
        item_id ,
        itemType_id,
        title,
        session_id,
        NULL,
        endDate,
        learningType_id,
        learningContent,
        educationTime
        FROM HLP


        -- we have to retrieve the startDate information of the elements
        -- for trainings we get the start date of the item
        -- for other items we have to retieve them from the corresponding session
        IF @itemType_helper_id = 2
        BEGIN
        UPDATE #hlp
        SET startDate = t.startDate
        FROM #hlp INNER JOIN tblTrainings AS t on t.id = #hlp.item_id
        END
        IF @itemType_helper_id = 0
        BEGIN
        UPDATE #hlp
        SET startDate = t.startDate
        FROM #hlp INNER JOIN tblTasTestSessions AS t ON t.id = #hlp.session_id
        END
        IF @itemType_helper_id = 1
        BEGIN
        UPDATE #hlp
        SET startDate = t.startDate
        FROM #hlp INNER JOIN tblThemeSessions AS t ON t.id = #hlp.session_id
        END

        SELECT
        Name,
        Vorname,
        descUserCn AS 'p0-Kennung',
        item_id AS 'Objekt-ID',
        HLP.title AS 'Titel für Lerner',
        eit.title AS 'Objekttyp',
        FORMAT(startDate,'dd.MM.yyyy') 'Startdatum', -- start of the item AS DD:MM:YYYY
        FORMAT(startDate,'HH:mm') AS 'Startzeit', -- start of the item as HH:MM
        FORMAT(endDate,'dd.MM.yyyy') AS 'Endedatum', -- end of the item AS DD:MM:YYYY
        FORMAT(endDate,'HH:mm') AS 'Endezeit', -- end of the item AS HH:MM
        l.title AS 'Lernform',
        CASE
        WHEN learningContent = 'Insurance1' THEN 'Privat-Vorsorge-Lebens-/Rentenversicherung'
        WHEN learningContent = 'Insurance2' THEN 'Privat-Vorsorge-Kranken-/Pflegeversicherung'
        WHEN learningContent = 'Insurance3' THEN 'Privat-Sach-/Schadenversicherung'
        WHEN learningContent = 'Insurance4' THEN 'Firmenkunden-Sach-/Schadenversicherung'
        WHEN learningContent = 'Insurance5General' THEN 'Spartenübergreifend'
        WHEN learningContent = 'Insurance6Business' THEN 'Firmenkunden-Vorsorge (bAV/Personenversicherung)'
        WHEN learningContent = 'Consulting' THEN 'Beratungskompetenz'
        ELSE LEFT(learningContent, 100)
        END AS Lerninhalt,
        CASE WHEN HLP.itemType_id = 0 THEN
        'Test bestanden'
        ELSE
        CASE WHEN HLP.itemType_id = 1 THEN
        'Lernprogramm vollständig bearbeitet'
        ELSE
        'Teilnahme bestätigt'
        END
        END AS 'Status',
        educationTime AS 'Bildungszeit'
        FROM
        #hlp AS HLP
        INNER JOIN @learningTypes AS l ON l.learningType_id = HLP.learningType_id
        INNER JOIN @existingItemTypes AS eit ON eit.itemType_id = HLP.itemType_id
        ORDER BY HLP.descUserCn

        DROP TABLE #hlp
      </command>
    </Report>
  </Reports>
  <Parameters>
    <Parameter id="d8e4595f-8bdd-4a54-9298-8f378450e6a0" isSystem="True" name="Zeitraum" reportParameterType_id="abff13be-91c3-4ee1-93a3-7292f8e013ba" queryParameterName="@dateRange" />
    <Parameter id="7a721121-2d2e-4a3d-b9e6-c6aa238e7ab2" isSystem="True" name="Auswahl (ja/nein)" reportParameterType_id="411ec93b-ab23-41ba-91d6-e7dc0235b5af" queryParameterName="@selection_yesno" />
    <Parameter id="aee9472c-2d96-4e67-b86b-e4c2cbff8ca3" isSystem="True" name="BausteinTyp (nur mit Status, schöne Namen)" reportParameterType_id="32872b47-da0f-49d9-8853-0aa4760c5a11" queryParameterName="@itemType" />
  </Parameters>
  <ParameterTypes>
    <ParameterType id="abff13be-91c3-4ee1-93a3-7292f8e013ba" isSystem="True" name="DateRange" datatype="DateRange" dataValueField="" dataTextField="" />
    <ParameterType id="411ec93b-ab23-41ba-91d6-e7dc0235b5af" isSystem="True" name="Boolean" datatype="StringDDL" dataValueField="Value" dataTextField="Text">
      <query>
        SELECT 0 AS Value, 'Nein' AS Text
        UNION
        SELECT 1 AS Value, 'Ja' AS Text
      </query>
    </ParameterType>
    <ParameterType id="32872b47-da0f-49d9-8853-0aa4760c5a11" isSystem="True" name="ItemType (with Status, nice names)" datatype="StringDDL" dataValueField="codekey" dataTextField="text">
      <query>
        DECLARE @itemTypes AS TABLE (
        codekey NVARCHAR(50),
        text NVARCHAR(50)
        )

        INSERT INTO @itemTypes VALUES ('Theme', 'Lernprogramm')
        INSERT INTO @itemTypes VALUES ('Test', 'Test')
        INSERT INTO @itemTypes VALUES ('TrainingTitle', 'Seminartitel')
        INSERT INTO @itemTypes VALUES ('Document', 'Dokument')

        SELECT codekey, text FROM @itemTypes ORDER BY text
      </query>
    </ParameterType>
  </ParameterTypes>
</ReportsExport>