-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstaticticsGenerate.py
157 lines (126 loc) · 5.05 KB
/
staticticsGenerate.py
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
import io
from PIL import Image
import xlsxwriter
def find_possible_answers(question_id, cursor, worksheet, row, workbook):
cursor.execute("SELECT * FROM Answer WHERE questionID = " +
str(question_id) + " ORDER BY 100*chosen/(shown + 1) DESC")
answers = cursor.fetchall()
first = True
answer_number = 1
for answer in answers:
showed = answer[7]
chosen = answer[6]
percentage = 0
content = answer[2]
default = answer[5]
image = answer[4]
rows = 1
if default == 1:
continue
if image is not None:
img = Image.open(io.BytesIO(image))
image_file_name = "img/a" + str(answer_number) + ".png"
img.save(image_file_name)
width, height = img.size
scale = 60.0/float(height)
# worksheet.set_row(row+2, 20) # Set the height of Row 1 to 20.
worksheet.insert_image(row+1, 1, image_file_name, {'x_scale': scale, 'y_scale': scale})
# os.remove(imageFileName)
rows = 5
if chosen != 0 and showed != 0:
percentage = 100 * chosen / showed
print("\tAnswer: " + str(answer_number))
if first:
bold = workbook.add_format({'bold': True})
first = False
else:
bold = workbook.add_format({'bold': False})
worksheet.write(row,1, content, bold)
worksheet.write(row,2, "showed: " + str(showed), bold)
worksheet.write(row,3, "chosen:" + str(chosen), bold)
worksheet.write(row,4, str(percentage) + "%", bold)
row += rows
answer_number += 1
return row
def find_answers_for_open_qestion(question_id, cursor, worksheet, row):
cursor.execute("SELECT * FROM Log WHERE questionID = " + str(question_id))
logs_dictionary = dict()
logs = cursor.fetchall()
for log in logs:
answer = log[4]
if answer is None:
continue
if answer in logs_dictionary:
logs_dictionary[answer] += 1
else:
logs_dictionary[answer] = 1
for answer in logs_dictionary:
worksheet.write(row,1, answer)
worksheet.write(row,2, "appeared: " + str(logs_dictionary[answer]))
print(str(answer) + " appeared: " + str(logs_dictionary[answer]))
row += 1
return row
def write_game_properties(game_id, cursor, worksheet):
cursor.execute("SELECT * FROM Game WHERE gameID = " + str(game_id))
game = cursor.fetchone()
game_name = game[1]
min_level = game[2]
lang = game[3]
worksheet.set_column('A:A', 30)
worksheet.set_column('B:B', 30)
worksheet.set_column('C:C', 12)
worksheet.set_column('D:D', 12)
worksheet.write(0,0,"game : " + str(game_name))
worksheet.write(1,0,"min level: " + str(min_level))
worksheet.write(2,0,"language: " + str(lang))
return 4 # next row number
def read_questions(game_id, cursor, worksheet, row, workbook):
cursor.execute("SELECT * FROM Question WHERE gameID = " + str(game_id))
questions = cursor.fetchall()
questions_number = 1
for question in questions:
question_id = question[0]
content = question[2]
type_id = question[6]
image = question[4]
closed = question[7]
rows = 1
if image is not None:
img = Image.open(io.BytesIO(image))
image_file_name = "img/q" + str(questions_number) + ".png"
img.save(image_file_name)
width, height = img.size
# print(width)
# print(height)
scale = 95.0/float(height)
# print(scale)
worksheet.set_row(row+2, 20) # Set the height of Row 1 to 20.
worksheet.insert_image(row+2, 0, image_file_name, {'x_scale': scale, 'y_scale': scale})
rows = 2
print("\nQuestion: " + str(questions_number))
row += 1
if closed == 1:
cell_format = workbook.add_format({'bg_color': '99ff66'}) # green
worksheet.write(row, 1, "Found proper answer", cell_format)
else:
cell_format = workbook.add_format({'bg_color': 'ff9966'}) # red
worksheet.write(row, 1, "", cell_format)
worksheet.write(row, 0, str(questions_number) + ": " + content, cell_format)
worksheet.write(row, 2, "", cell_format)
worksheet.write(row, 3, "", cell_format)
worksheet.write(row, 4, "", cell_format)
row += 1
if type_id == 1004: # open answer
row = find_answers_for_open_qestion(question_id, cursor, worksheet, row)
else:
row = find_possible_answers(question_id, cursor, worksheet, row, workbook)
questions_number += 1
row += rows
def generate_statistics(user_id, game_id, db_cursor):
cursor = db_cursor
workbook = xlsxwriter.Workbook('Report.xlsx')
worksheet = workbook.add_worksheet()
row = write_game_properties(game_id, cursor, worksheet)
read_questions(game_id, cursor, worksheet, row, workbook)
print("\nDone, check report.")
workbook.close()