﻿<?xml version="1.0" encoding="utf-8"?>
<ReportsExport>
  <Reports>
    <Report id="bb8a4469-6f18-441b-a842-b33c1e0ae105" codekey="Kosmos_AuswertungWBT_anonymisiert" categoryCodekey="LearningPrograms" name="Auswertung WBT anonymisiert" description="">
      <MetaData created="2017-08-25T13:09:25" createdBy="VIWIS, Administrator (Administrator)" createdBy_user_id="3" modified="2020-06-23T15:29:22" modifiedBy="Administrator, Rolle (Administrator)" modifiedBy_user_id="12" />
      <ExecutionDetails format="TableResult" commandType="SqlCommandOrQuery" exportHandler="" adminControl="" exportMultipleTablesToSheets="False" datesWithTime="False" extraParams="" />
			<Mandators mandatorMode="OnlyOwner" mandator_id="0ab3e804-57cb-42a7-82a3-a6fd412d317d" mandatorName="Kosmos" isStandard="False" isUsedByMenu="False" />
      <Parameters>
		<Parameter id="f5d69246-37d4-4c96-a9dc-8cdcbeee1705" isRequired="True" allowMultiSelect="False" name="Ignore OU" contextName="Organisationseinheiten ignorieren" defaultValue="0" renderHint="Undefined" disableParameter="DontDisable" />
		<Parameter id="3ffbb2af-4676-4bf7-9f52-8b5c2b672971" isRequired="False" allowMultiSelect="True" name="OrgUnits" contextName="Organisationseinheiten" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="93460158-2520-48a7-ac64-9350fa3d159f" isRequired="False" allowMultiSelect="True" name="Zielgruppen" contextName="Zielgruppen Multiselektion" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="5d075bc9-a866-4eb1-8760-c28bc3913a79" isRequired="True" allowMultiSelect="False" name="Lernprogramme" contextName="WBTs for selected Target Groups " defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
      </Parameters>
      <Roles>
        <Role id="19" />
        <Role id="20" />
        <Role id="22" />
        <Role id="24" />
        <Role id="34" />
        <Role id="36" />
        <Role id="37" />
        <Role id="38" />
        <Role id="39" />
        <Role id="40" />
        <Role id="41" />
        <Role id="42" />
        <Role id="43" />
        <Role id="44" />
        <Role id="46" />
        <Role id="47" />
        <Role id="48" />
		<Role id="90" />
      </Roles>
      <command>
		create table #Users (user_id int)

		insert into #Users
		select  distinct uai.user_id
		  from v_UsersAssignedItems uai 
		 where uai.item_id = @wbtsForTargetGroups
			and 
			(
				(@boolean = 0 
					and 
					exists(select 1 from tblUsersOrganisationUnits uou 
								join @orgUnit_id on value = uou.organisationUnit_id
							where uai.user_id = uou.user_id)
					and 
						(not exists(select 1 from @targetGroupId)
						or
							exists(select 1 from tblUsersTargetGroups utg 
									join @targetGroupId tg on utg.targetGroup_id = tg.value
									where utg.UserCn = uai.user_id)
						)
							
				)
					or
				(@boolean = 1 
					and 
					exists(select 1 from tblUsersTargetGroups utg 
							join @targetGroupId tg on utg.targetGroup_id = tg.value
							where utg.UserCn = uai.user_id)
				)
			)
		
		;WITH WbtUserCounts(id, title, userCount, passedCount)
			AS (
				select  i.id,
						i.title,
						(select count(user_id) from #Users) as userCount,
						(select count(user_id) from #Users where dbo.fn_GetRealUserItemStatus(user_id, @wbtsForTargetGroups)=2) as passedCount
				from tblItems i
				where i.id = @wbtsForTargetGroups
				)
		select  --id,
				title as 'Lernprogramm',
				userCount as 'Benutzer',
				passedCount as 'Benutzer Lernprogramm bearbeitet',
				userCount - passedCount as 'Benutzer Lernprogramm nicht bearbeitet'
		from WbtUserCounts

		;WITH OuCounts(id, title, userCount, passedCount)
			AS (
				select  ou.id,
						ou.title,
						(select count(uc.user_id) from tblUsersOrganisationUnits uc join #Users on #Users.user_id = uc.user_id where organisationUnit_id = ou.id) as userCount,
						(select count(uc.user_id) from tblUsersOrganisationUnits uc join #Users on #Users.user_id = uc.user_id where organisationUnit_id = ou.id and dbo.fn_GetRealUserItemStatus(uc.user_id, @wbtsForTargetGroups)=2) as passedCount
				from tblOrganisationUnits ou 
				where exists(select 1 from #Users join tblUsersOrganisationUnits uou on #Users.user_id = uou.user_id where ou.id = uou.organisationUnit_id)
				)
		select  --id,
				title as 'Organisationseinheiten',
				userCount as 'Benutzer OE',
				passedCount as 'Bearbeitet',
				userCount - passedCount as 'nicht bearbeitet'
		from OuCounts
		where userCount >= 5
		drop table #Users		  
      </command>
    </Report>
  </Reports>
  <Parameters>
	<Parameter id="f5d69246-37d4-4c96-a9dc-8cdcbeee1705" isSystem="False" name="Ignore OU" reportParameterType_id="9ec1e04a-1a50-4938-8d12-54041c902407" queryParameterName="@boolean" />
	<Parameter id="3ffbb2af-4676-4bf7-9f52-8b5c2b672971" isSystem="True" name="OrgUnits" reportParameterType_id="8a3022c2-6ef5-4246-b2bf-58fdf7f30afa" queryParameterName="@orgUnit_id" />
    <Parameter id="93460158-2520-48a7-ac64-9350fa3d159f" isSystem="True" name="Zielgruppen Multiselektion" reportParameterType_id="f4c178f7-fa5a-4229-92ea-2839c7c82ea4" queryParameterName="@targetGroupId" />
    <Parameter id="5d075bc9-a866-4eb1-8760-c28bc3913a79" isSystem="False" name="WBTs for selected Target Groups" reportParameterType_id="cf245854-b392-4105-b1e3-1ee224d3d86a" queryParameterName="@wbtsForTargetGroups" />
  </Parameters>
  <ParameterTypes>
    <ParameterType id="9ec1e04a-1a50-4938-8d12-54041c902407" isSystem="True" name="Boolean" datatype="StringDDL" dataValueField="Value" dataTextField="Text">
	    <query>
		    DECLARE	@userRole_id INT = 10
		    SELECT @userRole_id = SecurityID FROM tableUsers WHERE intUserCn = @current_user_id

		    IF(
				    -- Ignore OU choice is available only for tg manager or for users with function FcnIsAuthorizedForAllTGs
				    ( 
				    dbo.fn_IsTGManager(@current_user_id, NULL) = 1
				    OR
				    @userRole_id = 100 -- PLATMIN
				    OR
				    EXISTS
					    (
						    select 1 
							    from tblRolesFunctions rf
							    join tblFunctions f on f.id = rf.function_id
							    where rf.mandator_id = @current_mandator_id
							    and f.shortcut like 'FcnIsAuthorizedForAllTGs'
							    and rf.role_id = @userRole_id
					    )
				    )
				    AND
				    -- and only when any OU is visible to the user
				    (
				    @userRole_id = 100 -- PLATMIN
				    OR
				    EXISTS
					    (
						    select 1 
							    from tblRolesFunctions rf
							    join tblFunctions f on f.id = rf.function_id
							    where rf.mandator_id = @current_mandator_id
							    and f.shortcut like 'FcnIsAuthorizedForEvalOfOrgUnits'
							    and rf.role_id = @userRole_id
					    )
				    OR
				    EXISTS(
						        SELECT 1
								    FROM tblUsersOrganisationUnits
								    WHERE user_id = @current_user_id
								    AND position IN (1, 2) -- Manager or Delegate 
						    )
				    )
			    )
			    BEGIN
			    SELECT 0 AS Value, 'Nein' AS Text
			    UNION
			    SELECT 1 AS Value, 'Ja' AS Text
			    END	
			    ELSE
			    SELECT 1 AS Value, 'Ja' AS Text
	    </query>
    </ParameterType>
    <ParameterType id="8a3022c2-6ef5-4246-b2bf-58fdf7f30afa" isSystem="True" name="OrgUnit" datatype="OrgUnit" dataValueField="" dataTextField="">
	    <Parameters>
		    <Parameter id="f5d69246-37d4-4c96-a9dc-8cdcbeee1705" isRequired="False" allowMultiSelect="False" name="Ignore OU" contextName="Ignore OU" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
	    </Parameters>
    </ParameterType>	  
    <ParameterType id="f4c178f7-fa5a-4229-92ea-2839c7c82ea4" isSystem="False" name="Auswhal Target Groups CurrentMandator" datatype="IntegerDDL" dataValueField="id" dataTextField="title">
      <query>
        SELECT
        id, title
        FROM
        tblTargetGroups
        WHERE
        mandator_id = @current_mandator_id
        AND
        (
        EXISTS
        (
        SELECT  *
        FROM    v_Users
        WHERE
        intUserCn = @current_user_id
        AND securityId = 100
        )
        OR
        EXISTS
        (
        SELECT  *
        FROM    tblTargetGroupManagers
        WHERE
        tblTargetGroupManagers.user_id = @current_user_id
        AND tblTargetGroupManagers.targetGroup_id = tblTargetGroups.id
        )
        OR
        EXISTS
        (
        SELECT  *
        FROM
        tblRolesFunctions
        JOIN tblRoles
        ON  tblRoles.id = tblRolesFunctions.role_id
        JOIN tblFunctions
        ON  tblFunctions.id = tblRolesFunctions.function_id
        AND tblFunctions.shortcut = 'FcnIsAuthorizedForAllTGs'
        INNER JOIN v_Users
        ON  v_Users.SecurityID = tblRoles.id
        WHERE
        tblRolesFunctions.mandator_id = @current_mandator_id
        AND v_Users.intUserCn = @current_user_id
        )
        )
        AND defaultforlibrary = 0
        ORDER BY
        title
      </query>
    </ParameterType>
    <ParameterType id="cf245854-b392-4105-b1e3-1ee224d3d86a" isSystem="False" name="WBTs for seleted Target Groups mandatory Type" datatype="GuidDDL" dataValueField="id" dataTextField="title">
      <query>
        SELECT DISTINCT tblThemes.id, tblItems.title
          FROM tblThemes
         INNER JOIN tblItems ON tblItems.id = tblThemes.id
         WHERE tblThemes.mandator_id = @current_mandator_id
            AND tblItems.deleted IS NULL
			AND (
				NOT EXISTS (SELECT * FROM @targetGroupId)
				OR
				EXISTS (SELECT 1 FROM tblEduOffersTargetGroups
						INNER JOIN @targetGroupId tg ON tg.value = tblEduOffersTargetGroups.targetGroup_id
						INNER JOIN tblEduOffersModules ON tblEduOffersModules.eduOffer_id = tblEduOffersTargetGroups.eduOffer_id
						INNER JOIN tblModuleItems ON tblModuleItems.module_id = tblEduOffersModules.module_id
						WHERE tblThemes.id = tblModuleItems.item_id
						)
				)
        ORDER BY title
      </query>
      <Parameters>
        <Parameter id="93460158-2520-48a7-ac64-9350fa3d159f" isRequired="False" allowMultiSelect="True" name="Zielgruppen Multiselektion" contextName="Zielgruppen Multiselektion" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
      </Parameters>
    </ParameterType>
  </ParameterTypes>
</ReportsExport>