PaferaPy Async 0.1
ASGI framework focused on simplicity and efficiency
Loading...
Searching...
No Matches
problem.py
Go to the documentation of this file.
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4from pprint import pprint
5
6import pafera.db
8
9from pafera.utils import *
10
11from apps.learn.card import *
12
13# Flag constants
14PROBLEM_REQUEST_REVIEW = 0x01
15
16# *********************************************************************
18 """A problem in the Pafera Learning System is defined as a
19 collection of cards. The two required cards are a problem card and
20 an answer card, but cards can also be added to explain how to do the
21 problem or explicitly define choices for multiple choice challenges.
22
23 Like cards, a problem also has owners and editors, and will be
24 copied if someone does not have permission to change the original card.
25 """
26
27 _dbfields = {
28 'rid': ('INTEGER PRIMARY KEY', 'NOT NULL',),
29 'problemid': ('INT', 'NOT NULL'),
30 'answerid': ('INT', 'NOT NULL'),
31 'explanationid': ('INT', 'NOT NULL DEFAULT 0'),
32 'choice1id': ('INT', 'NOT NULL DEFAULT 0'),
33 'choice2id': ('INT', 'NOT NULL DEFAULT 0'),
34 'choice3id': ('INT', 'NOT NULL DEFAULT 0'),
35 'choice4id': ('INT', 'NOT NULL DEFAULT 0'),
36 'choice5id': ('INT', 'NOT NULL DEFAULT 0'),
37 'choice6id': ('INT', 'NOT NULL DEFAULT 0'),
38 'choice7id': ('INT', 'NOT NULL DEFAULT 0'),
39 'timelimit': ('INT16', 'NOT NULL DEFAULT 0'),
40 'points': ('INT16', 'NOT NULL DEFAULT 0'),
41 'ownerid': ('INT', 'NOT NULL',),
42 'editors': ('NEWLINELIST', "NOT NULL DEFAULT ''",),
43 'flags': ('INT', 'NOT NULL DEFAULT 0'),
44 }
45 _dbindexes = ()
46 _dblinks = ['learn_card']
47 _dbdisplay = ['problemid', 'answerid']
48 _dbflags = 0
49
50 # -------------------------------------------------------------------
51 def __init__(self):
52 super().__init__()
53
54# =====================================================================
56 g,
57 cardids,
58 problemtype = CARD_LINK_PROBLEM,
59 extraproblemids = []
60 ):
61 """This function returns a list of problems as dicts given lesson ids
62 or explicit problem ids.
63 """
64
65 problems = []
66 problemcardids = []
67
68 if not isinstance(cardids, list):
69 cardids = [cardids]
70
71 cardids = list(set(cardids))
72
73 for cardid in cardids:
74 c = g.db.Load(learn_card, cardid, 'rid')
75
76 GetChildProblems(g, c, problems, problemcardids, problemtype)
77
78 if extraproblemids:
79 extraproblemids = list(set(extraproblemids))
80
81 for r in g.db.LoadMany(
82 learn_problem,
83 [x for x in extraproblemids],
84 ):
85 o = {}
86
87 o['idcode'] = ToShortCode(r.rid)
88 o['problemidcode'] = ToShortCode(r.problemid)
89 o['answeridcode'] = ToShortCode(r.answerid)
90
91 problems.append(o)
92
93 problemcardids.append(r.problemid)
94 problemcardids.append(r.answerid)
95
96 cards = {}
97
98 if len(problemcardids) == 0:
99 return []
100
101 problemcardids = list(set(problemcardids))
102
103 for r in g.db.LoadMany(
104 learn_card,
105 problemcardids,
106 'rid, content, image, sound, video',
107 ):
108 o = r.ToJSON('content, image, sound, video')
109
110 o['idcode'] = ToShortCode(r.rid)
111
112 cards[o['idcode']] = o
113
114 intactproblems = []
115
116 for p in problems:
117 if p['problemidcode'] not in cards or p['answeridcode'] not in cards or not cards[p['problemidcode']] or not cards[p['answeridcode']]:
118 print(f'''GetAllProblems():\tSkipping incomplete problem with ID {p['idcode']}''')
119 continue
120
121 p['problem'] = cards[p['problemidcode']]
122 p['answer'] = cards[p['answeridcode']]
123
124 intactproblems.append(p)
125
126 return intactproblems
127
128# =====================================================================
129def GetChildProblems(g, card, problems, problemcardids, problemtype):
130 """A helper function for GetAllProblems() to iterate through
131 lesson trees.
132 """
133 for r in g.db.Linked(card, learn_card, CARD_CHILD, fields = 'rid'):
134 GetChildProblems(g, r, problems, problemcardids)
135
136 for r in g.db.Linked(card, learn_problem, problemtype, fields = 'rid, problemid, answerid'):
137 o = {}
138
139 o['idcode'] = ToShortCode(r.rid)
140 o['problemidcode'] = ToShortCode(r.problemid)
141 o['answeridcode'] = ToShortCode(r.answerid)
142
143 problems.append(o)
144
145 problemcardids.append(r.problemid)
146 problemcardids.append(r.answerid)
A problem in the Pafera Learning System is defined as a collection of cards.
Definition: problem.py:17
def __init__(self)
Initialize all fields at creation like a good programmer should.
Definition: problem.py:51
Base class for all database models.
Definition: modelbase.py:20
def GetAllProblems(g, cardids, problemtype=CARD_LINK_PROBLEM, extraproblemids=[])
This function returns a list of problems as dicts given lesson ids or explicit problem ids.
Definition: problem.py:60
def GetChildProblems(g, card, problems, problemcardids, problemtype)
A helper function for GetAllProblems() to iterate through lesson trees.
Definition: problem.py:129
Definition: db.py:1
def ToShortCode(val, chars='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_')
Turns a 32-bit value into a six character alphanumeric code.
Definition: utils.py:36