File size: 2,928 Bytes
e74c738
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import streamlit as st
from supabase import create_client, Client
import os
import json
from datetime import datetime

def init_supabase():
    """Initialize Supabase client"""
    try:
        url = st.secrets.get("SUPABASE_URL")
        key = st.secrets.get("SUPABASE_KEY")
    except:
        url = os.getenv("SUPABASE_URL")
        key = os.getenv("SUPABASE_KEY")
    
    if not url or not key:
        st.error("Supabase credentials not found.")
        return None
    
    return create_client(url, key)

def save_submission(username, week_number, submission_type, submission_data, score=None, max_score=None):
    """Save a student submission to the database"""
    supabase = init_supabase()
    if not supabase:
        return False
    
    try:
        data = {
            'username': username,
            'week_number': week_number,
            'submission_type': submission_type,
            'submission_data': submission_data,
            'score': score,
            'max_score': max_score
        }
        
        response = supabase.table('student_submissions').insert(data).execute()
        return True
    except Exception as e:
        st.error(f"Error saving submission: {str(e)}")
        return False

def get_existing_submission(username, week_number, submission_type):
    """Get the most recent submission for a student"""
    supabase = init_supabase()
    if not supabase:
        return None
    
    try:
        response = supabase.table('student_submissions').select('*').eq('username', username).eq('week_number', week_number).eq('submission_type', submission_type).order('submitted_at', desc=True).limit(1).execute()
        if response.data:
            return response.data[0]
        return None
    except Exception as e:
        st.error(f"Error retrieving submission: {str(e)}")
        return None

def get_all_submissions(username, week_number, submission_type):
    """Get all submissions for a student (for history)"""
    supabase = init_supabase()
    if not supabase:
        return []

    try:
        response = supabase.table('student_submissions').select('*').eq('username', username).eq('week_number', week_number).eq('submission_type', submission_type).order('submitted_at', desc=True).execute()
        return response.data if response.data else []
    except Exception as e:
        st.error(f"Error retrieving submissions: {str(e)}")
        return []

def get_student_debate_case(username):
    """Get the student's debate case Google Doc link"""
    supabase = init_supabase()
    if not supabase:
        return None

    try:
        response = supabase.table('student_debate_case').select('*').eq('student_id', username).order('created_at', desc=True).limit(1).execute()
        if response.data:
            return response.data[0]
        return None
    except Exception as e:
        st.error(f"Error retrieving debate case: {str(e)}")
        return None