private SurveyResponse GetResponse(HttpContext context)
        {
            SurveyResponse rspReturn = new SurveyResponse();

            if (context.Request["List"] != null && context.Request["ResponseID"] != null)
            {
                using (SPWeb web = SPContext.Current.Web)
                {
                    try
                    {
                        SPList lst          = web.Lists.TryGetList(context.Request["List"]);
                        Guid   guidResponse = new Guid(context.Request["ResponseID"]);

                        if (lst != null)
                        {
                            SPListItem lsi = lst.GetItemByUniqueId(guidResponse);

                            rspReturn = new SurveyResponse(lsi);
                        }
                    }
                    catch (Exception ex)
                    {
                        context.Response.StatusCode        = 500;
                        context.Response.StatusDescription = "DWF Survey: " + ex.Message;
                    }
                }
            }


            return(rspReturn);
        }
        public ResultSet Main(SurveyTemplate st, SurveyResponse rs)
        {
            ResultSet rsReturn = new ResultSet(0, "");

            StringBuilder sbMessage = new StringBuilder();
            int           intScore  = 0;

            double dblDamage;

            if (Double.TryParse(rs.GetAnswerText(st.GetQuestionFromNumber("Q1").ID), out dblDamage))
            {
                if (dblDamage <= 2000)
                {
                    sbMessage.AppendLine("Small Claims Track");
                }
            }

            double dblScale;



            if (Double.TryParse(rs.GetAnswerText(st.GetQuestionFromNumber("Q2").ID), out dblScale))
            {
                if (dblScale < 100)
                {
                    sbMessage.AppendLine("Outside the portal");
                }
            }

            rsReturn.Message = sbMessage.ToString();
            rsReturn.Score   = intScore;
            return(rsReturn);
        }
        private string GetScore(HttpContext context)
        {
            SurveyScore scoReturn = new SurveyScore();

            try
            {
                if (context.Request["TemplateList"] != null &&
                    context.Request["ResponseData"] != null &&
                    context.Request["ScoreCardList"] != null &&
                    context.Request["TemplateID"] != null &&
                    context.Request["ScoreCardID"] != null)
                {
                    JavaScriptSerializer ser = new JavaScriptSerializer(new SimpleTypeResolver());

                    SurveyResponse rspThis = ser.Deserialize <SurveyResponse>(context.Request["ResponseData"]);

                    //if (context.Request["Suspicion"] == null)
                    //{
                    //    srThis.AlternateScore = false;
                    //}
                    //else
                    //{
                    //    srThis.AlternateScore = (context.Request["Suspicion"] == "Yes");
                    //}

                    //srThis.ResponseData = ser.Deserialize<List<SurveyAnswer>>(context.Request["ResponseData"]);

                    SurveyTemplateAdmin tmpThis = GetAdminTemplate(context.Request["TemplateList"], new Guid(context.Request["TemplateID"]));
                    //SurveyScoreCard sscThis = GetSurveyScoreCard(context.Request["ScoreCardList"], new Guid(context.Request["ScoreCardID"]));
                    //SurveyScoreCard sscThis = GetScoreCard(context);

                    //srThis.Score(tmpThis, sscThis);
                    //scoReturn.Message = srThis.ScoreDescription;
                    //scoReturn.Color = srThis.ScoreColor;
                    //scoReturn.Rating = srThis.RAG;

                    return(CompileExecutable(tmpThis, rspThis));
                }

                else
                {
                    context.Response.StatusCode        = 500;
                    context.Response.StatusDescription = "DWF Survey: Incorrect score request";
                }
            }
            catch (ArgumentException argEx)
            {
                context.Response.StatusCode = 500;

                context.Response.StatusDescription = "DWF Survey: Guid does not match with a list item in the list. " + argEx.Message + ". Data:" + argEx.Data.ToString();
            }
            catch (Exception ex)
            {
                context.Response.StatusCode        = 500;
                context.Response.StatusDescription = "DWF Survey: " + ex.Message;
            }

            return(String.Empty);
        }
        private List <SurveyResponse> GetPreviousResponses(HttpContext context)
        {
            List <SurveyResponse> lstReturn = new List <SurveyResponse>();

            if (context.Request["List"] != null)
            {
                using (SPWeb web = SPContext.Current.Web)
                {
                    try
                    {
                        SPList lst = web.Lists.TryGetList(context.Request["List"]);

                        if (lst != null)
                        {
                            SPView vwSpecified;
                            SPListItemCollection items;

                            if (context.Request["ListView"] != null)
                            {
                                vwSpecified = lst.Views[context.Request["ListView"]];
                            }
                            else
                            {
                                vwSpecified = lst.DefaultView;
                            }

                            items = lst.GetItems(vwSpecified);

                            foreach (SPListItem item in items)
                            {
                                SurveyResponse rspThis = new SurveyResponse(item);
                                lstReturn.Add(rspThis);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        context.Response.StatusCode        = 500;
                        context.Response.StatusDescription = "DWF Survey: " + ex.Message;
                    }
                }
            }


            return(lstReturn);
        }
        public ResultSet Main(SurveyTemplate st, SurveyResponse rs)
        {
            ResultSet rsReturn = new ResultSet(0, "");

            StringBuilder sbMessage = new StringBuilder();
            int intScore = 0;

            double dblDamage;            
            
            if (Double.TryParse(rs.GetAnswerText(st.GetQuestionFromNumber("Q1").ID), out dblDamage))
            {

                if (dblDamage <= 2000)
                {
                    sbMessage.AppendLine("Small Claims Track");  
                }
                
                             
            }

            double dblScale;

            

            if (Double.TryParse(rs.GetAnswerText(st.GetQuestionFromNumber("Q2").ID), out dblScale))
            {

                if (dblScale < 100)
                {
                    sbMessage.AppendLine("Outside the portal");    
                }
                
                
            }

            rsReturn.Message = sbMessage.ToString();
            rsReturn.Score = intScore;
            return rsReturn;

        }
        public string CompileExecutable(SurveyTemplateAdmin st, SurveyResponse rs)
        {
            CodeDomProvider provider = null;

            provider = CodeDomProvider.CreateProvider("CSharp");

            if (provider != null)
            {
                // Format the executable file name.
                // Build the output assembly path using the current directory
                // and <source>_cs.exe or <source>_vb.exe.


                CompilerParameters cp = new CompilerParameters();

                //string strReference = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), references[i]);

                cp.ReferencedAssemblies.Add(System.Reflection.Assembly.GetExecutingAssembly().Location);

                // Generate an executable instead of
                // a class library.
                cp.GenerateExecutable = false;

                // Specify the assembly file name to generate.
                //cp.OutputAssembly = exeName;

                // Save the assembly as a physical file.
                cp.GenerateInMemory = true;

                // Set whether to treat all warnings as errors.
                cp.TreatWarningsAsErrors = false;

                // Invoke compilation of the source file.
                CompilerResults cr = provider.CompileAssemblyFromSource(cp,
                                                                        st.ScoreFunction);

                if (cr.Errors.Count > 0)
                {
                    StringBuilder sb = new StringBuilder();

                    // Display compilation errors.
                    foreach (CompilerError ce in cr.Errors)
                    {
                        sb.AppendLine(ce.ErrorText);
                    }

                    return(sb.ToString());
                }
                else
                {
                    Assembly   a  = cr.CompiledAssembly;
                    var        o  = a.CreateInstance("SurveyFunction");
                    Type       t  = o.GetType();
                    MethodInfo mi = t.GetMethod("Main");


                    object[] oParams = new object[2];
                    oParams[0] = st as SurveyTemplate;
                    oParams[1] = rs;
                    object s = mi.Invoke(o, oParams);
                    return(s.ToString());
                }
            }

            return("Code has not run");
        }
        private DWFResponse SaveResponse(HttpContext context)
        {
            DWFResponse rspReturn = new DWFResponse();
            string      strList   = context.Request["List"] == null ? mcstrResponseList : context.Request["List"];

            rspReturn.IsError = true;

            if (context.Request["ResponseData"] != null)
            {
                JavaScriptSerializer ser = new JavaScriptSerializer(new SimpleTypeResolver());

                SurveyResponse srThis = new SurveyResponse();

                srThis.ResponseData = ser.Deserialize <List <SurveyAnswer> >(context.Request["ResponseData"]);
                srThis.ResponseName = context.Request["ResponseName"];
                srThis.TemplateID   = new Guid(context.Request["TemplateID"]);


                using (SPWeb web = SPContext.Current.Web)
                {
                    try
                    {
                        SPList lst = web.Lists.TryGetList(strList);

                        if (lst != null)
                        {
                            SPListItem lsi;

                            if (context.Request["ID"] == "-1")
                            {
                                lsi = lst.Items.Add();
                            }
                            else
                            {
                                lsi = lst.GetItemByUniqueId(new Guid(context.Request["ID"]));
                            }
                            lsi["Title"]        = srThis.ResponseName;
                            lsi["ResponseName"] = srThis.ResponseName;
                            int intScore = srThis.Score(SurveyHandler.GetTemplate(context.Request["TemplateList"], srThis.TemplateID), SurveyHandler.GetScoreCard());
                            lsi["Score"]           = intScore;
                            lsi["TemplateID"]      = srThis.TemplateID;
                            lsi["ResponseData"]    = context.Request["ResponseData"];
                            web.AllowUnsafeUpdates = true;
                            SPSecurity.RunWithElevatedPrivileges(delegate()
                            {
                                lsi.Update();
                            });
                            web.AllowUnsafeUpdates = false;

                            rspReturn.IsError  = false;
                            rspReturn.Message  = "<h2>" + intScore + " out of 10</h2>";
                            rspReturn.Message += "<p>" + srThis.ScoreDescription + "</p>";
                        }
                    }
                    catch (Exception ex)
                    {
                        context.Response.StatusCode        = 500;
                        context.Response.StatusDescription = "DWF Survey: " + ex.Message;
                    }
                }
            }
            else
            {
                context.Response.StatusCode        = 500;
                context.Response.StatusDescription = "DWF Survey: Specified Responses List Not Found";
            }


            return(rspReturn);
        }
        public string CompileExecutable(SurveyTemplateAdmin st, SurveyResponse rs)
        {
            CodeDomProvider provider = null;            
            
            provider = CodeDomProvider.CreateProvider("CSharp");
           
            if (provider != null)
            {

                // Format the executable file name. 
                // Build the output assembly path using the current directory 
                // and <source>_cs.exe or <source>_vb.exe.               
                
               
                CompilerParameters cp = new CompilerParameters();

                //string strReference = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), references[i]);

                cp.ReferencedAssemblies.Add(System.Reflection.Assembly.GetExecutingAssembly().Location);

                // Generate an executable instead of  
                // a class library.
                cp.GenerateExecutable = false;

                // Specify the assembly file name to generate.
                //cp.OutputAssembly = exeName;

                // Save the assembly as a physical file.
                cp.GenerateInMemory = true;

                // Set whether to treat all warnings as errors.
                cp.TreatWarningsAsErrors = false;

                // Invoke compilation of the source file.
                CompilerResults cr = provider.CompileAssemblyFromSource(cp, 
                    st.ScoreFunction);

                if(cr.Errors.Count > 0)
                {

                    StringBuilder sb = new StringBuilder();
                    
                    // Display compilation errors.
                    foreach(CompilerError ce in cr.Errors)
                    {
                        sb.AppendLine(ce.ErrorText);
                    }

                    return sb.ToString();
                }
                else
                {
                    Assembly a = cr.CompiledAssembly;
                    var o = a.CreateInstance("SurveyFunction");
                    Type t = o.GetType();
                    MethodInfo mi = t.GetMethod("Main");


                    object[] oParams = new object[2];
                    oParams[0] = st as SurveyTemplate;
                    oParams[1] = rs;
                    object s = mi.Invoke(o, oParams);
                    return s.ToString();



                }
                
            }

            return "Code has not run";

        }
        private SurveyResponse GetResponse(HttpContext context)
        {
            SurveyResponse rspReturn = new SurveyResponse();

            if (context.Request["List"] != null && context.Request["ResponseID"] != null)
            {

                using (SPWeb web = SPContext.Current.Web)
                {
                    try
                    {
                        SPList lst = web.Lists.TryGetList(context.Request["List"]);
                        Guid guidResponse = new Guid(context.Request["ResponseID"]);

                        if (lst != null)
                        {

                            SPListItem lsi = lst.GetItemByUniqueId(guidResponse);

                            rspReturn = new SurveyResponse(lsi);

                        }

                    }
                    catch (Exception ex)
                    {

                        context.Response.StatusCode = 500;
                        context.Response.StatusDescription = "DWF Survey: " + ex.Message;

                    }

                }

            }


            return rspReturn;

        }
        private List<SurveyResponse> GetPreviousResponses(HttpContext context)
        {

            List<SurveyResponse> lstReturn = new List<SurveyResponse>();

            if (context.Request["List"] != null)
            {

                using (SPWeb web = SPContext.Current.Web)
                {
                    try
                    {
                        SPList lst = web.Lists.TryGetList(context.Request["List"]);

                        if (lst != null)
                        {

                            SPView vwSpecified;
                            SPListItemCollection items;

                            if (context.Request["ListView"] != null)
                            {
                                vwSpecified = lst.Views[context.Request["ListView"]];

                            }
                            else
                            {
                                vwSpecified = lst.DefaultView;

                            }

                            items = lst.GetItems(vwSpecified);

                            foreach (SPListItem item in items)
                            {
                                SurveyResponse rspThis = new SurveyResponse(item);
                                lstReturn.Add(rspThis);

                            }
                        }

                    }
                    catch (Exception ex)
                    {

                        context.Response.StatusCode = 500;
                        context.Response.StatusDescription = "DWF Survey: " + ex.Message;

                    }

                }

            }


            return lstReturn;

        }
        private DWFResponse SaveResponse(HttpContext context)
        {
            DWFResponse rspReturn = new DWFResponse();
            string strList = context.Request["List"] == null ? mcstrResponseList : context.Request["List"];

            rspReturn.IsError = true;

            if (context.Request["ResponseData"] != null)
            {

                JavaScriptSerializer ser = new JavaScriptSerializer(new SimpleTypeResolver());

                SurveyResponse srThis = new SurveyResponse();

                srThis.ResponseData = ser.Deserialize<List<SurveyAnswer>>(context.Request["ResponseData"]);
                srThis.ResponseName = context.Request["ResponseName"];
                srThis.TemplateID = new Guid(context.Request["TemplateID"]);


                using (SPWeb web = SPContext.Current.Web)
                {
                    try
                    {
                        SPList lst = web.Lists.TryGetList(strList);

                        if (lst != null)
                        {

                            SPListItem lsi;

                            if (context.Request["ID"] == "-1")
                            {
                                lsi = lst.Items.Add();

                            }
                            else
                            {
                                lsi = lst.GetItemByUniqueId(new Guid(context.Request["ID"]));
                            }
                            lsi["Title"] = srThis.ResponseName;
                            lsi["ResponseName"] = srThis.ResponseName;
                            int intScore = srThis.Score(SurveyHandler.GetTemplate(context.Request["TemplateList"], srThis.TemplateID), SurveyHandler.GetScoreCard());
                            lsi["Score"] = intScore;
                            lsi["TemplateID"] = srThis.TemplateID;
                            lsi["ResponseData"] = context.Request["ResponseData"];
                            web.AllowUnsafeUpdates = true;
                            SPSecurity.RunWithElevatedPrivileges(delegate()
                            {
                                lsi.Update();
                            });
                            web.AllowUnsafeUpdates = false;

                            rspReturn.IsError = false;
                            rspReturn.Message = "<h2>" + intScore + " out of 10</h2>";
                            rspReturn.Message += "<p>" + srThis.ScoreDescription + "</p>";
                        }
                    }
                    catch (Exception ex)
                    {

                        context.Response.StatusCode = 500;
                        context.Response.StatusDescription = "DWF Survey: " + ex.Message;
                    }

                }
            }
            else
            {
                context.Response.StatusCode = 500;
                context.Response.StatusDescription = "DWF Survey: Specified Responses List Not Found";
            }


            return rspReturn;
        }