示例#1
0
        public IActionResult Edit(Guid?id, [Bind("UserId,QuestionId,AnswerDate,Text")] AnswerEditModel answerEditModel)
        {
            var answerToBeEdited = _service.GetAnswerById(id.Value);

            if (answerToBeEdited == null)
            {
                return(NotFound());
            }

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

            answerToBeEdited.UserId     = answerEditModel.UserId;
            answerToBeEdited.QuestionId = answerEditModel.QuestionId;
            answerToBeEdited.AnswerDate = answerEditModel.AnswerDate;
            answerToBeEdited.Text       = answerEditModel.Text;

            try
            {
                _service.EditAnswer(answerToBeEdited);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AnswerExists(_service.GetAnswerById(id.Value).Id))
                {
                    return(NotFound());
                }

                throw;
            }

            return(RedirectToAction("Index", "Questions"));
        }
示例#2
0
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var userId = this.User.Identity.GetUserId();
            var answer = this.Data.Answers.GetById(id);

            if (answer == null)
            {
                return(this.HttpNotFound());
            }

            if (answer.AuthorId != userId && !this.User.IsModerator() && !this.User.IsAdmin())
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var model = new AnswerEditModel {
                Id = answer.Id, Content = answer.Content
            };

            return(this.PartialView(model));
        }
示例#3
0
        public ActionResult Edit(AnswerEditModel model)
        {
            if (this.ModelState.IsValid)
            {
                var userId = this.User.Identity.GetUserId();
                var answer = this.Data.Answers.GetById(model.Id);

                answer.Content = model.Content;

                this.Data.Answers.Update(answer);
                this.Data.SaveChanges();

                if (model.Reason != null)
                {
                    var answerUpdate = new AnswerUpdate
                    {
                        AuthorId = userId,
                        AnswerId = answer.Id,
                        Reason   = model.Reason
                    };

                    this.Data.AnswerUpdates.Add(answerUpdate);
                    this.Data.SaveChanges();
                }

                var viewModel = Mapper.Map <AnswerViewModel>(answer);
                viewModel.IsUpdating = true;

                return(this.PartialView("_AnswerDetailPartial", viewModel));
            }

            return(this.JsonError("Content is required"));
        }
示例#4
0
 private Answer MapAnswerEditModelToPersistence(AnswerEditModel answer)
 {
     return(new Answer()
     {
         Id = answer.Id,
         Body = answer.Body
     });
 }
        public async Task <ActionResult> _EditAnswer(Guid answerId)
        {
            AnswerDTO answerDto = await _answerService.GetAnswerById(answerId);

            AnswerEditModel model = _mapper.Map <AnswerEditModel>(answerDto);

            return(PartialView(model));
        }
        public ActionResult _CreateAnswer(Guid questionId)
        {
            AnswerEditModel model = new AnswerEditModel {
                QuestionId = questionId
            };

            return(PartialView(model));
        }
        /*
         * // POST: Question/Delete/5
         * [HttpPost]
         * public ActionResult Delete(int id, FormCollection collection)
         * {
         *  try
         *  {
         *      // TODO: Add delete logic here
         *
         *      return RedirectToAction("Index");
         *  }
         *  catch
         *  {
         *      return View();
         *  }
         * }
         */

        // GET: Answer/Create
        public ActionResult CreateAnswer(int questionID)
        {
            var answerEditModel = new AnswerEditModel()
            {
                QuestionID = questionID
            };

            return(View(answerEditModel));
        }
 public ActionResult CreateAnswer(AnswerEditModel model)
 {
     try
     {
         answerFacade.CreateAnswer(model.Answer, model.QuestionID);
         return(RedirectToAction("Edit", new { id = model.QuestionID }));
     }
     catch
     {
         return(View());
     }
 }
示例#9
0
        protected override void Importing(QuestionnairePart part, ImportContentContext context)
        {
            var root      = context.Data.Element(part.PartDefinition.Name);
            var questions = root.Elements("Question");
            var editModel = _questServices.BuildEditModelForQuestionnairePart(part);

            editModel.MustAcceptTerms = bool.Parse(root.Attribute("MustAcceptTerms").Value);
            editModel.UseRecaptcha    = bool.Parse(root.Attribute("UseRecaptcha").Value);
            editModel.TermsText       = root.Element("TermsText").Value;

            var questionModelList = new List <QuestionEditModel>();

            foreach (var q in questions)   // recupero le questions
            {
                var answers         = q.Elements("Answer");
                var answerModelList = new List <AnswerEditModel>();
                foreach (var a in answers)   // recupero le answers
                {
                    var answerEditModel = new AnswerEditModel {
                        Position = a.Attribute("Position") != null?int.Parse(a.Attribute("Position").Value) : 0,
                                       Published = a.Attribute("Published") != null?bool.Parse(a.Attribute("Published").Value) : false,
                                                       Answer          = a.Attribute("Answer") != null?a.Attribute("Answer").Value                               : "",
                                                       OriginalId      = a.Attribute("OriginalId") != null?int.Parse(a.Attribute("OriginalId").Value)            : 0,
                                                       CorrectResponse = a.Attribute("CorrectResponse") != null?bool.Parse(a.Attribute("CorrectResponse").Value) : false,
                                                       AllFiles        = ImportMedia(a, "AllFiles"),
                                                       GUIdentifier    = a.Attribute("GUIdentifier") != null?a.Attribute("GUIdentifier").Value : "",
                    };
                    answerModelList.Add(answerEditModel);
                }
                var questionEditModel = new QuestionEditModel {
                    Position = q.Attribute("Position") != null?int.Parse(q.Attribute("Position").Value) : 0,
                                   Published = q.Attribute("Published") != null?bool.Parse(q.Attribute("Published").Value) : false,
                                                   Question     = q.Attribute("Question") != null?q.Attribute("Question").Value                                                           : "",
                                                   Section      = q.Attribute("Section") != null?q.Attribute("Section").Value                                                             : "",
                                                   QuestionType = q.Attribute("QuestionType") != null ? (QuestionType)Enum.Parse(typeof(QuestionType), q.Attribute("QuestionType").Value) : QuestionType.SingleChoice,
                                                   AnswerType   = q.Attribute("AnswerType") != null ? (AnswerType)Enum.Parse(typeof(AnswerType), q.Attribute("AnswerType").Value)         : AnswerType.None,
                                                   IsRequired   = q.Attribute("IsRequired") != null?bool.Parse(q.Attribute("IsRequired").Value)                                           : false,
                                                   QuestionnairePartRecord_Id = part.Id,
                                                   Answers       = answerModelList,
                                                   Condition     = q.Attribute("Condition") == null ? null                                                                                     : q.Attribute("Condition").Value,
                                                   ConditionType = q.Attribute("ConditionType") != null ? (ConditionType)Enum.Parse(typeof(ConditionType), q.Attribute("ConditionType").Value) : ConditionType.Show,
                                                   OriginalId    = q.Attribute("OriginalId") != null?int.Parse(q.Attribute("OriginalId").Value)                                                : 0,
                                                   AllFiles      = ImportMedia(q, "AllFiles"),
                                                   GUIdentifier  = q.Attribute("GUIdentifier") != null?q.Attribute("GUIdentifier").Value : ""
                };
                questionModelList.Add(questionEditModel);
            }
            editModel.Questions = questionModelList; // metto tutto nel model
            _questServices.UpdateForContentItem(
                part.ContentItem, editModel);        //aggiorno
        }
        public void DeleteAnswer(AnswerEditModel _model)
        {
            List <Answer> _answers = dataManager.Questions.GetQuestionById(_model.questionId).Answers;
            Answer        _answer  = new Answer();

            foreach (var item in _answers)
            {
                if (item.Text == _model.Name)
                {
                    _answer = item;
                }
            }
            dataManager.Answers.DeleteAnswer(_answer);
        }
示例#11
0
        public ActionResult Edit(AnswerEditModel model)
        {
            if (ModelState.IsValid)
            {
                Answer answer = GetAnswerForCurrentUserById(model.AnswerId);
                if (answer.Poll.WasSent)
                {
                    return(RedirectToAction("Index", "Polls")); // Do not allow modification after poll submission.
                }
                answer.PollAnswer = model.Answer;

                Db.SaveChanges();
                return(RedirectToAction("Edit", "Polls", new { id = answer.PollId }));
            }
            return(View(model));
        }
示例#12
0
        public ActionResult Edit(AnswerEditModel model)
        {
            if (model != null && this.ModelState.IsValid)
            {
                var answer = this.Data.Answers.GetById(model.Id);

                answer.Content   = model.Content;
                answer.IsDeleted = model.IsDeleted;

                this.Data.Answers.Update(answer);
                this.Data.SaveChanges();

                return(this.RedirectToAction("All"));
            }

            return(this.View(model));
        }
示例#13
0
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var answer = this.Data.Answers.GetById(id);

            if (answer == null)
            {
                return(this.HttpNotFound());
            }

            var model = new AnswerEditModel {
                Id = answer.Id, Content = answer.Content, IsDeleted = answer.IsDeleted
            };

            return(this.View(model));
        }
        public async Task <ActionResult> _CreateAnswer(AnswerEditModel model, Guid questionId)
        {
            if (ModelState.IsValid)
            {
                AnswerDTO answerDto = _mapper.Map <AnswerDTO>(model);

                OperationDetails result = await _answerService.CreateAnswer(answerDto);

                if (result.Succeeded)
                {
                    TempData["PartialMessageSuccess"] = result.Message;
                    return(RedirectToAction("_AnswersForQuestion", "Panel", new { questionId = answerDto.QuestionId }));
                }
                else
                {
                    ModelState.AddModelError(result.Property, result.Message);
                }
            }

            return(PartialView(model));
        }
示例#15
0
        // GET: Answers/Edit/5
        public IActionResult Edit(Guid?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var answer = _service.GetAnswerById(id.Value);

            if (answer == null)
            {
                return(NotFound());
            }

            var answerEditModel = new AnswerEditModel(
                answer.UserId,
                answer.QuestionId,
                answer.Text
                );

            return(View(answerEditModel));
        }
        // GET: Answers/Edit/5
        public IActionResult Edit(Guid?id)
        {
            if (id == null)
            {
                return(RedirectToAction("Not_Found", "Home"));
            }

            var answer = _service.GetAnswerById(id.Value);

            if (answer == null)
            {
                return(RedirectToAction("Not_Found", "Home"));
            }

            var answerEditModel = new AnswerEditModel(
                answer.Id,
                answer.UserId,
                answer.QuestionId,
                answer.Text
                );

            return(View(answerEditModel));
        }
示例#17
0
        public ActionResult Edit(AnswerEditModel model)
        {
            if (this.ModelState.IsValid)
            {
                var userId = this.User.Identity.GetUserId();
                var answer = this.Data.Answers.GetById(model.Id);

                if (answer.AuthorId != userId && !this.User.IsModerator() && !this.User.IsAdmin())
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                }

                answer.Content = model.Content;

                this.Data.Answers.Update(answer);
                this.Data.SaveChanges();

                var viewModel = Mapper.Map <AnswerViewModel>(answer);

                return(this.PartialView("_AnswerDetailPartial", viewModel));
            }

            return(this.JsonError("Content is required"));
        }
        public IActionResult CreateOrEditQuestion(int testId, QuestionEditModel questionModel, QuestionsActionType actionType, int answerIdToDelete)
        {
            /*
             *  Method for create/edit test.
             *  Functional :
             *      - add more answer,
             *      - delete selected answer,
             *      - create more question (NextQuestion case),
             *      - take a step to previous question created,
             *      - take a step to forward question created (NextQuestion case),
             *      - remove selected answer,
             *      - remove selected question
             *  Used:
             *      - Enums constant for option identification (path to file PL/Enums/Common)
             *      - Tests and Questions services
             */

            this.logger.LogInformation("CreateOrEditQuestion action");

            this.ViewBag.testId = testId;
            bool detected = false;

            /*the value may be null when we have just created a test and gone to the questions page*/
            if (questionModel.Answers != null)
            {
                foreach (var item in questionModel.Answers)
                {
                    if (item.Name == null)
                    {
                        detected = true;
                    }
                }
            }

            switch (actionType)
            {
            case QuestionsActionType.AddAnswer:
                logger.LogInformation("CreateOrEditQuestion action: Switch AddAnswer");

                questionModel.Answers.Add(new AnswerEditModel());
                break;

            case QuestionsActionType.NextQuestion:
                this.logger.LogInformation("CreateOrEditQuestion action: Switch NextQuestion");

                // if any answer is empty - detected and throw error on frontend
                QuestionEditModel lastModelInList = this.servicesManager.Questions.GetLastQuestionFromTestQuestionsList(testId);
                if (questionModel.Name != null && questionModel.questionId != 0 && /*not last in list*/ lastModelInList.questionId != questionModel.questionId)
                {
                    if (detected)
                    {
                        this.ViewBag.Error = "Answer cannot be empty. Input or delete it.";
                    }
                    else
                    {
                        QuestionEditModel aheadQuestion = this.servicesManager.Questions.GetQuestionThatIsOneStepAheadFromTestQuestionsList(questionModel, testId);
                        this.servicesManager.Questions.SaveQuestionFromViewIntoDb(questionModel);
                        questionModel = aheadQuestion;
                    }
                }
                else if (questionModel.Name == null)
                {
                    this.ViewBag.Error = "Question cannot be empty";
                }
                else
                {
                    if (detected)
                    {
                        this.ViewBag.Error = "Answer cannot be empty. Input or delete it.";
                    }
                    else
                    {
                        this.servicesManager.Questions.SaveQuestionFromViewIntoDb(questionModel);
                        questionModel.Name       = null;
                        questionModel.questionId = 0;
                        questionModel.Answers    = null;
                    }
                }

                this.ModelState.Clear();     // For clear razor state
                break;

            case QuestionsActionType.RemoveAnswer:
                this.logger.LogInformation("CreateOrEditQuestion action: Switch RemoveAnswer");

                AnswerEditModel answerToDelete = questionModel.Answers[answerIdToDelete];
                if (questionModel.Answers.Count > 1)     // if this input not last
                {
                    if (questionModel.questionId != 0)
                    {
                        this.servicesManager.Questions.DeleteAnswer(answerToDelete);
                    }

                    questionModel.Answers.Remove(answerToDelete);
                    this.ModelState.Clear();
                }
                else
                {
                    this.ViewBag.Error = "You cannot delete the last answer";
                }

                break;

            case QuestionsActionType.PreviousQuestion:
                this.logger.LogInformation("CreateOrEditQuestion action: Switch PreviousQuestion");

                if (questionModel.questionId == 0 && questionModel.Name != null && !detected)
                {
                    /*
                     * if the current question have non-empty inputs
                     */
                    QuestionEditModel lastQuestion = this.servicesManager.Questions.GetLastQuestionFromTestQuestionsList(testId);
                    this.servicesManager.Questions.SaveQuestionFromViewIntoDb(questionModel);
                    questionModel = lastQuestion;
                }
                else if (questionModel.questionId != 0)     // If question has been already exist (in db)
                {
                    /*
                     * if the current question has been exist in db (for edit from or if we are already there in previouses questions in test creating form)
                     * logic : indexOf currentQeustuoion in questions by testId and take a step back
                     */

                    List <QuestionEditModel> currentTestQuestions = this.servicesManager.Questions.GetAllEditQuestionsByTestId(testId);
                    int currentQuestionId = this.servicesManager.Questions.GetIndexOfSpecifierQuestionById(testId, questionModel.questionId);
                    QuestionEditModel previousQuestion = currentTestQuestions[currentQuestionId - 1];
                    questionModel = previousQuestion;
                }
                else
                {
                    /*
                     * if current question creating inputs are empty and we need to take step to back
                     */
                    QuestionEditModel lastQuestion = this.servicesManager.Questions.GetLastQuestionFromTestQuestionsList(testId);
                    questionModel = lastQuestion;
                }
                this.ModelState.Clear();
                break;

            case QuestionsActionType.RemoveQuestion:
                this.logger.LogInformation("CreateOrEditQuestion action: Switch RemoveQuestion");

                this.servicesManager.Questions.RemoveEditQuestionFromDb(questionModel);
                QuestionEditModel lastQuestionModel = this.servicesManager.Questions.GetLastQuestionFromTestQuestionsList(testId);
                questionModel = lastQuestionModel != null ? lastQuestionModel : new QuestionEditModel()
                {
                    testId = testId
                };
                this.ModelState.Clear();
                break;

            case QuestionsActionType.Finish:
                if (questionModel.Name != null)
                {
                    if (detected)
                    {
                        this.ViewBag.Error = "Answer cannot be empty. Input or delete it.";
                    }
                    else
                    {
                        this.servicesManager.Questions.SaveQuestionFromViewIntoDb(questionModel);
                        return(this.RedirectToAction("FinishTestCreating", new { testId }));
                    }
                }

                this.ViewBag.Error = "Fields must be filled";
                break;

            default:
                break;
            }

            int questionsAmount = this.servicesManager.Questions.GetAllEditQuestionsByTestId(testId).Count;

            this.ViewBag.QuestionsAmount = questionsAmount;
            int currentQuestionIndexInList = this.servicesManager.Questions.GetIndexOfSpecifierQuestionById(testId, questionModel.questionId);

            this.ViewBag.CurrentQuestionIndex = currentQuestionIndexInList;
            return(this.View(questionModel));
        }
示例#19
0
        public IActionResult StartCreateQuestion(QuestionEditModel _model, Actions actionType, int removingAnswerId)
        {
            int truthCheckCounter = 0;

            foreach (var prop in _model.AnswerEditModels)
            {
                if (prop.Correct)
                {
                    truthCheckCounter++;
                }
                if (truthCheckCounter > 1)
                {
                    ModelState.AddModelError("Truth answers greater then 1", "There can be only one correct answer");
                    return(View(_model));
                }
            }
            //this.ViewBag.testId = testId;
            //_model.Id = testId;
            switch (actionType)
            {
            case Actions.MoreQuestion:
                if (truthCheckCounter == 0)
                {
                    ModelState.AddModelError("Correct not selected", "The correct answer is not selected");
                    return(View(_model));
                }
                _serviceManager.Questions.SaveQuestionEditModelIntoDb(_model);
                _model = new QuestionEditModel()
                {
                    Id = _model.Id
                };
                ModelState.Clear();
                break;

            case Actions.MoreAnswer:
                _model.AnswerEditModels.Add(new AnswerEditModel());
                break;

            case Actions.RemoveAnswer:
                AnswerEditModel _answerToDelete = _model.AnswerEditModels[removingAnswerId];
                if (_model.AnswerEditModels.Count > 1)
                {
                    _model.AnswerEditModels.Remove(_answerToDelete);
                    ModelState.Clear();
                }
                break;

            case Actions.End:
                if (truthCheckCounter == 0)
                {
                    ModelState.AddModelError("Correct not selected", "The correct answer is not selected");
                    return(View(_model));
                }
                _serviceManager.Questions.SaveQuestionEditModelIntoDb(_model);
                //return RedirectToAction("EndCreating", new { testId=_model.Id });
                return(RedirectToAction("MyTests"));

                break;

            default:
                break;
            }
            return(View(_model));
        }