Skip to content

Commit d1be821

Browse files
committed
Map authenticated students to stable ids
1 parent 5055428 commit d1be821

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

feedback_agent/conversational_tools.py

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def check_authorization(
5555
return False, "Not authenticated. Please tell me your name and role first."
5656

5757
current_role = tool_context.state.get("current_user_role")
58-
current_user_id = tool_context.state.get("current_user_id")
58+
current_student_id = tool_context.state.get("current_student_id")
5959

6060
# No specific role required - any authenticated user is allowed
6161
if required_role is None:
@@ -64,7 +64,7 @@ def check_authorization(
6464
# Check teacher role
6565
if required_role == "teacher" and current_role != "teacher":
6666
# But allow students to access their own data if enabled
67-
if allow_own_data and target_student_id and current_user_id == target_student_id:
67+
if allow_own_data and target_student_id and current_student_id == target_student_id:
6868
return True, ""
6969
return False, "This action requires teacher privileges."
7070

@@ -321,6 +321,19 @@ def authenticate_user(
321321

322322
logger.info(f"Authenticated user: {name} ({role_lower}) - ID: {user_id}")
323323

324+
# For students, map to a stable student_id
325+
if role_lower == "student":
326+
try:
327+
from feedback_agent.agent import get_feedback_system
328+
329+
system = get_feedback_system()
330+
student = system.db.get_student_by_name(name)
331+
student_id = student["student_id"] if student else system.register_student(name)
332+
tool_context.state["current_student_id"] = student_id
333+
except Exception as e:
334+
logger.error(f"Failed to map student id for {name}: {e}")
335+
tool_context.state["current_student_id"] = None
336+
324337
# Return capabilities based on role
325338
if role_lower == "teacher":
326339
capabilities = [
@@ -592,6 +605,7 @@ def get_my_results(tool_context: ToolContext) -> Dict[str, Any]:
592605

593606
user_name = tool_context.state.get("current_user_name")
594607
user_role = tool_context.state.get("current_user_role")
608+
student_id = tool_context.state.get("current_student_id")
595609

596610
logger.info(f"get_my_results called by {user_name} ({user_role})")
597611

@@ -601,21 +615,17 @@ def get_my_results(tool_context: ToolContext) -> Dict[str, Any]:
601615
system = get_feedback_system()
602616
db = system.db
603617

604-
# Find student by name (simplified - in production would use proper user mapping)
605-
all_students = db.get_all_students()
606-
student = next(
607-
(s for s in all_students if s["name"].lower() == user_name.lower()),
608-
None,
609-
)
618+
if not student_id:
619+
# Fallback to name lookup if student_id not mapped
620+
student = db.get_student_by_name(user_name) if user_name else None
621+
student_id = student["student_id"] if student else None
610622

611-
if not student:
623+
if not student_id:
612624
return {
613625
"status": "success",
614626
"message": f"No exam records found for {user_name}.",
615627
"exams": [],
616628
}
617-
618-
student_id = student["student_id"]
619629
exams = db.get_student_exams(student_id)
620630

621631
if not exams:
@@ -700,11 +710,7 @@ def get_student_results(
700710
if student_id:
701711
student = db.get_student(student_id)
702712
else:
703-
all_students = db.get_all_students()
704-
student = next(
705-
(s for s in all_students if student_name.lower() in s["name"].lower()),
706-
None,
707-
)
713+
student = db.get_student_by_name(student_name) if student_name else None
708714

709715
if not student:
710716
return {
@@ -929,6 +935,7 @@ def list_students(tool_context: ToolContext) -> Dict[str, Any]:
929935
def get_learning_recommendations(
930936
tool_context: ToolContext,
931937
student_name: Optional[str] = None,
938+
student_id: Optional[str] = None,
932939
) -> Dict[str, Any]:
933940
"""
934941
Get personalized learning recommendations.
@@ -939,6 +946,7 @@ def get_learning_recommendations(
939946
Args:
940947
tool_context: ADK tool context with session state
941948
student_name: Name of student (teachers can specify, students use own)
949+
student_id: Student ID (preferred for teachers)
942950
943951
Returns:
944952
Dictionary with learning recommendations
@@ -950,11 +958,13 @@ def get_learning_recommendations(
950958

951959
current_role = tool_context.state.get("current_user_role")
952960
current_name = tool_context.state.get("current_user_name")
961+
current_student_id = tool_context.state.get("current_student_id")
953962

954963
# Students can only get their own recommendations
955964
if current_role == "student":
956965
student_name = current_name
957-
elif not student_name:
966+
student_id = current_student_id
967+
elif not student_name and not student_id:
958968
return {
959969
"status": "error",
960970
"message": "Please specify which student to get recommendations for.",
@@ -969,19 +979,14 @@ def get_learning_recommendations(
969979
db = system.db
970980

971981
# Find student
972-
all_students = db.get_all_students()
973-
student = next(
974-
(s for s in all_students if student_name.lower() in s["name"].lower()),
975-
None,
976-
)
977-
978-
if not student:
979-
return {
980-
"status": "error",
981-
"message": f"Student not found: {student_name}",
982-
}
983-
984-
student_id = student["student_id"]
982+
if not student_id:
983+
student = db.get_student_by_name(student_name) if student_name else None
984+
if not student:
985+
return {
986+
"status": "error",
987+
"message": f"Student not found: {student_name}",
988+
}
989+
student_id = student["student_id"]
985990

986991
# Get latest exam with recommendations
987992
exams = db.get_student_exams(student_id)

0 commit comments

Comments
 (0)