HRM-10/HRM.DA/Service/LearningManagement/CoursesService.cs

593 lines
16 KiB
C#
Raw Permalink Normal View History

2026-11-03 13:06:00 +06:00
using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using HRM.BO;
using HRM.DA;
using Ease.Core.Model;
using Ease.Core.DataAccess;
using Ease.Core.Utility;
using Ease.Core;
using NPOI.SS.Formula.Functions;
using System.Net.Mail;
using Microsoft.AspNetCore.Http.HttpResults;
namespace HRM.DA
{
public class CoursesService : ServiceTemplate, ICoursesService
{
public CoursesService() { }
private void MapObject(Courses oCourses, DataReader oReader)
{
base.SetObjectID(oCourses, oReader.GetInt32("CourseID").Value);
oCourses.CourseTitle = oReader.GetString("CourseTitle");
oCourses.CourseCode = oReader.GetString("CourseCode");
oCourses.CourseTypeID = oReader.GetInt32("CourseTypeID").Value;
oCourses.Description = oReader.GetString("Description","");
oCourses.DifficultyLevel = (EnumDifficultyLevel)oReader.GetInt32("DifficultyLevel").Value;
oCourses.TrainerID = oReader.GetInt32("TrainerID").Value;
oCourses.Duration = oReader.GetString("Duration","");
oCourses.IsForFree = oReader.GetBoolean("IsForFree", false);
oCourses.Price = oReader.GetDouble("Price", 0.0);
oCourses.TotalEnrollments = oReader.GetInt32("TotalEnrollments");
oCourses.Rating = oReader.GetDecimal("Rating",0);
oCourses.PopularityScore = oReader.GetFloat("PopularityScore", 0);
oCourses.CompletionRate = oReader.GetFloat("CompletionRate", 0);
oCourses.AverageProgress = oReader.GetFloat("AverageProgress", 0);
oCourses.IsExamRequired = oReader.GetBoolean("IsExamRequired", false);
oCourses.Tags = oReader.GetString("Tags");
oCourses.TargetedAudience = oReader.GetString("TargetedAudience");
oCourses.LearningOutcomes = oReader.GetString("LearningOutcomes");
oCourses.CourseIncludes = oReader.GetString("CourseIncludes");
oCourses.StartGuideLine = oReader.GetString("StartGuideLine");
oCourses.Status = (EnumStatus)oReader.GetInt32("Status").Value;
oCourses.CreatedBy = oReader.GetInt32("CreatedBy", 0);
oCourses.CreatedDate = oReader.GetDateTime("CreationDate").Value;
oCourses.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
oCourses.ModifiedDate = oReader.GetDateTime("ModifiedDate");
this.SetObjectState(oCourses, ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
Courses oCourses = new Courses();
MapObject(oCourses, oReader);
return oCourses as T;
}
protected Courses CreateObject(DataReader oReader)
{
Courses oCourses = new Courses();
MapObject(oCourses, oReader);
return oCourses;
}
#region Save
public int Save(Courses oCourses)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oCourses.IsNew)
{
int ID = tc.GenerateID("LMS_Courses", "CourseID");
base.SetObjectID(oCourses, ID);
CoursesDA.Insert(tc, oCourses);
}
else
{
CoursesDA.Update(tc, oCourses);
}
#region For Template
oCourses.CourseTemplate.ObjectID = oCourses.ID;
string[] items = oCourses.CourseTemplate.FileData.Split(new char[] { ',', ' ' }, StringSplitOptions.None);
byte[] newBytes;
if (items.Length == 1)
{
newBytes = Convert.FromBase64String(items[0]);
LMSTemplateDA.Delete(tc, oCourses.CourseTemplate.ObjectID);
}
else
{
newBytes = Convert.FromBase64String(items[1]);
}
oCourses.CourseTemplate.FileAsByteArray = newBytes;
LMSTemplateDA.SaveLMSTemplate(oCourses.CourseTemplate);
#endregion
#region For Lessons & LessonsContent
foreach (Lessons oLessons in oCourses.Lessons)
{
if (oLessons.IsNew == true)
{
oLessons.CourseID = oCourses.ID;
oLessons.CreatedBy = oCourses.CreatedBy;
oLessons.CreatedDate = oCourses.CreatedDate;
}
else
{
oLessons.ModifiedBy = oCourses.ModifiedBy;
oLessons.ModifiedDate = oCourses.ModifiedDate;
}
new LessonsService().Save(tc, oLessons);
}
#endregion
tc.End();
return oCourses.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
#endregion
#region Delete
public void Delete(int ID)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
CoursesDA.Delete(tc, ID);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
#endregion
#region Update Status
public void UpdateStatus(Courses oCourses)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oCourses.IsNew != true)
{
CoursesDA.UpdateStatus(tc, oCourses);
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
#endregion
#region Update Status
public void UpdateTotalEnrollments(int courseID)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
CoursesDA.UpdateTotalEnrollments(tc, courseID);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
#endregion
#region GET
public Courses Get(int ID)
{
Courses oCourses = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(CoursesDA.Get(tc, ID));
if (oreader.Read())
{
oCourses = this.CreateObject<Courses>(oreader);
}
oreader.Close();
if (oCourses != null)
{
this.LoadCourseAllData(tc, oCourses);
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oCourses;
}
public Courses GetCourseForUser(int ID, int userID)
{
Courses oCourses = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(CoursesDA.Get(tc, ID));
if (oreader.Read())
{
oCourses = this.CreateObject<Courses>(oreader);
}
oreader.Close();
if (oCourses != null)
{
this.LoadCourseAllData(tc, oCourses);
this.LoadUserCoursesProgress(tc, oCourses, userID);
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oCourses;
}
public Courses GetForEnrollment(int ID, int userID)
{
Courses oCourses = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(CoursesDA.Get(tc, ID));
if (oreader.Read())
{
oCourses = this.CreateObject<Courses>(oreader);
}
oreader.Close();
if (oCourses != null)
{
this.LoadCourseAllDataForEnrollment(tc, oCourses, userID);
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oCourses;
}
public Courses UserCourseProgress(Courses item)
{
Courses oCourses = item;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
if (oCourses != null)
{
this.LoadCourseAllDataForCourseUpdate(tc, oCourses);
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oCourses;
}
public List<Courses> Get(EnumStatus status)
{
List<Courses> oCourses = new List<Courses>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(CoursesDA.Get(tc, status));
oCourses = this.CreateObjects<Courses>(dr);
dr.Close();
if (oCourses.Count > 0)
{
foreach (Courses oCourse in oCourses)
{
oCourse.CourseType = new CourseTypeService().Get(tc, oCourse.CourseTypeID);
oCourse.Trainer = new EmployeeService().Get(tc, oCourse.TrainerID);
oCourse.TotalLesson = CoursesDA.GetTotalLesson(tc, oCourse.ID);
oCourse.TotalContent = CoursesDA.GetTotalContent(tc, oCourse.ID);
oCourse.CourseTemplate = new LMSTemplateService().GetCourseTemplate(tc, oCourse.ID);
}
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oCourses;
}
public List<Courses> Get(EnumStatus status, int userID)
{
List<Courses> oCourses = new List<Courses>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(CoursesDA.Get(tc, status));
oCourses = this.CreateObjects<Courses>(dr);
dr.Close();
if (oCourses.Count > 0)
{
foreach (Courses oCourse in oCourses)
{
oCourse.CourseType = new CourseTypeService().Get(tc, oCourse.CourseTypeID);
oCourse.Trainer = new EmployeeService().Get(tc, oCourse.TrainerID);
oCourse.TotalLesson = CoursesDA.GetTotalLesson(tc, oCourse.ID);
oCourse.TotalContent = CoursesDA.GetTotalContent(tc, oCourse.ID);
oCourse.CourseTemplate = new LMSTemplateService().GetCourseTemplate(tc, oCourse.ID);
this.LoadUserCoursesProgress(tc, oCourse, userID);
}
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oCourses;
}
public void LoadUserCoursesProgress(TransactionContext tc, Courses oCourse, int userID)
{
oCourse.Enrollments = new EnrollmentService().GetForEnrollement(tc, oCourse.ID, userID);
if (oCourse.Enrollments != null)
{
CourseProgres oCourseProgres = new CourseProgressService().GetForEnrollement(tc, oCourse.ID, oCourse.Enrollments.ID, userID);
if (oCourseProgres != null)
{
oCourse.CourseProgres = oCourseProgres;
}
}
}
public void LoadCourseAllData(TransactionContext tc, Courses oCourse)
{
oCourse.CourseType = new CourseTypeService().Get(tc, oCourse.CourseTypeID);
oCourse.Trainer = new EmployeeService().Get(tc, oCourse.TrainerID);
oCourse.TotalLesson = CoursesDA.GetTotalLesson(tc, oCourse.ID);
oCourse.TotalContent = CoursesDA.GetTotalContent(tc, oCourse.ID);
oCourse.CourseTemplate = new LMSTemplateService().GetCourseTemplate(tc, oCourse.ID);
oCourse.Lessons = new LessonsService().GetByCourseID(tc, oCourse.ID);
}
public void LoadCourseAllDataForEnrollment(TransactionContext tc, Courses oCourse, int userID)
{
this.LoadCourseAllData(tc, oCourse);
oCourse.Enrollments = new EnrollmentService().GetForEnrollement(tc,oCourse.ID, userID);
if (oCourse.Enrollments == null)
{
Enrollments oEnrollments = new Enrollments(oCourse.ID, userID);
oCourse.Enrollments = new EnrollmentService().Save(tc, oEnrollments);
if (oCourse.Enrollments != null)
{
CourseProgres oCourseProgres = new CourseProgres(oCourse.Enrollments.ID, oCourse.ID, userID);
oCourse.CourseProgres = new CourseProgressService().Save(tc, oCourseProgres);
}
}
else
{
oCourse.Enrollments.IsCurrentEnroll = true;
oCourse.Enrollments.NoOfEnroll++;
EnrollmentDA.UpdateEnrollStatusForEnrollment(tc, oCourse.Enrollments);
CourseProgres oCourseProgres = new CourseProgressService().GetForEnrollement(tc, oCourse.ID, oCourse.Enrollments.ID, userID);
if (oCourseProgres != null)
{
oCourse.CourseProgres = oCourseProgres;
oCourse.LessonProgres = new LessonProgressService().GetByProgressID(oCourse.CourseProgres.ID);
}
}
this.UserLessonsCompletion(oCourse);
}
public void LoadCourseAllDataForCourseUpdate(TransactionContext tc, Courses oCourse)
{
int totalCompletedContent = 0;
this.LoadCourseAllData(tc, oCourse);
oCourse.Enrollments = new EnrollmentService().Get(tc, oCourse.Enrollments.ID);
if (oCourse.Enrollments.IsCurrentEnroll)
{
if (oCourse.LessonProgres.Count > 0)
{
foreach (LessonProgress oLProgress in oCourse.LessonProgres)
{
if (oLProgress.IsNew)
{
new LessonProgressService().Save(tc, oLProgress);
}
else
{
if (oLProgress.IsLessonComplete == false)
{
oLProgress.IsLessonComplete = false;
oLProgress.CompletionPercentage = 0;
oLProgress.CompleteDate = null;
oLProgress.TimeSpent = null;
oLProgress.LastAccessed = DateTime.Now;
oLProgress.RetryCount++;
}
else
{
oLProgress.IsLessonComplete = true;
oLProgress.CompletionPercentage = 100;
oLProgress.CompleteDate = DateTime.Now; ;
oLProgress.TimeSpent = "1 Minute";
oLProgress.LastAccessed = DateTime.Now;
oLProgress.RetryCount++;
}
new LessonProgressService().UpdateProgress(tc, oLProgress);
}
}
oCourse.LessonProgres = [];
oCourse.LessonProgres = new LessonProgressService().GetByProgressID(oCourse.CourseProgres.ID);
totalCompletedContent = oCourse.LessonProgres.Where(x=> x.IsLessonComplete == true).ToList().Count;
}
double complationRate = (double)totalCompletedContent/oCourse.TotalContent*100;
complationRate = Math.Round(complationRate, 2);
oCourse.CourseProgres.CompletionPercentage = (float)complationRate;
if (oCourse.CourseProgres.CompletionPercentage >= 80)
{
oCourse.CourseProgres.IsCourseComplete = true;
}
oCourse.CourseProgres = new CourseProgressService().UpdateCompletionRate(tc, oCourse.CourseProgres);
}
this.UserLessonsCompletion(oCourse);
}
public void UserLessonsCompletion(Courses oCourse)
{
if (oCourse != null){
if (oCourse.Lessons != null && oCourse.Lessons.Count > 0){
foreach (Lessons oLesson in oCourse.Lessons){
if (oLesson.LessonContents != null && oLesson.LessonContents.Count > 0){
foreach (LessonContent oLessonContent in oLesson.LessonContents){
if (oCourse.LessonProgres != null && oCourse.LessonProgres.Count > 0){
LessonProgress oLessonProgress = oCourse.LessonProgres.Find(x => x.LessonContentID == oLessonContent.ID);
if (oLessonProgress != null){
oLessonContent.IsLessonComplete = oLessonProgress.IsLessonComplete;
}
else{
oLessonContent.IsLessonComplete = false;
}
}else{
oLessonContent.IsLessonComplete = false;
}
}
}
}
}
}
}
public List<Courses> GetAll()
{
List<Courses> oCourses = new List<Courses>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(CoursesDA.GetAll(tc));
oCourses = this.CreateObjects<Courses>(dr);
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oCourses;
}
public List<Courses> GetByRating(float rating)
{
List<Courses> oCourses = new List<Courses>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(CoursesDA.GetByRating(tc, rating));
oCourses = this.CreateObjects<Courses>(dr);
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oCourses;
}
public List<Courses> GetByDifficultyLevel(EnumDifficultyLevel level)
{
List<Courses> oCourses = new List<Courses>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(CoursesDA.GetByDifficultyLevel(tc, level));
oCourses = this.CreateObjects<Courses>(dr);
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oCourses;
}
#endregion
}
}