~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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# IVLE
# 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

# Author: Matt Giuca, Will Grant

'''Tutorial/worksheet/exercise application.

Displays tutorial content with editable exercises, allowing students to test
and submit their solutions to exercises and have them auto-tested.
'''

import os
import urllib
import re
import mimetypes
from datetime import datetime
from xml.dom import minidom

import genshi

import ivle.util
import ivle.conf
import ivle.database
from ivle.database import Subject
import ivle.worksheet
from ivle.webapp.base.views import BaseView
from ivle.webapp.base.xhtml import XHTMLView
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
from ivle.webapp.media import BaseMediaFileView
from ivle.webapp.errors import NotFound, Forbidden
from ivle.webapp.tutorial.rst import rst as rstfunc
from ivle.webapp.tutorial.service import AttemptsRESTView, \
                                        AttemptRESTView, ExerciseRESTView

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

class Worksheet:
    def __init__(self, id, name, assessable):
        self.id = id
        self.name = name
        self.assessable = assessable
        self.loc = urllib.quote(id)
        self.complete_class = ''
        self.optional_message = ''
        self.total = 0
        self.mand_done = 0
    def __repr__(self):
        return ("Worksheet(id=%s, name=%s, assessable=%s)"
                % (repr(self.id), repr(self.name), repr(self.assessable)))

class SubjectView(XHTMLView):
    '''The view of the index of worksheets for a subject.'''
    template = 'subjectmenu.html'
    appname = 'tutorial' # XXX
    permission = 'view'

    def __init__(self, req, subject):
        self.context = req.store.find(Subject, code=subject).one()

    def populate(self, req, ctx):
        self.plugin_styles[Plugin] = ['tutorial.css']

        if not self.context:
            raise NotFound()

        # Subject names must be valid identifiers
        if not is_valid_subjname(self.context.code):
            raise NotFound()

        # Parse the subject description file
        # The subject directory must have a file "subject.xml" in it,
        # or it does not exist (404 error).
        ctx['subject'] = self.context.code
        try:
            subjectfile = open(os.path.join(ivle.conf.subjects_base,
                                    self.context.code, "subject.xml")).read()
        except:
            raise NotFound()

        subjectfile = genshi.Stream(list(genshi.XML(subjectfile)))

        ctx['worksheets'] = get_worksheets(subjectfile)

        # 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 ctx['worksheets']:
            stored_worksheet = ivle.database.Worksheet.get_by_name(req.store,
                self.context.code, worksheet.id)
            # If worksheet is not in database yet, we'll simply not display
            # data about it yet (it should be added as soon as anyone visits
            # the worksheet itself).
            if stored_worksheet is not None:
                # If the assessable status of this worksheet has changed,
                # update the DB
                # (Note: This fails the try block if the worksheet is not yet
                # in the DB, which is fine. The author should visit the
                # worksheet page to get it into the DB).
                if worksheet.assessable != stored_worksheet.assessable:
                    # XXX If statement to avoid unnecessary database writes.
                    # Is this necessary, or will Storm check for us?
                    stored_worksheet.assessable = worksheet.assessable
                if worksheet.assessable:
                    # Calculate the user's score for this worksheet
                    mand_done, mand_total, opt_done, opt_total = (
                        ivle.worksheet.calculate_score(req.store, req.user,
                            stored_worksheet))
                    if opt_total > 0:
                        optional_message = " (excluding optional exercises)"
                    else:
                        optional_message = ""
                    if mand_done >= mand_total:
                        worksheet.complete_class = "complete"
                    elif mand_done > 0:
                        worksheet.complete_class = "semicomplete"
                    else:
                        worksheet.complete_class = "incomplete"
                    problems_done += mand_done
                    problems_total += mand_total
                    worksheet.mand_done = mand_done
                    worksheet.total = mand_total
                    worksheet.optional_message = optional_message


        ctx['problems_total'] = problems_total
        ctx['problems_done'] = problems_done
        if problems_total > 0:
            if problems_done >= problems_total:
                ctx['complete_class'] = "complete"
            elif problems_done > 0:
                ctx['complete_class'] = "semicomplete"
            else:
                ctx['complete_class'] = "incomplete"
            ctx['problems_pct'] = (100 * problems_done) / problems_total
            # TODO: Put this somewhere else! What is this on about? Why 16?
            # XXX Marks calculation (should be abstracted out of here!)
            # percent / 16, rounded down, with a maximum mark of 5
            ctx['max_mark'] = 5
            ctx['mark'] = min(ctx['problems_pct'] / 16, ctx['max_mark'])

class WorksheetView(XHTMLView):
    '''The view of a worksheet with exercises.'''
    template = 'worksheet.html'
    appname = 'tutorial' # XXX
    permission = 'view'

    def __init__(self, req, subject, worksheet):
        # XXX: Worksheet is actually context, but it's not really there yet.
        self.context = req.store.find(Subject, code=subject).one()
        self.worksheetname = worksheet

    def populate(self, req, ctx):
        self.plugin_scripts[Plugin] = ['tutorial.js']
        self.plugin_styles[Plugin] = ['tutorial.css']

        if not self.context:
            raise NotFound()

        # Subject and worksheet names must be valid identifiers
        if not is_valid_subjname(self.context.code) or \
           not is_valid_subjname(self.worksheetname):
            raise NotFound()

        # Read in worksheet data
        worksheetfilename = os.path.join(ivle.conf.subjects_base,
                               self.context.code, self.worksheetname + ".xml")
        try:
            worksheetfile = open(worksheetfilename)
            worksheetmtime = os.path.getmtime(worksheetfilename)
        except:
            raise NotFound()

        worksheetmtime = datetime.fromtimestamp(worksheetmtime)
        worksheetfile = worksheetfile.read()

        ctx['subject'] = self.context.code
        ctx['worksheet'] = self.worksheetname
        ctx['worksheetstream'] = genshi.Stream(list(genshi.XML(worksheetfile)))

        #TODO: Replace this with a nice way, possibly a match template
        generate_worksheet_data(ctx, req)

        update_db_worksheet(req.store, self.context.code, self.worksheetname,
            worksheetmtime, ctx['exerciselist'])

        ctx['worksheetstream'] = add_exercises(ctx['worksheetstream'], ctx, req)

class SubjectMediaView(BaseMediaFileView):
    '''The view of subject media files.

    URIs pointing here will just be served directly, from the subject's
    media directory.
    '''
    permission = 'view'

    def __init__(self, req, subject, path):
        self.context = req.store.find(Subject, code=subject).one()
        self.path = os.path.normpath(path)

    def _make_filename(self, req):
        # If the subject doesn't exist, self.subject will be None. Die.
        if not self.context:
            raise NotFound()

        subjectdir = os.path.join(ivle.conf.subjects_base,
                                  self.context.code, 'media')
        return os.path.join(subjectdir, self.path)

def is_valid_subjname(subject):
    m = re_ident.match(subject)
    return m is not None and m.end() == len(subject)

def get_worksheets(subjectfile):
    '''Given a subject stream, get all the worksheets and put them in ctx'''
    worksheets = []
    for kind, data, pos in subjectfile:
        if kind is genshi.core.START:
            if data[0] == 'worksheet':
                worksheetid = ''
                worksheetname = ''
                worksheetasses = False
                for attr in data[1]:
                    if attr[0] == 'id':
                        worksheetid = attr[1]
                    elif attr[0] == 'name':
                        worksheetname = attr[1]
                    elif attr[0] == 'assessable':
                        worksheetasses = attr[1] == 'true'
                worksheets.append(Worksheet(worksheetid, worksheetname, \
                                                            worksheetasses))
    return worksheets

# This generator adds in the exercises as they are required. This is returned    
def add_exercises(stream, ctx, req):
    """A filter which adds exercises into the stream."""
    exid = 0
    for kind, data, pos in stream:
        if kind is genshi.core.START:
            # Remove the worksheet tags, as they are not xhtml valid.
            if data[0] == 'worksheet':
                continue
            # If we have an exercise node, replace it with the content of the
            # exercise.
            elif data[0] == 'exercise':
                new_stream = ctx['exercises'][exid]['stream']
                exid += 1
                for item in new_stream:
                    yield item
            else:
                yield kind, data, pos
        # Remove the end tags for exercises and worksheets
        elif kind is genshi.core.END:
            if data == 'exercise':
                continue
            elif data == 'worksheet':
                continue
            else:
                yield kind, data, pos
        else:
            yield kind, data, pos

# This function runs through the worksheet, to get data on the exercises to
# build a Table of Contents, as well as fill in details in ctx
def generate_worksheet_data(ctx, req):
    """Runs through the worksheetstream, generating the exericises"""
    exid = 0
    ctx['exercises'] = []
    ctx['exerciselist'] = []
    for kind, data, pos in ctx['worksheetstream']:
        if kind is genshi.core.START:
            if data[0] == 'exercise':
                exid += 1
                src = ""
                optional = False
                for attr in data[1]:
                    if attr[0] == 'src':
                        src = attr[1]
                    if attr[0] == 'optional':
                        optional = attr[1] == 'true'
                # Each item in toc is of type (name, complete, stream)
                ctx['exercises'].append(present_exercise(req, src, exid))
                ctx['exerciselist'].append((src, optional))
            elif data[0] == 'worksheet':
                ctx['worksheetname'] = 'bob'
                for attr in data[1]:
                    if attr[0] == 'name':
                        ctx['worksheetname'] = attr[1]

def innerXML(elem):
    """Given an element, returns its children as XML strings concatenated
    together."""
    s = ""
    for child in elem.childNodes:
        s += child.toxml()
    return s

def getTextData(element):
    """ Get the text and cdata inside an element
    Leading and trailing whitespace are stripped
    """
    data = ''
    for child in element.childNodes:
        if child.nodeType == child.CDATA_SECTION_NODE:
            data += child.data
        elif child.nodeType == child.TEXT_NODE:
            data += child.data
        elif child.nodeType == child.ELEMENT_NODE:
            data += getTextData(child)

    return data.strip()

#TODO: This needs to be re-written, to stop using minidom, and get the data
# about the worksheet directly from the database
def present_exercise(req, exercisesrc, exerciseid):
    """Open a exercise file, and write out the exercise to the request in HTML.
    exercisesrc: "src" of the exercise file. A path relative to the top-level
        exercises base directory, as configured in conf.
    """
    # Exercise-specific context is used here, as we already have all the data
    # we need
    curctx = genshi.template.Context()
    curctx['filename'] = exercisesrc
    curctx['exerciseid'] = exerciseid

    # Retrieve the exercise details from the database
    exercise = ivle.database.Exercise.get_by_name(req.store, exercisesrc)
    #Open the exercise, and double-check that it exists
    exercisefile = ivle.util.open_exercise_file(exercisesrc)
    if exercisefile is None:
        raise NotFound()

    # Read exercise file and present the exercise
    # Note: We do not use the testing framework because it does a lot more
    # work than we need. We just need to get the exercise name and a few other
    # fields from the XML.

    #TODO: Replace calls to minidom with calls to the database directly
    exercisedom = minidom.parse(exercisefile)
    exercisefile.close()
    exercisedom = exercisedom.documentElement
    assert exercisedom.tagName == "exercise", \
           "Exercise file top-level element must be <exercise>."
    curctx['exercisename'] = exercisedom.getAttribute("name")

    curctx['rows'] = exercisedom.getAttribute("rows")
    if not curctx['rows']:
        curctx['rows'] = "12"
    # Look for some other fields we need, which are elements:
    # - desc
    # - partial
    curctx['exercisedesc'] = None
    curctx['exercisepartial'] = ""
    for elem in exercisedom.childNodes:
        if elem.nodeType == elem.ELEMENT_NODE:
            if elem.tagName == "desc":
                curctx['exercisedesc'] = genshi.XML(
                                              rstfunc(innerXML(elem).strip()))
            if elem.tagName == "partial":
                curctx['exercisepartial'] = getTextData(elem) + '\n'
    curctx['exercisepartial_backup'] = curctx['exercisepartial']

    # If the user has already saved some text for this problem, or submitted
    # an attempt, then use that text instead of the supplied "partial".
    saved_text = ivle.worksheet.get_exercise_stored_text(req.store,
        req.user, exercise)
    # Also get the number of attempts taken and whether this is complete.
    complete, curctx['attempts'] = \
            ivle.worksheet.get_exercise_status(req.store, req.user, exercise)
    if saved_text is not None:
        curctx['exercisepartial'] = saved_text.text
    curctx['complete'] = 'Complete' if complete else 'Incomplete'
    curctx['complete_class'] = curctx['complete'].lower()

    #Save the exercise details to the Table of Contents

    loader = genshi.template.TemplateLoader(".", auto_reload=True)
    tmpl = loader.load(os.path.join(os.path.dirname(__file__), "exercise.html"))
    ex_stream = tmpl.generate(curctx)
    return {'name': curctx['exercisename'],
            'complete': curctx['complete_class'],
            'stream': ex_stream,
            'exid': exerciseid}


def update_db_worksheet(store, subject, worksheetname, file_mtime,
    exercise_list=None, assessable=None):
    """
    Determines if the database is missing this worksheet or out of date,
    and inserts or updates its details about the worksheet.
    file_mtime is a datetime.datetime with the modification time of the XML
    file. The database will not be updated unless worksheetmtime is newer than
    the mtime in the database.
    exercise_list is a list of (filename, optional) pairs as returned by
    present_table_of_contents.
    assessable is boolean.
    exercise_list and assessable are optional, and if omitted, will not change
    the existing data. If the worksheet does not yet exist, and assessable
    is omitted, it defaults to False.
    """
    worksheet = ivle.database.Worksheet.get_by_name(store, subject,
                                                    worksheetname)

    updated_database = False
    if worksheet is None:
        # If assessable is not supplied, default to False.
        if assessable is None:
            assessable = False
        # Create a new Worksheet
        worksheet = ivle.database.Worksheet(subject=unicode(subject),
            name=unicode(worksheetname), assessable=assessable,
            mtime=datetime.now())
        store.add(worksheet)
        updated_database = True
    else:
        if file_mtime > worksheet.mtime:
            # File on disk is newer than database. Need to update.
            worksheet.mtime = datetime.now()
            if exercise_list is not None:
                # exercise_list is supplied, so delete any existing problems
                worksheet.remove_all_exercises(store)
            if assessable is not None:
                worksheet.assessable = assessable
            updated_database = True

    if updated_database and exercise_list is not None:
        # Insert each exercise into the worksheet
        for exercise_name, optional in exercise_list:
            # Get the Exercise from the DB
            exercise = ivle.database.Exercise.get_by_name(store,exercise_name)
            # Create a new binding between the worksheet and the exercise
            worksheetexercise = ivle.database.WorksheetExercise(
                    worksheet=worksheet, exercise=exercise, optional=optional)

    store.commit()

class Plugin(ViewPlugin, MediaPlugin):
    urls = [
        ('subjects/:subject/+worksheets', SubjectView),
        ('subjects/:subject/+worksheets/+media/*(path)', SubjectMediaView),
        ('subjects/:subject/+worksheets/:worksheet', WorksheetView),
        ('api/subjects/:subject/+worksheets/:worksheet/*exercise/'
            '+attempts/:username', AttemptsRESTView),
        ('api/subjects/:subject/+worksheets/:worksheet/*exercise/'
                '+attempts/:username/:date', AttemptRESTView),
        ('api/subjects/:subject/+worksheets/:worksheet/*exercise', ExerciseRESTView),
    ]

    tabs = [
        ('tutorial', 'Worksheets',
         'Online tutorials and exercises for lab work.', 'tutorial.png',
         'tutorial', 2)
    ]

    media = 'media'
    help = {'Tutorial': 'help.html'}