/* * NAME * * Class_Form::Delete_One_Click - deletes one selected student. * * SYNOPSIS * * void Delete_One_Click(object sender, EventArgs e); * * sender --> the object sending the event. * e --> the event arguments. * * DESCRIPTION * * This function gets the selected student and * sends their details to the database interface * to be deleted. */ private void Delete_One_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("Are you sure you want to delete this student? (This will also delete all notes associated with the student.)", "Delete Student", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { //Populate_Textboxes(); //Student to_delete = Textboxes_To_Student(); //Get row DataGridViewRow row = StudentPanel.SelectedRows[0]; //Get id int to_delete = Convert.ToInt32(row.Cells[0].Value); //Cannot cast properly //Delete notes and student info Database_Interface.Delete_Notes(to_delete); Database_Interface.Delete_Student(to_delete); //Refresh view Clear_TextBoxes(); Refresh_Data_Grid_View(); } else { return; } }
/* * NAME * * Conference_Form::Edit_Note_Click - event that triggers when the user clicks the Edit Note button. * * SYNOPSIS * * void Edit_Note_Click(object sender, EventArgs e); * * sender --> the object sending the event. * e --> the event arguments. * * DESCRIPTION * * This function triggers when the user clicks the Edit Note button. * First, it verifies that the user has selected a note from the view. * It creates a note object and sends it to a form to edit the note. */ private void Edit_Note_Click(object sender, EventArgs e) { //If nothing selected, pop-up message if (notes.SelectedItems.Count == 0) { MessageBox.Show("Please select a note to edit"); return; } Note edit_note = new Note(); ListViewItem item = notes.SelectedItems[0]; edit_note.id = Convert.ToInt32(item.SubItems[0].Text); edit_note.student_id = Convert.ToInt32(item.SubItems[1].Text); edit_note.date = item.SubItems[2].Text; edit_note.note = item.SubItems[3].Text; edit_note.category = item.SubItems[4].Text; using (New_Note_Form edit = new New_Note_Form()) { edit.Note = edit_note.note; if (edit.ShowDialog() == DialogResult.OK) { Database_Interface.Update_Note(edit_note.id, edit.Note, new Conference_Types(edit.Category).Type); } } Refresh_Notes(); }
/* * NAME * * IO::Import - Imports the data from a file * * SYNOPSIS * * void Import(string filename); * * filename --> the name of the file to import * * DESCRIPTION * * This function contains the brunt of the importing work. It takes a filename, * reads the file line by line and converts it into data to be stored on the database. */ private static void Import(string filename) { StreamReader reader = new StreamReader(filename); int count = 0; string line; while ((line = reader.ReadLine()) != null) //https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-read-a-text-file-one-line-at-a-time { if (count == 0) //if we are on the first line, then it is student data { try { Student import_student = String_To_Student(line); Database_Interface.Add_Student(import_student); } catch (Exception e) { new Log(e, "IO.cs: Import.", "Could not import file."); MessageBox.Show("Error importing student."); return; } } else if (line.Substring(0, 4).ToLower() == "note") { Note import_note = String_To_Note(line); Database_Interface.Add_Note(import_note); } else if (line.Substring(0, 4).ToLower() == "hist") { History_Entry import_entry = String_To_History_Entry(line); Database_Interface.Add_History(import_entry); } count++; } }
/* * NAME * * Class_Form::New_Student_Click - Verifies a student and sends him or her to the database. * * SYNOPSIS * * void New_Student_Click(object sender, EventArgs e); * * sender --> the object sending the event. * e --> the event arguments. * * DESCRIPTION * * Pulls the student data from the textboxes and verifies that the student is valid. * Alerts user if student already exists, and asks for further action. If the student passes the checks, * the function sends the student to the database. */ private void New_Student_Click(object sender, EventArgs e) //https://stackoverflow.com/questions/3036829/how-do-i-create-a-message-box-with-yes-no-choices-and-a-dialogresult { //Get all values from textbox Student new_student = Textboxes_To_Student(); //if part of student is incorrect, do nothing! Let user edit and try again. if (new_student.FirstName == "" || new_student.LastName == "" || !Regex.IsMatch(new_student.StartLevel.ToString(), @"^[A-Z]+$") || !Regex.IsMatch(new_student.CurrentLevel.ToString(), @"^[A-Z]+$") || !Regex.IsMatch(new_student.GoalLevel.ToString(), @"^[A-Z]+$")) { return; } if (Database_Interface.Query_Student_Exist(new_student.FirstName, new_student.LastName)) //if student already exists... { DialogResult dialog = MessageBox.Show("Student already exists. Would you like to update their records?", "Student already exists", MessageBoxButtons.YesNo); if (dialog == DialogResult.Yes) { Database_Interface.Update_Student(new_student); } else if (dialog == DialogResult.No) { return; //close dialog and do nothing } } else if (cf_id.Text != "") { Database_Interface.Update_Student(new_student); } else { Database_Interface.Add_Student(new_student); } Clear_TextBoxes(); Refresh_Data_Grid_View(); }
/* * Program::Main * * NAME * * Program::Main - The main function for the application. * * DESCRIPTION * * This function is the starting point for the application. * It runs the welcome form if it detects that the program has * not been run before, or the main menu if the program has * been used before. */ static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); string filename = @"\startup.txt"; string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\HomeroomHelper\Settings"; string logpath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\HomeroomHelper\Logs"; if (!System.IO.File.Exists(filepath + filename)) { //Intialize folders and database System.IO.Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\HomeroomHelper"); System.IO.Directory.CreateDirectory(filepath); System.IO.Directory.CreateDirectory(logpath); Database_Interface.Create_DB(); //One-time form Application.Run(new Welcome_Form()); System.IO.File.WriteAllText(filepath + filename, "1"); } else { Application.Run(new App()); } }
/* * NAME * * Analytics_Form::Analytics_Form - Form Constructor * * DESCRIPTION * * This function initializes the Analytics_Form. * It also instantiates the elements of the form - the * bar graph, line graph, and level average. */ public Analytics_Form() { InitializeComponent(); students = Database_Interface.Query_All_Students(); Instantiate_Bar_Graph(); Instantiate_Avg_Textbox(); Instantiate_Names_Combobox(); }
/* * NAME * * Help_Reader_Form::Student_List_SelectedIndexChanged - event that changes the reading level * based on the student. * * SYNOPSIS * * void Help_Reader_Form::Student_List_SelectedIndexChanged(object sender, EventArgs e); * * sender --> object sending the event. * e --> the event arguments. * * DESCRIPTION * * This function triggers when the selected student has changed. It loads their current * reading level from the database into a textbox to be viewed. * */ private void Student_List_SelectedIndexChanged(object sender, EventArgs e) { //Save id in hidden textbox int id = students.ElementAt(Student_List.SelectedIndex).ID; Student_ID_Box.Text = id.ToString(); //Load current reading lvl char lvl = Database_Interface.Query_Student(id).CurrentLevel; Reading_Lvl_Txtbox.Text = lvl.ToString(); }
/* * NAME * * Conference_Form::Refresh_Notes - refreshes the notes window. * * DESCRIPTION * * This function refreshes the notes window by clearing it, and then reloading from the database. */ private void Refresh_Notes() { notes.Items.Clear(); List <Note> student_notes = Database_Interface.Query_Note(current_student_id); //Display in listview foreach (Note n in student_notes) { ListViewItem item = new ListViewItem(new string[] { Convert.ToString(n.id), Convert.ToString(n.student_id), n.date, n.note, n.category }); notes.Items.Add(item); } }
/* * NAME * * Conference_Form::Student_Select_SelectedIndexChanged - event that triggers when the user selects a new student. * * SYNOPSIS * * void Student_Select_SelectedIndexChanged(object sender, EventArgs e); * * sender --> the object sending the event. * e --> the event arguments. * * DESCRIPTION * * This function triggers when the user selects a new student from the combobox. */ private void Student_Select_SelectedIndexChanged(object sender, EventArgs e) { int combobox_id = Student_Select.SelectedIndex; string[] fullname = new string[2]; fullname[0] = students[combobox_id][0]; fullname[1] = students[combobox_id][1]; //MessageBox.Show(fullname[0] + " " + fullname[1]); current_student_id = Database_Interface.Query_ID(fullname[0], fullname[1]); Refresh_Notes(); }
/// <summary> /// The class supporting the Student Conferences form. /// </summary> /* * NAME * * Conference_Form::Conference_Form - The constructor for the class * * DESCRIPTION * * This function will instantiate the form. It also loads the student * names into the combobox on the form. */ public Conference_Form() { InitializeComponent(); //Load ComboBox List <string[]> names = Database_Interface.Query_Student_Names(); foreach (string[] name in names) { Student_Select.Items.Add(name[0] + " " + name[1]); students.Add(name); } }
/* * NAME * * Help_Reader_Form::Populate_Combobox - event that populates the combobox with student names. * * DESCRIPTION * * This function loads all student names from the database into the combobox in a "First Last" format. * */ private void Populate_Combobox() { students = Database_Interface.Query_All_Students(); //Query db for all students foreach (Student s in students) { Student_List.Items.Add(s.FirstName + " " + s.LastName); //Add names to combobox } for (char c = 'A'; c <= 'Z'; c++) { Level.Items.Add(c); } }
/* * NAME * * Class_Form::Populate_Data_Grid_View - loads data into the data grid view. * * DESCRIPTION * * This function gets the student data from the database and loads it into the data grid view object. */ private void Populate_Data_Grid_View() { //Get list of all students List <Student> all_students = Database_Interface.Query_All_Students(); int num_students = Database_Interface.Query_Num_Students(); //Add each student to student panel for (int i = 0; i < num_students; i++) { string[] student = all_students.ElementAt(i).ToArray(); StudentPanel.Rows.Add(student); } }
/* * NAME * * IO::Students_To_File - Exports students to individual files * * SYNOPSIS * * void Students_To_File (List<Student> students); * * students --> a list of student objects to be exported. * * DESCRIPTION * * This function takes each student and saves their data to a unique file. */ //https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-write-to-a-text-file public static void Students_To_File(List <Student> students) { try { string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\HomeroomHelper\"; foreach (Student s in students) { string filename = path + s.FirstName + s.LastName + Generate_File_Date() + ".csv"; string line = ""; line += s.ID + ","; line += s.FirstName + ","; line += s.LastName + ","; line += s.StartLevel + ","; line += s.CurrentLevel + ","; line += s.GoalLevel + Environment.NewLine; System.IO.File.WriteAllText(filename, line); List <Note> notes = Database_Interface.Query_Note(s.ID); foreach (Note n in notes) { string note_line = "note,"; note_line += n.id + ","; note_line += n.student_id + ","; note_line += n.date + ","; note_line += n.note + ","; note_line += n.category + Environment.NewLine; System.IO.File.AppendAllText(filename, note_line); } List <History_Entry> entries = Database_Interface.Query_History_Entries(s.ID); foreach (History_Entry h in entries) { string entry_line = "entry,"; entry_line += h.id + ","; entry_line += h.student_id + ","; entry_line += h.date + ","; entry_line += h.current_lvl + Environment.NewLine; System.IO.File.AppendAllText(filename, entry_line); } } } catch (Exception e) { new Log(e, "IO.cs: Students_To_File", "Failed to save student data to file."); } }
/* * NAME * * Class_Form::Delete_All_Click - deletes all students. * * SYNOPSIS * * void Delete_All_Click(object sender, EventArgs e); * * sender --> the object sending the event. * e --> the event arguments. * * DESCRIPTION * * This function verifies that the user wants to pursue * this action. If yes, the database interface drops all * tables and recreates them. */ private void Delete_All_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("Are you sure you want to delete all students? (This will also delete all notes.)", "Delete Student", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { Database_Interface.Delete_All_Students(); Clear_TextBoxes(); Refresh_Data_Grid_View(); } else { return; } }
/* * NAME * * Conference_Form::Delete_Note_Click - event that triggers when the user * clicks the Delete Note button. * * SYNOPSIS * * void Delete_Note_Click(object sender, EventArgs e); * * sender --> the object sending the event. * e --> the event arguments. * * DESCRIPTION * * This function triggers when the user clicks the Delete Note button. * First, the function verifies that a note has been selected. Then, it * gets the note id and sends it to the database to be deleted. * Finally, it refreshes the view. */ private void Delete_Note_Click(object sender, EventArgs e) { //If nothing selected, pop-up message if (notes.SelectedItems.Count == 0) { MessageBox.Show("Please select a note to delete"); return; } int note; ListViewItem item = notes.SelectedItems[0]; note = Convert.ToInt32(item.SubItems[0].Text); Database_Interface.Delete_Note(note); Refresh_Notes(); }
/* * NAME * * Conference_Form::Add_Note_Click - event that triggers when the user clicks the Add Note button. * * SYNOPSIS * * void Add_Note_Click(object sender, EventArgs e); * * sender --> the object sending the event. * e --> the event arguments. * * DESCRIPTION * * This function triggers when the user clicks the Add Note button. * The function first verifies that a student has been selected. * Then, the function creates a new note and sends it to the database. */ private void Add_Note_Click(object sender, EventArgs e) { if (!(Student_Select.SelectedIndex > -1)) { MessageBox.Show("Please select a student.", "Error"); return; } using (New_Note_Form new_note = new New_Note_Form()) { if (new_note.ShowDialog() == DialogResult.OK) { string category = new Conference_Types(new_note.Category).Type; Database_Interface.Add_Note(current_student_id, new_note.Note, category); } } Refresh_Notes(); }
/* * NAME * * Analytics_Form::Instantiate_Line_Graph - instantiates the line graph for a student. * * SYNOPSIS * * void Analytics_Form::Instantiate_Line_Graph(int id); * * id --> the id of the selected student. * * DESCRIPTION * * This function retrieves the historical data using the student's id, * and displays it in a line graph using a chart object. */ private void Instantiate_Line_Graph(int id) { Lvl_History_Chart.Series.Clear(); Lvl_History_Chart.Series.Add("Reading Levels"); Lvl_History_Chart.Series["Reading Levels"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; //Lvl_History_Chart.ChartAreas.Add("Reading Levels"); List <History_Entry> entries = new List <History_Entry>(); entries = Database_Interface.Query_History_Entries(id); //all entries for that student foreach (History_Entry he in entries) { Lvl_History_Chart.Series["Reading Levels"].Points.AddXY(he.date, (int)he.current_lvl); } Lvl_History_Chart.DataBind(); }
/* * NAME * * Help_Reader_Form::Level_Up_Button_Click - event that increases the reading level * of that student by 1. * * SYNOPSIS * * void Help_Reader_Form::Level_Up_Button_Click(object sender, EventArgs e); * * sender --> object sending the event. * e --> the event arguments. * * DESCRIPTION * * This function triggers when the user clicks the "Level Up!" button. It increases the student * level by 1, or does nothing if they've reached the maximum reading level. * */ private void Level_Up_Button_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("Are you sure you want to level up this student?", "Are you sure?", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { //Increase reading level by 1 string student_id = Student_ID_Box.Text; int id = Convert.ToInt32(Student_ID_Box.Text); char current_level = Database_Interface.Query_Reading_Level(id); if (!current_level.Equals('Z')) { char new_level = (char)(Convert.ToUInt16(current_level) + 1); Database_Interface.Update_Reading_Lvl(student_id, new_level); //Refresh current level char lvl = Database_Interface.Query_Student(id).CurrentLevel; Reading_Lvl_Txtbox.Text = lvl.ToString(); } } }
/// <summary> /// The class supporting the main menu form. /// </summary> /* * NAME * * App_Form::App - The constructor for the class * * DESCRIPTION * * This function will instantiate the form. It also initializes the sentences on the dashboard. */ public App() { InitializeComponent(); string greeting = "Teacher"; { Teacher teacher = IO.Load_Teacher(); try { if (teacher.First_Name != string.Empty) { greeting = teacher.First_Name; } } catch (Exception e) { new Log(e, "App_Form.cs: Constructor", "Teacher not assigned at start"); } } welcome_label.Text = "Welcome, " + greeting + "!"; num_students_label.Text = "You have " + Database_Interface.Query_Num_Students() + " students in your class."; high_score.Text = "The highest reading level in your class is " + Database_Interface.Query_Max_Level() + "."; }
/* * NAME * * Class_Form::Export_All_Click - sends all students to IO class to be exported. * * SYNOPSIS * * void Export_All_Click(object sender, EventArgs e); * * sender --> the object sending the event. * e --> the event arguments. * * DESCRIPTION * * This function takes all the student data and sends it to the IO class * to be exported separately. It also informs the user where the files will be saved. */ private void Export_All_Click(object sender, EventArgs e) { IO.Students_To_File(Database_Interface.Query_All_Students()); MessageBox.Show("Exports saved in HomeroomHelper directory"); }