public ActionResult Run()
        {
            if (Session["User"].ToString() == "Admin" || Session["User"].ToString() == "SuperAdmin")
            {
                if ((bool)HttpContext.Application["RecActive"] == false && (bool)HttpContext.Application["AfterRec"] == true)
                {
                    using (PPDBEntities db = new PPDBEntities())
                    {
                        DateTime saveTime = DateTime.Now;
                        foreach (var oneClassGroup in db.ClassGroups)
                        {
                            foreach (var oneCategory in db.Categories.Where(x => x.ClassGroupID == oneClassGroup.ClassGroupID))
                            {
                                var subjects = db.ElectiveSubjectsAndSpecialities.Where(x => x.CategoryID == oneCategory.CategoryID);

                                Dictionary <int, List <int> > algorithmChoices = AssignStudentToSubjectInOneCategory(subjects, oneCategory, oneClassGroup);
                                if (algorithmChoices == null)
                                {
                                    return(RedirectToAction("", "Home"));
                                }

                                //Zapisz wyniki do bazy
                                foreach (var resultSubject in algorithmChoices)
                                {
                                    foreach (var resultStudent in resultSubject.Value)
                                    {
                                        var checkIfFinalChoiceExists = db.FinalChoices.Where(x => x.StudentNo == resultStudent && x.CategoryID == oneCategory.CategoryID).FirstOrDefault();
                                        if (checkIfFinalChoiceExists != null)
                                        {
                                            if (checkIfFinalChoiceExists.ChoiceID != resultSubject.Key)
                                            {
                                                checkIfFinalChoiceExists.ChoiceID        = resultSubject.Key;
                                                checkIfFinalChoiceExists.LastEdit        = saveTime;
                                                checkIfFinalChoiceExists.LastEditedBy    = Int32.Parse(Session["AdminID"].ToString());
                                                db.Entry(checkIfFinalChoiceExists).State = EntityState.Modified;
                                            }
                                        }
                                        else
                                        {
                                            var newResult = new FinalChoices();
                                            newResult.StudentNo    = resultStudent;
                                            newResult.CategoryID   = oneCategory.CategoryID;
                                            newResult.ChoiceID     = resultSubject.Key;
                                            newResult.CreationDate = saveTime;
                                            newResult.CreatedBy    = Int32.Parse(Session["AdminID"].ToString());
                                            db.FinalChoices.Add(newResult);
                                        }
                                    }
                                }
                            }
                        }
                        db.SaveChanges();
                        TempData["Success"] = "Algorytm przydziału studentów zakończył się pomyślnie!";
                    }
                }
            }
            return(RedirectToAction("", "Home"));
        }
        public ActionResult SaveStudentChoices(string[] Subjects)
        {
            if (Session["User"].ToString() == "Student")
            {
                DateTime saveTime     = DateTime.Now;
                int      activeChoice = 0;
                int      StdNo        = Int32.Parse(Session["StudentNo"].ToString());
                using (PPDBEntities db = new PPDBEntities())
                {
                    var ClassGroups = db.StudentsAndClassGroups.Where(x => x.StudentNo == StdNo);
                    foreach (var oneClassGroup in ClassGroups)
                    {
                        var Categories = db.Categories.Where(x => x.ClassGroupID == oneClassGroup.ClassGroupID);
                        foreach (var Cat in Categories)
                        {
                            var findUserChoices = db.StudentChoices.Where(u => u.StudentNo == StdNo && u.CategoryID == Cat.CategoryID);

                            //if istnieje to nadpisz else dodaj do bazy
                            for (int i = 1; i <= Cat.MaxNoChoices; i++)
                            {
                                if (findUserChoices.Where(x => x.PreferenceNo == i).Count() == 1)
                                {
                                    var findChoice = findUserChoices.Where(x => x.PreferenceNo == i).FirstOrDefault();

                                    var newChoiceName = Subjects[activeChoice];
                                    int newChoiceID   = db.ElectiveSubjectsAndSpecialities.Where(x => x.Name == newChoiceName && x.CategoryID == Cat.CategoryID).Select(x => x.ElectiveSubjectAndSpecialityID).FirstOrDefault();
                                    if (findChoice.ChoiceID != newChoiceID)
                                    {
                                        findChoice.ChoiceID        = newChoiceID;
                                        findChoice.ChoiceDate      = saveTime;
                                        db.Entry(findChoice).State = EntityState.Modified;
                                    }
                                }
                                else
                                {
                                    var newChoice = new StudentChoices.Models.StudentChoices();
                                    newChoice.StudentNo  = StdNo;
                                    newChoice.CategoryID = Cat.CategoryID;
                                    var newChoiceName = Subjects[activeChoice];
                                    int newChoiceID   = db.ElectiveSubjectsAndSpecialities.Where(x => x.Name == newChoiceName && x.CategoryID == Cat.CategoryID).Select(x => x.ElectiveSubjectAndSpecialityID).FirstOrDefault();
                                    newChoice.ChoiceID     = newChoiceID;
                                    newChoice.PreferenceNo = Byte.Parse(i.ToString());
                                    newChoice.ChoiceDate   = saveTime;
                                    db.StudentChoices.Add(newChoice);
                                }
                                activeChoice++;
                            }
                        }
                    }
                    db.SaveChanges();
                    TempData["Success"] = "Zapisano wybory pomyślnie!";
                }
            }
            return(RedirectToAction("", "Home"));
        }
        public ActionResult Index(Users user)
        {
            using (PPDBEntities db = new PPDBEntities())
            {
                byte[] clientPassword = Encoding.ASCII.GetBytes(user.Password);

                //utworzenie skrotu od pobranego hasla (SHA256)
                using (var sha256 = SHA256.Create())
                {
                    byte[] clientPasswordSHA256 = sha256.ComputeHash(clientPassword);
                    user.Password = BitConverter.ToString(clientPasswordSHA256).Replace("-", string.Empty);;
                }

                //Sprawdzenie czy to admin
                var usrAdmin = db.Admins.Where(u => u.Login == user.Login &&
                                               u.Password == user.Password).FirstOrDefault();

                if (usrAdmin != null)
                {
                    //Sprawdzenie czy konto jest aktywne
                    if (usrAdmin.Active == true)
                    {
                        Session["UserName"] = usrAdmin.Login;
                        Session["AdminID"]  = usrAdmin.AdminID;
                        if (usrAdmin.SuperAdmin == true)
                        {
                            Session["User"] = "******";
                        }
                        else
                        {
                            Session["User"] = "******";
                        }
                        usrAdmin.LastLogin       = DateTime.Now;
                        db.Entry(usrAdmin).State = EntityState.Modified;
                        db.SaveChanges();

                        if (Session["ClassGroups"] == null)
                        {
                            setSessionClassGroups("");
                        }

                        return(RedirectToAction("", "Home"));
                    }
                    else
                    {
                        ViewBag.Alert = "Konto jest nieaktywne!";
                        return(View());
                    }
                }
                else
                {
                    //Sprawdzenie czy to student
                    var usrStudent = db.Students.Where(u => u.Login == user.Login).FirstOrDefault();

                    if (usrStudent != null)
                    {
                        //Sprawdzenie czy nie przekroczono limitu logowań
                        if (usrStudent.TriesNo < 3)
                        {
                            //Sprawdzenie poprawności hasła
                            if (usrStudent.Password == user.Password)
                            {
                                Session["UserName"]        = usrStudent.Login + " (" + usrStudent.Name + " " + usrStudent.Surname + ")";
                                Session["User"]            = "******";
                                Session["StudentNo"]       = usrStudent.StudentNo;
                                usrStudent.TriesNo         = 0;
                                db.Entry(usrStudent).State = EntityState.Modified;
                                db.SaveChanges();

                                if ((bool)HttpContext.Application["RecActive"] == true)
                                {
                                    var ChosenOptions = new Dictionary <string, string>();
                                    if (Session["Options"] == null)
                                    {
                                        Dictionary <string, Dictionary <ArrayList, Dictionary <List <List <string> >, SelectList> > > optionsAll = new Dictionary <string, Dictionary <ArrayList, Dictionary <List <List <string> >, SelectList> > >();
                                        Dictionary <ArrayList, Dictionary <List <List <string> >, SelectList> > optionsOneGroup;

                                        Dictionary <List <List <string> >, SelectList> optionsOneCategory;
                                        List <List <string> > optionsOneCategoryList;
                                        string oneClassGroupStr = string.Empty;

                                        var ClassGroups = db.StudentsAndClassGroups.Where(x => x.StudentNo == usrStudent.StudentNo);
                                        foreach (var oneClassGroup in ClassGroups)
                                        {
                                            optionsOneGroup = new Dictionary <ArrayList, Dictionary <List <List <string> >, SelectList> >();
                                            var Categories = db.Categories.Where(x => x.ClassGroupID == oneClassGroup.ClassGroupID);
                                            foreach (var Cat in Categories)
                                            {
                                                optionsOneCategory     = new Dictionary <List <List <string> >, SelectList>();
                                                optionsOneCategoryList = new List <List <string> >();
                                                var optionsOneCategorySubjects = db.ElectiveSubjectsAndSpecialities.Where(x => x.CategoryID == Cat.CategoryID);
                                                foreach (var Sub in optionsOneCategorySubjects)
                                                {
                                                    var SubInfo = new List <string>();
                                                    SubInfo.Add(Sub.Name);
                                                    SubInfo.Add(Sub.Information);
                                                    var files = String.Empty;
                                                    foreach (var file in db.Files.Where(x => x.ElectiveSubjectAndSpecialityID == Sub.ElectiveSubjectAndSpecialityID))
                                                    {
                                                        files += file.Filename + "\n" + file.Path + " ";
                                                    }
                                                    SubInfo.Add(files);
                                                    optionsOneCategoryList.Add(SubInfo);
                                                }

                                                optionsOneCategory[optionsOneCategoryList] = new SelectList(optionsOneCategorySubjects.Select(x => x.Name).ToList());

                                                var optionsOneGroupParams = new ArrayList();
                                                optionsOneGroupParams.Add(Cat.Name);
                                                optionsOneGroupParams.Add(Cat.MaxNoChoices);

                                                for (int i = 1; i <= Cat.MaxNoChoices; i++)
                                                {
                                                    ChosenOptions[Cat.Name + " " + i] = "";
                                                }

                                                optionsOneGroup[optionsOneGroupParams] = optionsOneCategory;
                                            }
                                            var ClassGroup = db.ClassGroups.Where(x => x.ClassGroupID == oneClassGroup.ClassGroupID).FirstOrDefault();

                                            oneClassGroupStr = ClassGroup.DegreeCourse.ToString() + ", " + ClassGroup.Graduate.ToString() + ". stopień, ";
                                            if (ClassGroup.FullTimeStudies == true)
                                            {
                                                oneClassGroupStr += "st. stacjonarne";
                                            }
                                            else
                                            {
                                                oneClassGroupStr += "st. niestacjonarne";
                                            }
                                            oneClassGroupStr += ", sem. " + ClassGroup.Semester.ToString() + "., " + ClassGroup.Speciality.ToString()
                                                                + ", średnia ocen: " + oneClassGroup.AverageGrade.ToString();

                                            optionsAll[oneClassGroupStr] = optionsOneGroup;
                                        }
                                        Session["Options"] = optionsAll;
                                    }

                                    var ChosenOptionsFromDB = db.StudentChoices.Where(x => x.StudentNo == usrStudent.StudentNo);
                                    foreach (var item in ChosenOptionsFromDB)
                                    {
                                        ChosenOptions[db.Categories.Where(x => x.CategoryID == item.CategoryID).FirstOrDefault().Name + " " + item.PreferenceNo] = db.ElectiveSubjectsAndSpecialities.Where(x => x.ElectiveSubjectAndSpecialityID == item.ChoiceID).FirstOrDefault().Name;
                                    }
                                    Session["ChosenOptions"] = ChosenOptions;
                                }
                                else if ((bool)HttpContext.Application["ShareResults"] == true)
                                {
                                    Dictionary <string, Dictionary <string, string> > resultsAll = new Dictionary <string, Dictionary <string, string> >();

                                    Dictionary <string, string> resultsOneGroup;
                                    string oneClassGroupStr = string.Empty;

                                    var ClassGroups = db.StudentsAndClassGroups.Where(x => x.StudentNo == usrStudent.StudentNo);
                                    foreach (var oneClassGroup in ClassGroups)
                                    {
                                        resultsOneGroup = new Dictionary <string, string>();
                                        var Categories = db.Categories.Where(x => x.ClassGroupID == oneClassGroup.ClassGroupID);
                                        foreach (var Cat in Categories)
                                        {
                                            var FinalChoiceID   = db.FinalChoices.Where(x => x.StudentNo == usrStudent.StudentNo && x.CategoryID == Cat.CategoryID).Select(x => x.ChoiceID).FirstOrDefault();
                                            var FinalChoiceName = db.ElectiveSubjectsAndSpecialities.Where(x => x.ElectiveSubjectAndSpecialityID == FinalChoiceID).Select(x => x.Name).FirstOrDefault();
                                            resultsOneGroup[Cat.Name] = FinalChoiceName;
                                        }
                                        var ClassGroup = db.ClassGroups.Where(x => x.ClassGroupID == oneClassGroup.ClassGroupID).FirstOrDefault();

                                        oneClassGroupStr = ClassGroup.DegreeCourse.ToString() + ", " + ClassGroup.Graduate.ToString() + ". stopień, ";
                                        if (ClassGroup.FullTimeStudies == true)
                                        {
                                            oneClassGroupStr += "st. stacjonarne";
                                        }
                                        else
                                        {
                                            oneClassGroupStr += "st. niestacjonarne";
                                        }
                                        oneClassGroupStr += ", sem. " + ClassGroup.Semester.ToString() + "., " + ClassGroup.Speciality.ToString()
                                                            + ", średnia ocen: " + oneClassGroup.AverageGrade.ToString();

                                        resultsAll[oneClassGroupStr] = resultsOneGroup;
                                    }


                                    Session["Results"] = resultsAll;
                                }
                                return(RedirectToAction("", "Home"));
                            }
                            else
                            {
                                usrStudent.TriesNo        += 1;
                                db.Entry(usrStudent).State = EntityState.Modified;
                                db.SaveChanges();
                            }
                        }
                        else
                        {
                            ViewBag.Alert = "Konto jest zablokowane - przekroczono liczbę błędnych logowań. Skontaktuj się z administratorem systemu!";
                            return(View());
                        }
                    }
                }
            }
            ViewBag.Alert = "Dane logowania są niepoprawne!";
            return(View());
        }