@@ -356,3 +356,89 @@ func (db *DB) GetAdminDashboardInfo(facilityID uint) (models.AdminDashboardJoin,
356
356
357
357
return dashboard , nil
358
358
}
359
+ func (db * DB ) GetTotalCoursesOffered (facilityID * uint ) (int , error ) {
360
+ var totalCourses int
361
+ subQry := db .Table ("courses c" ).
362
+ Select ("COUNT(DISTINCT c.id) as total_courses_offered" ).
363
+ Joins ("INNER JOIN user_enrollments ue on c.id = ue.course_id" ).
364
+ Joins ("INNER JOIN users u on ue.user_id = u.id" )
365
+
366
+ if facilityID != nil {
367
+ subQry = subQry .Where ("u.facility_id = ?" , facilityID )
368
+ }
369
+
370
+ err := subQry .Find (& totalCourses ).Error
371
+ if err != nil {
372
+ return 0 , NewDBError (err , "error getting total courses offered" )
373
+ }
374
+ return totalCourses , nil
375
+ }
376
+
377
+ func (db * DB ) GetTotalStudentsEnrolled (facilityID * uint ) (int , error ) {
378
+ var totalStudents int
379
+ query := db .Table ("user_enrollments ue" ).
380
+ Select ("COUNT(DISTINCT ue.user_id) AS students_enrolled" ).
381
+ Joins ("INNER JOIN users u on ue.user_id = u.id" )
382
+
383
+ if facilityID != nil {
384
+ query = query .Where ("u.facility_id = ?" , facilityID )
385
+ }
386
+
387
+ err := query .Scan (& totalStudents ).Error
388
+ if err != nil {
389
+ return 0 , NewDBError (err , "error getting total students enrolled" )
390
+ }
391
+ return totalStudents , nil
392
+ }
393
+
394
+ func (db * DB ) GetTotalHourlyActivity (facilityID * uint ) (int , error ) {
395
+ var totalActivity int
396
+ subQry := db .Table ("users u" ).
397
+ Select ("CASE WHEN SUM(a.total_time) IS NULL THEN 0 ELSE ROUND(SUM(a.total_time)/3600, 0) END AS total_time" ).
398
+ Joins ("LEFT JOIN activities a ON u.id = a.user_id" ).
399
+ Where ("u.role = ?" , "student" )
400
+
401
+ if facilityID != nil {
402
+ subQry = subQry .Where ("u.facility_id = ?" , facilityID )
403
+ }
404
+
405
+ err := subQry .Scan (& totalActivity ).Error
406
+ if err != nil {
407
+ return 0 , NewDBError (err , "error getting total hourly activity" )
408
+ }
409
+ return totalActivity , nil
410
+ }
411
+
412
+ func (db * DB ) GetLearningInsights (facilityID * uint ) ([]models.LearningInsight , error ) {
413
+ var insights []models.LearningInsight
414
+ subQry := db .Table ("outcomes o" ).
415
+ Select ("o.course_id, COUNT(o.id) AS outcome_count" ).
416
+ Group ("o.course_id" )
417
+
418
+ subQry2 := db .Table ("courses c" ).
419
+ Select (`
420
+ c.name AS course_name,
421
+ COUNT(DISTINCT u.id) AS total_students_enrolled,
422
+ CASE
423
+ WHEN MAX(subqry.outcome_count) > 0 THEN
424
+ COUNT(DISTINCT u.id) / NULLIF(CAST(MAX(c.total_progress_milestones) AS float), 0) * 100.0
425
+ ELSE 0
426
+ END AS completion_rate,
427
+ COALESCE(ROUND(SUM(a.total_time) / 3600, 0), 0) AS activity_hours
428
+ ` ).
429
+ Joins ("LEFT JOIN milestones m ON m.course_id = c.id" ).
430
+ Joins ("LEFT JOIN users u ON m.user_id = u.id" ).
431
+ Joins ("LEFT JOIN activities a ON u.id = a.user_id" ).
432
+ Joins ("INNER JOIN (?) AS subqry ON m.course_id = subqry.course_id" , subQry ).
433
+ Where ("u.role = ?" , "student" )
434
+
435
+ if facilityID != nil {
436
+ subQry2 = subQry2 .Where ("u.facility_id = ?" , facilityID )
437
+ }
438
+
439
+ err := subQry2 .Group ("c.name, c.total_progress_milestones" ).Find (& insights ).Error
440
+ if err != nil {
441
+ return nil , NewDBError (err , "error getting learning insights" )
442
+ }
443
+ return insights , nil
444
+ }
0 commit comments