Skip to the content.

PPR Blog

πŸš€ AP CSP Personalized Project Reference (PPR) FRQ

Below are my code segments that meet the AP CSP requirements.

πŸ“Œ Summary

βœ” List Creation: Initializes and stores student grade data.
βœ” List Processing: Retrieves and processes stored grade data.
βœ” Function with Sequencing, Selection, and Iteration: A function that iterates over a list, applies conditional logic, and processes data.
βœ” Call to Function: A function call that modifies a user’s grade log dynamically.

πŸ“‚ List Creation

This segment initializes student grade logs and creates a list of entries.

entry1 = GradeLog(user_id=1, subject="Math", grade=95, notes="Test 1: Easy exam")
entry2 = GradeLog(user_id=2, subject="History", grade=88, notes="Project Presentation")
entry3 = GradeLog(user_id=3, subject="Science", grade=92, notes="Lab Report")

entry1.create()
entry2.create()
entry3.create()

πŸ“ Explanation: This code initializes multiple grade log entries and assigns them a subject, grade, and notes. The .create() method ensures that each entry is stored in the database.

πŸ”„ List Processing

This function retrieves all stored grade logs and processes their data.

def get(self):
    try:
        gradelogs = GradeLog.query.all()
        return jsonify([log.read() for log in gradelogs])
    except Exception as e:
        return {"message": f"An error occurred: {str(e)}"}, 500

πŸ“ Explanation: This function retrieves all grade logs from the database and converts them into JSON format using log.read(). This allows the grade logs to be accessed dynamically.

πŸ› οΈ Function with Sequencing, Selection & Iteration

This function modifies a student’s grade log by iterating over it and applying conditional logic.

def update(self, data):
    for key, value in data.items():
        if key == "subject":
            self.subject = value
        if key == "grade":
            self.grade = value
        if key == "notes":
            self.notes = value
    db.session.commit()
    return self

πŸ“Œ Explanation of Key Concepts:
βœ” Sequencing: The function follows a structured processβ€”iterating through the data, checking conditions, modifying the log, and committing changes to the database.
βœ” Selection: The if statements ensure only relevant fields are updated.
βœ” Iteration: The for loop iterates over the data fields dynamically.

πŸ“ž Call to Function

This function call updates a student’s grade log dynamically.

entry = GradeLog.query.get(data["id"])
if entry:
    entry.update(data)

πŸ“ Explanation: This retrieves a grade log by ID and updates it with new data. The function ensures only valid fields are modified.

πŸ” Queries

This snippet defines how the API retrieves grade logs by querying the database.

class GradelogAPI:
    class _CRUD(Resource):
        @token_required()
        def get(self):
            current_user = g.current_user
            all_gradelogs = GradeLog.query.filter_by(user_id=current_user.id).all()
            gradelog = [log.read() for log in all_gradelogs]
            return jsonify(gradelog)

πŸ“ Explanation: This function queries all grade logs for a user and converts them into a list of dictionaries before returning a JSON response.

⚑ CRUD Methods

These methods enable database manipulation for grade logs.

def create(self):
    try:
        db.session.add(self)
        db.session.commit()
        return self
    except IntegrityError:
        db.session.rollback()
        return None

def read(self):
    return {
        "id": self.id,
        "user_id": self.user_id,
        "subject": self.subject,
        "grade": self.grade,
        "notes": self.notes,
        "date": self.date.strftime('%Y-%m-%d %H:%M:%S')
    }

def update(self, data):
    for key, value in data.items():
        if key == "subject":
            self.subject = value
        if key == "grade":
            self.grade = value
        if key == "notes":
            self.notes = value
    db.session.commit()
    return self

def delete(self):
    db.session.delete(self)
    db.session.commit()

πŸ“Š Algorithmic Code Request

Methods: CRUD operations such as get(), update(), delete().

  • Sequencing: The method follows structured steps:
    1. Retrieve the authenticated user.
    2. Query the database for grade logs.
    3. Convert logs to a list of dictionaries.
    4. Return a JSON response.
  • Selection: Filtering by user_id ensures only authorized logs are retrieved.
  • Iteration: The for loop processes each grade log.

πŸ“ž Call to Algorithm Request

try {
      const response = await fetch('http://127.0.0.1:8887/api/gradelog', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(data),
        credentials: 'include',
      });

      if (response.ok) {
        alert('Grade Log added successfully!');
        gradeLogForm.reset();
        loadGradeLogs(); // Refresh logs
      } else {
        const errorText = await response.text();
        alert('Failed to add grade log: ' + errorText);
      }
    } catch (error) {
      console.error('Error:', error);
      alert('An error occurred while adding the grade log.');
    }

πŸ“ Explanation: This fetch request sends new grade log data to the backend, handles responses, and refreshes the grade log list dynamically.


This markdown document maintains the structured approach while effectively translating the original code into the AP CSP Personalized Project Reference format.