Poll.cs :  » Content-Management-Systems-CMS » mojoPortal » PollFeature » Business » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » mojoPortal 
mojoPortal » PollFeature » Business » Poll.cs
/// Author:        Christian Fredh
/// Created:      2007-04-29
/// Last Modified:    2009-06-23
/// 
/// The use and distribution terms for this software are covered by the 
/// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
/// which can be found in the file CPL.TXT at the root of this distribution.
/// By using this software in any fashion, you are agreeing to be bound by 
/// the terms of this license.
///
/// You must not remove this notice, or any other, from this software. 

using System;
using System.Data;
using mojoPortal.Business;
using PollFeature.Data;
using System.Collections.Generic;

namespace PollFeature.Business{
    /// <summary>
    /// Represents an instance of a poll
    /// </summary>
    public class Poll
    {
        #region Constructors


        public Poll()
        { }

        public Poll(Guid pollGuid)
        {
            if (pollGuid != Guid.Empty)
            {
                GetPoll(pollGuid);
            }
        }

        public Poll(int moduleID)
        {
            if (moduleID >= 0)
            {
                GetPoll(moduleID);
            }
        }

        #endregion

        #region Private Properties

        private Guid pollGuid = Guid.Empty;
        private Guid siteGuid = Guid.Empty;
        private string question = String.Empty;
        private bool activated = true;
        private int totalVotes = 0;
        private bool anonymousVoting = false;
        private bool allowViewingResultsBeforeVoting = false;
        private bool showOrderNumbers = false;
        private bool showResultsWhenDeactivated = false;
        private DateTime activeFrom = DateTime.UtcNow;
        private DateTime activeTo = DateTime.UtcNow.AddYears(1);
        private Guid newPollGuid = Guid.NewGuid();

        #endregion

        #region Public Properties

        public Guid PollGuid
        {
            get { return pollGuid; }
        }

        public Guid SiteGuid
        {
            get { return siteGuid; }
            set { siteGuid = value; }
        }

        public string Question
        {
            get { return question; }
            set { question = value; }
        }

        public bool IsActive
        {
            get
            {
                if (DateTime.Now < activeFrom) return false;
                if (DateTime.Now > activeTo) return false;
                return activated;
            }
        }

        public int TotalVotes
        {
            get { return totalVotes; }
        }

        public bool AnonymousVoting
        {
            get { return anonymousVoting; }
            set { anonymousVoting = value; }
        }

        public bool AllowViewingResultsBeforeVoting
        {
            get { return allowViewingResultsBeforeVoting; }
            set { allowViewingResultsBeforeVoting = value; }
        }

        public bool ShowOrderNumbers
        {
            get { return showOrderNumbers; }
            set { showOrderNumbers = value; }
        }

        public bool ShowResultsWhenDeactivated
        {
            get { return showResultsWhenDeactivated; }
            set { showResultsWhenDeactivated = value; }
        }

        public DateTime ActiveFrom
        {
            get { return activeFrom; }
            set { activeFrom = value; }
        }

        public DateTime ActiveTo
        {
            get { return activeTo; }
            set { activeTo = value; }
        }

        public bool Activated
        {
            get { return activated; }
            set { activated = value; }
        }

        #endregion


        #region Private Methods

        private void GetPoll(Guid pollGuid)
        {
            using (IDataReader reader = DBPoll.GetPoll(pollGuid))
            {
                GetPoll(reader);
            }
        }

        private void GetPoll(int moduleID)
        {
            using (IDataReader reader = DBPoll.GetPollByModuleID(moduleID))
            {
                GetPoll(reader);
            }
        }

        private void GetPoll(IDataReader reader)
        {
            if (reader.Read())
            {
                this.pollGuid = new Guid(reader["PollGuid"].ToString());
                this.siteGuid = new Guid(reader["SiteGuid"].ToString());
                this.question = reader["Question"].ToString();
                this.activated = Convert.ToBoolean(reader["Active"]);
                this.anonymousVoting = Convert.ToBoolean(reader["AnonymousVoting"]);
                this.allowViewingResultsBeforeVoting = Convert.ToBoolean(reader["AllowViewingResultsBeforeVoting"]);
                this.showOrderNumbers = Convert.ToBoolean(reader["ShowOrderNumbers"]);
                this.showResultsWhenDeactivated = Convert.ToBoolean(reader["ShowResultsWhenDeactivated"]);
                this.activated = Convert.ToBoolean(reader["Active"]);
                this.activeFrom = Convert.ToDateTime(reader["ActiveFrom"]);
                this.activeTo = Convert.ToDateTime(reader["ActiveTo"]);
                try
                {
                    this.totalVotes = int.Parse(reader["TotalVotes"].ToString());
                }
                catch (FormatException) { }
            }
            
        }

        private bool Create()
        {
            pollGuid = newPollGuid;

            int rowsAffected = DBPoll.Add(
                this.pollGuid,
                this.siteGuid,
                this.question,
                this.anonymousVoting,
                this.allowViewingResultsBeforeVoting,
                this.showOrderNumbers,
                this.showResultsWhenDeactivated,
                this.activated,
                this.activeFrom,
                this.activeTo);

            return (rowsAffected > 0);
        }

        private bool Update()
        {
            return DBPoll.Update(
                this.pollGuid,
                this.question,
                this.anonymousVoting,
                this.allowViewingResultsBeforeVoting,
                this.showOrderNumbers,
                this.showResultsWhenDeactivated,
                this.activated,
                this.activeFrom,
                this.activeTo);
        }

        #endregion


        #region Public Methods

        public bool Save()
        {
            if (pollGuid == Guid.Empty) return Create();
            return Update();
        }

        public bool ClearVotes()
        {
            if (totalVotes <= 0) return true; // always correct?

            return DBPoll.ClearVotes(pollGuid);
        }

        public bool Delete()
        {
            if (pollGuid == Guid.Empty) return false;

            return DBPoll.Delete(pollGuid);
        }

        public bool Activate()
        {
            if (pollGuid == Guid.Empty) return false;
            if (DateTime.UtcNow < activeFrom) return false;
            if (DateTime.UtcNow > activeTo) return false;
            activated = true;
            return true;
        }

        public bool Deactivate()
        {
            if (pollGuid == Guid.Empty) return false;
            activated = false;
            return true;
        }

        public bool UserHasVoted(SiteUser user)
        {
            if (user == null) return false;
            //if (anonymousVoting) return false;

            return DBPoll.UserHasVoted(pollGuid, user.UserGuid);
        }

        public bool AddToModule(int moduleID)
        {
            return DBPoll.AddToModule(pollGuid, moduleID);
        }

        public bool CopyToNewPoll(out Poll newPoll)
        {
            newPoll = new Poll();
            newPoll.AllowViewingResultsBeforeVoting = allowViewingResultsBeforeVoting;
            newPoll.AnonymousVoting = anonymousVoting;
            newPoll.Question = question;
            newPoll.ShowOrderNumbers = showOrderNumbers;
            newPoll.ShowResultsWhenDeactivated = showResultsWhenDeactivated;
            newPoll.SiteGuid = siteGuid;

            bool result = newPoll.Save();

            if (result)
            {
                PollOption option;
                List<PollOption> pollOptions = PollOption.GetOptionsByPollGuid(pollGuid);
                foreach (PollOption optionToCopy in pollOptions)
                {
                    option = new PollOption();
                    option.Answer = optionToCopy.Answer;
                    option.Order = optionToCopy.Order;
                    option.PollGuid = newPoll.PollGuid;
                    option.Save();
                }
            }

            return result;
        }

        #endregion

        #region Static methods

        public static IDataReader GetPolls(Guid siteGuid)
        {
            return DBPoll.GetPolls(siteGuid);
        }

        public static IDataReader GetActivePolls(Guid siteGuid)
        {
            return DBPoll.GetActivePolls(siteGuid);
        }

        public static IDataReader GetPollsByUser(Guid userGuid)
        {
            if (userGuid == Guid.Empty) return null;
            return DBPoll.GetPollsByUserGuid(userGuid);
        }

        public static bool RemoveFromModule(int moduleID)
        {
            return DBPoll.RemoveFromModule(moduleID);
        }

        

        public static bool DeleteBySite(int siteId)
        {
            return DBPoll.DeleteBySite(siteId);
        }

        #endregion
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.