~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
167
168
169
170
171
172
173
174
175
176
177
#!/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
import optparse
from xml.dom import minidom

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

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

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_header(worksheets):
    """
    Given a list of Worksheet objects (the assessable worksheets), returns a
    list of strings -- the column headings for the marks section of the CSV
    output.
    """
    return (userdata_header + [ws.name for ws in worksheets]
            + ["Total %", "Mark"])

def get_marks_user(worksheets, user):
    """Gets marks for a particular user for a particular set of worksheets.
    @param worksheets: List of Worksheet objects to get marks for.
    @param user: User to get marks for.
    @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.
    """
    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 in worksheets:
        # We simply ignore optional exercises here
        mand_done, mand_total, _, _ = (
            ivle.worksheet.utils.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(worksheets, user, csvfile):
    userdata = get_userdata(user)
    marksdata = get_marks_user(worksheets, user)
    csvfile.writerow(userdata + marksdata)

def main(argv=None):
    global store
    if argv is None:
        argv = sys.argv

    usage = """usage: %prog [options] subject
    (requires root)
    Reports each student's marks for a given subject offering."""

    # Parse arguments
    parser = optparse.OptionParser(usage)
    parser.add_option("-s", "--semester",
        action="store", dest="semester", metavar="YEAR/SEMESTER",
        help="Semester of the subject's offering (eg. 2009/1). "
        "Defaults to the currently active semester.",
        default=None)
    (options, args) = parser.parse_args(argv[1:])

    if len(args) < 1:
        parser.print_help()
        parser.exit()

    subject_name = unicode(args[0])

    if options.semester is None:
        year, semester = None, None
    else:
        try:
            year, semester = options.semester.split('/')
            if len(year) == 0 or len(semester) == 0:
                raise ValueError()
        except ValueError:
            parser.error('Invalid semester (must have form "year/semester")')

    store = ivle.database.get_store()

    # Get the subject from the DB
    subject = store.find(ivle.database.Subject,
                     ivle.database.Subject.short_name == subject_name).one()
    if subject is None:
        print >>sys.stderr, "No subject with short name '%s'" % subject_name
        return 1

    # Get the offering from the DB
    if semester is None:
        # None specified - get the current offering from the DB
        offerings = list(subject.active_offerings())
        if len(offerings) == 0:
            print >>sys.stderr, ("No active offering for subject '%s'"
                                 % subject_name)
            return 1
        elif len(offerings) > 1:
            print >>sys.stderr, ("Multiple active offerings for subject '%s':"
                                 % subject_name)
            print >>sys.stderr, "Please use one of:"
            for offering in offerings:
                print >>sys.stderr, ("    --semester=%s/%s"
                    % (offering.semester.year, offering.semester.semester))
            return 1
        else:
            offering = offerings[0]
    else:
        # Get the offering for the specified semester
        offering = subject.offering_for_semester(year, semester)
        if offering is None:
            print >>sys.stderr, (
                "No offering for subject '%s' in semester %s/%s"
                % (subject_name, year, semester))
            return 1

    # Get the list of assessable worksheets
    worksheets = list(offering.worksheets.find(assessable=True))

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

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

if __name__ == "__main__":
    sys.exit(main(sys.argv))