Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

Commit

Permalink
Merge pull request #23 from colincoghill/3.9.2-bugfixes
Browse files Browse the repository at this point in the history
Spreadsheet export of assessments wasn't working.
  • Loading branch information
colincoghill committed Jul 5, 2013
2 parents 64e6bac + 405102e commit 14b777d
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/oasis/lib/Groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def active_by_course(course_id):
AND "groupcourses"."groupid" = "ugroups"."id"
AND "groupcourses"."course" = %s
AND "ugroups"."period" = "periods"."id";""",
# AND "periods"."finish" > NOW();;""",
# AND "periods"."finish" > NOW();""",
(course_id,))
groups = {}
if ret:
Expand Down Expand Up @@ -297,8 +297,8 @@ def enrolment_groups():
FROM "ugroups", "periods"
WHERE "ugroups"."gtype" = 2
AND "ugroups"."active" = TRUE
AND "ugroups"."period" = "periods"."id"
AND "periods"."finish" > NOW();""") # gtype 2 = enrolment
AND "ugroups"."period" = "periods"."id";""")
# AND "periods"."finish" > NOW();""") # gtype 2 = enrolment
groups = {}
if ret:
for row in ret:
Expand Down
91 changes: 91 additions & 0 deletions src/oasis/lib/Spreadsheets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# coding=utf-8
"""
Functionality for importing and exporting spreadsheets.
"""

from oasis.lib import Groups, Courses2, Exams, Users2

from openpyxl.reader.excel import load_workbook
from openpyxl.writer.excel import save_virtual_workbook
from openpyxl import Workbook
from datetime import datetime


def exam_results_as_spreadsheet(course_id, group, exam_id):
""" Export the assessment results as a XLSX spreadsheet """

course = Courses2.get_course(course_id)
exam = Exams.get_exam_struct(exam_id, course_id)

uids = set([])
totals = {}

results = Exams.get_marks(group, exam_id)
for user_id in results:
uids.add(user_id)
if not user_id in totals:
totals[user_id] = 0.0
for qt, val in results[user_id].iteritems():
totals[user_id] += val['score']

questions = Exams.get_qts_list(exam_id)
users = {}
for uid in uids:
users[uid] = Users2.get_user(uid)

when = datetime.now().strftime("%H:%m, %a %d %b %Y"),

wb = Workbook()

ws = wb.get_active_sheet()

ws.title = "Results"

ws.cell(row=1, column=0).value = course['name']
ws.cell(row=1, column=1).value = course['title']
ws.cell(row=2, column=0).value = "Assessment:"
ws.cell(row=2, column=1).value = exam['title']
ws.cell(row=3, column=0).value = "Group:"
ws.cell(row=3, column=1).value = group.name

col = 5
qcount = 1
for _ in questions:
ws.cell(row=4, column=col).value = "Q%s" % qcount
qcount += 1
col += 1

ws.cell(row=4, column=col).value = "Total"

row = 5
sortusers = users.keys()
sortusers.sort(key=lambda us: users[us]['familyname'])

for user_id in sortusers:
result = results[user_id]
ws.cell(row=row, column=0).value = users[user_id]['uname']
ws.cell(row=row, column=1).value = users[user_id]['student_id']
ws.cell(row=row, column=2).value = users[user_id]['familyname']
ws.cell(row=row, column=3).value = users[user_id]['givenname']
ws.cell(row=row, column=4).value = users[user_id]['email']
col = 5

for pos in questions:
for qt in pos:
if qt['id'] in result:
ws.cell(row=row, column=col).value = result[qt['id']]['score']
col += 1

ws.cell(row=row, column=col).value = totals[user_id]
row += 1
# ws.cell(row=2,column=3).value = "NCEA4"

# for q in range(11,56): # questions
# ws.cell(row=1,column=x).value="%s" % (q,)
# ws.cell(row=3, column=x).value=correct["q%s"%(q,)]

# for row in [1,2,3]:
# for col in range(0,121):
# ws.cell(row=row, column=col).style.font.bold=True

return save_virtual_workbook(wb)
22 changes: 9 additions & 13 deletions src/oasis/views_cadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
# ask Google about: AttributeError: _strptime
from logging import log, ERROR
from flask import render_template, session, request, redirect, \
abort, url_for, flash
abort, url_for, flash, make_response

from oasis.lib import OaConfig, Users2, DB, Topics, Permissions, \
Exams, Courses, Courses2, Setup, CourseAdmin, Groups, General, Assess
Exams, Courses, Courses2, Setup, CourseAdmin, Groups, General, Assess, \
Spreadsheets

MYPATH = os.path.dirname(__file__)

Expand Down Expand Up @@ -346,18 +347,13 @@ def cadmin_export_csv(course_id, exam_id, group_id):
flash("Assessment %s does not belong to this course." % int(exam_id))
return redirect(url_for('cadmin_top', course_id=course_id))

exam['start_date'] = int(date_from_py2js(exam['start']))
exam['end_date'] = int(date_from_py2js(exam['end']))
exam['start_hour'] = int(exam['start'].hour)
exam['end_hour'] = int(exam['end'].hour)
exam['start_minute'] = int(exam['start'].minute)
exam['end_minute'] = int(exam['end'].minute)
group = Groups.Group(g_id=group_id)
output = Spreadsheets.exam_results_as_spreadsheet(course_id, group, exam_id)
response = make_response(output)
response.headers.add('Content-Type', "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8")
response.headers.add('Content-Disposition', 'attachment; filename="OASIS_%s_%s_Results.xlsx"' % (course['title'], exam['title']))

return render_template(
"exam_edit.html",
course=course,
exam=exam
)
return response


@app.route("/cadmin/<int:course_id>/exam/<int:exam_id>/view/<int:student_uid>")
Expand Down

0 comments on commit 14b777d

Please sign in to comment.