CourseDB
Documentation for CourseDB.
CourseDB.CourseCourseDB.FileDataCourseDB.GradeCourseDB.ResultCourseDB.StudentBase.namesCourseDB.addGradesCourseDB.addStudentsCourseDB.coursesCourseDB.createCourseCourseDB.emailsCourseDB.getGradesCourseDB.gradesnamesCourseDB.readdataCourseDB.writedata
CourseDB.Course — TypeCourseA structure representing a course, with multiple constructors for creating Course objects in various formats.
Fields
id::Union{String, Int}: A unique identifier for the course, which can either be a string (default is"new") or an integer (after the course is created in the database).term::String: The academic term in which the course is offered, represented as a string (e.g.,"241").code::String: The course code (e.g.,"MATH371") with spaces removed and converted to uppercase.name::String: The full name of the course (e.g.,"Introduction to Numerical Computing").section::String: The section number of the course, represented as a string.students::Vector{Student}: A list (vector) of students enrolled in the course.
Constructors
Course(term::String, code::String, name::String, section::String): Creates a newCoursewith the term, code, name, and section as strings, initializingidto"new"andstudentsto an empty array.Course(term::Integer, code::String, name::String, section::Integer): Creates a newCoursewith an integer term and section, with the code converted to uppercase and spaces removed.Course(term::Integer, code::String, name::String, section::String): Creates a newCoursewith an integer term, string section, and processes the course code similarly.Course(term::String, code::String, name::String, section::Integer): Creates aCoursewith a string term and an integer section.Course(c::Course, id::Int): Creates a newCoursewith an updated ID while retaining all other attributes of the existingCourseobject.Course(c::Course, students::Vector{Student}): Creates a newCourseby updating the list of students, while keeping all other course information the same.
Example
course = Course(241, "MATH 371", "Introduction to Numerical Computing", 1)
println(course.code) # Prints "MATH371"
println(course.id) # Prints "new"CourseDB.FileData — TypeFileData(path::String, fields::Dict{String, String}, data::DataFrame)A structure representing data loaded from a file, along with metadata about its fields.
Fields
path::String: The file path from which the data was read.fields::Dict{String, String}: A dictionary mapping column names to their data types. The keys are the column names (as strings), and the values are either"number"or"string", indicating the type of data in each column.data::DataFrame: The actual data loaded from the file, stored as aDataFrame.
Example
file_data = FileData("students.csv", Dict("id" => "number", "name" => "string"), DataFrame())
println(file_data.path) # Prints the file path
println(file_data.fields) # Prints the fields dictionary
println(file_data.data) # Prints the DataFrameCourseDB.Grade — TypeGradeA structure representing a detailed record of a student's grade for a specific assessment in a course.
Fields
student_id::Int: The unique identifier of the student.student_name::String: The full name of the student.student_email::String: The email address of the student.course_id::Int: The unique identifier of the course.course_code::String: The code of the course (e.g.,"MATH371").course_name::String: The full name of the course (e.g.,"Introduction to Numerical Computing").name::String: The name of the assessment or assignment (e.g.,"Midterm Exam").value::Float64: The grade received by the student for the assessment.max_value::Float64: The maximum possible grade for the assessment.
Example
grade = Grade(12345, "John Doe", "johndoe@example.com", 1, "MATH371", "Introduction to Numerical Computing", "Final Exam", 90.0, 100.0)
println(grade.student_name) # Prints "John Doe"
println(grade.value) # Prints 90.0
CourseDB.Result — TypeResultA structure used to represent the outcome of an operation, which can include data and a corresponding message.
Fields
data::Union{Nothing, Vector{Student}, Vector{Grade}, DataFrame}: The result of an operation. This field can contain:Nothing: If no data is available or the operation failed.Vector{Student}: A list ofStudentobjects, typically representing enrolled students.Vector{Grade}: A list ofGradeobjects, typically representing grades assigned to students.DataFrame: ADataFramecontaining tabular data, such as a dataset from an external source (e.g., a file).
message::String: A descriptive message accompanying the result, which may provide feedback on the operation (e.g., success or error messages).
Example
result = Result(Vector{Student}(), "Operation successful")
println(result.message) # Prints "Operation successful"
println(result.data) # Prints an empty vector of studentsCourseDB.Student — TypeStudent(id::Int, name::String, email::String)A structure representing a student with their basic details.
Fields
id::Int: The student's unique identifier, typically a numeric value.name::String: The student's full name.email::String: The student's email address.
Example
student = Student(12345, "John Doe", "johndoe@example.com")
println(student.id) # Prints the student ID
println(student.name) # Prints the student name
println(student.email) # Prints the student emailBase.names — Methodnames(c::Course)::Vector{String}Retrieves the names of students enrolled in a given Course.
Arguments
c::Course: ACourseobject containing student data.
Returns
- A
Vector{String}of student names. If the course has no students, returns an empty array.
Example
course = createCourse(241, "MATH 371", "Introduction to Numerical Computing", 1)
student_names = names(course)
println(student_names) # Prints a vector of student namesCourseDB.addGrades — MethodaddGrades(c::Course, file_path::String, args...;
fields::Union{Dict{Symbol, String}, Dict{Symbol, Any}}=Dict(:sid => "sid", :name => "name", :value => "value", :max_value => "max_value"), kwargs...)Adds student grades to a Course object by reading grade data from a file and creating Grade objects.
Arguments
c::Course: TheCourseobject to which grades will be added.file_path::String: The path to the file containing grade data. Supported file types arecsv,txt, andxlsx.args...: Additional positional arguments passed to the file reading function (readdata).fields::Union{Dict{Symbol, String}, Dict{Symbol, Any}}: A dictionary mapping the grade data fields (student ID, name, value, max value) to their corresponding column names in the file. Default mapping is:sid => "sid",:name => "name",:value => "value", and:max_value => "max_value". Each field can also be a tuple where the first value is the column name, and the second value is a transformation function.kwargs...: Additional keyword arguments passed to the file reading function.
Returns
- A
Courseobject with the added grades.
Workflow
- Reads the grade data from the specified file using
readdata. - Extracts student IDs, names, grade values, and maximum possible values based on the provided or default
fieldsmapping. - Creates
Gradeobjects for each student in the course. - Filters out any grades that do not match the existing student IDs in the course.
- Adds the valid grades to the course using the
add_student_gradesfunction. - If some grades cannot be matched to students, a warning is issued.
Example
course = createCourse(241, "MATH 371", "Introduction to Numerical Computing", 1)
updated_course = addGrades(course, "grades.csv", delim=',', header=true)
println(updated_course.students) # Prints the course with students and their gradesCourseDB.addStudents — MethodaddStudents(c::Course, file_path::String, args...; fields::Union{Dict{Symbol, String}, Dict{Symbol, Any}}=Dict(:id => "id", :name => "name", :email => "email"), kwargs...)Adds students to a Course object by reading data from a file and creating Student objects based on the file content.
Arguments
c::Course: TheCourseobject to which students will be added.file_path::String: The path to the file containing student data. Supported file types arecsv,txt, andxlsx.args...: Additional positional arguments passed to the file reading function (readdata).fields::Union{Dict{Symbol, String}, Dict{Symbol, Any}}: A dictionary mapping the student data fields (ID, name, and email) to their corresponding column names in the file. By default, it assumes:id => "id",:name => "name", and:email => "email". If any field is a tuple, the first value is the column name, and the second value is a transformation function.kwargs...: Additional keyword arguments passed to the file reading function.
Returns
- A new
Courseobject containing the students loaded from the file, with student data saved to the database.
Workflow
- Reads the student data from the specified file using the
readdatafunction. - Extracts student IDs, names, and emails based on the provided or default
fieldsmapping. - Converts the extracted data to create
Studentobjects. - Associates the students with the provided course and saves the student data to the database.
Example
course = Course("241", "MATH 371", "Introduction to Numerical Computing", 1)
updated_course = addStudents(course, "students.csv", delim=',', header=true)
println(updated_course.students) # Prints the list of added studentsCourseDB.courses — Methodcourses(term::Union{Int,String}) -> Vector{Course}
courses() -> Vector{Course}Retrieves a list of courses. The function can either return courses for a specific term or return all courses.
Methods
courses(term::Union{Int,String}): Returns courses for a specific term.courses(): Returns all courses, regardless of the term.
Arguments
term::Union{Int,String}: The term for which courses should be retrieved. If anIntis provided, it is converted to a string (e.g.,241becomes"241"). AStringterm can also be passed directly.
Returns
Vector{Course}: A vector containingCourseobjects. Each object represents a course in the system.
Example
courses_for_term = courses(241) # Retrieves courses for the term "241"
all_courses = courses() # Retrieves all coursesCourseDB.createCourse — MethodcreateCourse(term::Union{Integer, String}, code::String, name::String, section::Union{Integer, String})::CourseCreates a Course object and populates it with students by fetching data from a database.
Arguments
term::Union{Integer, String}: The term in which the course is offered. This can be an integer (e.g.,241) or a string (e.g.,"241").code::String: The course code (e.g.,"MATH 371").name::String: The name of the course (e.g.,"Introduction to Numerical Computing").section::Union{Integer, String}: The section number of the course, either as an integer or string.
Returns
- A
Courseobject that contains:course_with_id: The course details (term, code, name, section) with an assigned unique ID from the database.students: A list or collection of students enrolled in the course, fetched from the database.
Example
course = createCourse("241", "MATH 371", "Introduction to Numerical Computing", 1)
println(course.students) # Prints the list of students enrolledCourseDB.emails — Methodemails(c::Course)::Vector{String}Retrieves the email addresses of students enrolled in a given Course.
Arguments
c::Course: ACourseobject containing student data.
Returns
- A
Vector{String}of student email addresses. If the course has no students, returns an empty array.
Example
course = createCourse(241, "MATH 371", "Introduction to Numerical Computing", 1)
student_emails = emails(course)
println(student_emails) # Prints a vector of student email addressesCourseDB.getGrades — MethodgetGrades(c::Course)::Union{Nothing, Vector{Grade}}Retrieves the grades of students enrolled in a given Course.
Arguments
c::Course: ACourseobject for which to retrieve the grades.
Returns
- A
Vector{Grade}containing the grades of the students if the course has an existing ID. - If the course is marked as "new" (i.e.,
c.id == "new"), returnsnothingbecause no grades are associated with a new course.
Example
course = createCourse(241, "MATH 371", "Introduction to Numerical Computing", 1)
grades = getGrades(course)
if grades !== nothing
println(grades) # Prints the grades of the students
else
println("No grades available for a new course.")
endCourseDB.gradesnames — Methodgradesnames(c::Course) -> Vector{String}Retrieves a unique list of grade names (e.g., assignment or exam names) for a given course.
Arguments
c::Course: The course for which the grade names are to be retrieved.
Returns
Vector{String}: A vector containing the unique names of the assessments or assignments (e.g.,"Quiz1","MidtermExam","FinalProject") for the course.
Example
course = createCourse(241, "MATH 371", "Introduction to Numerical Computing", 1)
grade_names = gradsnames(course)
println(grade_names) # Example output: ["Quiz1", "Midterm", "FinalExam"]CourseDB.readdata — Methodreaddata(filepath::String, args...; kwargs...)::FileDataReads data from a file and returns a FileData object, which includes the file path, a dictionary of field types, and the data as a DataFrame.
Arguments
filepath::String: The path to the file to be read. Supported file formats are.csv,.txt, and.xlsx.args...: Positional arguments passed to the file reading functions (XLSX.readtablefor.xlsxfiles).kwargs...: Keyword arguments passed to the file reading functions (CSV.Filefor.csvand.txtfiles,XLSX.readtablefor.xlsx).
Returns
- A
FileDataobject containing:filepath: The path of the file.fields: A dictionary where the keys are the column names and the values indicate the data type ("number"for numeric fields and"string"for other types).data: The content of the file as aDataFrame.
Errors
- Throws an
AssertionErrorif the file does not exist or if the file extension is not supported (csv,txt,xlsx).
Example
file_data = readdata("data.csv", delim=',', header=true)
println(file_data.fields) # Dict with column names and their types
println(file_data.data) # DataFrame with the file contentCourseDB.writedata — Methodwritedata(filepath::String, students::Vector{Student}, args...; kwargs...) -> Result
writedata(filepath::String, grades::Vector{Grade}, args...; kwargs...) -> ResultWrites data to a specified file in tabular format using either XLSX.writetable for .xlsx files or CSV.write for .csv files. The input data can either be a vector of Student objects or a vector of Grade objects.
Arguments
filepath::String: The path of the file where the data will be written. The file extension determines whether the data is written in CSV or XLSX format:.csvfiles are written usingCSV.write..xlsxfiles are written usingXLSX.writetable.
students::Vector{Student}: A vector ofStudentobjects to be written to the file (used in the first method).grades::Vector{Grade}: A vector ofGradeobjects to be written to the file (used in the second method).args...: Additional positional arguments passed to the underlying write operation.kwargs...: Optional keyword arguments passed to the underlying write operation.
Returns
Result: AResultobject containing the data that was written (Vector{Student}orVector{Grade}) and a message describing the outcome.
Methods
writedata(filepath::String, students::Vector{Student}, args...; kwargs...): Converts the student data to aDataFramewith columnsid,name, andemail, and writes it to a.csvor.xlsxfile usingCSV.writeorXLSX.writetable, respectively.writedata(filepath::String, grades::Vector{Grade}, args...; kwargs...): Converts the grade data to aDataFramewith columnsstudent_id,student_name,student_email,course_code,course_name,grade,value, andmax_value, and writes it to a.csvor.xlsxfile usingCSV.writeorXLSX.writetable, respectively.
Example
students = [Student(123, "John Doe", "john@example.com"), Student(456, "Jane Doe", "jane@example.com")]
res = writedata("students.csv", students)
println(res.message) # Prints the result message after writing the data using CSV.write
grades = [Grade(123, "John Doe", "john@example.com", 1, "MATH371", "Numerical Computing", "Midterm", 85.0, 100.0)]
res = writedata("grades.xlsx", grades)
println(res.message) # Prints the result message after writing the data using XLSX.writetable