Example #1
0
        public static VoteData CreateVoteData(int maxValue, int split)
        {
            var list = new List <int>(maxValue * split);

            for (int i = 0; i < maxValue * split; i++)
            {
                list.Add(0);
            }
            var data = new VoteData(list, split);

            return(data);
        }
Example #2
0
        public static VoteData CreateVoteData(string values)
        {
            var stringItemList = values.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries).ToList();

            stringItemList.RemoveAt(0);
            var split = Convert.ToInt32(stringItemList[0]);

            stringItemList.RemoveAt(0);
            var data = new VoteData(stringItemList.ConvertAll(s => Convert.ToInt32(s)), split);

            return(data);
        }
Example #3
0
        /// <summary>
        /// Converts object array containing data from Content Repository to string list.
        /// </summary>
        /// <param name="handlerValues">String array with a single string containing average rating and numbers of rating separated by pipe character.</param>
        /// <returns>String list containing average rating and numbers of ratings</returns>
        protected override object ConvertTo(object[] handlerValues)
        {
            var fs = FieldSetting as RatingFieldSetting;

            if (fs == null)
            {
                throw new NotSupportedException("Invalid field setting.");
            }

            var      items = handlerValues[0] as string;
            VoteData data  = string.IsNullOrEmpty(items)
                                ? VoteData.CreateVoteData(fs.Range, fs.Split)
                                : VoteData.CreateVoteData(items);

            return(data);
        }
Example #4
0
        /// <summary>
        /// Converts string list to object array to store data in Content Repository.
        /// </summary>
        /// <param name="value">String list containing average rating and numbers of ratings</param>
        /// <returns>String array with a single string containing average rating and numbers of rating separated by pipe character.</returns>
        protected override object[] ConvertFrom(object value)
        {
            var fs = FieldSetting as RatingFieldSetting;

            if (fs == null)
            {
                throw new NotSupportedException("Invalid field setting.");
            }

            if (fs.Range <= 0)
            {
                throw new NotSupportedException("Range is invalid, must be greater than 0.");
            }

            var voteData = value as VoteData;

            if (voteData == null)
            {
                throw new InvalidCastException("Unable to cast the given value to VoteData.");
            }

            var originalValueData = OriginalValue as VoteData;

            if (originalValueData == null)
            {
                throw new NotSupportedException("Invalid items in the field.");
            }

            //If there was no stored rating or it is invalid, reset votes
            if ((originalValueData.MaxVotes != fs.Range * fs.Split || originalValueData.Split != fs.Split) || voteData.SelectedValue == -1)
            {
                originalValueData = VoteData.CreateVoteData(fs.Range, fs.Split);
            }
            //Adding vote if present
            if (voteData.SelectedValue.HasValue && voteData.SelectedValue.Value > 0)
            {
                if (voteData.OldValue.HasValue && voteData.OldValue.Value > 0)
                {
                    originalValueData.RemoveVote(voteData.OldValue.Value);
                }

                originalValueData.AddVote(voteData.SelectedValue.Value);
            }

            return(new object[] { originalValueData.Serialize(), originalValueData.SumVotes, originalValueData.AverageRate });
        }
Example #5
0
        protected override void ImportData(System.Xml.XmlNode fieldNode, ImportContext context)
        {
            var      text = fieldNode.InnerText;
            VoteData vd   = null;

            var fs = FieldSetting as RatingFieldSetting;

            if (fs == null)
            {
                throw new NotSupportedException("Invalid field setting.");
            }
            if (fs.Range <= 0)
            {
                throw new NotSupportedException("Range is invalid, must be greater than 0.");
            }

            vd = string.IsNullOrEmpty(text) ? VoteData.CreateVoteData(fs.Range, fs.Split) : VoteData.CreateVoteData(text);
            if (vd.MaxVotes != fs.Range * fs.Split || vd.Split != fs.Split)
            {
                vd = VoteData.CreateVoteData(fs.Range, fs.Split);
            }

            this.SetData(vd);
        }
Example #6
0
 public static VoteData CreateVoteData(int maxValue, int split)
 {
     var list = new List<int>(maxValue*split);
     for (int i = 0; i < maxValue*split; i++)
     {
         list.Add(0);
     }
     var data = new VoteData(list, split);
     return data;
 }
Example #7
0
 public static VoteData CreateVoteData(string values)
 {
     var stringItemList = values.Split(new char[] {'|'}, StringSplitOptions.RemoveEmptyEntries).ToList();
     stringItemList.RemoveAt(0);
     var split = Convert.ToInt32(stringItemList[0]);
     stringItemList.RemoveAt(0);
     var data = new VoteData(stringItemList.ConvertAll(s => Convert.ToInt32(s)), split);
     return data;
 }