source: trunk/ExerciseMaterial.py @ 2029

Revision 2029, 5.5 KB checked in by jukka, 12 years ago (diff)

Added method get_values_from_fitbs which finds the correct answers for fill-in-the-blanks-chapter and returns them as dictionary. Dictionary has answers with keys 'exercise_x_answers_y', and the results are lists, as there can be many good answers. (Pjotr: Evaluating student's response should use this method.)

Line 
1# Copyright 2006 by the LeMill Team (see AUTHORS)
2#
3# This file is part of LeMill.
4#
5# LeMill is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# LeMill is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with LeMill; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1
18
19from AccessControl import ClassSecurityInfo, getSecurityManager
20from Products.Archetypes.public import *
21from Products.CMFCore.utils import getToolByName
22
23from config import PROJECTNAME, MODIFY_CONTENT, VIEW
24from FieldsWidgets import ChapterField, ChapterWidget, StringWidget
25from SharedMetadata import *
26from Material import Material
27from permissions import MODIFY_CONTENT
28from random import shuffle
29
30import re
31
32schema = BaseSchema + Schema((
33   ChapterField('bodyText',
34        accessor="getBodyText",
35        edit_accessor = 'getRawBodyText',
36        mutator = "setBodyText",
37        index='ZCTextIndex:schema',
38        index_method = 'getOnlyText',
39        searchable = True,
40        deleteEmptyChapters = False,
41        allowable_content_types = ['text/html',],
42        allow_file_upload = False,
43        default_output_type = 'text/x-html-captioned',
44        default_content_type = 'text/html',
45        default=[('','guidelines'),],
46        widget=ExerciseWidget(label = "Body text",
47            label_msgid = "label_bodytext",
48            i18n_domain = "lemill",
49            ),
50    ),
51))
52
53schema = schema + tags + language_schema + group_sharing + subject_area_schema + target_group_schema + license_schema + coverImage + lemill_metadata_mods + no_description + author_schema + deletionReason + version_schema + translation_schema + ieee_lom_lre_ap + latest_edit_schema + score
54
55schema = schema.copy()
56schema.moveField('rights', pos='bottom')
57schema.moveField('language', after='bodyText')
58schema['title'].required = True
59
60fill_in_the_blanks=re.compile(r"""(?P<filler>({.*?})+)""",  re.IGNORECASE)
61
62class ExerciseMaterial(Material):
63    """Exercise page"""
64
65    schema = schema
66
67    meta_type = "ExerciseMaterial"
68    archetype_name = "ExerciseMaterial"
69
70    security = ClassSecurityInfo()
71    security.declareObjectPublic()
72
73    security.declarePrivate('manage_afterAdd')
74    def manage_afterAdd(self, item, container):
75        Material.manage_afterAdd(self, item, container)
76
77        if not hasattr(item.aq_base, 'left_slots'):
78            self._setProperty('left_slots', ['here/portlet_material_actions/macros/portlet',], 'lines')
79        else:
80            self._updateProperty('left_slots', ['here/portlet_material_actions/macros/portlet',])
81
82    def getOnlyText(self):
83        field=self.getField('bodyText')
84        values = field.get(self)
85        dump= '\n'.join([x[0] for x in values if not x[1]!='piece'])
86        return dump
87
88    def isCorrectAnswer(self, chapter, answer_index):
89        """ given a chapter having a multiple choice question, is this answer in correct answers? """
90        return answer_index < len(chapter[1])
91       
92
93    security.declareProtected(MODIFY_CONTENT,'delChapter')
94    def delChapter(self, REQUEST):
95        """ delete chapter """
96        field = self.getField('bodyText')
97        field.delChapter(self, int(REQUEST.get('delete')))
98        return REQUEST.RESPONSE.redirect(self.absolute_url()+'/edit')
99
100    def getAllAnswers(self, chapter):
101        """ Will return the list of the shuffled combined correct and incorrect answers """
102        # Chapter should have the structure: [question,correct_list,incorrect_list]
103        all_answers = chapter[1] + chapter[2]
104        extended_answers = []
105        for numerated_answer in enumerate(all_answers):
106            extended_answers.append(numerated_answer)
107        shuffle(extended_answers)
108        return extended_answers
109
110    def get_values_from_fitbs(self, chapter_index):
111        """ find words in {}:s and return them as dict """
112       
113        text=self.getBodyText()
114        text=text[chapter_index][0]
115        matches=fill_in_the_blanks.findall(text)
116        results={}
117        index=0
118        print matches
119        for m in matches:
120            print m
121            answer=m[0]
122            answer=answer.strip(' {}')
123            answer=answer.split('}{')
124            print answer
125            results['exercise_%s_answer_%s' % (chapter_index, index)]=answer
126            index=index+1
127        print results
128        return results
129           
130
131#    def replace_blanks_with_textfields(self, instance, field, chapter_index, show_answer=True):
132#        """ find words in { } and replace them with input boxes """
133#
134#        replacement="""<input type="text" value="%s" name="exercise_%s_answer_%s" id="exercise_%s_answer_%s" /> """
135#        self.iterator=0
136#       
137#        def rep(match):
138#            value=match.group()
139#            answer_index=self.iterator
140#            if not show_answer:
141#                value=self.get
142#            else:
143#                value=""
144#            self.iterator=self.iterator+1
145#            return replacement % (value, chapter_index, answer_index, chapter_index, answer_index)
146#
147#        text=fill_in_the_blanks.sub(rep, fill_in_the_blanks)
148#        del self.iterator
149#        return text
150
151
152
153
154registerType(ExerciseMaterial, PROJECTNAME)
Note: See TracBrowser for help on using the repository browser.