예제 #1
0
        // GET: Quizzes/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            if (!id.HasValue)
            {
                return(BadRequest(Constants.ErrorMessages.BadRequest));
            }

            var quiz = await _quizRepository.GetById(id);

            if (quiz == null)
            {
                return(NotFound(Constants.ErrorMessages.NotFound));
            }

            var questions = await _questionRepository.GetByQuizId(id);

            if (questions == null)
            {
                return(NotFound(Constants.ErrorMessages.NotFoundQuestion));
            }

            var model = new DetailsQuizViewModel
            {
                Quiz      = quiz,
                Questions = questions,
            };

            return(View(model));
        }
예제 #2
0
        public IActionResult Details(int id)
        {
            if (id <= 0)
            {
                return(this.NotFound());
            }

            DetailsQuizViewModel model = this.quizzesService.GetQuizWithQuestionsAndAnswersById(id);

            return(this.View(model));
        }
예제 #3
0
        public IActionResult ExportQuizQuestions(int id)
        {
            if (id <= 0)
            {
                return(this.NotFound());
            }

            DetailsQuizViewModel model = this.quizzesService.GetQuizWithQuestionsAndAnswersById(id);

            MemoryStream stream = this.exportService.ExportQuizQuestions(model);

            //Download Word document in the browser
            return(this.File(stream, "application/msword", $"{model.Title}.docx"));
        }
예제 #4
0
        public DetailsQuizViewModel GetQuizWithQuestionsAndAnswersById(int id)
        {
            DetailsQuizViewModel model = this.quizisRepository
                                         .AllAsNoTracking()
                                         .Where(x => x.Id == id)
                                         .To <DetailsQuizViewModel>()
                                         .FirstOrDefault(x => x.Id == id);

            model.Questions = this.questionsService.GetQuestionsByQuizId <QuestionQuizViewModel>(id);

            foreach (var question in model.Questions)
            {
                question.Answers = this.answersService.GetQuestionAnswersById <AnswerViewModel>(question.Id);
            }

            return(model);
        }
예제 #5
0
        public MemoryStream ExportQuizQuestions(DetailsQuizViewModel model)
        {
            WordDocument document = new WordDocument();
            //Adds new section to the document
            IWSection section = document.AddSection();

            IWParagraph paragraph = section.AddParagraph();

            paragraph.ApplyStyle(BuiltinStyle.Heading1);
            paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
            paragraph.AppendText($"{model.CategoryName} - {model.Title}");

            foreach (var question in model.Questions)
            {
                paragraph = section.AddParagraph();
                //Applies default numbered list style
                paragraph.ListFormat.ApplyDefNumberedStyle();
                //Adds text to the paragraph
                paragraph.AppendText($"{question.Value}");

                //Continues the list defined
                paragraph.ListFormat.ContinueListNumbering();
                paragraph = section.AddParagraph();
                paragraph.ListFormat.IncreaseIndentLevel();

                foreach (var answer in question.Answers)
                {
                    paragraph.AppendText($"{answer.Value}");
                    //Continues the list defined
                    paragraph.ListFormat.ContinueListNumbering();
                    paragraph = section.AddParagraph();
                }
                paragraph.ListFormat.DecreaseIndentLevel();
            }

            //Saves the Word document to  MemoryStream
            MemoryStream stream = new MemoryStream();

            document.Save(stream, FormatType.Docx);
            stream.Position = 0;

            //Download Word document in the browser
            return(stream);
        }