Exemplo n.º 1
0
        public IActionResult CreateAccount(CreateAccountVm vm)
        {
            if (ModelState.IsValid)
            {
                User user = new User()
                {
                    Username = vm.Username,
                    Password = GetPasswordHash(vm.Password)
                };

                using (var context = new DegreePlannerContext()) {
                    if (context.Users.Any(u => u.Username == vm.Username))
                    {
                        ModelState.AddModelError("Username", "Username already exists");
                        return(View(vm));
                    }

                    context.Users.Add(user);
                    context.SaveChanges();

                    user = context.Users.FirstOrDefault(u => u.Username == user.Username && u.Password == user.Password);
                }
                HttpContext.Session.SetInt32(USERNAME, user.UserID);
                return(RedirectToAction("Index", "Planner"));
            }
            return(View(vm));
        }
Exemplo n.º 2
0
        public IActionResult UploadCourseGroup(FileUploadVm vm)
        {
            if (!Authenticate())
            {
                return(RedirectToAction("Index", "Home"));
            }
            var user = GetCurrentlyLoggedInUser();

            if (!user.IsAdmin)
            {
                return(RedirectToAction("Index", "Planner"));
            }

            if (!ModelState.IsValid)
            {
                return(View(vm));
            }

            using (var context = new DegreePlannerContext())
                using (var stream = vm.File.OpenReadStream())
                    using (var reader = new StreamReader(stream)) {
                        string line;
                        while ((line = reader.ReadLine()) != null)
                        {
                            var data = line.Split(',');

                            var name        = data[0];
                            var courseGroup = new CourseGroup()
                            {
                                Name = name,
                                CourseCourseGroupLinks = new List <CourseCourseGroupLink>()
                            };
                            for (var i = 1; i < data.Length; i++)
                            {
                                var link       = new CourseCourseGroupLink();
                                var department = data[i].Substring(0, 4);
                                var number     = data[i].Substring(4);
                                var course     = CreateOrFetchCourse(context, department, number, out var gen);

                                link.Course = course;

                                courseGroup.CourseCourseGroupLinks.Add(link);
                            }

                            context.CourseGroups.Add(courseGroup);
                            context.SaveChanges();
                        }
                    }

            return(RedirectToAction("Index", "Planner"));
        }
Exemplo n.º 3
0
        public JsonResult AddCourseTaken(string department, string catalog)
        {
            if (!Authenticate())
            {
                return(Json(""));
            }
            var user = GetCurrentlyLoggedInUser();

            if (string.IsNullOrEmpty(department) || string.IsNullOrEmpty(catalog))
            {
                return(Json("false"));
            }

            using (var context = new DegreePlannerContext()) {
                var hasCourseAlready = context.Users
                                       .Include(u => u.CourseUserLinks)
                                       .ThenInclude(cul => cul.Course)
                                       .FirstOrDefault(u => u.UserID == user.UserID)
                                       ?.Courses
                                       .Any(c => c.Department == department && c.CatalogNumber == catalog);

                if (hasCourseAlready == null || hasCourseAlready.Value)
                {
                    return(GetCoursesTaken());
                }
                var course = CreateOrFetchCourse(context, department, catalog, out var generated);

                if (!generated)
                {
                    var link = new CourseUserLink()
                    {
                        User   = user,
                        Course = course
                    };

                    context.CourseUserLinks.Add(link);
                    context.SaveChanges();
                }
                else
                {
                    return(Json("false"));
                }
            }

            return(GetCoursesTaken());
        }
Exemplo n.º 4
0
        public JsonResult ToggleAdmin(int userID, bool isAdmin)
        {
            if (!Authenticate())
            {
                return(Json(false));
            }
            var user = GetCurrentlyLoggedInUser();

            if (!user.IsAdmin)
            {
                return(Json(false));
            }

            using (var context = new DegreePlannerContext()) {
                var toEdit = context.Users.FirstOrDefault(u => u.UserID == userID);
                toEdit.IsAdmin = isAdmin;
                context.SaveChanges();

                return(Json(true));
            }
        }
Exemplo n.º 5
0
        public JsonResult RemoveCourseTaken(string department, string catalog)
        {
            if (!Authenticate())
            {
                return(Json(""));
            }
            var user = GetCurrentlyLoggedInUser();

            if (string.IsNullOrEmpty(department) || string.IsNullOrEmpty(catalog))
            {
                return(Json("false"));
            }

            using (var context = new DegreePlannerContext()) {
                var hasCourseAlready = context.Users
                                       .Include(u => u.CourseUserLinks)
                                       .ThenInclude(cul => cul.Course)
                                       .FirstOrDefault(u => u.UserID == user.UserID)
                                       ?.Courses
                                       .Any(c => c.Department == department && c.CatalogNumber == catalog);

                if (hasCourseAlready == null || !hasCourseAlready.Value)
                {
                    return(GetCoursesTaken());
                }

                context.Users.Include(u => u.CourseUserLinks).ThenInclude(cul => cul.Course)
                .FirstOrDefault(u => u.UserID == user.UserID)
                ?.CourseUserLinks.Remove(
                    context.CourseUserLinks.FirstOrDefault(c =>
                                                           c.UserID == user.UserID && c.Course.Department == department &&
                                                           c.Course.CatalogNumber == catalog));
                context.SaveChanges();
            }

            return(GetCoursesTaken());
        }
Exemplo n.º 6
0
        public IActionResult UploadDegree(FileUploadVm vm)
        {
            if (!Authenticate())
            {
                return(RedirectToAction("Index", "Home"));
            }
            var user = GetCurrentlyLoggedInUser();

            if (!user.IsAdmin)
            {
                return(RedirectToAction("Index", "Planner"));
            }

            if (!ModelState.IsValid)
            {
                return(View(vm));
            }

            var degree = new Degree {
                Name         = vm.Name,
                Requirements = new List <DegreeElement>()
            };

            using (var context = new DegreePlannerContext())
                using (var stream = vm.File.OpenReadStream())
                    using (var reader = new StreamReader(stream)) {
                        context.Degrees.Add(degree);
                        context.SaveChanges();

                        string line;
                        while ((line = reader.ReadLine()) != null)
                        {
                            var data = line.Split(',');

                            var element = new DegreeElement();
                            element.Hours = int.Parse(data[0]);
                            var courseGroupName = data[1];
                            var courseGroup     = context.CourseGroups.FirstOrDefault(cg => cg.Name == courseGroupName);
                            if (data.Length == 2)
                            {
                                if (courseGroup == null)
                                {
                                    courseGroup = new CourseGroup()
                                    {
                                        Name = courseGroupName
                                    }
                                }
                                ;
                            }
                            else
                            {
                                if (courseGroup == null)
                                {
                                    courseGroup = new CourseGroup()
                                    {
                                        Name = courseGroupName,
                                        CourseCourseGroupLinks = new List <CourseCourseGroupLink>()
                                    };

                                    for (var i = 2; i < data.Length; i++)
                                    {
                                        var link       = new CourseCourseGroupLink();
                                        var department = data[i].Substring(0, 4);
                                        var number     = data[i].Substring(4);
                                        var course     = CreateOrFetchCourse(context, department, number, out var gen);

                                        link.Course = course;
                                        courseGroup.CourseCourseGroupLinks.Add(link);
                                    }
                                }
                            }
                            element.Members = courseGroup;
                            element.Degree  = degree;
                            context.DegreeElements.Add(element);
                            context.SaveChanges();
                        }
                    }

            return(RedirectToAction("Index", "Planner"));
        }
Exemplo n.º 7
0
        public IActionResult UploadCourseList(FileUploadVm vm)
        {
            if (!Authenticate())
            {
                return(RedirectToAction("Index", "Home"));
            }
            var user = GetCurrentlyLoggedInUser();

            if (!user.IsAdmin)
            {
                return(RedirectToAction("Index", "Planner"));
            }

            if (!ModelState.IsValid)
            {
                return(View(vm));
            }

            using (var context = new DegreePlannerContext())
                using (var stream = vm.File.OpenReadStream())
                    using (var reader = new StreamReader(stream)) {
                        string line;
                        while ((line = reader.ReadLine()) != null)
                        {
                            var data = line.Split(',');

                            var department    = data[0].Substring(0, 4);
                            var catalogNumber = data[0].Substring(4);

                            var course = CreateOrFetchCourse(context, department, catalogNumber, out var gen, true);
                            var start  = 1;
                            var name   = data[1];
                            if (name[0] == '"')
                            {
                                start = 2;
                                while (!name.EndsWith("\""))
                                {
                                    name += data[start];
                                    start++;
                                }
                                course.Name = name.Substring(1, name.Length - 2);
                            }

                            if (gen)
                            {
                                context.Courses.Add(course);
                            }
                            else
                            {
                                context.Courses.Update(course);
                            }

                            var save = gen;

                            for (var i = start; i < data.Length; i++)
                            {
                                var prereqDepartment = data[i].Substring(0, 4);
                                var prereqCatalog    = data[i].Substring(4);

                                var prereq = CreateOrFetchCourse(context, prereqDepartment, prereqCatalog, out var genPrereq, true);
                                if (genPrereq || gen)
                                {
                                    var link = new PrerequisiteLink()
                                    {
                                        Prerequisite = prereq,
                                        Course       = course
                                    };
                                    save = true;
                                    if (genPrereq)
                                    {
                                        context.Courses.Add(prereq);
                                    }
                                    context.PrerequisiteLinks.Add(link);
                                }
                                else if (!context.PrerequisiteLinks.Any(p => p.CourseID == course.CourseID && p.PrerequisiteID == prereq.CourseID))
                                {
                                    var link = new PrerequisiteLink()
                                    {
                                        Prerequisite = prereq,
                                        Course       = course
                                    };

                                    context.PrerequisiteLinks.Add(link);
                                    save = true;
                                }
                            }

                            if (save)
                            {
                                context.SaveChanges();
                            }
                        }

                        context.SaveChanges();
                    }
            return(RedirectToAction("Index", "Planner"));
        }
Exemplo n.º 8
0
        public IActionResult ViewDegreePlan(DegreePlanFormSubmissionVm data)
        {
            if (!Authenticate())
            {
                return(RedirectToAction("Index", "Home"));
            }
            User user = GetCurrentlyLoggedInUser();

            // only doing front end validation for now.
            //TODO fix later.
            using (var context = new DegreePlannerContext()) {
                //Figure out what courses i need to take to graduate
                IList <Course> coursesTaken = context.Users
                                              .Include(u => u.CourseUserLinks)
                                              .ThenInclude(cul => cul.Course)
                                              .ThenInclude(c => c.PrerequisiteLinks)
                                              .ThenInclude(pl => pl.Prerequisite)
                                              .FirstOrDefault(u => u.UserID == user.UserID)
                                              ?.Courses
                                              .ToList();

                IList <Course> coursesToTake = new List <Course>();
                foreach (var courseData in data.Courses)
                {
                    if (coursesTaken.All(c => c.CourseID != courseData.CourseID) && coursesToTake.All(c => c.CourseID != courseData.CourseID))
                    {
                        var course = CreateOrFetchCourse(context, courseData.Department, courseData.CatalogNumber,
                                                         out _, true);
                        if (course.Hours == -1)
                        {
                            course.Hours = courseData.Hours;
                        }
                        coursesToTake.Add(course);
                    }
                }

                var degreeRequiredCourses = context.Degrees
                                            .Include(d => d.Requirements)
                                            .ThenInclude(de => de.Members)
                                            .ThenInclude(cg => cg.CourseCourseGroupLinks)
                                            .ThenInclude(ccgl => ccgl.Course)
                                            .ThenInclude(c => c.PrerequisiteLinks)
                                            .ThenInclude(pl => pl.Prerequisite)
                                            .FirstOrDefault(d => d.DegreeID == data.DegreeID)
                                            ?.Requirements
                                            .Where(de => de.Hours == de.Members.Courses.Sum(c => c.Hours))
                                            .SelectMany(de => de.Members.Courses);

                foreach (var course in degreeRequiredCourses)
                {
                    if (coursesTaken.All(c => c.CourseID != course.CourseID) && coursesToTake.All(c => c.CourseID != course.CourseID))
                    {
                        coursesToTake.Add(course);
                    }
                }

                for (var i = 0; i < coursesToTake.Count; i++)
                {
                    foreach (var prereq in coursesToTake[i].Prerequisites)
                    {
                        if (coursesTaken.All(c => c.CourseID != prereq.CourseID) &&
                            coursesToTake.All(c => c.CourseID != prereq.CourseID))
                        {
                            Course fullPrereqData = context.Courses
                                                    .Include(c => c.PrerequisiteLinks)
                                                    .ThenInclude(pl => pl.Prerequisite)
                                                    .FirstOrDefault(p => p.CourseID == prereq.CourseID);
                            coursesToTake.Add(fullPrereqData);
                        }
                    }
                }

                //Build an adjacency matrix
                int n = coursesToTake.Count;
                bool[,] matrix = new bool[n, n];
                for (var i = 0; i < n; i++)
                {
                    for (var j = 0; j < n; j++)
                    {
                        if (coursesToTake[i].Prerequisites.Any(c => c.CourseID == coursesToTake[j].CourseID))
                        {
                            matrix[i, j] = true;
                        }
                        else
                        {
                            matrix[i, j] = false; // matrix[i, j] = true IF course j is a prerequisite of course i
                        }
                    }
                }

                int[] hours = new int[n];
                int[] years = new int[n];
                for (int i = 0; i < n; i++)
                {
                    hours[i] = coursesToTake[i].Hours;
                    years[i] = int.Parse(coursesToTake[i].CatalogNumber[0] + "");
                }

                // Use topological sort to show the minimum semester for a course
                var planData = GeneratePlan(matrix, hours, years, data.MaxHoursPerSemester, data.MinHoursPerSemester, data.MinSemesters, n);

                // convert the graph data to actual courses
                DegreePlan plan = new DegreePlan()
                {
                    Semesters    = new List <Semester>(),
                    MinHours     = data.MinHoursPerSemester,
                    MinSemesters = data.MinSemesters,
                    MaxHours     = data.MaxHoursPerSemester
                };

                int    index = 0;
                Course free  = context.Courses.FirstOrDefault(c => c.Department == "UARK" && c.CatalogNumber == "000E");
                foreach (var semesterData in planData)
                {
                    var courses  = new List <SemesterCourseLink>();
                    var semester = new Semester()
                    {
                        Index = index
                    };
                    foreach (int courseData in semesterData)
                    {
                        if (courseData != -1)
                        {
                            courses.Add(new SemesterCourseLink()
                            {
                                Course   = coursesToTake[courseData],
                                Semester = semester
                            });
                        }
                        else
                        {
                            courses.Add(new SemesterCourseLink()
                            {
                                Course   = free,
                                Semester = semester
                            });
                        }
                    }

                    index++;

                    semester.SemesterCourseLinks = courses;
                    plan.Semesters.Add(semester);
                }

                var oldPlan = context.DegreePlans.Where(dp => dp.UserID == user.UserID);
                if (oldPlan.Count() != 0)
                {
                    context.DegreePlans.RemoveRange(oldPlan);
                }
                context.SaveChanges();

                plan.UserID = user.UserID;
                context.DegreePlans.Add(plan);
                context.SaveChanges();

                return(RedirectToAction("ViewDegreePlan", "Planner"));
            }
        }