public void SaveCurrentData()
        {
            SchoolClass schoolClass = new SchoolClass();

            Double[] gradeScaleValues;

            //Class Information
            //Class Name
            if ((textBoxClassName.Text != "") && (textBoxClassName.Text != @"(None)"))
            {
                schoolClass.className = textBoxClassName.Text;
            }
            else
            {
                MessageBox.Show("Class Name is invalid", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //Professor
            if (textBoxProfessor.Text != "")
            {
                schoolClass.professor = textBoxProfessor.Text;
            }
            else
            {
                schoolClass.professor = null;
            }

            //Term
            if (comboBoxTermSeason.Text != "")
            {
                schoolClass.termSeason = comboBoxTermSeason.Text;
                schoolClass.termYear   = (int)numericUpDownTermYear.Value;
            }
            else
            {
                schoolClass.termSeason = null;
                schoolClass.termYear   = -1;
            }

            //Credits
            schoolClass.credits = (int)numericUpDownCredits.Value;

            //Students
            schoolClass.enrolled = (int)numericUpDownEnrolled.Value;

            //Grade Scale
            //Grade Scale Format
            if (radioButtonAF.Checked == true)
            {
                schoolClass.gradeScaleFormat = 1;
            }
            else if (radioButtonSN.Checked == true)
            {
                schoolClass.gradeScaleFormat = 2;
            }
            else //note: this should never happen as radio buttons are being used.
            {
                MessageBox.Show("Grade Scale Format is invalid. (This should never happen)", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //Grade Scale Values
            //A-F
            int c = 0;

            if (schoolClass.gradeScaleFormat == 1)
            {
                TextBox[]  gradeScaleTextBoxes  = new TextBox[11];
                CheckBox[] gradeScaleCheckBoxes = new CheckBox[11];
                bool[]     gradeScaleEnableds   = new bool[11];

                gradeScaleTextBoxes[0]  = TextBoxA;
                gradeScaleTextBoxes[1]  = TextBoxAM;
                gradeScaleTextBoxes[2]  = TextBoxBP;
                gradeScaleTextBoxes[3]  = TextBoxB;
                gradeScaleTextBoxes[4]  = TextBoxBM;
                gradeScaleTextBoxes[5]  = TextBoxCP;
                gradeScaleTextBoxes[6]  = TextBoxC;
                gradeScaleTextBoxes[7]  = TextBoxCM;
                gradeScaleTextBoxes[8]  = TextBoxDP;
                gradeScaleTextBoxes[9]  = TextBoxD;
                gradeScaleTextBoxes[10] = TextBoxDM;

                /*
                 * gradeScaleCheckBoxes[0] = checkBoxA;
                 * gradeScaleCheckBoxes[1] = checkBoxAM;
                 * gradeScaleCheckBoxes[2] = checkBoxBP;
                 * gradeScaleCheckBoxes[3] = checkBoxB;
                 * gradeScaleCheckBoxes[4] = checkBoxBM;
                 * gradeScaleCheckBoxes[5] = checkBoxCP;
                 * gradeScaleCheckBoxes[6] = checkBoxC;
                 * gradeScaleCheckBoxes[7] = checkBoxCM;
                 * gradeScaleCheckBoxes[8] = checkBoxDP;
                 * gradeScaleCheckBoxes[9] = checkBoxD;
                 * gradeScaleCheckBoxes[10] = checkBoxDM;
                 */

                for (int i = 0; i < 11; i++)
                {
                    gradeScaleEnableds[c] = gradeScaleTextBoxes[i].Text != "";
                    c++;
                }

                gradeScaleValues = new double[11];

                c = 0;
                Double  prevVal = Double.MaxValue;
                Boolean flag    = false; //if no checkbox is enabled (other than F)
                foreach (Double element in gradeScaleValues)
                {
                    if (gradeScaleEnableds[c])
                    {
                        flag = true;
                        if (ErrorChecking.TextIsType("Double", gradeScaleTextBoxes[c].Text))
                        {
                            Double currentGradeScaleValue = Convert.ToDouble(gradeScaleTextBoxes[c].Text);
                            if (prevVal > currentGradeScaleValue)
                            {
                                gradeScaleValues[c] = currentGradeScaleValue;
                                prevVal             = currentGradeScaleValue;
                            }
                            else
                            {
                                MessageBox.Show("Grade scale has impossible values!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return;
                            }
                        }
                        else
                        {
                            MessageBox.Show("Non-numeric value entered in grade scale!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                    else
                    {
                        gradeScaleValues[c] = -1; //if this grade value is disabled
                    }
                    c++;
                }
                if (!flag)
                {
                    MessageBox.Show("At least one grade must be enabled!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                schoolClass.gradeScale = gradeScaleValues;
            }

            //S/N (TODO)
            else
            {
                return;
            }

            //Categories
            SaveCurrentCatData();
            schoolClass.catNames = categoryNames;
            foreach (string cat in schoolClass.catNames)
            {
                if (cat.Equals(""))
                {
                    MessageBox.Show("One or more categories have no name!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            schoolClass.catWorths = new double[categoryWorthsS.Length];
            c = 0;
            Double total = 0;

            foreach (string worthString in categoryWorthsS)
            {
                if (ErrorChecking.TextIsType("Double", worthString))
                {
                    schoolClass.catWorths[c] = Convert.ToDouble(worthString);
                    total += schoolClass.catWorths[c];
                    c++;
                }
                else
                {
                    MessageBox.Show("Category total percentage contains non-numeric value!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            if ((int)Math.Round(total) != 100)
            {
                var result = MessageBox.Show("% of total adds to " + total.ToString() + ". Continue?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (result == DialogResult.No)
                {
                    return;
                }
            }
            if (!schoolClass.IsParseable())
            {
                MessageBox.Show("An entry contains an illegal XML character! Try removing any symbols entered.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //Data has been verified and is ready to be written to a file!
            _oldClass?.RemapAssignments(schoolClass, true);
            _oldClass?.RemapCurves(schoolClass);
            if (_oldClass != null && !_oldClass.className.Equals(schoolClass.className))
            {
                XMLHandler.ChangeAssignmentDirName(schoolClass, _oldClass);
                XMLHandler.DeleteClass(_oldClass.className, false);
            }

            if (XMLHandler.SaveSchoolClassToFile(schoolClass, XMLHandler.D_SCHEMA_VER))
            {
                main.InitialSetup();
                this.Close();
            }
            else
            {
                MessageBox.Show("An entry contains an illegal XML character! Try removing any symbols entered.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemple #2
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);
            }
        }