You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: WebAPI/LearningHub.Nhs.Database/Stored Procedures/Resources/GetAchievedCertificatedResourcesWithOptionalPagination.sql
DECLARE @MaxRows INT = @MaxPageNUmber * @FetchRows
26
+
DECLARE @OffsetRows INT = (@PageNumber - 1) * @FetchRows
27
+
28
+
DECLARE @MyActivity TABLE (ResourceId [int] NOT NULL PRIMARY KEY, ResourceActivityId [int] NOT NULL);
29
+
DECLARE @Resources TABLE (ResourceId [int] NOT NULL PRIMARY KEY, ResourceActivityCount [int] NOT NULL);
30
+
31
+
INSERT INTO @MyActivity
32
+
SELECT TOP (@MaxRows) ra.ResourceId, MAX(ra.Id) ResourceActivityId
33
+
FROM
34
+
activity.ResourceActivity ra
35
+
JOIN [resources].[Resource] r ON ra.ResourceId = r.Id
36
+
JOIN [resources].[ResourceVersion] rv ON rv.Id = ra.ResourceVersionId
37
+
LEFT JOIN [resources].[AssessmentResourceVersion] arv ON arv.ResourceVersionId = ra.ResourceVersionId
38
+
LEFT JOIN [activity].[AssessmentResourceActivity] ara ON ara.ResourceActivityId = ra.Id
39
+
LEFT JOIN [activity].[MediaResourceActivity] mar ON mar.ResourceActivityId = ra.Id
40
+
LEFT JOIN [activity].[ScormActivity] sa ON sa.ResourceActivityId = ra.Id
41
+
WHERE ra.UserId = @UserId AND rv.CertificateEnabled = 1
42
+
AND (
43
+
(r.ResourceTypeId IN (2, 7) AND ra.ActivityStatusId = 3 OR ra.ActivityStart < '2020-09-07 00:00:00 +00:00' OR mar.Id IS NOT NULL AND mar.PercentComplete = 100)
44
+
OR (r.ResourceTypeId = 6 AND (sa.CmiCoreLesson_status IN(3,5) OR (ra.ActivityStatusId IN(3, 5))))
45
+
OR ((r.ResourceTypeId = 11 AND arv.AssessmentType = 2) AND (ara.Score >= arv.PassMark OR ra.ActivityStatusId IN(3, 5)))
46
+
OR ((r.ResourceTypeId = 11 AND arv.AssessmentType =1) AND (ara.Score >= arv.PassMark AND ra.ActivityStatusId IN(3, 5,7)))
47
+
OR (r.ResourceTypeId IN (1, 5, 8, 9, 10, 12) AND ra.ActivityStatusId = 3))
48
+
GROUP BY ra.ResourceId
49
+
ORDER BY ResourceActivityId DESC
50
+
51
+
SELECT r.Id AS ResourceId
52
+
,( SELECT TOP 1 rr.OriginalResourceReferenceId
53
+
FROM [resources].[ResourceReference] rr
54
+
JOIN hierarchy.NodePath np on np.id = rr.NodePathId and np.NodeId = n.Id and np.Deleted = 0
55
+
WHERE rr.ResourceId = rv.ResourceId AND rr.Deleted = 0
56
+
) AS ResourceReferenceID
57
+
,r.CurrentResourceVersionId AS ResourceVersionId
58
+
,r.ResourceTypeId AS ResourceTypeId
59
+
,rv.Title
60
+
,rv.Description
61
+
,CASE
62
+
WHEN r.ResourceTypeId = 7 THEN
63
+
(SELECT vrv.DurationInMilliseconds from [resources].[VideoResourceVersion] vrv WHERE vrv.[ResourceVersionId] = r.CurrentResourceVersionId)
64
+
WHEN r.ResourceTypeId = 2 THEN
65
+
(SELECT vrv.DurationInMilliseconds from [resources].[AudioResourceVersion] vrv WHERE vrv.[ResourceVersionId] = r.CurrentResourceVersionId)
66
+
ELSE
67
+
NULL
68
+
END AS DurationInMilliseconds
69
+
,CASE WHEN n.id = 1 THEN NULL ELSE cnv.Name END AS CatalogueName
70
+
,cnv.Url AS Url
71
+
,CASE WHEN n.id = 1 THEN NULL ELSE cnv.BadgeUrl END AS BadgeUrl
72
+
,cnv.RestrictedAccess
73
+
,CAST(CASE WHEN cnv.RestrictedAccess = 1 AND auth.CatalogueNodeId IS NULL THEN 0 ELSE 1 END AS bit) AS HasAccess
74
+
,ub.Id AS BookMarkId
75
+
,CAST(ISNULL(ub.[Deleted], 1) ^ 1 AS BIT) AS IsBookmarked
76
+
,rvrs.AverageRating
77
+
,rvrs.RatingCount
78
+
FROM @MyActivity ma
79
+
JOIN activity.ResourceActivity ra ON ra.id = ma.ResourceActivityId
80
+
JOIN resources.resourceversion rv ON rv.id = ra.ResourceVersionId AND rv.Deleted = 0
81
+
JOIN Resources.Resource r ON r.Id = rv.ResourceId
82
+
JOIN hierarchy.Publication p ON rv.PublicationId = p.Id AND p.Deleted = 0
83
+
JOIN resources.ResourceVersionRatingSummary rvrs ON rv.Id = rvrs.ResourceVersionId AND rvrs.Deleted = 0
84
+
JOIN hierarchy.NodeResource nr ON r.Id = nr.ResourceId AND nr.Deleted = 0
85
+
JOIN hierarchy.Node n ON n.Id = nr.NodeId AND n.Hidden = 0 AND n.Deleted = 0
86
+
JOIN hierarchy.NodePath np ON np.NodeId = n.Id AND np.Deleted = 0 AND np.IsActive = 1
87
+
JOIN hierarchy.NodeVersion nv ON nv.NodeId = np.CatalogueNodeId AND nv.VersionStatusId = 2 AND nv.Deleted = 0
88
+
JOIN hierarchy.CatalogueNodeVersion cnv ON cnv.NodeVersionId = nv.Id AND cnv.Deleted = 0
89
+
LEFT JOIN hub.UserBookmark ub ON ub.UserId = @UserId AND ub.ResourceReferenceId = (SELECT TOP 1 rr.OriginalResourceReferenceId
90
+
FROM [resources].[ResourceReference] rr
91
+
JOIN hierarchy.NodePath np on np.id = rr.NodePathId and np.NodeId = n.Id and np.Deleted = 0
92
+
WHERE rr.ResourceId = rv.ResourceId AND rr.Deleted = 0)
93
+
LEFT JOIN ( SELECT DISTINCT CatalogueNodeId
94
+
FROM [hub].[RoleUserGroupView] rug JOIN hub.UserUserGroup uug ON rug.UserGroupId = uug.UserGroupId
95
+
WHERE rug.ScopeTypeId = 1 and rug.RoleId in (1,2,3) and uug.Deleted = 0 and uug.UserId = @userId) auth ON n.Id = auth.CatalogueNodeId
96
+
ORDER BY ma.ResourceActivityId DESC, rv.Title
97
+
OFFSET @OffsetRows ROWS
98
+
FETCH NEXT @FetchRows ROWS ONLY
99
+
100
+
SELECT @TotalRecords = CASE WHEN COUNT(*) > 12 THEN @MaxRows ELSE COUNT(*) END FROM @MyActivity
0 commit comments