通用调查系统开发

      调查系统是一个变化非常频繁的系统,无论界面还是调查内容每个调查都大不相同,要想做出一个扩展性强、能应对所有调查系统非常不易,在做了许多调查的程序后,我设计了这个调查系统,

首先:为了应对调查内容的变化,在数据库设计的时候不为调查的每一项设计字段,而是把整个调查页面的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;  
  }
   
 }
}

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值