public ActionResult DeleteConfirmed(int id)
        {
            AspnetLesson aspnetLesson = db.AspnetLessons.Find(id);

            db.AspnetLessons.Remove(aspnetLesson);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        public ActionResult GetLessonVideo(int LessonID)
        {
            AspnetLesson Lesson         = db.AspnetLessons.Where(x => x.Id == LessonID).FirstOrDefault();
            string       LessonVideoUrl = "";


            if (Lesson != null)
            {
                LessonVideoUrl = Lesson.Video_Url;
            }

            return(Json(new { LessonId = Lesson.Id, LessonName = Lesson.Name, LessonVideo = Lesson.Video_Url, LessonDescription = Lesson.Description }, JsonRequestBehavior.AllowGet));
        }
        // GET: AspnetLessons/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            AspnetLesson aspnetLesson = db.AspnetLessons.Find(id);

            if (aspnetLesson == null)
            {
                return(HttpNotFound());
            }
            return(View(aspnetLesson));
        }
        public ActionResult CheckLessonOrderBy(string TopicId, string OrderBy)
        {
            int TopId        = Convert.ToInt32(TopicId);
            int OrderByValue = Convert.ToInt32(OrderBy);

            var          TopicExist = "";
            AspnetLesson Lesson     = db.AspnetLessons.Where(x => x.TopicId == TopId && x.OrderBy == OrderByValue).FirstOrDefault();

            if (Lesson == null)
            {
                TopicExist = "No";
            }
            else
            {
                TopicExist = "Yes";
            }


            return(Json(TopicExist, JsonRequestBehavior.AllowGet));
        }
        public ActionResult Create(LessonViewModel LessonViewModel)
        {
            AspnetLesson Lesson = new AspnetLesson();

            Lesson.Name            = LessonViewModel.LessonName;
            Lesson.Video_Url       = LessonViewModel.LessonVideoURL;
            Lesson.TopicId         = LessonViewModel.TopicId;
            Lesson.DurationMinutes = LessonViewModel.LessonDuration;
            Lesson.StartTime       = LessonViewModel.StartTime;
            Lesson.EndTime         = LessonViewModel.EndTime;
            Lesson.AddLink         = LessonViewModel.AddLink;
            Lesson.CreationDate    = LessonViewModel.CreationDate;
            Lesson.Description     = LessonViewModel.LessonDescription;
            Lesson.CreationDate    = DateTime.Now;
            db.AspnetLessons.Add(Lesson);
            db.SaveChanges();


            HttpPostedFileBase Assignment  = Request.Files["Assignment"];
            HttpPostedFileBase Attachment1 = Request.Files["Attachment1"];
            HttpPostedFileBase Attachment2 = Request.Files["Attachment2"];
            HttpPostedFileBase Attachment3 = Request.Files["Attachment3"];

            if (Assignment.ContentLength > 0)
            {
                var fileName = Path.GetFileName(Assignment.FileName);
                Assignment.SaveAs(Server.MapPath("~/Content/StudentAssignments/") + fileName);
                AspnetStudentAssignment studentAssignment = new AspnetStudentAssignment();

                studentAssignment.FileName = fileName;

                studentAssignment.Name = LessonViewModel.AssignmentName;


                string DueDate = Convert.ToString(LessonViewModel.AssignmentDueDate);


                if (DueDate == "1/1/0001 12:00:00 AM")
                {
                    studentAssignment.DueDate = null;
                }
                else
                {
                    studentAssignment.DueDate = LessonViewModel.AssignmentDueDate;
                }


                studentAssignment.Description  = LessonViewModel.AssignmentDescription;
                studentAssignment.CreationDate = DateTime.Now;
                studentAssignment.LessonId     = Lesson.Id;

                db.AspnetStudentAssignments.Add(studentAssignment);
                db.SaveChanges();
            }

            if (Attachment1.ContentLength > 0)
            {
                var fileName = Path.GetFileName(Attachment1.FileName);
                Attachment1.SaveAs(Server.MapPath("~/Content/StudentAttachments/") + fileName);

                AspnetStudentAttachment studentAttachment1 = new AspnetStudentAttachment();

                studentAttachment1.Name         = LessonViewModel.AttachmentName1;
                studentAttachment1.Path         = fileName;
                studentAttachment1.CreationDate = DateTime.Now;
                studentAttachment1.LessonId     = Lesson.Id;
                db.AspnetStudentAttachments.Add(studentAttachment1);
                db.SaveChanges();
            }
            if (Attachment2.ContentLength > 0)
            {
                var fileName = Path.GetFileName(Attachment2.FileName);
                Attachment2.SaveAs(Server.MapPath("~/Content/StudentAttachments/") + fileName);

                AspnetStudentAttachment studentAttachment2 = new AspnetStudentAttachment();

                studentAttachment2.Name         = LessonViewModel.AttachmentName2;
                studentAttachment2.Path         = fileName;
                studentAttachment2.CreationDate = DateTime.Now;
                studentAttachment2.LessonId     = Lesson.Id;
                db.AspnetStudentAttachments.Add(studentAttachment2);

                db.SaveChanges();
            }

            if (Attachment3.ContentLength > 0)
            {
                var fileName = Path.GetFileName(Attachment3.FileName);
                Attachment3.SaveAs(Server.MapPath("~/Content/StudentAttachments/") + fileName);

                AspnetStudentAttachment studentAttachment3 = new AspnetStudentAttachment();

                studentAttachment3.Name         = LessonViewModel.AttachmentName3;
                studentAttachment3.Path         = fileName;
                studentAttachment3.CreationDate = DateTime.Now;
                studentAttachment3.LessonId     = Lesson.Id;
                db.AspnetStudentAttachments.Add(studentAttachment3);
                db.SaveChanges();
            }

            if (LessonViewModel.LinkUrl1 != null)
            {
                AspnetStudentLink link1 = new AspnetStudentLink();

                link1.URL          = LessonViewModel.LinkUrl1;
                link1.CreationDate = DateTime.Now;
                link1.LessonId     = Lesson.Id;
                db.AspnetStudentLinks.Add(link1);
                db.SaveChanges();
            }

            if (LessonViewModel.LinkUrl2 != null)
            {
                AspnetStudentLink link2 = new AspnetStudentLink();

                link2.URL          = LessonViewModel.LinkUrl2;
                link2.CreationDate = DateTime.Now;
                link2.LessonId     = Lesson.Id;
                db.AspnetStudentLinks.Add(link2);
                db.SaveChanges();
            }


            if (LessonViewModel.LinkUrl3 != null)
            {
                AspnetStudentLink link3 = new AspnetStudentLink();

                link3.URL          = LessonViewModel.LinkUrl3;
                link3.CreationDate = DateTime.Now;
                link3.LessonId     = Lesson.Id;
                db.AspnetStudentLinks.Add(link3);
                db.SaveChanges();
            }



            return(RedirectToAction("ViewTopicsAndLessons", "AspnetSubjectTopics"));
        }
        public ActionResult Edit(LessonViewModel LessonViewModel)
        {
            AspnetLesson Lesson = db.AspnetLessons.Where(x => x.Id == LessonViewModel.Id).FirstOrDefault();

            Lesson.Name            = LessonViewModel.LessonName;
            Lesson.Video_Url       = LessonViewModel.LessonVideoURL;
            Lesson.TopicId         = LessonViewModel.TopicId;
            Lesson.DurationMinutes = LessonViewModel.LessonDuration;
            Lesson.Description     = LessonViewModel.LessonDescription;
            db.SaveChanges();


            HttpPostedFileBase Assignment  = Request.Files["Assignment"];
            HttpPostedFileBase Attachment1 = Request.Files["Attachment1"];
            HttpPostedFileBase Attachment2 = Request.Files["Attachment2"];
            HttpPostedFileBase Attachment3 = Request.Files["Attachment3"];

            var fileName = "";

            if (Assignment.ContentLength > 0)
            {
                fileName = Path.GetFileName(Assignment.FileName);
                Assignment.SaveAs(Server.MapPath("~/Content/StudentAssignments/") + fileName);
            }
            AspnetStudentAssignment studentAssignment = db.AspnetStudentAssignments.Where(x => x.LessonId == Lesson.Id).FirstOrDefault();

            if (studentAssignment != null)
            {
                if (fileName != "")
                {
                    studentAssignment.FileName = fileName;
                }

                studentAssignment.Name = LessonViewModel.AssignmentName;
                string DueDate = Convert.ToString(LessonViewModel.AssignmentDueDate);

                if (DueDate == "1/1/0001 12:00:00 AM")
                {
                    studentAssignment.DueDate = null;
                }
                else
                {
                    studentAssignment.DueDate = LessonViewModel.AssignmentDueDate;
                }

                studentAssignment.Description = LessonViewModel.AssignmentDescription;
                db.SaveChanges();
            }
            else
            {
                if (Assignment.ContentLength > 0)
                {
                    AspnetStudentAssignment studentAssignment1 = new AspnetStudentAssignment();

                    studentAssignment1.FileName = fileName;

                    studentAssignment1.Name = LessonViewModel.AssignmentName;


                    string DueDate = Convert.ToString(LessonViewModel.AssignmentDueDate);


                    if (DueDate == "1/1/0001 12:00:00 AM")
                    {
                        studentAssignment1.DueDate = null;
                    }
                    else
                    {
                        studentAssignment1.DueDate = LessonViewModel.AssignmentDueDate;
                    }


                    studentAssignment1.Description  = LessonViewModel.AssignmentDescription;
                    studentAssignment1.CreationDate = DateTime.Now;
                    studentAssignment1.LessonId     = Lesson.Id;

                    db.AspnetStudentAssignments.Add(studentAssignment1);
                    db.SaveChanges();
                }
            }


            List <AspnetStudentAttachment> studentAttachments = db.AspnetStudentAttachments.Where(x => x.LessonId == Lesson.Id).ToList();
            List <AspnetStudentLink>       studentLinks       = db.AspnetStudentLinks.Where(x => x.LessonId == Lesson.Id).ToList();



            //db.AspnetStudentAttachments.RemoveRange(studentAttachments);
            //db.SaveChanges();

            db.AspnetStudentLinks.RemoveRange(studentLinks);
            db.SaveChanges();

            SEA_DatabaseEntities db1 = new SEA_DatabaseEntities();

            List <AspnetStudentAttachment> studentAttachments1 = db1.AspnetStudentAttachments.Where(x => x.LessonId == Lesson.Id).ToList();

            int TotalAttachments = studentAttachments1.Count;

            if (TotalAttachments == 0)
            {
                if (Attachment1.ContentLength > 0)
                {
                    var fileName1 = Path.GetFileName(Attachment1.FileName);
                    Attachment1.SaveAs(Server.MapPath("~/Content/StudentAttachments/") + fileName1);

                    AspnetStudentAttachment studentAttachment1 = new AspnetStudentAttachment();

                    studentAttachment1.Name         = LessonViewModel.AttachmentName1;
                    studentAttachment1.Path         = fileName1;
                    studentAttachment1.CreationDate = DateTime.Now;
                    studentAttachment1.LessonId     = Lesson.Id;
                    db.AspnetStudentAttachments.Add(studentAttachment1);
                    db.SaveChanges();
                }
                if (Attachment2.ContentLength > 0)
                {
                    var fileName1 = Path.GetFileName(Attachment2.FileName);
                    Attachment2.SaveAs(Server.MapPath("~/Content/StudentAttachments/") + fileName1);

                    AspnetStudentAttachment studentAttachment2 = new AspnetStudentAttachment();

                    studentAttachment2.Name         = LessonViewModel.AttachmentName2;
                    studentAttachment2.Path         = fileName1;
                    studentAttachment2.CreationDate = DateTime.Now;
                    studentAttachment2.LessonId     = Lesson.Id;
                    db.AspnetStudentAttachments.Add(studentAttachment2);

                    db.SaveChanges();
                }

                if (Attachment3.ContentLength > 0)
                {
                    var fileName1 = Path.GetFileName(Attachment3.FileName);
                    Attachment3.SaveAs(Server.MapPath("~/Content/StudentAttachments/") + fileName1);

                    AspnetStudentAttachment studentAttachment3 = new AspnetStudentAttachment();

                    studentAttachment3.Name         = LessonViewModel.AttachmentName3;
                    studentAttachment3.Path         = fileName1;
                    studentAttachment3.CreationDate = DateTime.Now;
                    studentAttachment3.LessonId     = Lesson.Id;
                    db.AspnetStudentAttachments.Add(studentAttachment3);
                    db.SaveChanges();
                }
            }
            else
            {
                if (TotalAttachments == 1)
                {
                    var FirstElement = studentAttachments1.ElementAt(0);
                    FirstElement.Name = LessonViewModel.AttachmentName1;

                    var FileName = FirstElement.Path;

                    if (Attachment1.ContentLength > 0)
                    {
                        var fileName1 = Path.GetFileName(Attachment1.FileName);
                        FileName = fileName1;
                        Attachment1.SaveAs(Server.MapPath("~/Content/StudentAttachments/") + fileName1);
                    }
                    FirstElement.Path = FileName;
                    db1.SaveChanges();

                    if (Attachment2.ContentLength > 0)
                    {
                        var fileName1 = Path.GetFileName(Attachment2.FileName);
                        Attachment2.SaveAs(Server.MapPath("~/Content/StudentAttachments/") + fileName1);

                        AspnetStudentAttachment studentAttachment2 = new AspnetStudentAttachment();

                        studentAttachment2.Name         = LessonViewModel.AttachmentName2;
                        studentAttachment2.Path         = fileName1;
                        studentAttachment2.CreationDate = DateTime.Now;
                        studentAttachment2.LessonId     = Lesson.Id;
                        db.AspnetStudentAttachments.Add(studentAttachment2);

                        db.SaveChanges();
                    }

                    if (Attachment3.ContentLength > 0)
                    {
                        var fileName1 = Path.GetFileName(Attachment3.FileName);
                        Attachment3.SaveAs(Server.MapPath("~/Content/StudentAttachments/") + fileName1);

                        AspnetStudentAttachment studentAttachment3 = new AspnetStudentAttachment();

                        studentAttachment3.Name         = LessonViewModel.AttachmentName3;
                        studentAttachment3.Path         = fileName1;
                        studentAttachment3.CreationDate = DateTime.Now;
                        studentAttachment3.LessonId     = Lesson.Id;
                        db.AspnetStudentAttachments.Add(studentAttachment3);
                        db.SaveChanges();
                    }
                }

                else if (TotalAttachments == 2)
                {
                    var FirstElement = studentAttachments1.ElementAt(0);
                    FirstElement.Name = LessonViewModel.AttachmentName1;

                    var FileName0 = FirstElement.Path;

                    if (Attachment1.ContentLength > 0)
                    {
                        var fileName1 = Path.GetFileName(Attachment1.FileName);
                        FileName0 = fileName1;
                        Attachment1.SaveAs(Server.MapPath("~/Content/StudentAttachments/") + fileName1);
                    }
                    FirstElement.Path = FileName0;
                    db1.SaveChanges();


                    var SecondElement = studentAttachments1.ElementAt(1);
                    SecondElement.Name = LessonViewModel.AttachmentName2;

                    var FileName1 = SecondElement.Path;

                    if (Attachment2.ContentLength > 0)
                    {
                        var fileName2 = Path.GetFileName(Attachment2.FileName);
                        FileName1 = fileName2;
                        Attachment2.SaveAs(Server.MapPath("~/Content/StudentAttachments/") + fileName2);
                    }
                    SecondElement.Path = FileName1;
                    db1.SaveChanges();



                    if (Attachment3.ContentLength > 0)
                    {
                        var fileName1 = Path.GetFileName(Attachment3.FileName);
                        Attachment3.SaveAs(Server.MapPath("~/Content/StudentAttachments/") + fileName1);

                        AspnetStudentAttachment studentAttachment3 = new AspnetStudentAttachment();

                        studentAttachment3.Name         = LessonViewModel.AttachmentName3;
                        studentAttachment3.Path         = fileName1;
                        studentAttachment3.CreationDate = DateTime.Now;
                        studentAttachment3.LessonId     = Lesson.Id;
                        db.AspnetStudentAttachments.Add(studentAttachment3);
                        db.SaveChanges();
                    }
                }

                else
                {
                    var FirstElement = studentAttachments1.ElementAt(0);
                    FirstElement.Name = LessonViewModel.AttachmentName1;

                    var FileName0 = FirstElement.Path;

                    if (Attachment1.ContentLength > 0)
                    {
                        var fileName1 = Path.GetFileName(Attachment1.FileName);
                        FileName0 = fileName1;
                        Attachment1.SaveAs(Server.MapPath("~/Content/StudentAttachments/") + fileName1);
                    }
                    FirstElement.Path = FileName0;
                    db1.SaveChanges();


                    var SecondElement = studentAttachments1.ElementAt(1);
                    SecondElement.Name = LessonViewModel.AttachmentName2;

                    var FileName1 = SecondElement.Path;

                    if (Attachment2.ContentLength > 0)
                    {
                        var fileName2 = Path.GetFileName(Attachment2.FileName);
                        FileName1 = fileName2;
                        Attachment2.SaveAs(Server.MapPath("~/Content/StudentAttachments/") + fileName2);
                    }
                    SecondElement.Path = FileName1;
                    db1.SaveChanges();


                    var ThirdElement = studentAttachments1.ElementAt(2);
                    ThirdElement.Name = LessonViewModel.AttachmentName3;

                    var FileName2 = ThirdElement.Path;

                    if (Attachment3.ContentLength > 0)
                    {
                        var fileName3 = Path.GetFileName(Attachment3.FileName);
                        FileName2 = fileName3;
                        Attachment2.SaveAs(Server.MapPath("~/Content/StudentAttachments/") + fileName3);
                    }
                    ThirdElement.Path = FileName2;
                    db1.SaveChanges();
                }
            }



            if (LessonViewModel.LinkUrl1 != null)
            {
                AspnetStudentLink link1 = new AspnetStudentLink();

                link1.URL          = LessonViewModel.LinkUrl1;
                link1.CreationDate = DateTime.Now;
                link1.LessonId     = Lesson.Id;
                db.AspnetStudentLinks.Add(link1);
                db.SaveChanges();
            }

            if (LessonViewModel.LinkUrl2 != null)
            {
                AspnetStudentLink link2 = new AspnetStudentLink();

                link2.URL          = LessonViewModel.LinkUrl2;
                link2.CreationDate = DateTime.Now;
                link2.LessonId     = Lesson.Id;
                db.AspnetStudentLinks.Add(link2);
                db.SaveChanges();
            }


            if (LessonViewModel.LinkUrl3 != null)
            {
                AspnetStudentLink link3 = new AspnetStudentLink();

                link3.URL          = LessonViewModel.LinkUrl3;
                link3.CreationDate = DateTime.Now;
                link3.LessonId     = Lesson.Id;
                db.AspnetStudentLinks.Add(link3);
                db.SaveChanges();
            }


            return(RedirectToAction("ViewTopicsAndLessons", "AspnetSubjectTopics"));
        }
        // GET: AspnetLessons/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            AspnetLesson aspnetLesson = db.AspnetLessons.Find(id);

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

            AspnetStudentAssignment        studentAssignment  = db.AspnetStudentAssignments.Where(x => x.LessonId == aspnetLesson.Id).FirstOrDefault();
            List <AspnetStudentAttachment> studentAttachments = db.AspnetStudentAttachments.Where(x => x.LessonId == aspnetLesson.Id).ToList();
            List <AspnetStudentLink>       studentLinks       = db.AspnetStudentLinks.Where(x => x.LessonId == aspnetLesson.Id).ToList();
            LessonViewModel lessonViewModel = new LessonViewModel();

            lessonViewModel.LessonDescription = aspnetLesson.Description;
            lessonViewModel.LessonVideoURL    = aspnetLesson.Video_Url;
            lessonViewModel.LessonName        = aspnetLesson.Name;
            lessonViewModel.LessonDuration    = aspnetLesson.DurationMinutes;
            lessonViewModel.StartTime         = aspnetLesson.StartTime;
            lessonViewModel.EndTime           = aspnetLesson.EndTime;
            lessonViewModel.AddLink           = aspnetLesson.AddLink;

            int?TopicId = aspnetLesson.TopicId;

            ViewBag.LessonDuration = aspnetLesson.DurationMinutes;
            int?           SubjectId = db.AspnetSubjectTopics.Where(x => x.Id == TopicId).FirstOrDefault().SubjectId;
            GenericSubject Subject   = db.GenericSubjects.Where(x => x.Id == SubjectId).FirstOrDefault();

            var CourseType = Subject.SubjectType;

            lessonViewModel.Id = aspnetLesson.Id;
            if (studentAssignment != null)
            {
                lessonViewModel.AssignmentName        = studentAssignment.Name;
                lessonViewModel.AssignmentDescription = studentAssignment.Description;
                DateTime Date = Convert.ToDateTime(studentAssignment.DueDate);
                string   date = Date.ToString("yyyy-MM-dd");

                ViewBag.AssignmentFileName = studentAssignment.FileName;

                lessonViewModel.AssignmentDueDate = studentAssignment.DueDate;
                ViewBag.Date = date;
            }


            int count = 1;

            foreach (var link in studentLinks)
            {
                if (count == 1)
                {
                    lessonViewModel.LinkUrl1 = link.URL;
                }
                else if (count == 2)
                {
                    lessonViewModel.LinkUrl2 = link.URL;
                }
                else if (count == 3)
                {
                    lessonViewModel.LinkUrl3 = link.URL;
                }
                else
                {
                }

                count++;
            }

            count = 1;
            foreach (var attachment in studentAttachments)
            {
                if (count == 1)
                {
                    lessonViewModel.AttachmentName1 = attachment.Name;
                    ViewBag.Attachment1FileName     = attachment.Path;
                }
                else if (count == 2)
                {
                    lessonViewModel.AttachmentName2 = attachment.Name;
                    ViewBag.Attachment2FileName     = attachment.Path;
                }
                else if (count == 3)
                {
                    lessonViewModel.AttachmentName3 = attachment.Name;
                    ViewBag.Attachment3FileName     = attachment.Path;
                }
                else
                {
                }

                count++;
            }


            //  ViewBag.SecId = new SelectList(db.AspNetClasses, "Id", "ClassName", ClassId);
            // ViewBag.SubId = new SelectList(db.GenericSubjects.Where(x => x.SubjectType == Subject.SubjectType), "Id", "SubjectName", SubjectId);


            var UserId = User.Identity.GetUserId();


            var SubjectofCurrentSessionTeacher = from subject in db.GenericSubjects
                                                 join TeacherSubject in db.Teacher_GenericSubjects on subject.Id equals TeacherSubject.SubjectId
                                                 join employee in db.AspNetEmployees on TeacherSubject.TeacherId equals employee.Id
                                                 where employee.UserId == UserId && subject.SubjectType == Subject.SubjectType
                                                 select new
            {
                subject.Id,
                subject.SubjectName,
            };

            ViewBag.SubId = new SelectList(SubjectofCurrentSessionTeacher, "Id", "SubjectName", SubjectId);

            ViewBag.TopicId = new SelectList(db.AspnetSubjectTopics.Where(x => x.SubjectId == SubjectId), "Id", "Name", aspnetLesson.TopicId);
            ViewBag.CTId    = Subject.SubjectType;



            return(View(lessonViewModel));
        }
        public ActionResult DeleteLessons(int?id)
        {
            int?LessonId = id;

            AspnetLesson LessonToDelete = db.AspnetLessons.Where(x => x.Id == LessonId).FirstOrDefault();

            if (LessonToDelete != null)
            {
                IEnumerable <AspnetComment> CommentsToDelete = db.AspnetComment_Head.Where(x => x.LessonId == LessonId).SelectMany(x => x.AspnetComments);
                db.AspnetComments.RemoveRange(CommentsToDelete);
                db.SaveChanges();

                List <AspnetComment_Head> ListCommentHeadToDelete = db.AspnetComment_Head.Where(x => x.LessonId == LessonId).ToList();
                db.AspnetComment_Head.RemoveRange(ListCommentHeadToDelete);
                db.SaveChanges();

                var AssignmentToDelete = db.AspnetStudentAssignments.Where(x => x.LessonId == LessonId).FirstOrDefault();
                if (AssignmentToDelete != null)
                {
                    db.AspnetStudentAssignments.Remove(AssignmentToDelete);
                    db.SaveChanges();
                }

                List <AspnetStudentAttachment> StudentAttachmentListToDelete = db.AspnetStudentAttachments.Where(x => x.LessonId == LessonId).ToList();
                db.AspnetStudentAttachments.RemoveRange(StudentAttachmentListToDelete);
                db.SaveChanges();


                List <AspnetStudentLink> StudentLinkListToDelete = db.AspnetStudentLinks.Where(x => x.LessonId == LessonId).ToList();
                db.AspnetStudentLinks.RemoveRange(StudentLinkListToDelete);
                db.SaveChanges();

                List <AspnetStudentAssignmentSubmission> StudentAssignmentSubmissionListToDelete = db.AspnetStudentAssignmentSubmissions.Where(x => x.LessonId == LessonId).ToList();
                db.AspnetStudentAssignmentSubmissions.RemoveRange(StudentAssignmentSubmissionListToDelete);
                db.SaveChanges();

                List <StudentLessonTracking> StudentLessonTrackingListToDelete = db.StudentLessonTrackings.Where(x => x.LessonId == LessonId).ToList();
                db.StudentLessonTrackings.RemoveRange(StudentLessonTrackingListToDelete);
                db.SaveChanges();

                List <Student_Quiz_Scoring> StudentQuizScoringToDelete = db.AspnetQuestions.Where(x => x.LessonId == LessonId).SelectMany(x => x.Student_Quiz_Scoring).ToList();
                db.Student_Quiz_Scoring.RemoveRange(StudentQuizScoringToDelete);
                db.SaveChanges();

                List <Lesson_Session> LessonSessionToDelete = db.Lesson_Session.Where(x => x.LessonId == LessonId).ToList();
                db.Lesson_Session.RemoveRange(LessonSessionToDelete);
                db.SaveChanges();

                List <Quiz_Topic_Questions> QuizTopicQuestionnsToDelete = db.AspnetQuestions.Where(x => x.LessonId == LessonId).SelectMany(x => x.Quiz_Topic_Questions).ToList();
                db.Quiz_Topic_Questions.RemoveRange(QuizTopicQuestionnsToDelete);
                db.SaveChanges();

                List <AspnetQuestion> QuestionListToDelete = db.AspnetQuestions.Where(x => x.LessonId == LessonId).ToList();
                db.AspnetQuestions.RemoveRange(QuestionListToDelete);
                db.SaveChanges();

                db.AspnetLessons.Remove(LessonToDelete);
                db.SaveChanges();
            }

            return(RedirectToAction("ViewTopicsAndLessons", "AspnetSubjectTopics"));
        }
        // GET: AspnetLessons/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            AspnetLesson aspnetLesson = db.AspnetLessons.Find(id);

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

            AspnetStudentAssignment        studentAssignment  = db.AspnetStudentAssignments.Where(x => x.LessonId == aspnetLesson.Id).FirstOrDefault();
            List <AspnetStudentAttachment> studentAttachments = db.AspnetStudentAttachments.Where(x => x.LessonId == aspnetLesson.Id).ToList();
            List <AspnetStudentLink>       studentLinks       = db.AspnetStudentLinks.Where(x => x.LessonId == aspnetLesson.Id).ToList();
            LessonViewModel lessonViewModel = new LessonViewModel();

            lessonViewModel.LessonDescription = aspnetLesson.Description;
            lessonViewModel.LessonVideoURL    = aspnetLesson.Video_Url;
            lessonViewModel.LessonName        = aspnetLesson.Name;
            lessonViewModel.LessonDuration    = aspnetLesson.DurationMinutes;

            //    Lesson_Session LessonSession = db.Lesson_Session.Where(x => x.LessonId == id).FirstOrDefault();

            lessonViewModel.IsActive = Convert.ToBoolean(aspnetLesson.IsActive);

            //var StartDate = Convert.ToDateTime(LessonSession.StartDate);

            //var StartDateInString = StartDate.ToString("yyyy-MM-dd");

            //ViewBag.LessonStartDate = StartDateInString;

            ////Due Date
            //var DueDate = Convert.ToDateTime(LessonSession.DueDate);

            //var DueDateInString = DueDate.ToString("yyyy-MM-dd");


            //ViewBag.LessonDueDate = DueDateInString;


            int?TopicId = aspnetLesson.TopicId;

            ViewBag.LessonDuration = aspnetLesson.DurationMinutes;

            int?           SubjectId = db.AspnetSubjectTopics.Where(x => x.Id == TopicId).FirstOrDefault().SubjectId;
            GenericSubject Subject   = db.GenericSubjects.Where(x => x.Id == SubjectId).FirstOrDefault();



            var CourseType = Subject.SubjectType;

            lessonViewModel.Id = aspnetLesson.Id;
            if (studentAssignment != null)
            {
                lessonViewModel.AssignmentName        = studentAssignment.Name;
                lessonViewModel.AssignmentDescription = studentAssignment.Description;
                DateTime Date = Convert.ToDateTime(studentAssignment.DueDate);
                string   date = Date.ToString("yyyy-MM-dd");

                ViewBag.AssignmentFileName = studentAssignment.FileName;

                lessonViewModel.AssignmentDueDate = studentAssignment.DueDate;
                ViewBag.Date = date;
            }


            int count = 1;

            foreach (var link in studentLinks)
            {
                if (count == 1)
                {
                    lessonViewModel.LinkUrl1 = link.URL;
                }
                else if (count == 2)
                {
                    lessonViewModel.LinkUrl2 = link.URL;
                }
                else if (count == 3)
                {
                    lessonViewModel.LinkUrl3 = link.URL;
                }
                else
                {
                }

                count++;
            }

            count = 1;
            foreach (var attachment in studentAttachments)
            {
                if (count == 1)
                {
                    lessonViewModel.AttachmentName1 = attachment.Name;
                    ViewBag.Attachment1FileName     = attachment.Path;
                }
                else if (count == 2)
                {
                    lessonViewModel.AttachmentName2 = attachment.Name;
                    ViewBag.Attachment2FileName     = attachment.Path;
                }
                else if (count == 3)
                {
                    lessonViewModel.AttachmentName3 = attachment.Name;
                    ViewBag.Attachment3FileName     = attachment.Path;
                }
                else
                {
                }

                count++;
            }


            //  ViewBag.SecId = new SelectList(db.AspNetClasses, "Id", "ClassName", ClassId);
            // ViewBag.SubId = new SelectList(db.GenericSubjects.Where(x => x.SubjectType == Subject.SubjectType), "Id", "SubjectName", SubjectId);

            ViewBag.SubId   = new SelectList(db.GenericSubjects.Where(x => x.SubjectType == Subject.SubjectType), "Id", "SubjectName", SubjectId);
            ViewBag.TopicId = new SelectList(db.AspnetSubjectTopics.Where(x => x.SubjectId == SubjectId), "Id", "Name", aspnetLesson.TopicId);
            // ViewBag.SessionId = new SelectList(db.AspNetSessions, "Id", "SessionName", LessonSession.SessionId);

            ViewBag.CTId = Subject.SubjectType;

            ViewBag.OrderBy = aspnetLesson.OrderBy;

            return(View(lessonViewModel));
        }
        public ActionResult Create(LessonViewModel LessonViewModel)
        {
            AspnetLesson Lesson = new AspnetLesson();

            Lesson.Name            = LessonViewModel.LessonName;
            Lesson.Video_Url       = LessonViewModel.LessonVideoURL;
            Lesson.TopicId         = LessonViewModel.TopicId;
            Lesson.DurationMinutes = LessonViewModel.LessonDuration;
            Lesson.IsActive        = LessonViewModel.IsActive;
            Lesson.CreationDate    = LessonViewModel.CreationDate;
            Lesson.Description     = LessonViewModel.LessonDescription;
            Lesson.OrderBy         = LessonViewModel.OrderBy;
            Lesson.CreationDate    = DateTime.Now;

            string EncrID = Lesson.Name + Lesson.Description + Lesson.Id;

            Lesson.EncryptedID = Encrpt.Encrypt(EncrID, true);


            var newString = Regex.Replace(Lesson.EncryptedID, @"[^0-9a-zA-Z]+", "s");

            // Lesson.EncryptedID.Replace('/', 's').Replace('-','s').Replace('+','s').Replace('%','s').Replace('&','s');

            Lesson.EncryptedID = newString;
            db.AspnetLessons.Add(Lesson);
            db.SaveChanges();



            //Lesson_Session lessonSession = new Lesson_Session();
            //lessonSession.LessonId = Lesson.Id;
            //lessonSession.SessionId = LessonViewModel.SessionId;
            //lessonSession.StartDate = LessonViewModel.StartDate;
            //lessonSession.DueDate = LessonViewModel.DueDate;
            //db.Lesson_Session.Add(lessonSession);

            //db.SaveChanges();



            HttpPostedFileBase Assignment  = Request.Files["Assignment"];
            HttpPostedFileBase Attachment1 = Request.Files["Attachment1"];
            HttpPostedFileBase Attachment2 = Request.Files["Attachment2"];
            HttpPostedFileBase Attachment3 = Request.Files["Attachment3"];

            if (Assignment.ContentLength > 0)
            {
                var fileName = Path.GetFileName(Assignment.FileName);
                Assignment.SaveAs(Server.MapPath("~/Content/StudentAssignments/") + fileName);
                AspnetStudentAssignment studentAssignment = new AspnetStudentAssignment();

                studentAssignment.FileName = fileName;

                studentAssignment.Name = LessonViewModel.AssignmentName;


                string DueDate = Convert.ToString(LessonViewModel.AssignmentDueDate);


                if (DueDate == "1/1/0001 12:00:00 AM")
                {
                    studentAssignment.DueDate = null;
                }
                else
                {
                    studentAssignment.DueDate = LessonViewModel.AssignmentDueDate;
                }


                studentAssignment.Description  = LessonViewModel.AssignmentDescription;
                studentAssignment.CreationDate = DateTime.Now;
                studentAssignment.LessonId     = Lesson.Id;

                db.AspnetStudentAssignments.Add(studentAssignment);
                db.SaveChanges();
            }

            if (Attachment1.ContentLength > 0)
            {
                var fileName = Path.GetFileName(Attachment1.FileName);
                Attachment1.SaveAs(Server.MapPath("~/Content/StudentAttachments/") + fileName);

                AspnetStudentAttachment studentAttachment1 = new AspnetStudentAttachment();

                studentAttachment1.Name         = LessonViewModel.AttachmentName1;
                studentAttachment1.Path         = fileName;
                studentAttachment1.CreationDate = DateTime.Now;
                studentAttachment1.LessonId     = Lesson.Id;
                db.AspnetStudentAttachments.Add(studentAttachment1);
                db.SaveChanges();
            }
            if (Attachment2.ContentLength > 0)
            {
                var fileName = Path.GetFileName(Attachment2.FileName);
                Attachment2.SaveAs(Server.MapPath("~/Content/StudentAttachments/") + fileName);

                AspnetStudentAttachment studentAttachment2 = new AspnetStudentAttachment();

                studentAttachment2.Name         = LessonViewModel.AttachmentName2;
                studentAttachment2.Path         = fileName;
                studentAttachment2.CreationDate = DateTime.Now;
                studentAttachment2.LessonId     = Lesson.Id;
                db.AspnetStudentAttachments.Add(studentAttachment2);

                db.SaveChanges();
            }

            if (Attachment3.ContentLength > 0)
            {
                var fileName = Path.GetFileName(Attachment3.FileName);
                Attachment3.SaveAs(Server.MapPath("~/Content/StudentAttachments/") + fileName);

                AspnetStudentAttachment studentAttachment3 = new AspnetStudentAttachment();

                studentAttachment3.Name         = LessonViewModel.AttachmentName3;
                studentAttachment3.Path         = fileName;
                studentAttachment3.CreationDate = DateTime.Now;
                studentAttachment3.LessonId     = Lesson.Id;
                db.AspnetStudentAttachments.Add(studentAttachment3);
                db.SaveChanges();
            }

            if (LessonViewModel.LinkUrl1 != null)
            {
                AspnetStudentLink link1 = new AspnetStudentLink();

                link1.URL          = LessonViewModel.LinkUrl1;
                link1.CreationDate = DateTime.Now;
                link1.LessonId     = Lesson.Id;
                db.AspnetStudentLinks.Add(link1);
                db.SaveChanges();
            }

            if (LessonViewModel.LinkUrl2 != null)
            {
                AspnetStudentLink link2 = new AspnetStudentLink();

                link2.URL          = LessonViewModel.LinkUrl2;
                link2.CreationDate = DateTime.Now;
                link2.LessonId     = Lesson.Id;
                db.AspnetStudentLinks.Add(link2);
                db.SaveChanges();
            }


            if (LessonViewModel.LinkUrl3 != null)
            {
                AspnetStudentLink link3 = new AspnetStudentLink();

                link3.URL          = LessonViewModel.LinkUrl3;
                link3.CreationDate = DateTime.Now;
                link3.LessonId     = Lesson.Id;
                db.AspnetStudentLinks.Add(link3);
                db.SaveChanges();
            }



            return(RedirectToAction("ViewTopicsAndLessons", "AspnetSubjectTopics"));
        }