⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 question.cs

📁 wrox c#高级编程
💻 CS
字号:
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
 
namespace Wrox.WebModules.Polls.Business
{
	/// <summary>
	/// Summary description for Question.
	/// </summary>
	public sealed class Question : Wrox.WebModules.Business.BizObject
	{	
		private Configuration.ModuleSettings settings;
		private int questionID;
		private string text;
		private bool isCurrent;
		private bool archived;
		private DateTime addedDate;
		private int totalVotes;
		
		// constructors
		public Question()
		{ 
			settings = Configuration.ModuleConfig.GetSettings();
			ResetProperties();
		}

		public Question(int existingQuestionID)
		{			
			settings = Configuration.ModuleConfig.GetSettings();
			questionID = existingQuestionID;
			LoadFromID();						
		}

		public Question(Question existingQuestion)
		{									
			settings = Configuration.ModuleConfig.GetSettings();
			questionID = existingQuestion.ID;
			LoadFromID();
		}
		
		// retrieve the values for the properties
		private void LoadFromID()
		{
			Data.Questions questions = new Data.Questions(settings.ConnectionString);
			Data.QuestionDetails details = questions.GetDetails(questionID);

			questionID = details.QuestionID;
			text = details.QuestionText;
			isCurrent = details.IsCurrentQuestion;
			archived = details.Archived;
			addedDate = details.AddedDate;
			totalVotes = details.TotalVotes;
		}

		public int LoadFromID(int existingQuestionID)
		{
			questionID = existingQuestionID;
			LoadFromID();
			return questionID;
		}

		// load the current question, if any
		public int LoadCurrent()
		{
			questionID = GetCurrentID();
			
			if (questionID == -1)
				ResetProperties();
			else
				LoadFromID();

			return questionID;
		}

		// reset the properties
		private void ResetProperties()
		{
			questionID = -1;
			text = "";
			isCurrent = false;
			archived = false;
			addedDate = new DateTime();
			totalVotes = 0;
		}

		// create a new record
		public int Create(string questionText, bool isCurrentQuestion, bool questionArchived)
		{		
			Data.Questions questions = new Data.Questions(settings.ConnectionString);
			questionID = questions.Add(questionText, isCurrentQuestion, questionArchived);
			LoadFromID();
			return questionID;
		}

		// update the record represented by this object
		public bool Update()
		{
			Data.Questions questions = new Data.Questions(settings.ConnectionString);
			return questions.Update(questionID, text, isCurrent, archived);
		}

		// delete the record represented by this object
		public bool Delete()
		{
			Data.Questions questions = new Data.Questions(settings.ConnectionString);
			bool ret = questions.Delete(questionID);
			ResetProperties();
			return ret;
		}

		// return the questions
		public static DataSet GetQuestions(bool archivedOnly)
		{
			Configuration.ModuleSettings settings = Configuration.ModuleConfig.GetSettings();
			Data.Questions questions = new Data.Questions(settings.ConnectionString);
			return questions.GetQuestions(archivedOnly);
		}

		public static DataSet GetQuestions()
		{
			return GetQuestions(false);
		}

		// return the ID of the current question
		public static int GetCurrentID()
		{
			Configuration.ModuleSettings settings = Configuration.ModuleConfig.GetSettings();
			return new Data.Questions(settings.ConnectionString).GetCurrentID();
		}
		
		// return a DataSet with the current question and the results, all together
		public static DataSet GetCurrent()
		{
			Configuration.ModuleSettings settings = Configuration.ModuleConfig.GetSettings();

			// get the current question's ID
			Data.Questions questions = new Data.Questions(settings.ConnectionString);
			int questionID = questions.GetCurrentID();

			// if there is no current questions, return an empty DataSet
			if (questionID == -1) return new DataSet();

			// get the options for this question
			Data.Options options = new Data.Options(settings.ConnectionString);
			DataSet currPoll = options.GetOptions(questionID);

			// get the current question's details
			DataRow question = questions.GetDetailsRow(questionID);
			// clone its parent table's structure and add it to the DataSet 
			currPoll.Tables.Add(question.Table.Clone());
			// import the details row into the new table
			currPoll.Tables["Questions"].ImportRow(question);

			// return the Dataset with the two tables
			return currPoll;
		}


		// return the child options
		public DataSet GetOptions()
		{
			Data.Options options = new Data.Options(settings.ConnectionString);
			return options.GetOptions(questionID);
		}

		// add a new child option
		public Option AddOption(string optionText)
		{
			Business.Option option = new Business.Option();
			option.Create(questionID, optionText);
			return option;
		}


		// return whether the user can vote for the specified question. the check is done against
		// the client cookie and the IP stored in the DB. If the user has already voted,
		// check also if the lock period for multiple voted has expired
		public bool AllowVote
		{
			get
			{	
				bool cookieOK = false;
				bool ipOK = false;

				HttpRequest request = HttpContext.Current.Request;
				int duration = settings.LockDuration;
	
				// if the lock period is 0, return true
				if (duration == 0) return true;

				// check the cookie if necessary
				if (settings.LockByCookie)
				{
					if (request.Cookies["Polls_Question" + questionID.ToString()] != null)
						return false;
					else
						// if the cookie is null, we're ok so far
						cookieOK = true;			
				}
				else
					cookieOK = true;

					
				// check the ID if necessary
				if (settings.LockByIP)
				{
					Data.Votes votes = new Data.Votes(settings.ConnectionString);
					int voteID = votes.GetUserVoteID(questionID, request.UserHostAddress.ToString());
					// if GetUserVoteID returned -1, the current user has never voted for this question
					if (voteID == -1)
						ipOK = true;
					else
					{
						// if a vote for this question is found, and the duration time is -1
						// it means that it will never expire --> return false
						if (duration == -1) return false;

						// otherwise check if the LockDuration time has expired
						DateTime addedDate = votes.GetDetails(voteID).AddedDate;
						ipOK = (addedDate.AddDays(duration) <= DateTime.Today);
					}
				}
				else
					ipOK = true;

				return (cookieOK && ipOK);
			}
		}

		public bool Vote(int optionID)
		{
			// before voting, check if the user has already voted,
			// and if so check also if the lock period for multiple votes has expired
			if (AllowVote && questionID != -1 && optionID != -1)
			{
				int voteID;
				int duration = settings.LockDuration;
				if (duration == -1) duration = 365;

				// add the vote
				Data.Votes votes = new Data.Votes(settings.ConnectionString);
				voteID = votes.Add(questionID, optionID, 
					HttpContext.Current.Request.UserHostAddress);

				// add a new cookie for this question
				HttpCookie voteCookie = new HttpCookie("Polls_Question" + questionID.ToString());
				voteCookie.Value = optionID.ToString();
				voteCookie.Expires = DateTime.Today.AddDays(duration);
				HttpContext.Current.Response.Cookies.Add(voteCookie);

				return (voteID != -1);
			}
			else
			{
				return false;
			}
		}

		public bool Vote(Option option)
		{
			return Vote(option.ID);
		}


		// PROPERTIES DEFINED BELOW

		public int ID
		{
			get { return questionID; }
		}

		public string Text
		{
			get { return text; }
			set { text = value; }
		}

		public bool IsCurrent
		{
			get { return isCurrent; }
			set { isCurrent = value; }
		}

		public bool Archived
		{
			get { return archived; }
			set { archived = value; }
		}

		public int TotalVotes
		{
			get { return totalVotes; }
		}

		public DateTime AddedDate
		{
			get { return addedDate; }
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -