﻿<?xml version="1.0" encoding="utf-8"?>
<ReportsExport>
  <Reports>
    <Report id="6143ea40-c7e2-4afd-9cd2-4a1ca22fba49" codekey="LernzeitLocation_CUM" categoryCodekey="UserStats" name="Lernzeit Location CUM" description="Der Report ermittelt für einen gewählten Zeitraum und eine gewählte Zielgruppe für alle Lernprogramme, die der Zielgruppe zugeordnet sind die Anzahlen der erfolgreichen Bearbeitungen des jeweiligen Lernpgrogramms durch alle Lerner der Zielgruppe aggrigiert auf CUM">
      <MetaData created="2020-06-10T20:12:01" createdBy="Administrator, Albrecht (Administrator)" createdBy_user_id="1" modified="2020-07-30T11:54:18" modifiedBy="ELECT, Administrator (Administrator)" modifiedBy_user_id="2" />
      <ExecutionDetails format="TableResult" commandType="SqlCommandOrQuery" exportHandler="" adminControl="" exportMultipleTablesToSheets="False" datesWithTime="False" extraParams="" />
      <Mandators mandatorMode="OnlyOwner" mandator_id="d5a90a49-e697-4aeb-8a5b-298782815490" mandatorName="ELECT" 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="3c128e78-ecb2-4256-accb-07ced081bc03" isRequired="True" allowMultiSelect="True" name="AllSelectedTargetGroups" contextName="AllSelectedTargetGroups" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="7a721121-2d2e-4a3d-b9e6-c6aa238e7ab2" isRequired="False" allowMultiSelect="False" name="Auswahl (ja/nein)" contextName="Nur Führungskräfte auswerten" defaultValue="0" renderHint="Undefined" disableParameter="DontDisable" />
      </Parameters>
      <Roles />
      <command>
        -- Global supplied variables
        --DECLARE @current_mandator_id UNIQUEIDENTIFIER;
        --SELECT @current_mandator_id = id FROM tblMandators WHERE name='ELECT';

        -- -----------------------------------------------------------------------------
        -- Parameters
        --DECLARE @AllSelectedTargetGroups  [dbo].[typeIntList];
        --INSERT INTO @AllSelectedTargetGroups ([value]) VALUES (493); -- TargetGroupAllContent, TargetGroupAllUsers
        --DECLARE @dateRange_start DATETIME = '2019-01-01';
        --DECLARE @dateRange_end DATETIME = '2019-12-31';
        -- End parameters
        -- ------------------------------------------------------------------------------

        DECLARE @results TABLE
        (
        cum_title NVARCHAR(255),
        theme_title NVARCHAR(255),
        totalSuccess BIGINT,
        duration DECIMAL(10,2),
        totalDuration DECIMAL(10,2)
        )

        -- CR: 28751!
        -- we have a given set of targetgroups
        -- for those we have to detect the users assigned to
        -- as for those assigned users we need all user assigned targetgroups for detecting the themes
        DECLARE @hlpTargetGroups dbo.[typeIntList]
        ;WITH hlpTargetGroups(targetGroup_id)
        AS
        (
        SELECT DISTINCT mergedTargetGroups.targetGroup_id
        FROM tblUsersTargetGroups AS originalUsers
        INNER JOIN @AllSelectedTargetGroups AS requestedTargetGroups ON requestedTargetGroups.value = originalUsers.targetGroup_id
        INNER JOIN tblUsersTargetGroups AS mergedTargetGroups ON mergedTargetGroups.UserCn = originalUsers.UserCn
        )
        INSERT INTO @hlpTargetGroups
        SELECT targetGroup_id FROM hlpTargetGroups


        -- Themes for target group
        ;WITH ThemesForTargetGroupBase(cum_title, theme_title, theme_id, duration, cum_theme_index)
        AS
        (
        SELECT DISTINCT
        cum.title as cum_title, tblItems.title as theme_title, tblItems.id as theme_id, tblThemes.learningDuration as duration,
        ROW_NUMBER() OVER (PARTITION BY tblItems.id ORDER BY cum.title)
        FROM
        tblThemes
        JOIN tblItems
        ON	tblItems.id = tblThemes.id
        JOIN tblModuleItems
        ON	tblItems.id = tblModuleItems.item_id
        JOIN tblEduOffersModules
        ON	tblModuleItems.module_id = tblEduOffersModules.module_id
        JOIN tblEduOffersTargetGroups
        ON  tblEduOffersModules.eduOffer_id = tblEduOffersTargetGroups.eduOffer_id
        JOIN @hlpTargetGroups stg
        ON	stg.value = tblEduOffersTargetGroups.targetGroup_id
        JOIN tblItems cum
        ON	cum.id = tblEduOffersModules.eduOffer_id
        WHERE
        tblItems.mandator_id = @current_mandator_id
        AND tblThemes.learningDuration IS NOT NULL
        AND (cum.title NOT LIKE '%test%' OR cum.titleForLearners NOT LIKE '%test%')
        ),
        ThemesForTargetGroup(cum_title, theme_title, theme_id, duration, cum_theme_index)
        AS
        (
        SELECT * FROM ThemesForTargetGroupBase WHERE cum_theme_index = 1
        ),
        CompletedResults(theme_id, cum_title, successCount)
        AS
        (
        SELECT
        tftg.theme_id,
        tftg.cum_title,
        COUNT(DISTINCT suc.UserId)
        FROM
        (
        -- Avoid counting themes multiple times when they are available via multiple modules / edu offers
        SELECT DISTINCT
        theme_id,
        cum_title
        FROM
        ThemesForTargetGroup
        ) AS tftg
        JOIN [dbo].[tblStatusUserCourse] suc
        ON suc.theme_id = tftg.theme_id
        INNER JOIN tblUsersTargetGroups ON tblUsersTargetGroups.UserCn = suc.UserId
        INNER JOIN @AllSelectedTargetGroups AS requestedTargetGroups ON requestedTargetGroups.value = tblUsersTargetGroups.targetGroup_id
        WHERE
        suc.BestCompleteDate &gt; @dateRange_start
        AND suc.BestCompleteDate &lt; @dateRange_end
        AND suc.BestComplete &gt; 80
		AND (
			ISNULL(@selection_yesno,0) = 0
			OR
			-- only managers if parameter provided
			EXISTS(
				SELECT TOP 1 * FROM
				tblOrganisationRolesOfUsers AS orou
				INNER JOIN tblOrganisationRoles AS oro ON oro.id = orou.organisationRole_id
				WHERE codekey = 'Manager'
				AND mandator_id = @current_mandator_id
				AND orou.user_id = suc.UserId	
			)
		)
        GROUP BY
        tftg.theme_id,
		tftg.cum_title
        )
        INSERT INTO @results
        SELECT
        (
        CASE WHEN GROUPING(tftg.theme_title) = 1
        THEN 'Summe : ' + tftg.cum_title
        ELSE tftg.cum_title
        END
        ) as module_title,
        tftg.theme_title,
        SUM(cr.successCount) as totalSuccess,
        duration as duration,
        SUM(duration * cr.successCount) as totalDuration
        FROM
        ThemesForTargetGroup tftg
        JOIN CompletedResults cr
        ON	cr.theme_id = tftg.theme_id
        GROUP BY
        GROUPING SETS((tftg.cum_title, tftg.theme_title, duration), (tftg.cum_title))
        ORDER BY
        -- Order by module_title, theme_title then total row
        CASE
        WHEN GROUPING(tftg.theme_title) = 1
        -- Just concatenate '1' so it will be sorted after the entries for this module
        THEN tftg.cum_title + '1'
        ELSE tftg.cum_title
        END,
        tftg.theme_title

        -- Add the "grand total" row as separate insert and not a union so we are guaranteed it goes at the end
		DECLARE @minutes DECIMAL(18,2)
		DECLARE @hours DECIMAL(18,2)
		DECLARE @counter BIGINT
		DECLARE @days DECIMAL(18,2)
		SELECT @counter = SUM(totalSuccess),
        @minutes = SUM(totalDuration)
        FROM
        @results
		WHERE theme_title IS NULL -- only for the SUM groups

		SET @hours = @minutes / 60
		SET @days = @hours / 8

        -- Select our results with aliases, the rows should already be in the correct order
		SELECT 
			REPLACE([Bildungsangebot/CUM],'Summe : ','') AS 'Bildungsangebot/CUM',
			[Anzahl erfolgr. Bearbeitung],
			[Gesamte Lerndauer (min)]
		FROM
		(
        SELECT
        cum_title as 'Bildungsangebot/CUM',
        totalSuccess as 'Anzahl erfolgr. Bearbeitung',
        totalDuration  as 'Gesamte Lerndauer (min)',
		0 AS sortHelper
        FROM
        @results
		WHERE theme_title IS NULL
		UNION ALL
		-- we are providing the SUM row at the end of the result for the Minutes
        SELECT
        'Min (# / min)',
        @counter,
        @minutes ,
		1
		UNION ALL
		-- we are providing the SUM row at the end of the result for the Minutes
        SELECT
        'Stunden',
        NULL,
        @hours ,
		2
		UNION ALL
		-- we are providing the SUM row at the end of the result for the Minutes
        SELECT
        'Arbeitstage',
        NULL,
        @days ,
		3
		) AS A
		ORDER BY A.sortHelper</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="3c128e78-ecb2-4256-accb-07ced081bc03" isSystem="False" name="AllSelectedTargetGroups" reportParameterType_id="04238627-5da0-47cd-bbfa-db313a02b5b2" queryParameterName="@AllSelectedTargetGroups" />
    <Parameter id="7a721121-2d2e-4a3d-b9e6-c6aa238e7ab2" isSystem="True" name="Auswahl (ja/nein)" reportParameterType_id="411ec93b-ab23-41ba-91d6-e7dc0235b5af" queryParameterName="@selection_yesno" />
  </Parameters>
  <ParameterTypes>
    <ParameterType id="abff13be-91c3-4ee1-93a3-7292f8e013ba" isSystem="True" name="DateRange" datatype="DateRange" dataValueField="" dataTextField="" />
    <ParameterType id="04238627-5da0-47cd-bbfa-db313a02b5b2" isSystem="False" name="AllTargetGroupsForCurrentMandator" datatype="IntegerDDL" dataValueField="id" dataTextField="title">
      <query>
        select
        id, title
        from
        tblTargetGroups
        where
        mandator_id = @current_mandator_id
        order by
        title
      </query>
    </ParameterType>
    <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>
  </ParameterTypes>
</ReportsExport>