2017-08-24 48 views
0

我正在编辑odoo中的调查模块。 当我回答由页面组成的调查,然后点击提交调查时,它会计算调查的整个得分并显示它。我想要的是它计算每个页面的得分并在点击提交调查时显示它,例如:Odoo:对调查模块进行更改

它显示:Your score is: 200 points
我希望它显示:Score of first page : 20 pointsSecond page: 50 points等等...

这里是我想改变的代码,但我不知道我应该怎么改变这个功能。

questionnaire.py(survey.py):类survey_user_input

class survey_user_input(osv.Model): 
''' Metadata for a set of one user's answers to a particular survey ''' 
_name = "questionnaire.user_input" 
_rec_name = 'date_create' 
_description = 'Survey User Input' 

def _quizz_get_score(self, cr, uid, ids, name, args, context=None): 
    ret = dict() 
    for user_input in self.browse(cr, uid, ids, context=context): 
     ret[user_input.id] = sum([uil.quizz_mark for uil in user_input.user_input_line_ids] or [0.0]) 
    return ret 

_columns = { 
    'survey_id': fields.many2one('questionnaire.questionnaire', 'Questionnaire', required=True, 
           readonly=1, ondelete='restrict'), 
    'date_create': fields.datetime('Date de creation', required=True, 
            readonly=1, copy=False), 
    'deadline': fields.datetime("Date limite", 
          oldname="date_deadline"), 
    'type': fields.selection([('manually', 'Manuellement'), ('link', 'Lien')], 
          'Type de reponse', required=1, readonly=1, 
          oldname="response_type"), 
    'state': fields.selection([('new', 'Pas encore commence'), 
           ('skip', 'Partiellement acheve'), 
           ('done', 'Termine')], 
           'Statut', 
           readonly=True), 
    'test_entry': fields.boolean('Entree de test', readonly=1), 
    'token': fields.char("Piece d'identite", readonly=1, required=1, copy=False), 

    # Optional Identification data 
    'partner_id': fields.many2one('res.partner', 'Partenaire', readonly=1), 
    'email': fields.char("E-mail", readonly=1), 

    # Displaying data 
    'last_displayed_page_id': fields.many2one('questionnaire.page', 
              'Derniere page affichee'), 
    # The answers ! 
    'user_input_line_ids': fields.one2many('questionnaire.user_input_line', 
              'user_input_id', 'Reponses', copy=True), 

    # URLs used to display the answers 
    'result_url': fields.related('survey_id', 'result_url', type='char', 
           string="Lien public aux resultats du sondage"), 
    'print_url': fields.related('survey_id', 'print_url', type='char', 
           string="Lien public au sondage vide"), 

    'quizz_score': fields.function(_quizz_get_score, type="float", string="Score pour le quiz", store=True) 
} 
_defaults = { 
    'date_create': fields.datetime.now, 
    'type': 'manually', 
    'state': 'new', 
    'token': lambda s, cr, uid, c: uuid.uuid4().__str__(), 
    'quizz_score': 0.0, 
} 

questionnaire.py(survey.py):类survey_user_input_line

_name = 'questionnaire.user_input_line' 
_description = 'Survey User Input Line' 
_rec_name = 'date_create' 
_columns = { 
    'user_input_id': fields.many2one('questionnaire.user_input', 'Entree de l\'utilisateur', 
            ondelete='cascade', required=1), 
    'question_id': fields.many2one('questionnaire.question', 'Question', 
            ondelete='restrict', required=1), 
    'page_id': fields.related('question_id', 'page_id', type='many2one', 
           relation='questionnaire.page', string="Page"), 
    'survey_id': fields.related('user_input_id', 'survey_id', 
           type="many2one", relation="questionnaire.questionnaire", 
           string='Questionnaire', store=True), 
    'date_create': fields.datetime('Date de creation', required=1), 
    'skipped': fields.boolean('Ignore'), 
    'answer_type': fields.selection([('text', 'Texte'), 
            ('number', 'Nombre'), 
            ('date', 'Date'), 
            ('free_text', 'Texte Libre'), 
            ('suggestion', 'Suggestion')], 
            'Type de reponse'), 
    'value_text': fields.char("Reponse texte"), 
    'value_number': fields.float("Reponse numerique"), 
    'value_date': fields.datetime("Reponse date"), 
    'value_free_text': fields.text("Reponse texte libre"), 
    'value_suggested': fields.many2one('questionnaire.label', "Reponse suggeree"), 
    'value_suggested_row': fields.many2one('questionnaire.label', "Reponse en ligne"), 
    'quizz_mark': fields.float("Score donne pour ce choix") 
} 

survey_templates.xml

<!-- "Thank you" message when the survey is completed --> 
<template id="sfinished" name="Survey Finished"> 
    <t t-call="website.layout"> 
     <div class="wrap"> 
      <div class="container"> 
       <t t-call="questionnaire.back" /> 
       <div class="jumbotron mt32"> 
        <h1>Thank you!</h1> 
        <div t-field="questionnaire.thank_you_message" class="oe_no_empty" /> 
        <div> You scored <t t-esc="user_input.quizz_score" /> points.</div> 
        <div>If you want you can <a t-att-href="'/questionnaire/print/%s/%s' % (slug(questionnaire), token)">review your answers</a>.</div> 
       </div> 
      </div> 
     </div> 
    </t> 
</template> 

回答

0

另一种方法是使用纯qweb而不是功能字段;像这样:

<!-- First finding which page is related to the survey. This is a bit odd but I couldn't find any other way!! --> 
<t t-set="pages" t-value="dict((l.id, l.page_id.id) for l in user_input.user_input_line_ids).values()" /> 

<!-- Then print the score per each page --> 
<t t-foreach='pages' t-as='p'> 
    <t t-set="page_score" t-value="sum([(uil.quizz_mark or 0.0) if uil.page_id.id == p else 0.0 for uil in user_input.user_input_line_ids])" /> 
    <div> You scored <t t-esc="page_score" /> points in page <t t-esc="p" />.</div> 
</t> 

我还没有测试过这个代码,但逻辑上它似乎工作正常。

0

谢谢你这么多@MICROCOM ..这显示每个页面的分数diplayed不止一次行..

The result Of The code - 1

所以我加了一个条件,只是显示了一定的成绩页面一次(我不知道这是否是正确的,但它的工作原理:p)。这里是代码:

<!-- First finding which page is related to the survey. This is a bit odd but I couldn't find any other way!! --> 
        <t t-set="pages" t-value="dict((l.id, l.page_id.title) for l in user_input.user_input_line_ids).values()" /> 
        <!-- Then print the score per each page --> 
        <t t-set="previous" t-value="void" /> 
        <t t-foreach='pages' t-as='p'> 
         <t t-if="p != previous"> 
         <t t-set="page_score" t-value="sum([(uil.quizz_mark or 0.0) if uil.page_id.title == p else 0.0 for uil in user_input.user_input_line_ids])" /> 
         <div> You scored <t t-esc="page_score" /> points in page <t t-esc="p" />.</div> 
         </t> 
         <t t-set="previous" t-value="p" /> 
        </t> 

The result Of The code - 2

非常感谢你的帮助@MICROCOM ^^

+0

很高兴我能帮到你。但我认为重复的事情是因为第一行。也许下面一行会以更简单的方式修复它: 干杯。 – MICROCOM

+0

它的工作原理:D ..非常感谢你@MICROCOM .. – LatifaShi

+0

你完全欢迎:)顺便说一句,你可以通过点击答案旁边的复选标记标记正确的答案为其他人接受。更多信息,请访问:https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – MICROCOM