public GradeValuesResult editGradeValue(int gradeId, int registrationId, string value, byte[] currentTimeStamp)
        {
            using (var db = new NTR2013Entities())
            {
                var original = db.GradeValues.Where(g => g.GradeID == gradeId && g.RegistrationID == registrationId).ToList().ElementAt(0);

                if (!compareTimeStamp(original.TimeStamp, currentTimeStamp))
                {
                    GradeValuesResult result = new GradeValuesResult(ErrorMessage.REMOVE_GRADES_CONCURRENCY);
                    GradeValues       grade  = new GradeValues {
                        GradeValueID = original.GradeValueID, GradeID = original.GradeID, RegistrationID = original.RegistrationID, Date = original.Date, Value = original.Value, TimeStamp = original.TimeStamp, Grades = null, Registrations = null
                    };
                    result.data = grade;
                    return(result);
                }

                //db.GradeValues.Remove(original);
                original.Value = value;
                db.SaveChanges();

                GradeValues current = new GradeValues {
                    GradeValueID = original.GradeValueID, GradeID = original.GradeID, RegistrationID = original.RegistrationID, Date = original.Date, Value = original.Value, TimeStamp = original.TimeStamp, Grades = null, Registrations = null
                };
                return(new GradeValuesResult(current));
            }
        }
 public List <Semesters> getSemesters()
 {
     using (var db = new NTR2013Entities())
     {
         return(db.Semesters.ToList());
     }
 }
 public List <Students> getStudents()
 {
     using (var db = new NTR2013Entities())
     {
         return(db.Students.ToList());
     }
 }
        public List <Semesters> getSemesters(int studentId)
        {
            using (var db = new NTR2013Entities())
            {
                List <Registrations> list   = db.Registrations.Where(r => r.StudentID == studentId).ToList();
                List <Semesters>     result = new List <Semesters>();

                foreach (Registrations r in list)
                {
                    Realisations realisation  = db.Realisations.Find(r.RealisationID);
                    bool         semesterFlag = false;

                    foreach (Semesters s in result)
                    {
                        if (s.SemesterID == realisation.SemesterID)
                        {
                            semesterFlag = true;
                            break;
                        }
                    }

                    if (!semesterFlag)
                    {
                        var original = db.Semesters.Find(realisation.SemesterID);
                        result.Add(new Semesters {
                            SemesterID = original.SemesterID, Name = original.Name, TimeStamp = original.TimeStamp, Realisations = null
                        });
                    }
                }

                return(result);
            }
        }
        public List <Subjects> getSubjects(int semesterId, int studentId)
        {
            using (var db = new NTR2013Entities())
            {
                //return db.Realisations.Include("Subjects").Where(b => b.SemesterID == semesterId).ToList();

                List <Registrations> list   = db.Registrations.Where(r => r.StudentID == studentId).ToList();
                List <Subjects>      result = new List <Subjects>();

                foreach (Registrations r in list)
                {
                    var realisations = db.Realisations.Where(re => re.RealisationID == r.RealisationID).ToList().ElementAt(0);

                    if (realisations.SemesterID == semesterId)
                    {
                        var original = db.Subjects.Find(realisations.SubjectID);
                        result.Add(new Subjects {
                            SubjectID = original.SubjectID, Name = original.Name, Conspect = original.Conspect, TimeStamp = original.TimeStamp, url = original.url, Realisations = null
                        });
                    }
                }

                return(result);
            }
        }
 private string getSubjectName(int id)
 {
     using (var db = new NTR2013Entities())
     {
         var original = db.Subjects.Find(id);
         return(original.Name);
     }
 }
 public Grades createGrade(int realisationId, string name, string maxValue)
 {
     using (var db = new NTR2013Entities())
     {
         var grade = new Grades { RealisationID = realisationId, Name = name, MaxValue = maxValue };
         db.Grades.Add(grade);
         db.SaveChanges();
         return grade;
     }
 }
 public Realisations createRealisation(int subjectId, int semesterId)
 {
     using (var db = new NTR2013Entities())
     {
         var realisation = new Realisations { SubjectID = subjectId, SemesterID = semesterId };
         db.Realisations.Add(realisation);
         db.SaveChanges();
         return realisation;
     }
 }
        public GradesResult removeGrades(int id, byte[] currentTimeStamp)
        {
            using (var db = new NTR2013Entities())
            {
                var original = db.Grades.Find(id);
                if (original != null)
                {
                    //Console.WriteLine("sfasdfasdf stamp" + Convert.ToString(original.TimeStamp));
                    if (!compareTimeStamp(original.TimeStamp, currentTimeStamp))
                    {
                        GradesResult result = new GradesResult(ErrorMessage.REMOVE_GRADES_CONCURRENCY);
                        Grades       grade  = new Grades {
                            GradeID = original.GradeID, RealisationID = original.RealisationID, Name = original.Name, MaxValue = original.MaxValue, TimeStamp = original.TimeStamp, Realisations = null, GradeValues = null
                        };
                        result.data = grade;
                        return(result);
                    }

                    List <GradeValues> list           = db.GradeValues.ToList();
                    string             studentsToEdit = "";

                    foreach (GradeValues g in list)
                    {
                        if (g.GradeID == id)
                        {
                            //Value 10 pleaces
                            if (g.Value.Equals("          "))
                            {
                                db.GradeValues.Remove(g);
                            }
                            else
                            {
                                //sprawdzenie studentow z ocena
                                var originalRegistration = db.Registrations.Find(g.RegistrationID);
                                var originalStudent      = db.Students.Find(originalRegistration.StudentID);
                                studentsToEdit += originalStudent.FirstName + " " + originalStudent.LastName + ", ";
                            }
                        }
                    }

                    if (!studentsToEdit.Equals(""))
                    {
                        studentsToEdit = studentsToEdit.Substring(0, studentsToEdit.Length - 2);
                        return(new GradesResult(studentsToEdit));
                    }

                    db.Grades.Remove(original);
                    db.SaveChanges();
                    return(new GradesResult(original));
                }

                return(new GradesResult(ErrorMessage.REMOVE_GRADES_COULD_NOT_FIND_GRADES_ID));
            }
        }
 public Realisations createRealisation(int subjectId, int semesterId)
 {
     using (var db = new NTR2013Entities())
     {
         var realisation = new Realisations {
             SubjectID = subjectId, SemesterID = semesterId
         };
         db.Realisations.Add(realisation);
         db.SaveChanges();
         return(realisation);
     }
 }
 public Grades createGrade(int realisationId, string name, string maxValue)
 {
     using (var db = new NTR2013Entities())
     {
         var grade = new Grades {
             RealisationID = realisationId, Name = name, MaxValue = maxValue
         };
         db.Grades.Add(grade);
         db.SaveChanges();
         return(grade);
     }
 }
        //GradeValues functions
        public GradeValuesResult addGradeValues(int gradeId, int registrationId, string value)
        {
            using (var db = new NTR2013Entities())
            {
                GradeValues grade = new GradeValues { GradeID = gradeId, RegistrationID = registrationId, Date = "2012-11-17", Value = value, Grades = null, Registrations = null, TimeStamp = null };
                GradeValues result = db.GradeValues.Add(grade);
                db.SaveChanges();
                result.Grades = null;
                result.Registrations = null;

                return new GradeValuesResult(result);
            }
        }
 public bool removeGrade(int id)
 {
     using (var db = new NTR2013Entities())
     {
         var original = db.Grades.Find(id);
         if (original != null)
         {
             db.Grades.Remove(original);
             db.SaveChanges();
             return(true);
         }
         return(false);
     }
 }
        public List <Grades> getGradeValues(int semesterId, int subjectId, int studentId)
        {
            using (var db = new NTR2013Entities())
            {
                //return db.Realisations.Include("Subjects").Where(b => b.SemesterID == semesterId).ToList();

                int realizationId = db.Realisations.Where(r => (r.SubjectID == subjectId && r.SemesterID == semesterId)).ToList().ElementAt(0).RealisationID;

                int registrationId = db.Registrations.Where(r => r.StudentID == studentId && r.RealisationID == realizationId).ToList().ElementAt(0).RegistrationID;


                //b => b.Posts.Select(p => p.Comments)
                var gradeList = db.Grades.Include("GradeValues").Where(g => g.RealisationID == realizationId).ToList();

                //var original = db.GradeValues.Include("Grades").Where(g => g.RegistrationID == registrationId).ToList();
                List <Grades> result = new List <Grades>();

                foreach (Grades grades in gradeList)
                {
                    bool flag = false;

                    foreach (GradeValues g in grades.GradeValues)
                    {
                        if (g.RegistrationID == registrationId)
                        {
                            ICollection <GradeValues> values = new List <GradeValues>();
                            values.Add(new GradeValues()
                            {
                                GradeValueID = g.GradeValueID, GradeID = g.GradeID, RegistrationID = g.RegistrationID, Value = g.Value, Date = g.Date, TimeStamp = g.TimeStamp, Grades = null, Registrations = null
                            });
                            result.Add(new Grades {
                                GradeID = grades.GradeID, Name = grades.Name, MaxValue = grades.MaxValue, RealisationID = grades.RealisationID, TimeStamp = grades.TimeStamp, Realisations = null, GradeValues = values
                            });
                            flag = true;
                            break;
                        }
                    }

                    if (!flag)
                    {
                        result.Add(new Grades {
                            GradeID = grades.GradeID, Name = grades.Name, MaxValue = grades.MaxValue, RealisationID = grades.RealisationID, TimeStamp = grades.TimeStamp, GradeValues = null, Realisations = null
                        });
                    }
                }


                return(result);
            }
        }
        //GradeValues functions

        public GradeValuesResult addGradeValues(int gradeId, int registrationId, string value)
        {
            using (var db = new NTR2013Entities())
            {
                GradeValues grade = new GradeValues {
                    GradeID = gradeId, RegistrationID = registrationId, Date = "2012-11-17", Value = value, Grades = null, Registrations = null, TimeStamp = null
                };
                GradeValues result = db.GradeValues.Add(grade);
                db.SaveChanges();
                result.Grades        = null;
                result.Registrations = null;

                return(new GradeValuesResult(result));
            }
        }
        public List <Realization> getRealizations()
        {
            using (var db = new NTR2013Entities())
            {
                List <Realization>  result = new List <Realization>();
                List <Realisations> list   = db.Realisations.ToList();

                foreach (Realisations r in list)
                {
                    result.Add(new Realization {
                        Id = r.RealisationID, Semester = this.getSemesterName(r.SemesterID), Subject = this.getSubjectName(r.SubjectID)
                    });
                }
                return(result);
            }
        }
        public List <Grades> getGrades(int id)
        {
            using (var db = new NTR2013Entities())
            {
                //var db = new NTR2013Entities();
                List <Grades> result = new List <Grades>();
                List <Grades> list   = db.Grades.ToList();

                foreach (Grades g in list)
                {
                    if (g.RealisationID == id)
                    {
                        result.Add(g);
                    }
                }
                return(result);
            }
        }
        public List<Grades> getGradeValues(int semesterId, int subjectId, int studentId)
        {
            using (var db = new NTR2013Entities())
            {
                //return db.Realisations.Include("Subjects").Where(b => b.SemesterID == semesterId).ToList();

                int realizationId = db.Realisations.Where(r => (r.SubjectID == subjectId && r.SemesterID == semesterId)).ToList().ElementAt(0).RealisationID;

                int registrationId = db.Registrations.Where(r => r.StudentID == studentId && r.RealisationID == realizationId).ToList().ElementAt(0).RegistrationID;

                //b => b.Posts.Select(p => p.Comments)
                var gradeList = db.Grades.Include("GradeValues").Where(g => g.RealisationID == realizationId).ToList();

                //var original = db.GradeValues.Include("Grades").Where(g => g.RegistrationID == registrationId).ToList();
                List<Grades> result = new List<Grades>();

                foreach (Grades grades in gradeList)
                {
                    bool flag = false;

                    foreach (GradeValues g in grades.GradeValues)
                    {
                        if (g.RegistrationID == registrationId)
                        {
                            ICollection<GradeValues> values = new List<GradeValues>();
                            values.Add(new GradeValues() { GradeValueID = g.GradeValueID, GradeID = g.GradeID, RegistrationID = g.RegistrationID, Value = g.Value, Date = g.Date, TimeStamp = g.TimeStamp, Grades = null, Registrations = null });
                            result.Add(new Grades { GradeID = grades.GradeID, Name = grades.Name, MaxValue = grades.MaxValue, RealisationID = grades.RealisationID, TimeStamp = grades.TimeStamp, Realisations = null, GradeValues = values });
                            flag = true;
                            break;
                        }
                    }

                    if (!flag)
                    {
                        result.Add(new Grades { GradeID = grades.GradeID, Name = grades.Name, MaxValue = grades.MaxValue, RealisationID = grades.RealisationID, TimeStamp = grades.TimeStamp, GradeValues = null, Realisations = null });
                    }

                }

                return result;

            }
        }
        public Semesters createSemester(string name)
        {
            using (var db = new NTR2013Entities())
            {
                var original = db.Semesters.ToList();
                foreach (Semesters s in original)
                {

                    if (s.Name.Trim().Equals(name))
                        return null;
                }

                var semester = new Semesters { Name = name };
                db.Semesters.Add(semester);
                db.SaveChanges();
                return semester;

            }
        }
        //dodaje ocene do realizacji
        //szuka realizacji
        //sprawdza czy name jest unikatowe w realizacji
        public GradesResult addGrades(int realizationId, string name, string maxValue)
        {
            using (var db = new NTR2013Entities())
            {
                var original = db.Realisations.Find(realizationId);

                /*
                 * List<Realisations> i = db.Realisations.ToList();
                 * bool f = false;
                 * foreach (Realisations r in i)
                 * {
                 *  if (r.RealisationID == realizationId)
                 *  {
                 *      f = true;
                 *      break;
                 *  }
                 * }
                 */
                //
                if (original == null)
                {
                    return(new GradesResult(ErrorMessage.ADD_GRADES_COULD_NOT_FIND_REALIZATIONS_ID));
                }

                List <Grades> list = getGrades(realizationId);
                foreach (Grades g in list)
                {
                    if (g.Name.Equals(name))
                    {
                        return(new GradesResult(ErrorMessage.ADD_GRADES_NAME_EXISTS));
                    }
                }

                Grades grade = new Grades {
                    RealisationID = realizationId, Name = name, MaxValue = maxValue, Realisations = null
                };
                Grades result = db.Grades.Add(grade);
                db.SaveChanges();
                result.Realisations = null;

                return(new GradesResult(result));
            }
        }
        //w danym semestrze jedna realizacja przedmiotu
        //student jest raz zapisany na dana realizacje
        public List <Students> getStudents(int semesterId, int subjectId)
        {
            using (var db = new NTR2013Entities())
            {
                int realisationId = db.Realisations.Where(r => r.SemesterID == semesterId && r.SubjectID == subjectId).ToList().ElementAt(0).RealisationID;

                List <Registrations> items  = db.Registrations.Where(r => r.RealisationID == realisationId).ToList();
                List <Students>      result = new List <Students>();

                foreach (Registrations current in items)
                {
                    var o = db.Students.Find(current.StudentID);
                    result.Add(new Students {
                        StudentID = o.StudentID, FirstName = o.FirstName, LastName = o.LastName, IndexNo = o.IndexNo, GroupID = o.GroupID, TimeStamp = o.TimeStamp, Groups = null, Registrations = null
                    });
                }

                return(result);
            }
        }
        public Subjects createSubject(string name, string conspect, string url)
        {
            using (var db = new NTR2013Entities())
            {
                var original = db.Subjects.ToList();
                foreach (Subjects s in original)
                {
                    if (s.Name.Trim().Equals(name))
                    {
                        return(null);
                    }
                }

                var subject = new Subjects {
                    Name = name, Conspect = conspect, url = url
                };
                db.Subjects.Add(subject);
                db.SaveChanges();
                return(subject);
            }
        }
        public Semesters createSemester(string name)
        {
            using (var db = new NTR2013Entities())
            {
                var original = db.Semesters.ToList();
                foreach (Semesters s in original)
                {
                    if (s.Name.Trim().Equals(name))
                    {
                        return(null);
                    }
                }

                var semester = new Semesters {
                    Name = name
                };
                db.Semesters.Add(semester);
                db.SaveChanges();
                return(semester);
            }
        }
        public GradeValuesResult editGradeValue(int gradeId, int registrationId, string value, byte[] currentTimeStamp)
        {
            using (var db = new NTR2013Entities())
            {
                var original = db.GradeValues.Where(g => g.GradeID == gradeId && g.RegistrationID == registrationId).ToList().ElementAt(0);

                if (!compareTimeStamp(original.TimeStamp, currentTimeStamp))
                {
                    GradeValuesResult result = new GradeValuesResult(ErrorMessage.REMOVE_GRADES_CONCURRENCY);
                    GradeValues grade = new GradeValues { GradeValueID = original.GradeValueID, GradeID = original.GradeID, RegistrationID = original.RegistrationID, Date = original.Date, Value = original.Value, TimeStamp = original.TimeStamp, Grades = null, Registrations = null };
                    result.data = grade;
                    return result;
                }

                //db.GradeValues.Remove(original);
                original.Value = value;
                db.SaveChanges();

                GradeValues current = new GradeValues { GradeValueID = original.GradeValueID, GradeID = original.GradeID, RegistrationID = original.RegistrationID, Date = original.Date, Value = original.Value, TimeStamp = original.TimeStamp, Grades = null, Registrations = null };
                return new GradeValuesResult(current);
            }
        }
        //dodaje ocene do realizacji
        //szuka realizacji
        //sprawdza czy name jest unikatowe w realizacji
        public GradesResult addGrades(int realizationId, string name, string maxValue)
        {
            using (var db = new NTR2013Entities())
            {
                var original = db.Realisations.Find(realizationId);
                /*
                List<Realisations> i = db.Realisations.ToList();
                bool f = false;
                foreach (Realisations r in i)
                {
                    if (r.RealisationID == realizationId)
                    {
                        f = true;
                        break;
                    }
                }
                */
                //
                if (original == null)
                    return new GradesResult(ErrorMessage.ADD_GRADES_COULD_NOT_FIND_REALIZATIONS_ID);

                List<Grades> list = getGrades(realizationId);
                foreach (Grades g in list)
                {
                    if (g.Name.Equals(name))
                        return new GradesResult(ErrorMessage.ADD_GRADES_NAME_EXISTS);
                }

                Grades grade = new Grades { RealisationID = realizationId, Name = name, MaxValue = maxValue, Realisations = null };
                Grades result = db.Grades.Add(grade);
                db.SaveChanges();
                result.Realisations = null;

                return new GradesResult(result);
            }
        }
        //w danym semestrze jedna realizacja przedmiotu
        //student jest raz zapisany na dana realizacje
        public List<Students> getStudents(int semesterId, int subjectId)
        {
            using (var db = new NTR2013Entities())
            {
                int realisationId = db.Realisations.Where(r => r.SemesterID == semesterId && r.SubjectID == subjectId).ToList().ElementAt(0).RealisationID;

                List<Registrations> items = db.Registrations.Where(r => r.RealisationID == realisationId).ToList();
                List<Students> result = new List<Students>();

                foreach (Registrations current in items)
                {
                    var o = db.Students.Find(current.StudentID);
                    result.Add(new Students { StudentID = o.StudentID, FirstName = o.FirstName, LastName = o.LastName, IndexNo = o.IndexNo, GroupID = o.GroupID, TimeStamp = o.TimeStamp, Groups = null, Registrations = null });

                }

                return result;
            }
        }
 public List<Students> getStudents()
 {
     using (var db = new NTR2013Entities())
     {
         return db.Students.ToList();
     }
 }
 private string getSubjectName(int id)
 {
     using (var db = new NTR2013Entities())
     {
         var original = db.Subjects.Find(id);
         return original.Name;
     }
 }
 public bool removeSubject(int id)
 {
     using (var db = new NTR2013Entities())
     {
         var original = db.Subjects.Find(id);
         if (original != null)
         {
             db.Subjects.Remove(original);
             db.SaveChanges();
             return true;
         }
         return false;
     }
 }
        public GradesResult removeGrades(int id, byte[] currentTimeStamp)
        {
            using (var db = new NTR2013Entities())
            {
                var original = db.Grades.Find(id);
                if (original != null)
                {
                    //Console.WriteLine("sfasdfasdf stamp" + Convert.ToString(original.TimeStamp));
                    if (!compareTimeStamp(original.TimeStamp, currentTimeStamp))
                    {
                        GradesResult result = new GradesResult(ErrorMessage.REMOVE_GRADES_CONCURRENCY);
                        Grades grade = new Grades { GradeID = original.GradeID, RealisationID = original.RealisationID, Name = original.Name, MaxValue = original.MaxValue, TimeStamp = original.TimeStamp, Realisations = null, GradeValues = null };
                        result.data = grade;
                        return result;
                    }

                    List<GradeValues> list = db.GradeValues.ToList();
                    string studentsToEdit = "";

                    foreach (GradeValues g in list)
                    {
                        if (g.GradeID == id)
                        {
                            //Value 10 pleaces
                            if (g.Value.Equals("          "))
                                db.GradeValues.Remove(g);
                            else
                            {
                                //sprawdzenie studentow z ocena
                                var originalRegistration = db.Registrations.Find(g.RegistrationID);
                                var originalStudent = db.Students.Find(originalRegistration.StudentID);
                                studentsToEdit += originalStudent.FirstName + " " + originalStudent.LastName + ", ";
                            }
                        }
                    }

                    if (!studentsToEdit.Equals(""))
                    {
                        studentsToEdit = studentsToEdit.Substring(0, studentsToEdit.Length - 2);
                        return new GradesResult(studentsToEdit);
                    }

                    db.Grades.Remove(original);
                    db.SaveChanges();
                    return new GradesResult(original);
                }

                return new GradesResult(ErrorMessage.REMOVE_GRADES_COULD_NOT_FIND_GRADES_ID);
            }
        }
        public List<Realization> getRealizations()
        {
            using (var db = new NTR2013Entities())
            {
                List<Realization> result = new List<Realization>();
                List<Realisations> list = db.Realisations.ToList();

                foreach (Realisations r in list)
                {
                    result.Add(new Realization { Id = r.RealisationID, Semester = this.getSemesterName(r.SemesterID), Subject = this.getSubjectName(r.SubjectID) });
                }
                return result;
            }
        }
        public List<Grades> getGrades(int id)
        {
            using (var db = new NTR2013Entities())
            {
                //var db = new NTR2013Entities();
                List<Grades> result = new List<Grades>();
                List<Grades> list = db.Grades.ToList();

                foreach (Grades g in list)
                {
                    if (g.RealisationID == id)
                        result.Add(g);
                }
                return result;
            }
        }
        public GradesResult editGrades(int gradeId, string name, string maxValue, byte[] currentTimeStamp)
        {
            using (var db = new NTR2013Entities())
            {
                var original = db.Grades.Find(gradeId);
                if (original != null)
                {
                    if (!compareTimeStamp(original.TimeStamp, currentTimeStamp))
                    {
                        GradesResult result = new GradesResult(ErrorMessage.REMOVE_GRADES_CONCURRENCY);
                        Grades g = new Grades { GradeID = original.GradeID, RealisationID = original.RealisationID, Name = original.Name, MaxValue = original.MaxValue, TimeStamp = original.TimeStamp, Realisations = null, GradeValues = null };
                        result.data = g;
                        return result;
                    }

                    List<Grades> list = getGrades(original.RealisationID);
                    foreach (Grades g in list)
                    {
                        if (g.Name.Equals(name))
                            return new GradesResult(ErrorMessage.EDIT_GRADES_NAME_EXISTS);
                    }

                    if (!original.MaxValue.Equals(maxValue))
                    {
                        List<GradeValues> l = db.GradeValues.ToList();
                        string studentsToEdit = "";

                        foreach (GradeValues g in l)
                        {
                            if (g.GradeID == original.GradeID)
                            {
                                //Value 10 pleaces
                                if (!g.Value.Equals("          "))
                                {
                                    //sprawdzenie studentow z ocena
                                    var originalRegistration = db.Registrations.Find(g.RegistrationID);
                                    var originalStudent = db.Students.Find(originalRegistration.StudentID);
                                    studentsToEdit += originalStudent.FirstName + " " + originalStudent.LastName + ", ";
                                }
                            }
                        }

                        if (!studentsToEdit.Equals(""))
                        {
                            studentsToEdit = studentsToEdit.Substring(0, studentsToEdit.Length - 2);
                            return new GradesResult(studentsToEdit);
                        }
                    }

                    original.Name = name;
                    original.MaxValue = maxValue;
                    //original.TimeStamp = null;
                    db.SaveChanges();

                    Grades grade = new Grades { GradeID = original.GradeID, RealisationID = original.RealisationID, Name = original.Name, MaxValue = original.MaxValue, TimeStamp = original.TimeStamp, Realisations = null, GradeValues = null };

                    return new GradesResult(grade);
                }

                return new GradesResult(ErrorMessage.EDIT_GRADES_COULD_NOT_FIND_GRADES_ID);
            }
        }
 public List<Semesters> getSemesters()
 {
     using (var db = new NTR2013Entities())
     {
         return db.Semesters.ToList();
     }
 }
        public List<Subjects> getSubjects(int semesterId, int studentId)
        {
            using (var db = new NTR2013Entities())
            {
                //return db.Realisations.Include("Subjects").Where(b => b.SemesterID == semesterId).ToList();

                List<Registrations> list = db.Registrations.Where(r => r.StudentID == studentId).ToList();
                List<Subjects> result = new List<Subjects>();

                foreach (Registrations r in list)
                {
                    var realisations = db.Realisations.Where(re => re.RealisationID == r.RealisationID).ToList().ElementAt(0);

                        if (realisations.SemesterID == semesterId)
                        {
                            var original = db.Subjects.Find(realisations.SubjectID);
                            result.Add(new Subjects { SubjectID = original.SubjectID, Name = original.Name, Conspect = original.Conspect, TimeStamp = original.TimeStamp, url = original.url, Realisations = null });

                        }
                }

                return result;
            }
        }
        public GradesResult editGrades(int gradeId, string name, string maxValue, byte[] currentTimeStamp)
        {
            using (var db = new NTR2013Entities())
            {
                var original = db.Grades.Find(gradeId);
                if (original != null)
                {
                    if (!compareTimeStamp(original.TimeStamp, currentTimeStamp))
                    {
                        GradesResult result = new GradesResult(ErrorMessage.REMOVE_GRADES_CONCURRENCY);
                        Grades       g      = new Grades {
                            GradeID = original.GradeID, RealisationID = original.RealisationID, Name = original.Name, MaxValue = original.MaxValue, TimeStamp = original.TimeStamp, Realisations = null, GradeValues = null
                        };
                        result.data = g;
                        return(result);
                    }

                    List <Grades> list = getGrades(original.RealisationID);
                    foreach (Grades g in list)
                    {
                        if (g.Name.Equals(name))
                        {
                            return(new GradesResult(ErrorMessage.EDIT_GRADES_NAME_EXISTS));
                        }
                    }

                    if (!original.MaxValue.Equals(maxValue))
                    {
                        List <GradeValues> l = db.GradeValues.ToList();
                        string             studentsToEdit = "";

                        foreach (GradeValues g in l)
                        {
                            if (g.GradeID == original.GradeID)
                            {
                                //Value 10 pleaces
                                if (!g.Value.Equals("          "))
                                {
                                    //sprawdzenie studentow z ocena
                                    var originalRegistration = db.Registrations.Find(g.RegistrationID);
                                    var originalStudent      = db.Students.Find(originalRegistration.StudentID);
                                    studentsToEdit += originalStudent.FirstName + " " + originalStudent.LastName + ", ";
                                }
                            }
                        }

                        if (!studentsToEdit.Equals(""))
                        {
                            studentsToEdit = studentsToEdit.Substring(0, studentsToEdit.Length - 2);
                            return(new GradesResult(studentsToEdit));
                        }
                    }

                    original.Name     = name;
                    original.MaxValue = maxValue;
                    //original.TimeStamp = null;
                    db.SaveChanges();

                    Grades grade = new Grades {
                        GradeID = original.GradeID, RealisationID = original.RealisationID, Name = original.Name, MaxValue = original.MaxValue, TimeStamp = original.TimeStamp, Realisations = null, GradeValues = null
                    };


                    return(new GradesResult(grade));
                }

                return(new GradesResult(ErrorMessage.EDIT_GRADES_COULD_NOT_FIND_GRADES_ID));
            }
        }
        public Subjects createSubject(string name, string conspect, string url)
        {
            using (var db = new NTR2013Entities())
            {
                var original = db.Subjects.ToList();
                foreach (Subjects s in original)
                {

                    if (s.Name.Trim().Equals(name))
                        return null;
                }

                var subject = new Subjects { Name = name, Conspect = conspect, url = url };
                db.Subjects.Add(subject);
                db.SaveChanges();
                return subject;
            }
        }
        public List<Semesters> getSemesters(int studentId)
        {
            using (var db = new NTR2013Entities())
            {
                List<Registrations> list = db.Registrations.Where(r => r.StudentID == studentId).ToList();
                List<Semesters> result = new List<Semesters>();

                foreach (Registrations r in list)
                {
                    Realisations realisation = db.Realisations.Find(r.RealisationID);
                    bool semesterFlag = false;

                    foreach(Semesters s in result)
                    {
                        if (s.SemesterID == realisation.SemesterID)
                        {
                            semesterFlag = true;
                            break;
                        }
                    }

                    if (!semesterFlag)
                    {
                        var original = db.Semesters.Find(realisation.SemesterID);
                        result.Add(new Semesters { SemesterID = original.SemesterID, Name = original.Name, TimeStamp = original.TimeStamp, Realisations = null });
                    }

                }

                return result;

            }
        }