PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/mojoPortal.Features.Business/Survey/Survey.cs

#
C# | 355 lines | 203 code | 76 blank | 76 comment | 5 complexity | c6d00869412214bf694b7f3671a22189 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause, CPL-1.0, CC-BY-SA-3.0, GPL-2.0
  1. /// Author: Rob Henry
  2. /// Created: 2007-10-03
  3. /// Last Modified: 2009-06-23
  4. ///
  5. /// The use and distribution terms for this software are covered by the
  6. /// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
  7. /// which can be found in the file CPL.TXT at the root of this distribution.
  8. /// By using this software in any fashion, you are agreeing to be bound by
  9. /// the terms of this license.
  10. ///
  11. /// You must not remove this notice, or any other, from this software.
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Data;
  15. using SurveyFeature.Data;
  16. using System.Globalization;
  17. using mojoPortal.Business;
  18. namespace SurveyFeature.Business
  19. {
  20. /// <summary>
  21. /// Represents a survey
  22. /// </summary>
  23. public class Survey
  24. {
  25. private const string featureGuid = "263ecff1-f321-4bb8-8d0a-64c0ba89caa7";
  26. public static Guid FeatureGuid
  27. {
  28. get { return new Guid(featureGuid); }
  29. }
  30. #region Constructors
  31. public Survey()
  32. {}
  33. public Survey(
  34. Guid surveyGuid)
  35. {
  36. GetSurvey(
  37. surveyGuid);
  38. }
  39. #endregion
  40. #region Private Properties
  41. private Guid surveyGuid = Guid.Empty;
  42. private Guid siteGuid = Guid.Empty;
  43. private string surveyName;
  44. private DateTime creationDate;
  45. private string startPageText;
  46. private string endPageText;
  47. private int pageCount = 0;
  48. private int responseCount = 0;
  49. #endregion
  50. #region Public Properties
  51. public Guid SurveyGuid
  52. {
  53. get { return surveyGuid; }
  54. set { surveyGuid = value; }
  55. }
  56. public Guid SiteGuid
  57. {
  58. get { return siteGuid; }
  59. set { siteGuid = value; }
  60. }
  61. public string SurveyName
  62. {
  63. get { return surveyName; }
  64. set { surveyName = value; }
  65. }
  66. public DateTime CreationDate
  67. {
  68. get { return creationDate; }
  69. set { creationDate = value; }
  70. }
  71. public string StartPageText
  72. {
  73. get { return startPageText; }
  74. set { startPageText = value; }
  75. }
  76. public string EndPageText
  77. {
  78. get { return endPageText; }
  79. set { endPageText = value; }
  80. }
  81. public int PageCount
  82. {
  83. get { return pageCount; }
  84. }
  85. public int ResponseCount
  86. {
  87. get { return responseCount; }
  88. }
  89. #endregion
  90. #region Private Methods
  91. /// <summary>
  92. /// Gets an instance of Survey.
  93. /// </summary>
  94. /// <param name="surveyGuid"> surveyGuid </param>
  95. private void GetSurvey(Guid guidSurveyGuid)
  96. {
  97. using (IDataReader reader = DBSurvey.GetOne(guidSurveyGuid))
  98. {
  99. if (reader.Read())
  100. {
  101. this.surveyGuid = new Guid(reader["SurveyGuid"].ToString());
  102. this.siteGuid = new Guid(reader["SiteGuid"].ToString());
  103. this.surveyName = reader["SurveyName"].ToString();
  104. this.creationDate = Convert.ToDateTime(reader["CreationDate"], CultureInfo.CurrentCulture);
  105. this.startPageText = reader["StartPageText"].ToString();
  106. this.endPageText = reader["EndPageText"].ToString();
  107. this.pageCount = Convert.ToInt32(reader["PageCount"]);
  108. this.responseCount = Convert.ToInt32(reader["ResponseCount"]);
  109. }
  110. }
  111. }
  112. /// <summary>
  113. /// Persists a new instance of Survey. Returns true on success.
  114. /// </summary>
  115. /// <returns></returns>
  116. private bool Create()
  117. {
  118. Guid newID = Guid.NewGuid();
  119. this.surveyGuid = newID;
  120. int rowsAffected = DBSurvey.Add(
  121. this.surveyGuid,
  122. this.siteGuid,
  123. this.surveyName,
  124. DateTime.Now,
  125. this.startPageText,
  126. this.endPageText);
  127. return (rowsAffected > 0);
  128. }
  129. /// <summary>
  130. /// Updates this instance of Survey. Returns true on success.
  131. /// </summary>
  132. /// <returns>bool</returns>
  133. private bool Update()
  134. {
  135. return DBSurvey.Update(
  136. this.surveyGuid,
  137. this.siteGuid,
  138. this.surveyName,
  139. this.creationDate,
  140. this.startPageText,
  141. this.endPageText);
  142. }
  143. #endregion
  144. #region Public Methods
  145. /// <summary>
  146. /// Saves this instance of Survey. Returns true on success.
  147. /// </summary>
  148. /// <returns>bool</returns>
  149. public bool Save()
  150. {
  151. if(this.surveyGuid != Guid.Empty)
  152. {
  153. return Update();
  154. }
  155. else
  156. {
  157. return Create();
  158. }
  159. }
  160. public void AddToModule(int moduleId)
  161. {
  162. DBSurvey.AddToModule(surveyGuid, moduleId);
  163. }
  164. public void RemoveFromModule(int moduleId)
  165. {
  166. DBSurvey.RemoveFromModule(surveyGuid, moduleId);
  167. }
  168. #endregion
  169. #region Static Methods
  170. /// <summary>
  171. /// Deletes an instance of Survey. Returns true on success.
  172. /// </summary>
  173. /// <param name="surveyGuid"> surveyGuid </param>
  174. /// <returns>bool</returns>
  175. public static void Delete(Guid surveyGuid)
  176. {
  177. DBSurvey.Delete(surveyGuid);
  178. }
  179. public static bool DeleteBySite(int siteId)
  180. {
  181. return DBSurvey.DeleteBySite(siteId);
  182. }
  183. public static void DeleteFromModule(int moduleId)
  184. {
  185. DBSurvey.RemoveFromModule(moduleId);
  186. }
  187. ///// <summary>
  188. ///// Returns the count of pages on the given survey
  189. ///// </summary>
  190. ///// <param name="surveyGuid"> pageGuid </param>
  191. ///// <returns></returns>
  192. //public static int GetPageCount(Guid surveyGuid)
  193. //{
  194. // return DB.SurveyPagesCount(surveyGuid);
  195. //}
  196. /// <summary>
  197. /// Gets an IList with all instances of Survey.
  198. /// </summary>
  199. public static List<Survey> GetAll(Guid siteGuid)
  200. {
  201. List<Survey> surveyList
  202. = new List<Survey>();
  203. using (IDataReader reader = DBSurvey.GetAll(siteGuid))
  204. {
  205. while (reader.Read())
  206. {
  207. Survey survey = new Survey();
  208. survey.surveyGuid = new Guid(reader["SurveyGuid"].ToString());
  209. survey.siteGuid = new Guid(reader["SiteGuid"].ToString());
  210. survey.surveyName = reader["SurveyName"].ToString();
  211. survey.creationDate = Convert.ToDateTime(reader["CreationDate"]);
  212. survey.startPageText = reader["StartPageText"].ToString();
  213. survey.endPageText = reader["EndPageText"].ToString();
  214. survey.pageCount = Convert.ToInt32(reader["PageCount"]);
  215. survey.responseCount = Convert.ToInt32(reader["ResponseCount"]);
  216. surveyList.Add(survey);
  217. }
  218. }
  219. return surveyList;
  220. }
  221. public static Guid GetModulesCurrentSurvey(int moduleId)
  222. {
  223. return DBSurvey.GetModulesCurrentSurvey(moduleId);
  224. }
  225. //public static int ResponseCount(Guid surveyGuid)
  226. //{
  227. // return DB.SurveyGetResponseCount(surveyGuid);
  228. //}
  229. public static DataTable GetResultsTable(Guid surveyGuid)
  230. {
  231. IDataReader reader = DBSurvey.GetResults(surveyGuid);
  232. return DatabaseHelper.GetTableFromDataReader(reader);
  233. }
  234. public static List<Result> GetResults(Guid responseGuid)
  235. {
  236. List<Result> results = new List<Result>();
  237. Result result;
  238. using (IDataReader reader = DBSurvey.GetOneResult(responseGuid))
  239. {
  240. while (reader.Read())
  241. {
  242. string pageTitle = reader["PageTitle"].ToString();
  243. string answer = reader["Answer"].ToString();
  244. string questionText = reader["QuestionText"].ToString();
  245. Guid questionGuid = new Guid(reader["QuestionGuid"].ToString());
  246. result = new Result(questionGuid, responseGuid, answer, pageTitle, questionText);
  247. results.Add(result);
  248. }
  249. }
  250. return results;
  251. }
  252. //public static StringBuilder GetResultsCsv(Guid surveyGuid)
  253. //{
  254. // StringBuilder results = new StringBuilder();
  255. // List<SurveyResponse> responses = SurveyResponse.GetAll(surveyGuid);
  256. // foreach (SurveyResponse response in responses)
  257. // {
  258. // IDataReader reader = DB.SurveyResultsGetOne(response.ResponseGuid);
  259. // while (reader.Read())
  260. // {
  261. // string tmpResult = reader["Answer"].ToString();
  262. // if (!tmpResult.Contains(","))
  263. // {
  264. // results.Append(tmpResult + ",");
  265. // }
  266. // else
  267. // {
  268. // results.Append("\"" + tmpResult + "\",");
  269. // }
  270. // }
  271. // if (results.Length > 0) results.Remove(results.Length - 1, 1);
  272. // results.Append(Environment.NewLine);
  273. // reader.Close();
  274. // }
  275. // return results;
  276. //}
  277. #endregion
  278. }
  279. }