﻿<?xml version="1.0" encoding="utf-8"?>
<ReportsExport>
  <Reports>
    <Report id="79671c59-6e40-4fba-bf88-51af4f74d651" codekey="dividionsperwbt" categoryCodekey="LearningPrograms" name="Department  per WBT" 
            description="The report list all departments for a selected learning program, which contain users, which accessed the learning programm in the selected period.">
      <MetaData created="2012-05-08T13:52:30" createdBy="unbekannt unbekannt (2)" createdBy_user_id="1" modified="2015-08-01T20:25:10" modifiedBy="Administrator ELECT" />
      <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="False" allowMultiSelect="False" name="Zeitraum" contextName="Selected period" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="a3916c66-34e4-4a8c-a399-12e038af0e93" isRequired="True" allowMultiSelect="False" name="Lernprogramm" contextName="Selected program" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
      </Parameters>
      <Roles>
        <Role id="90" />
      </Roles>
      <command>
        -- Report for the ERGO mandator, created as part of Viversa 7.0 development

		/* Debugging information 
		declare @theme_id uniqueidentifier
        select  @theme_id = theme_id from tblStatsThemeSessions group by theme_id having count(theme_id) &gt; 40 order by count(theme_id) ASC
        select @theme_id

        declare @current_mandator_id uniqueidentifier
        select @current_mandator_id = id from tblMandators where name = 'ELECT'

        declare @dateRange_start datetime
        set @dateRange_start = '2011-11-01'

        declare @dateRange_end datetime
        set @dateRange_end = '2012-01-02'
        */

        -- The report without debugging information starts here

        if (@dateRange_start is not null)
        set @dateRange_start = cast(convert(NVARCHAR(32), @dateRange_start, 112) as datetime)

        if ( @dateRange_end is not null )
        begin
        set @dateRange_end  = cast(convert(NVARCHAR(32), @dateRange_end, 112) as datetime)
        set @dateRange_end = dateadd(day, 1, @dateRange_end)
        set @dateRange_end = dateadd(second, -1, @dateRange_end)
        end


        -- create a table which holds all individual users having a theme session with the sum of learning times over all themesessions of the user
        SELECT
        user_id,
        (SELECT COUNT (*) FROM tableUsers AS usr
        WHERE usr.division = tableUsers.division
        AND userType = 0 -- "Normal" user
        AND (@dateRange_start IS NULL OR usr.deleted IS NULL OR (@dateRange_start IS NOT NULL AND @dateRange_start &lt; usr.deleted))
        AND (@dateRange_end IS NULL OR (@dateRange_end IS NOT NULL AND @dateRange_end &gt; created))) AS 'CountUsersPerDivision',
        SUM(tblStatsThemeSessions.durationSeconds) as 'sumLearningTime',
        tableUsers.division
        INTO
        #tempResult
        FROM
        tblStatsThemeSessions
        INNER JOIN tableUsers on tableUsers.intUserCn = tblStatsThemeSessions.user_id
        WHERE
        tblStatsThemeSessions.theme_id=@theme_id
        AND tblStatsThemeSessions.durationSeconds IS NOT NULL
        AND (@dateRange_start IS NULL OR tblStatsThemeSessions.startTime &gt; @dateRange_start)
        AND (@dateRange_end IS NULL OR tblStatsThemeSessions.startTime &lt; @dateRange_end)
        AND tableUsers.division IS NOT NULL
        AND tableUsers.userType = 0 -- "Normal" user
        GROUP BY user_id,  tableUsers.division

        -- get the desired result
        SELECT
        division AS 'Department',
        CountUsersPerDivision AS 'users in department',
        COUNT(*) as 'learners in department',
        ISNULL(ROUND(CAST(COUNT(*)*100 AS FLOAT) / NULLIF((CountUsersPerDivision),0),2),0) as 'learners in percentage',
        round(cast(avg(sumLearningTime) as float)/60, 2) as 'Average learning time per user (min.)'
        FROM
        #tempResult
        GROUP BY division,CountUsersPerDivision
        ORDER BY division

        DROP TABLE #tempResult
      </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="a3916c66-34e4-4a8c-a399-12e038af0e93" isSystem="True" name="Lernprogramm" reportParameterType_id="9b1ab1b2-f839-433b-8da7-02781b96def7" queryParameterName="@theme_id" />
  </Parameters>
  <ParameterTypes>
    <ParameterType id="abff13be-91c3-4ee1-93a3-7292f8e013ba" isSystem="True" name="DateRange" datatype="DateRange" dataValueField="" dataTextField="" />
    <ParameterType id="9b1ab1b2-f839-433b-8da7-02781b96def7" isSystem="True" name="LearningProgramme" datatype="Theme" dataValueField="" dataTextField="" />
  </ParameterTypes>
</ReportsExport>