﻿<?xml version="1.0" encoding="utf-8"?>
<ReportsExport>
  <Reports>
    <Report id="9a55697f-ec3e-4f15-aad8-24b56f7c6e1e" codekey="ERGO_DigiCheck" categoryCodekey="LearningPrograms" name="Stage 1 results for the DigiCheck WBT" description="">
      <MetaData created="2018-08-30T12:39:33" createdBy="Rolle Administrator (Administrator)" createdBy_user_id="12" modified="2018-08-30T16:57:34" modifiedBy="Rolle Administrator" />
      <ExecutionDetails format="TableResult" commandType="SqlCommandOrQuery" exportHandler="" adminControl="" exportMultipleTablesToSheets="False" datesWithTime="False" extraParams="" />
      <Mandators mandatorMode="OnlyOwner" mandator_id="96ba2868-8baf-4e69-b1fb-d2cc6d6832e8" mandatorName="ERGO_e-Campus" isStandard="False" isUsedByMenu="False" />
      <Parameters>
        <Parameter id="93460158-2520-48a7-ac64-9350fa3d159f" isRequired="True" allowMultiSelect="True" name="Zielgruppen Multiselektion" contextName="Zielgruppen Multiselektion" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="030eafc1-6d2e-45d2-97fa-d19e413b3fec" isRequired="True" allowMultiSelect="False" name="Theme" contextName="Theme" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
      </Parameters>
      <Roles>
        <Role id="55" />
        <Role id="80" />
        <Role id="90" />
      </Roles>
      <command>
        -- Start DEBUG
        --DECLARE @targetGroupId dbo.typeIntList;
        --INSERT INTO
        --    @targetGroupId (value)
        --SELECT
        --    id
        --FROM
        --    tblTargetGroups
        --WHERE
        --    title IN  ('DigiCheck ZG')

        --DECLARE @theme_id UNIQUEIDENTIFIER = '561D0BC9-40C7-4744-A974-3013E19553EA'
        -- End   DEBUG


        -- Setup the target values for each persona and dimension, taken from the Excel file and
        -- calculated to two decimal places in case they want a bit more precision later
        DECLARE @TargetValues TABLE
        (
        dimension NVARCHAR(50),
        persona INT,
        targetValue DECIMAL(4,2)
        )
        INSERT INTO
        @TargetValues (persona, dimension, targetValue)
        VALUES
        -- NINA
        (0, 'social', 3.5),
        (0, 'agile', 2.8),
        (0, 'innovative', 3),
        (0, 'technological', 3.5),
        (0, 'analytical', 3),
        (0, 'resilient', 3.66),
        -- Brigitte
        (1, 'social', 3.5),
        (1, 'agile', 2.8),
        (1, 'innovative', 3),
        (1, 'technological', 3.5),
        (1, 'analytical', 3),
        (1, 'resilient', 3.66),
        -- Michael
        (2, 'social', 3.75),
        (2, 'agile', 3),
        (2, 'innovative', 3.5),
        (2, 'technological', 3.5),
        (2, 'analytical', 3.5),
        (2, 'resilient', 3.66),
        -- Rolf
        (3, 'social', 3.71),
        (3, 'agile', 4.57),
        (3, 'innovative', 4),
        (3, 'technological', 4),
        (3, 'analytical', 3.5),
        (3, 'resilient', 4.75),
        -- Mara
        (4, 'social', 4),
        (4, 'agile', 4),
        (4, 'innovative', 4),
        (4, 'technological', 3.66),
        (4, 'analytical', 4),
        (4, 'resilient', 4),
        -- Tobias
        (5, 'social', 4.14),
        (5, 'agile', 4.71),
        (5, 'innovative', 4.25),
        (5, 'technological', 4),
        (5, 'analytical', 4.5),
        (5, 'resilient', 4.75)

        -- Extract the WBT data we're interested in
        DECLARE @UserData TABLE
        (
        userId INT,
        dataName NVARCHAR(50),
        dataValue VARCHAR(4096)
        );

        ;WITH TargetGroupUsers
        AS
        (
        SELECT DISTINCT
        utg.UserCn as userId
        FROM
        tblUsersTargetGroups utg
        INNER JOIN @targetGroupId tg
        ON  tg.value = utg.targetGroup_id
        INNER JOIN v_Users u
        ON  u.intUserCn = utg.UserCn
        )
        INSERT INTO
        @UserData (userId, dataName, dataValue)
        SELECT
        tgu.userId, sd.Name, sd.Data
        FROM
        TargetGroupUsers tgu
        INNER JOIN tblSCORMData sd
        ON  sd.UserID = tgu.userId

        WHERE
        sd.theme_id = @theme_id
        AND
        (
        sd.Name like 'cmi.objectives.%'
        OR
        sd.Name like 'cmi.interactions.%'
        )

        -- Start massaging and combining the raw WBT data into something we can use
        DECLARE @UserDataGroupings TABLE
        (
        userId INT,
        groupName NVARCHAR(50),
        groupValue VARCHAR(4096),
        groupValueDecimal DECIMAL(4,1),
        isDimension BIT
        )

        -- Both the dimension scores and the persona are
        INSERT INTO
        @UserDataGroupings (userId, groupName, groupValue, groupValueDecimal, isDimension)
        SELECT
        ud.userId, ud.dataValue, ud2.dataValue, TRY_PARSE(ud2.dataValue AS DECIMAL(4,1)), 0
        FROM
        @UserData ud
        INNER JOIN @UserData ud2
        ON  ud2.userId = ud.userId
        AND ud2.dataName = REPLACE(ud.dataName, '.id', '.score.raw')
        WHERE
        ud.dataName like 'cmi.objectives.%.id'

        -- Adjust the dimension answer score scale to 1 to 5 from its 20 to 100 value
        UPDATE
        @UserDataGroupings
        SET
        groupValueDecimal = ROUND(groupValueDecimal / 20, 1),
        groupValue = FORMAT(ROUND(groupValueDecimal / 20, 1), 'N1', 'en-us'),
        isDimension = 1
        WHERE
        groupName != 'persona'

        -- Add the learning recommendations
        INSERT INTO
        @UserDataGroupings (userId, groupName, groupValue, isDimension)
        SELECT
        ud.userId, 'lernempfehlungen', REPLACE(LTRIM(RTRIM(ud2.dataValue)), ' ', ', '), 0
        FROM
        @UserData ud
        INNER JOIN @UserData ud2
        ON  ud2.userId = ud.userId
        AND ud2.dataName = REPLACE(ud.dataName, '.id', '.student_response')
        WHERE
        ud.dataName LIKE 'cmi.interactions.%.id'
        AND ud.dataValue = 'recommendation'

        -- Add the target values for each user and dimension
        INSERT INTO
        @UserDataGroupings (userId, groupName, groupValue, groupValueDecimal)
        SELECT
        userPersona.userId, tv.dimension + '_target', FORMAT(ROUND(tv.targetValue, 1), 'N1', 'en-us'), ROUND(tv.targetValue, 1)
        FROM
        (
        SELECT
        userId, groupValue as persona
        FROM
        @UserDataGroupings udg
        WHERE
        groupName = 'persona'
        ) AS userPersona
        INNER JOIN @TargetValues tv
        ON  tv.persona = userPersona.persona

        -- Add the delta values for each user and dimension
        INSERT INTO
        @UserDataGroupings (userId, groupName, groupValue)
        SELECT
        udgActual.userId,
        udgActual.groupName + '_diff',
        FORMAT((ABS(udgTarget.groupValueDecimal - udgActual.groupValueDecimal) + (udgTarget.groupValueDecimal - udgActual.groupValueDecimal))/2, 'N1', 'en-us')
        FROM
        @UserDataGroupings as udgActual
        INNER JOIN @UserDataGroupings as udgTarget
        ON  udgTarget.userId = udgActual.userId
        AND udgTarget.groupName = udgActual.groupName + '_target'
        WHERE
        udgActual.isDimension = 1


        -- Finally select and pivot the results
        SELECT
        ROW_NUMBER() OVER (ORDER by persona) AS Nutzer,
        persona,
        social_target,
        social,
        social_diff,
        agile_target,
        agile,
        agile_diff,
        innovative_target,
        innovative,
        innovative_diff,
        technological_target,
        technological,
        technological_diff,
        analytical_target,
        analytical,
        analytical_diff,
        resilient_target,
        resilient,
        resilient_diff,
        lernempfehlungen

        FROM
        (
        SELECT
        u.descUserCn as Username, udg.groupName, udg.groupValue
        FROM @UserDataGroupings udg
        INNER JOIN tableUsers u
        ON u.intUserCn = udg.userId
        ) AS filter
        PIVOT
        (
        MAX(groupValue)
        FOR groupName IN
        (
        persona,
        social_target,
        social,
        social_diff,
        agile_target,
        agile,
        agile_diff,
        innovative_target,
        innovative,
        innovative_diff,
        technological_target,
        technological,
        technological_diff,
        analytical_target,
        analytical,
        analytical_diff,
        resilient_target,
        resilient,
        resilient_diff,
        lernempfehlungen
        )
        ) as pivotTable
      </command>
    </Report>
  </Reports>
  <Parameters>
    <Parameter id="93460158-2520-48a7-ac64-9350fa3d159f" isSystem="True" name="Zielgruppen Multiselektion" reportParameterType_id="4123c2c2-d408-476a-82bf-ca77f9ecf944" queryParameterName="@targetGroupId" />
    <Parameter id="030eafc1-6d2e-45d2-97fa-d19e413b3fec" isSystem="False" name="Theme Fast" reportParameterType_id="ee94dc4a-25f0-49c3-a1f9-74b8afae61cc" queryParameterName="@theme_id" />
  </Parameters>
  <ParameterTypes>
    <ParameterType id="4123c2c2-d408-476a-82bf-ca77f9ecf944" isSystem="False" name="TargetGroup" 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
        JOIN v_Users ON v_Users.intUserCn = @current_user_id
        WHERE tblFunctions.shortcut = 'FcnIsAuthorizedForAllTGs' AND tblRoles.id = v_Users.SecurityID))
        AND defaultforlibrary = 0
        ORDER BY title
      </query>
    </ParameterType>
    <ParameterType id="ee94dc4a-25f0-49c3-a1f9-74b8afae61cc" isSystem="False" name="Theme Fast Type" datatype="GuidDDL" dataValueField="id" dataTextField="title">
      <query>
-- Start DEBUG
    --declare @current_mandator_id UNIQUEIDENTIFIER;
    --declare @current_user_id INT;
        
    --SELECT  @current_user_id = u.intUserCn,
    --    @current_mandator_id = u.mandator_id
    --FROM
    --    v_Users u
    --    inner join tblMandators m
    --        on m.id = u.mandator_id
    --WHERE
    --    u.descuserCn = 'Administrator'
    --    AND m.name = 'ERGO_e-campus'
-- End DEBUG

DECLARE @hasFullItemRight BIT = 0;

DECLARE @currentUserOUs typeGuidList
INSERT INTO @currentUserOUs
SELECT * FROM dbo.fn_GetUserOrganisationUnits(@current_user_id)

IF
-- Platform admin
EXISTS (SELECT * FROM v_Users WHERE intUserCn = @current_user_id AND SecurityID = 100)
OR
-- Is authorised for all items
EXISTS
(
    SELECT
        *
    FROM
        tblFunctions f
        JOIN tblRolesFunctions rf
            ON rf.function_id = f.id
        JOIN v_Users u
            ON u.SecurityID = rf.role_id
    WHERE
        rf.mandator_id = @current_mandator_id
        AND f.shortcut = 'FcnIsAuthorizedForAllItems'
)
BEGIN
    SET @hasFullItemRight = 1;
END


SELECT
    t.id, i.title
FROM
    tblThemes t
    JOIN tblItems i
        ON i.id = t.id
WHERE
    i.deleted IS NULL
    AND i.mandator_id = @current_mandator_id
    AND
    (
        @hasFullItemRight = 1 /* user has the right for editing all items FcnIsAuthorizedForAllItems*/
        OR
        i.right_global &gt; 0 /* at least readable for the user*/
        OR
        i.owner_id = @current_user_id /* getting the items where the user is the owner*/
        OR
        EXISTS
        (
            /* At least read OU permissions */
            SELECT
                1
            FROM
                tblItemsOUPermissions 
                JOIN tblUsersOrganisationUnits
                    ON tblUsersOrganisationUnits.organisationUnit_id = tblItemsOUPermissions.organisationUnit_id
                    AND tblItemsOUPermissions.item_id = i.id
                    AND tblUsersOrganisationUnits.user_id = @current_user_id
            WHERE
                permission &gt; 0
        )
    )
ORDER BY
    i.title
      </query>
    </ParameterType>
  </ParameterTypes>
</ReportsExport>