// // DotNetNuke - http://www.dotnetnuke.com // Copyright (c) 2002-2005 // by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca ) // // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated // documentation files (the "Software"), to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and // to permit persons to whom the Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all copies or substantial portions // of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. // using System; using System.Xml; using System.Xml.XPath; using System.Data; using System.Collections.Generic; using DotNetNuke; using DotNetNuke.Services.Search; using DotNetNuke.Common.Utilities; using System.Text; namespace DotNetNuke.Modules.Survey { public class SurveyController : Entities.Modules.ISearchable, Entities.Modules.IPortable, DotNetNuke.Entities.Modules.IUpgradeable { static public List GetSurveys(int ModuleId) { List SurveyInfolist = new List(); using (IDataReader dr = DataProvider.Instance().GetSurveys(ModuleId)) { while (dr.Read()) { SurveyInfo colSurveyInfo = new SurveyInfo(); colSurveyInfo.SurveyId = Convert.ToInt32(dr["SurveyId"]); colSurveyInfo.Question = Convert.ToString(dr["Question"]); colSurveyInfo.OptionType = Convert.ToString(dr["OptionType"]); colSurveyInfo.ViewOrder = Convert.ToInt32(ConvertNullInteger(dr["ViewOrder"])); colSurveyInfo.CreatedByUser = Convert.ToInt32(dr["CreatedByUser"]); colSurveyInfo.CreatedDate = Convert.ToDateTime(dr["CreatedDate"]); SurveyInfolist.Add(colSurveyInfo); } } return SurveyInfolist; } static public SurveyInfo GetSurvey(int SurveyID, int ModuleId) { SurveyInfo colSurveyInfo = new SurveyInfo(); using (IDataReader dr = DataProvider.Instance().GetSurvey(SurveyID, ModuleId)) { while (dr.Read()) { colSurveyInfo.SurveyId = Convert.ToInt32(dr["SurveyId"]); colSurveyInfo.ModuleId = Convert.ToInt32(dr["ModuleID"]); colSurveyInfo.Question = Convert.ToString(dr["Question"]); colSurveyInfo.OptionType = Convert.ToString(dr["OptionType"]); colSurveyInfo.ViewOrder = Convert.ToInt32(ConvertNullInteger(dr["ViewOrder"])); colSurveyInfo.Votes = Convert.ToInt32(ConvertNullInteger(dr["Votes"])); colSurveyInfo.CreatedByUser = Convert.ToInt32(dr["CreatedByUser"]); colSurveyInfo.CreatedDate = Convert.ToDateTime(dr["CreatedDate"]); } } return colSurveyInfo; } public static void DeleteSurvey(SurveyInfo objSurvey) { DataProvider.Instance().DeleteSurvey(objSurvey.SurveyId, objSurvey.ModuleId); } public static int AddSurvey(SurveyInfo objSurvey) { return ((int)(DataProvider.Instance().AddSurvey(objSurvey.ModuleId, objSurvey.Question, objSurvey.ViewOrder, objSurvey.OptionType, objSurvey.CreatedByUser))); } public static void UpdateSurvey(SurveyInfo objSurvey) { DataProvider.Instance().UpdateSurvey(objSurvey.SurveyId, objSurvey.Question, objSurvey.ViewOrder, objSurvey.OptionType, objSurvey.CreatedByUser, objSurvey.ModuleId); } #region Utility Functions public static int ConvertNullInteger(object Field) { if (Field == DBNull.Value) { return 0; } else { return Convert.ToInt32(Field); } } #endregion #region IUpgradable public string UpgradeModule(string Version) { return "Custom upgrade code goes here for Version: " + Version; } #endregion #region IPortable Members string DotNetNuke.Entities.Modules.IPortable.ExportModule(int ModuleID) { StringBuilder strXML = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.OmitXmlDeclaration = true; XmlWriter Writer = XmlWriter.Create(strXML, settings); // Outer Loop - To build the Surveys List colSurveys = GetSurveys(ModuleID); if ((colSurveys.Count > 0)) { Writer.WriteStartElement("surveys"); foreach (SurveyInfo colSurveyInfo in colSurveys) { Writer.WriteStartElement("survey"); Writer.WriteElementString("question", colSurveyInfo.Question); Writer.WriteElementString("vieworder", colSurveyInfo.ViewOrder.ToString()); Writer.WriteElementString("createdbyuser", colSurveyInfo.CreatedByUser.ToString()); Writer.WriteElementString("createddate", colSurveyInfo.CreatedDate.ToShortDateString()); Writer.WriteElementString("optiontype", colSurveyInfo.OptionType.ToString()); // Inner Loop - To build the Options for each Survey List colSurveyOptions = SurveyOptionController.GetSurveyOptions(colSurveyInfo.SurveyId); if ((colSurveyOptions.Count > 0)) { Writer.WriteStartElement("surveyoptions"); foreach (SurveyOptionInfo colSurveyOptionInfo in colSurveyOptions) { Writer.WriteStartElement("surveyoption"); Writer.WriteElementString("optionname", colSurveyOptionInfo.OptionName); Writer.WriteElementString("iscorrect", colSurveyOptionInfo.IsCorrect.ToString()); Writer.WriteElementString("vieworder", colSurveyOptionInfo.ViewOrder.ToString()); Writer.WriteEndElement(); } // Retrieve the next SurveyOption Writer.WriteEndElement(); } Writer.WriteEndElement(); } Writer.WriteEndElement(); Writer.Close(); } else { // There is nothing to export return String.Empty; } return strXML.ToString(); } void DotNetNuke.Entities.Modules.IPortable.ImportModule(int ModuleID, string Content, string Version, int UserID) { //Import the Surveys //Outer Loop - To insert the Surveys int intCurrentSurvey; XmlNode xmlSurveys = DotNetNuke.Common.Globals.GetContent(Content, "surveys"); foreach (XmlNode xmlSurvey in xmlSurveys) { SurveyInfo SurveyInfo = new SurveyInfo(); SurveyInfo.ModuleId = ModuleID; SurveyInfo.Question = xmlSurvey["question"].InnerText; SurveyInfo.ViewOrder = Convert.ToInt32(xmlSurvey["vieworder"].InnerText); SurveyInfo.CreatedByUser = Convert.ToInt32(xmlSurvey["createdbyuser"].InnerText); SurveyInfo.CreatedDate = Convert.ToDateTime(xmlSurvey["createddate"].InnerText); SurveyInfo.OptionType = xmlSurvey["optiontype"].InnerText; // Add the Survey to the database intCurrentSurvey = AddSurvey(SurveyInfo); //Inner Loop - To insert the Survey Options XmlNode xmlSurveyOptions = xmlSurvey.SelectSingleNode("surveyoptions"); foreach (XmlNode xmlSurveyOption in xmlSurveyOptions) { SurveyOptionInfo SurveyOptionInfo = new SurveyOptionInfo(); SurveyOptionInfo.SurveyId = intCurrentSurvey; SurveyOptionInfo.OptionName = xmlSurveyOption["optionname"].InnerText; SurveyOptionInfo.IsCorrect = Convert.ToBoolean(xmlSurveyOption["iscorrect"].InnerText); SurveyOptionInfo.ViewOrder = Convert.ToInt32(xmlSurveyOption["vieworder"].InnerText); // Add the Survey to the database SurveyOptionController.AddSurveyOption(SurveyOptionInfo); } } } #endregion #region ISearchable Members public SearchItemInfoCollection GetSearchItems(DotNetNuke.Entities.Modules.ModuleInfo ModInfo) { // Get the Surveys for this Module instance List colSurveys = GetSurveys(ModInfo.ModuleID); SearchItemInfoCollection SearchItemCollection = new SearchItemInfoCollection(); foreach (SurveyInfo SurveyInfo in colSurveys) { SearchItemInfo SearchItem; SearchItem = new SearchItemInfo (ModInfo.ModuleTitle + " - " + SurveyInfo.Question, SurveyInfo.Question, SurveyInfo.CreatedByUser, SurveyInfo.CreatedDate, ModInfo.ModuleID, Convert.ToString(SurveyInfo.SurveyId), SurveyInfo.Question); SearchItemCollection.Add(SearchItem); } return SearchItemCollection; } #endregion } }