private string RunFSharpScript(HttpContext context)
        {
            SurveyTemplateAdmin st = GetCurrentTemplate(context);


            return(CompileExecutable(st, new SurveyResponse()));
        }
        public static SurveyTemplateAdmin GetAdminTemplate(string pstrList, Guid pguidTemplate)
        {
            SurveyTemplateAdmin stThis = null;

            SPWeb web = SPContext.Current.Web;

            {
                try
                {
                    SPList lst = web.Lists.TryGetList(pstrList);

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

                        return(new SurveyTemplateAdmin(lsi));
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            return(stThis);
        }
        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 SurveyTemplateAdmin GetCurrentTemplate(HttpContext context)
        {
            SurveyTemplateAdmin stThis = null;
            string strList             = context.Request["List"] == null ? mcstrTemplateList : context.Request["List"];



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

                    if (lst != null)
                    {
                        SPListItem lsi = null;

                        if (context.Request["TemplateID"] == null)
                        {
                            SPQuery qry = new SPQuery();
                            qry.RowLimit = 1;
                            qry.Query    = @"<OrderBy><FieldRef Name='Created' Ascending='False' /></OrderBy>";
                            SPListItemCollection lic = lst.GetItems(qry);
                            if (lic.Count == 1)
                            {
                                lsi = lic[0];
                            }
                        }
                        else
                        {
                            lsi = lst.GetItemByUniqueId(new Guid(context.Request["TemplateID"]));
                        }

                        if (lsi != null)
                        {
                            stThis = new SurveyTemplateAdmin(lsi);
                        }
                    }
                    else
                    {
                        context.Response.StatusCode        = 500;
                        context.Response.StatusDescription = "DWF Survey: Specified List Not Found";
                    }
                }
                catch (Exception ex)
                {
                    context.Response.StatusCode        = 500;
                    context.Response.StatusDescription = "DWF Survey: " + ex.Message;
                }
            }

            return(stThis);
        }
        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");
        }
        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 SurveyTemplateAdmin GetCurrentTemplate(HttpContext context)
        {
            SurveyTemplateAdmin stThis = null;
            string strList = context.Request["List"] == null ? mcstrTemplateList : context.Request["List"];



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

                    if (lst != null)
                    {

                        SPListItem lsi = null;

                        if (context.Request["TemplateID"] == null)
                        {
                            SPQuery qry = new SPQuery();
                            qry.RowLimit = 1;
                            qry.Query = @"<OrderBy><FieldRef Name='Created' Ascending='False' /></OrderBy>";
                            SPListItemCollection lic = lst.GetItems(qry);
                            if (lic.Count == 1)
                            {
                                lsi = lic[0];
                            }
                        }
                        else
                        {
                            lsi = lst.GetItemByUniqueId(new Guid(context.Request["TemplateID"]));
                        }

                        if (lsi != null)
                        {

                            
                            stThis = new SurveyTemplateAdmin(lsi);
                        }


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


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


            }

            return stThis;

        }