public JsonResult AssignTo(int CourseID, int TeacherID)
        {
            CourseRepository courseRepository = new CourseRepository();
            CourseSubjectRepository courseSubjectRepo = new CourseSubjectRepository();
            TeacherRepository teacherRepository = new TeacherRepository();
            Teacher teacher = new Teacher();
            teacher = teacherRepository.GetById(TeacherID);
            List<SelectListItem> listSubjects = new List<SelectListItem>();
            var allSubjects = courseSubjectRepo.GetAll(filter: s => s.CourseID == CourseID);

            foreach (var item in allSubjects)
            {
                if (teacher.CourseSubject.Any(cs => cs.CourseID == CourseID && cs.SubjectID == item.SubjectID))
                {
                    listSubjects.Add(new SelectListItem() { Text = item.Subject.Name, Value = item.SubjectID.ToString(), Selected = true });
                }
                else
                {
                    listSubjects.Add(new SelectListItem() { Text = item.Subject.Name, Value = item.SubjectID.ToString(), Selected = false });
                }

            }

            return Json(listSubjects, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Edit()
        {
            TeacherControllerTeacherVM teacherModel = new TeacherControllerTeacherVM();
            Teacher teacher = new Teacher();
            TeacherRepository teacherRepo = new TeacherRepository();

            teacherModel.Id = AuthenticationManager.LoggedUser.Id;
            teacher = teacherRepo.GetById(teacherModel.Id);
            teacherModel.FirstName = teacher.FirstName;
            teacherModel.LastName = teacher.LastName;
            teacherModel.Password = teacher.Password;

            return View(teacherModel);
        }
 public ActionResult Edit(TeacherControllerTeacherVM teacherModel)
 {
     TryUpdateModel(teacherModel);
     if (ModelState.IsValid)
     {
         Teacher teacher = new Teacher();
         TeacherRepository teacherRepo = new TeacherRepository();
         teacher = teacherRepo.GetById(teacherModel.Id);
         teacher.FirstName = teacherModel.FirstName;
         teacher.LastName = teacherModel.LastName;
         teacher.Password = teacherModel.Password;
         teacherRepo.Save(teacher);
         return RedirectToAction("Index");
     }
     return View(teacherModel);
 }
        public ActionResult Courses()
        {
            if (!AuthenticationManager.LoggedUser.GetType().BaseType.Equals(typeof(Teacher)))
            {
                return RedirectToAction("Default", "Login");
            }
            TeacherControllerCoursesVM model = new TeacherControllerCoursesVM();
            CourseRepository courseRepository = new CourseRepository();

            Teacher teacher = new Teacher();
            TeacherRepository teacherRepository = new TeacherRepository();
            teacher = teacherRepository.GetById(AuthenticationManager.LoggedUser.Id);

            List<CourseSubject> courseSubjectList = teacher.CourseSubject.ToList();
            List<int> courseSubjectsId = courseSubjectList.Select(cs => cs.CourseID).Distinct().ToList();
            List<Course> courseList = new List<Course>();
            foreach (var item in courseSubjectsId)
            {
                courseList.Add(courseRepository.GetById(item));
            }
            model.CourseList = courseList;
            return View(model);
        }
 public ActionResult Index()
 {
     if (AuthenticationManager.LoggedUser == null || !AuthenticationManager.LoggedUser.GetType().BaseType.Equals(typeof(Teacher)))
     {
         return RedirectToAction("Login", "Default");
     }
     TeacherControllerTeacherVM model = new TeacherControllerTeacherVM();
     Teacher teacher = new Teacher();
     TeacherRepository teacherRepository = new TeacherRepository();
     teacher = teacherRepository.GetById(AuthenticationManager.LoggedUser.Id);
     model.FirstName = teacher.FirstName;
     model.LastName = teacher.LastName;
     CourseSubjectRepository courseSubjectRepository = new CourseSubjectRepository();
     CourseRepository courseRepository = new CourseRepository();
     SubjectRepository subjectRepository = new SubjectRepository();
     List<int> subjectList = new List<int>();
     Dictionary<string, List<Subject>> courseSubjectList = new Dictionary<string, List<Subject>>();
     List<Subject> subjects = new List<Subject>();
     List<int> courseList = new List<int>();
     foreach (var courseSubject in teacher.CourseSubject)
     {
         courseList.Add(courseSubject.Course.Id);
         subjectList.Add(courseSubject.Subject.Id);
     }
     subjectList = subjectList.Distinct().ToList();
     courseList = courseList.Distinct().ToList();
     Course course = new Course();
     foreach (var courseID in courseList)
     {
         course = courseRepository.GetById(courseID);
         subjects = courseSubjectRepository.GetAll(filter: c => c.Course.Id == courseID && subjectList.Contains(c.Subject.Id)).Select(s => s.Subject).ToList();
         courseSubjectList.Add(course.Name, subjects);
     }
     model.CourseSubjectList = courseSubjectList;
     return View(model);
 }
        public ActionResult EditTeachers(int id, AdminControllerTeacherVM teacherModel)
        {
            UnitOfWork uOw = new UnitOfWork();
            Teacher teacher = null;
            Title title = new Title();
            TitleRepository titleRepository = new TitleRepository(uOw);
            TeacherRepository teacherRepository = new TeacherRepository(uOw);

            TryUpdateModel(teacherModel);
            if (teacherModel.ListItems == null)
            {
                teacherModel.TitleList = titleRepository.GetAll();
                List<SelectListItem> SelectListTitle = new List<SelectListItem>();

                foreach (var item in teacherModel.TitleList)
                {
                    SelectListTitle.Add(new SelectListItem() { Text = item.Name, Value = item.Id.ToString() });
                }
                teacherModel.ListItems = SelectListTitle;
            }

            if (ModelState.IsValid)
            {
                if (id > 0)
                {
                    teacher = teacherRepository.GetById(id);
                    teacher.FirstName = teacherModel.FirstName;
                    teacher.LastName = teacherModel.LastName;
                    teacher.UserName = teacherModel.UserName;
                    teacher.Password = teacherModel.Password;
                    title = titleRepository.GetById(teacherModel.TitleID);
                    teacher.Title = title;
                    teacher.IsActive = teacherModel.isActive;
                    teacherRepository.Save(teacher);
                    uOw.Commit();
                    return RedirectToAction("ManageTeachers");
                }
                else
                {
                    teacher = teacherRepository.GetAll(filter: t => t.UserName == teacherModel.UserName).FirstOrDefault();
                    if (teacher == null)
                    {
                        teacher = new Teacher();
                        teacher.FirstName = teacherModel.FirstName;
                        teacher.LastName = teacherModel.LastName;
                        teacher.UserName = teacherModel.UserName;
                        teacher.Password = SecurityService.CreateHash(teacherModel.Password);
                        title = titleRepository.GetById(teacherModel.TitleID);
                        teacher.Title = title;
                        teacher.IsActive = teacherModel.isActive;
                        teacherRepository.Save(teacher);
                        uOw.Commit();
                        return RedirectToAction("ManageTeachers");
                    }
                    else
                    {
                        throw new ArgumentException("Invalid username !");
                    }
                }
            }
            return View(teacherModel);
        }
        public ActionResult EditTeachers(int id)
        {
            TeacherRepository teacherRepository = new TeacherRepository();
            TitleRepository titleRepository = new TitleRepository();
            Teacher teacher = new Teacher();
            AdminControllerTeacherVM teacherModel = new AdminControllerTeacherVM();
            List<SelectListItem> SelectListTitle = new List<SelectListItem>();
            SelectListItem select = null;
            Title title = new Title();
            teacher.Title = title;

            if (id > 0)
            {
                teacher = teacherRepository.GetById(id);
                select = new SelectListItem() { Text = teacher.Title.Name, Value = teacher.Title.Id.ToString() };
                SelectListTitle.Add(select);
            }

            teacherModel.TitleList = titleRepository.GetAll();
            foreach (var item in teacherModel.TitleList)
            {
                if (item.Id != teacher.Title.Id)
                {
                    select = new SelectListItem() { Text = item.Name, Value = item.Id.ToString() };
                    SelectListTitle.Add(select);
                }
            }

            if (id > 0)
            {
                teacher = teacherRepository.GetById(id);
                teacherModel.FirstName = teacher.FirstName;
                teacherModel.LastName = teacher.LastName;
                teacherModel.UserName = teacher.UserName;
                teacherModel.Password = teacher.Password;
                teacherModel.isActive = teacher.IsActive;
                teacherModel.Id = id;
            }

            if (id == 0)
            {
                teacher.FirstName = teacherModel.FirstName;
                teacher.LastName = teacherModel.LastName;
                teacher.UserName = teacherModel.UserName;
                teacher.Password = teacherModel.Password;
                teacher.IsActive = teacherModel.isActive;
            }
            teacherModel.ListItems = SelectListTitle;
            return View(teacherModel);
        }
 public ActionResult DeleteTeacher(int id)
 {
     Teacher teacher = new Teacher();
     TeacherRepository teacherRepository = new TeacherRepository();
     teacher = teacherRepository.GetById(id);
     teacher.IsActive = false;
     teacherRepository.Save(teacher);
     return RedirectToAction("ManageTeachers");
 }
        public JsonResult CheckForExistingName(string name, string type, int id)
        {
            bool exist = false;
            switch (type)
            {
                case "Admin":
                    Administrator admin = new Administrator();
                    AdministratorRepository adminRepository = new AdministratorRepository();
                    admin = adminRepository.GetAll(filter: a => a.UserName == name && a.Id != id).FirstOrDefault();
                    if (admin != null)
                    {
                        exist = true;
                    };
                    break;
                case "Student":
                    Student student = new Student();
                    StudentRepository studentRepository = new StudentRepository();
                    student = studentRepository.GetAll(filter: s => s.UserName == name && s.Id != id).FirstOrDefault();
                    if (student != null)
                    {
                        exist = true;
                    };
                    break;
                case "Teacher":
                    Teacher teacher = new Teacher();
                    TeacherRepository teacherRepository = new TeacherRepository();
                    teacher = teacherRepository.GetAll(filter: t => t.UserName == name && t.Id != id).FirstOrDefault();
                    if (teacher != null)
                    {
                        exist = true;
                    };
                    break;
                case "Course":
                    Course course = new Course();
                    CourseRepository courseRepository = new CourseRepository();
                    course = courseRepository.GetAll(filter: c => c.Name == name).FirstOrDefault();
                    if (course != null)
                    {
                        exist = true;
                    };
                    break;
                case "Title":
                    Title title = new Title();
                    TitleRepository titleRepository = new TitleRepository();
                    title = titleRepository.GetAll(filter: t => t.Name == name).FirstOrDefault();
                    if (title != null)
                    {
                        exist = true;
                    };
                    break;
                case "Subject":
                    Subject subject = new Subject();
                    SubjectRepository subjectRepository = new SubjectRepository();
                    subject = subjectRepository.GetAll(filter: s => s.Name == name).FirstOrDefault();
                    if (subject != null)
                    {
                        exist = true;
                    };
                    break;
            }

            return Json(exist, JsonRequestBehavior.AllowGet);
        }
 public ActionResult ShowTeachingSubjects(int id)
 {
     Teacher teacher = new Teacher();
     TeacherRepository teacherRepository = new TeacherRepository();
     teacher = teacherRepository.GetById(id);
     AdminControllerTeacherVM model = new AdminControllerTeacherVM();
     model.FirstName = teacher.FirstName + " " + teacher.LastName;
     model.CourseSubjectList = teacher.CourseSubject.ToList();
     return View(model);
 }
 public ActionResult EditArticle(ArticleControllerArticlesVM model)
 {
     TryUpdateModel(model);
     if (model.SubjectID < 1 || !ModelState.IsValid)
     {
         model.ArticleId = 0;
         List<CourseSubject> courseSubject = new List<CourseSubject>();
         Teacher teacher = new Teacher();
         TeacherRepository teacherRepository = new TeacherRepository();
         teacher = teacherRepository.GetById(AuthenticationManager.LoggedUser.Id);
         courseSubject = teacher.CourseSubject.ToList();
         List<SelectListItem> listSubjects = new List<SelectListItem>();
         listSubjects.Add(new SelectListItem() { Text = "Select subject", Value = "" });
         foreach (var item in courseSubject)
         {
             if (item.Subject.Id != model.ArticleId)
             {
                 listSubjects.Add(new SelectListItem() { Text = item.Subject.Name, Value = item.Subject.Id.ToString() });
             }
         }
         model.SubjectsListItems = listSubjects;
     }
     if (ModelState.IsValid)
     {
         Article article = new Article();
         ArticleRepository articleRepository = new ArticleRepository();
         Subject subject = new Subject();
         SubjectRepository subjectRepository = new SubjectRepository();
         Teacher teacher = new Teacher();
         TeacherRepository teacherRepository = new TeacherRepository();
         teacher = teacherRepository.GetById(AuthenticationManager.LoggedUser.Id);
         if (model.ArticleId > 0)
         {
             article = articleRepository.GetById(model.ArticleId);
             article.Content = model.Content;
             article.DateCreated = model.DateCreated;
             article.DateModified = DateTime.Now;
             article.SubjectID = model.SubjectID;
             article.TeacherID = teacher.Id;
             article.Title = model.Title;
         }
         else
         {
             article.Content = model.Content;
             article.DateCreated = DateTime.Now;
             article.SubjectID = model.SubjectID;
             article.TeacherID = teacher.Id;
             article.Title = model.Title;
         }
         articleRepository.Save(article);
         return RedirectToAction("Articles");
     }
     return View(model);
 }
        public ActionResult EditArticle(int id)
        {
            if (AuthenticationManager.LoggedUser.GetType().BaseType.Equals(typeof(Teacher)))
            {
                ArticleControllerArticlesVM model = new ArticleControllerArticlesVM();
                TeacherRepository teacherRepository = new TeacherRepository();
                Article article = new Article();
                ArticleRepository articleRepository = new ArticleRepository();
                List<Subject> subjectList = new List<Subject>();
                SubjectRepository subjectRepository = new SubjectRepository();
                Teacher teacher = new Teacher();
                List<SelectListItem> listSubjects = new List<SelectListItem>();
                teacher = teacherRepository.GetById(AuthenticationManager.LoggedUser.Id);
                List<int> subjectId = new List<int>();
                foreach (var item in teacher.CourseSubject)
                {
                    subjectId.Add(item.Subject.Id);
                }
                subjectId = subjectId.Distinct().ToList();
                foreach (var item in subjectId)
                {
                    subjectList.Add(subjectRepository.GetById(item));
                }

                if (id > 0)
                {
                    article = articleRepository.GetById(id);
                    model.ArticleId = article.Id;
                    model.TeacherID = teacher.Id;
                    model.Title = article.Title;
                    model.Content = article.Content;
                    model.DateCreated = article.DateCreated;
                    model.DateModified = article.DateModified;
                    model.Subject = article.Subject;
                    model.Teacher = teacher;
                    listSubjects.Add(new SelectListItem() { Text = article.Subject.Name, Value = article.Subject.Id.ToString(), Selected = true });
                }
                if (id == 0)
                {
                    model.ArticleId = 0;
                    listSubjects.Add(new SelectListItem() { Text = "Select subject", Value = "" });
                }
                foreach (var item in subjectList)
                {
                    if (item.Id != model.ArticleId)
                    {
                        listSubjects.Add(new SelectListItem() { Text = item.Name, Value = item.Id.ToString() });
                    }
                }
                model.SubjectsListItems = listSubjects;
                return View(model);
            }
            return RedirectToAction("Articles");
        }
        public ActionResult Articles()
        {
            if (AuthenticationManager.LoggedUser == null)
            {
                return RedirectToAction("Login", "Default");
            }
            List<Article> articleList = new List<Article>();
            ArticleRepository articleRepository = new ArticleRepository();
            Dictionary<int, List<Comment>> comments = new Dictionary<int, List<Comment>>();
            CommentRepository commentRepository = new CommentRepository();
            Dictionary<int, string> userDictionary = new Dictionary<int, string>();
            List<int> subjectId = new List<int>();
            Teacher teacher = new Teacher();
            TeacherRepository teacherRepository = new TeacherRepository();
            Student student = new Student();
            StudentRepository studentRepository = new StudentRepository();
            List<Article> list = new List<Article>();
            if (AuthenticationManager.LoggedUser.GetType().BaseType.Equals(typeof(Teacher)))
            {
                teacher = teacherRepository.GetById(AuthenticationManager.LoggedUser.Id);
                foreach (var item in teacher.CourseSubject)
                {
                    subjectId.Add(item.Subject.Id);
                }
            }
            else if (AuthenticationManager.LoggedUser.GetType().BaseType.Equals(typeof(Student)))
            {
                student = studentRepository.GetById(AuthenticationManager.LoggedUser.Id);
                List<CourseSubject> courseSubjectList = new List<CourseSubject>();
                CourseSubjectRepository courseSubjectRepository = new CourseSubjectRepository();
                courseSubjectList = courseSubjectRepository.GetAll(filter: cs => cs.CourseID == student.CourseID);
                foreach (var item in courseSubjectList)
                {
                    subjectId.Add(item.Subject.Id);
                }
            }
            subjectId = subjectId.Distinct().ToList();
            foreach (var item in subjectId)
            {
                List<Article> article = articleRepository.GetAll(filter: s => s.Subject.Id == item);
                if (article != null)
                {
                    articleList.AddRange(article);
                }
            }
            articleList = articleList.OrderBy(d => d.DateCreated.TimeOfDay).ToList();
            articleList.Reverse();
            ArticleControllerArticlesVM model = new ArticleControllerArticlesVM();
            LikeRepository likeRepository = new LikeRepository();
            Dictionary<Article, int> ArticlesAndLikeCount = new Dictionary<Article, int>();
            Dictionary<int, bool> Liked = new Dictionary<int, bool>();
            int articleId = 0;
            string type = AuthenticationManager.LoggedUser.GetType().BaseType.ToString();
            int start = type.LastIndexOf(".") + 1;
            int positions = type.Length - start;
            type = type.Substring(start, positions);
            foreach (var item in articleList)
            {
                List<Comment> commentedCommentList = new List<Comment>();
                commentedCommentList = commentRepository.GetAll(filter: c => c.Article.Id == item.Id);
                commentedCommentList.OrderBy(c => c.DateCreated.TimeOfDay).ToList();
                commentedCommentList.Reverse();
                foreach (var comment in commentedCommentList)
                {
                    string userName = "";
                    if (comment.UserType == "Teacher")
                    {
                        teacher = teacherRepository.GetById(comment.UserID);
                        if (teacher != null)
                        {
                            userName = teacher.FirstName + " " + teacher.LastName;
                            userDictionary.Add(comment.Id, userName);
                        }
                    }
                    else
                    {
                        student = studentRepository.GetById(comment.UserID);
                        userName = student.FirstName + " " + student.LastName;
                        userDictionary.Add(comment.Id, userName);
                    }
                }
                comments.Add(item.Id, commentedCommentList);
                int count = likeRepository.GetAll(filter: a => a.ArticleID == item.Id).Count;
                ArticlesAndLikeCount.Add(item, count);
                List<Like> likes = new List<Like>();

                likes = likeRepository.GetAll(l => l.ArticleID == item.Id);
                foreach (var like in likes.Where(l => l.UserID == AuthenticationManager.LoggedUser.Id && l.UserType == type))
                {
                    Liked.Add(item.Id, true);
                }
                model.ArticleId = item.Id;
                if (Liked.Count != ArticlesAndLikeCount.Count)
                {
                    foreach (var dictionary in ArticlesAndLikeCount.Where(a => a.Key.Id == item.Id))
                    {
                        articleId = item.Id;
                    }
                    Liked.Add(articleId, false);
                }
            }
            model.UserType = type;
            model.IsLiked = Liked;
            model.UserID = AuthenticationManager.LoggedUser.Id;
            model.Articles = ArticlesAndLikeCount;
            model.CommentList = comments;
            model.UserDictionary = userDictionary;
            return View(model);
        }