11namespace DigitalLearningSolutions . Data . DataServices
22{
3+ using Dapper ;
4+ using DigitalLearningSolutions . Data . Helpers ;
5+ using DigitalLearningSolutions . Data . Models . DelegateGroups ;
36 using System ;
47 using System . Collections . Generic ;
58 using System . Data ;
69 using System . Linq ;
7- using Dapper ;
8- using DigitalLearningSolutions . Data . Models . Centres ;
9- using DigitalLearningSolutions . Data . Models . Courses ;
10- using DigitalLearningSolutions . Data . Models . DelegateGroups ;
1110
1211 public interface IGroupsDataService
1312 {
1413 IEnumerable < Group > GetGroupsForCentre ( int centreId ) ;
1514
15+ ( IEnumerable < Group > , int ) GetGroupsForCentre (
16+ string ? search ,
17+ int ? offset ,
18+ int ? rows ,
19+ string ? sortBy ,
20+ string ? sortDirection ,
21+ int ? centreId ,
22+ string ? filterAddedBy ,
23+ string ? filterLinkedField
24+ ) ;
25+
1626 IEnumerable < GroupDelegate > GetGroupDelegates ( int groupId ) ;
1727
1828 IEnumerable < GroupCourse > GetGroupCoursesVisibleToCentre ( int centreId ) ;
@@ -84,10 +94,13 @@ void AddDelegatesWithMatchingAnswersToGroup(
8494 public class GroupsDataService : IGroupsDataService
8595 {
8696 private const string CourseCountSql = @"SELECT COUNT(*)
87- FROM GroupCustomisations AS gc
88- JOIN Customisations AS c ON c.CustomisationID = gc.CustomisationID
89- INNER JOIN dbo.CentreApplications AS ca ON ca.ApplicationID = c.ApplicationID
90- INNER JOIN dbo.Applications AS ap ON ap.ApplicationID = ca.ApplicationID
97+ FROM GroupCustomisations AS gc WITH (NOLOCK)
98+ JOIN Customisations AS c WITH (NOLOCK)
99+ ON c.CustomisationID = gc.CustomisationID
100+ INNER JOIN dbo.CentreApplications AS ca WITH (NOLOCK)
101+ ON ca.ApplicationID = c.ApplicationID
102+ INNER JOIN dbo.Applications AS ap WITH (NOLOCK)
103+ ON ap.ApplicationID = ca.ApplicationID
91104 WHERE gc.GroupID = g.GroupID
92105 AND ca.CentreId = @centreId
93106 AND gc.InactivatedDate IS NULL
@@ -172,9 +185,11 @@ AND NOT EXISTS (SELECT * FROM GroupCustomisations AS GCInner
172185 END AS LinkedToFieldName,
173186 AddNewRegistrants,
174187 SyncFieldChanges
175- FROM Groups AS g
176- JOIN AdminUsers AS au ON au.AdminID = g.CreatedByAdminUserID
177- JOIN Centres AS c ON c.CentreID = g.CentreID
188+ FROM Groups AS g WITH (NOLOCK)
189+ JOIN AdminUsers AS au WITH (NOLOCK)
190+ ON au.AdminID = g.CreatedByAdminUserID
191+ JOIN Centres AS c WITH (NOLOCK)
192+ ON c.CentreID = g.CentreID
178193 WHERE RemovedDate IS NULL" ;
179194
180195 public GroupsDataService ( IDbConnection connection )
@@ -190,6 +205,72 @@ public IEnumerable<Group> GetGroupsForCentre(int centreId)
190205 ) ;
191206 }
192207
208+ public ( IEnumerable < Group > , int ) GetGroupsForCentre (
209+ string ? search = "" ,
210+ int ? offset = 0 ,
211+ int ? rows = 10 ,
212+ string ? sortBy = "" ,
213+ string ? sortDirection = "" ,
214+ int ? centreId = 0 ,
215+ string ? filterAddedBy = "" ,
216+ string ? filterLinkedField = "" )
217+ {
218+ if ( ! string . IsNullOrEmpty ( search ) )
219+ {
220+ search = search . Trim ( ) ;
221+ }
222+
223+ var rootSqlQuery = @$ "{ groupsSql } AND g.CentreId = @centreId";
224+
225+ var filtersClause = "" ;
226+ if ( ! string . IsNullOrEmpty ( filterAddedBy ) )
227+ {
228+ filtersClause += @$ "AND (g.CreatedByAdminUserID = " + filterAddedBy + ") " ;
229+ }
230+ if ( ! string . IsNullOrEmpty ( filterLinkedField ) )
231+ {
232+ filtersClause += @$ "AND (LinkedToField = " + filterLinkedField + ") " ;
233+ }
234+
235+ var searchClause = "AND(COALESCE(GroupLabel, '') LIKE N'%" + search + "%' OR COALESCE(GroupDescription, '') LIKE N'%" + search + "%')" ;
236+
237+ var sortOrder = sortDirection == "Ascending" ? " ASC " : " DESC " ;
238+
239+ if ( string . IsNullOrEmpty ( sortBy ) || sortBy == DefaultSortByOptions . Name . PropertyName )
240+ {
241+ sortBy = "GroupLabel" ;
242+ }
243+ var orderByClause = " ORDER BY " + sortBy + " " + sortOrder ;
244+
245+ var paginationClause = " OFFSET " + offset . ToString ( ) + " ROWS FETCH NEXT " + rows + " ROWS ONLY " ;
246+
247+ var groupsForCentreQuery = rootSqlQuery + " " + searchClause + " " + filtersClause + " " + orderByClause + " " + paginationClause ;
248+
249+ IEnumerable < Group > groups = connection . Query < Group > (
250+ groupsForCentreQuery ,
251+ new { centreId } ,
252+ commandTimeout : 3000
253+ ) ;
254+
255+ int resultCount = connection . ExecuteScalar < int > (
256+ @$ "SELECT COUNT(g.GroupID) AS Matches
257+ FROM Groups AS g WITH (NOLOCK)
258+ JOIN AdminUsers AS au WITH (NOLOCK)
259+ ON au.AdminID = g.CreatedByAdminUserID
260+ JOIN Centres AS c WITH (NOLOCK)
261+ ON c.CentreID = g.CentreID
262+ WHERE RemovedDate IS NULL
263+ AND g.CentreId = @centreId
264+ AND (COALESCE(GroupLabel, '') LIKE N'%' + @search + N'%'
265+ OR COALESCE(GroupDescription, '') LIKE N'%' + @search + N'%')"
266+ + filtersClause ,
267+ new { centreId , search } ,
268+ commandTimeout : 3000
269+ ) ;
270+
271+ return ( groups , resultCount ) ;
272+ }
273+
193274 public IEnumerable < GroupDelegate > GetGroupDelegates ( int groupId )
194275 {
195276 return connection . Query < GroupDelegate > (
@@ -420,25 +501,9 @@ int centreId
420501 {
421502 return connection . QuerySingle < int > (
422503 @"GroupCustomisation_Add_V2" ,
423- new { groupId , customisationId , centreId , completeWithinMonths , adminUserID = addedByAdminUserId , cohortLearners , supervisorAdminId = supervisorAdminId ?? 0 } ,
504+ new { groupId , customisationId , centreId , completeWithinMonths , adminUserID = addedByAdminUserId , cohortLearners , supervisorAdminId = supervisorAdminId ?? 0 } ,
424505 commandType : CommandType . StoredProcedure
425506 ) ;
426- //return connection.QuerySingle<int>(
427- // @"INSERT INTO GroupCustomisations
428- // (GroupID, CustomisationID, CompleteWithinMonths, AddedByAdminUserID, CohortLearners, SupervisorAdminID)
429- // OUTPUT Inserted.GroupCustomisationId
430- // VALUES
431- // (@groupId, @customisationId, @completeWithinMonths, @addedByAdminUserId, @cohortLearners, @supervisorAdminID)",
432- // new
433- // {
434- // groupId,
435- // customisationId,
436- // completeWithinMonths,
437- // addedByAdminUserId,
438- // cohortLearners,
439- // supervisorAdminId,
440- // }
441- //);
442507 }
443508
444509 public void AddDelegatesWithMatchingAnswersToGroup (
0 commit comments