diff --git a/application/db_connect.py b/application/db_connect.py index 2682fb1..6cb23f8 100644 --- a/application/db_connect.py +++ b/application/db_connect.py @@ -150,9 +150,20 @@ def insert_db(query, args=()): db.commit() -################# -# STUDENT FUNCS # -################# +def get_class_name(class_id): + """Get name of class""" + class_name = query_db( + "SELECT name FROM classes WHERE class_id=?;", [class_id], one=True + ) + return str(class_name[0]) if class_name else None + + +def get_quiz_name(quiz_id): + """Get name of quiz""" + quiz_name = query_db( + "SELECT name FROM quizzes WHERE quiz_id=?;", [quiz_id], one=True + ) + return str(quiz_name[0]) if quiz_name else None def validate_student(func): @@ -188,7 +199,7 @@ def get_student_classes(): return classes -def get_student_grade(class_id): +def get_student_grades(class_id): """Gets all the grades given to the student""" grades = [] quiz_grade = query_db( diff --git a/application/students.py b/application/students.py index 61d2d73..52a8ca2 100644 --- a/application/students.py +++ b/application/students.py @@ -15,40 +15,36 @@ def student_home(): ) -@app.route("/students/classes/") -@db.validate_student -def student_classes_home(): - """Student classes page""" - return flask.render_template( - "/students/classes.html", classes=db.get_student_classes() - ) - - -@app.route("/students/classes/join/", methods=["POST"]) +@app.route("/students/classes/join/", methods=["GET", "POST"]) @db.validate_student def student_class_join(): """If student joins a class""" - db.insert_db( - "INSERT INTO roster (person_id, class_id) VALUES (?, ?);", - [flask.session["id"], flask.request.form["id"]], - ) - flask.flash(f"You joined the class with an id of {flask.request.form['id']}") - return flask.redirect("/students/classes") + if flask.request.method == "GET": + return flask.render_template("/students/join.html") + + class_id = flask.request.form["id"] + class_name = db.get_class_name(class_id) + if class_name: + db.insert_db( + "INSERT INTO roster (person_id, class_id) VALUES (?, ?);", + [flask.session["id"], class_id], + ) + flask.flash(f"You joined {class_name}") + return flask.redirect(f"/students/classes/{class_id}/") + flask.flash(f"No class exists with id {class_id}!") + return flask.redirect(f"/students/") @app.route("/students/classes//") @db.validate_student def student_class_page(class_id): """Show classes""" - class_name = db.query_db( - "SELECT name FROM classes WHERE class_id=?;", [class_id], one=True - ) return flask.render_template( "/students/class_page.html", class_id=class_id, - class_name=str(class_name[0]), + class_name=db.get_class_name(class_id), quizzes=db.get_class_quizzes(class_id), - grades=db.get_student_grade(class_id), + grades=db.get_student_grades(class_id), ) @@ -56,12 +52,7 @@ def student_class_page(class_id): @db.validate_student def student_quiz_page(class_id, quiz_id): """Allows students to view/take quizzes""" - quiz_name = str( - db.query_db( - "SELECT name FROM quizzes WHERE quiz_id=? AND class_id=?;", - [quiz_id, class_id], - )[0][0] - ) + quiz_name = db.get_quiz_name(quiz_id) result = db.query_db( "SELECT grade from quiz_grades WHERE quiz_id=? AND student_id=?;", [quiz_id, flask.session["id"]], @@ -81,7 +72,7 @@ def student_quiz_page(class_id, quiz_id): question_info["type"] = question[1] question_info["text"] = question[2] # if this is a multiple-choice question - if question[0] == 1: + if question[1] == 1: question_info["answers"] = {} question_info["answers"]["a"] = question[3] question_info["answers"]["b"] = question[4] @@ -94,10 +85,10 @@ def student_quiz_page(class_id, quiz_id): quiz_name=quiz_name, quiz_id=quiz_id, class_id=class_id, + class_name=db.get_class_name(class_id), ) - flask.flash(f"You receieved {result[0]} on this quiz.") - return flask.render_template("/students/quiz_page.html", quiz_name=quiz_name) + return flask.redirect(f"/students/classes/{class_id}/quizzes/{quiz_id}/grade/") @app.route( @@ -108,12 +99,6 @@ def student_grade_quiz(class_id, quiz_id): """Show or calculate a student's grade on a quiz""" # display grades if flask.request.method == "GET": - quiz_name = str( - db.query_db( - "SELECT name FROM quizzes WHERE quiz_id=? AND class_id=?;", - [quiz_id, class_id], - )[0][0] - ) result = db.query_db( "SELECT grade from quiz_grades WHERE quiz_id=? AND student_id=?;", [quiz_id, flask.session["id"]], @@ -121,7 +106,11 @@ def student_grade_quiz(class_id, quiz_id): ) return flask.render_template( - "/students/quiz_grade.html", quiz_name=quiz_name, grade=result[0] + "/students/quiz_grade.html", + class_id=class_id, + class_name=db.get_class_name(class_id), + quiz_name=db.get_quiz_name(quiz_id), + grade=result[0], ) # process quiz form from POST request diff --git a/application/templates/students/base_template.html b/application/templates/students/base_template.html index b465f24..d7898fe 100644 --- a/application/templates/students/base_template.html +++ b/application/templates/students/base_template.html @@ -21,12 +21,11 @@ - +
diff --git a/application/templates/students/classes.html b/application/templates/students/classes.html deleted file mode 100644 index 2bd96eb..0000000 --- a/application/templates/students/classes.html +++ /dev/null @@ -1,43 +0,0 @@ - -{% extends "/students/base_template.html" %} - {% block title %}Your Classes{% endblock %} - {% block body %} - -
-
-
-

Classes

-
-
-
-
-

Your Classes

- -
-
-

Join a Class

-
-
-
Class ID:
-
-
-
-
-
-
- {% endblock %} diff --git a/application/templates/students/index.html b/application/templates/students/index.html index 4c9d50f..788a2e3 100644 --- a/application/templates/students/index.html +++ b/application/templates/students/index.html @@ -8,6 +8,12 @@ list-style-type: none; margin: 0; } + div.button { + margin: 2px; + } + .inline { + display: inline-block; + }
@@ -17,7 +23,12 @@

Dashboard Home

-

Your Classes

+

Classes

+
+
+ +
+
    {% for class in classes %}
  • {{class.name}}
  • diff --git a/application/templates/students/join.html b/application/templates/students/join.html new file mode 100644 index 0000000..e483cdf --- /dev/null +++ b/application/templates/students/join.html @@ -0,0 +1,32 @@ + +{% extends "/students/base_template.html" %} + {% block title %}Your Classes{% endblock %} + {% block body %} + +
    +
    +
    +

    Join a Class

    +
    +
    +
    +
    +
    +
    + + + +
    +
    +
    +
    +
    + {% endblock %} diff --git a/application/templates/students/quiz_grade.html b/application/templates/students/quiz_grade.html new file mode 100644 index 0000000..6fde2ac --- /dev/null +++ b/application/templates/students/quiz_grade.html @@ -0,0 +1,24 @@ + +{% extends "/students/base_template.html" %} + {% block title %}{{class_name}}: {{quiz_name}}{% endblock %} + {% block body %} + +
    +
    +
    +

    {{class_name}}: {{quiz_name}}

    +
    +
    +
    +
    +

    Grade Received

    + {{grade}} +
    +
    +
    + {% endblock %} diff --git a/application/templates/teachers/base_template.html b/application/templates/teachers/base_template.html index a8d889a..6b19b7b 100644 --- a/application/templates/teachers/base_template.html +++ b/application/templates/teachers/base_template.html @@ -1,11 +1,4 @@ -{% set navigation_items = [ - ('/teachers/classes/', 'classes', 'Classes'), - ('/teachers/quiz_page/', 'quiz_page', 'Quizzes'), - ('/logout/', 'logout', 'Logout'), -] -%} - -{% set active_page = active_page -%} @@ -30,10 +23,10 @@ +
    diff --git a/application/templates/teachers/classes/class_page.html b/application/templates/teachers/classes/class_page.html index 8c58839..a971ba8 100644 --- a/application/templates/teachers/classes/class_page.html +++ b/application/templates/teachers/classes/class_page.html @@ -10,7 +10,7 @@ div.button { margin: 2px; } - div.inline { + .inline { display: inline-block; } @@ -22,14 +22,13 @@

    {{class_name}}

-

- Quizzes -
-
- -
-
-

+

Quizzes

+
+
+ +
+
+
    {% for quiz in quizzes %}
  • diff --git a/application/templates/teachers/index.html b/application/templates/teachers/index.html index da7db6a..6dc4f25 100644 --- a/application/templates/teachers/index.html +++ b/application/templates/teachers/index.html @@ -10,7 +10,7 @@ div.button { margin: 2px; } - div.inline { + .inline { display: inline-block; } @@ -22,14 +22,13 @@

    Dashboard Home

-

- Classes -
-
- -
-
-

+

Classes

+
+
+ +
+
+
    {% for class in classes %}
  • {{class.name}}
  • diff --git a/run.py b/run.py index c221222..a0ac947 100644 --- a/run.py +++ b/run.py @@ -1,6 +1,7 @@ """Run this file to run the development server""" import argparse +import os from application import appfactory @@ -32,7 +33,24 @@ help="host to serve from (default: 0.0.0.0)", ) +parser.add_argument( + "--clear", + dest="clear", + default=False, + action="store_true", + help="clear the database", +) + args = parser.parse_args() -app.debug = args.debug -app.run(args.host, args.port) +if args.clear: + if os.path.isfile(app.config["DATABASE"]): + os.remove(app.config["DATABASE"]) + with app.app_context(): + from application import db_connect + + db_connect.db_init() + print("Reinitialized database!") +else: + app.debug = args.debug + app.run(args.host, args.port)