Purpose of your groups program

Our team created a platform that allows us to prepare and teach CSP students about Big Idea 2 (about Binary). This is a key part of the CSP course and the different features of our program will test your knowledge and help you prepare for the upcoming AP exam as well as do great in this class.

Purpose of your individual feature

My individual feature is a multiple choice quiz that tests your knowledge on Binary code! This quiz will help you understand what you do or dont know and will save your attempts and scores.

  • Randomized quiz questions order
  • 5 questions at a time
  • Save the times you take the quiz and the amount of correct answers
  • green and red answers to alert if you got it correct or incorrect
  • unlimited amount of tries

Input/Output requests

1) Fontnend input

  • First, The user loads the quiz page and takes the quiz
  • Then they complete the quiz and hit submit
  • The attempt and quizgrade recieved are sent to the backend via a POST request to the /api/quizgrading endpoint.
  • The attempt is displayed in the table, and the a new attempt can be tried

2) API Output and response

  • A GET request is sent to fetch all the quiz attempt entries stored in the database.
  • The results are displayed on the webpage under the Quiz Attempt History section. The response is formatted as an array of objects containing the quiz id, attempt time, and quizgrade.

3) Postman with raw API request and restful response

  • Postman displays the API request to get the quiz attempts and grades stores wihin the API

4) Using db_init, db_restore, db_backup to show tester data creation and data recovery

  • db_init db_backup:

    Database Initialization (db_init) The db_init function initializes the database by creating required tables and setting up the initial schema. We use this function when starting a new project or resetting an existing database to its default state.

    Command to Run Initialization: ./scripts/db_init.py def init(self, quizgrade, attempt): self._quizgrade = quizgrade self._attempt = attempt

  • db_backup Database Backup (db_backup) The db_backup function allows you to safely back up the current state of your database. This ensures that your data is preserved and can be restored later in case of accidental deletion or other issues.

    Command to Run Backup: ./scripts/db_backup.py Database Restore (db_restore) The db_restore function allows you to revert your database to a previously saved state by restoring data from a backup file.

  • db_restore, Command to Run Restore: ./scripts/db_restore.py

List requests

1) In your API, lists represent rows in a database table, and dictionaries represent the columns. Each record (or row) corresponds to a quiz attempt, and its attributes (like attempt time and quizgrade) are represented as key-value pairs in a dictionary.

List Requests: When you make a request to the endpoint /api/quizgrading, it returns a list of all quiz attempts in JSON format. This list consists of dictionaries where each dictionary represents a single quiz attempt with keys as column names (attempt, quizgrade).

In the QuizGradingAPI class, the get() method retrieves all quiz records:

  • quizgrading.query.all(): This method retrieves all quiz records from the database as a list (rows).
  • appending all chats: Each quiz attempt object is converted into a dictionary representing its attributes (columns).

2) Formatting Response Data (JSON) When the API responds to a request, it formats the data in JSON, which is suitable for communication between a server and a client:

This JSON structure makes it easy to manipulate and display data in the frontend, where each quiz attempt can be rendered in the DOM using JavaScript.

3) The Quizgrading API supports the following requests:

  • GET /api/quizgrading: Retrieve quiz scores and attempts
  • POST /api/quizgrading: Add a new quiz score and attempt.
  • PUT /api/quizgrading: Update an existing quiz score and attempt entry.
  • DELETE /api/quizgrading: Delete a specific quiz attempt and score entry.

4) Use of Lists and Dictionaries In the API, we use lists to handle multiple quiz score and attempt entries to represent individual entries. The database rows are converted to dictionaries for JSON responses.

Converting database rows to list of dictionaries:

entries = QuizQradingEntry.query.order_by(QuizGrading.score.desc()).all()
entries_list = [entry.read() for entry in entries]

5) Formatting Response Data (JSON) from API into DOM We use Flask’s jsonify function to format the response data as JSON. In the frontend, we use JavaScript to fetch the JSON data from the API and update the DOM. This involves converting the JSON response into HTML elements to display the quiz scores and attempts.

Example: - @quizgrading_api.route('/api/quizgrading', methods=['GET']) def get_quizgrading():
entries = quizgrading.query.order_by(quizgrading.score.desc()).all()
return jsonify([entry.read() for entry in entries]), 200

4) Database Queries We use SQLAlchemy ORM to interact with the database. SQLAlchemy provides methods to query the database and return results as Python lists. For example, we can retrieve all quiz entries sorted by quizgrade or attempt.

Example:

- Get all entries sorted by score entries = quizgrading.query.order_by(quizgrading.quizgrade.desc()).all()

- Filter by profile name entry = quizgrading.query.filter_by(attempt=attempt).first()

CRUD Methods in Class

We define methods in the QuizGrading class to perform CRUD operations on the database: Create: Adds a new entry to the database. Read: Converts a database entry to a dictionary. Update: Updates entry fields with new data. Delete: Removes an entry from the database.

Algorithmic Code Request

1) We define API endpoints to handle different types of requests. In “api/quizgrading.py”, I define a class “QuizgradingAPI” which handles different HTTP methods for managing books in my database. This class utilizes the Flask-RESTful framework, allowing me to structure the API cleanly.

  1. Discussion of a Method/Procedure in the Class The “post” method in the “QuizGradingAPI” class illustrates sequencing, selection, and iteration:

Sequencing: The method executes a sequence of actions to process a quiz attempt creation request: fetching the JSON data, validating the input, checking for duplicates, and attempting to create a new quiz attempt. Selection: The method uses conditional statements (if-else) to check for required fields, handle existing quiz attempts, and catch exceptions during quiz attempt creation. Iteration: While there’s no explicit iteration in this method, the principles can be seen in similar contexts, such as processing multiple quiz attempt entries when restoring data.

  1. Parameters and Return Type Parameters: The “post” method expects a JSON object in the request body containing the quiz attempt. If the request is malformed (missing title), it returns a 400 status code with an error message.

Return Type: The responses are formatted as JSON using “jsonify”, providing clear and structured data for the client.

    return jsonify({"message": "New entry created"}), 201
    return jsonify({"error": "Failed to create entry"}), 400

4) Call to Algorithm Request In the frontend, we use the fetch API to make requests to the backend. For example, to submit an attempt, we send a PUT request with the quizgrade and attempt. The response is handled by checking the status and updating the DOM accordingly. If the request is successful, we update the quiz attempt table; if there’s an error, we display an error message.

  • Return/Response from Method with Algorithm The “fetch” method captures the response from the server:

The code checks the HTTP response status to determine if the request was successful. If not, it extracts the error message from the response and displays it.

Handling Data and Changing responses

My application is designed to manage editing and deleting incorrect quiz attempts.

This will allow the user to edit their score or attempt number to fix any errors or repeats and to make it more visually pleasing by chaing the attempt time to just the attempt number.

This allos the user to delete a quiz attempt from the table if they wish to retry or are embarssed of a score.

Feature on the page