Attachment 'quiz_score.py'

Download

   1 import re
   2 from MoinMoin import config, wikiutil
   3 from MoinMoin.Page import Page
   4 
   5 dependencies = []
   6 
   7 class QuizScore:
   8     def __init__(self, macro, args):
   9         self.macro = macro
  10         self.pagename = macro.request.page.page_name
  11         self.request = macro.request
  12         self.formatter = macro.formatter
  13         self.args = args
  14         self.arglist = args.split(',')
  15 	self.counters = {}
  16         if len(self.arglist)<1:
  17             self.base_page_name = self.pagename;
  18         else:
  19             self.base_page_name = self.arglist[0]
  20         if len(self.arglist)<2:
  21             self.answers_page_name = macro.request.user.name;
  22         else:
  23             self.answers_page_name = self.arglist[1]
  24 
  25     def get_answer_pages(self):
  26         isgroup = re.compile(self.request.cfg.page_group_regex, re.UNICODE).search
  27         pages = self.request.rootpage.getPageList(user='', exists='')
  28         matches = []
  29         for name in pages:
  30 	    p = Page(self.request, name)
  31             if name.startswith(self.base_page_name+'/') and p.exists() and self.request.user.may.read(name):
  32 		    if not isgroup(name): 
  33 			matches.append(name)
  34         return matches
  35     
  36     def get_answers(self, name):
  37         p = Page(self.request, name)
  38         text = p.get_raw_body()
  39 	if not text:
  40 		return {}
  41         answers = {}
  42         question = ''
  43         answers[''] = set()
  44         regex = re.compile(r'^\s*(?P<question>.*)::|^\s*\*\s*(?P<answer>.*)|^\#.*$|^\s*$')
  45         for line in text.split('\n'):
  46             m = regex.match(line)
  47             if m:
  48                 keys = m.groupdict()
  49                 if keys.has_key('question') and keys['question']:
  50                     question = keys['question'].strip()
  51                     if not answers.has_key(question) and question!='counter':
  52                         answers[question] = set()
  53                 elif keys.has_key('answer') and keys['answer']:
  54 		    answer = keys['answer'].strip()
  55 		    if question == 'counter' and name==self.base_page_name+'/'+self.answers_page_name:
  56 			    if not self.counters.has_key(answer):
  57 				    self.counters[answer] = 0
  58 			    self.counters[answer]+=1
  59 		    elif question!='counter':
  60 			    answers[question].add(answer)
  61         return answers
  62     
  63     def clean_name(self,name):
  64         return name[len(self.base_page_name):]
  65 
  66     def score_answers(self, answer1, answer2, question):
  67 	equal = answer1==answer2
  68 	if self.counters.has_key(question):
  69 		score = self.counters[question] - len(answer1-answer2) - len(answer2-answer1)
  70 	else:
  71 		score = -1 
  72         return equal, score
  73 
  74 
  75     def execute(self):
  76         _ = self.request.getText
  77         answer_pages = self.get_answer_pages()
  78         user_answers = {}
  79         for page_name in answer_pages:
  80             user_answers[page_name] = self.get_answers(page_name)
  81         html = self.formatter.rawHTML('<table class="quizscore">')
  82         html+=self.formatter.rawHTML('<tr class="head">')
  83 	if not user_answers.has_key(self.base_page_name+'/'+self.answers_page_name):
  84 		return self.formatter.text(_(u'Score not available yet.'))
  85         correct = user_answers[self.base_page_name+'/'+self.answers_page_name]
  86         questions = [x for (x,y) in correct.iteritems()]
  87         questions.remove('')
  88         questions.sort()
  89         html+=self.formatter.rawHTML('<td>')
  90         html+=self.formatter.text(_('Question'))
  91         html+=self.formatter.rawHTML('</td>')
  92         for question in questions:
  93                 html+=self.formatter.rawHTML('<td>')
  94                 html+=self.formatter.text(question)
  95                 html+=self.formatter.rawHTML('</td>')
  96         html+=self.formatter.rawHTML('<td>')
  97         html+=self.formatter.text(_('Total'))
  98         html+=self.formatter.rawHTML('</td>')
  99         html+=self.formatter.rawHTML('</tr>')
 100         l = [x for x in user_answers.iteritems()];
 101         l.sort();
 102         for user, answers in l:
 103             if user:
 104                 row = ''
 105                 row+=self.formatter.rawHTML('<td>')
 106                 row+=self.formatter.pagelink(None, pagename=user, text=self.clean_name(user))
 107                 row+=self.formatter.rawHTML('</td>')
 108                 sum = 0;
 109 		small_sum = 0;
 110                 for question in questions:
 111                     row+=self.formatter.rawHTML('<td>')
 112                     if answers.has_key(question):
 113                         ok,small = self.score_answers(correct[question], answers[question], question)
 114 			if ok:
 115                             row+=self.formatter.text('1')
 116                             sum += 1
 117                         else:
 118                             row+=self.formatter.text('0')
 119 			if small>=0:
 120 				row+=self.formatter.rawHTML('<small>')
 121 				row+=self.formatter.text(' (')
 122 				row+=self.formatter.text(str(small))
 123 				row+=self.formatter.text(')')
 124 				row+=self.formatter.rawHTML('</small>')
 125 				small_sum += small
 126                     else:
 127                         row+=self.formatter.text('-')
 128                     row+=self.formatter.rawHTML('</td>')
 129                 row+=self.formatter.rawHTML('<td>')
 130                 row+=self.formatter.text(str(sum))
 131 		row+=self.formatter.rawHTML('<small>')
 132                 row+=self.formatter.text(' (')
 133                 row+=self.formatter.text(str(small_sum))
 134                 row+=self.formatter.text(')')
 135 		row+=self.formatter.rawHTML('</small>')
 136                 row+=self.formatter.rawHTML('</td>')
 137 		if sum == len(questions):
 138 			score='perfect'
 139 		elif sum == 0:
 140 			score='failed'
 141 		elif user==self.base_page_name+'/'+self.answers_page_name:
 142 			score='author'
 143 		else:
 144 			score = 'normal'
 145                 html+=self.formatter.rawHTML('<tr class="score%s" >' % score)
 146                 html+=row
 147                 html+=self.formatter.rawHTML('</tr>')
 148         html+=self.formatter.rawHTML('</table>')
 149         return html
 150                 
 151             
 152 
 153 # Glue for MoinMoin 1.3.5
 154 def execute(macro, args):
 155     return QuizScore(macro, args).execute()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2008-09-08 12:05:39, 0.1 KB) [[attachment:quiz_I.py]]
  • [get | view] (2008-09-08 12:05:20, 0.3 KB) [[attachment:quiz_O.py]]
  • [get | view] (2008-09-08 12:04:20, 0.3 KB) [[attachment:quiz_X.py]]
  • [get | view] (2008-09-08 12:03:39, 1.7 KB) [[attachment:quiz_action.py]]
  • [get | view] (2008-09-08 12:04:00, 7.2 KB) [[attachment:quiz_parser.py]]
  • [get | view] (2008-09-08 12:06:01, 5.8 KB) [[attachment:quiz_score.py]]
  • [get | view] (2008-09-08 12:06:19, 6.2 KB) [[attachment:test_score.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.