public ImgChoiceAnswerViewModel(ImgChoiceQuestion question, SingleChoiceAnswer answer): base(question)
 {
     foreach (var choice in question.ChoiceList)
     {
         _choices.Add(new TextChoiceViewModelForAnswer((TextChoice) choice, answer, question.QuestionId));
     }
 }
Beispiel #2
0
 public void VoteDown(User.User user)
 {
     if (Form != null)
     {
         FormReply reply = new FormReply()
         {
             Answers   = new List <Answer.Answer>(),
             User      = user,
             Anonymous = user == null,
             Form      = Form
         };
         Answer.Answer answer = new SingleChoiceAnswer()
         {
             SelectedChoice = 0,
             OrderIndex     = 0,
             QuestionIndex  = 0,
         };
         reply.Answers.Add(answer);
         Form.Replies.Add(reply);
     }
     else if (IdeationReply != null)
     {
         IdeationReply.VoteDown(user);
     }
 }
Beispiel #3
0
        private void GetSingleChoiceResult(SingleChoiceAnswer singleChoiceAnswer, Question _question, ref uint count)
        {
            _context.Entry(singleChoiceAnswer).Reference(x => x.Option).Load();

            var question = _question as SingleChoiceQuestion;

            _context.Entry(question).Reference(x => x.RightAnswer).Load();
            singleChoiceAnswer.Score = 0;
            if (singleChoiceAnswer.Option != null)
            {
                if (singleChoiceAnswer.Option == question.RightAnswer)
                {
                    singleChoiceAnswer.Score = question.Score;
                    count++;
                    singleChoiceAnswer.Result = AnswerResult.Right;
                }
                else
                {
                    singleChoiceAnswer.Result = AnswerResult.Wrong;
                }
            }
            else
            {
                singleChoiceAnswer.Result = null;
            }
            _context.SingleChoiceAnswers.Update(singleChoiceAnswer);
        }
Beispiel #4
0
        public IActionResult Reply(FormReplyDTO formReply)
        {
            FormReply reply = new FormReply()
            {
                Answers = new List <Answer>(),
                Email   = formReply.Email
            };

            foreach (FormAnswerDTO formAnswer in formReply.Answers)
            {
                Answer answer;

                switch (formAnswer.FieldType)
                {
                case FieldType.OpenText:
                    answer = new OpenTextAnswer()
                    {
                        Value = formAnswer.Reply
                    };
                    break;

                case FieldType.MultipleChoice:
                    answer = new MultipleChoiceAnswer()
                    {
                        SelectedChoices = formAnswer.MultipleAnswer.ToList()
                    };
                    break;

                case FieldType.DropDown:
                case FieldType.SingleChoice:
                    answer = new SingleChoiceAnswer()
                    {
                        SelectedChoice = formAnswer.SelectedChoice
                    };
                    break;

                case FieldType.Statement:
                    answer = new SingleChoiceAnswer()
                    {
                        SelectedChoice = formAnswer.SelectedChoice
                    };
                    break;

                default:
                    throw new NotSupportedException("Fieldtype not supported yet");
                }
                reply.Answers.Add(answer);
            }

            _formManager.AddFormReply(reply);
            _unitOfWorkManager.Save();

            return(Ok());
        }
Beispiel #5
0
        public void Validate_InvalidAnswerType_Throw()
        {
            // arrange
            var container = CreateContainer();

            var wrapper = new PollAnswerValidatorWrapper(container);

            var instruction = new MultipleChoiceInstruction(new[] { "A", "B" }, null);
            var answer      = new SingleChoiceAnswer("A");

            // act
            Assert.ThrowsAny <Exception>(() => wrapper.Validate(instruction, answer));
        }
        public override QuestionAnswer Deserialize(string serializeData)
        {
            if (string.IsNullOrWhiteSpace(serializeData))
            {
                return(null);
            }

            var options = JsonUtility.JsonDeserialize <List <AnswerChoiceOption> >(serializeData);

            var answer = new SingleChoiceAnswer();

            answer.PushRange(options);

            return(answer);
        }
Beispiel #7
0
        public IActionResult Vote(int id, int vote, [FromServices] UserManager <User> userManager)
        {
            Form form = _formManager.GetForm(id);

            if (form == null)
            {
                return(NotFound());
            }
            User user      = userManager.GetUserAsync(User).Result;
            bool anonymous = false;

            anonymous = user == null;
            FormReply reply = new FormReply()
            {
                User      = user,
                Anonymous = anonymous,
                Answers   = new List <Answer>(),
                Form      = form,
            };
            Answer answer = new SingleChoiceAnswer()
            {
                OrderIndex     = 0,
                QuestionIndex  = 0,
                SelectedChoice = vote
            };

            reply.Answers.Add(answer);
            _formManager.AddFormReply(reply);
            //form.Replies.Add(reply);
            _unitOfWorkManager.Save();

            int upvotes   = form.Replies.Select(f => ((SingleChoiceAnswer)f.Answers[0]).SelectedChoice).Count(c => c == 1);
            int downvotes = form.Replies.Select(f => ((SingleChoiceAnswer)f.Answers[0]).SelectedChoice).Count(c => c == 0);



            _voteHubContext.Clients.Group($"form - {id}").ReceiveUpvote();



            return(Created("", new { upvotes = upvotes, downvotes = downvotes }));
        }
 private void Update()
 {
     if (_model != null)
     {
         _questions.Clear();
         foreach (var question in _model.Questions)
         {
             if (question is TextQuestion)
             {
                 var answer = new TextAnswer();
                 _questions.Add(new TextAnswerViewModel((TextQuestion)question, answer));
                 _filledModel.Answers.Add(answer);
             }
             if (question is ImgChoiceQuestion)
             {
                 var answer = new SingleChoiceAnswer();
                 _questions.Add(new ImgChoiceAnswerViewModel((ImgChoiceQuestion)question, answer));
                 _filledModel.Answers.Add(answer);
             }
         }
     }
     OnNotifyPropertyChanged("Questions");
 }
        public IActionResult PostIdeationReply([FromForm] IdeationReplyDTO ideationReplyDto)
        {
            // return Created("/ideation/overview/1", null);
            //log test 1
            Ideation ideation = _ideationManager.GetIdeationWithQuestions(ideationReplyDto.IdeationId);
            //log test 2
            User user = _usermanager.GetUserAsync(User).Result;

            //log test 3
            if (ideation == null || user == null)
            {
                return(NotFound());
            }
            //log test 4

            IdeationReply newReply = new IdeationReply()
            {
                Ideation = ideation,
                Title    = ideationReplyDto.Title,
                Answers  = new List <Answer>(),
                Votes    = new List <Vote>(),
                Comments = new List <Comment>(),
                Created  = DateTime.Now,
                User     = user,
                Reports  = new List <IdeationReport>()
            };
            //log test 5
            int index = 0;

            ideationReplyDto.Answers.ForEach(dto =>
            {
                Answer newAnswer = null;

                switch (dto.FieldType)
                {
                case FieldType.OpenText:
                    newAnswer = new OpenTextAnswer()
                    {
                        QuestionIndex = dto.QuestionIndex,
                        Value         = dto.OpenAnswer
                    };
                    break;

                case FieldType.Image:
                case FieldType.Video:
                    string fileName = Util.Util.GenerateDataStoreObjectName(dto.FileAnswer.FileName);
                    string pathName = _fileUploader.UploadFile(fileName, "ideationReply", dto.FileAnswer).Result;
                    newAnswer       = new MediaAnswer()
                    {
                        QuestionIndex = dto.QuestionIndex,
                        Value         = new Media()
                        {
                            Name = dto.FileAnswer.FileName,
                            Url  = pathName
                        }
                    };
                    break;

                case FieldType.SingleChoice:
                case FieldType.DropDown:
                    newAnswer = new SingleChoiceAnswer()
                    {
                        QuestionIndex  = dto.QuestionIndex,
                        SelectedChoice = dto.SingleAnswer
                    };
                    break;

                case FieldType.MultipleChoice:
                    newAnswer = new MultipleChoiceAnswer()
                    {
                        QuestionIndex   = dto.QuestionIndex,
                        SelectedChoices = dto.MultipleAnswer
                    };
                    break;

                case FieldType.Location:
                    newAnswer = new LocationAnswer()
                    {
                        QuestionIndex = dto.QuestionIndex,
                        Value         = new Location()
                        {
                            Latitude  = dto.LocationAnswer.Latitude,
                            Longitude = dto.LocationAnswer.Longitude,
                            ZoomLevel = dto.LocationAnswer.ZoomLevel,
                        }
                    };
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                newAnswer.OrderIndex = index++;
                newReply.Answers.Add(newAnswer);
            });
            //log test 6
            _ideationManager.AddIdeationReply(newReply);
            //log test 7
            // Create activity
            var activity = CreateActivity(ActivityType.IdeationReply, user);

            activity.IdeationReply = newReply;
            _activityManager.AddActivity(activity);
            //log test 8
            // Save everything
            _unitOfWorkManager.Save();
            //log test 9
            // Push activity
            var activityVm = new ActivityViewModel(activity);

            //log test 10
            PushWebsockets(activityVm).Wait();
            //log test 11
            return(Created("/ideation/view/" + newReply.IdeationReplyId, new { id = newReply.IdeationReplyId }));
        }
        public IActionResult PostIdeationReplyApp([FromBody] IdeationReplyAppDto ideationReplyApp)
        {
            Ideation ideation = _ideationManager.GetIdeationWithReplies(ideationReplyApp.IdeationId);

            User user = _usermanager.FindByEmailAsync(HttpContext.User.Claims.FirstOrDefault(c => c.Type == "Email").Value).Result;

            IdeationReply newReply = new IdeationReply()
            {
                Ideation = ideation,
                Title    = ideationReplyApp.Title,
                Answers  = new List <Answer>(),
                Votes    = new List <Vote>(),
                Created  = DateTime.Now,
                Comments = new List <Comment>(),
                User     = user,
                Reports  = new List <IdeationReport>()
            };

            int index = 0;

            foreach (var dto in ideationReplyApp.Answers)
            {
                Answer newAnswer = null;

                switch (dto.FieldType)
                {
                case FieldType.OpenText:
                    newAnswer = new OpenTextAnswer()
                    {
                        QuestionIndex = dto.QuestionIndex,
                        Value         = dto.Reply
                    };
                    break;

                case FieldType.SingleChoice:
                case FieldType.DropDown:
                    newAnswer = new SingleChoiceAnswer()
                    {
                        QuestionIndex  = dto.QuestionIndex,
                        SelectedChoice = dto.SelectedChoice
                    };
                    break;

                case FieldType.MultipleChoice:
                    newAnswer = new MultipleChoiceAnswer()
                    {
                        QuestionIndex   = dto.QuestionIndex,
                        SelectedChoices = dto.MultipleAnswer
                    };
                    break;

                case FieldType.Location:
                    newAnswer = new LocationAnswer()
                    {
                        QuestionIndex = dto.QuestionIndex,
                        Value         = new Location()
                        {
                            Latitude  = dto.LocationAnswer.Latitude,
                            Longitude = dto.LocationAnswer.Longitude,
                            ZoomLevel = dto.LocationAnswer.ZoomLevel,
                        }
                    };
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                newAnswer.OrderIndex = index++;
                newReply.Answers.Add(newAnswer);
            }

            // Create activity
            var activity = CreateActivity(ActivityType.IdeationReply, user, ideation.Project.Platform);

            activity.IdeationReply = newReply;
            _activityManager.AddActivity(activity);

            // Save everything
            _unitOfWorkManager.Save();

            // Push activity
            var activityVm = new ActivityViewModel(activity);

            PushWebsockets(activityVm).Wait();

            return(Ok());
        }
Beispiel #11
0
        public IActionResult Reply(FormViewModel formVM)
        {
            if (!ModelState.IsValid)
            {
                return(View(formVM));
            }

            Form form = _formManager.GetForm(formVM.FormId);

            FormReply reply = new FormReply()
            {
                Answers = new List <Answer>(),
                Email   = formVM.Email.Email,
                Form    = form
            };

            int index = 0;

            foreach (FormQuestionViewModel questionVM in formVM.Questions)
            {
                Answer answer;
                bool   isValid = true;
                switch (questionVM.FieldType)
                {
                case FieldType.OpenText:
                    isValid = questionVM.OpenAnswer != null;
                    answer  = new OpenTextAnswer()
                    {
                        QuestionIndex = index,
                        OrderIndex    = index,
                        Value         = questionVM.OpenAnswer
                    };
                    break;

                case FieldType.MultipleChoice:
                    isValid = questionVM.MultipleChoiceAnswer.Contains(true);
                    answer  = new MultipleChoiceAnswer()
                    {
                        QuestionIndex   = index,
                        OrderIndex      = index,
                        SelectedChoices = questionVM.MultipleChoiceAnswer.ToList()
                    };
                    break;

                case FieldType.DropDown:
                case FieldType.SingleChoice:
                case FieldType.Statement:
                    if (questionVM.SingleChoiceAnswer.HasValue)
                    {
                        answer = new SingleChoiceAnswer()
                        {
                            QuestionIndex  = index,
                            OrderIndex     = index,
                            SelectedChoice = questionVM.SingleChoiceAnswer.Value
                        };
                    }
                    else
                    {
                        answer  = null;
                        isValid = false;
                    }

                    break;

                default:
                    throw new NotSupportedException("Fieldtype not supported yet");
                }

                if (isValid)
                {
                    reply.Answers.Add(answer);
                }
                index++;
            }
            _formManager.AddFormReply(reply);
            _unitOfWorkManager.Save();

            return(RedirectToAction("Confirmation", new { email = reply.Email }));
        }
Beispiel #12
0
        public async Task <IActionResult> Start(int testResultId, ErrorViewModel model)
        {
            var user = await _userManager.GetUserAsync(HttpContext.User);

            var testResult = await _context.TestResults
                             .Include(tr => tr.Test)
                             .ThenInclude(t => t.Questions)
                             .SingleAsync(t => t.Id == testResultId);

            if (testResult == null)
            {
                return(NotFound());
            }
            if (!testResult.Test.IsEnabled)
            {
                return(Forbid());
            }
            var questions = testResult.Test.Questions.Where(q => !q.IsDeleted).ToList();

            if (questions.Count() == 0)
            {
                return(NotFound());
            }
            if (_context.Answers.Any(a => a.TestResult == testResult))
            {
                return(RedirectToAction("Answer", "Answer",
                                        new
                {
                    testResultId = testResult.Id,
                    answerId = _context.Answers.Where(a => a.TestResult == testResult)
                               .SingleOrDefault(a => a.Order == 1).Id
                }));
            }
            var    answers = new List <Answer>();
            Answer answer  = null;

            if (testResult.Test.Count != 0 && testResult.Test.Count < questions.Count && testResult.Test.Shuffled)
            {
                var order = new ushort[testResult.Test.Count];
                for (var i = 0; i < order.Length; i++)
                {
                    order[i] = (ushort)(i + 1);
                }
                if (testResult.Test.Shuffled)
                {
                    Shuffle(order);
                }
                var j = 0;
                Shuffle(questions);
                for (var k = 0; k < order.Length; k++)
                {
                    var question = questions[k];
                    switch (question.QuestionType)
                    {
                    case "SingleChoiceQuestion":
                        answer = new SingleChoiceAnswer();
                        break;

                    case "MultiChoiceQuestion":
                        answer = new MultiChoiceAnswer();
                        break;

                    case "TextQuestion":
                        answer = new TextAnswer();
                        break;

                    case "DragAndDropQuestion":
                        answer = new DragAndDropAnswer();
                        break;

                    case "CodeQuestion":
                        answer = new CodeAnswer();
                        break;
                    }

                    if (answer == null)
                    {
                        throw new NullReferenceException();
                    }
                    answer.Result     = null;
                    answer.Question   = question;
                    answer.Score      = 0;
                    answer.TestResult = testResult;
                    answer.Order      = order[j++];
                    await _context.Answers.AddAsync(answer);

                    answers.Add(answer);
                    await _context.SaveChangesAsync();
                }
            }
            else
            {
                var order = new ushort[questions.Count()];
                for (var i = 0; i < order.Length; i++)
                {
                    order[i] = (ushort)(i + 1);
                }
                if (testResult.Test.Shuffled)
                {
                    Shuffle(order);
                }
                var j = 0;
                foreach (var question in questions)
                {
                    switch (question.QuestionType)
                    {
                    case "SingleChoiceQuestion":
                        answer = new SingleChoiceAnswer();
                        break;

                    case "MultiChoiceQuestion":
                        answer = new MultiChoiceAnswer();
                        break;

                    case "TextQuestion":
                        answer = new TextAnswer();
                        break;

                    case "DragAndDropQuestion":
                        answer = new DragAndDropAnswer();
                        break;

                    case "CodeQuestion":
                        answer = new CodeAnswer();
                        break;
                    }

                    if (answer == null)
                    {
                        throw new NullReferenceException();
                    }
                    answer.Question   = question;
                    answer.Score      = 0;
                    answer.TestResult = testResult;
                    answer.Order      = order[j++];
                    await _context.Answers.AddAsync(answer);

                    answers.Add(answer);
                    await _context.SaveChangesAsync();
                }
            }
            testResult.StartedOn = DateTime.UtcNow;
            _context.TestResults.Update(testResult);
            await _context.SaveChangesAsync();

            // TODO: redirect to first answer (question)
            //throw new NotImplementedException();
            return(RedirectToAction("Answer", "Answer",
                                    new { testResultId = testResult.Id, answerId = answers.SingleOrDefault(a => a.Order == 1).Id }));
        }
Beispiel #13
0
        private void StartTest(User user, TestResult testResult, Test test)
        {
            var    answers   = new List <Answer>();
            Answer answer    = null;
            var    questions = testResult.Test.Questions.Where(q => !q.IsDeleted).ToList();

            testResult.TotalQuestions = (uint)questions.Count;
            if (test.Count != 0 && test.Count < test.Questions.Count && testResult.Test.Shuffled)
            {
                var order = new ushort[testResult.Test.Count];
                for (var i = 0; i < order.Length; i++)
                {
                    order[i] = (ushort)(i + 1);
                }

                var j = 0;
                for (var k = 0; k < order.Length; k++)
                {
                    var question = questions[k];
                    switch (question.QuestionType)
                    {
                    case "SingleChoiceQuestion":
                        answer = new SingleChoiceAnswer();
                        break;

                    case "MultiChoiceQuestion":
                        answer = new MultiChoiceAnswer();
                        break;

                    case "TextQuestion":
                        answer = new TextAnswer();
                        break;

                    case "DragAndDropQuestion":
                        answer = new DragAndDropAnswer();
                        break;

                    case "CodeQuestion":
                        answer = new CodeAnswer();
                        break;
                    }

                    if (answer == null)
                    {
                        throw new NullReferenceException();
                    }
                    answer.Result     = null;
                    answer.Question   = question;
                    answer.Score      = 0;
                    answer.TestResult = testResult;
                    answer.Order      = order[j++];
                    _context.Answers.Add(answer);
                    answers.Add(answer);
                    _context.SaveChanges();
                }
            }
            else
            {
                var order = new ushort[questions.Count()];
                for (var i = 0; i < order.Length; i++)
                {
                    order[i] = (ushort)(i + 1);
                }

                var j = 0;
                foreach (var question in questions)
                {
                    switch (question.QuestionType)
                    {
                    case "SingleChoiceQuestion":
                        answer = new SingleChoiceAnswer();
                        break;

                    case "MultiChoiceQuestion":
                        answer = new MultiChoiceAnswer();
                        break;

                    case "TextQuestion":
                        answer = new TextAnswer();
                        break;

                    case "DragAndDropQuestion":
                        answer = new DragAndDropAnswer();
                        break;

                    case "CodeQuestion":
                        answer = new CodeAnswer();
                        break;
                    }

                    if (answer == null)
                    {
                        throw new NullReferenceException();
                    }
                    answer.Question   = question;
                    answer.Score      = 0;
                    answer.TestResult = testResult;
                    answer.Order      = order[j++];
                    _context.Answers.Add(answer);
                    answers.Add(answer);
                    _context.SaveChanges();
                }
            }

            testResult.StartedOn = DateTime.UtcNow;
            _context.TestResults.Update(testResult);
            _context.SaveChanges();
        }