public ActionResult CreateQuestion(Question data, string tags)
        {
            if (Request.IsAuthenticated && (!tags.Equals("")))
            {
                //Create a list for the tags
                var tagList = new List<String>();
                var manager = new QuestionManager();

                //Split the given string by commas
                var boxtags = tags.Split(',');

                //If the last character is a comma, create a list without that
                if (boxtags[boxtags.Length - 1].Equals(" "))
                    tagList = boxtags.Take(boxtags.Length - 1).ToList();
                else
                    tagList = boxtags.ToList();

                //Remove all leading and trailing whitespaces from the tage
                var tagsWithoutWhitespaces = new List<String>();
                foreach (var item in tagList)
                {
                    tagsWithoutWhitespaces.Add(item.Trim());
                }

                //If the created question is valid, add a new question to the database
                if (ModelState.IsValid)
                {
                    manager.AddQuestion(data, WebSecurity.GetUserId(User.Identity.Name), tagsWithoutWhitespaces);
                }
            }

            return RedirectToAction("Index");
        }
        /// <summary>
        /// Votes for a questions
        /// </summary>
        /// <param name="questionid">Question Id</param>
        /// <param name="vote">Vote: +1 or -1</param>
        /// <returns></returns>
        public ActionResult VoteForQuestion(int questionid, int vote)
        {
            if (!Request.IsAuthenticated)
            {
                //If the user is not logged in, we create an error message
                return Json(Resources.Global.YouHaveToLoginBeforeVote);
            }
            else
            {
                //We check if the user already voted for this question
                var vman = new VoteManager();
                bool isvoted = vman.IsVotedForQuestion(questionid, WebSecurity.GetUserId(User.Identity.Name));
                if (isvoted)
                {
                    return Json(Resources.Global.YouAlreadyVotedForThisQuestion);
                }
                //We check if the user want to vote for his own question
                var qman = new QuestionManager();
                if (qman.GetQuestion(questionid).UserId == WebSecurity.GetUserId(User.Identity.Name))
                {
                    return Json(Resources.Global.YouCantVoteForYourOwnQuestion);
                }
                //We store the vote
                vman.Vote(questionid, WebSecurity.GetUserId(User.Identity.Name), vote);

                //We returns the new vote of the question
                return Json(qman.GetVote(questionid));

            }
        }
 /// <summary>
 /// Displays the questions which contains one of the favorite tags of the user
 /// </summary>
 /// <param name="userid">User Id</param>
 /// <returns></returns>
 public ActionResult QuestionsToUser()
 {
     var qman = new QuestionManager();
     ViewBag.QuestionTitle = Resources.Global.LatestUnansweredQuestionsWithYourFavoriteSpeciality;
     var questions = qman.GetQuestionsFitToUser(WebSecurity.CurrentUserId);
     if (questions.Count == 0)
     {
         return new EmptyResult();
     }
     else
     {
         return PartialView("_ShowQuestions", MyHelpers.ToQuestionIndexModel(qman.GetQuestionsFitToUser(WebSecurity.CurrentUserId)));
     }
 }
        /// <summary>
        /// Shows the page of a tag
        /// </summary>
        /// <param name="id">Tag id</param>
        /// <param name="page">Page number</param>
        /// <returns></returns>
        public ActionResult Tag(int id, int? page)
        {
            var actpage = page ?? 1;
            var qman = new QuestionManager();
            var tman = new TagManager();
            int total;
            var allresult = qman.AllQuestionToOneTagToPagedList(id,actpage,10,out total);
            ViewBag.OnePageOfProducts = new StaticPagedList<Question>(allresult, actpage, 10, total);
            ViewBag.Page = actpage;
            ViewBag.Tag = tman.GetTagById(id);
            ViewBag.MorePage = total - allresult.Count();

            return View();
        }
        /// <summary>
        /// List the latest questions, their votes and the number of answers for them
        /// </summary>
        /// <returns></returns>
        public ActionResult IndexQuestions()
        {
            var qman = new QuestionManager();

            List<Question> question = qman.GetXLatestQuestion(10);

            //We create the viewmodel from the questions
            List<QuestionIndexModel> qim = MyHelpers.ToQuestionIndexModel(question);

            return PartialView("_ShowQuestions", qim);
        }
 /// <summary>
 /// Lists the tags
 /// </summary>
 /// <returns></returns>
 public ActionResult ListAllTags()
 {
     //Manager classes
     var tim = new TagManager();
     var qim = new QuestionManager();
     var tags = tim.GetAllTag();
     //We list the tags
     var retlist = new List<TagListModel>();
     foreach (var item in tags)
     {
         var add = new TagListModel();
         add.Tag = item;
         int count;
         qim.AllQuestionToOneTagToPagedList(item.Id, 1, 1, out count);
         add.Questions = count;
         retlist.Add(add);
     }
     return View(retlist.OrderByDescending(q => q.Questions).ToList());
 }
        /// <summary>
        /// Lists questions which related to one other. It based on the tags.
        /// </summary>
        /// <param name="questionid">Questions Id</param>
        /// <returns></returns>
        public ActionResult GetRelatedQuestions(int questionid)
        {
            var qim = new QuestionManager();

            //We create a partial view, which contains the related questions
            return PartialView("_GetRelatedQuestions", qim.GetRelatedQuestions(questionid));
        }
        /// <summary>
        /// Shows the page of a user
        /// </summary>
        /// <param name="id">User id</param>
        /// <returns></returns>
        public ActionResult GetUser(int id, int? questionpage, int? answerpage)
        {
            var usermanager = new UserManager();
            var questionmanager = new QuestionManager();
            var answermanager = new AnswerManager();
            var tagmanager = new TagManager();
            var ucm = new UserControllerModel();

            //If the pages are not given, set it to 1
            var actquestionpage = questionpage ?? 1;
            var actanswerpage = answerpage ?? 1;

            //Get the questions for the actual page
            int totalquestions;
            var retrievedQuestions = questionmanager.AllQuestionToOneUserToPagedList(id, actquestionpage, 5, out totalquestions);
            //Convert it to a StaticPagedList for the pager
            var actualQuestions = new StaticPagedList<Question>(retrievedQuestions, actquestionpage, 5, totalquestions);

            //Add it to the model
            ucm.Questions = actualQuestions;

            //Set the actual pagenumbers
            ViewBag.QuestionPage = actquestionpage;
            ViewBag.AnswerPage = actanswerpage;

            //Add the actual user to the model
            ucm.UserProfile = usermanager.GetUserById(id);

            //Get the answered questions by the user for the actual page
            int totalanswers;
            var retrievedAnsweredQuestions = questionmanager.AnsweredQuestionsToPagedList(id, actanswerpage, 5, out totalanswers);
            //Convert it to a StaticPagedList for the pager
            var actualAnsweredQuestions = new StaticPagedList<Question>(retrievedAnsweredQuestions, actanswerpage, 5, totalanswers);

            //Add it to the model
            ViewBag.AnsweredQuestions = actualAnsweredQuestions;

            //Add the reputation of the user to the model
            ucm.UserRating = usermanager.GetUserRating(id);

            //Create a list with the answered questions
            var answeredquestions = new List<GetQuestionModels>();
            foreach (var item in actualAnsweredQuestions)
            {

                var actansweredquestion = new GetQuestionModels();
                //Add the current question to the model
                actansweredquestion.CurrentQuestion = item;
                //Add the answers by the user of the current question to the model
                actansweredquestion.Answers = answermanager.GetAllAnswerToOneQuestionFromOneUser(item.Id, ucm.UserProfile.UserId);
                //Add the vote of current question to the model
                actansweredquestion.Vote = questionmanager.GetVote(item.Id);
                //Add the tags of the current question to the model
                actansweredquestion.QuestionTags = tagmanager.GetAllTagToOneQuestion(item.Id);

                //Create a dictionary for the answers and the votes of them
                actansweredquestion.AnswerVotes = new Dictionary<Answer, int>();

                //Upload the dictionary with the votes
                foreach (var answer in actansweredquestion.Answers)
                {
                    actansweredquestion.AnswerVotes.Add(answer, answermanager.GetVote(answer.Id));
                }

                //Add the author of the question to the model
                actansweredquestion.QuestionUser = usermanager.GetUserById(item.UserId);

                //Sort the answers by the votes of them
                actansweredquestion.Answers = actansweredquestion.Answers.OrderByDescending(d => actansweredquestion.AnswerVotes[d]).ToList();

                //Add this QuestionModel to the list
                answeredquestions.Add(actansweredquestion);
            }

            //Add the list of the questionmodels to the model
            ucm.AnsweredQuesitions = answeredquestions;

            return View(ucm);
        }
        public ActionResult GetQuestion(int id)
        {
            //Manager objects
            var manager = new QuestionManager();
            var ansman = new AnswerManager();
            var usman = new UserManager();
            var tagman = new TagManager();

            var actQues = manager.GetQuestion(id);
            var model = new GetQuestionModels();
            model.CurrentQuestion = actQues;

            //We gets the answers which belong to this question
            model.Answers = ansman.GetAllAnswerToOneQuestion(id);
            //We gets the vote of the question
            model.Vote = manager.GetVote(model.CurrentQuestion.Id);
            //We lists the tags of the question
            model.QuestionTags = tagman.GetAllTagToOneQuestion(id);

            //We gets the votes of the answers
            Dictionary<Answer, int> ansvotes = new Dictionary<Answer, int>();
            Dictionary<Answer, UserProfile> answeruser = new Dictionary<Answer, UserProfile>();
            foreach (var item in model.Answers)
            {
                int vote = ansman.GetVote(item.Id);
                UserProfile userprof = usman.GetUserById(item.UserId);
                answeruser.Add(item, userprof);
                ansvotes.Add(item, vote);
            }
            //We creates the viewmodel
            model.AnswerVotes = ansvotes;
            model.AnswerUser = answeruser;
            model.QuestionUser = usman.GetUserById(model.CurrentQuestion.UserId);
            model.Answers = model.Answers.OrderByDescending(d => model.AnswerVotes[d]).ToList();
            return View(model);
        }
 /// <summary>
 /// Displays all questions at the site by pages
 /// </summary>
 /// <param name="page">Page number</param>
 /// <returns></returns>
 public ActionResult GetAllQuestions(int? page)
 {
     var actpage = page ?? 1;
     var qman = new QuestionManager();
     int total;
     var questions=qman.AllQuestionsToPagedList(actpage, 10, out total);
     ViewBag.OnePageOfProducts = new StaticPagedList<Question>(questions, actpage, 10, total);
     ViewBag.Page = actpage;
     ViewBag.MorePage = total - (ViewBag.OnePageOfProducts as IEnumerable<Question>).Count();
     return View();
 }
        /// <summary>
        /// Lists the questions via Ajax
        /// </summary>
        /// <param name="prefix">Prefix of the question's title which we look for in tags</param>
        /// <returns></returns>
        public ActionResult FindQuestion(string prefix)
        {
            var manager = new QuestionManager();

            Regex rgx = new Regex("[^a-zA-Z0-9 -]");
            //We remove the characters which can't included in the tags
            prefix = rgx.Replace(prefix, "");
            //We get the wordlist
            var wordlist = prefix.Split(' ');

            List<Question> quesList = null;
            foreach (var item in wordlist)
            {
                if (quesList != null)
                {

                    var temp = manager.AllQuestionToTag(item);
                    if (temp != null)
                    {
                        quesList = quesList.Concat(temp).ToList();
                    }
                }
                else
                {
                    quesList = manager.AllQuestionToTag(item);
                }
            }
            //we remove the duplications from the questions
            List<Question> questions = null;
            if(quesList!=null)
                 questions = quesList.GroupBy(q => q.Id).Select(g => g.First()).ToList();

            //we creates a KeyValue list
            var allQues = new List<KeyValuePair<int, string>>();
            if (questions != null)
            {
                foreach (var item in questions)
                {
                    allQues.Add(new KeyValuePair<int, string>(item.Id, item.Title));
                }

                var result = (from l in allQues select l).ToList();

                result = (from l in allQues
                          orderby l.Value
                          select l).ToList();

                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return null;
            }
        }
        public ActionResult EditQuestion(Question data, string QuesID)
        {
            data.Id = Int32.Parse(QuesID);
            var manager = new QuestionManager();
            data.UserId = WebSecurity.CurrentUserId;
            if (ModelState.IsValid)
            {
                manager.EditQuestion(data);
            }

            return RedirectToAction("Index");
        }
        public ActionResult EditQuestion(string actId)
        {
            var manager = new QuestionManager();
            int actualId = Int32.Parse(actId);
            //We get the actual question
            var model = manager.GetQuestion(actualId);

            if (model.UserId == WebSecurity.CurrentUserId)
            {
                return View(model);
            }
            //If the user not eligble to edit the question, we returns to home page
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }