summaryrefslogtreecommitdiff
path: root/worker/CanvasWorker.py
blob: 0f785cadf41c13c6b5c6826d4f0a0b4edb8da798 (plain)
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
# from canvasapi import Canvas
from util import first, Logger
# import json


class CanvasWorker():
    def __init__(self,
                 args,
                 rubric,
                 canvasToken,
                 courseID,
                 names,
                 indvScores,
                 groupScores,
                 jojScores,
                 totalScores=None,
                 logger=Logger()):
        self.args = args
        self.rubric = rubric
        """
        self.canvas = Canvas("https://umjicanvas.com/", canvasToken)
        self.course = self.canvas.get_course(courseID)
        self.users = self.course.get_users()
        self.assignments = self.course.get_assignments()
        """
        self.logger = logger
        self.scores = {}
        if totalScores is None:
            self.names = names
            for key in names:
                self.scores[key] = {
                    **indvScores.get(key, {}),
                    **groupScores.get(key, {}),
                    **jojScores.get(key, {})
                }
        else:
            self.scores = totalScores

    def generateHomeworkData(self, scoreInfo):
        score = 0
        comment = []
        for key, value in self.rubric.items():
            for _ in range(scoreInfo.get(key, 0)):
                score += value[0]
                comment.append(f"{value[1]}, {value[0]}")
        if not comment:
            comment = ['good job']
        else:
            comment.insert(0, "Genral Info:")
            comment.append("")
            comment.append("Detail:")
            comment.extend(
                scoreInfo.get("indvComment", []) +
                scoreInfo.get("groupComment", []) +
                scoreInfo.get("jojComment", []))
        return {
            'submission': {
                'posted_grade': max(score, -2.5)
            },
            'comment': {
                'text_comment': '\n'.join(comment)
            },
        }
    """
    def grade2Canvas(self):
        hwNum = self.args.hw
        assignment = first(self.assignments,
                           lambda x: x.name.startswith(f"h{hwNum}"))
        for submission in assignment.get_submissions():
            currentUser = first(self.users,
                                lambda user: user.id == submission.user_id)
            if currentUser is None: continue
            name = currentUser.name.strip()
            if name not in self.names: continue
            if not self.scores[name]: continue
            data = self.generateHomeworkData(self.scores[name])
            self.logger.debug(f"{name} {data.__repr__()}")
            submission.edit(**data)

    def exportScores(self, fileName):
        json.dump(self.scores,
                  open(fileName, "w"),
                  ensure_ascii=False,
                  indent=4)
        self.logger.debug(f"score dump to {fileName} succeed")
    """