﻿<?xml version="1.0" encoding="utf-8"?>
<ReportsExport>
	<Reports>
		<Report id="6d879ae9-29da-459e-b6f7-d99c2123f8a5" codekey="glc_theme_per_department_cy" categoryCodekey="UserStats" name="GCL Gesamtübersicht CY" description="Stati der Lerner bei der Kalenderjährlich einmalig Rezertifizierung einzelner Programme, ausgewertet nach Departments.">
			<MetaData created="2020-06-12T15:39:52" createdBy="62483_LastName, 62483_FirstName (62483)" createdBy_user_id="62483" modified="2024-09-03T12:45:06" modifiedBy="Administrator_LastName, Administrator_FirstName (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="a8a7528e-21e3-4608-97f6-c363a985de0e" isRequired="True" allowMultiSelect="False" name="Lernprogramm" contextName="Selected program" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
			</Parameters>
			<Roles>
				<Role id="50" />
			</Roles>
			<command>
				--      declare @theme_id uniqueidentifier
				--SET @theme_id = 'AC43585C-7542-4021-91E1-6020CE4620E1'

				--      declare @current_mandator_id uniqueidentifier
				--      select @current_mandator_id = id from tblMandators where name = 'ELECT'

				-- The report without debugging information starts here
				--APA4
				-- The report without debugging information starts here
				DECLARE @recertificationInterval INT
				DECLARE @MinUsers INT = 2
				DECLARE @todayDay DATETIME
				SET @todayDay = GETDATE()
				SELECT @recertificationInterval = overrideSpanMonths FROM tblItems WHERE id = @theme_id

				CREATE TABLE #result
				(
					user_id INT,
					division NVARCHAR(200),
					company NVARCHAR(200),
					activeLearner INT,
					greenStatus INT,
					yellowStatus INT,
					greyStatus INT,
					overrideDate DATETIME NULL
				)

				-- select all users, which had the WBT assigned in the given time range and which have a "non empty" department set and calculate theme status and activity
				INSERT INTO #result
				SELECT
					intUserCn,
					division,
					ISNULL(tblUserCustomAttributes.customAttribute3,'') AS [Company],
					
					-- we have to check tblStatsThemeSessions and tblStatusUserItemHistory as there are some statuses without entry in tblStatsThemeSessions
					CASE
					WHEN EXISTS (SELECT * FROM tblStatsThemeSessions
					WHERE tblStatsThemeSessions.theme_id = @theme_id
					AND tblStatsThemeSessions.durationSeconds IS NOT NULL
					AND tblStatsThemeSessions.user_id = v_users.intUserCn
					)
					OR EXISTS (SELECT * FROM tblStatusUserItemHistory
					WHERE tblStatusUserItemHistory.item_id = @theme_id
					AND tblStatusUserItemHistory.user_id = v_users.intUserCn
					)
					THEN 1
					ELSE 0
					END AS [ActiveLearner], -- active learner provided some status information
					
					CASE WHEN ((SELECT TOP 1 ISNULL(status,0) FROM tblStatusUserItemHistory UIH
					WHERE user_id = v_users.intUserCn
					AND UIH.item_id = @theme_id
					ORDER BY endDate
					)
					= 2)
					THEN 1
					ELSE 0
					END AS [GreenStatus],
					
					CASE WHEN ((SELECT TOP 1 ISNULL(status,0) FROM tblStatusUserItemHistory UIH
					WHERE user_id = v_users.intUserCn
					AND UIH.item_id = @theme_id
					ORDER BY endDate
					)
					= 1)
					THEN 1
					ELSE 0
					END AS [YellowStatus],
					
					CASE WHEN ((SELECT TOP 1 ISNULL(status,0) FROM tblStatusUserItemHistory UIH
					WHERE user_id = v_users.intUserCn
					AND UIH.item_id = @theme_id
					ORDER BY endDate
					)
					= 0)
					THEN 1
					ELSE 0
					END AS [GreyStatus],
					
					NULL --overrideDate
					
				FROM v_users
					LEFT JOIN tblUserCustomAttributes ON tblUserCustomAttributes.user_id = v_users.intUserCn
				WHERE
					EXISTS 
						(SELECT * FROM tblStatusUserItemOverride
						  WHERE tblStatusUserItemOverride.item_id = @theme_id
							AND tblStatusUserItemOverride.user_id = v_users.intUserCn
						)
					AND ISNULL(division,'') != ''
					--AND division = 'APA3'
					AND NOT EXISTS 
						(SELECT * FROM tblRecertificationExcludedTargetGroups AS excluded
						INNER JOIN tblUsersTargetGroups AS utg ON excluded.targetGroup_id = utg.targetGroup_id
						AND excluded.item_id = @theme_id
						WHERE utg.UserCn = v_users.intUserCn)
					AND v_Users.userStatus NOT IN (2,3)


				-- getting override information for the item
				UPDATE #result
					SET overrideDate = ISNULL(tblStatusUserItemOverride.overrideDate,dbo.fn_GetUserItemStatusWillOverrideDate(#result.user_id, @theme_id))
				FROM #result INNER JOIN tblStatusUserItemOverride
					ON #result.user_id = tblStatusUserItemOverride.user_id AND tblStatusUserItemOverride.item_id = @theme_id

				UPDATE #result
					SET greyStatus = 1
				WHERE greenStatus + yellowStatus + greyStatus = 0

				--SELECT * FROM #result
				--SELECT * FROM tblStatusUserItemHistory AS suih
				--INNER JOIN #result AS r ON r.user_id = suih.user_id AND suih.item_id = @theme_id
				--ORDER BY suih.user_id , status

				-- get the desired result
				;WITH CTE
				AS(
				SELECT
					division AS [Department],
					
					CASE WHEN -- When there is a boss inside the department, dont show bosses from outside
					(EXISTS (SELECT * FROM v_users
					WHERE v_users.division = #result.division
					AND EXISTS (SELECT * FROM tblOrganisationRolesOfUsers WHERE tblOrganisationRolesOfUsers.user_id = v_users.intUserCn ))
					)
					THEN
					(SELECT   Bosses.Nachname + ', ' + Bosses.Vorname + ' (' +  Bosses.descUserCn + '); '
					FROM v_users Bosses
					WHERE EXISTS (SELECT * FROM v_users
					JOIN tblUsersOrganisationRolesUsers ON tblUsersOrganisationRolesUsers.userFor_id = v_users.intUserCn
					WHERE v_users.division = #result.division
					AND tblUsersOrganisationRolesUsers.userIs_id = Bosses.intUserCn
					)
					AND Bosses.division = #result.division
					FOR XML PATH (''))
					ELSE
					(SELECT   Bosses.Nachname + ', ' + Bosses.Vorname + ' (' +  Bosses.descUserCn + '); '
					FROM v_users Bosses
					WHERE EXISTS (SELECT * FROM v_users
					JOIN tblUsersOrganisationRolesUsers ON tblUsersOrganisationRolesUsers.userFor_id = v_users.intUserCn
					WHERE v_users.division = #result.division
					AND tblUsersOrganisationRolesUsers.userIs_id = Bosses.intUserCn
					)
					FOR XML PATH (''))
					END
					AS [manager],
					
					Company AS [Company],
					
					-- counting the users per department company
					COUNT(*) AS 'totalLearners',
					
					-- users with no green status in the given periode
					SUM(case when ISNULL(greenStatus, 0) = 0 AND overrideDate &gt; @todayDay THEN 1 ELSE 0 END) AS 'dueLearnersInPeriode',
					
					-- expired users due of no green status and periode is over
					SUM(CASE WHEN overrideDate &lt; @todayDay AND ISNULL(greenStatus, 0) = 0 THEN 1 ELSE 0 END) AS 'expiredLearnersInPeriode',
					
					-- users with green status in the given periode
					SUM(greenStatus) AS 'completedLearnersInPeriode'
				
				FROM
					#result
				GROUP BY division,company
				)
				SELECT
					[Department],
					[manager],
					[Company],
					
					-- counting the users per department company
					totalLearners,
					
					-- users with no green status in the given periode
					dueLearnersInPeriode AS dueLearnersInPeriode,
					
					-- users with green status in the given periode
					completedLearnersInPeriode AS completedLearnersInPeriode,
					
					CASE WHEN ISNULL(totalLearners,0) &gt; 0 THEN 
						ROUND(CAST(completedLearnersInPeriode * 100 AS FLOAT) / totalLearners,2)
					ELSE NULL
					END AS completedLearnersInPercentage,
					
					-- users with override date in the past
					expiredLearnersInPeriode AS expiredLearnersInPeriode,
					
					CASE WHEN ISNULL(totalLearners,0) &gt; 0 THEN 
						ROUND(CAST((expiredLearnersInPeriode) * 100 AS FLOAT) / totalLearners,2)
					ELSE NULL
					END AS expiredLearnersInPercentage
					
				INTO #tmpResult
				FROM CTE

				SELECT *
				FROM
				(
				SELECT
					[Department],
					[manager],
					[Company],
				
					-- counting the users per department company
					totalLearners AS [Learners in recertification],
				
					-- users with no green status in the given periode
					CASE WHEN (totalLearners &gt; @MinUsers) THEN
						dueLearnersInPeriode
					ELSE NULL END AS 'Due learners in certification period',
				
					-- users with green status in the given periode
					CASE WHEN (totalLearners &gt; @MinUsers) THEN
						completedLearnersInPeriode
					ELSE NULL END AS [Learners completed in abs],
				
					CASE WHEN (totalLearners &gt; @MinUsers) THEN 
						completedLearnersInPercentage
					ELSE NULL
					END AS [Learners completed in %],
				
					-- users with override date in the past
					CASE WHEN (totalLearners &gt; @MinUsers) THEN
						expiredLearnersInPeriode
					ELSE NULL END AS [Leaners expired abs],
					
					CASE WHEN (totalLearners &gt; @MinUsers) AND ISNULL(totalLearners,0) &gt; 0 THEN 
						expiredLearnersInPercentage
					ELSE NULL
					END AS [Learners expired in %]
				
				FROM #tmpResult
				
				UNION ALL
				
				SELECT
					NULL,
					NULL,
					NULL,
					
					-- counting the users
					SUM(totalLearners),
					
					-- users with no green status in the given periode
					SUM(dueLearnersInPeriode) AS 'Due learners in certification period',
					
					-- users with green status in the given periode
					SUM(completedLearnersInPeriode) AS 'Learners completed in abs',
					
					CASE WHEN ISNULL(SUM(totalLearners),0) &gt; 0 THEN 
						ROUND(CAST((SUM(completedLearnersInPeriode)) * 100 AS FLOAT) / SUM(totalLearners),2)
					ELSE NULL
					END AS [Learners completed in %],

					-- users with override date in the past
					SUM(expiredLearnersInPeriode) AS [Leaners expired abs],

					CASE WHEN ISNULL(SUM(totalLearners),0) &gt; 0 THEN 
						ROUND(CAST((SUM(expiredLearnersInPeriode)) * 100 AS FLOAT) / SUM(totalLearners),2)
					ELSE NULL
					END AS [Learners expired in %]

				FROM #tmpResult
				) AS A ORDER BY ISNULL([Department],'ZZZZZZZZZZZZZ'),company
				
				DROP TABLE #tmpResult
				DROP TABLE #result
			</command>
		</Report>
	</Reports>
	<Parameters>
		<Parameter id="a8a7528e-21e3-4608-97f6-c363a985de0e" mandator_id="d5a90a49-e697-4aeb-8a5b-298782815490" isSystem="False" name="WBTs assigned to module GCL Auswertung CY" reportParameterType_id="d89f3146-4cc4-4d7c-850a-1bf997274a8e" queryParameterName="@theme_id" />
	</Parameters>
	<ParameterTypes>
		<ParameterType id="d89f3146-4cc4-4d7c-850a-1bf997274a8e" mandator_id="d5a90a49-e697-4aeb-8a5b-298782815490" isSystem="False" name="WBTs assigned to module GCL Auswertung CY" datatype="GuidDDL" dataValueField="id" dataTextField="title">
			<query>
				declare @module_id uniqueidentifier = (select top 1 id from tblItems where title like 'GCL Auswertung')
				SELECT DISTINCT tblItems.id, tblItems.title
				FROM tblItems
					JOIN tblModuleItems ON tblModuleItems.item_id = tblItems.id
				WHERE (@module_id is null or tblModuleItems.module_id = @module_id)
					AND itemType_id = 1 --theme
					AND tblItems.certificationType = 3 -- CalendarYearlyOnce
					AND tblItems.deleted is null
				ORDER BY title
			</query>
		</ParameterType>
	</ParameterTypes>
</ReportsExport>