public ActionResult Index()
 {
     if (AuthenticationManager.LoggedUser == null)
     {
         return RedirectToAction("Login", "Default");
     }
     Student student = new Student();
     StudentRepository studentRepository = new StudentRepository();
     student = studentRepository.GetById(AuthenticationManager.LoggedUser.Id);
     StudentControllerStudentVM model = new StudentControllerStudentVM();
     Course course = new Course();
     CourseRepository courseRepository = new CourseRepository();
     course = courseRepository.GetAll(filter: c => c.Id == student.CourseID).FirstOrDefault();
     List<Subject> subjectList = new List<Subject>();
     CourseSubjectRepository courseSubjectRepository = new CourseSubjectRepository();
     subjectList = courseSubjectRepository.GetAll(filter: c => c.CourseID == course.Id).Select(s => s.Subject).ToList();
     List<string> subjectNames = new List<string>();
     foreach (var subject in subjectList)
     {
         subjectNames.Add(subject.Name);
     }
     model.Subjects = subjectNames;
     model.FirstName = student.FirstName;
     model.LastName = student.LastName;
     model.StudentID = AuthenticationManager.LoggedUser.Id;
     model.Course = course.Name;
     model.FaculityNumber = student.FacultyNumber;
     return View(model);
 }
 public DataTable StudentsGradesToExcel(Student student)
 {
     List<Grade> gradeList = new List<Grade>();
     GradeRepository gradeRepository = new GradeRepository();
     gradeList = gradeRepository.GetAll(filter: g => g.Student.Id == student.Id);
     var gradeTable = new DataTable("StudentsGrades");
     gradeTable.Columns.Add("SubjectName", typeof(string));
     gradeTable.Columns.Add("GradeValue", typeof(string));
     Dictionary<string, string> details = new Dictionary<string, string>();
     List<string> subjectNameList = new List<string>();
     List<string> gradeValues = new List<string>();
     foreach (var item in gradeList)
     {
         subjectNameList.Add(item.Subject.Name);
     }
     subjectNameList = subjectNameList.Distinct().ToList();
     foreach (var item in subjectNameList)
     {
         StringBuilder sb = new StringBuilder();
         List<Grade> grades = new List<Grade>();
         grades = gradeRepository.GetAll(filter: s => s.Subject.Name == item && s.Student.Id == student.Id);
         foreach (var grade in grades)
         {
             sb.Append(grade.GradeValue);
             sb.Append(",");
         }
         sb.Length -= 1;
         details.Add(item, sb.ToString());
     }
     foreach (var item in details)
     {
         gradeTable.Rows.Add(item.Key, item.Value);
     }
     return gradeTable;
 }
 public ActionResult ChangePassword(int id)
 {
     Student student = new Student();
     StudentRepository studentRepository = new StudentRepository();
     student = studentRepository.GetById(id);
     StudentControllerStudentVM model = new StudentControllerStudentVM();
     return View(model);
 }
 public ActionResult ChangePassword(int id, StudentControllerStudentVM model)
 {
     TryUpdateModel(model);
     if (ModelState.IsValid)
     {
         Student student = new Student();
         StudentRepository studentRepository = new StudentRepository();
         student = studentRepository.GetById(id);
         student.Password = model.Password;
         studentRepository.Save(student);
         return RedirectToAction("Index");
     }
     return View(model);
 }
        public DataTable SubjectGrades(Subject subject)
        {
            List<Grade> gradeList = new List<Grade>();
            GradeRepository gradeRepository = new GradeRepository();
            gradeList = gradeRepository.GetAll(filter: s => s.Subject.Id == subject.Id);
            StudentRepository studentRepository = new StudentRepository();
            var gradeTable = new DataTable("SubjectsGrades");
            gradeTable.Columns.Add("StudentName",typeof(string));
            gradeTable.Columns.Add("GradeValue", typeof(string));
            Dictionary<string, string> details = new Dictionary<string, string>();
            List<string> gradeValues = new List<string>();
            List<int> studentList = new List<int>();
            foreach (var item in gradeList)
            {
                studentList.Add(item.Student.Id);
            }
            studentList = studentList.Distinct().ToList();
            foreach (var item in studentList)
            {
                StringBuilder sb = new StringBuilder();
                List<Grade> grades = new List<Grade>();
                grades = gradeRepository.GetAll(filter: s => s.Student.Id == item && s.Subject.Id == subject.Id);
                foreach (var grade in grades)
                {
                    sb.Append(grade.GradeValue);
                    sb.Append(",");
                }
                sb.Length -= 1;
                Student student = new Student();
                student = studentRepository.GetAll(filter: s=> s.Id == item).FirstOrDefault();
                string fullName = student.FirstName + " " + student.LastName;
                details.Add(fullName, sb.ToString());
            }

            foreach (var item in details)
            {
                gradeTable.Rows.Add(item.Key, item.Value);
            }
            return gradeTable;
        }
        public ActionResult ShowDetails(int id)
        {
            StudentControllerStudentVM model = new StudentControllerStudentVM();
            List<Grade> gradeList = new List<Grade>();
            GradeRepository gradeRepository = new GradeRepository();
            gradeList = gradeRepository.GetAll(filter: s => s.Student.Id == id);
            Dictionary<string, List<string>> details = new Dictionary<string, List<string>>();
            var subjectList = new List<string>();

            foreach (var item in gradeList)
            {
                subjectList.Add(item.Subject.Name);
            }
            subjectList = subjectList.Distinct().ToList();

            foreach (var item in subjectList)
            {
                var gradeValueList = new List<string>();
                List<Grade> grades = new List<Grade>();
                grades = gradeRepository.GetAll(filter: s => s.Subject.Name == item && s.Student.Id == id);
                foreach (var grade in grades)
                {
                    gradeValueList.Add(grade.GradeValue.ToString());
                }
                details.Add(item, gradeValueList);
            }
            model.SubjectGradeList = details;
            Student student = new Student();
            StudentRepository studentRepository = new StudentRepository();
            student = studentRepository.GetById(id);
            model.FirstName = student.FirstName;
            model.LastName = student.LastName;
            return View(model);
        }
 public JsonResult EditGrade(int gradeId, double gradeValue, int subjectId, int studentId)
 {
     Grade grade = new Grade();
     GradeRepository gradeRepo = new GradeRepository();
     SelectListItem gradeItem = null;
     if (gradeId != 0)
     {
         grade = gradeRepo.GetById(gradeId);
         gradeValue = System.Math.Round(gradeValue, 2);
         grade.GradeValue = gradeValue;
         gradeRepo.Save(grade);
     }
     else
     {
         UnitOfWork unitOfWork = new UnitOfWork();
         StudentRepository studentRepository = new StudentRepository(unitOfWork);
         GradeRepository gradeRepository = new GradeRepository(unitOfWork);
         SubjectRepository subjectRepository = new SubjectRepository(unitOfWork);
         Student student = new Student();
         student = studentRepository.GetById(studentId);
         Subject subject = new Subject();
         subject = subjectRepository.GetById(subjectId);
         grade.SubjectID = subjectId;
         grade.Subject = subject;
         grade.Student = student;
         gradeValue = System.Math.Round(gradeValue, 2);
         grade.GradeValue = gradeValue;
         gradeRepository.Save(grade);
         unitOfWork.Commit();
     }
     gradeItem = new SelectListItem() { Text = grade.GradeValue.ToString(), Value = grade.Id.ToString() };
     return Json(gradeItem, JsonRequestBehavior.AllowGet);
 }
        public ActionResult StudentDetails(int StudentID)
        {
            if (!AuthenticationManager.LoggedUser.GetType().BaseType.Equals(typeof(Teacher)))
            {
                return RedirectToAction("Default", "Login");
            }
            List<Grade> studentGrades = new List<Grade>();
            GradeRepository gradeRepository = new GradeRepository();
            Student student = new Student();
            StudentRepository studentRepository = new StudentRepository();
            student = studentRepository.GetById(StudentID);
            TeacherControllerStudentsVM model = new TeacherControllerStudentsVM();
            studentGrades = gradeRepository.GetAll(filter: g => g.Student.Id == StudentID);

            model.FirstName = student.FirstName;
            model.LastName = student.LastName;
            model.FaculityNumber = student.FacultyNumber;
            model.Course = student.Course.Name;
            model.StudentID = student.Id;
            model.CourseID = student.Course.Id;

            Dictionary<string, List<Grade>> details = new Dictionary<string, List<Grade>>();
            List<string> subjectNameList = new List<string>();

            foreach (var item in studentGrades)
            {
                subjectNameList.Add(item.Subject.Name);
            }
            subjectNameList = subjectNameList.Distinct().ToList();

            foreach (var item in subjectNameList)
            {
                List<Grade> grades = new List<Grade>();
                grades = gradeRepository.GetAll(filter: s => s.Subject.Name == item && s.Student.Id == StudentID);
                details.Add(item, grades);
            }
            model.SubjectGradeList = details;

            List<Subject> subjects = new List<Subject>();
            CourseSubjectRepository courseSubjectRepo = new CourseSubjectRepository();
            List<CourseSubject> courseSubjectList = new List<CourseSubject>();
            courseSubjectList = courseSubjectRepo.GetAll(filter: c => c.CourseID == student.CourseID);
            foreach (var item in courseSubjectList)
            {
                subjects.Add(item.Subject);
            }
            model.SubjectList = subjects;
            return View(model);
        }
        public ActionResult ExportGrade(int studentId)
        {
            Student student = new Student();
            StudentRepository studentRepository = new StudentRepository();
            student = studentRepository.GetById(studentId);
            Export export = new Export();
            DataTable gradeTable = export.StudentsGradesToExcel(student);

            GridView Grid = new GridView();
            Grid.DataSource = gradeTable;
            Grid.DataBind();

            string filename = "attachment; filename=" + student.FirstName + "_" + student.LastName + "_" + student.FacultyNumber + ".xls";
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", filename);
            Response.ContentType = "application/ms-excel";

            Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            Grid.RenderControl(htw);

            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
            return View();
        }
        public ActionResult EditStudents(int id, AdminControllerStudentVM studentModel)
        {
            TryUpdateModel(studentModel);
            if (studentModel.CourseListItems == null)
            {
                CourseRepository courseRepository = new CourseRepository();
                List<SelectListItem> List = new List<SelectListItem>();
                studentModel.CourseList = courseRepository.GetAll();

                foreach (var item in studentModel.CourseList)
                {
                    List.Add(new SelectListItem() { Text = item.Name, Value = item.Id.ToString() });
                }
                studentModel.CourseListItems = List;
            }

            if (ModelState.IsValid)
            {
                StudentRepository studentRepository = new StudentRepository();
                Student student = null;
                if (id > 0)
                {
                    student = studentRepository.GetById(id);
                    if (student.Course.Id != studentModel.CourseID)
                    {
                        CourseRepository courseRepository = new CourseRepository();
                        int newCode = courseRepository.GetById(studentModel.CourseID).Code;
                        int oldCode = courseRepository.GetById(student.Course.Id).Code;
                        StringBuilder sb = new StringBuilder();
                        sb.Append(student.FacultyNumber.ToString());
                        sb.Replace(oldCode.ToString(), newCode.ToString());
                        student.FacultyNumber = Convert.ToInt32(sb.ToString());
                    }
                    student.FirstName = studentModel.FirstName;
                    student.LastName = studentModel.LastName;
                    student.UserName = studentModel.UserName;
                    student.Password = SecurityService.CreateHash(studentModel.Password);
                    student.IsActive = studentModel.isActive;
                    student.CourseID = studentModel.CourseID;
                    studentRepository.Save(student);
                    return RedirectToAction("ManageStudents");
                }
                else
                {
                    student = studentRepository.GetAll(filter: s => s.UserName == studentModel.UserName).FirstOrDefault();
                    if (student == null)
                    {
                        student = new Student();
                        student.FirstName = studentModel.FirstName;
                        student.LastName = studentModel.LastName;
                        student.UserName = studentModel.UserName;
                        student.Password = studentModel.Password;
                        student.IsActive = studentModel.isActive;
                        student.FacultyNumber = GenerateFaculityNumber(studentModel.CourseID);
                        student.CourseID = studentModel.CourseID;
                        studentRepository.Save(student);
                    }
                    else
                    {
                        throw new ArgumentException("Invalid username !");
                    }
                    return RedirectToAction("ManageStudents");
                }
            }
            return View(studentModel);
        }
        public ActionResult EditStudents(int id)
        {
            CourseRepository courseRepository = new CourseRepository();
            StudentRepository studentRepository = new StudentRepository();
            AdminControllerStudentVM studentModel = new AdminControllerStudentVM();
            Student student = new Student();
            List<SelectListItem> List = new List<SelectListItem>();
            studentModel.CourseList = courseRepository.GetAll();

            foreach (var item in studentModel.CourseList)
            {
                List.Add(new SelectListItem() { Text = item.Name, Value = item.Id.ToString() });
            }

            if (id > 0)
            {
                student = studentRepository.GetById(id);
                List = List.Where(c => c.Text != student.Course.Name).ToList();
                studentModel.FirstName = student.FirstName;
                studentModel.LastName = student.LastName;
                studentModel.UserName = student.UserName;
                studentModel.Password = student.Password;
                studentModel.FacultyNumber = student.FacultyNumber;
                studentModel.CourseID = student.CourseID;
                studentModel.isActive = student.IsActive;
                studentModel.SelectedCurse = student.Course.Name;
                studentModel.Id = id;
            }
            if (id == 0)
            {
                student.FirstName = studentModel.FirstName;
                student.LastName = studentModel.LastName;
                student.UserName = studentModel.UserName;
                student.Password = studentModel.Password;
                student.CourseID = studentModel.CourseID;
            }
            studentModel.CourseListItems = List;
            return View(studentModel);
        }
 public ActionResult DeleteStudent(int id)
 {
     Student student = new Student();
     StudentRepository studentRepository = new StudentRepository();
     student = studentRepository.GetById(id);
     student.IsActive = false;
     studentRepository.Save(student);
     return RedirectToAction("ManageStudents");
 }
        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 ShowGrades(int id, int courseSubjectID)
        {
            AdminControllerCourseSubjectVM courseSubjectModel = new AdminControllerCourseSubjectVM();
            GradeRepository gradeRepository = new GradeRepository();
            List<Grade> studentGrades = gradeRepository.GetAll(filter: s => s.Student.Id == id);

            Dictionary<string, List<Grade>> details = new Dictionary<string, List<Grade>>();
            List<string> subjectNameList = new List<string>();

            foreach (var item in studentGrades)
            {
                subjectNameList.Add(item.Subject.Name);
            }
            subjectNameList = subjectNameList.Distinct().ToList();

            foreach (var item in subjectNameList)
            {
                List<Grade> grades = new List<Grade>();
                grades = gradeRepository.GetAll(filter: s => s.Subject.Name == item && s.Student.Id == id);
                details.Add(item, grades);
            }

            courseSubjectModel.details = details;

            courseSubjectModel.StudentID = id;
            Student student = new Student();
            StudentRepository studentRepository = new StudentRepository();
            student = studentRepository.GetById(id);
            courseSubjectModel.StudentName = student.FirstName + " " + student.LastName;
            courseSubjectModel.CourseSubjectID = courseSubjectID;
            return View(courseSubjectModel);
        }
        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);
        }