Ignore:
Timestamp:
09/19/07 00:38:53 (12 years ago)
Author:
jukka
Message:

Worked on exerciseMaterial, e-mail body is now fine correct and the translation strings should come out automatically. Redirecting after message sending is half-broken, messages seem not to appear.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ExerciseMaterial.py

    r2035 r2036  
    2121from Products.CMFCore.utils import getToolByName 
    2222 
    23 from config import PROJECTNAME, MODIFY_CONTENT, VIEW 
     23from config import PROJECTNAME, MODIFY_CONTENT, VIEW, to_unicode 
    2424from FieldsWidgets import ChapterField, ChapterWidget, StringWidget 
    2525from SharedMetadata import * 
     
    137137            else: 
    138138                value="" 
    139             self.iterator=self.iterator+1 
     139            self.iterator+=1 
    140140            return replacement % (value, readonly, chapter_index, answer_index, chapter_index, answer_index) 
    141141 
     
    150150        return text 
    151151 
     152    def replace_blanks_with_your_answers(self, text, your_answerdict, i): 
     153        """ find braces {text} and add own answers before them in brackets [mytext]{text} """ 
     154        self.iterator=0 
     155         
     156        def rep(match): 
     157            value=match.group('filler')             
     158            your_answer=your_answerdict.get('exercise_%s_answer_%s' % (i, self.iterator), '') 
     159            self.iterator+=1  
     160            return '[%s]%s' % (your_answer, value)  
     161        text=fill_in_the_blanks.sub(rep, text) 
     162        del self.iterator 
     163        return text 
     164 
     165 
     166 
    152167    def sendAnswers(self, REQUEST): 
    153168        """ Send e-mail to a teacher """ 
    154169        putils = getToolByName(self,'plone_utils') 
    155170        mhost = putils.getMailHost() 
    156         message = '' 
    157         students_name = '' 
    158         students_email = '' 
    159         teachers_email = '' 
    160         if REQUEST.has_key('your_name'): 
    161             students_name = REQUEST.get('your_name') 
    162         if REQUEST.has_key('students_email'): 
    163             students_email = REQUEST.get('students_email') 
    164         if REQUEST.has_key('teachers_email'): 
    165             teachers_email = REQUEST.get('teachers_email') 
    166         if not teachers_email or not students_email or not students_name: 
     171        mbody = u'' 
     172        students_name = REQUEST.get('your_name','') 
     173        students_email = REQUEST.get('students_email','') 
     174        teachers_email = REQUEST.get('teachers_email','') 
     175 
     176        if not students_email or (teachers_email and not students_name): 
    167177            msg = _(u"You have not provided enough information to send an e-mail.") 
    168178            putils.addPortalMessage(msg) 
    169179            return REQUEST.RESPONSE.redirect(self.absolute_url()) 
    170         # Now we should compose the message body 
    171180        exercise_body = self.getBodyText() 
    172         message += "This is an answer of %s to the exercise %s at %s\n\n\n" % (students_name, self.Title(), self.absolute_url()) 
    173         # XXX Message body should be composed question by question 
    174         for i in range(1,len(exercise_body)): 
    175             if exercise_body[i][1] == 'multiple_choices': 
    176                 answered_right = [] 
    177                 answered_wrong = [] 
    178                 question_text = exercise_body[i][0][0] 
    179                 correct_answers = exercise_body[i][0][1] 
    180                 all_answers = exercise_body[i][0][1] + exercise_body[i][0][2] 
    181                 for a in all_answers: 
    182                     answer_value = REQUEST.get('bodyText_'+str(i)+'_'+str(a)) 
    183                     if answer_value == 'checked' and a in correct_answers: 
    184                         answered_right.append(a) 
    185                     elif answer_value == 'checked' and a not in correct_answers: 
    186                         answered_wrong.append(a) 
    187                 message += "\n\n\n%s\n\nAnswered right: %s out of %s.\nRight answers given: %s. Wrong answers given: %s." % (question_text, len(answered_right), len(all_answers), ','.join(answered_right), ','.join(answered_wrong)) 
    188             elif exercise_body[i][1] == 'fill_in_the_blanks': 
    189                 # XXX Do some stuff 
    190                 print "Lalala" 
    191             elif exercise_body[i][1] == 'open_ended': 
    192                 question_text = exercise_body[i][0] 
    193                 students_answer = REQUEST.get('bodyText_'+str(i)) 
    194                 message += "\n\n\n%s\n\n%s" % (question_text, students_answer) 
    195         message += "\n\n\nBest regards,\nLeMill" 
    196          
    197         message_from = "%s <%s>" % (students_name, students_email) 
    198         message_subject = "Answers to exercise %s by %s" % (self.Title(), students_name) 
    199         try: 
    200             mhost.send(message, mto=teachers_email, mfrom=message_from, subject=message_subject) 
    201             msg = _(u"The e-mail has been sent to a teacher.") 
    202             putils.addPortalMessage(msg) 
    203             return REQUEST.RESPONSE.redirect(self.absolute_url()) 
    204         except: 
    205             msg = _(u"The e-mail could not be sent.") 
    206             putils.addPortalMessage(msg) 
    207             return REQUEST.RESPONSE.redirect(self.absolute_url()) 
     181 
     182 
     183        # Now we should compose the message body         
     184        if teachers_email: 
     185            mbody += _(u""" 
     186Hello, 
     187 
     188%(name)s has completed the LeMill exercise '%(exercise_title)s' at %(exercise_url)s. 
     189""") % {'name':to_unicode(students_name), 'exercise_title':to_unicode(self.Title()), 'exercise_url':to_unicode(self.absolute_url())}  
     190        else: 
     191            mbody += _(u""" 
     192Hello %(name)s, 
     193 
     194You have completed the LeMill exercise '%(exercise_title)s' at %(exercise_url)s. 
     195""") % {'name':to_unicode(students_name), 'exercise_title':to_unicode(self.Title()), 'exercise_url':to_unicode(self.absolute_url())} 
     196 
     197        mbody += _(u"If you are surprised by getting this e-mail, you can simply ignore and delete it as someone may have accidentally inserted your address as a recipient.") 
     198        # Message body is composed chapter by chapter 
     199        i=0 
     200        for (chapter_text, chapter_type) in exercise_body: 
     201            if chapter_type=='multiple_choices': 
     202                answered_right = 0 
     203                answered_wrong = 0 
     204                question = chapter_text[0] 
     205                correct_answers = chapter_text[1] 
     206                answers_total = correct_answers+chapter_text[2] 
     207                my_answers=[] 
     208                # Correct answers have small index number 
     209                for a in range(len(correct_answers)): 
     210                    answer_value = REQUEST.get('exercise_%s_checkbox_%s' % (i,a),'') 
     211                    if answer_value: 
     212                        my_answers.append(answers_total[a]) 
     213                        answered_right+=1 
     214                    else: 
     215                        answered_wrong+=1 
     216                # These are all wrong answers 
     217                for a in range(len(correct_answers),len(answers_total)): 
     218                    answer_value = REQUEST.get('exercise_%s_checkbox_%s' % (i,a),'') 
     219                    if answer_value: 
     220                        my_answers.append(answers_total[a]) 
     221                        answered_wrong+=1 
     222                    else: 
     223                        answered_right+=1 
     224                percentage=int((float(answered_right)/(answered_wrong+answered_right))*100) 
     225                mbody += _(u""" 
     226 
     227Your answer to question '%(question)s' was: 
     228%(my_answers)s 
     229The correct answer is: 
     230%(correct_answers)s 
     231You got %(percentage)s%% correct! 
     232""") % {'question':question, 'my_answers':to_unicode(', '.join(my_answers)), 'correct_answers':to_unicode(', '.join(correct_answers)), 'percentage':percentage} 
     233 
     234            elif chapter_type == 'fill_in_the_blanks': 
     235                correct_answer_dict=self.get_values_from_fitbs(i) 
     236                correct_answers_n=0 
     237                my_answer_dict={} 
     238                all_answers_n=len(correct_answer_dict.keys()) 
     239                for it in range(all_answers_n): 
     240                    my_answer=REQUEST.get('exercise_%s_answer_%s' % (i,it),'') 
     241                    correct_answer=[to_unicode(c.strip()) for c in correct_answer_dict['exercise_%s_answer_%s' % (i,it)]] 
     242                    if to_unicode(my_answer.strip()) in correct_answer: 
     243                        correct_answers_n+=1 
     244                    my_answer_dict['exercise_%s_answer_%s' % (i,it)]=my_answer 
     245                exercise_text=self.replace_blanks_with_your_answers(chapter_text, my_answer_dict, i) 
     246                mbody += _(u""" 
     247In the following fill-in-the-blanks exercise your answers and correct answers were: 
     248%(exercise_text)s 
     249 
     250You got %(correct_answers_n)s/%(all_answers_n)s correct! 
     251""") % {'exercise_text':to_unicode(exercise_text), 'correct_answers_n':correct_answers_n, 'all_answers_n':all_answers_n}             
     252 
     253            elif chapter_type == 'open_ended': 
     254                students_answer = REQUEST.get('exercise_%s_answer' % i, '') 
     255                mbody += _(u""" 
     256Your answer to question '%(question)s' was:  
     257%(answer)s 
     258 
     259Your teacher will give you feedback on your answer. 
     260""") % {'question':to_unicode(chapter_text), 'answer':to_unicode(students_answer)} 
     261            i+=1 # <--- remember to increase index 
     262             
     263        mbody += _(u"\n\nBest regards, LeMill") 
     264         
     265        #message_from = "%s <%s>" % (students_name, students_email) 
     266        #message_subject = "Answers to exercise %s by %s" % (self.Title(), students_name) 
     267#        try: 
     268#            mhost.send(message, mto=teachers_email, mfrom=message_from, subject=message_subject) 
     269        msg = _(u"The e-mail has been sent to a teacher.") 
     270        putils.addPortalMessage(msg) 
     271        return REQUEST.RESPONSE.redirect(self.absolute_url()) 
     272#        except: 
     273#            msg = _(u"The e-mail could not be sent.") 
     274#            putils.addPortalMessage(msg) 
     275#            return REQUEST.RESPONSE.redirect(self.absolute_url()) 
    208276 
    209277 
Note: See TracChangeset for help on using the changeset viewer.