Beispiel #1
0
        //[HttpPost]
        public ActionResult QuestionSlide(int?ExamId, int?QuizId, int?skip, int?answerId, int?questionId)
        {
            int chooseAnswerID;

            if (ExamId == null)
            {
                return(HttpNotFound());
            }
            if (QuizId == null)
            {
                return(HttpNotFound());
            }
            skip = skip.HasValue ? skip.Value > 0 ? skip.Value : 0 : 0;
            QuestionViewModel vm = new QuestionViewModel();

            vm.Quiz = db.Quizs.FirstOrDefault(x => x.ID == QuizId);

            vm.Exam     = db.Exams.FirstOrDefault(x => x.ID == ExamId);
            vm.Question = db.Questions.Where(x => x.ExamID == ExamId)
                          .OrderBy(x => x.ID)
                          .Select(x => new QuestionDto()
            {
                ID      = x.ID,
                Name    = x.Name,
                Answers = db.Answers
                          .Where(ans => ans.QuestionID == x.ID).Select(ans => new AnswerDto()
                {
                    ID   = ans.ID,
                    Name = ans.Name
                }).ToList()
            }).Skip(skip.Value).Take(1).First();
            vm.CurrentQuestion += skip.Value;

            vm.TotalQuestionCount = db.Questions.Where(x => x.ExamID == ExamId).Count();
            if (questionId != null && answerId != null)
            {
                MyAnswer chooseAnswer = db.MyAnswers.FirstOrDefault(x => x.QuestionID == questionId && x.QuizID == QuizId);
                if (chooseAnswer == null)
                {
                    MyAnswer ans = new MyAnswer();
                    ans.QuestionID = questionId;
                    ans.AnswerID   = answerId;
                    ans.QuizID     = QuizId;
                    db.MyAnswers.Add(ans);
                    db.SaveChanges();
                    chooseAnswerID = (int)ans.AnswerID;
                }
                else
                {
                    chooseAnswer.AnswerID = answerId;
                    db.SaveChanges();
                }
            }
            else
            {
                questionId = vm.Question.ID;
            }
            vm.MyAnswerId = db.MyAnswers.FirstOrDefault(x => x.QuestionID == questionId && x.QuizID == QuizId);

            return(PartialView("QuestionSlide", vm));
        }
        public IHttpActionResult GetMyAnswers(int id)    //当前登陆者的id !!!
        {
            List <MyAnswer> myanswerlist = new List <MyAnswer>();
            var             myanswers    = from Notes in db.Notes
                                           join Questions in db.Questions on Notes.QuestionId equals Questions.id
                                           join Courses in db.Courses on Questions.CourseId equals Courses.id
                                           join Projects in db.Projects on Courses.ProjectId equals Projects.id
                                           where Notes.UserId == id
                                           select new MyAnswer
            {
                CourseId   = Courses.id,
                CourseName = Courses.CourseName,

                QuestionId      = Questions.id,
                QuestionContent = Questions.QuestionDesc,

                AnswerId = Notes.id,
                Answer   = Notes.Notes,

                PublicFlag = Notes.PublicFlag,
                UserId     = id,
            };

            List <MyAnswer> notelist = myanswers.OrderByDescending(c => c.CourseId).ToList();

            if (notelist.Count == 0)
            {
                myanswerlist = null;
                return(Ok(myanswerlist));    //我没有回答任何问题
            }

            foreach (MyAnswer n in notelist)
            {
                MyAnswer myansweritem = new MyAnswer();
                myansweritem.CourseId   = n.CourseId;
                myansweritem.CourseName = n.CourseName;
                var cou = db.Courses.Where(x => x.id == n.CourseId).FirstOrDefault();
                myansweritem.CouStartTime    = cou.StartDate.ToString();
                myansweritem.QuestionId      = n.QuestionId;
                myansweritem.QuestionContent = n.QuestionContent;
                var qu = db.Questions.Where(x => x.id == n.QuestionId).FirstOrDefault();
                myansweritem.QuReleaseTime = qu.LastUpdateDate.ToString("f");
                myansweritem.AnswerId      = n.AnswerId;
                myansweritem.Answer        = n.Answer;
                var an = db.Notes.Where(x => x.id == n.AnswerId).FirstOrDefault();
                myansweritem.AnswerTime = an.LastUpdateDate.ToString("f");
                myansweritem.PublicFlag = n.PublicFlag;
                myansweritem.UserId     = n.UserId;
                var user = db.Users.Where(x => x.id == n.UserId).FirstOrDefault();
                if (user.Role == "S")
                {
                    var stu = db.Students.Where(x => x.id == user.PersonId).FirstOrDefault();
                    myansweritem.HeadImg = stu.HeadImage;
                    myansweritem.name    = stu.StudentName;
                }
                if (user.Role == "M")
                {
                    var man = db.Managers.Where(x => x.id == user.PersonId).FirstOrDefault();
                    myansweritem.HeadImg = man.HeadImage;
                    myansweritem.name    = man.ManagerName;
                }

                var commentList = from Comments in db.Comments
                                  where Comments.PublicFlag == "Y" && Comments.NoteId == n.AnswerId
                                  select Comments;
                if (commentList.ToList().Count == 0)
                {
                    myansweritem.IsCommented = false;    //该回答暂无评论
                    myansweritem.Comments    = null;
                }
                else
                {
                    myansweritem.IsCommented = true;
                }

                List <AnswerComments> Clists = new List <AnswerComments>();
                foreach (Comment c in commentList.ToList())
                {
                    AnswerComments commentitem = new AnswerComments();
                    commentitem.CommentId   = c.id;
                    commentitem.Comment     = c.Comments;
                    commentitem.CommentTime = c.CreationDate.ToString("f");
                    Collection col2 = db.Collections.FirstOrDefault(p => p.CommentId == c.id && p.UserId == id);   //当前登陆者的userid !!!
                    if (col2 != null)
                    {
                        commentitem.IsCollected = true;
                    }
                    else
                    {
                        commentitem.IsCollected = false;
                    }

                    commentitem.UserId = c.CommentedBy;
                    var user2 = db.Users.Where(x => x.id == c.CommentedBy).FirstOrDefault();
                    if (user2.Role == "S")
                    {
                        var stu = db.Students.Where(x => x.id == user2.PersonId).FirstOrDefault();
                        commentitem.HeadImg = stu.HeadImage;
                        commentitem.name    = stu.StudentName;
                    }
                    if (user2.Role == "M")
                    {
                        var man = db.Managers.Where(x => x.id == user2.PersonId).FirstOrDefault();
                        commentitem.HeadImg = man.HeadImage;
                        commentitem.name    = man.ManagerName;
                    }
                    Clists.Add(commentitem);
                }
                myansweritem.Comments = Clists;
                myanswerlist.Add(myansweritem);
            }
            return(Ok(myanswerlist));
        }