Exemplo n.º 1
0
        /// <summary>
        /// The process distractors.
        /// </summary>
        /// <param name="question">
        /// The question.
        /// </param>
        /// <param name="convertedQuestion">
        /// The converted question.
        /// </param>
        /// <param name="saveImage">
        /// The save image.
        /// </param>
        /// <param name="subModuleItem">
        /// The sub module item.
        /// </param>
        /// <param name="creator">
        /// The creator.
        /// </param>
        /// <param name="distractorModel">
        /// The distractor model.
        /// </param>
        private static void ProcessDistractors(
            EdugameQuestion question,
            Question convertedQuestion,
            Func <string, string, File> saveImage,
            SubModuleItem subModuleItem,
            User creator,
            DistractorModel distractorModel)
        {
            foreach (EdugameDistractor distractor in question.Distractors)
            {
                Distractor convertedDistractor = EdugameConverter.Convert(distractor, convertedQuestion);
                File       distractorImage     = saveImage(distractor.ImageName, distractor.Image);
                if (distractorImage != null)
                {
                    convertedDistractor.Image = distractorImage;
                }

                if (convertedDistractor.CreatedBy == null)
                {
                    convertedDistractor.CreatedBy = subModuleItem.Return(x => x.CreatedBy, creator);
                }

                if (convertedDistractor.ModifiedBy == null)
                {
                    convertedDistractor.ModifiedBy = convertedDistractor.CreatedBy;
                }

                if (subModuleItem != null)
                {
                    distractorModel.RegisterSave(convertedDistractor);
                }

                convertedQuestion.Distractors.Add(convertedDistractor);
            }
        }
Exemplo n.º 2
0
        private void CreateDistractors(
            QuestionDTO dto,
            Question question,
            DistractorModel distractorModel,
            QuestionDTO result)
        {
            var distractors = new List <DistractorDTO>();

            foreach (var distractorDTO in dto.distractors)
            {
                distractorDTO.questionId = question.Id;
                ValidationResult distractorValidationResult;
                if (this.IsValid(distractorDTO, out distractorValidationResult))
                {
                    Distractor distractor        = null;
                    bool       savedSuccessfully = false;
                    try
                    {
                        var isDistractorTransient = distractorDTO.distractorId == 0;
                        distractor = isDistractorTransient
                                         ? null
                                         : distractorModel.GetOneById(distractorDTO.distractorId).Value;
                        distractor = DistractorService.ConvertDto(
                            distractorDTO,
                            distractor,
                            false,
                            this.FileModel,
                            this.QuestionModel,
                            this.UserModel,
                            this.SubModuleItemModel);
                        distractorModel.RegisterSave(distractor, true);
                        savedSuccessfully = true;
                    }
                    finally
                    {
                        if (distractor != null && savedSuccessfully && result != null)
                        {
                            distractors.Add(new DistractorDTO(distractor));
                        }
                    }
                }
            }

            if (result != null)
            {
                result.distractors = distractors.ToArray();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Get questions from imported file.
        /// </summary>
        /// <param name="fileId">
        /// Imported file id.
        /// </param>
        /// <param name="smiId">
        /// Sub module item id.
        /// </param>
        /// <param name="userId">
        /// The user Id.
        /// </param>
        /// <param name="format">
        /// Questions format.
        /// </param>
        /// <returns>
        /// The <see cref="ServiceResponse"/>.
        /// </returns>
        protected QuestionDTO[] Import(string fileId, int?smiId, int?userId, FormatsEnum format)
        {
            var   questionDtos = new List <QuestionDTO>();
            Error error        = null;

            try
            {
                SubModuleItem subModuleItem = smiId.HasValue
                                                  ? this.SubModuleItemModel.GetOneById(smiId.Value).Value
                                                  : null;
                User   creator    = userId.HasValue ? this.UserModel.GetOneById(userId.Value).Value : null;
                string fileName   = fileId + ".xml";
                string filePath   = Path.Combine(this.ImportPath, fileName);
                string schemaPath = Path.Combine(this.SchemasPath, format + ".xsd");

                if (subModuleItem == null && creator == null)
                {
                    throw new ArgumentException();
                }

                EdugameQuestions questions = this.DeserializeEdugameQuestions(format, filePath, schemaPath);

                QuestionModel   questionModel   = this.QuestionModel;
                DistractorModel distractorModel = this.DistractorModel;

                Func <string, string, File> saveImage = this.GetSaveImageRutine(subModuleItem, creator);

                List <QuestionType> questionTypes = this.QuestionTypeModel.GetAllActive().ToList();

                foreach (EdugameQuestion question in questions.Questions)
                {
                    Question convertedQuestion = EdugameConverter.Convert(question, questionTypes);
                    convertedQuestion.SubModuleItem = subModuleItem;
                    if (convertedQuestion.CreatedBy == null)
                    {
                        convertedQuestion.CreatedBy = subModuleItem.Return(x => x.CreatedBy, creator);
                    }

                    if (convertedQuestion.ModifiedBy == null)
                    {
                        convertedQuestion.ModifiedBy = convertedQuestion.CreatedBy;
                    }

                    File questionImage = saveImage(question.ImageName, question.Image);
                    if (questionImage != null)
                    {
                        convertedQuestion.Image = questionImage;
                    }
                    convertedQuestion.RandomizeAnswers =
                        convertedQuestion.QuestionType.Id == (int)QuestionTypeEnum.Sequence
                            ? (bool?)true
                            : null;

                    if (subModuleItem != null)
                    {
                        questionModel.RegisterSave(convertedQuestion);
                    }

                    QuestionFor customQuestion = this.ProcessCustomQuestionType(convertedQuestion, question);

                    ProcessDistractors(question, convertedQuestion, saveImage, subModuleItem, creator, distractorModel);

                    questionDtos.Add(new QuestionDTO(convertedQuestion, customQuestion));
                }

                System.IO.File.Delete(filePath);
            }
            catch (ArgumentException)
            {
                error = new Error(
                    Errors.CODE_ERRORTYPE_REQUEST_NOT_PROCESSED,
                    ErrorsTexts.ImportError_Subject,
                    ErrorsTexts.ImportError_NoData);
            }
            catch (SerializationException)
            {
                error = new Error(
                    Errors.CODE_ERRORTYPE_REQUEST_NOT_PROCESSED,
                    ErrorsTexts.ImportError_Subject,
                    ErrorsTexts.ImportError_InvalidFormat);
            }
            catch (FileNotFoundException)
            {
                error = new Error(
                    Errors.CODE_ERRORTYPE_REQUEST_NOT_PROCESSED,
                    ErrorsTexts.ImportError_Subject,
                    ErrorsTexts.ImportError_NotFound);
            }
            catch (Exception ex)
            {
                Logger.Error("Import", ex);
                error = new Error(
                    Errors.CODE_ERRORTYPE_REQUEST_NOT_PROCESSED,
                    ErrorsTexts.ImportError_Subject,
                    ErrorsTexts.ImportError_Unknown);
            }

            if (error != null)
            {
                this.LogError("Export.Import", error);
                throw new FaultException <Error>(error, error.errorMessage);
            }

            return(questionDtos.ToArray());
        }