﻿<?xml version="1.0" encoding="utf-8"?>
<ReportsExport>
  <Reports>
    <Report id="af9993d7-feab-4856-ae9e-5c22eb631c64" codekey="TrainingEventsCount" categoryCodekey="Trainings" name="Anzahl der durchgeführten Seminartermine" description="">
      <MetaData created="2011-01-13T15:39:36" createdBy="VIWIS" createdBy_user_id="3" modified="2015-07-31T09:38:57" modifiedBy="Administrator TestAllFeatures" />
      <ExecutionDetails format="KeyValueResult" commandType="SqlCommandOrQuery" exportHandler="" adminControl="" exportMultipleTablesToSheets="False" datesWithTime="False" extraParams="" />
      <Mandators mandatorMode="ExcludeMandators" mandator_id="96ba2868-8baf-4e69-b1fb-d2cc6d6832e8" isStandard="True" isUsedByMenu="True">
        <Mandator mandatorName="ADAC" />
        <Mandator mandatorName="ELECT" />
      </Mandators>
      <Parameters>
        <Parameter id="6ae269c9-c842-4fd6-96c7-0a6125828996" isRequired="False" allowMultiSelect="False" name="Bereich" contextName="Bereich" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="a2b0adeb-92f3-4ecb-a1e9-5d0c6c952585" isRequired="False" allowMultiSelect="False" name="Seminartitel abhängig von Bereich" contextName="Seminartitel abhängig von Bereich" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="d8e4595f-8bdd-4a54-9298-8f378450e6a0" isRequired="False" allowMultiSelect="False" name="Zeitraum" contextName="Zeitraum" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
      </Parameters>
      <Roles>
        <Role id="90" />
      </Roles>
      <command>
        -- Report: Anzahl der durchgeführten Seminartermine

        /*
        declare @current_mandator_id uniqueidentifier
        select @current_mandator_id = id from tblMandators where name = 'VIWIS'

        declare @division nvarchar(256)
        --set @division = 'Geschäftsstelle'

        declare @trainingTitle_id uniqueidentifier
        --set @trainingTitle_id=null

        declare @dateRange_start datetime
        --set @dateRange_start = '2011-12-01'

        declare @dateRange_end datetime
        --set @dateRange_end = '2010-12-31'
        --*/
        -- The report without debugging information starts here

        -- trick to use @current_mandator_id as @mandator_id (to make ADAC and Standard version as similar as possible)
        DECLARE @mandator_id UNIQUEIDENTIFIER
        SET @mandator_id = @current_mandator_id

        if (@division='' or @division='alle')
        set @division = null

        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 of relevant trainingTitle ids
        -- a training title is relevant if there are users of the given mandator which participated on that traing

        create table #relevantTrainingTitles
        (
        id uniqueidentifier
        )

        insert into #relevantTrainingTitles
        select distinct(tblTrainingTitles.id)
        from tblTrainingTitles
        inner join tblTrainings on tblTrainings.trainingTitle_id=tblTrainingTitles.id
        inner join tblUserTrainingStatus on tblUserTrainingStatus.training_id=tblTrainings.id
        inner join v_Users on v_Users.intUserCn=tblUserTrainingStatus.user_id
        where
        (tblTrainings.isClosed!=0)
        and (@trainingTitle_id is null or tblTrainingTitles.id=@trainingTitle_id)
        and (@mandator_id is null or v_Users.mandator_id=@mandator_id)
        and (@division is null or v_Users.division=@division)
        and (tblUserTrainingStatus.actionclass_id&gt;=70)

        -- first create the history
        declare @displayHistory int
        set @displayHistory = 1

        if ( YEAR(@dateRange_start)=YEAR(@dateRange_end) )
        set @displayHistory = 0

        declare cursorYears cursor
        forward_only read_only
        for
        select distinct(YEAR(tblTrainings.startDate)) as 'year'
        from tblTrainings
        inner join #relevantTrainingTitles on #relevantTrainingTitles.id=tblTrainings.trainingTitle_id
        where
        tblTrainings.isClosed!=0
        and (@dateRange_start is null or tblTrainings.startDate&gt;=@dateRange_start)
        and (@dateRange_end is null or tblTrainings.endDate&lt;=@dateRange_end)
        order by 'year'

        declare @startYear int
        open cursorYears

        declare @historyEvents nvarchar(4000)
        declare @historyUsers nvarchar(4000)

        fetch next from cursorYears into @startYear
        WHILE (@@FETCH_STATUS &lt;&gt; -1)
        BEGIN
        declare @start datetime
        declare @end datetime

        set @start = cast(convert(nvarchar(4), @startYear) + '-01-01T00:00:00' as datetime)
        set @end = cast(convert(nvarchar(4), @startYear+1) + '-01-01T00:00:00' as datetime)

        select
        tblTrainings.id as seminarEvent_id,
        (
        select count(*)
        from tblUserTrainingStatus
        inner join v_Users on v_Users.intUserCn=tblUserTrainingStatus.user_id
        where tblUserTrainingStatus.training_id=tblTrainings.id and tblUserTrainingStatus.actionclass_id&gt;=70
        and (@mandator_id is null or v_Users.mandator_id=@mandator_id)
        and (@division is null or v_Users.division=@division)
        ) as 'numberOfUsers'
        into
        #tempResult
        from tblTrainings
        inner join #relevantTrainingTitles on #relevantTrainingTitles.id=tblTrainings.trainingTitle_id
        where
        tblTrainings.isClosed!=0
        and tblTrainings.startDate&gt;=@start
        and tblTrainings.endDate&lt;@end

        declare @line nvarchar(256)
        declare @countEvents int
        declare @countUsers int

        select @countEvents=count(*) from #tempResult
        where exists(select * from tblUserTrainingStatus
                    inner join v_Users on v_Users.intUserCn=tblUserTrainingStatus.user_id
                    where tblUserTrainingStatus.training_id=#tempResult.seminarEvent_id
                    and tblUserTrainingStatus.actionclass_id&gt;=70
                    and (@mandator_id is null or v_Users.mandator_id=@mandator_id)
                    and (@division is null or v_Users.division=@division)
                    )
        select @countUsers=SUM(numberOfUsers) from #tempResult

        drop table #tempResult

        set @line = CONVERT(nvarchar(4), @startYear) + ':'	+ CONVERT(nvarchar(8), @countEvents)
        if ( @historyEvents is null )
        set @historyEvents = @line
        else
        set @historyEvents = @historyEvents + ' ; ' + @line

        set @line = CONVERT(nvarchar(4), @startYear) + ':'	+ CONVERT(nvarchar(8), @countUsers)
        if ( @historyUsers is null )
        set @historyUsers = @line
        else
        set @historyUsers = @historyUsers + ' ; ' + @line

        fetch next from cursorYears into @startYear
        END

        if ( @historyEvents is null )
        set @displayHistory = 0

        close cursorYears
        deallocate cursorYears

        select
        tblTrainings.id as seminarEvent_id,
        (
        select count(*)
        from tblUserTrainingStatus
        inner join v_Users on v_Users.intUserCn=tblUserTrainingStatus.user_id
        where tblUserTrainingStatus.training_id=tblTrainings.id and tblUserTrainingStatus.actionclass_id&gt;=70
        and (@mandator_id is null or v_Users.mandator_id=@mandator_id)
        and (@division is null or v_Users.division=@division)
        ) as 'numberOfUsers'
        into
        #tempResult1
        from tblTrainings
        inner join #relevantTrainingTitles on #relevantTrainingTitles.id=tblTrainings.trainingTitle_id
        where
        tblTrainings.isClosed!=0
        and (@dateRange_start is null or tblTrainings.startDate&gt;=@dateRange_start)
        and (@dateRange_end is null or tblTrainings.endDate&lt;=@dateRange_end)
        and exists(select * from tblUserTrainingStatus
                  inner join v_Users on v_Users.intUserCn=tblUserTrainingStatus.user_id
                  where tblUserTrainingStatus.training_id=tblTrainings.id
                  and tblUserTrainingStatus.actionclass_id&gt;=70
                  and (@mandator_id is null or v_Users.mandator_id=@mandator_id)
                  and (@division is null or v_Users.division=@division)
                  )

        if ( @displayHistory!=0 )
        select
        convert(nvarchar, GETDATE(), 104) as 'Auswertung vom:',
        count(*) as 'Anzahl Seminartermine',
        @historyEvents as 'History events',
        sum(numberOfUsers) as 'Anzahl Seminarteilnehmer',
        @historyUsers as 'History users'
        from #tempResult1
        else
        select
        convert(nvarchar, GETDATE(), 104) as 'Auswertung vom:',
        count(*) as 'Anzahl Seminartermine',
        sum(numberOfUsers) as 'Anzahl Seminarteilnehmer'
        from #tempResult1

        drop table #tempResult1
        drop table #relevantTrainingTitles


        -- Formatting starts here

        create table #$formating_0
        (
        tr nvarchar(128),
        col0f  nvarchar(128),
        col0v nvarchar(128),
        col1f  nvarchar(128),
        col1v nvarchar(128),
        sortOrder int
        )

        insert into #$formating_0
        select 'format info', 'cellspacing="4"', '', '', '', -1

        insert into #$formating_0
        select 'class="AdminGeneral"', 'style="width:150px"', '{key_0}', 'style="width: 200px;border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_0}', 0

        insert into #$formating_0
        select 'class="AdminGeneral"', 'style="width:150px"', '{key_1}', 'style="width: 200px;border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_1}', 1

        if ( @displayHistory!=0 )
        insert into #$formating_0
        select 'class="AdminGeneral"', 'style="width:150px"', '', 'style="width: 200px;border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:left"', '{value_2}', 2

        if ( @displayHistory!=0 )
        begin
        insert into #$formating_0
        select 'class="AdminGeneral"', 'style="width:150px"', '{key_3}', 'style="width: 200px;border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_3}', 3

        insert into #$formating_0
        select 'class="AdminGeneral"', 'style="width:150px"', '', 'style="width: 200px;border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:left"', '{value_4}', 4
        end
        else
        begin
        insert into #$formating_0
        select 'class="AdminGeneral"', 'style="width:150px"', '{key_2}', 'style="width: 200px;border:2px inset #ffffff; background-color: #c0c0c0; font-weight:bold; text-align:right"', '{value_2}', 3
        end

        select * from #$formating_0 order by sortOrder
        drop table #$formating_0
      </command>
    </Report>
  </Reports>
  <Parameters>
    <Parameter id="6ae269c9-c842-4fd6-96c7-0a6125828996" isSystem="False" name="Bereich" reportParameterType_id="e7333e57-684c-4b48-93fe-fd396d5f3559" queryParameterName="@division" />
    <Parameter id="a2b0adeb-92f3-4ecb-a1e9-5d0c6c952585" isSystem="False" name="Seminartitel abhängig von Bereich" reportParameterType_id="de447c2e-919b-48fc-89ad-18a088ed7695" queryParameterName="@trainingTitle_id" />
    <Parameter id="d8e4595f-8bdd-4a54-9298-8f378450e6a0" isSystem="True" name="Zeitraum" reportParameterType_id="abff13be-91c3-4ee1-93a3-7292f8e013ba" queryParameterName="@dateRange" />
  </Parameters>
  <ParameterTypes>
    <ParameterType id="e7333e57-684c-4b48-93fe-fd396d5f3559" isSystem="False" name="Bereich" datatype="StringDDL" dataValueField="division" dataTextField="division">
      <query>
        SELECT DISTINCT division
        FROM v_Users
        WHERE v_Users.mandator_id = @current_mandator_id AND division IS NOT NULL AND division != ''
        ORDER BY division
      </query>
    </ParameterType>
    <ParameterType id="de447c2e-919b-48fc-89ad-18a088ed7695" isSystem="False" name="Seminartitel abhängig von Bereich" datatype="GuidDDL" dataValueField="value" dataTextField="text">
      <query>
        select distinct(tblTrainingTitles.id) AS value, tblItems.title as text
        from tblTrainingTitles
        inner join tblItems on tblItems.id=tblTrainingTitles.id
        inner join tblTrainings on tblTrainings.trainingTitle_id=tblTrainingTitles.id
        inner join tblUserTrainingStatus on tblUserTrainingStatus.training_id=tblTrainings.id
        inner join v_Users on v_Users.intUserCn=tblUserTrainingStatus.user_id
        where
        (tblTrainings.isClosed!=0)
        and v_Users.mandator_id=@current_mandator_id
        and (@division is null or @division = 'alle' or v_Users.division=@division)
        and (tblUserTrainingStatus.actionclass_id&gt;=70)
        order by tblItems.title
      </query>
      <Parameters>
        <Parameter id="6ae269c9-c842-4fd6-96c7-0a6125828996" isRequired="False" allowMultiSelect="False" name="Bereich" contextName="Bereich" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
      </Parameters>
    </ParameterType>
    <ParameterType id="abff13be-91c3-4ee1-93a3-7292f8e013ba" isSystem="True" name="DateRange" datatype="DateRange" dataValueField="" dataTextField="" />
  </ParameterTypes>
</ReportsExport>