public ActionResult AddHoursInDay([Bind(Include = "Id,Topic,AmountOfTeachingHours,TimeAllocationId,RemainingTime,url")] AddCourseInDayViewModel addCourseInDayViewModel)
        {
            var timeAllocationInstance = db.TimeOfCourse.Find(addCourseInDayViewModel.TimeAllocationId);

            var course = db.Courses.Find(addCourseInDayViewModel.Id);

            if (course != null)
            {
                CourseWithTimeAllocation courseWithTimeAllocation = new CourseWithTimeAllocation()
                {
                    CourseId = addCourseInDayViewModel.Id, CourseName = course.CourseName, AmountOfTeachingHours = addCourseInDayViewModel.AmountOfTeachingHours, ProgramId = course.ProgramId, TimeAllocationHelperId = addCourseInDayViewModel.TimeAllocationId, Topic = addCourseInDayViewModel.Topic
                };
                timeAllocationInstance.RemainingTime -= addCourseInDayViewModel.RemainingTime;
                db.CourseWithTimeAllocations.Add(courseWithTimeAllocation);
                db.SaveChanges();
                var instanceOfCourseDataHelper = db.ScheduleHelperContextPerCourse.Where(x => x.courseId == course.Id).FirstOrDefault();

                instanceOfCourseDataHelper.OverallTotalHours -= addCourseInDayViewModel.AmountOfTeachingHours;

                instanceOfCourseDataHelper.NoOfTeachingDays += 1;
                if (course.EndDate < timeAllocationInstance.Date)
                {
                    course.EndDate = timeAllocationInstance.Date;
                }
                db.SaveChanges();

                string path = addCourseInDayViewModel.url;
                path += "#" + timeAllocationInstance.Date.ToString("MMMM") + "-" + timeAllocationInstance.Date.Year;
                return(Redirect(path));
            }
            ViewBag.Id = new SelectList(db.Courses.ToList(), "Id", "CourseName");

            return(View());
        }
Example #2
0
        public ActionResult DeleteConfirmed(int id)
        {
            CourseWithTimeAllocation courseWithTimeAllocation = db.CourseWithTimeAllocations.Find(id);

            db.CourseWithTimeAllocations.Remove(courseWithTimeAllocation);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Example #3
0
 public ActionResult Edit([Bind(Include = "Id,ProgramId,CourseId,CourseName,Topic,TimeAllocationHelperId,AmountOfTeachingHours")] CourseWithTimeAllocation courseWithTimeAllocation)
 {
     if (ModelState.IsValid)
     {
         db.Entry(courseWithTimeAllocation).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.TimeAllocationHelperId = new SelectList(db.TimeOfCourse, "Id", "Id", courseWithTimeAllocation.TimeAllocationHelperId);
     return(View(courseWithTimeAllocation));
 }
Example #4
0
        // GET: CourseWithTimeAllocations/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            CourseWithTimeAllocation courseWithTimeAllocation = db.CourseWithTimeAllocations.Find(id);

            if (courseWithTimeAllocation == null)
            {
                return(HttpNotFound());
            }
            return(View(courseWithTimeAllocation));
        }
Example #5
0
        // GET: CourseWithTimeAllocations/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            CourseWithTimeAllocation courseWithTimeAllocation = db.CourseWithTimeAllocations.Find(id);

            if (courseWithTimeAllocation == null)
            {
                return(HttpNotFound());
            }
            ViewBag.TimeAllocationHelperId = new SelectList(db.TimeOfCourse, "Id", "Id", courseWithTimeAllocation.TimeAllocationHelperId);
            return(View(courseWithTimeAllocation));
        }
        public ActionResult EditDay(int?id, string url = "")
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            CourseWithTimeAllocation courseWithTimeAllocation = db.CourseWithTimeAllocations.Find(id);

            if (courseWithTimeAllocation == null)
            {
                return(HttpNotFound());
            }

            CourseWithTimeAllocationsViewModel courseWithTimeAllocationsViewModel = new CourseWithTimeAllocationsViewModel()
            {
                Id = courseWithTimeAllocation.Id, CourseId = courseWithTimeAllocation.CourseId, Course = courseWithTimeAllocation.CourseName, Delete = false, TeachingHours = courseWithTimeAllocation.AmountOfTeachingHours, Topic = courseWithTimeAllocation.Topic, TimeAllocationHelperId = courseWithTimeAllocation.TimeAllocationHelperId, url = url
            };

            return(View(courseWithTimeAllocationsViewModel));
        }
        public ActionResult CalculateSchedule(int?programId)
        {
            if (programId == null)
            {
                throw new Exception("Program id was not present.");
            }
            var program = db.Programs.Find(programId);

            List <TimeAllocationHelper> daysOfStudy = program.GetAllDayInstances(program, db);


            var courses = db.Courses.Include(c => c.ScheduleType).Where(x => x.ProgramId == programId).ToList();

            foreach (var c in courses)
            {
                if (c.HoursPerDay > program.TotalTeachingHoursOfDay)
                {
                    throw new Exception("Course " + c.CourseName + " has " + c.HoursPerDay + "\n But program has " + program.ProgramName + " has " + program.TotalTeachingHoursOfDay + " .\n please update the inputs and try again.");
                }
            }
            var courseIds          = courses.Select(x => x.Id).ToList();
            var prerequsiteCourses = db.PrerequisiteCourses.Where(x => courseIds.Contains(x.ActualCourseId)).ToList();
            // find all courses which do not have prerequsites.
            var allCoursesWithOutPrerequsites     = new List <int>();
            var allCoursesWithPrerequsites        = new List <int>();
            List <SceduleHelperHolder>       data = new List <SceduleHelperHolder>();
            List <ScheduleHelperContextData> saveDataForCourse = new List <ScheduleHelperContextData>();

            foreach (var c in courses)
            {
                var checking = prerequsiteCourses.Where(x => x.ActualCourseId == c.Id).FirstOrDefault();
                if (checking == null)
                {
                    allCoursesWithOutPrerequsites.Add(c.Id);
                }
                else
                {
                    allCoursesWithPrerequsites.Add(c.Id);
                }
                SceduleHelperHolder holder = new SceduleHelperHolder();
                holder.PrerequsiteCourseIds = prerequsiteCourses.Where(x => x.ActualCourseId == c.Id).ToList().Select(x => x.RequiredCourseId).ToList();
                holder.Allocated            = false;
                holder.CourseId             = c.Id;

                holder.CourseName        = c.CourseName;
                holder.TotalHoursPerDay  = c.HoursPerDay;
                holder.OverallTotalHours = c.ContactHours;
                holder.TeachingDays      = new List <char>();
                holder.NoOfTeachingDays  = 0;
                string daysString = "";
                for (int i = 0; i < c.ScheduleType.DayOption.Length; i++)
                {
                    daysString += c.ScheduleType.DayOption[i];
                    holder.TeachingDays.Add(c.ScheduleType.DayOption[i]);
                }
                saveDataForCourse.Add(new ScheduleHelperContextData()
                {
                    courseId = c.Id, CourseName = c.CourseName, NoOfTeachingDays = (int)c.NumberOfDays, OverallTotalHours = c.ContactHours, ProgramId = c.ProgramId, TeachingDays = daysString, TotalHoursPerDay = c.HoursPerDay
                });
                data.Add(holder);
            }



            // allocating course with it's aloowed time per day..
            int loopingCounter = 0;

            while (loopingCounter < allCoursesWithOutPrerequsites.Count())
            {
                // tempcounter increments at assigning time..
                var courseIDToAssignTime = allCoursesWithOutPrerequsites[loopingCounter];
                var courseInput          = courses.Where(x => x.Id == courseIDToAssignTime).FirstOrDefault();
                int loopingIncrementor   = 0;

                var      c         = data.Where(x => x.CourseId == courseIDToAssignTime).FirstOrDefault();
                DateTime?startTemp = null;
                DateTime?endTemp   = null;
                bool     flag1     = true;

                while (c.OverallTotalHours > 0)
                {
                    if (loopingIncrementor == daysOfStudy.Count() || loopingIncrementor < 0)
                    {
                        loopingIncrementor = 0;
                    }
                    var day = daysOfStudy[loopingIncrementor];

                    if (day.CouresIds == null)
                    {
                        day.CouresIds = new List <int>();
                    }
                    if (day.AllocatedTimes == null)
                    {
                        day.AllocatedTimes = new List <int>();
                    }
                    if (day.RemainingTime > 0 && c.TeachingDays.Contains(day.Day))
                    {
                        if (flag1 == true && day.RemainingTime - courseInput.HoursPerDay >= 0)
                        {
                            startTemp = day.Date;
                            flag1     = false;
                        }

                        if (day.RemainingTime - courseInput.HoursPerDay >= 0)
                        {
                            day.CouresIds.Add(courseIDToAssignTime);
                            day.AllocatedTimes.Add(courseInput.HoursPerDay);
                            day.RemainingTime   -= courseInput.HoursPerDay;
                            c.OverallTotalHours -= courseInput.HoursPerDay;
                            c.NoOfTeachingDays++;
                            endTemp = day.Date;
                        }
                        //else
                        //{
                        //    day.CouresIds.Add(courseIDToAssignTime);
                        //    day.AllocatedTimes.Add(day.RemainingTime);
                        //    c.OverallTotalHours -= day.RemainingTime;
                        //    day.RemainingTime = 0;
                        //    c.NoOfTeachingDays++;
                        //}
                    }
                    loopingIncrementor++;
                }
                c.Allocated = true;

                //ading calculated no of hours in database
                var instanceOfSaveCourseInfo = saveDataForCourse.Where(x => x.courseId == c.CourseId).ToList().FirstOrDefault();
                instanceOfSaveCourseInfo.OverallTotalHours = c.OverallTotalHours;

                c.StartDate = startTemp;
                c.EndDate   = endTemp;
                loopingCounter++;
            }

            List <bool> endConditionCheck  = new List <bool>();
            List <bool> raceConditionCheck = new List <bool>();
            int         howManyLeft        = allCoursesWithPrerequsites.Count();

            while (endConditionCheck.Count() < howManyLeft)
            {
                // running untill all courses are allocated..
                //selecting course
                for (int o = 0; o < allCoursesWithPrerequsites.Count(); o++)
                {
                    var tempCourseData = data.Where(x => x.CourseId == allCoursesWithPrerequsites[o]).FirstOrDefault();
                    if (tempCourseData.Allocated == false && tempCourseData.PrerequsiteCourseIds != null)
                    {
                        bool op = false;
                        for (int p = 0; p < tempCourseData.PrerequsiteCourseIds.Count(); p++)
                        {
                            var alReadyAssignedCourse = data.Where(x => x.CourseId == tempCourseData.PrerequsiteCourseIds[p]).FirstOrDefault();

                            if (alReadyAssignedCourse.Allocated == false)
                            {
                                op = false;
                                break;
                            }
                            else
                            {
                                op = true;
                            }
                        }

                        if (op == true)
                        {
                            DateTime?miniMumStartDate = null;

                            for (int e = 0; e < tempCourseData.PrerequsiteCourseIds.Count(); e++)
                            {
                                if (miniMumStartDate == null)
                                {
                                    miniMumStartDate = data.Where(x => x.CourseId == tempCourseData.PrerequsiteCourseIds[e]).FirstOrDefault().EndDate;
                                }
                                else
                                {
                                    DateTime?tempDate = data.Where(x => x.CourseId == tempCourseData.PrerequsiteCourseIds[e]).FirstOrDefault().EndDate;
                                    if (miniMumStartDate.Value.Ticks < tempDate.Value.Ticks)
                                    {
                                        miniMumStartDate = tempDate;
                                    }
                                }
                            }



                            var courseIDToAssignTime = tempCourseData.CourseId;
                            var courseInput          = courses.Where(x => x.Id == courseIDToAssignTime).FirstOrDefault();
                            int loopingIncrementor   = 0;

                            var      c         = tempCourseData;
                            DateTime?startTemp = null;
                            DateTime?endTemp   = null;
                            bool     flag1     = true;


                            bool appropriateStartingDay = true;
                            while (appropriateStartingDay == true)
                            {
                                var day = daysOfStudy[loopingIncrementor];
                                if (day.Date.Ticks < miniMumStartDate.Value.Ticks)
                                {
                                    loopingIncrementor++;
                                }
                                else
                                {
                                    appropriateStartingDay = false;
                                }
                            }

                            while (c.OverallTotalHours > 0)
                            {
                                if (loopingIncrementor >= 0 && loopingIncrementor < daysOfStudy.Count())
                                {
                                    var day = daysOfStudy[loopingIncrementor];

                                    if (day.CouresIds == null)
                                    {
                                        day.CouresIds = new List <int>();
                                    }
                                    if (day.AllocatedTimes == null)
                                    {
                                        day.AllocatedTimes = new List <int>();
                                    }
                                    if (day.RemainingTime > 0 && c.TeachingDays.Contains(day.Day))
                                    {
                                        if (flag1 == true && day.RemainingTime - courseInput.HoursPerDay >= 0)
                                        {
                                            startTemp = day.Date;
                                            flag1     = false;
                                        }
                                        if (day.RemainingTime - courseInput.HoursPerDay >= 0)
                                        {
                                            day.CouresIds.Add(courseIDToAssignTime);
                                            day.AllocatedTimes.Add(courseInput.HoursPerDay);
                                            day.RemainingTime   -= courseInput.HoursPerDay;
                                            c.OverallTotalHours -= courseInput.HoursPerDay;
                                            c.NoOfTeachingDays++;
                                            endTemp = day.Date;
                                        }
                                        //else
                                        //{

                                        //    //day.CouresIds.Add(courseIDToAssignTime);
                                        //    //day.AllocatedTimes.Add(day.RemainingTime);
                                        //    //c.OverallTotalHours -= day.RemainingTime;
                                        //    //day.RemainingTime = 0;
                                        //    //c.NoOfTeachingDays++;
                                        //}
                                    }
                                }
                                else
                                {
                                    loopingIncrementor = 0;
                                }

                                loopingIncrementor++;
                            }

                            c.Allocated = true;
                            (saveDataForCourse.Where(x => x.courseId == c.CourseId).ToList().FirstOrDefault()).OverallTotalHours = c.OverallTotalHours;

                            allCoursesWithPrerequsites.Remove(c.CourseId);
                            c.StartDate = startTemp;
                            c.EndDate   = endTemp;
                            endConditionCheck.Add(true);
                        }
                        else
                        {
                            raceConditionCheck.Add(false);
                        }
                    }
                }

                if (allCoursesWithPrerequsites.Count() != 0 && raceConditionCheck.Count() == allCoursesWithPrerequsites.Count())
                {
                    throw new Exception(" Some course requires prerequsite courses but all prerequsite courses are dependent on each other. Please check the input prerequsite again. ");
                }
                else
                {
                    raceConditionCheck = new List <bool>();
                }
            }


            foreach (var courseOfInput in courses)
            {
                var tempCourseData = data.Where(x => x.CourseId == courseOfInput.Id).FirstOrDefault();
                courseOfInput.StartDate    = tempCourseData.StartDate;
                courseOfInput.EndDate      = tempCourseData.EndDate;
                courseOfInput.NumberOfDays = tempCourseData.NoOfTeachingDays;
                db.SaveChanges();
            }


            //daysOfStudy
            var programTimeLineData = db.TimeOfCourse.Where(x => x.ProgramId == program.Id).ToList().OrderBy(x => x.Date).ToList();

            foreach (var r in programTimeLineData)
            {
                db.TimeOfCourse.Remove(r);
            }
            db.SaveChanges();

            var previouseCalculatedInstances = db.CourseWithTimeAllocations.Where(x => x.ProgramId == program.Id).ToList();

            foreach (var op in previouseCalculatedInstances)
            {
                db.CourseWithTimeAllocations.Remove(op);
                db.SaveChanges();
            }

            foreach (var dayIns in daysOfStudy)
            {
                db.TimeOfCourse.Add(dayIns);
                db.SaveChanges();
            }


            var newInstancesOfTimeOfCourse = db.TimeOfCourse.Where(x => x.ProgramId == program.Id).ToList();

            foreach (var newIns in newInstancesOfTimeOfCourse)
            {
                var op = daysOfStudy.Find(x => x.Date == newIns.Date);
                if (op != null && op.CouresIds != null)
                {
                    int counterI = 0;

                    for (int pq = 0; pq < op.CouresIds.Count(); pq++)
                    {
                        CourseWithTimeAllocation courseWithTimeAllocation = new CourseWithTimeAllocation();
                        courseWithTimeAllocation.TimeAllocationHelperId = newIns.Id;
                        courseWithTimeAllocation.CourseId = op.CouresIds[pq];
                        courseWithTimeAllocation.AmountOfTeachingHours = op.AllocatedTimes[counterI];
                        counterI++;
                        courseWithTimeAllocation.ProgramId  = op.ProgramId;
                        courseWithTimeAllocation.CourseName = db.Courses.Find(op.CouresIds[pq]).CourseName;
                        courseWithTimeAllocation.Topic      = "";
                        db.CourseWithTimeAllocations.Add(courseWithTimeAllocation);
                        db.SaveChanges();
                    }
                }
            }
            foreach (var c in saveDataForCourse)
            {
                var instanceOfDbEntry = db.ScheduleHelperContextPerCourse.Where(x => x.courseId == c.courseId).FirstOrDefault();
                if (instanceOfDbEntry != null)
                {
                    //already there then update.
                    instanceOfDbEntry.CourseName        = c.CourseName;
                    instanceOfDbEntry.NoOfTeachingDays  = c.NoOfTeachingDays;
                    instanceOfDbEntry.OverallTotalHours = c.OverallTotalHours;
                    instanceOfDbEntry.TeachingDays      = c.TeachingDays;
                    instanceOfDbEntry.TotalHoursPerDay  = c.TotalHoursPerDay;
                    db.SaveChanges();
                }
                else
                {
                    //else create new instance of helper data.
                    db.ScheduleHelperContextPerCourse.Add(c);
                    db.SaveChanges();
                }
            }

            return(RedirectToAction("UpdateCalendar", new { programId = program.Id }));
        }