示例#1
0
        /// <summary>
        /// 获取并转换为<see cref="QuestionDetails"/>类型数据对象
        /// </summary>
        /// <returns></returns>
        public QuestionDetails ConvertToQuestionDetails()
        {
            if (Question == null)
            {
                return(null);
            }

            //科目名称
            var subjectName = SubjectsAccessor.GetName(Question.SubjectId);
            //课程标题
            var courseTitle = CourseAccessor.GetTitle(Question.CourseId);

            var details = new QuestionDetails
            {
                QuestionId  = Question.QuestionId,
                Answer      = QuestionTools.DeserializeAnswers(Question.Answer, Question.Type),
                Count       = Question.Count,
                CourseId    = Question.CourseId,
                CourseTitle = courseTitle,
                CreateTime  = Question.CreateTime,
                LastTime    = Question.LastTime,
                Marking     = Question.Marking,
                Status      = Question.Status,
                SubjectId   = Question.SubjectId,
                SubjectName = subjectName,
                Topic       = Question.Topic,
                Type        = Question.Type,
                UserId      = Question.UserId
            };

            return(details);
        }
        protected override Dictionary <string, string> ConvertAnswerOptionsToDictionaryFor(string answersJson)
        {
            //答案选项集合
            var answerDic = new Dictionary <string, string>();

            //从答案选项字符串中反序列化成答案对象
            var answers = QuestionTools.DeserializeAnswers(answersJson, (int)QuestionType.SINGLE_CHOICE) as SingleChoiceAnswer;

            if (answers != null && answers.Options != null && answers.Options.Count() > 0)
            {
                answerDic = answers.Options.ToDictionary(k => k.Code, v => v.Answer);
            }

            return(answerDic);
        }
示例#3
0
        public bool ModifyTo(QuestionModifyState state)
        {
            ThrowExceptionIfValidateFailure(() =>
            {
                if (state == null)
                {
                    AddBrokenRule(QuestionEditFailureRule.MODIFY_STATE_CANNOT_EMPTY);
                }
                else if (CanModify())
                {
                    //题目所属课程不存在
                    if (!CourseAccessor.Exists(state.CourseId))
                    {
                        AddBrokenRule(QuestionEditFailureRule.COURSE_NOT_EXISTS);
                    }

                    //题目类型无效
                    if (!((IList)Enum.GetValues(typeof(QuestionType))).Contains((QuestionType)state.Type))
                    {
                        AddBrokenRule(QuestionEditFailureRule.QUESTION_TYPE_ERROR);
                    }

                    //题目标题不能为空
                    if (string.IsNullOrWhiteSpace(state.Topic))
                    {
                        AddBrokenRule(QuestionEditFailureRule.TOPIC_CANNOT_EMPTY);
                    }

                    // 如果->非问答题且答案项为NULL,则抛出错误;
                    // 否则->题目类型与答案项进能校验,校验规则如下:
                    //  1、匹配,则验证答案项设置是否有效
                    //  2、不匹配,则添加业务错误
                    if (state.Type != (int)QuestionType.ESSAY_QUESTION && state.AnswerOptions == null)
                    {
                        AddBrokenRule(QuestionEditFailureRule.ANSWER_OPTIONS_CANNOT_EMPTY);
                    }
                    else if (QuestionTools.CheckAnswerOptionsType(state.AnswerOptions, state.Type))
                    {
                        //题目的答案项验证失败
                        if (!state.AnswerOptions.Validate())
                        {
                            if (state.AnswerOptions is SingleChoiceAnswer)
                            {
                                AddBrokenRule(QuestionEditFailureRule.SINGLE_CHOICE_ANSWER_OPTIONS_ERROR);
                            }
                            else if (state.AnswerOptions is MultipleChoiceAnswer)
                            {
                                AddBrokenRule(QuestionEditFailureRule.MULTIPLE_CHOICE_ANSWER_OPTIONS_ERROR);
                            }
                            else if (state.AnswerOptions is TrueFalseAnswer)
                            {
                                AddBrokenRule(QuestionEditFailureRule.TRUE_FALSE_ANSWER_OPTIONS_ERROR);
                            }
                            else if (state.AnswerOptions is GapFillingAnswer)
                            {
                                AddBrokenRule(QuestionEditFailureRule.GAP_FILLING_ANSWER_OPTIONS_ERROR);
                            }
                        }
                    }
                    else
                    {
                        AddBrokenRule(QuestionEditFailureRule.QUESTION_TYPE_AND_ANSWER_OPTIONS_REGEX_FAILURE);
                    }
                }
                else
                {
                    AddBrokenRule(QuestionEditFailureRule.CANNOT_EDIT);
                }
            });

            if (state.SubjectId == 0)
            {
                state.ReviseProperty();
            }


            return(QuestionsAccessor.Update(
                       ID,
                       state.CourseId,
                       state.SubjectId,
                       state.Type,
                       state.Marking,
                       state.Topic,
                       state.Answer,
                       state.Status));
        }
示例#4
0
        protected override void Validate()
        {
            //题目创建用户不存在
            if (!UsersAccessor.Exists(UserId))
            {
                AddBrokenRule(NewQuestionFailureRule.QUESTION_CREATOR_NOT_EXISTS);
            }

            //题目所属课程不存在
            if (!CourseAccessor.Exists(CourseId))
            {
                AddBrokenRule(NewQuestionFailureRule.COURSE_NOT_EXISTS);
            }

            //题目类型无效
            if (!((IList <int>)Enum.GetValues(typeof(QuestionType))).Contains(Type))
            {
                AddBrokenRule(NewQuestionFailureRule.QUESTION_TYPE_ERROR);
            }

            //题目标题不能为空
            if (string.IsNullOrWhiteSpace(Topic))
            {
                AddBrokenRule(NewQuestionFailureRule.TOPIC_CANNOT_EMPTY);
            }

            // 如果->非问答题且答案项为NULL,则抛出错误;
            // 否则->题目类型与答案项进能校验,校验规则如下:
            //  1、匹配,则验证答案项设置是否有效
            //  2、不匹配,则添加业务错误
            if (Type != (int)QuestionType.ESSAY_QUESTION && AnswerOptions == null)
            {
                AddBrokenRule(NewQuestionFailureRule.ANSWER_OPTIONS_CANNOT_EMPTY);
            }
            else if (QuestionTools.CheckAnswerOptionsType(AnswerOptions, Type))
            {
                //题目的答案项验证失败
                if (!AnswerOptions.Validate())
                {
                    if (AnswerOptions is SingleChoiceAnswer)
                    {
                        AddBrokenRule(NewQuestionFailureRule.SINGLE_CHOICE_ANSWER_OPTIONS_ERROR);
                    }
                    else if (AnswerOptions is MultipleChoiceAnswer)
                    {
                        AddBrokenRule(NewQuestionFailureRule.MULTIPLE_CHOICE_ANSWER_OPTIONS_ERROR);
                    }
                    else if (AnswerOptions is TrueFalseAnswer)
                    {
                        AddBrokenRule(NewQuestionFailureRule.TRUE_FALSE_ANSWER_OPTIONS_ERROR);
                    }
                    else if (AnswerOptions is GapFillingAnswer)
                    {
                        AddBrokenRule(NewQuestionFailureRule.GAP_FILLING_ANSWER_OPTIONS_ERROR);
                    }
                }
            }
            else
            {
                AddBrokenRule(NewQuestionFailureRule.QUESTION_TYPE_AND_ANSWER_OPTIONS_REGEX_FAILURE);
            }
        }