~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
472
473
474
475
476
477
478
479
480
481
482
# 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, Offering, Semester, Exercise, \
                          ExerciseSave, WorksheetExercise
from ivle.database import Worksheet as DBWorksheet
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, WorksheetRESTView, WorksheetsRESTView

class Worksheet:
    """This class represents a worksheet and a particular students progress
    through it.
    
    Do not confuse this with a worksheet in the database. This worksheet
    has extra information for use in the output, such as marks."""
    def __init__(self, id, name, assessable):
        self.id = id
        self.name = name
        self.assessable = assessable
        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 OfferingView(XHTMLView):
    '''The view of the index of worksheets for an offering.'''
    template = 'subjectmenu.html'
    appname = 'tutorial' # XXX
    permission = 'view'

    def __init__(self, req, subject, year, semester):
        """Find the given offering by subject, year and semester."""
        self.context = req.store.find(Offering,
            Offering.subject_id == Subject.id,
            Subject.code == subject,
            Offering.semester_id == Semester.id,
            Semester.year == year,
            Semester.semester == semester).one()
        
        if not self.context:
            raise NotFound()


    def populate(self, req, ctx):
        """Create the context for the given offering."""
        self.plugin_styles[Plugin] = ['tutorial.css']

        ctx['subject'] = self.context.subject.code
        ctx['year'] = self.context.semester.year
        ctx['semester'] = self.context.semester.semester

        # As we go, calculate the total score for this subject
        # (Assessable worksheets only, mandatory problems only)

        ctx['worksheets'] = []
        problems_done = 0
        problems_total = 0
        # Offering.worksheets is ordered by the worksheets seq_no
        for worksheet in self.context.worksheets:
            new_worksheet = Worksheet(worksheet.identifier, worksheet.name, 
                                      worksheet.assessable)
            if new_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,
                        worksheet))
                if opt_total > 0:
                    optional_message = " (excluding optional exercises)"
                else:
                    optional_message = ""
                if mand_done >= mand_total:
                    new_worksheet.complete_class = "complete"
                elif mand_done > 0:
                    new_worksheet.complete_class = "semicomplete"
                else:
                    new_worksheet.complete_class = "incomplete"
                problems_done += mand_done
                problems_total += mand_total
                new_worksheet.mand_done = mand_done
                new_worksheet.total = mand_total
                new_worksheet.optional_message = optional_message
            ctx['worksheets'].append(new_worksheet)

        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

            # We want to display a students mark out of 5. However, they are
            # allowed to skip 1 in 5 questions and still get 'full marks'.
            # Hence we divide by 16, essentially making 16 percent worth
            # 1 star, and 80 or above worth 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, year, semester, worksheet):
        self.context = req.store.find(DBWorksheet,
            DBWorksheet.offering_id == Offering.id,
            Offering.subject_id == Subject.id,
            Subject.code == subject,
            Offering.semester_id == Semester.id,
            Semester.year == year,
            Semester.semester == semester,
            DBWorksheet.identifier == worksheet).one()
        
        if self.context is None:
            raise NotFound(str(worksheet) + " was not found.")
        
        self.worksheetname = worksheet
        self.year = year
        self.semester = semester

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

        if not self.context:
            raise NotFound()

        ctx['subject'] = self.context.offering.subject.code
        ctx['worksheet'] = self.worksheetname
        ctx['semester'] = self.semester
        ctx['year'] = self.year
        ctx['worksheetstream'] = genshi.Stream(list(genshi.XML(self.context.data)))

        generate_worksheet_data(ctx, req, self.context)

        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 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':
                # XXX: Note that we presume ctx['exercises'] has a correct list
                #      of exercises. If it doesn't, something has gone wrong.
                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, worksheet):
    """Runs through the worksheetstream, generating the exericises"""
    ctx['exercises'] = []
    ctx['exerciselist'] = []
    for kind, data, pos in ctx['worksheetstream']:
        if kind is genshi.core.START:
            if data[0] == 'exercise':
                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)
                if src != "":
                    ctx['exercises'].append(present_exercise(req, src, worksheet))
                    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()

def present_exercise(req, src, worksheet):
    """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()

    worksheet_exercise = req.store.find(WorksheetExercise,
        WorksheetExercise.worksheet_id == worksheet.id,
        WorksheetExercise.exercise_id == src).one()

    if worksheet_exercise is None:
        raise NotFound()

    # Retrieve the exercise details from the database
    exercise = req.store.find(Exercise, 
        Exercise.id == worksheet_exercise.exercise_id).one()

    if exercise is None:
        raise NotFound(exercisesrc)

    # 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
    curctx['exercise'] = exercise
    if exercise.description is not None:
        desc = rstfunc(exercise.description)
        curctx['description'] = genshi.XML('<div id="description">' + desc + 
                                           '</div>')
    else:
        curctx['description'] = None

    # If the user has already saved some text for this problem, or submitted
    # an attempt, then use that text instead of the supplied "partial".
    # Get exercise stored text will return a save, or the most recent attempt,
    # whichever is more recent
    save = ivle.worksheet.get_exercise_stored_text(
                        req.store, req.user, worksheet_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, 
                                               worksheet_exercise)
    if save is not None:
        curctx['exercisesave'] = save.text
    else:
        curctx['exercisesave']= exercise.partial
    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': exercise.name,
            'complete': curctx['complete_class'],
            'stream': ex_stream,
            'exid': exercise.id}

class OfferingAdminView(XHTMLView):
    """The admin view for an Offering.
    
    This class is designed to check the user has admin privileges, and
    then allow them to edit the RST for the offering, which controls which
    worksheets are actually displayed on the page."""
    pass

class WorksheetEditView(XHTMLView):
    """The admin view for an offering.
    
    This view is designed to replace worksheets.xml, turning them instead
    into XML directly from RST."""
    permission = "edit"
    template = "worksheet_edit.html"
    appname = "Edit Worksheet"

    def __init__(self, req, **kwargs):
    
        subject = kwargs['subject']
        year = kwargs['year']
        semester = kwargs['semester']
        worksheet = kwargs['worksheet']
        self.context = req.store.find(DBWorksheet,
            DBWorksheet.identifier == worksheet,
            DBWorksheet.offering_id == Offering.id,
            Offering.semester_id == Semester.id,
            Semester.year == year,
            Semester.semester == semester,
            Offering.subject_id == Subject.id,
            Subject.code == subject
        ).one()
        
        if self.context is None:
            raise NotFound()
        
        self.subject = subject
        self.year = year
        self.semester = semester
        self.worksheet = worksheet
        
            
    def populate(self, req, ctx):
        self.plugin_styles[Plugin] = ["tutorial_admin.css"]
        self.plugin_scripts[Plugin] = ['tutorial_admin.js']
        
        ctx['worksheet'] = self.context
        ctx['worksheetname'] = self.worksheet
        ctx['subject'] = self.subject
        ctx['year'] = self.year
        ctx['semester'] = self.semester


class WorksheetAddView(XHTMLView):
    """This view allows a user to add a worksheet"""
    permission = "edit"
    template = "worksheet_add.html"
    appname = "Add Worksheet"

    def __init__(self, req, subject, year, semester):
        self.context = req.store.find(Offering,
            Offering.semester_id == Semester.id,
            Semester.year == year,
            Semester.semester == semester,
            Offering.subject_id == Subject.id,
            Subject.code == subject
        ).one()
        
        self.subject = subject
        self.year = year
        self.semester = semester
        
        if self.context is None:
            raise NotFound()
            
    def populate(self, req, ctx):
        self.plugin_styles[Plugin] = ["tutorial_admin.css"]
        self.plugin_scripts[Plugin] = ['tutorial_admin.js']
        
        ctx['subject'] = self.subject
        ctx['year'] = self.year
        ctx['semester'] = self.semester
        
        #XXX: Get the list of formats from somewhere else
        ctx['formats'] = ['xml', 'rst']


class Plugin(ViewPlugin, MediaPlugin):
    urls = [
        ('subjects/:subject/:year/:semester/+worksheets', OfferingView),
        ('subjects/:subject/:year/:semester/+worksheets/+add', WorksheetAddView),
        ('subjects/:subject/+worksheets/+media/*(path)', SubjectMediaView),
        ('subjects/:subject/:year/:semester/+worksheets/:worksheet', WorksheetView),
        ('subjects/:subject/:year/:semester/+worksheets/:worksheet/+edit', WorksheetEditView),
        ('api/subjects/:subject/:year/:semester/+worksheets', WorksheetsRESTView),
        ('api/subjects/:subject/:year/:semester/+worksheets/:worksheet/*exercise/'
            '+attempts/:username', AttemptsRESTView),
        ('api/subjects/:subject/:year/:semester/+worksheets/:worksheet/*exercise/'
                '+attempts/:username/:date', AttemptRESTView),
        ('api/subjects/:subject/:year/:semester/+worksheets/:worksheet', WorksheetRESTView),
        ('api/subjects/:subject/:year/:semester/+worksheets/:worksheet/*exercise', ExerciseRESTView),
    ]

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

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