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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
#!/usr/bin/env python
import sys, subprocess, time
import cgi, cgitb
from sugar.activity import activity
from path import path
from activities import getInstalled
from activities import getActivitiesJs
from activities import fetchMilestone
from activities import getMilestones
from activities import getActivities
from activities import setMilestones
from activities import setActivities
DATAPATH = path(activity.get_activity_root())/ 'data'
log = open('/tmp/logr','w')
retval = 0
cgitb.enable(display=True)
print 'Content-type:text/html\n\n'
#parameters is score
form = cgi.FieldStorage()
score = form.getfirst('score', default="")
subject = form.getfirst('subject',default="")
milestone = form.getfirst('milestone',default="")
activity = form.getfirst('activity',default="")
print >> log, 'score', score, subject, milestone, activity
if len(score) > 0 and len(subject) > 0 and len(milestone) >0 and len(activity) > 0:
#close current session (/tmp/current_session)
#update student record (/tmp/learner)
#check for log in
try:
fin=open(DATAPATH / 'learner','r')
learner = fin.read()
fin.close()
except:
#no one is logged in
print >> log, 'no one is logged in'
learner = 'unknown'
print >> log, 'learner',learner
#read student record
try:
fin = open(DATAPATH / learner,'r')
txt = fin.read()
fin.close()
except:
print >> log, 'no matching student record'
#no matching student_record - force blank student_record
learner = 'unknown'
txt = '{}'
try:
student_record = eval(txt)
except:
student_record = {}
student_record['grade'] = ''
student_record['milestones'] = ['1.1','1.1']
student_record['progress'] = []
print >> log, 'student_record eval failed',sys.exc_info()[:2]
print >> log, 'student record txt',txt
#read current_session
try:
fin = open('/tmp/current_session','r')
txt = fin.read()
fin.close()
except:
#no session open
txt = ""
print >> log,'no session open'
print >> log, 'session', txt
session_data = txt.split(',')
if len(session_data)>1:
activity=session_data[0]
starttime = session_data[1]
stoptime = str(int(time.time()))
result = score
print >> log, 'session data', activity, starttime, stoptime, result
else:
activity = ""
result = ''
print >> log, 'no session data'
#update activities.js
print >> log, 'update activities.js', activity, result
if len(activity)>0 and int(result) >= 70:
#we can do this via the form
#update activities.js
#find current milestone
#get milestones, find red one
#find current activity
#get activities
#find red one
#update to green
#find next one
#update to red
milestone = activity[:7]
if activity[:2] == 'en':
subject = 'English'
else:
subject = 'Mathematics'
pth = path('content') / subject / milestone / 'activities.js'
print >> log, 'read activities activities.js',pth
try:
fin = open(pth,'r')
txt = fin.read()
fin.close()
except:
print 'open activities activities.js failed', sys.exc_info()[:2]
lines = txt.split('\n')
txtout = ""
nxtflag = False
print >>log, 'activities.js lines',len(lines)
for line in lines:
line = line + '\n'
if activity in line:
nxtflag=True
retval = 1
txtout=txtout+line.replace('red','green')
elif nxtflag and line.find('blue')>-1:
nxtflag=False
txtout=txtout+line.replace('blue','red')
else:
txtout = txtout+line
try:
fout = open(pth,'w')
fout.write(txtout)
fout.close()
except:
print >> log, 'write activities activities.js failed',sys.exc_info()[:2]
print >> log, 'activity activities.js updated', nxtflag
#if nxtflag == True - means end of milestone
if nxtflag:
#update milestones in student record
try:
milestones = student_record['milestones']
print >> log, 'milestones', milestones
if subject == 'English':
msstate = milestones[0]
else:
msstate = milestones[1]
print >> log, 'msstate', msstate
try:
temp = msstate.split('.')
msstr = int(temp[0])+1
temp[0] = str(int(msstr))
temp[1] = '1'
msstate = temp[0]+'.'+temp[1]
except:
print >> log, 'msstate set up failed', sys.exc_info()[:2]
if subject == 'English':
milestones[0] = msstate
else:
milestones[1] = msstate
student_record['milestones']=milestones
except:
print >> log, 'update milestones failed',sys.exc_info()[:2]
#update activities.js for subject
milestone = activity[:7]
pth = path('content') / subject / 'activities.js'
print >> log, 'milestone',milestone,'pth',pth
fin = open(pth,'r')
txt = fin.read()
fin.close()
lines = txt.split('\n')
txtout = ""
nxtmsflag = False
for line in lines:
line = line + '\n'
if milestone in line:
nxtmsflag = True
retval = 2
txtout = txtout + line.replace('red','green')
elif nxtmsflag == True:
if line.find('blue')>0:
nxtmsflag = False
txtout = txtout + line.replace('blue','red')
elif line.find('Blue')>0:
nxtmsflag = False
txtout = txtout + line.replace('Blue','red')
else:
print >> log, nxtmsflag, line
txtout = txtout + line
fout = open(pth,'w')
fout.write(txtout)
fout.close()
if nxtmsflag:
#what do we do now - this learner is done!
print >> log, 'learner is done!'
retval = 3
#update first activity in next milestone
print >> log, 'first activity in next milestone', milestone
nxt = str(int(milestone[5:7])+1)
if len(nxt)<2:
nxt = '0'+nxt
next_milestone = milestone[:5]+nxt
next_activity = next_milestone + 'a01'
print >> log, 'next', next_milestone, next_activity
pth = path('content') / subject / next_milestone / 'activities.js'
print >> log, 'pth', pth
fin = open(pth,'r')
txt = fin.read()
fin.close()
lines = txt.split('\n')
txtout = ""
for line in lines:
line = line + '\n'
if next_activity in line:
t1 = line.replace('Blue','red')
txtout = txtout + t1.replace('blue','red')
else:
txtout = txtout + line
fout = open(pth,'w')
fout.write(txtout)
fout.close()
else:
#update milestones in student record
milestones = student_record['milestones']
if subject == 'English':
msstate = milestones[0]
else:
msstate = milestones[1]
temp = msstate.split('.')
msstr = int(temp[1])+1
temp[1] = str(int(msstr))
msstate = temp[0]+'.'+temp[1]
if subject == 'English':
milestones[0] = msstate
else:
milestones[1] = msstate
student_record['milestones']=milestones
#update student_record
#if there is a problem, above processing should leave activity = ''
print >> log, activity, len(activity)
if len(activity) == 10:
try:
progress = student_record['progress']
status = (starttime, stoptime, result)
progress.append({activity:status})
student_record['progress'] = progress
except:
print >> log, 'update progress failed', sys.exc_info()[:2]
try:
fout = open(DATAPATH / learner,'w')
fout.write(str(student_record))
fout.close()
except:
print >> log, 'update student record failed', sys.exc_info()[:2]
#close current session - works even if none open
subprocess.call('rm -rf /tmp/current_session',shell=True)
print >> log, 'exception?',sys.exc_info()[:2]
print retval
log.close()
|