File size: 4,445 Bytes
5957456
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import json
from datetime import datetime, timezone
import pytz

class DashboardService:
    def __init__(self, assignment_service, submission_service):
        self.assignment_service = assignment_service
        self.submission_service = submission_service

    def get_dashboard_data(self, user_id):
        assignments = self.assignment_service.get_user_assignments(user_id)
        dashboard_data = []

        for assignment_id, assignment_info in assignments.items():
            assignment_data = self.assignment_service.get_assignment(assignment_id)
            submission_ids = assignment_data.get("submission_ids", [])
            submission_count = len(submission_ids)

            dashboard_item = {
                "assignment_id": assignment_id,
                "topic": assignment_data["metadata"]["topic"],
                "assignment_type": assignment_data["assignment_type"],
                "submission_count": submission_count,
                "creation_date": assignment_data["timestamp"],
                "deadline": assignment_data["metadata"].get("submission_deadline")
            }
            dashboard_data.append(dashboard_item)

        # 按創建日期降序排序
        dashboard_data.sort(key=lambda x: x["creation_date"], reverse=True)
        return dashboard_data

    def get_assignment_details(self, assignment_id):
        assignment_data = self.assignment_service.get_assignment(assignment_id)
        submission_ids = assignment_data.get("submission_ids", [])
        submissions = []

        for submission_id in submission_ids:
            submission_data = self.submission_service.get_submission_from_gcs(submission_id)
            if submission_data:
                submissions.append({
                    "submission_id": submission_id,
                    "student_id": submission_data["student_id"],
                    "student_name": submission_data["student_name"],
                    "submission_date": submission_data["timestamp"]
                })

        return {
            "assignment_data": assignment_data,
            "submissions": submissions
        }

    def get_submission_details(self, submission_id):
        submission_data = self.submission_service.get_submission_from_gcs(submission_id)
        if not submission_data:
            return {"error": "找不到提交記錄"}

        assignment_data = self.assignment_service.get_assignment(submission_data["assignment_id"])
        return {
            "submission_data": submission_data,
            "assignment_data": assignment_data
        }

    def get_all_submissions(self, user_id):
        assignments = self.assignment_service.get_user_assignments(user_id)
        all_submissions = []

        for assignment_id, assignment_info in assignments.items():
            assignment_data = self.assignment_service.get_assignment(assignment_id)
            submission_ids = assignment_data.get("submission_ids", [])

            for submission_id in submission_ids:
                submission_data = self.submission_service.get_submission_from_gcs(submission_id)
                if submission_data:
                    score = self.extract_score(submission_data)
                    all_submissions.append({
                        "作業名稱": assignment_data["metadata"]["topic"],
                        "作業類型": assignment_data["assignment_type"],
                        "學生_id": submission_data["student_id"],
                        "學生姓名": submission_data["student_name"],
                        "繳交日期": submission_data["timestamp"],
                        "分數": score
                    })

        all_submissions.sort(key=lambda x: x["繳交日期"], reverse=True)
        return all_submissions

    def extract_score(self, submission_data):
        try:
            content = submission_data.get("submission_data", {}).get("content", {})
            if isinstance(content, str):
                content = json.loads(content)
            
            output_table = content.get("chinese_full_paragraph_evaluate_output_table", [])
            
            for item in output_table:
                if item.get("架構") == "綜合評分":
                    return item.get("評分", "N/A")
        except Exception as e:
            print(f"提取分數時出錯:{str(e)}")
            print(f"提交數據:{submission_data}")
        
        return "N/A"