Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AddSynonym(object sender, AddTermSynonymEventArgs <string> e)
        {
            var objTerm        = Terms.CreateAndReturnTerm(e.TermName, VocabularyId);
            var portalSynonyms = Controller.GetTermSynonyms(ModuleContext.PortalId);
            var colMaster      = (from t in portalSynonyms where t.MasterTermId == objTerm.TermId select t).ToList();
            var colRelated     = (from t in portalSynonyms where t.RelatedTermId == objTerm.TermId select t).ToList();

            View.Model.ErrorMessage = "";

            if (colMaster.Count > 0)
            {
                View.Model.ErrorMessage = "AlreadyMaster";
                return;
            }
            if (colRelated.Count > 0)
            {
                var colSameTerm =
                    (from t in portalSynonyms
                     where ((t.MasterTermId == View.Model.SelectedTerm.TermId) && (t.RelatedTermId == objTerm.TermId))
                     select t).ToList();

                View.Model.ErrorMessage = colSameTerm.Count > 0 ? "SameRelationSuggested" : "AlreadyRelated";
                return;
            }

            var objSynonym = new TermSynonymInfo
            {
                MasterTermId    = View.Model.SelectedTerm.TermId,
                RelatedTermId   = objTerm.TermId,
                PortalId        = ModuleContext.PortalId,
                CreatedByUserId = ModuleContext.PortalSettings.UserId,
                CreatedOnDate   = DateTime.Now
            };

            Controller.AddTermSynonym(objSynonym);

            var updatedPortalSynonyms = Controller.GetTermSynonyms(ModuleContext.PortalId);
            var objTermApprove        =
                ThresholdCollection.Single(s => s.Key == Constants.OpThresholds.TermSynonymApproveCount.ToString());
            var objTermReject = ThresholdCollection.Single(s => s.Key == Constants.OpThresholds.TermSynonymRejectCount.ToString());

            View.Model.SuggestedTermSynonyms =
                (from t in updatedPortalSynonyms
                 where ((t.MasterTermId == View.Model.SelectedTerm.TermId) && ((t.Score < objTermApprove.Value) && (t.Score > -objTermReject.Value)))
                 select t).ToList();

            View.ShowSuggestedSynonyms(true);
            View.ShowActiveSynonyms(View.Model.ActiveTermSynonyms.Count > 0);
            View.ShowAddSynonym(true);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Save(object sender, AskQuestionEventArgs <PostInfo, bool, string> e)
        {
            try
            {
                var control = (System.Web.UI.UserControl)sender;

                if (e.Tags.Trim() == "")
                {
                    var minMsg = Localization.GetString("SelectMinTag", LocalResourceFile);
                    //var minCount = 1;
                    //minMsg = minMsg.Replace("{0}", minCount.ToString());

                    UI.Skins.Skin.AddModuleMessage(control, minMsg, ModuleMessage.ModuleMessageType.RedError);
                    return;
                }

                if (ModuleContext.Settings.ContainsKey(Constants.SettingMaxQuestionTags))
                {
                    var enteredTerms = e.Tags.Trim().Split(',').ToList();

                    if (enteredTerms.Count > Convert.ToInt32(ModuleContext.Settings[Constants.SettingMaxQuestionTags]))
                    {
                        var maxMsg = Localization.GetString("SelectMoreTags", LocalResourceFile);
                        maxMsg = maxMsg.Replace("{0}", (ModuleContext.Settings[Constants.SettingMaxQuestionTags]).ToString());

                        UI.Skins.Skin.AddModuleMessage(control, maxMsg, ModuleMessage.ModuleMessageType.RedError);
                        return;
                    }
                }

                // TODO: validate selected tags are not in the disallow collection (module setting, loop through current collection and look for disallowed tags if found show message below)
                //UI.Skins.Skin.AddModuleMessage(this, Localization.GetString("InvalidTag", SharedResourceFile), ModuleMessage.ModuleMessageType.RedError);

                if (ModuleContext.Settings.ContainsKey(Constants.SettingMinBodyChars))
                {
                    if (e.Post.Content.Length > Convert.ToInt32(ModuleContext.Settings[Constants.SettingMaxQuestionTags]))
                    {
                        UI.Skins.Skin.AddModuleMessage(control, Localization.GetString("InvalidBody", LocalResourceFile),
                                                       ModuleMessage.ModuleMessageType.RedError);
                        return;
                    }
                }

                var userEnteredTerms = e.Tags.Split(',')
                                       .Where(s => s != string.Empty)
                                       .Select(p => p.Trim())
                                       .Distinct().ToList();

                foreach (var s in userEnteredTerms)
                {
                    if (!Utils.ContainsSpecialCharacter(s))
                    {
                        continue;
                    }
                    var msg = Localization.GetString("UnAllowedCharacters", LocalResourceFile);
                    msg = msg.Replace("{0}", Constants.DisallowedCharacters);
                    UI.Skins.Skin.AddModuleMessage(control, msg, ModuleMessage.ModuleMessageType.RedError);
                    return;
                }

                var terms = new List <Term>();
                userEnteredTerms.ForEach(t => terms.Add(Terms.CreateAndReturnTerm(t, VocabularyId)));

                var colOpThresholds = QaSettings.GetOpThresholdCollection(Controller.GetQaPortalSettings(ModuleContext.PortalId), ModuleContext.PortalId);
                var objTermApprove  = colOpThresholds.Single(s => s.Key == Constants.OpThresholds.TermSynonymApproveCount.ToString());
                var portalSynonyms  = Controller.GetTermSynonyms(ModuleContext.PortalId);
                var postTerms       = new List <Term>();

                foreach (var term in terms)
                {
                    var matchedSynonym = (from t in portalSynonyms where t.RelatedTermId == term.TermId && t.Score >= objTermApprove.Value select t).SingleOrDefault();
                    if (matchedSynonym != null)
                    {
                        var masterTerm = Terms.GetTermById(matchedSynonym.MasterTermId, VocabularyId);
                        // we have to make sure the masterTerm is not already in the list of terms
                        if (!terms.Contains(masterTerm))
                        {
                            postTerms.Add(masterTerm);
                            // update replaced count (for synonym)
                            Controller.TermSynonymReplaced(matchedSynonym.RelatedTermId, ModuleContext.PortalId);
                        }
                        //else
                        //{
                        //    // show it was removed?
                        //}
                    }
                    else
                    {
                        postTerms.Add(term);
                    }
                }

                e.Post.Terms.AddRange(postTerms);

                e.Post.PortalId = ModuleContext.PortalId;
                e.Post.ModuleID = ModuleContext.ModuleId;
                e.Post.Body     = Utils.ProcessSavePostBody(e.Post.Body);

                // remove line below if you want to enable post approval (changes in ascx too)
                e.Post.Approved = true;

                if (e.Post.Approved)
                {
                    e.Post.ApprovedDate = DateTime.Now;
                }

                e.Post.ParentId           = 0;
                e.Post.TabID              = ModuleContext.TabId;
                e.Post.CreatedUserId      = ModuleContext.PortalSettings.UserId;
                e.Post.CreatedDate        = DateTime.Now;
                e.Post.LastModifiedUserId = ModuleContext.PortalSettings.UserId;
                e.Post.LastModifiedDate   = DateTime.Now;

                // make sure title/body are passing through security filters
                var objSecurity = new PortalSecurity();
                e.Post.Title = objSecurity.InputFilter(e.Post.Title, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoAngleBrackets | PortalSecurity.FilterFlag.NoMarkup);
                e.Post.Body  = objSecurity.InputFilter(e.Post.Body, PortalSecurity.FilterFlag.NoScripting);

                var objPost = Controller.AddPost(e.Post, ModuleContext.TabId);
                Controller.UpdatePost(objPost, ModuleContext.TabId);

                // subscribe user
                if (e.Notify)
                {
                    var objSub = new SubscriptionInfo
                    {
                        CreatedOnDate    = DateTime.Now,
                        PortalId         = ModuleContext.PortalId,
                        PostId           = objPost.PostId,
                        SubscriptionType = (int)Constants.SubscriptionType.InstantPost,
                        UserId           = ModuleContext.PortalSettings.UserId
                    };

                    Controller.AddSubscription(objSub);
                }

                var colUserScoring = QaSettings.GetUserScoringCollection(Controller.GetQaPortalSettings(ModuleContext.PortalId), ModuleContext.PortalId);
                var objScoreLog    = new UserScoreLogInfo
                {
                    UserId              = ModuleContext.PortalSettings.UserId,
                    PortalId            = ModuleContext.PortalId,
                    UserScoringActionId = (int)Constants.UserScoringActions.AskedQuestion,
                    Score =
                        colUserScoring.Single(
                            s => s.Key == Constants.UserScoringActions.AskedQuestion.ToString()).Value,
                    CreatedOnDate = DateTime.Now
                };

                Controller.AddScoringLog(objScoreLog, PrivilegeCollection);

                // if we ever allow moderation/approval to be enabled, this needs to respect that.
                var cntJournal  = new Journal();
                var questionUrl = Links.ViewQuestion(objPost.PostId, objPost.Title, ModuleContext.PortalSettings.ActiveTab, ModuleContext.PortalSettings);

                cntJournal.AddQuestionToJournal(objPost, objPost.Title, ModuleContext.PortalId, ModuleContext.PortalSettings.UserId, questionUrl);
                Response.Redirect(questionUrl, false);
            }
            catch (Exception exception)
            {
                ProcessModuleLoadException(exception);
            }
        }