@@ -11,6 +11,7 @@ namespace DigitalLearningSolutions.Data.DataServices
1111 using System . Collections . Generic ;
1212 using System . Data ;
1313 using System . Linq ;
14+ using System . Threading . Tasks ;
1415
1516 public interface ICourseDataService
1617 {
@@ -96,6 +97,12 @@ int diagCompletionThreshold
9697
9798 IEnumerable < CourseDelegateForExport > GetDelegatesOnCourseForExport ( int customisationId , int centreId ) ;
9899
100+ int GetCourseDelegatesCountForExport ( string searchString , string sortBy , string sortDirection ,
101+ int customisationId , int centreId , bool ? isDelegateActive , bool ? isProgressLocked , bool ? removed , bool ? hasCompleted , string ? answer1 , string ? answer2 , string ? answer3 ) ;
102+
103+ Task < IEnumerable < CourseDelegateForExport > > GetCourseDelegatesForExport ( string searchString , int offSet , int itemsPerPage , string sortBy , string sortDirection ,
104+ int customisationId , int centreId , bool ? isDelegateActive , bool ? isProgressLocked , bool ? removed , bool ? hasCompleted , string ? answer1 , string ? answer2 , string ? answer3 ) ;
105+
99106 int EnrolOnActivitySelfAssessment ( int selfAssessmentId , int candidateId , int supervisorId , string adminEmail ,
100107 int selfAssessmentSupervisorRoleId , DateTime ? completeByDate , int delegateUserId , int centreId ) ;
101108
@@ -1358,6 +1365,192 @@ FROM DelegateAccounts AS da
13581365 ) ;
13591366 }
13601367
1368+ public int GetCourseDelegatesCountForExport ( string searchString , string sortBy , string sortDirection ,
1369+ int customisationId , int centreId , bool ? isDelegateActive , bool ? isProgressLocked , bool ? removed , bool ? hasCompleted , string ? answer1 , string ? answer2 , string ? answer3 )
1370+ {
1371+
1372+ var fromTableQuery = $@ " FROM Customisations cu WITH (NOLOCK)
1373+ INNER JOIN Applications AS ap WITH (NOLOCK) ON ap.ApplicationID = cu.ApplicationID
1374+ INNER JOIN Progress AS pr WITH (NOLOCK) ON pr.CustomisationID = cu.CustomisationID
1375+ LEFT OUTER JOIN AdminAccounts AS aaSupervisor WITH (NOLOCK) ON aaSupervisor.ID = pr.SupervisorAdminId
1376+ LEFT OUTER JOIN Users AS uSupervisor WITH (NOLOCK) ON uSupervisor.ID = aaSupervisor.UserID
1377+ LEFT OUTER JOIN AdminAccounts AS aaEnrolledBy WITH (NOLOCK) ON aaEnrolledBy.ID = pr.EnrolledByAdminID
1378+ LEFT OUTER JOIN Users AS uEnrolledBy WITH (NOLOCK) ON uEnrolledBy.ID = aaEnrolledBy.UserID
1379+ INNER JOIN DelegateAccounts AS da WITH (NOLOCK) ON da.ID = pr.CandidateID
1380+ INNER JOIN Users AS u WITH (NOLOCK) ON u.ID = da.UserID
1381+ LEFT JOIN UserCentreDetails AS ucd WITH (NOLOCK) ON ucd.UserID = da.UserID AND ucd.centreID = da.CentreID
1382+
1383+ WHERE (cu.CentreID = @centreId OR
1384+ (cu.AllCentres = 1 AND
1385+ EXISTS (SELECT CentreApplicationID
1386+ FROM CentreApplications cap
1387+ WHERE cap.ApplicationID = cu.ApplicationID AND
1388+ cap.CentreID = @centreID AND
1389+ cap.Active = 1)))
1390+ AND da.CentreID = @centreId
1391+ AND pr.CustomisationID = @customisationId
1392+ AND ap.DefaultContentTypeID <> 4
1393+
1394+ AND ( u.FirstName + ' ' + u.LastName + ' ' + COALESCE(ucd.Email, u.PrimaryEmail) + ' ' + COALESCE(CandidateNumber, '') LIKE N'%' + @searchString + N'%')
1395+ AND ((@isDelegateActive IS NULL) OR (@isDelegateActive = 1 AND (da.Active = 1)) OR (@isDelegateActive = 0 AND (da.Active = 0)))
1396+ AND ((@isProgressLocked IS NULL) OR (@isProgressLocked = 1 AND (pr.PLLocked = 1)) OR (@isProgressLocked = 0 AND (pr.PLLocked = 0)))
1397+ AND ((@removed IS NULL) OR (@removed = 1 AND (pr.RemovedDate IS NOT NULL)) OR (@removed = 0 AND (pr.RemovedDate IS NULL)))
1398+ AND ((@hasCompleted IS NULL) OR (@hasCompleted = 1 AND pr.Completed IS NOT NULL) OR (@hasCompleted = 0 AND pr.Completed IS NULL))
1399+
1400+ AND ((@answer1 IS NULL) OR ((@answer1 = 'No option selected' OR @answer1 = 'FREETEXTBLANKVALUE') AND (pr.Answer1 IS NULL OR TRIM(pr.Answer1) = ''))
1401+ OR (@answer1 = 'FREETEXTNOTBLANKVALUE' AND (pr.Answer1 IS NOT NULL OR pr.Answer1 = @answer1)))
1402+
1403+ AND ((@answer2 IS NULL) OR ((@answer2 = 'No option selected' OR @answer2 = 'FREETEXTBLANKVALUE') AND (pr.Answer2 IS NULL OR TRIM(pr.Answer2) = ''))
1404+ OR (@answer2 = 'FREETEXTNOTBLANKVALUE' AND (pr.Answer2 IS NOT NULL OR pr.Answer2 = @answer2)))
1405+
1406+ AND ((@answer3 IS NULL) OR ((@answer3 = 'No option selected' OR @answer3 = 'FREETEXTBLANKVALUE') AND (pr.Answer3 IS NULL OR TRIM(pr.Answer3) = ''))
1407+ OR (@answer3 = 'FREETEXTNOTBLANKVALUE' AND (pr.Answer3 IS NOT NULL OR pr.Answer3 = @answer3)))
1408+
1409+ AND COALESCE(ucd.Email, u.PrimaryEmail) LIKE '%_@_%.__%'" ;
1410+
1411+
1412+ var mainSql = "SELECT COUNT(*) AS TotalRecords " + fromTableQuery ;
1413+
1414+ return connection . ExecuteScalar < int > (
1415+ mainSql ,
1416+ new
1417+ {
1418+ searchString ,
1419+ sortBy ,
1420+ sortDirection ,
1421+ customisationId ,
1422+ centreId ,
1423+ isDelegateActive ,
1424+ isProgressLocked ,
1425+ removed ,
1426+ hasCompleted ,
1427+ answer1 ,
1428+ answer2 ,
1429+ answer3
1430+ } ,
1431+ commandTimeout : 3000
1432+ ) ;
1433+ }
1434+
1435+
1436+ public async Task < IEnumerable < CourseDelegateForExport > > GetCourseDelegatesForExport ( string searchString , int offSet , int itemsPerPage , string sortBy , string sortDirection ,
1437+ int customisationId , int centreId , bool ? isDelegateActive , bool ? isProgressLocked , bool ? removed , bool ? hasCompleted , string ? answer1 , string ? answer2 , string ? answer3 )
1438+ {
1439+
1440+ var selectColumnQuery = $@ "SELECT
1441+ ap.ApplicationName,
1442+ cu.CustomisationName,
1443+ da.ID AS DelegateId,
1444+ da.CandidateNumber,
1445+ u.FirstName AS DelegateFirstName,
1446+ u.LastName AS DelegateLastName,
1447+ COALESCE(ucd.Email, u.PrimaryEmail) AS DelegateEmail,
1448+ da.Active AS IsDelegateActive,
1449+ da.Answer1 AS RegistrationAnswer1,
1450+ da.Answer2 AS RegistrationAnswer2,
1451+ da.Answer3 AS RegistrationAnswer3,
1452+ da.Answer4 AS RegistrationAnswer4,
1453+ da.Answer5 AS RegistrationAnswer5,
1454+ da.Answer6 AS RegistrationAnswer6,
1455+ pr.ProgressID,
1456+ pr.PLLocked AS IsProgressLocked,
1457+ pr.SubmittedTime AS LastUpdated,
1458+ pr.FirstSubmittedTime AS Enrolled,
1459+ pr.CompleteByDate AS CompleteBy,
1460+ pr.RemovedDate,
1461+ pr.Completed,
1462+ pr.CustomisationId,
1463+ pr.LoginCount,
1464+ pr.Duration,
1465+ pr.DiagnosticScore,
1466+ pr.Answer1,
1467+ pr.Answer2,
1468+ pr.Answer3,
1469+ { DelegateAllAttemptsQuery } ,
1470+ { DelegateAttemptsPassedQuery } ,
1471+ { DelegatePassRateQuery } " ;
1472+
1473+ var fromTableQuery = $@ " FROM Customisations cu WITH (NOLOCK)
1474+ INNER JOIN Applications AS ap WITH (NOLOCK) ON ap.ApplicationID = cu.ApplicationID
1475+ INNER JOIN Progress AS pr WITH (NOLOCK) ON pr.CustomisationID = cu.CustomisationID
1476+ LEFT OUTER JOIN AdminAccounts AS aaSupervisor WITH (NOLOCK) ON aaSupervisor.ID = pr.SupervisorAdminId
1477+ LEFT OUTER JOIN Users AS uSupervisor WITH (NOLOCK) ON uSupervisor.ID = aaSupervisor.UserID
1478+ LEFT OUTER JOIN AdminAccounts AS aaEnrolledBy WITH (NOLOCK) ON aaEnrolledBy.ID = pr.EnrolledByAdminID
1479+ LEFT OUTER JOIN Users AS uEnrolledBy WITH (NOLOCK) ON uEnrolledBy.ID = aaEnrolledBy.UserID
1480+ INNER JOIN DelegateAccounts AS da WITH (NOLOCK) ON da.ID = pr.CandidateID
1481+ INNER JOIN Users AS u WITH (NOLOCK) ON u.ID = da.UserID
1482+ LEFT JOIN UserCentreDetails AS ucd WITH (NOLOCK) ON ucd.UserID = da.UserID AND ucd.centreID = da.CentreID
1483+
1484+ WHERE (cu.CentreID = @centreId OR
1485+ (cu.AllCentres = 1 AND
1486+ EXISTS (SELECT CentreApplicationID
1487+ FROM CentreApplications cap
1488+ WHERE cap.ApplicationID = cu.ApplicationID AND
1489+ cap.CentreID = @centreID AND
1490+ cap.Active = 1)))
1491+ AND da.CentreID = @centreId
1492+ AND pr.CustomisationID = @customisationId
1493+ AND ap.DefaultContentTypeID <> 4
1494+
1495+ AND ( u.FirstName + ' ' + u.LastName + ' ' + COALESCE(ucd.Email, u.PrimaryEmail) + ' ' + COALESCE(CandidateNumber, '') LIKE N'%' + @searchString + N'%')
1496+ AND ((@isDelegateActive IS NULL) OR (@isDelegateActive = 1 AND (da.Active = 1)) OR (@isDelegateActive = 0 AND (da.Active = 0)))
1497+ AND ((@isProgressLocked IS NULL) OR (@isProgressLocked = 1 AND (pr.PLLocked = 1)) OR (@isProgressLocked = 0 AND (pr.PLLocked = 0)))
1498+ AND ((@removed IS NULL) OR (@removed = 1 AND (pr.RemovedDate IS NOT NULL)) OR (@removed = 0 AND (pr.RemovedDate IS NULL)))
1499+ AND ((@hasCompleted IS NULL) OR (@hasCompleted = 1 AND pr.Completed IS NOT NULL) OR (@hasCompleted = 0 AND pr.Completed IS NULL))
1500+
1501+ AND ((@answer1 IS NULL) OR ((@answer1 = 'No option selected' OR @answer1 = 'FREETEXTBLANKVALUE') AND (pr.Answer1 IS NULL OR TRIM(pr.Answer1) = ''))
1502+ OR (@answer1 = 'FREETEXTNOTBLANKVALUE' AND (pr.Answer1 IS NOT NULL OR pr.Answer1 = @answer1)))
1503+
1504+ AND ((@answer2 IS NULL) OR ((@answer2 = 'No option selected' OR @answer2 = 'FREETEXTBLANKVALUE') AND (pr.Answer2 IS NULL OR TRIM(pr.Answer2) = ''))
1505+ OR (@answer2 = 'FREETEXTNOTBLANKVALUE' AND (pr.Answer2 IS NOT NULL OR pr.Answer2 = @answer2)))
1506+
1507+ AND ((@answer3 IS NULL) OR ((@answer3 = 'No option selected' OR @answer3 = 'FREETEXTBLANKVALUE') AND (pr.Answer3 IS NULL OR TRIM(pr.Answer3) = ''))
1508+ OR (@answer3 = 'FREETEXTNOTBLANKVALUE' AND (pr.Answer3 IS NOT NULL OR pr.Answer3 = @answer3)))
1509+
1510+ AND COALESCE(ucd.Email, u.PrimaryEmail) LIKE '%_@_%.__%'" ;
1511+
1512+ string orderBy ;
1513+ string sortOrder ;
1514+
1515+ if ( sortDirection == "Ascending" )
1516+ sortOrder = " ASC " ;
1517+ else
1518+ sortOrder = " DESC " ;
1519+
1520+ if ( sortBy == "SearchableName" || sortBy == "FullNameForSearchingSorting" )
1521+ orderBy = " ORDER BY LTRIM(u.LastName) " + sortOrder + ", LTRIM(u.FirstName) " ;
1522+ else
1523+ orderBy = " ORDER BY " + sortBy + sortOrder ;
1524+
1525+ orderBy += " OFFSET " + offSet + " ROWS FETCH NEXT " + itemsPerPage + " ROWS ONLY " ;
1526+
1527+
1528+ var mainSql = selectColumnQuery + fromTableQuery + orderBy ;
1529+
1530+ IEnumerable < CourseDelegateForExport > courseDelegates = connection . Query < CourseDelegateForExport > (
1531+ mainSql ,
1532+ new
1533+ {
1534+ searchString ,
1535+ sortBy ,
1536+ sortDirection ,
1537+ customisationId ,
1538+ centreId ,
1539+ isDelegateActive ,
1540+ isProgressLocked ,
1541+ removed ,
1542+ hasCompleted ,
1543+ answer1 ,
1544+ answer2 ,
1545+ answer3
1546+ } ,
1547+ commandTimeout : 3000
1548+ ) ;
1549+
1550+
1551+ return courseDelegates ;
1552+ }
1553+
13611554 public bool IsCourseCompleted ( int candidateId , int customisationId )
13621555 {
13631556 return connection . ExecuteScalar < bool > (
0 commit comments