~azzar1/unity/add-show-desktop-key

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
#!/usr/bin/env python
# IVLE - Informatics Virtual Learning Environment
# Copyright (C) 2007-2008 The University of Melbourne
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

# Program: Marks
# Author:  Matt Giuca
# Date:    17/4/2008

# Script to calculate the marks for all students for a particular subject.
# Requires root to run.

import sys
import os
import re
import csv
from xml.dom import minidom

import ivle.database
import ivle.worksheet
import ivle.conf

if os.getuid() != 0:
    print >>sys.stderr, "Must run %s as root." % os.path.basename(sys.argv[0])
    sys.exit()

if len(sys.argv) <= 1:
    print >>sys.stderr, "Usage: %s subject" % os.path.basename(sys.argv[0])
    sys.exit()

# Regex for valid identifiers (subject/worksheet names)
re_ident = re.compile("[0-9A-Za-z_]+")

# This code copy/edited from www/apps/tutorial/__init__.py
def is_valid_subjname(subject):
    m = re_ident.match(subject)
    return m is not None and m.end() == len(subject)

subject = sys.argv[1]

# Subject names must be valid identifiers
if not is_valid_subjname(subject):
    print >>sys.stderr, "Invalid subject name: %s." % repr(subject)
    sys.exit()

def get_userdata(user):
    """
    Given a User object, returns a list of strings for the user data which
    will be part of the output for this user.
    (This is not marks, it's other user data).
    """
    last_login = (None if user.last_login is None else
                    user.last_login.strftime("%d/%m/%y"))
    return [user.studentid, user.login, user.fullname, last_login]
userdata_header = ["Student ID", "Login", "Full name", "Last login"]

def get_assessable_worksheets(subject):
    """
    Given a subject name, returns a list of strings - the worksheet names (not
    primary key IDs) for all assessable worksheets for that subject.
    May raise Exceptions, which are fatal.
    """
    # NOTE: This code is copy/edited from
    # www/apps/tutorial/__init__.py:handle_subject_menu
    # Should be factored out of there.

    # Parse the subject description file
    # The subject directory must have a file "subject.xml" in it,
    # or it does not exist (raise exception).
    try:
        subjectfile = open(os.path.join(ivle.conf.subjects_base, subject,
            "subject.xml"))
    except:
        raise Exception("Subject %s not found." % repr(subject))

    assessable_worksheets = []
    # Read in data about the subject
    subjectdom = minidom.parse(subjectfile)
    subjectfile.close()
    # TEMP: All of this is for a temporary XML format, which will later
    # change.
    worksheetsdom = subjectdom.documentElement
    worksheets = []     # List of string IDs
    for worksheetdom in worksheetsdom.childNodes:
        if worksheetdom.nodeType == worksheetdom.ELEMENT_NODE:
            # (Note: assessable will default to False, unless it is explicitly
            # set to "true").
            if worksheetdom.getAttribute("assessable") == "true":
                assessable_worksheets.append(worksheetdom.getAttribute("id"))

    return assessable_worksheets

def get_marks_header(worksheets):
    """
    Given a list of strings - the assessable worksheets - returns a new list
    of strings - the column headings for the marks section of the CSV output.
    """
    return worksheets + ["Total %", "Mark"]

def get_marks_user(subject, worksheet_names, user):
    """
    Given a subject, a list of strings (the assessable worksheets), and a user
    object, returns the user's percentage for each worksheet, overall, and
    their final mark, as a list of strings, in a manner which corresponds to
    the headings produced by get_marks_header.
    """
    # NOTE: This code is copy/edited from
    # www/apps/tutorial/__init__.py:handle_subject_menu
    # Should be factored out of there.

    worksheet_pcts = []
    # As we go, calculate the total score for this subject
    # (Assessable worksheets only, mandatory problems only)
    problems_done = 0
    problems_total = 0

    for worksheet_name in worksheet_names:
        worksheet = ivle.database.Worksheet.get_by_name(store,
            subject, worksheet_name)
        # We simply ignore optional exercises here
        mand_done, mand_total, _, _ = (
            ivle.worksheet.calculate_score(store, user, worksheet))
        worksheet_pcts.append(float(mand_done) / mand_total)
        problems_done += mand_done
        problems_total += mand_total
    problems_pct = float(problems_done) / problems_total
    problems_pct_int = (100 * problems_done) / problems_total
    # XXX Marks calculation (should be abstracted out of here!)
    # percent / 16, rounded down, with a maximum mark of 5
    max_mark = 5
    mark = min(problems_pct_int / 16, max_mark)
    return worksheet_pcts + [problems_pct, mark]

def writeuser(subject, worksheets, user, csvfile):
    userdata = get_userdata(user)
    marksdata = get_marks_user(subject, worksheets, user)
    csvfile.writerow(userdata + marksdata)

try:
    # Get the list of assessable worksheets from the subject.xml file.
    worksheets = get_assessable_worksheets(subject)
    store = ivle.database.get_store()
except Exception, message:
    print >>sys.stderr, "Error: " + str(message)
    sys.exit(1)

# Start writing the CSV file - header
csvfile = csv.writer(sys.stdout)
csvfile.writerow(userdata_header + get_marks_header(worksheets))

for user in store.find(ivle.database.User).order_by(ivle.database.User.login):
    writeuser(subject, worksheets, user, csvfile)