public ActionResult DeleteSubject(int id)
        {
            SubjectRepository subjectRepo = new SubjectRepository();
            SubjectDeleteSubjectVM model = new SubjectDeleteSubjectVM();

            Subject subject = subjectRepo.GetByID(id);

            model.SubjectID = subject.ID;
            model.SubjectName = subject.Name;

            return View(model);
        }
        private bool GetableSubject()
        {
            var routeData = context.RequestContext.RouteData;

            if (!routeData.Values.ContainsKey("id"))
            {
                return(false);
            }

            int id         = Int32.Parse(routeData.Values["id"].ToString());
            var repository = new SubjectRepository();

            entry = repository.GetByID(id);
            if (entry != null)
            {
                return(true);
            }
            return(false);
        }
        public ActionResult AddGrade(int studentID, int subjectID)
        {
            GradeAddGradeVM model = new GradeAddGradeVM();

            UserRepository<Student> sRepo = new UserRepository<Student>();
            SubjectRepository subjRepo = new SubjectRepository();
            GradeRepository gRepo = new GradeRepository();

            Student student = sRepo.GetAll(filter: s => s.ID == studentID && s.Course.CourseSubject.Any(sub => sub.SubjectID == subjectID)).FirstOrDefault();

            Subject subject = subjRepo.GetByID(subjectID);

            Grade grade = gRepo.GetAll(filter: g => g.StudentID == student.ID).FirstOrDefault();

            model.StudentID = student.ID;
            model.SubjectID = subject.ID;

            return View(model);
        }
        public ActionResult DeleteSubject(SubjectDeleteSubjectVM model)
        {
            SubjectRepository subjectRepo = new SubjectRepository();

            Subject subject = subjectRepo.GetByID(model.SubjectID);

            if (subject.CourseSubject.Count() > 0)
            {

                ModelState.AddModelError("Error", "Subject contains teacher or course");
                model.SubjectID = subject.ID;
                model.SubjectName = subject.Name;
                return View(model);
            }

            subjectRepo.Delete(subject);

            return RedirectToAction("ListSubjects", "Subject");
        }
Exemple #5
0
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (httpContext.User.IsInRole("admin"))
            {
                return(true);
            }
            if (!httpContext.User.IsInRole("teacher"))
            {
                return(false);
            }
            var subjectRepository = new SubjectRepository();

            var authorized = base.AuthorizeCore(httpContext);

            if (!authorized)
            {
                return(false);
            }

            var routeData = httpContext.Request.RequestContext.RouteData;

            if (!routeData.Values.ContainsKey("id"))
            {
                return(false);
            }

            int id       = Int32.Parse(routeData.Values["id"].ToString());
            var username = httpContext.User.Identity.Name;
            var model    = subjectRepository.GetByID(id);

            if (model == null)
            {
                return(false);
            }
            return(model.TeacherID == username);
        }
        public void ExportSubjectListToCSV(int id)
        {
            GradeStudentsGradesVM model = new GradeStudentsGradesVM();
            GradeRepository gradeRepo = new GradeRepository();
            SubjectRepository subjRepo = new SubjectRepository();
            UserRepository<Student> studentRepo = new UserRepository<Student>();

            List<Student> students = studentRepo.GetAll(filter: s => s.Course.CourseSubject.Any(t => t.SubjectID == id));
            List<Grade> grades = gradeRepo.GetAll(filter: s => s.SubjectID == id);
            Subject  subject = subjRepo.GetByID(id);

            StringWriter sw = new StringWriter();

            sw.WriteLine("\"Faculty number\",\"First name\",\"Last name\",\"Grade\"");

            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment;filename="+ subject.Name +".csv");
            Response.ContentType = "text/csv";

            foreach (var student in students)
            {
                string grade = student.Grades.Select(x => x.GradeValue).FirstOrDefault().ToString();
                grade = grade == "0" ? "" : grade;
                sw.WriteLine(string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\"",
                                           student.FacultiNumber,
                                           student.FirstName,
                                           student.LastName,
                                           grade));
            }

            Response.Write(sw.ToString());

            Response.End();
        }
        public ActionResult StudentsGrades(int id)
        {
            GradeStudentsGradesVM model = new GradeStudentsGradesVM();
            GradeRepository gradeRepo = new GradeRepository();
            SubjectRepository subjRepo = new SubjectRepository();
            UserRepository<Student> studentRepo = new UserRepository<Student>();

            model.Students = studentRepo.GetAll(filter: s => s.Course.CourseSubject.Any(t => t.SubjectID == id));

            Subject subject = subjRepo.GetByID(id);

            model.SubjectID = subject.ID;

            return View(model);
        }
        public void ExportSubjectListToExcel(int id)
        {
            GradeStudentsGradesVM model = new GradeStudentsGradesVM();
            GradeRepository gradeRepo = new GradeRepository();
            SubjectRepository subjRepo = new SubjectRepository();
            UserRepository<Student> studentRepo = new UserRepository<Student>();

            List<Student> students = studentRepo.GetAll(filter: s => s.Course.CourseSubject.Any(t => t.SubjectID == id));
            List<Grade> grades = gradeRepo.GetAll(filter: s => s.SubjectID == id);

            Subject subject = subjRepo.GetByID(id);

            var grid = new System.Web.UI.WebControls.GridView();

            var products = new System.Data.DataTable(subject.Name);

            products.Columns.Add("FacultiNumber", typeof(string));
            products.Columns.Add("FirstName", typeof(string));
            products.Columns.Add("LastName", typeof(string));
            products.Columns.Add("Grade", typeof(string));
            foreach (var student in students)
            {
                string grade = student.Grades.Select(x => x.GradeValue).FirstOrDefault().ToString();
                grade = grade == "0" ? "" : grade;
                products.Rows.Add(student.FacultiNumber, student.FirstName, student.LastName, grade);
            }

            grid.DataSource = products;
            // grid.DataSource = model.Students;

            grid.DataBind();

            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=" + subject.Name + ".xlsx");
            Response.ContentType = "application/excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            grid.RenderControl(htw);

            Response.Write(sw.ToString());

            Response.End();
        }
        public ActionResult ListStudentsBySubject(int id)
        {
            UserRepository<Student> stuRepo = new UserRepository<Student>();
            SubjectRepository subjectRepo = new SubjectRepository();
            StudentListStudentsBySubjectVM model = new StudentListStudentsBySubjectVM();
            Subject course = subjectRepo.GetByID(id);

            var students = stuRepo.GetAll(filter: s => s.Course.CourseSubject.Any(c => c.SubjectID == id));

            model.Students = students;
            model.SubjectName = course.Name;

            return View(model);
        }
        public ActionResult EditSubject(SubjectEditSubjectVM model)
        {
            if (!ModelState.IsValid)
            {
                return View();
            }

            SubjectRepository subjectRepo = new SubjectRepository();
            Subject subject = new Subject();

            if (model.SubjectID != 0)
            {
                subject = subjectRepo.GetByID(model.SubjectID);
            }

            subject.Name = model.SubjectName;

            subjectRepo.Save(subject);

            return RedirectToAction("ListSubjects", "Subject");
        }