通用調查系統開發

      調查系統是一個變化非常頻繁的系統,無論界面還是調查內容每個調查都大不相同,要想做出一個擴展性強、能應對所有調查系統非常不易,在做了許多調查的程序後,我設計了這個調查系統,

首先:爲了應對調查內容的變化,在數據庫設計的時候不爲調查的每一項設計字段,而是把整個調查頁面的html代碼保存在一個字段裏,

其次:爲了應對調查界面上的變化,美工把調查頁面設計好以後,我把放在web編輯器裏提交到數據庫裏,填寫調查的時候,用asp.net程序地址欄的suivryid參數從數據庫裏把相應的調查內容讀出來,賦給一個label標籤。每個調查用到的樣式表都不一樣,爲了能讓每個調查調用到自己的樣式,我寫了一個xml文檔 。象下面那樣:

<?xml version="1.0" encoding="gb2312"?>
<root>
<cssnode>
<css sureyid="105" sureycss="/css/main.css"/>
<css sureyid="106" sureycss="/css/spc.css"/>
<css sureyid="108" sureycss="/css/eworkshd.css"/>
<css sureyid="110" sureycss="/css/ge.css"/>
<css sureyid="112" sureycss="/css/sap.css"/>
<css sureyid="114" sureycss="/css/style_sap.css"/>
<css sureyid="115" sureycss="/css/scm.css"/>
<css sureyid="117" sureycss="/css/eworkssurvey.css"/>
<css sureyid="119" sureycss="/css/oracle.css"/>

</cssnode>
</root>

 

其中的sureyid表示調查id,sureycss表示這個調查用到樣式表。在調查頁面上放一個literal標籤,在頁面加載的時候,根據調查id,獲取xml文檔中的sureycss屬性值賦給literal, 如下所示:literal1.Text = "<link href=\"" + GetConfigStr(surveyid, "xml/surveycss.xml", "cssnode") + "\" rel=\"stylesheet\" type=\"text/css\">";

GetConfigStr方法:

  public string GetConfigStr(string type, string xmlFile, string rootnode)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(System.Web.HttpContext.Current.Server.MapPath(xmlFile));

            XmlNodeList topM = xmlDoc.DocumentElement.ChildNodes;
            foreach (XmlElement element in topM)
            {
                if (element.Name.ToLower() == rootnode)
                {
                    //得到該節點的子節點集
                    XmlNodeList nodelist = element.ChildNodes;

                    if (nodelist.Count > 0)
                    {
                        foreach (XmlElement el in nodelist)//讀元素值
                        {
                            if (el.Attributes["sureyid"].Value == type)
                            {
                                return el.Attributes["sureycss"].Value;
                            }

                        }

                    }
                }
            }
            return "";

        }

 每個調查提交的時候都需要用js做一些必填項、安全性驗證,而且每個調查的驗證都不一樣,爲了能應對這個問題,首先我把一通用的js提取出來放在一個js文件裏,

然後針對每個不同的調查分別寫驗證的js,然後象上面調用樣式表那樣用一個xml文檔記錄每個調查對應的js文件,如下所示:

 <?xml version="1.0" encoding="gb2312"?>
<root>
<cssnode>
<css sureyid="105" sureyjs="/js/sap2013.js"/>
<css sureyid="106" sureyjs="/js/surveysave.js"/>
</cssnode>
</root>

 在調查頁面上放一個literal標籤,在頁面加載的時候,根據調查id,獲取xml文檔中的sureyjs屬性值賦給literal, 如下所示: jssrc.Text = "<script src=\"" + im.GetConfigStr("105", "xml/jssrc.xml", "cssnode") + "\" type=\"text/javascript\"></script>";

有人會問:你把所有調查內容保存在一個字段裏做調查的時候是簡單輕鬆快捷了,但要得到調查結果就讓人頭疼了,爲了解決這個問題我做了一個通用的調查結果分析程序用來分析、提取客戶提交的html代碼的類,這個類代碼大致如下:


 

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Text;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Collections.Generic;
namespace Campaign
{
 /// <summary>
 /// SurveyAnalysis 的摘要說明。
    /// 調查結果統計分析類
    /// 編寫者陳文軍
    /// 編寫日期:20090727
    /// 20100604改成了泛型版本
 /// </summary>
 public class SurveyAnalysis
 {
  public SurveyAnalysis()
  {
   //
   // TODO: 在此處添加構造函數邏輯
   //
  }
  public static string FilterScript(string content)
  {
   if(content==null || content=="")
   {
    return content;
   }
   string regexstr=@"(?i)<script([^>])*>(\w|\W)*</script([^>])*>";//@"<script.*</script>";
   content=Regex.Replace(content,regexstr,string.Empty,RegexOptions.IgnoreCase);
   content=Regex.Replace(content,"<script([^>])*>",string.Empty,RegexOptions.IgnoreCase);
   return Regex.Replace(content,"</script>",string.Empty,RegexOptions.IgnoreCase);
  }
  public static string RemoveHtml(string content)
  {
   string newstr=FilterScript(content);
   string regexstr=@"<[^>]*>";
   return Regex.Replace(newstr,regexstr,string.Empty,RegexOptions.IgnoreCase);
  }
        public static string InfoFilter2(string info)//去掉html代碼
        {
            string otherInfo = "";
            info = info.Replace("name=", "<name=");
            otherInfo = System.Web.HttpContext.Current.Server.HtmlDecode(info).ToLower().Replace("checked", "> checked<").Replace("checked>", "> checked<");
            otherInfo = otherInfo.Replace("<input", "#<input");
            otherInfo = RemoveHtml(otherInfo).Replace("\r", "").Replace("\n", "").Replace("&nbsp;", "").Replace("type=checkbox", "").Replace("type=radio", "").Replace("*", "");
            return otherInfo;
        }
  public static string InfoFilter(string info)//去掉html代碼
  {
   string otherInfo="";
            info = info.ToLower().Replace("value=", "value=>").Replace("name=", "<name=").Replace("id=", "<id=").Replace("style=", "<style=").Replace("selected>", "selected<>").Replace("option", "option><");

            otherInfo = System.Web.HttpContext.Current.Server.HtmlDecode(info).Replace("checked", "> checked<").Replace("checked>", "> checked<");
            //otherInfo = info.Replace("CHECKED", "> CHECKED<").Replace("CHECKED>", "> CHECKED<");
            otherInfo = otherInfo.Replace("<input", " #<input");        
            otherInfo = RemoveHtml(otherInfo).Replace("\r", "").Replace("\n", "").Replace("&nbsp;", "").Replace("type=checkbox", "").Replace("type=radio", "").Replace("*", ""); 
   return otherInfo;
  }
        /// <summary>
        /// //得到選中的項,並合併成逗號分割的字符串
        /// </summary>
        /// <param name="info"></param>
        /// <param name="startItem"></param>
        /// <param name="endItem"></param>
        /// <returns></returns>
  public  static string GetItemInfo(string info,string startItem,string endItem)
  {
            //    string str4 = SurveyAnalysis.GetItemInfo(pageInfo, "是否有采購戴爾工作站", "7、");
            info = info.ToLower();
   int a=0;
   int b=0;
   string itemTitle = "";
   if(!startItem.Equals(""))
   {
    a = info.IndexOf(startItem);
   }
   if(!endItem.Equals(""))
   {
    b = info.IndexOf(endItem);
   }
   if(a!=-1&&b!=-1)
   {
    string titleInfo="";
                if(b>a)
    titleInfo = info.Substring(a,b-a);
    
    int count=Regex.Matches(titleInfo,"checked").Count;
   
    for( int n=0;n<count;n++)
    {
                    a = titleInfo.IndexOf("checked");
     b = titleInfo.IndexOf("#",a);   
     if(a!=-1&&b!=-1)
     {
        if(b>a)
      itemTitle+= titleInfo.Substring(a+7,b-(a+7))+",";
      

     }
     if(b!=-1)
      titleInfo=titleInfo.Substring(b);
    
    }
    if(itemTitle!=string.Empty)
     itemTitle=itemTitle.Trim(',');
   }
   return itemTitle;
  }

        public static string GetItemInfoByUps(string info, string startItem, string endItem)
        {
        
            info = info.ToLower();
            int a = 0;
            int b = 0;
            string itemTitle = "";
            if (!startItem.Equals(""))
            {
             
                a = info.IndexOf(startItem.ToLower());
            
            }
            if (!endItem.Equals(""))
            {
                b = info.IndexOf(endItem.ToLower());
            }
            if (a != -1 && b != -1)
            {
                string titleInfo = "";
                titleInfo = info.Substring(a, b - a);
                titleInfo = titleInfo.Substring(0, titleInfo.LastIndexOf("#"));
                int count = Regex.Matches(titleInfo, "checked").Count;

                for (int n = 0; n < count; n++)
                {
                    a = titleInfo.IndexOf("checked");
                    b = titleInfo.IndexOf("#");
                    if (a != -1 && b != -1)
                    {

                        try
                        {
                            int strs=titleInfo.Substring(a).IndexOf("#");
                           string strInfo= titleInfo.Substring(a, strs);
                         
                            itemTitle = Regex.Replace(strInfo, @"(<checked>\d)", "").Replace("checked","");
                            itemTitle= Regex.Replace(itemTitle, @"\d", "");  


                        }
                        catch (Exception ex)
                        {
                            itemTitle = "";
                            //HttpContext.Current.Response.Write(ex.Message);
                            Console.WriteLine(ex.Message);
                        }


                    }
                    if (b != -1)
                        titleInfo = titleInfo.Substring(b);

                }
                if (itemTitle != string.Empty)
                    itemTitle = itemTitle.Trim(',');
            }
            return itemTitle;
        }
        /// <summary>
        /// //得到選中的項,並合併成逗號分割的字符串
        /// </summary>
        /// <param name="info"></param>
        /// <param name="startItem"></param>
        /// <param name="endItem"></param>
        /// <returns></returns>
        public static string GetItemInfo(string info)
        {
            string itemTitle = "";
            int a = 0;
            int b = 0;
            string newInfo = info.ToUpper();
            int count = Regex.Matches(newInfo, "CHECKED").Count;
            for (int n = 0; n < count; n++)
            {
                a = newInfo.IndexOf("CHECKED");
                b = newInfo.IndexOf("#", a);
                if(a>0&&b>0)
                    itemTitle += newInfo.Substring(a + 7, b - (a + 7)) + ",";   
                if (b != -1)
                    newInfo = newInfo.Substring(b);

            }
            if (itemTitle != string.Empty)
                itemTitle = itemTitle.Trim(',');
            return itemTitle;
        }
        public static string[] GetQuestionTitle(DataTable dt)//得到調查所有的問題
        {
            int SuveryNum = dt.Rows.Count;
            string[] titlearry = null;
            int count = 0;
            string resultInfo = SurveyAnalysis.InfoFilter(dt.Rows[0]["resultinfo"].ToString());
            count = Regex.Matches(resultInfo, "?").Count;
            titlearry = new string[count];
            for (int n = 0; n < count; n++)
            {
                titlearry[n] = (n + 1).ToString() + "、" + SurveyAnalysis.GetItemTitle(resultInfo, (n + 1).ToString() + "、", "?");
            }
            return titlearry;
        }
        public static string GetTextBoxInfo(string info, string startItem, string endItem)//得到填寫的項
        {
            int a = 0;
            int b = 0;
            a = info.IndexOf(startItem);
            b = info.IndexOf(endItem);
            string itemTitle = "";
            if ((a != -1) && (b != -1)&&(b>a))
            {
                itemTitle = info.Substring(a + startItem.Length, b - (a + startItem.Length));             
            }
            return itemTitle;
        }
        /// <summary>
        /// 得到兩個標識之間的內容
        /// </summary>
        /// <param name="info"></param>
        /// <param name="startItem"></param>
        /// <param name="endItem"></param>
        /// <returns></returns>
        public static string GetItemTitle(string info, string startItem, string endItem)
  {        
                int a = 0;
                int b = 0;
                a = info.IndexOf(startItem);
                b = info.IndexOf(endItem, a + 1);
                string itemTitle = "";
                if ((a !=-1) && (b!=-1))
                {
                    itemTitle = info.Substring(a + startItem.Length, b - (a + startItem.Length));
                }
              
                return itemTitle;
          
  }
        public static Dictionary<string, int> GetTitle(string info, string startItem, string endItem)//得到所有的選項,存入Dictionary,並把得票數置爲0
  {
   int a=0;
   int b=0;
            Dictionary<string, int> ht = new Dictionary<string, int>();  
   int count=Regex.Matches(info,"#").Count;
            info = info.Replace("CHECKED", "").Replace(" ", "") + "#";
   string itemTitle = "";
   for( int n=0;n<count;n++)
   {
    a = info.IndexOf(startItem);
              
    b = info.IndexOf(endItem,a+1);
                if ((a != -1) && (b != -1))
                {
                    itemTitle = info.Substring(a + startItem.Length, b - (a + startItem.Length)).Replace(">", "").Trim().Replace("#", "");
                   
                    if (!itemTitle.Equals(""))
                    {
                        if(!ht.ContainsKey(itemTitle))
                        ht.Add(itemTitle, 0);
                       
                    }
                    info = info.Substring(b);
                }
               
   }
   return ht;
  }
        public static Dictionary<string, int> GetTitle( Dictionary<string, int> ht,string info, string startItem, string endItem)//得到所有的選項,存入Dictionary,並把得票數置爲0
        {
            int a = 0;
            int b = 0;
            int count = Regex.Matches(info, "#").Count;
            info = info.ToUpper().Replace("CHECKED", "").Replace(" ", "") + "#";
            string itemTitle = "";
            for (int n = 0; n < count; n++)
            {
                a = info.IndexOf(startItem);

                b = info.IndexOf(endItem, a + 1);
                if ((a != -1) && (b != -1))
                {
                    itemTitle = info.Substring(a + startItem.Length, b - (a + startItem.Length)).Replace(">", "").Trim().Replace("#", "").Replace("CISCO","").Replace("IBM","").Replace("Autodesk","");
                    if (!itemTitle.Equals(""))
                    {
                        ht.Add(itemTitle, 0);

                    }
                    info = info.Substring(b);
                }

            }
            return ht;
        }
        public static ArrayList GetArrTitle(string info, string startItem, string endItem)//得到所有的選項,存入ArrayList,並把得票數置爲0
        {
            int a = 0;
            int b = 0;
            ArrayList ht = new ArrayList();
            int count = Regex.Matches(info, "#").Count;
            info = info.Replace("CHECKED", "").Replace(" ", "") + "#";
            string itemTitle = "";
            for (int n = 0; n < count; n++)
            {
                a = info.IndexOf(startItem);

                b = info.IndexOf(endItem, a + 1);
                if ((a != -1) && (b != -1))
                {
                    itemTitle = info.Substring(a + startItem.Length, b - (a + startItem.Length)).Replace(">", "").Trim().Replace("#", "");
                    if (!itemTitle.Equals(""))
                    {
                        ht.Add(itemTitle);

                    }
                    info = info.Substring(b);
                }

            }
            return ht;
        }
        public static ArrayList GetArrTitle(ArrayList arr,string info, string startItem, string endItem)//得到所有的選項,存入Dictionary,並把得票數置爲0
        {
            int a = 0;
            int b = 0;
            //ArrayList ht = new ArrayList();
            int count = Regex.Matches(info, "#").Count;
            info = info.ToUpper().Replace("CHECKED", "").Replace(" ", "") + "#";
            string itemTitle = "";
            for (int n = 0; n < count; n++)
            {
                a = info.IndexOf(startItem);

                b = info.IndexOf(endItem, a + 1);
                if ((a != -1) && (b != -1))
                {
                    itemTitle = info.Substring(a + startItem.Length, b - (a + startItem.Length)).Replace(">", "").Trim().Replace("#", "");
                    if (!itemTitle.Equals(""))
                    {
                        arr.Add(itemTitle);

                    }
                    info = info.Substring(b);
                }

            }
            return arr;
        }
        public static ArrayList Drawoff(string info, string startItem, string endItem)//得到所有的選項,存入Dictionary,並把得票數置爲0
        {
            int a = 0;
            int b = 0;
            ArrayList ht = new ArrayList();
            int count = Regex.Matches(info, "#").Count;
            info = info.ToUpper().Replace("CHECKED", "").Replace(" ", "") + "#";
            string itemTitle = "";
            for (int n = 0; n < count; n++)
            {
                a = info.IndexOf(startItem);

                b = info.IndexOf(endItem, a + 1);
                if ((a != -1) && (b != -1))
                {
                    itemTitle = info.Substring(a + startItem.Length, b - (a + startItem.Length)).Replace(">", "").Trim();
                    if (!itemTitle.Equals(""))
                    {
                        ht.Add(itemTitle);

                    }
                    info = info.Substring(b);
                }

            }
            return ht;
        }
        public static ArrayList GetInfoHaveChecked (string info, string startItem, string endItem)//得到所有的選項,存入Dictionary,並把得票數置爲0
        {
            int a = 0;
            int b = 0;
            ArrayList ht = new ArrayList();
            int count = Regex.Matches(info, "#").Count;
            info = info.Replace(" ", "").ToLower().Replace("有#checked無#", "有#無#") + "#";
            string itemTitle = "";
            for (int n = 0; n < count; n++)
            {
                a = info.IndexOf(startItem);

                b = info.IndexOf(endItem, a + 1);
                if ((a != -1) && (b != -1))
                {
                    itemTitle = info.Substring(a + startItem.Length, b - (a + startItem.Length)).Replace(">", "").Trim();
                    if (!itemTitle.Equals(""))
                    {
                        ht.Add(itemTitle);

                    }
                    info = info.Substring(b);
                }

            }
            return ht;
        }
        public static Dictionary<string, int> GetResultInfo(DataTable dt, string startStr, string endStr)
  {
            //Hashtable ht = new Hashtable();
            Dictionary<string, int> ht = new Dictionary<string, int>();
            if (dt.Rows.Count > 0)
            {
                string resultInfo = "";
                string title = "";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    resultInfo = dt.Rows[i]["resultinfo"].ToString();
                    resultInfo = InfoFilter(resultInfo) + "<!--surveyend-->";//給調查加一個結尾標記
                    if (!resultInfo.Equals(""))
                    {
                        resultInfo = GetItemTitle(resultInfo, startStr, endStr).Replace("\"\"", "");
                        if (i < 5)
                        {
                           resultInfo = resultInfo.Replace("產品部適合我的業務", "產品不適合我的業務");
                        }
                        if (i == 0)
                        {
                            //if (resultInfo.IndexOf("所處行業的價值鏈發生了變化") != -1)
                            //{
                            ht = GetTitle(resultInfo, "#", "#");//把一個問題下的所有選項存入hashtable,並把得票數置爲0
                            //}
                        }
                        resultInfo = startStr + resultInfo + "#" + endStr;
                        title = GetItemInfo(resultInfo, startStr, endStr);//得到選中的選項字符串
                        if (title != "")
                        {
                            string[] s = title.Split(',');
                            if (s.Length > 0)
                            {
                                for (int n = 0; n < s.Length; n++)
                                {
                                    //if(ht[s[n]]!=null)
                                    string key = s[n].ToString().Trim();
                                    if (ht.ContainsKey(key))                                      
                                        ht["" + key + ""] = ht["" + key + ""] + 1;//如果被選中則得票數加1
                                }
                            }

                        }
                    }

                }
            }
   return ht;  
  }
   
 }
}

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章