Ejemplo n.º 1
0
        private void FillAssgnCheckedListBox(IEnumerable <string> activeCats)
        {
            List <string> prevUnchecked = new List <string>();
            List <string> prevExisted   = new List <string>();

            foreach (var item in CheckedListBoxAssignments.Items)
            {
                prevExisted.Add(item.ToString());
                if (!CheckedListBoxAssignments.CheckedItems.Contains(item))
                {
                    prevUnchecked.Add(item.ToString());
                }
            }
            CheckedListBoxAssignments.Items.Clear();

            List <string> assgnsInCats = new List <string>();

            foreach (var item in activeCats)
            {
                int          index      = _schoolClass.CatExists(item);
                Assignment[] tempAssgns = _schoolClass.GetAssgnsInCat(index);
                foreach (Assignment assgn in tempAssgns)
                {
                    assgnsInCats.Add(assgn.name);
                }
            }
            foreach (string assgnName in assgnsInCats)
            {
                CheckedListBoxAssignments.Items.Add(assgnName);
                if (prevExisted.Contains(assgnName))
                {
                    CheckedListBoxAssignments.SetItemChecked(CheckedListBoxAssignments.Items.IndexOf(assgnName),
                                                             !prevUnchecked.Contains(assgnName));
                }
                else
                {
                    CheckedListBoxAssignments.SetItemChecked(CheckedListBoxAssignments.Items.IndexOf(assgnName), true);
                }
            }
        }
Ejemplo n.º 2
0
        public static Assignment ParseAssgnJObjToAssignment(SchoolClass schoolClass, JObject assgn)
        {
            //TODO: add a date handler that will auto set if the assgn is active based on if the assignment due date has passed
            Assignment newAssignment = new Assignment
            {
                catIndex =
                    schoolClass.CatExists(
                        schoolClass.canvasData.canvasCategoryIDtoGCCatName
                        [assgn.GetValue("assignment_group_id").ToString()]),
                name  = CleanseName(assgn.GetValue("name").ToString()),
                outOf = assgn.GetValue("points_possible").ToObject <Double>()
            };

            try
            {
                newAssignment.active = !assgn.GetValue("omit_from_final_grade").ToObject <Boolean>();
            }
            catch
            {
                newAssignment.active = true;
            }

            HttpClient?.Dispose();
            HttpClient = new HttpClient
            {
                BaseAddress = BuildUri(SyncSettings.CanvasURL, SyncSettings.AccessToken, "courses/" + schoolClass.canvasData.id + "/assignments/" + assgn.GetValue("id") + "/submissions/self"),
                Timeout     = new TimeSpan(0, 0, SyncSettings.TimeoutLength)
            };
            HttpResponseMessage response;

            try
            {
                response = HttpClient.GetAsync(HttpClient.BaseAddress).Result;
            }
            catch
            {
                return(null);
            }

            if (!response.IsSuccessStatusCode)
            {
                MessageBox.Show(@"Error downloading data from Canvas. Check that the URL and access token are correct.", "Warning!", MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);
                return(null);
            }

            var    content      = response.Content.ReadAsStringAsync().Result;
            double pointsEarned = 0;

            try
            {
                JArray jArray = JArray.Parse(content);
                foreach (JObject jObj in jArray)
                {
                    if (jObj == null)
                    {
                        continue;
                    }
                    try
                    {
                        if (jObj.GetValue("grade_matches_current_submission") != null)
                        {
                            if (jObj.GetValue("grade_matches_current_submission").ToObject <Boolean>())
                            {
                                var temp = jObj.GetValue("score");
                                if (temp != null)
                                {
                                    pointsEarned = temp.ToObject <Double>();
                                }
                                break;
                            }
                        }
                    }
                    catch
                    {
                        continue;
                    }
                }
            }
            catch
            {
                try
                {
                    JObject jObj = JObject.Parse(content);
                    if (jObj.GetValue("grade_matches_current_submission").ToObject <Boolean>())
                    {
                        var temp = jObj.GetValue("score");
                        if (temp != null)
                        {
                            pointsEarned = temp.ToObject <Double>();
                        }
                    }
                }
                catch
                {
                    ;
                }
            }
            newAssignment.points = pointsEarned;

            return(newAssignment);
        }
Ejemplo n.º 3
0
        public static SchoolClass SyncCanvasCategories(SchoolClass schoolClass)
        {
            HttpClient?.Dispose();
            HttpClient = new HttpClient
            {
                BaseAddress = BuildUri(SyncSettings.CanvasURL, SyncSettings.AccessToken, "courses/" + schoolClass.canvasData.id + "/assignment_groups"),
                Timeout     = new TimeSpan(0, 0, SyncSettings.TimeoutLength)
            };
            HttpResponseMessage response;

            try
            {
                response = HttpClient.GetAsync(HttpClient.BaseAddress).Result;
            }
            catch
            {
                return(null);
            }

            if (!response.IsSuccessStatusCode)
            {
                MessageBox.Show(schoolClass.className + ":\nError downloading data from Canvas. Check that the URL and access token are correct.", "Warning!", MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);
                return(null);
            }

            var content = response.Content.ReadAsStringAsync().Result;

            SchoolClass newSchoolClass = schoolClass;

            try
            {
                JArray jArray = JArray.Parse(content);
                int    c      = 0;
                foreach (JObject jObj in jArray)
                {
                    try
                    {
                        if (newSchoolClass.canvasData.canvasCategoryIDtoGCCatName == null)
                        {
                            newSchoolClass.canvasData.canvasCategoryIDtoGCCatName = new Dictionary <string, string>();
                        }

                        if (schoolClass.canvasData.canvasCategoryIDtoGCCatName.ContainsKey(jObj.GetValue("id")
                                                                                           .ToString())) //this category has already been synced, so we just need to update it
                        {
                            int index = schoolClass.CatExists(schoolClass.canvasData.canvasCategoryIDtoGCCatName[jObj.GetValue("id")
                                                                                                                 .ToString()]);
                            if (index == -1)
                            {
                                //this should probably never happen, but if it somehow does...
                                throw new ArgumentOutOfRangeException();
                            }

                            if (newSchoolClass.catNames[c] != jObj.GetValue("name").ToString())
                            {
                                //if the category name needs to be updated, remap the current assignments
                                SchoolClass temp = newSchoolClass;
                                temp.catNames[c] = jObj.GetValue("name").ToString();
                                newSchoolClass.RemapAssignments(temp, false);
                                newSchoolClass.RemapCurves(temp);
                                newSchoolClass.catWorths[c] = jObj.GetValue("group_weight").ToObject <Double>();
                            }
                        }
                        else //we need to add this category to the newSchoolClass
                        {
                            int index = newSchoolClass.CatExists(jObj.GetValue("name").ToString());
                            if (index == -1)
                            {
                                //the category does not exist, so we need to create it
                                Array.Resize(ref newSchoolClass.catNames, newSchoolClass.catNames.Length + 1);
                                newSchoolClass.catNames[newSchoolClass.catNames.Length - 1] = jObj.GetValue("name").ToString();

                                Array.Resize(ref newSchoolClass.catWorths, newSchoolClass.catWorths.Length + 1);
                                newSchoolClass.catWorths[newSchoolClass.catWorths.Length - 1] = jObj.GetValue("group_weight").ToObject <Double>();
                            }

                            newSchoolClass.canvasData.canvasCategoryIDtoGCCatName.Add(jObj.GetValue("id")
                                                                                      .ToString(), jObj.GetValue("name").ToString());
                        }

                        if (jObj.ContainsKey("rules") && jObj["rules"] != null)
                        {
                            var rules = JObject.Parse(jObj.GetValue("rules").ToString());
                            if (rules.ContainsKey("drop_lowest"))
                            {
                                int   toDrop    = rules["drop_lowest"].ToObject <int>();
                                Curve tempCurve = new Curve("$$$ADJUST$$$" + "Drop lowest in " + jObj.GetValue("name"));
                                tempCurve.active = true;
                                //TODO: Add support for "never_drop" assignments from the Canvas API
                                tempCurve.kept = toDrop;
                                tempCurve.appliedCatIndexes = new int[] { newSchoolClass.CatExists(jObj.GetValue("name").ToString()) };
                                XMLHandler.SaveCurveToFile(newSchoolClass, tempCurve, false);
                            }
                        }
                    }
                    catch
                    {
                        //the response from canvas was messed up, but we can safely ignore it and move onto the next
                    }
                    c++;
                }
                return(newSchoolClass);
            }
            catch
            {
                //the JArray could not be parsed, and therefore something is critically wrong and we cannot sync, so we return the input
                MessageBox.Show(schoolClass.className + ":\nError downloading data from Canvas. Check that the URL and access token are correct.", "Warning!", MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);
                return(schoolClass);
            }
        }