﻿<?xml version="1.0" encoding="utf-8"?>
<ReportsExport>
  <Reports>
    <Report id="78ba72aa-42f9-4890-a4d8-4c1794e2489d" codekey="Neue_ERGO_PollStatisticsPerTargetGroup" categoryCodekey="TestsAndQuestions" name="Pulse Check" description="Poll statistics per target group">
      <MetaData created="2011-06-08T10:28:51" createdBy="Administrator neue_ERGO" createdBy_user_id="12" modified="2012-01-13T11:35:51" modifiedBy="Anton Administrator" />
      <ExecutionDetails format="TableResult" commandType="SqlCommandOrQuery" exportHandler="" adminControl="" exportMultipleTablesToSheets="False" datesWithTime="False" extraParams="" />
      <Mandators mandatorMode="OnlyOwner" mandator_id="345bbab2-62a8-424d-a6e6-133aeab480a4" mandatorName="Neue_ERGO" isStandard="False" isUsedByMenu="False" />
      <Parameters>
        <Parameter id="a5485539-67e0-4912-8255-8081a54efb3c" isRequired="False" name="Zielgruppe" contextName="Zielgruppe" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="4aaf4f45-f70a-452a-92b5-f8e9b8b0dfb6" isRequired="True" name="Test" contextName="Test" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
      </Parameters>
      <Roles />
      <command>SET ARITHABORT ON
SET QUOTED_IDENTIFIER ON 

create table #result
(
	col1 varchar(128),
	col2 varchar(1024),
	col3 varchar(128)
)

declare userCursor cursor
	forward_only static read_only for
	select distinct v_Users.intUserCn as 'user_id'
	from v_Users
	where @targetGroup_id is null or exists (select * from tblUsersTargetGroups where tblUsersTargetGroups.userCn=v_Users.intUserCn and targetGroup_id = @targetGroup_id)

declare @user_id int
declare @ts_id uniqueidentifier

open userCursor 
fetch next from userCursor into @user_id

-- user loop
while(@@FETCH_STATUS&lt;&gt;-1)
	begin
		set @ts_id = null
		declare tsCursor cursor
			forward_only static read_only for
			select testsession_id from tblTasStatsTestSessions 
			where test_id = @test_id and user_id = @user_id order by endDate desc

		open tsCursor 
		fetch next from tsCursor into @ts_id

		if(@@FETCH_STATUS&lt;&gt;-1)
		begin
			-- user name
			insert into #result (col1, col2) values('Nutzer:', (select Nachname + ' ' + Vorname from v_Users where intUserCn = @user_id))		

			-- ts loop
			while(@@FETCH_STATUS&lt;&gt;-1)
				begin
					-- empty line		
					insert into #result (col1) values(' ')
					-- poll date
					insert into #result (col1, col2) values('Poll - teilgenommen am:', (select endDate from tblTasStatsTestSessions where testsession_id = @ts_id))

					-- questions
					declare @q_id uniqueidentifier
					declare questionCursor cursor
						forward_only static read_only for
						select questionId
						from tblTasTestSessionQuestions
						where testSessionId = @ts_id order by questionIndexWithinPart

					open questionCursor 
					fetch next from questionCursor into @q_id
					while(@@FETCH_STATUS&lt;&gt;-1)
						begin
							declare @answerCount int
							declare @q_number int
							declare @q_type int
							declare @q_subType int
							declare @xml xml
							declare @answerXml xml
							declare @q_text nvarchar(1024)
							declare @lowerBound nvarchar(64)
							declare @upperBound nvarchar(64)

							select @q_number = numberSuffix, @xml = questionXML, @q_type = type from tblTasQuestions where id = @q_id
							
							-- question number
							insert into #result (col1) values('Fragennummer: ' + cast(@q_number as varchar(20)))
							
							if(@q_type in (1,3,6))-- choice questions single,multiple,gap
							begin
								-- question text
								SELECT @q_text = @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartChoice[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/MultilingualText[1]/LanguageTexts[1]/LanguageText[1]', 'VARCHAR(1024)')
								set @q_text = replace(@q_text, '&amp;nbsp;', '')
								insert into #result (col1, col2) values('Fragetext:', @q_text)
								
								-- question answers
								set @answerCount = cast(cast(@xml.query
								('count(/Question/QuestionElement/RichElement/Elements/QuestionPartChoice/QuestionPart/QuestionElement/RichElement/Elements/AnswerPartChoice)') as varchar(3)) as INTEGER)

								if(@answerCount&gt;0) insert into #result (col1, col2, col3) values('Antwort 1:', @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartChoice[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/AnswerPartChoice[1]/RichElement[1]/Elements[1]/MultilingualText[1]/LanguageTexts[1]/LanguageText[1]', 'VARCHAR(64)'), 
								case when exists(select * from tblTasStatsQuestionsChoice where testsession_id=@ts_id and question_id=@q_id and selected=1 and answerpart_id = @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartChoice[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/AnswerPartChoice[1]/RichElement[1]/LayoutableElement[1]/@id', 'uniqueidentifier')) then 'abgegeben' else '-' end)

								if(@answerCount&gt;1) insert into #result (col1, col2, col3) values('Antwort 2:', @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartChoice[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/AnswerPartChoice[2]/RichElement[1]/Elements[1]/MultilingualText[1]/LanguageTexts[1]/LanguageText[1]', 'VARCHAR(64)'),
								case when exists(select * from tblTasStatsQuestionsChoice where testsession_id=@ts_id and question_id=@q_id and selected=1 and answerpart_id = @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartChoice[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/AnswerPartChoice[2]/RichElement[1]/LayoutableElement[1]/@id', 'uniqueidentifier')) then 'abgegeben' else '-' end)

								if(@answerCount&gt;2) insert into #result (col1, col2, col3) values('Antwort 3:', @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartChoice[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/AnswerPartChoice[3]/RichElement[1]/Elements[1]/MultilingualText[1]/LanguageTexts[1]/LanguageText[1]', 'VARCHAR(64)'),
								case when exists(select * from tblTasStatsQuestionsChoice where testsession_id=@ts_id and question_id=@q_id and selected=1 and answerpart_id = @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartChoice[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/AnswerPartChoice[3]/RichElement[1]/LayoutableElement[1]/@id', 'uniqueidentifier')) then 'abgegeben' else '-' end)

								if(@answerCount&gt;3) insert into #result (col1, col2, col3) values('Antwort 4:', @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartChoice[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/AnswerPartChoice[4]/RichElement[1]/Elements[1]/MultilingualText[1]/LanguageTexts[1]/LanguageText[1]', 'VARCHAR(64)'),
								case when exists(select * from tblTasStatsQuestionsChoice where testsession_id=@ts_id and question_id=@q_id and selected=1 and answerpart_id = @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartChoice[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/AnswerPartChoice[4]/RichElement[1]/LayoutableElement[1]/@id', 'uniqueidentifier')) then 'abgegeben' else '-' end)

								if(@answerCount&gt;4) insert into #result (col1, col2, col3) values('Antwort 5:', @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartChoice[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/AnswerPartChoice[5]/RichElement[1]/Elements[1]/MultilingualText[1]/LanguageTexts[1]/LanguageText[1]', 'VARCHAR(64)'),
								case when exists(select * from tblTasStatsQuestionsChoice where testsession_id=@ts_id and question_id=@q_id and selected=1 and answerpart_id = @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartChoice[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/AnswerPartChoice[5]/RichElement[1]/LayoutableElement[1]/@id', 'uniqueidentifier')) then 'abgegeben' else '-' end)

								if(@answerCount&gt;5) insert into #result (col1, col2, col3) values('Antwort 6:', @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartChoice[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/AnswerPartChoice[6]/RichElement[1]/Elements[1]/MultilingualText[1]/LanguageTexts[1]/LanguageText[1]', 'VARCHAR(64)'),
								case when exists(select * from tblTasStatsQuestionsChoice where testsession_id=@ts_id and question_id=@q_id and selected=1 and answerpart_id = @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartChoice[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/AnswerPartChoice[6]/RichElement[1]/LayoutableElement[1]/@id', 'uniqueidentifier')) then 'abgegeben' else '-' end)

								if(@answerCount&gt;6) insert into #result (col1, col2, col3) values('Antwort 7:', @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartChoice[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/AnswerPartChoice[7]/RichElement[1]/Elements[1]/MultilingualText[1]/LanguageTexts[1]/LanguageText[1]', 'VARCHAR(64)'),
								case when exists(select * from tblTasStatsQuestionsChoice where testsession_id=@ts_id and question_id=@q_id and selected=1 and answerpart_id = @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartChoice[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/AnswerPartChoice[7]/RichElement[1]/LayoutableElement[1]/@id', 'uniqueidentifier')) then 'abgegeben' else '-' end)

								if(@answerCount&gt;7) insert into #result (col1, col2, col3) values('Antwort 8:', @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartChoice[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/AnswerPartChoice[8]/RichElement[1]/Elements[1]/MultilingualText[1]/LanguageTexts[1]/LanguageText[1]', 'VARCHAR(64)'),
								case when exists(select * from tblTasStatsQuestionsChoice where testsession_id=@ts_id and question_id=@q_id and selected=1 and answerpart_id = @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartChoice[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/AnswerPartChoice[8]/RichElement[1]/LayoutableElement[1]/@id', 'uniqueidentifier')) then 'abgegeben' else '-' end)

								if(@answerCount&gt;8) insert into #result (col1, col2, col3) values('Antwort 9:', @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartChoice[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/AnswerPartChoice[9]/RichElement[1]/Elements[1]/MultilingualText[1]/LanguageTexts[1]/LanguageText[1]', 'VARCHAR(64)'),
								case when exists(select * from tblTasStatsQuestionsChoice where testsession_id=@ts_id and question_id=@q_id and selected=1 and answerpart_id = @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartChoice[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/AnswerPartChoice[9]/RichElement[1]/LayoutableElement[1]/@id', 'uniqueidentifier')) then 'abgegeben' else '-' end)

								if(@answerCount&gt;9) insert into #result (col1, col2, col3) values('Antwort 10:', @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartChoice[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/AnswerPartChoice[10]/RichElement[1]/Elements[1]/MultilingualText[1]/LanguageTexts[1]/LanguageText[1]', 'VARCHAR(64)'),
								case when exists(select * from tblTasStatsQuestionsChoice where testsession_id=@ts_id and question_id=@q_id and selected=1 and answerpart_id = @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartChoice[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/AnswerPartChoice[10]/RichElement[1]/LayoutableElement[1]/@id', 'uniqueidentifier')) then 'abgegeben' else '-' end)
								
								insert into #result (col1, col2, col3) values('Keine Antwort abgegeben', null, case when exists(select * from tblTasStatsQuestionsChoice where testsession_id=@ts_id and question_id=@q_id and selected=1) then '-' else 'abgegeben' end)
							end
							else if(@q_type in (4,5,10))-- value questions value/gap/open
							begin
								-- question text
								if(@q_type = 10)
									SELECT @q_text = @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartOpen[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/MultilingualText[1]/LanguageTexts[1]/LanguageText[1]', 'VARCHAR(1024)')
								else
									SELECT @q_text = @xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartValue[1]/QuestionPart[1]/QuestionElement[1]/RichElement[1]/Elements[1]/MultilingualText[1]/LanguageTexts[1]/LanguageText[1]', 'VARCHAR(1024)')
									
								set @q_text = replace(@q_text, '&amp;nbsp;', '')
								insert into #result (col1, col2) values('Fragetext:', @q_text)
							
								-- select answer xml and check answer count
								select @answerXml = answerXml from tblTasTestSessionQuestions where testSessionId = @ts_id and questionId = @q_id
								set @answerCount = cast(cast(@answerXml.query('count(/AllAnswers[1]/Answers)') as varchar(3)) as INTEGER)

								-- question answers
								set @q_subType = cast(@xml.value('/Question[1]/QuestionElement[1]/RichElement[1]/Elements[1]/QuestionPartValue[1]/@subType', 'VARCHAR(3)') as INTEGER)
								if(@q_subType=0 or @q_type = 10)-- text value or open
								begin
									if(@answerCount&gt;0) insert into #result (col1, col2) values('Antwort 1:', @answerXml.value('/AllAnswers[1]/Answers[1]/AnswerValueText[1]/Text[1]/PlainMultilingualText[1]/LanguageTexts[1]/LanguageText[1]', 'VARCHAR(64)'))
									if(@answerCount&gt;1) insert into #result (col1, col2) values('Antwort 2:', @answerXml.value('/AllAnswers[1]/Answers[2]/AnswerValueText[1]/Text[1]/PlainMultilingualText[1]/LanguageTexts[1]/LanguageText[1]', 'VARCHAR(64)'))
								end
								else if(@q_subType=1)-- date value
								begin
									if(@answerCount&gt;0) 
									begin
										set @lowerBound = @answerXml.value('/AllAnswers[1]/Answers[1]/AnswerValueDate[1]/LowerBound[1]', 'VARCHAR(64)')
										set @upperBound = @answerXml.value('/AllAnswers[1]/Answers[1]/AnswerValueDate[1]/UpperBound[1]', 'VARCHAR(64)')
										insert into #result (col1, col2, col3) values('Antwort 1:', @lowerBound, case when @lowerBound&lt;&gt;@upperBound then @upperBound else '' end)
									end
								end
								else if(@q_subType=2)-- numeric value
								begin
									if(@answerCount&gt;0) 
									begin
										set @lowerBound = @answerXml.value('/AllAnswers[1]/Answers[1]/AnswerValueNumber[1]/LowerBound[1]', 'VARCHAR(64)')
										set @upperBound = @answerXml.value('/AllAnswers[1]/Answers[1]/AnswerValueNumber[1]/UpperBound[1]', 'VARCHAR(64)')
										insert into #result (col1, col2, col3) values('Antwort 1:', @lowerBound, case when @lowerBound&lt;&gt;@upperBound then @upperBound else '' end)
									end
								end
							end
						
							fetch next from questionCursor into @q_id
						end				
					close questionCursor
					deallocate questionCursor
					
					fetch next from tsCursor into @ts_id
				end
			-- empty line		
			insert into #result (col1) values(' ')
		end
		close tsCursor
		deallocate tsCursor
		
		fetch next from userCursor into @user_id
	end
close userCursor
deallocate userCursor

select * from #result
drop table #result
</command>
    </Report>
  </Reports>
  <Parameters>
    <Parameter id="a5485539-67e0-4912-8255-8081a54efb3c" isSystem="True" name="Zielgruppe" reportParameterType_id="9ae252e1-18b4-4b71-982c-e27b2d3a5287" queryParameterName="@targetGroup_id" />
    <Parameter id="4aaf4f45-f70a-452a-92b5-f8e9b8b0dfb6" isSystem="True" name="Test" reportParameterType_id="c613c165-96af-465b-bf6c-c0cba9a2dc5d" queryParameterName="@test_id" />
  </Parameters>
  <ParameterTypes>
    <ParameterType id="9ae252e1-18b4-4b71-982c-e27b2d3a5287" isSystem="True" name="TargetGroup" datatype="TargetGroup" dataValueField="" dataTextField="" />
    <ParameterType id="c613c165-96af-465b-bf6c-c0cba9a2dc5d" isSystem="True" name="Test" datatype="Test" dataValueField="" dataTextField="" />
  </ParameterTypes>
</ReportsExport>