source: trunk/ExerciseMaterial.py @ 2033

Revision 2033, 7.8 KB checked in by pjotr, 13 years ago (diff)

Renamed the method and did the open_ended question

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        for m in matches:
119            answer=m[0]
120            answer=answer.strip(' {}')
121            answer=answer.split('}{')
122            results['exercise_%s_answer_%s' % (chapter_index, index)]=answer
123            index=index+1
124        return results
125           
126    def replace_blanks_with_input_tag(self, chapter_index, answers=True, readonly=True):
127        """ find words in { } and replace them with input boxes """
128        replacement="""<input type="text" value="%s"%s name="exercise_%s_answer_%s" id="exercise_%s_answer_%s" />"""
129        self.iterator=0
130       
131        def rep(match):
132            answer_index=self.iterator
133            if answers:
134                value=match.group('filler').strip(' {}')
135                value=value.replace('}{','/')
136            else:
137                value=""
138            self.iterator=self.iterator+1
139            return replacement % (value, readonly, chapter_index, answer_index, chapter_index, answer_index)
140
141        if readonly:
142            readonly=' readonly="1"'
143        else:
144            readonly=''           
145        text=self.getBodyText()
146        text=text[chapter_index][0]
147        text=fill_in_the_blanks.sub(rep, text)
148        del self.iterator
149        return text
150
151    def sendAnswers(self, REQUEST):
152        """ Send e-mail to a teacher """
153        putils = getToolByName(self,'plone_utils')
154        mhost = putils.getMailHost()
155        message = ''
156        students_name = ''
157        students_email = ''
158        teachers_email = ''
159        if REQUEST.has_key('your_name'):
160            students_name = REQUEST.get('your_name')
161        if REQUEST.has_key('students_email'):
162            students_email = REQUEST.get('students_email')
163        if REQUEST.has_key('teachers_email'):
164            teachers_email = REQUEST.get('teachers_email')
165        if not teachers_email or not students_email or not students_name:
166            msg = _(u"You have not provided enough information to send an e-mail.")
167            putils.addPortalMessage(msg)
168            return REQUEST.RESPONSE.redirect(self.absolute_url())
169        # Now we should compose the message body
170        exercise_body = self.getBodyText()
171        message += "This is an answer of %s to the exercise %s at %s\n\n\n" % (students_name, self.Title(), self.absolute_url())
172        # XXX Message body should be composed question by question
173        for i in range(1,len(exercise_body)):
174            if exercise_body[i][1] == 'multiple_choices':
175                # XXX Do some stuff
176            elif exercise_body[i][1] == 'fill_in_the_blanks':
177                # XXX Do some stuff
178            elif exercise_body[i][1] == 'open_ended':
179                question_text = exercise_body[i][0]
180                students_answer = REQUEST.get('bodyText_'+i)
181                message += "\n\n\n %s \n\n %s" % (question_text, students_answer)
182        message += "\n\n\nBest regards,\nLeMill"
183       
184        message_from = "%s <%s>" % (students_name, students_email)
185        message_subject = "Answers to exercise %s by %s" % (self.Title(), students_name)
186        try:
187            mhost.send(message, mto=teachers_email, mfrom=message_from, subject=message_subject)
188            msg = _(u"The e-mail has been sent to a teacher.")
189            putils.addPortalMessage(msg)
190            return REQUEST.RESPONSE.redirect(self.absolute_url())
191        except:
192            msg = _(u"The e-mail could not be sent.")
193            putils.addPortalMessage(msg)
194            return REQUEST.RESPONSE.redirect(self.absolute_url())
195
196
197
198
199registerType(ExerciseMaterial, PROJECTNAME)
Note: See TracBrowser for help on using the repository browser.