private void SaveCurve(bool warning = true) { //everything in the required group box if (TextBoxCurveName.Text.StartsWith("$$$ADJUST$$$")) { MessageBox.Show( "You sly dog! You can't start a curve with \"$$$ADJUST$$$\" because it has a special meaning" + " in this program.", "Someone read the code!", MessageBoxButtons.OK, MessageBoxIcon.Hand); return; } Curve newCurve = new Curve(TextBoxCurveName.Text); newCurve.active = CheckBoxCurveActive.Checked; List <int> enabledCatIndexes = new List <int>(); foreach (var item in CheckedListBoxCategories.CheckedIndices) { enabledCatIndexes.Add(Convert.ToInt32(item)); } newCurve.appliedCatIndexes = enabledCatIndexes.ToArray(); List <string> enabledAssgns = new List <string>(); foreach (var item in CheckedListBoxAssignments.CheckedItems) { enabledAssgns.Add(item.ToString()); } newCurve.appliedAssgnNames = enabledAssgns.ToArray(); //the actual curve method if (RadioButtonDrop.Checked) { newCurve.kept = -1 * (int)NumericUpDownDrop.Value; if (!Settings.unrestrictedCurves) { if (enabledCatIndexes.Count > 1) { MessageBox.Show("This curve can only be applied to 1 category. Cannot apply/save curve.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } else if (RadioButtonKeep.Checked) { newCurve.kept = (int)NumericUpDownKeep.Value; if (!Settings.unrestrictedCurves) { if (enabledCatIndexes.Count > 1) { MessageBox.Show("This curve can only be applied to 1 category. Cannot apply/save curve.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } else if (RadioButtonConDropPercent.Checked) { double val; if (Double.TryParse(TextBoxConDropPercent.Text, out val)) { newCurve.conDropPercent = val; } else { MessageBox.Show("Value entered for drop if below % is invalid.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else if (RadioButtonConDropPoints.Checked) { double val; if (double.TryParse(TextBoxConDropPoints.Text, out val)) { newCurve.conDropPoints = val; } else { MessageBox.Show("Value entered for drop if below x points is invalid.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else if (RadioButtonCurveToMean.Checked) { double val; if (double.TryParse(TextBoxCurveToMean.Text, out val)) { newCurve.goalMeanPercent = val; } else { MessageBox.Show("Value entered for curve to mean % is invalid.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (!Settings.unrestrictedCurves) { if (enabledCatIndexes.Count > 1) { MessageBox.Show("This curve can only be applied to 1 category. Cannot apply/save curve.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } else if (RadioButtonAdditivePercent.Checked) { double val; if (double.TryParse(TextBoxAdditivePercent.Text, out val)) { newCurve.additivePercent = val / 100; } else { MessageBox.Show("Value entered for add x % is invalid.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else if (RadioButtonAdditivePoints.Checked) { double val; if (double.TryParse(TextBoxAdditivePoints.Text, out val)) { newCurve.additive = val; } else { MessageBox.Show("Value entered for add x points is invalid.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else if (RadioButtonMultiplicative.Checked) { double val; if (double.TryParse(TextBoxMultiplicative.Text, out val)) { newCurve.multiplicative = val; } else { MessageBox.Show("Value entered for multiply by x is invalid.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else if (!Settings.unrestrictedCurves) { MessageBox.Show("No curve method selected. Cannot save.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (_schoolClass.CurveExists(newCurve.name) != -1) //if the curve will be overwritten { var result = DialogResult.Yes; if (warning) { result = MessageBox.Show("Data already exists for " + newCurve.name + ". Overwrite the file?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); } if (result == DialogResult.Yes) { XMLHandler.DeleteCurve(_schoolClass, newCurve); } else { return; } } try { XMLHandler.SaveCurveToFile(_schoolClass, newCurve, true); } catch { if (warning) { MessageBox.Show("Curve name is not valid.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } return; } _schoolClass.LoadCurves(); DisplayCurvedData(); FillCurvesCheckedListBox(); CheckedListBoxCurves.SetSelected(CheckedListBoxCurves.Items.IndexOf(newCurve.name), true); }
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); } }