Exemplo n.º 1
0
        public static CanvasRespondRule FindAnswer(AutoResponderRuleTypes answerType, string sQuestion)
        {
            CanvasRespondRule answerFound = null;

            if (!string.IsNullOrEmpty(sQuestion))
            {
                using (LMSEntities db = new LMSEntities())
                {
                    List <int> lstDiscussionRules = (from dr in db.CanvasRespondRules
                                                     where (dr.CanvasRuleForId == ((int)answerType) &&
                                                            (dr.Active != null && dr.Active.Value == true))
                                                     select dr.CanvasRuleId)
                                                    .ToList();
                    if (lstDiscussionRules != null && lstDiscussionRules.Count > 0)
                    {
                        List <CanvasRespondRuleQuestion> lstQuestions = (from q in db.CanvasRespondRuleQuestions
                                                                         where (q.Active != null && q.Active.Value == true) &&
                                                                         lstDiscussionRules.Contains(q.CanvasRuleId)
                                                                         select q)
                                                                        .ToList();
                        if (lstQuestions != null && lstQuestions.Count > 0)
                        {
                            bool isAnswerFound = false;
                            foreach (var aQuestion in lstQuestions)
                            {
                                if (string.IsNullOrEmpty(aQuestion.CanvasQuestion))
                                {
                                    continue;
                                }
                                if (aQuestion.MatchType != null)
                                {
                                    if (Enum.IsDefined(typeof(AutoResponderConditionType), (int)aQuestion.MatchType.Value))
                                    {
                                        AutoResponderConditionType condition = (AutoResponderConditionType)aQuestion.MatchType.Value;
                                        string sPlainText = RemoveHtmlTags(sQuestion);
                                        if (!string.IsNullOrEmpty(sPlainText))
                                        {
                                            switch (condition)
                                            {
                                            case AutoResponderConditionType.CONTAINS_ANY_WORD:

                                                break;

                                            case AutoResponderConditionType.CONTAINS_STRING:
                                                isAnswerFound = sPlainText.Trim().ToLower().IndexOf(aQuestion.CanvasQuestion.Trim().ToLower()) != -1;
                                                break;

                                            case AutoResponderConditionType.MATCH_STRING:
                                                isAnswerFound = sPlainText.Trim().ToLower().Equals(aQuestion.CanvasQuestion.Trim().ToLower());
                                                break;
                                            }
                                            if (isAnswerFound)
                                            {
                                                answerFound = (from a in db.CanvasRespondRules
                                                               where a.CanvasRuleId == aQuestion.CanvasRuleId
                                                               select a)
                                                              .FirstOrDefault();
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(answerFound);
        }
Exemplo n.º 2
0
        private async Task <object> ProcessDiscussionTopicEntries(CanvasCourseModel oCourse, CanvasCourseDiscussionTopicModel oDiscussionTopics)
        {
            if (oCourse != null && oDiscussionTopics != null)
            {
                if (oDiscussionTopics.unread_count > 0)
                {
                    string sTopicsEntries = await cm.GetCourseDiscussionEntries(oCourse.id, oDiscussionTopics.id) as string;

                    if (!string.IsNullOrEmpty(sTopicsEntries))
                    {
                        List <CanvasDiscussionTopicEntriesModel> lstTopicEntries = sTopicsEntries.FromJson <List <CanvasDiscussionTopicEntriesModel> >();
                        if (lstTopicEntries != null && lstTopicEntries.Count > 0)
                        {
                            int iTopicsCount = lstTopicEntries.Count;
                            canvasLogger.Log(string.Format("Discussion topic '{0}' of '{1}' has {2} Entrie(s).", oDiscussionTopics.title, oCourse.name, lstTopicEntries.Count));
                            lstTopicEntries = (from t in lstTopicEntries
                                               where (t.read_state == "unread")
                                               select t)
                                              .ToList();
                            if (lstTopicEntries.Count > 0)
                            {
                                canvasLogger.Log(string.Format("Total unread entrie(s) for discussion topic '{0}' of course {1} : {2}", oDiscussionTopics.title, oCourse.name, lstTopicEntries.Count));
                                List <int> lstDiscussionRules = (from dr in db.CanvasRespondRules
                                                                 where (dr.CanvasRuleForId == ((int)AutoResponderRuleTypes.DISCUSSION) &&
                                                                        (dr.Active != null && dr.Active.Value == true))
                                                                 select dr.CanvasRuleId)
                                                                .ToList();
                                if (lstDiscussionRules.Count > 0) /* If any discussion rule found */
                                {
                                    List <CanvasRespondRuleQuestion> lstQuestions = (from q in db.CanvasRespondRuleQuestions
                                                                                     where (q.Active != null && q.Active.Value == true) &&
                                                                                     lstDiscussionRules.Contains(q.CanvasRuleId)
                                                                                     select q)
                                                                                    .ToList();
                                    if (lstQuestions != null && lstQuestions.Count > 0) /* If any discussion question found */
                                    {
                                        foreach (var aEntry in lstTopicEntries)
                                        {
                                            if (string.IsNullOrEmpty(aEntry.message))
                                            {
                                                continue;
                                            }

                                            bool isAnswerFound = false;
                                            foreach (var aQuestion in lstQuestions)
                                            {
                                                CanvasAnsweringRules aAnswer = null;
                                                if (string.IsNullOrEmpty(aQuestion.CanvasQuestion))
                                                {
                                                    continue;
                                                }
                                                if (aQuestion.MatchType != null)
                                                {
                                                    if (Enum.IsDefined(typeof(AutoResponderConditionType), (int)aQuestion.MatchType.Value))
                                                    {
                                                        AutoResponderConditionType condition = (AutoResponderConditionType)aQuestion.MatchType.Value;
                                                        string sPlainText = RemoveHtmlTags(aEntry.message);
                                                        if (!string.IsNullOrEmpty(sPlainText))
                                                        {
                                                            switch (condition)
                                                            {
                                                            case AutoResponderConditionType.CONTAINS_ANY_WORD:

                                                                break;

                                                            case AutoResponderConditionType.CONTAINS_STRING:
                                                                isAnswerFound = sPlainText.Trim().ToLower().IndexOf(aQuestion.CanvasQuestion.Trim().ToLower()) != -1;
                                                                break;

                                                            case AutoResponderConditionType.MATCH_STRING:
                                                                isAnswerFound = sPlainText.Trim().ToLower().Equals(aQuestion.CanvasQuestion.Trim().ToLower(), StringComparison.InvariantCultureIgnoreCase);
                                                                break;
                                                            }
                                                        }
                                                        if (isAnswerFound)
                                                        {
                                                            var answerFound = (from a in db.CanvasRespondRules
                                                                               where a.CanvasRuleId == aQuestion.CanvasRuleId
                                                                               select a)
                                                                              .FirstOrDefault();
                                                            if (answerFound != null && (!string.IsNullOrEmpty(answerFound.CanvasAnswer)))
                                                            {
                                                                canvasLogger.Log(string.Format("Marking entry '{0}' as read.", aEntry.message));
                                                                await cm.MarkEntryAsRead(oCourse.id, oDiscussionTopics.id, aEntry.id);

                                                                canvasLogger.Log(string.Format("Entry '{0}' marked as read.", aEntry.message));

                                                                canvasLogger.Log(string.Format("Replying to question '{0}' with answer '{1}'.", aEntry.message, answerFound.CanvasAnswer));
                                                                string sResponse = await cm.ReplyToTopicEntry(oCourse.id, oDiscussionTopics.id, aEntry.id, answerFound.CanvasAnswer) as string;

                                                                canvasLogger.Log(string.Format("Question '{0}' replied with answer '{1}'.", aEntry.message, answerFound.CanvasAnswer));
                                                            }
                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                canvasLogger.Log(string.Format("No unread topic(s) for discussion '{0}' of course {1} was found", oDiscussionTopics.title, oCourse.name));
                            }
                        }
                    }
                }
            }

            return(null);
        }