PaferaPy Async 0.1
ASGI framework focused on simplicity and efficiency
Loading...
Searching...
No Matches
answer.py
Go to the documentation of this file.
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4import time
5import datetime
6from pprint import pprint
7
8from pafera.utils import *
9from pafera.validators import *
10
11import pafera.db
13
15
16# Flag constants
17ANSWER_LOCKED = 0x01
18ANSWER_ANSWERED = 0x02
19ANSWER_ENABLE_COPY_PASTE = 0x04
20ANSWER_TEACHER = 0x08
21
22# *********************************************************************
24 """This class stores submitted student answers to show in the
25 classroom and keeps track of their scores. It has facilities for
26 showing different prompts, putting students into groups, and
27 purchasing helpful items.
28 """
29
30 _dbfields = {
31 'rid': ('INTEGER PRIMARY KEY', 'NOT NULL',),
32 'classid': ('INT', 'NOT NULL DEFAULT 0',),
33 'userid': ('INT', 'NOT NULL', BlankValidator()),
34 'numright': ('INT16', 'NOT NULL DEFAULT 0',),
35 'numwrong': ('INT16', 'NOT NULL DEFAULT 0',),
36 'numstrikes': ('INT16', 'NOT NULL DEFAULT 0',),
37 'scoreadded': ('INT16', 'NOT NULL DEFAULT 0',),
38 'groupnum': ('INT16', 'NOT NULL DEFAULT 0',),
39 'bonusscore': ('INT16', 'NOT NULL DEFAULT 0',),
40 'score': ('INT', 'NOT NULL DEFAULT 0',),
41 'displayname': ('TRANSLATION', 'NOT NULL', BlankValidator()),
42 'answer': ('MULTITEXT', "NOT NULL DEFAULT ''",),
43 'prompt': ('TEXT', "NOT NULL DEFAULT ''",),
44 'promptdata': ('DICT', "NOT NULL DEFAULT ''",),
45 'items': ('DICT', "NOT NULL DEFAULT ''",),
46 'coderesult': ('DICT', "NOT NULL DEFAULT ''",),
47 'choices': ('DICT', "NOT NULL DEFAULT ''",),
48 'flags': ('INT', 'NOT NULL DEFAULT 0',),
49 }
50 _dbindexes = ()
51 _dblinks = []
52 _dbdisplay = ['classid', 'displayname', 'userid', 'numright', 'numwrong', 'numstrikes', 'answer']
53 _dbflags = 0
54
55 # -------------------------------------------------------------------
56 def __init__(self):
57 super().__init__()
58
59 # -------------------------------------------------------------------
60 def UpdateScore(self):
61 """Recalculates the current score. Be sure to call db.Commit()
62 after you finish all calculations, or the updates won't be saved
63 into the database.
64 """
65 newscore = self.numright - self.numwrong + self.bonusscore
66
67 if self.numstrikes:
68 newscore -= 2 ** self.numstrikes
69
70 self.Set(
71 scoreadded = newscore - self.score,
72 score = newscore,
73 )
74
75 # -------------------------------------------------------------------
76 def AddHomeworkScores(self, g):
77 """Adds up all of the homework scores from the last time that
78 class was in session and applies them to the current answer.
79 This is automatically called by /learn/answerapi when a new
80 answer is created.
81 """
82 currenttime = time.time()
83
84 useridcode = ToShortCode(self.userid)
85 homeworkscore = 0
86
87 for r in g.db.Find(
88 apps.learn.challenge.learn_challenge,
89 'WHERE classid = ? AND endtime < ?',
90 [
91 self.classid,
92 currenttime,
93 ],
94 fields = 'endtime, challengetype, results',
95 orderby = 'endtime DESC',
96 ):
97 if (r.challengetype == apps.learn.challenge.CHALLENGE_CLASSPARTICIPATION
98 and r.results
99 and useridcode in r.results
100 ):
101 break
102
103 if r.results and useridcode in r.results:
104 homeworkscore += r.results[useridcode][4]
105
106 self.Set(
107 bonusscore = self.bonusscore + homeworkscore,
108 )
109
110 self.UpdateScore()
111
112
This class stores submitted student answers to show in the classroom and keeps track of their scores.
Definition: answer.py:23
def UpdateScore(self)
Recalculates the current score.
Definition: answer.py:60
def __init__(self)
Initialize all fields at creation like a good programmer should.
Definition: answer.py:56
def AddHomeworkScores(self, g)
Adds up all of the homework scores from the last time that class was in session and applies them to t...
Definition: answer.py:76
Base class for all database models.
Definition: modelbase.py:20
def Set(self, **kwargs)
We use this method instead of direct attribute access in order to keep track of what values have been...
Definition: modelbase.py:115
Throws an exception on blank values.
Definition: validators.py:43
Definition: db.py:1
def ToShortCode(val, chars='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_')
Turns a 32-bit value into a six character alphanumeric code.
Definition: utils.py:36