Ejemplo n.º 1
0
        private async Task BuildCharts()
        {
            ClearCharts();
            int updatePanelCounter    = 0;
            int catUpdatePanelCounter = 0;

            if (ddlFilterStudent.Text == "All Students")
            {
                // ALL STUDENTS/SUBJECTS
                if (ddlFilterSubject.Text == "All Subjects")
                {
                    int totalResponses               = 0;
                    int correctResponses             = 0;
                    IList <ParseObject> subjectStats = await ParseCloud.CallFunctionAsync <IList <ParseObject> >("getAllSubStats", null);

                    foreach (ParseObject stat in subjectStats)
                    {
                        totalResponses   += stat.Get <int>("totalResponses");
                        correctResponses += stat.Get <int>("correctResponses");
                    }
                    DoughnutChart subjectChart = (DoughnutChart)LoadControl("~/UserControls/DoughnutChart.ascx");
                    subjectChart.NumCorrect   = correctResponses;
                    subjectChart.NumIncorrect = totalResponses - correctResponses;
                    subjectChart.Title        = ddlFilterSubject.Text;
                    subjectChart.Chart.DataBind();
                    updatePanels[updatePanelCounter++].ContentTemplateContainer.Controls.Add(subjectChart);
                }
                // ALL STUDENTS, one subject
                else
                {
                    DoughnutChart subjectChart = (DoughnutChart)LoadControl("~/UserControls/DoughnutChart.ascx");
                    IDictionary <string, object> parameters = new Dictionary <string, object>
                    {
                        { "subjectName", ddlFilterSubject.Text }
                    };
                    ParseObject subjectStats = await ParseCloud.CallFunctionAsync <ParseObject>("getSubStats", parameters);

                    subjectChart.NumCorrect   = subjectStats.Get <int>("correctResponses");
                    subjectChart.NumIncorrect = subjectStats.Get <int>("totalResponses") - subjectStats.Get <int>("correctResponses");
                    subjectChart.Title        = subjectStats.Get <string>("subject");
                    subjectChart.Chart.DataBind();
                    updatePanels[updatePanelCounter++].ContentTemplateContainer.Controls.Add(subjectChart);

                    string[] categories = Constants.SubjectToCategory[ddlFilterSubject.Text];
                    IDictionary <string, object> catParams = new Dictionary <string, object>
                    {
                        { "catNames", categories }
                    };
                    IList <ParseObject> categoryStats = await ParseCloud.CallFunctionAsync <IList <ParseObject> >("getSomeCatStats", catParams);

                    foreach (ParseObject category in categoryStats)
                    {
                        SingleBarChart catChart = (SingleBarChart)LoadControl("~/UserControls/SingleBarChart.ascx");
                        catChart.SetCorrectIncorrect(category.Get <int>("correctResponses"), category.Get <int>("totalResponses") - category.Get <int>("correctResponses"));
                        catChart.Title = category.Get <string>("category");
                        catChart.Chart.DataBind();
                        catUpdatePanels[catUpdatePanelCounter++].ContentTemplateContainer.Controls.Add(catChart);
                    }
                }
            }
            // one student
            else
            {
                PublicUserData publicUserData = await PublicUserData.GetById(ddlFilterStudent.SelectedValue);

                Student student = await publicUserData.Student.FetchIfNeededAsync();

                //PrivateStudentData privateStudentData = await student.PrivateStudentData.FetchIfNeededAsync();

                // one student, ALL SUBJECTS
                if (ddlFilterSubject.Text == "All Subjects")
                {
                    StudentTotalRollingStats studentTotalRollingStats = await student.StudentTotalRollingStats.FetchIfNeededAsync();

                    int totalResponses   = 0;
                    int correctResponses = 0;
                    if (ddlFilterTime.SelectedValue == "PastWeek")
                    {
                        totalResponses   = studentTotalRollingStats.TotalPastWeek;
                        correctResponses = studentTotalRollingStats.CorrectPastWeek;
                    }
                    else if (ddlFilterTime.SelectedValue == "PastMonth")
                    {
                        totalResponses   = studentTotalRollingStats.TotalPastMonth;
                        correctResponses = studentTotalRollingStats.CorrectPastMonth;
                    }
                    else
                    {
                        totalResponses   = studentTotalRollingStats.TotalAllTime;
                        correctResponses = studentTotalRollingStats.CorrectAllTime;
                    }
                    Debug.WriteLine(totalResponses + " " + correctResponses);
                    DoughnutChart subjectChart = (DoughnutChart)LoadControl("~/UserControls/DoughnutChart.ascx");
                    subjectChart.NumCorrect   = correctResponses;
                    subjectChart.NumIncorrect = totalResponses - correctResponses;
                    subjectChart.Title        = ddlFilterSubject.Text;
                    subjectChart.Chart.DataBind();
                    updatePanels[updatePanelCounter++].ContentTemplateContainer.Controls.Add(subjectChart);
                }
                // one student, one subject
                else
                {
                    // ALL CATEGORIES
                    if (ddlFilterCategory.Text == "All Categories")
                    {
                        DoughnutChart subjectChart = (DoughnutChart)LoadControl("~/UserControls/DoughnutChart.ascx");
                        string        subjectName  = ddlFilterSubject.Text;
                        IEnumerable <StudentSubjectRollingStats> studentAllSubjectRollingStats = await student.StudentSubjectRollingStats.FetchAllIfNeededAsync();

                        StudentSubjectRollingStats studentSubjectRollingStats = studentAllSubjectRollingStats.Single(x => x.Subject == subjectName);

                        int totalResponses   = 0;
                        int correctResponses = 0;
                        if (ddlFilterTime.SelectedValue == "PastWeek")
                        {
                            totalResponses   = studentSubjectRollingStats.TotalPastWeek;
                            correctResponses = studentSubjectRollingStats.CorrectPastWeek;
                        }
                        else if (ddlFilterTime.SelectedValue == "PastMonth")
                        {
                            totalResponses   = studentSubjectRollingStats.TotalPastMonth;
                            correctResponses = studentSubjectRollingStats.CorrectPastMonth;
                        }
                        else
                        {
                            totalResponses   = studentSubjectRollingStats.TotalAllTime;
                            correctResponses = studentSubjectRollingStats.CorrectAllTime;
                        }
                        subjectChart.NumCorrect   = correctResponses;
                        subjectChart.NumIncorrect = totalResponses - correctResponses;
                        subjectChart.Title        = subjectName;
                        subjectChart.Chart.DataBind();
                        updatePanels[updatePanelCounter++].ContentTemplateContainer.Controls.Add(subjectChart);

                        string[] categories = Constants.SubjectToCategory[ddlFilterSubject.Text];
                        IEnumerable <StudentCategoryRollingStats> studentAllCategoryRollingStats = await student.StudentCategoryRollingStats.FetchAllIfNeededAsync();

                        //int i = 1;
                        foreach (string catName in categories)
                        {
                            SingleBarChart catChart = (SingleBarChart)LoadControl("~/UserControls/SingleBarChart.ascx");
                            StudentCategoryRollingStats categoryStats = studentAllCategoryRollingStats.First(x => x.Category == catName);

                            if (ddlFilterTime.SelectedValue == "PastWeek")
                            {
                                totalResponses   = categoryStats.TotalPastWeek;
                                correctResponses = categoryStats.CorrectPastWeek;
                            }
                            else if (ddlFilterTime.SelectedValue == "PastMonth")
                            {
                                totalResponses   = categoryStats.TotalPastMonth;
                                correctResponses = categoryStats.CorrectPastMonth;
                            }
                            else
                            {
                                totalResponses   = categoryStats.TotalAllTime;
                                correctResponses = categoryStats.CorrectAllTime;
                            }
                            catChart.SetCorrectIncorrect(correctResponses, totalResponses - correctResponses);
                            catChart.Title = catName;
                            catChart.Chart.DataBind();
                            catUpdatePanels[catUpdatePanelCounter++].ContentTemplateContainer.Controls.Add(catChart);
                        }
                    }
                    // one category
                    else
                    {
                        DoughnutChart categoryChart = (DoughnutChart)LoadControl("~/UserControls/DoughnutChart.ascx");
                        string        categoryName  = ddlFilterCategory.Text;
                        IEnumerable <StudentCategoryRollingStats> studentAllCategoryRollingStats = await student.StudentCategoryRollingStats.FetchAllIfNeededAsync();

                        StudentCategoryRollingStats studentCategoryRollingStats = studentAllCategoryRollingStats.Single(x => x.Category == categoryName);

                        int totalResponses   = 0;
                        int correctResponses = 0;
                        if (ddlFilterTime.SelectedValue == "PastWeek")
                        {
                            totalResponses   = studentCategoryRollingStats.TotalPastWeek;
                            correctResponses = studentCategoryRollingStats.CorrectPastWeek;
                        }
                        else if (ddlFilterTime.SelectedValue == "PastMonth")
                        {
                            totalResponses   = studentCategoryRollingStats.TotalPastMonth;
                            correctResponses = studentCategoryRollingStats.CorrectPastMonth;
                        }
                        else
                        {
                            totalResponses   = studentCategoryRollingStats.TotalAllTime;
                            correctResponses = studentCategoryRollingStats.CorrectAllTime;
                        }
                        categoryChart.NumCorrect   = correctResponses;
                        categoryChart.NumIncorrect = totalResponses - correctResponses;
                        categoryChart.Title        = categoryName;
                        categoryChart.Chart.DataBind();
                        updatePanels[updatePanelCounter++].ContentTemplateContainer.Controls.Add(categoryChart);
                    }
                }

                ProgressBarChart progressChart = (ProgressBarChart)LoadControl("~/UserControls/ProgressBarChart.ascx");
                DateTime         curDate;
                int startBlockNum;
                int endBlockNum;
                ParseRelation <ParseObject> rel;
                List <DataPoint>            correctDataPoints   = new List <DataPoint>();
                List <DataPoint>            incorrectDataPoints = new List <DataPoint>();
                string blockStatsType      = FigureBlockStatsType();
                string blockStatsSelection = FigureBlockStatsSelection();
                if (ddlFilterTime.SelectedValue == "PastWeek")
                {
                    rel           = student.GetRelation <ParseObject>("student" + blockStatsType + "DayStats");
                    endBlockNum   = DateUtils.getCurrentDayBlockNum();
                    startBlockNum = endBlockNum - 6;
                    curDate       = DateTime.Today.AddDays(-6);

                    for (int i = startBlockNum; i <= endBlockNum; i++, curDate = curDate.AddDays(1))
                    {
                        var query = from s in rel.Query
                                    where s.Get <int>("blockNum") == i
                                    select s;
                        if (blockStatsType != "Total")
                        {
                            query = from s in rel.Query
                                    where s.Get <int>("blockNum") == i
                                    where s.Get <string>(blockStatsType.ToLower()) == blockStatsSelection
                                    select s;
                        }
                        ParseObject stat = await query.FirstOrDefaultAsync();

                        DataPoint correct   = new DataPoint();
                        DataPoint incorrect = new DataPoint();
                        correct.YValues     = new double[] { stat == null ? 0 : stat.Get <int>("correct") };
                        incorrect.YValues   = new double[] { stat == null ? 0 : stat.Get <int>("total") - stat.Get <int>("correct") };
                        correct.AxisLabel   = curDate.ToShortDateString();
                        incorrect.AxisLabel = curDate.ToShortDateString();
                        correctDataPoints.Add(correct);
                        incorrectDataPoints.Add(incorrect);
                    }
                }
                else if (ddlFilterTime.SelectedValue == "PastMonth")
                {
                    rel           = student.GetRelation <ParseObject>("student" + blockStatsType + "DayStats");
                    endBlockNum   = DateUtils.getCurrentDayBlockNum();
                    startBlockNum = endBlockNum - 29;
                    curDate       = DateTime.Today.AddDays(-29);

                    for (int i = startBlockNum; i <= endBlockNum; i += 3, curDate = curDate.AddDays(3))
                    {
                        var query = from s in rel.Query
                                    where s.Get <int>("blockNum") >= i
                                    where s.Get <int>("blockNum") <= i + 2
                                    select s;
                        if (blockStatsType != "Total")
                        {
                            query = from s in rel.Query
                                    where s.Get <int>("blockNum") == i
                                    where s.Get <string>(blockStatsType.ToLower()) == blockStatsSelection
                                    select s;
                        }
                        IEnumerable <ParseObject> stats = await query.FindAsync();

                        int totalCorrect = 0, totalIncorrect = 0;
                        foreach (ParseObject stat in stats)
                        {
                            totalCorrect   += stat.Get <int>("correct");
                            totalIncorrect += stat.Get <int>("total") - stat.Get <int>("correct");
                        }
                        DataPoint correct   = new DataPoint();
                        DataPoint incorrect = new DataPoint();
                        correct.YValues     = new double[] { totalCorrect };
                        incorrect.YValues   = new double[] { totalIncorrect };
                        correct.AxisLabel   = curDate.ToShortDateString();
                        incorrect.AxisLabel = curDate.ToShortDateString();
                        correctDataPoints.Add(correct);
                        incorrectDataPoints.Add(incorrect);
                    }
                }
                else
                {
                    rel           = student.GetRelation <ParseObject>("student" + blockStatsType + "MonthStats");
                    endBlockNum   = DateUtils.getCurrentMonthBlockNum();
                    startBlockNum = endBlockNum - 11;
                    curDate       = DateTime.Today.AddMonths(-11);

                    for (int i = startBlockNum; i <= endBlockNum; i++, curDate = curDate.AddMonths(1))
                    {
                        var query = from s in rel.Query
                                    where s.Get <int>("blockNum") == i
                                    select s;
                        if (blockStatsType != "Total")
                        {
                            query = from s in rel.Query
                                    where s.Get <int>("blockNum") == i
                                    where s.Get <string>(blockStatsType.ToLower()) == blockStatsSelection
                                    select s;
                        }
                        ParseObject stat = await query.FirstOrDefaultAsync();

                        DataPoint correct   = new DataPoint();
                        DataPoint incorrect = new DataPoint();
                        correct.YValues     = new double[] { stat == null ? 0 : stat.Get <int>("correct") };
                        incorrect.YValues   = new double[] { stat == null ? 0 : stat.Get <int>("total") - stat.Get <int>("correct") };
                        correct.AxisLabel   = curDate.ToShortDateString();
                        incorrect.AxisLabel = curDate.ToShortDateString();
                        correctDataPoints.Add(correct);
                        incorrectDataPoints.Add(incorrect);
                    }
                }
                progressChart.SetUp(correctDataPoints, incorrectDataPoints);
                progressChart.Title = publicUserData.DisplayName + "'s Progress in " + FigureMostSpecificType();
                if (blockStatsType == "Subject")
                {
                    updatePanels[updatePanelCounter++].ContentTemplateContainer.Controls.Add(progressChart);
                }
                else
                {
                    catUpdatePanels[catUpdatePanelCounter++].ContentTemplateContainer.Controls.Add(progressChart);
                }
            }
            DoughnutChart chart1 = UpdatePanel1.ContentTemplateContainer.Controls[0] as DoughnutChart;

            if (chart1.NumCorrect + chart1.NumIncorrect == 0)
            {
                ClearCharts();
                lbNoResults.Visible = true;
            }
            else
            {
                lbNoResults.Visible = false;
            }
        }