Beispiel #1
0
        public static void CreateUserCred(string id, string username, string password)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    if (DoesUsernameExist(username, id))
                    {
                        throw new Exception("Username already exists.  Please select another.");
                    }

                    UserCredential cred = new UserCredential()
                    {
                        Password = password,
                        UserName = GetSHA256Hash(password),
                        User     = UserEntity.GetUser(id)
                    };

                    context.UserCredentials.Add(cred);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #2
0
        public static void MarkAllConvosAsRead(string userId, string otherUserId)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    var msgs = (from a in context.PrivateMessages
                                where (a.FromUser == userId && a.ToUser == otherUserId) || (a.FromUser == otherUserId && a.ToUser == userId)
                                select a).ToList();

                    foreach (var pm in msgs)
                    {
                        if (pm.DateRead == null && pm.FromUser != userId)
                        {
                            pm.DateRead             = DateTime.Now;
                            context.Entry(pm).State = EntityState.Modified;
                        }
                    }

                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #3
0
        public static void UpdateUserCred(string id, string username, string password)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    var cred = GetUserCred(id);

                    if (cred == null)
                    {
                        throw new Exception("User Credentials could not be found.");
                    }

                    if (DoesUsernameExist(username, id))
                    {
                        throw new Exception("Username already exists.  Please select another.");
                    }

                    cred.UserName = username;

                    if (password != null)
                    {
                        cred.Password = GetSHA256Hash(password);
                    }

                    context.Entry(cred).State = System.Data.Entity.EntityState.Modified;

                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #4
0
        public static void SendPrivateMessage(string toUserId, string fromUserId, string content)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    PrivateMessage pm = new PrivateMessage()
                    {
                        Content      = content,
                        DateSent     = DateTime.Now,
                        DateRead     = null,
                        UserSentFrom = UserEntity.GetUser(fromUserId, context),
                        UserSentTo   = UserEntity.GetUser(toUserId, context),
                        ToUser       = toUserId,
                        FromUser     = fromUserId
                    };

                    context.PrivateMessages.Add(pm);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #5
0
        public static void CreateTimeSlot(int schoolId, TimeSpan start, TimeSpan end)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    if ((from a in context.TimeSlots where a.StartTime == start && a.EndTime == end && a.SchoolId == schoolId select a).FirstOrDefault() != null)
                    {
                        throw new Exception("Time slot already exists.  Time slot not created.");
                    }

                    TimeSlot ts = new TimeSlot()
                    {
                        School    = (from a in context.Schools where a.SchoolId == schoolId select a).FirstOrDefault(),
                        StartTime = start,
                        EndTime   = end
                    };

                    context.TimeSlots.Add(ts);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #6
0
        public static void CreateEnrollment(string studentId, int periodId, int sectionId)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    var curEnroll = (from a in context.Enrollments where a.UserId == studentId && a.MarkingPeriodId == periodId && a.SectionId == sectionId select a).FirstOrDefault();
                    if (curEnroll != null)
                    {
                        throw new Exception("Enrollment already exists for " + curEnroll.Student.User.FirstName + " " + curEnroll.Student.User.LastName);
                    }
                    var enroll = new Enrollment()
                    {
                        Student       = (from a in context.Students where a.UserId == studentId select a).FirstOrDefault(),
                        MarkingPeriod = (from a in context.MarkingPeriods where a.MarkingPeriodId == periodId select a).FirstOrDefault(),
                        Section       = (from a in context.Sections where a.SectionId == sectionId select a).FirstOrDefault()
                    };

                    context.Enrollments.Add(enroll);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #7
0
 public static List <Gender> GetGenders()
 {
     using (SmacEntities context = new SmacEntities())
     {
         return((from a in context.Genders select a).ToList());
     }
 }
Beispiel #8
0
        public static void PromoteAdmin(string Id)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    if (UserEntity.GetUser(Id) == null)
                    {
                        throw new Exception("User not set as Admin.  User ID doesn't exist.");
                    }
                    else
                    {
                        if (IsAdmin(Id))
                        {
                            throw new Exception("User not set as Admin.  User already set as Admin.");
                        }
                        else
                        {
                            Admin admin = new Admin();
                            admin.UserId = Id;
                            admin.User   = UserEntity.GetUser(Id);

                            context.Admins.Add(admin);
                            context.SaveChanges();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #9
0
        public static void DeleteUser(string Id)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    if ((from a in context.Users where a.UserId == Id select a).FirstOrDefault() == null)
                    {
                        throw new Exception("User was not deleted.  User ID not found.");
                    }
                    else
                    {
                        var user = (from a in context.Users where a.UserId == Id select a).FirstOrDefault();

                        context.Users.Remove(user);

                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #10
0
        public static void UpdateTimeSlot(int schoolId, TimeSpan start, TimeSpan end, int id)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    if ((from a in context.TimeSlots where a.TimeSlotId == id select a) == null)
                    {
                        throw new Exception("Time slot could not be found");
                    }

                    if ((from a in context.TimeSlots where a.StartTime == start && a.EndTime == end && a.SchoolId == schoolId && a.TimeSlotId != id select a).FirstOrDefault() != null)
                    {
                        throw new Exception("Time slot already exists.  Time slot not updated.");
                    }

                    TimeSlot ts = (from a in context.TimeSlots where a.TimeSlotId == id select a).FirstOrDefault();
                    ts.StartTime = start;
                    ts.EndTime   = end;

                    context.Entry(ts).State = System.Data.Entity.EntityState.Modified;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #11
0
 public static DateTime?GetLastLoggedOut(string Id)
 {
     using (SmacEntities context = new SmacEntities())
     {
         return((from a in context.Users where a.UserId == Id select a).FirstOrDefault().LastLoggedOut);
     }
 }
Beispiel #12
0
        public static void AddEndDate(string Id, DateTime endDate)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    if (GetUser(Id) == null)
                    {
                        throw new Exception("User was not edited.  User ID not found.");
                    }
                    else
                    {
                        var user = GetUser(Id);

                        user.EndDate = endDate;
                        context.Entry(user).State = System.Data.Entity.EntityState.Modified;
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #13
0
 public static List <usp_GetUsersInSchool_Result> GetAllUsersInSchool(int schoolId, string userIdToFilter)
 {
     using (SmacEntities context = new SmacEntities())
     {
         return(context.usp_GetUsersInSchool(schoolId, userIdToFilter).OrderBy(t => t.LastName).ToList());
     }
 }
Beispiel #14
0
        public static Roles GetUserRole(string userId)
        {
            using (SmacEntities context = new SmacEntities())
            {
                var user = (from a in context.Users where a.UserId == userId select a).FirstOrDefault();

                if (user.Admin != null)
                {
                    return(Roles.Admin);
                }
                else if (user.Student != null)
                {
                    return(Roles.Student);
                }
                else if (user.Staff != null)
                {
                    return(Roles.Staff);
                }
                else if (user.Teacher != null)
                {
                    return(Roles.Teacher);
                }
                else
                {
                    return(Roles.None);
                }
            }
        }
Beispiel #15
0
        public static void CreateGrade(string val, int schoolId)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    if ((from a in context.Grades where a.SchoolId == schoolId && a.GradeValue == val select a).FirstOrDefault() != null)
                    {
                        throw new Exception("Grade not created as it already exists.");
                    }

                    Grade grade = new Grade()
                    {
                        GradeValue = val,
                        School     = (from a in context.Schools where a.SchoolId == schoolId select a).FirstOrDefault()
                    };

                    context.Grades.Add(grade);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #16
0
        public static void RevokeAdmin(string Id)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    if (UserEntity.GetUser(Id) == null)
                    {
                        throw new Exception("User not removed as Admin.  User doesn't exist.");
                    }
                    else if (!IsAdmin(Id))
                    {
                        throw new Exception("User not removed as Admin.  User not an Admin.");
                    }
                    else
                    {
                        Admin admin = new Admin();
                        admin.UserId = Id;
                        admin.User   = UserEntity.GetUser(Id);

                        context.Admins.Add(admin);
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #17
0
        public static void CreateSchedule(int sectionId, int tsId, string day, int periodId)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    if ((from a in context.SectionSchedules where a.SectionId == sectionId && a.TimeSlotId == tsId && a.DayValue == day && a.MarkingPeriodId == periodId select a).FirstOrDefault() != null)
                    {
                        throw new Exception("Schedule already exists for this section.");
                    }

                    SectionSchedule sch = new SectionSchedule()
                    {
                        Section       = (from a in context.Sections where a.SectionId == sectionId select a).FirstOrDefault(),
                        Day           = (from a in context.Days where a.DayValue == day select a).FirstOrDefault(),
                        TimeSlot      = (from a in context.TimeSlots where a.TimeSlotId == tsId select a).FirstOrDefault(),
                        MarkingPeriod = (from a in context.MarkingPeriods where a.MarkingPeriodId == periodId select a).FirstOrDefault()
                    };

                    context.SectionSchedules.Add(sch);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #18
0
 public static Gender GetGender(string gType)
 {
     using (SmacEntities context = new SmacEntities())
     {
         return((from a in context.Genders where a.GenderType == gType select a).FirstOrDefault());
     }
 }
Beispiel #19
0
        public static void RemoveGrade(string studentId, int schoolId, int subjectId, int classId,
                                       int sectionId, int markingPeriodId)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    var enroll = GetEnrollment(studentId, schoolId, subjectId, classId, sectionId, markingPeriodId);

                    if (enroll != null)
                    {
                        enroll.GradeValue           = null;
                        context.Entry(enroll).State = System.Data.Entity.EntityState.Modified;
                        context.SaveChanges();
                    }
                    else
                    {
                        throw new Exception("Enrollment not found.  Grade not set.");
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #20
0
        public static void SetGrade(string studentId, int sectionId, int markingPeriodId, string grade)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    var enroll = (from a in context.Enrollments where a.UserId == studentId && a.SectionId == sectionId && a.MarkingPeriodId == markingPeriodId select a).FirstOrDefault();

                    if (enroll != null)
                    {
                        enroll.GradeValue           = grade;
                        context.Entry(enroll).State = System.Data.Entity.EntityState.Modified;
                        context.SaveChanges();
                    }
                    else
                    {
                        throw new Exception("Enrollment not found.  Grade not set.");
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #21
0
 public static List <usp_GetUsersInSchool_Result> GetAllStudentsInSchool(int schoolId)
 {
     using (SmacEntities context = new SmacEntities())
     {
         return(context.usp_GetUsersInSchool(schoolId, string.Empty).OrderBy(t => t.LastName).Where(t => t.Student == true).ToList());
     }
 }
Beispiel #22
0
        public static void PromoteStudent(string uId)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    if (UserEntity.GetUser(uId) == null)
                    {
                        throw new Exception("User not set as Student.  User ID doesn't exist.");
                    }
                    else
                    {
                        if (IsStudent(uId))
                        {
                            throw new Exception("User not set as Student.  User already set as Student.");
                        }
                        else
                        {
                            Student Student = new Student();
                            Student.UserId = uId;
                            Student.User   = UserEntity.GetUser(uId);

                            context.Students.Add(Student);
                            context.SaveChanges();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #23
0
 public static bool IsStudent(string uId)
 {
     using (SmacEntities context = new SmacEntities())
     {
         return((from a in context.Students where a.UserId == uId select a).FirstOrDefault() != null);
     }
 }
Beispiel #24
0
        public static void RevokeStudent(string uId)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    if (UserEntity.GetUser(uId) == null)
                    {
                        throw new Exception("User not removed as Student.  User doesn't exist.");
                    }
                    else if (!IsStudent(uId))
                    {
                        throw new Exception("User not removed as Student.  User not an Student.");
                    }
                    else
                    {
                        Student Student = new Student();
                        Student.UserId = uId;
                        Student.User   = UserEntity.GetUser(uId);

                        context.Students.Add(Student);
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #25
0
        public static KhanShare CreateKhanShare(string title, string url, string apiId)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    var ks = GetKhanShare(title, url, apiId);

                    if (ks != null)
                    {
                        KhanShare newKs = new KhanShare()
                        {
                            ApiId = apiId,
                            Url   = url,
                            Title = title
                        };

                        context.KhanShares.Add(newKs);
                        context.SaveChanges();
                    }

                    return(GetKhanShare(title, url, apiId));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #26
0
        public static void CreatePost(string userId, string title, int sectionId, string content, int?repliedTo)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    var thread = new Thread()
                    {
                        Section        = (from a in context.Sections where a.SectionId == sectionId select a).FirstOrDefault(),
                        RepliedTo      = repliedTo,
                        DateTimePosted = DateTime.Now,
                        Content        = content,
                        ThreadTitle    = title,
                        User           = (from a in context.Users where a.UserId == userId select a).FirstOrDefault()
                    };

                    context.Threads.Add(thread);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #27
0
        public static void PromoteTeacher(string uId)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    if (UserEntity.GetUser(uId) == null)
                    {
                        throw new Exception("User not set as Teacher.  User ID doesn't exist.");
                    }
                    else
                    {
                        if (IsTeacher(uId))
                        {
                            throw new Exception("User not set as Teacher.  User already set as Teacher.");
                        }
                        else
                        {
                            Teacher Teacher = new Teacher();
                            Teacher.UserId = uId;
                            Teacher.User   = UserEntity.GetUser(uId);

                            context.Teachers.Add(Teacher);
                            context.SaveChanges();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #28
0
        public static void RevokeTeacher(string uId)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    if (UserEntity.GetUser(uId) == null)
                    {
                        throw new Exception("User not removed as Teacher.  User doesn't exist.");
                    }
                    else if (!IsTeacher(uId))
                    {
                        throw new Exception("User not removed as Teacher.  User is not a Teacher.");
                    }
                    else
                    {
                        Teacher Teacher = new Teacher();
                        Teacher.UserId = uId;
                        Teacher.User   = UserEntity.GetUser(uId);

                        context.Teachers.Add(Teacher);
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #29
0
        private static void CreateEditMarkingPeriod(string op, int?markingPeriodId, string period, bool fullYear, int schoolYearId, DateTime start, DateTime end)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    if (op.Equals("ADD"))
                    {
                        if ((from a in context.MarkingPeriods where a.Period == period && a.FullYear == fullYear && a.SchoolYearId == schoolYearId select a).FirstOrDefault() != null)
                        {
                            throw new Exception("Marking period was not created.  Marking period already exists for this school year.");
                        }

                        MarkingPeriod MarkingPeriod = new MarkingPeriod()
                        {
                            SchoolYear = (from a in context.SchoolYears where a.SchoolYearId == schoolYearId select a).FirstOrDefault(),
                            Period     = fullYear ? null : period,
                            FullYear   = fullYear,
                            StartDate  = start,
                            EndDate    = end
                        };

                        context.MarkingPeriods.Add(MarkingPeriod);
                        context.SaveChanges();
                    }
                    else if (op.Equals("EDIT"))
                    {
                        var mPeriod = (from a in context.MarkingPeriods where a.MarkingPeriodId == markingPeriodId.Value select a).FirstOrDefault();

                        if (mPeriod == null)
                        {
                            throw new Exception("Marking period was not updated.  Marking period not found.");
                        }

                        if (GetMarkingPeriod(period, fullYear, schoolYearId, markingPeriodId.Value, context) != null)
                        {
                            throw new Exception("Marking period was not updated.  New marking period values already exist.");
                        }

                        mPeriod.Period     = fullYear ? null : period;
                        mPeriod.FullYear   = fullYear;
                        mPeriod.StartDate  = start;
                        mPeriod.EndDate    = end;
                        mPeriod.SchoolYear = (from a in context.SchoolYears where a.SchoolYearId == schoolYearId select a).FirstOrDefault();

                        context.Entry(mPeriod).State = System.Data.Entity.EntityState.Modified;
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #30
0
        private static void CreateEditSchoolYear(string op, int schoolId, string year, DateTime start, DateTime end, int?schoolYearId)
        {
            try
            {
                using (SmacEntities context = new SmacEntities())
                {
                    if (op.Equals("ADD"))
                    {
                        if ((from a in context.SchoolYears where a.Year == year select a).FirstOrDefault() != null)
                        {
                            throw new Exception("School year was not created.  School year already exists for this school.");
                        }
                        else
                        {
                            SchoolYear schoolyear = new SchoolYear()
                            {
                                School    = (from a in context.Schools where a.SchoolId == schoolId select a).FirstOrDefault(),
                                Year      = year,
                                StartDate = start,
                                EndDate   = end
                            };

                            context.SchoolYears.Add(schoolyear);
                            context.SaveChanges();
                        }
                    }
                    else if (op.Equals("EDIT"))
                    {
                        if ((from a in context.SchoolYears where a.SchoolYearId == schoolYearId.Value select a).FirstOrDefault() == null)
                        {
                            throw new Exception("Subject was not updated.  Subject name not found.");
                        }
                        else if ((from a in context.SchoolYears where a.SchoolYearId != schoolYearId.Value && a.SchoolId == schoolId && a.Year == year select a).FirstOrDefault() != null)
                        {
                            throw new Exception("Subject was not updated.  New subject name already exists in database.");
                        }
                        else
                        {
                            var schYear = (from a in context.SchoolYears where a.SchoolYearId == schoolYearId.Value select a).FirstOrDefault();

                            schYear.Year                 = year;
                            schYear.StartDate            = start;
                            schYear.EndDate              = end;
                            context.Entry(schYear).State = System.Data.Entity.EntityState.Modified;
                            context.SaveChanges();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }