6e26f21e5b3f62ad2e210938b89ce1fef05215a3
[odoo/odoo.git] / addons / survey / report / survey_browse_response.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import pooler
24 from report.interface import report_rml
25 from tools import to_xml
26 import tools
27 import time
28
29 class survey_browse_response(report_rml):
30     def create(self, cr, uid, ids, datas, context):
31         rml ="""<document filename="Survey Analysis Report.pdf">
32                 <template pageSize="(595.0,842.0)" title="Test" author="Martin Simon" allowSplitting="20">
33                     <pageTemplate id="first">
34                       <frame id="first" x1="57.0" y1="57.0" width="481" height="728"/>
35                 </pageTemplate>
36                   </template>
37                   <stylesheet>
38                     <blockTableStyle id="ans_tbl_white">
39                       <blockAlignment value="LEFT"/>
40                       <blockValign value="TOP"/>
41                       <lineStyle kind="LINEBELOW" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
42                     </blockTableStyle>
43                     <blockTableStyle id="ans_tbl_gainsboro">
44                       <blockAlignment value="LEFT"/>
45                       <blockValign value="TOP"/>
46                       <lineStyle kind="LINEBELOW" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
47                       <blockBackground colorName="gainsboro" start="0,0" stop="-1,-1"/>
48                     </blockTableStyle>
49                     <blockTableStyle id="Table1">
50                       <blockAlignment value="LEFT"/>
51                       <blockValign value="TOP"/>
52                       <lineStyle kind="LINEBELOW" colorName="#e6e6e6"/>
53                     </blockTableStyle>
54                     <blockTableStyle id="Table2">
55                       <blockAlignment value="LEFT"/>
56                       <blockValign value="TOP"/>
57                     </blockTableStyle>
58                     <blockTableStyle id="Table3">
59                       <blockAlignment value="LEFT"/>
60                       <lineStyle kind="LINEBELOW" colorName="#e6e6e6" start="1,0" stop="2,-1"/>
61                       <blockValign value="TOP"/>
62                     </blockTableStyle>
63                     <blockTableStyle id="Table4">
64                       <blockAlignment value="LEFT"/>
65                       <blockValign value="TOP"/>
66                       <lineStyle kind="LINEBELOW" colorName="#000000" start="0,-1" stop="1,-1"/>
67                     </blockTableStyle>
68                     <blockTableStyle id="Table5">
69                       <blockAlignment value="LEFT"/>
70                       <blockValign value="TOP"/>
71                       <lineStyle kind="LINEBELOW" colorName="#8f8f8f" start="0,-1" stop="1,-1"/>
72                     </blockTableStyle>
73                     <blockTableStyle id="Table41">
74                       <blockAlignment value="LEFT"/>
75                       <blockValign value="TOP"/>
76                       <lineStyle kind="LINEBELOW" colorName="#000000" start="0,0" stop="-1,-1"/>
77                       <lineStyle kind="LINEBEFORE" colorName="#777777" start="0,0" stop="-1,-1"/>
78                       <lineStyle kind="LINEAFTER" colorName="#777777" start="0,0" stop="-1,-1"/>
79                     </blockTableStyle>
80                     <blockTableStyle id="Table51">
81                       <blockAlignment value="LEFT"/>
82                       <blockValign value="TOP"/>
83                       <lineStyle kind="LINEBELOW" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
84                       <lineStyle kind="LINEBEFORE" colorName="#777777" start="0,0" stop="-1,-1"/>
85                       <lineStyle kind="LINEAFTER" colorName="#777777" start="0,0" stop="-1,-1"/>
86                     </blockTableStyle>
87                     <blockTableStyle id="Table_heading">
88                       <blockAlignment value="LEFT"/>
89                       <blockValign value="TOP"/>
90                       <lineStyle kind="LINEBEFORE" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
91                       <lineStyle kind="LINEAFTER" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
92                       <lineStyle kind="LINEBELOW" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
93                       <lineStyle kind="LINEABOVE" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
94                     </blockTableStyle>
95                     <blockTableStyle id="Table_head_2">
96                       <blockAlignment value="LEFT"/>
97                       <blockValign value="TOP"/>
98                       <lineStyle kind="LINEBEFORE" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
99                       <lineStyle kind="LINEAFTER" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
100                       <lineStyle kind="LINEBELOW" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
101                       <lineStyle kind="LINEABOVE" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
102                     </blockTableStyle>
103                     <initialize>
104                       <paraStyle name="all" alignment="justify"/>
105                     </initialize>
106                     <paraStyle name="answer_right" alignment="RIGHT" fontName="helvetica" fontSize="09.0" leftIndent="2.0"/>
107                     <paraStyle name="Standard1" fontName="helvetica-bold" alignment="RIGHT" fontSize="09.0"/>
108                     <paraStyle name="Standard" alignment="LEFT" fontName="Helvetica-Bold" fontSize="11.0"/>
109                     <paraStyle name="header1" fontName="Helvetica" fontSize="11.0"/>
110                     <paraStyle name="response" fontName="Helvetica-oblique" fontSize="9.5"/>
111                     <paraStyle name="page" fontName="helvetica" fontSize="11.0" leftIndent="0.0"/>
112                     <paraStyle name="question" fontName="helvetica-boldoblique" fontSize="10.0" leftIndent="3.0"/>
113                     <paraStyle name="answer_bold" fontName="Helvetica-Bold" fontSize="09.0" leftIndent="2.0"/>
114                     <paraStyle name="answer" fontName="helvetica" fontSize="09.0" leftIndent="2.0"/>
115                     <paraStyle name="answer1" fontName="helvetica" fontSize="09.0" leftIndent="2.0"/>
116                     <paraStyle name="Title" fontName="helvetica" fontSize="20.0" leading="15" spaceBefore="6.0" spaceAfter="6.0" alignment="CENTER"/>
117                     <paraStyle name="P2" fontName="Helvetica" fontSize="14.0" leading="15" spaceBefore="6.0" spaceAfter="6.0"/>
118                     <paraStyle name="comment" fontName="Helvetica" fontSize="14.0" leading="50" spaceBefore="0.0" spaceAfter="0.0"/>
119                     <paraStyle name="P1" fontName="Helvetica" fontSize="9.0" leading="12" spaceBefore="0.0" spaceAfter="1.0"/>
120                     <paraStyle name="terp_tblheader_Details" fontName="Helvetica-Bold" fontSize="9.0" leading="11" alignment="LEFT" spaceBefore="6.0" spaceAfter="6.0"/>
121                     <paraStyle name="terp_default_9" fontName="Helvetica" fontSize="9.0" leading="11" alignment="LEFT" spaceBefore="0.0" spaceAfter="0.0"/>
122                     <paraStyle name="terp_tblheader_General_Centre" fontName="Helvetica-Bold" fontSize="9.0" leading="10" alignment="CENTER" spaceBefore="6.0" spaceAfter="6.0"/>
123                     <paraStyle name="terp_default_Centre_8" fontName="Helvetica" fontSize="9.0" leading="10" alignment="CENTER" spaceBefore="0.0" spaceAfter="0.0"/>
124                   </stylesheet>
125                   <images/>
126                   <story>
127                     <para style="Title"><u>Browse Responses </u></para>
128                     <para style="Standard"><font></font></para>
129                     <para style="P2">
130                       <font color="white"> </font>
131                     </para>
132                     """
133         surv_resp_obj = pooler.get_pool(cr.dbname).get('survey.response')
134         if datas.has_key('form') and datas['form'].has_key('response_ids'):
135             response_id = datas['form']['response_ids'][0][2]
136         elif context.has_key('response_id'):
137             response_id = [int(context['response_id'])]
138         else:
139             response_id = surv_resp_obj.search(cr, uid, [('survey_id','in',ids)])
140         surv_resp_line_obj = pooler.get_pool(cr.dbname).get('survey.response.line')
141         surv_obj = pooler.get_pool(cr.dbname).get('survey')
142         for response in surv_resp_obj.browse(cr,uid, response_id): 
143             for survey in surv_obj.browse(cr, uid, [response.survey_id.id]):
144                 status = "Skip"
145                 if response.state == "done":
146                     status = "Complete"
147                 rml += """<blockTable colWidths="230.0,120.0,100.0,50" style="Table_heading">
148                           <tr>
149                             <td>
150                               <para style="terp_tblheader_General_Centre">Survey Title </para>
151                             </td>
152                             <td>
153                               <para style="terp_tblheader_General_Centre">Response Create Date </para>
154                             </td>
155                             <td>
156                               <para style="terp_tblheader_General_Centre">Respose By </para>
157                             </td>
158                             <td>
159                               <para style="terp_tblheader_General_Centre">Status</para>
160                             </td>
161                           </tr>
162                           </blockTable>
163                           <blockTable colWidths="230.0,120.0,100.0,50" style="Table_head_2">
164                           <tr>
165                             <td>
166                               <para style="terp_default_Centre_8">""" + to_xml(tools.ustr(survey.title)) + """</para>
167                             </td>
168                             <td>
169                               <para style="terp_default_Centre_8">""" + to_xml(time.strftime('%d-%m-%Y %I:%M:%S %p', time.strptime(response.date_create.split('.')[0], '%Y-%m-%d %H:%M:%S'))) + """</para>
170                             </td>
171                             <td>
172                               <para style="terp_default_Centre_8">""" + to_xml(response.user_id.name) + """</para>
173                             </td>
174                             <td>
175                               <para style="terp_default_Centre_8">""" + status + """</para>
176                             </td>
177                           </tr>
178                         </blockTable>"""
179                 for page in survey.page_ids:
180                     rml += """<para style="P2"></para>
181                              <blockTable colWidths="500" style="Table4">
182                                   <tr>
183                                     <td><para style="page">Page :- """ + to_xml(tools.ustr(page.title)) + """</para></td>
184                                   </tr>
185                                </blockTable>"""
186                     for que in page.question_ids:
187                         rml += """<para style="P2"></para>
188                                 <blockTable colWidths="500" style="Table5">
189                                   <tr>
190                                     <td><para style="question">Que : """ + to_xml(to_xml(que.question)) + """</para></td>
191                                   </tr>
192                                  </blockTable>"""
193                         answer = surv_resp_line_obj.browse(cr,uid, surv_resp_line_obj.search(cr, uid, [('question_id','=',que.id),('response_id','=',response.id)]))
194                         if que.type in ['descriptive_text']:
195                             rml +="""<blockTable colWidths="500" style="Table1">
196                              <tr>  <td> <para style="response">""" + to_xml(tools.ustr(que.descriptive_text)) + """</para></td> </tr>
197                             </blockTable>"""
198                         elif que.type in ['table']:
199                             if len(answer) and answer[0].state == "done":
200                                 col_heading = pooler.get_pool(cr.dbname).get('survey.tbl.column.heading')
201                                 cols_widhts = []
202                                 for col in range(0, len(que.column_heading_ids)):
203                                     cols_widhts.append(float(500 / (len(que.column_heading_ids))))
204                                 colWidths = ",".join(map(tools.ustr, cols_widhts))
205                                 matrix_ans = []
206                                 rml +="""<para style="P2"></para><blockTable colWidths=" """ + str(colWidths) + """ " style="Table41"><tr>"""
207                                 for col in que.column_heading_ids:
208                                     if col.title not in matrix_ans:
209                                         matrix_ans.append(col.title)
210                                         rml +="""<td> <para style="terp_tblheader_Details">""" + to_xml(tools.ustr(col.title)) +"""</para></td>"""
211                                 rml += """</tr></blockTable>"""
212                                 for row in range(0, que.no_of_rows):
213                                     rml +="""<blockTable colWidths=" """ + str(colWidths) + """ " style="Table51"><tr>"""
214                                     table_data = col_heading.browse(cr, uid, col_heading.search(cr, uid, [('response_table_id', '=', answer[0].id),('name','=',row)]))
215                                     for column in matrix_ans:
216                                         value = """<font color="white"> </font>"""
217                                         for col in table_data:
218                                             if column == col.column_id.title:
219                                                 value = col.value
220                                         rml += """<td> <para style="terp_default_9">""" + to_xml(tools.ustr(value)) +"""</para></td>"""
221                                     rml += """</tr></blockTable>"""
222                             else:
223                                 rml +="""<blockTable colWidths="500" style="Table1">
224                                  <tr>  <td> <para style="response">No Response</para></td> </tr>
225                                 </blockTable>"""
226                         elif que.type in ['multiple_choice_only_one_ans','multiple_choice_multiple_ans']:
227                             if len(answer) and answer[0].state == "done":
228                                 for ans in answer[0].response_answer_ids:
229                                     rml +="""<blockTable colWidths="500" style="Table1">
230                                          <tr> <td> <para style="response">""" + to_xml(tools.ustr(ans.answer_id.answer)) + """</para></td></tr>
231                                         </blockTable>"""
232                                 if que.comment_field_type:
233                                     if answer[0].comment:
234                                         rml+="""<blockTable colWidths="500" style="Table1"><tr>
235                                                 <td><para style="answer">""" + to_xml(tools.ustr(answer[0].comment)) + """</para></td></tr></blockTable>"""
236                             else:
237                                 rml +="""<blockTable colWidths="500" style="Table1">
238                                  <tr>  <td> <para style="response">No Response</para></td> </tr>
239                                 </blockTable>"""
240                         elif que.type in ['multiple_textboxes','date','date_and_time','numerical_textboxes']:
241                             if len(answer) and answer[0].state == "done":
242                                 for ans in answer[0].response_answer_ids:
243                                     rml +="""<blockTable colWidths="200,300" style="Table1">
244                                          <tr> <td> <para style="response">""" + to_xml(tools.ustr(ans.answer_id.answer)) + """</para></td>
245                                          <td> <para style="response">""" + to_xml(tools.ustr(ans.answer)) + """</para></td></tr>
246                                         </blockTable>"""
247                             else:
248                                 rml +="""<blockTable colWidths="500" style="Table1">
249                                  <tr>  <td> <para style="response">No Response</para></td> </tr>
250                                 </blockTable>"""
251                         elif que.type in ['single_textbox']:
252                             if len(answer) and answer[0].state == "done":
253                                 rml +="""<blockTable colWidths="500" style="Table1">
254                                      <tr> <td> <para style="response">""" + to_xml(tools.ustr(answer[0].single_text)) + """</para></td></tr>
255                                     </blockTable>"""
256                             else:
257                                 rml +="""<blockTable colWidths="500" style="Table1">
258                                  <tr>  <td> <para style="response">No Response</para></td> </tr>
259                                 </blockTable>"""
260                         elif que.type in ['comment']:
261                             if len(answer) and answer[0].state == "done":
262                                 rml +="""<blockTable colWidths="500" style="Table1">
263                                      <tr> <td> <para style="response">""" + to_xml(tools.ustr(answer[0].comment)) + """</para></td></tr>
264                                     </blockTable>"""
265                             else:
266                                 rml +="""<blockTable colWidths="500" style="Table1">
267                                  <tr>  <td> <para style="response">No Response</para></td> </tr>
268                                 </blockTable>"""
269                         elif que.type in ['matrix_of_choices_only_one_ans','matrix_of_choices_only_multi_ans','rating_scale','matrix_of_drop_down_menus']:
270                             if len(answer) and answer[0].state == "done":
271                                 if que.type  in ['matrix_of_choices_only_one_ans','rating_scale'] and que.comment_column:
272                                     pass
273                                 cols_widhts = []
274                                 cols_widhts.append(200)
275                                 len_col_heading = len(que.column_heading_ids)
276                                 for col in range(0, len_col_heading):
277                                     cols_widhts.append(float(300 / len_col_heading))
278                                 tmp=0.0
279                                 sum = 0.0
280                                 i = 0
281                                 if que.type in ['matrix_of_choices_only_one_ans','rating_scale'] and que.comment_column:
282                                     for col in cols_widhts:
283                                         if i==0:
284                                             cols_widhts[i] = cols_widhts[i]/2.0
285                                             tmp = cols_widhts[i]
286                                         sum += col
287                                         i+=1
288                                     cols_widhts.append(round(tmp,2))
289                                 colWidths = ",".join(map(tools.ustr, cols_widhts))
290                                 matrix_ans = ['',]
291                                 for col in que.column_heading_ids:
292                                     if col.title not in matrix_ans:
293                                         matrix_ans.append(col.title)
294                                 len_matrix = len(matrix_ans)
295                                 if que.type in ['matrix_of_choices_only_one_ans','rating_scale'] and que.comment_column:
296                                     matrix_ans.append(que.column_name)
297                                 rml+="""<blockTable colWidths=" """ + colWidths + """ " style="Table1"><tr>"""
298                                 for mat_col in matrix_ans:
299                                     rml+="""<td><para style="response">""" + to_xml(tools.ustr(mat_col)) + """</para></td>"""
300                                 rml +="""</tr>"""
301                                 rml+="""</blockTable>"""
302                                 i=0
303                                 for ans in que.answer_choice_ids:
304                                     if i%2!=0:
305                                         style='ans_tbl_white'
306                                     else:
307                                         style='ans_tbl_gainsboro'
308                                     i+=1
309                                     rml+="""<blockTable colWidths=" """ + colWidths + """ " style='"""+style+"""'>
310                                     <tr><td><para style="response">""" + to_xml(tools.ustr(ans.answer)) + """</para></td>"""
311                                     comment_value = ""
312                                     for mat_col in range(1, len_matrix):
313                                         value = """"""
314                                         for res_ans in answer[0].response_answer_ids:
315                                             if res_ans.answer_id.id == ans.id and res_ans.answer == matrix_ans[mat_col]:
316                                                 comment_value =  to_xml(tools.ustr(res_ans.comment_field))
317                                                 if que.type in ['matrix_of_drop_down_menus']:
318                                                     value = """<para style="response">""" + to_xml(tools.ustr(res_ans.value_choice)) + """</para>"""
319                                                 elif que.type in ['matrix_of_choices_only_one_ans','rating_scale']:
320                                                     value = """<illustration><fill color="white"/>
321                                                             <circle x="0.3cm" y="-0.18cm" radius="0.22 cm" fill="yes" stroke="yes"/>
322                                                             <fill color="gray"/>
323                                                             <circle x="0.3cm" y="-0.18cm" radius="0.10 cm" fill="yes" stroke="no"/>
324                                                         </illustration>"""
325                                                 elif que.type in ['matrix_of_choices_only_multi_ans']:
326                                                     value = """<illustration>
327                                                         <fill color="white"/>
328                                                         <rect x="0.1cm" y="-0.45cm" width="0.5 cm" height="0.5cm" fill="yes" stroke="yes"  round="0.1cm"/>
329                                                         <fill color="gray"/>
330                                                         <rect x="0.2cm" y="-0.35cm" width="0.3 cm" height="0.3cm" fill="yes" stroke="no"  round="0.1cm"/>
331                                                         </illustration>"""
332                                                 break
333                                             else:
334                                                 if que.type in ['matrix_of_drop_down_menus']:
335                                                     value = """"""
336                                                 elif que.type in ['matrix_of_choices_only_one_ans','rating_scale']:
337                                                     value = """<illustration><fill color="white"/>
338                                                             <circle x="0.3cm" y="-0.18cm" radius="0.22 cm" fill="yes" stroke="yes"  round="0.1cm"/>
339                                                         </illustration>"""
340                                                 elif que.type in ['matrix_of_choices_only_multi_ans']:
341                                                     value = """<illustration><fill color="white"/>
342                                                         <rect x="0.1cm" y="-0.45cm" width="0.5 cm" height="0.5cm" fill="yes" stroke="yes"  round="0.1cm"/>
343                                                         </illustration>"""
344                                         rml+= """<td>""" + value + """</td>"""
345                                     if que.type in ['matrix_of_choices_only_one_ans','rating_scale'] and que.comment_column:
346                                         if comment_value=='False':
347                                             comment_value = ''
348                                         rml+= """<td><para style="response">"""+ to_xml(tools.ustr(comment_value)) + """</para></td>"""
349                                     rml+="""  </tr></blockTable>"""
350                                 if que.is_comment_require:
351                                     rml+="""<blockTable colWidths="500" style="Table1"><tr>
352                                             <td><para style="answer">""" + to_xml(tools.ustr(answer[0].comment or '')) + """</para></td></tr></blockTable>"""
353                             else:
354                                 rml +="""<blockTable colWidths="500" style="Table1">
355                                  <tr>  <td> <para style="response">No Response</para></td> </tr>
356                                 </blockTable>"""
357                 
358                 rml += """<pageBreak/>"""
359         rml += """</story></document>"""
360         report_type = datas.get('report_type', 'pdf')
361         create_doc = self.generators[report_type]
362         pdf = create_doc(rml, title=self.title)
363         return (pdf, report_type)
364
365 survey_browse_response('report.survey.browse.response', 'survey','','')
366
367 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
368