725e2134f76ca8deebb7f3e1f700af0ea1bd53d1
[odoo/odoo.git] / addons / survey / report / survey_form.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
28 class survey_form(report_rml):
29     def create(self, cr, uid, ids, datas, context):
30         _divide_columns_for_matrix = 0.7
31         _display_ans_in_rows = 5
32         _pageSize = ('29.7cm','21.1cm')
33         if datas.has_key('form') and datas['form']['orientation']=='vertical':
34             if datas['form']['paper_size']=='letter':
35                 _pageSize = ('21.6cm','27.9cm')
36             elif datas['form']['paper_size']=='legal':
37                 _pageSize = ('21.6cm','35.6cm')
38             elif datas['form']['paper_size']=='a4':
39                 _pageSize = ('21.1cm','29.7cm')
40         elif datas.has_key('form') and datas['form']['orientation']=='horizontal':
41             if datas['form']['paper_size']=='letter':
42                 _pageSize = ('27.9cm','21.6cm')
43             elif datas['form']['paper_size']=='legal':
44                 _pageSize = ('35.6cm','21.6cm')
45             elif datas['form']['paper_size']=='a4':
46                 _pageSize = ('29.7cm','21.1cm')
47
48         _frame_width = tools.ustr(_pageSize[0])
49         _frame_height = tools.ustr(float(_pageSize[1].replace('cm','')) - float(1.90))+'cm'
50         _tbl_widths = tools.ustr(float(_pageSize[0].replace('cm','')) - float(2.10))+'cm'
51         rml="""<document filename="Survey Form.pdf">
52             <template pageSize="("""+_pageSize[0]+""","""+_pageSize[1]+""")" title='Survey Form.pdf' author="Martin Simon" allowSplitting="20" >
53                 <pageTemplate id="first">
54                     <frame id="first" x1="0.0cm" y1="1.0cm" width='"""+_frame_width+"""' height='"""+_frame_height+"""'/>
55                     <pageGraphics>
56                         <lineMode width="1.0"/>
57                         <lines>1.0cm """+tools.ustr(float(_pageSize[1].replace('cm','')) - float(1.00))+'cm'+""" """+tools.ustr(float(_pageSize[0].replace('cm','')) - float(1.00))+'cm'+""" """+tools.ustr(float(_pageSize[1].replace('cm','')) - float(1.00))+'cm'+"""</lines>
58                         <lines>1.0cm """+tools.ustr(float(_pageSize[1].replace('cm','')) - float(1.00))+'cm'+""" 1.0cm 1.00cm</lines>
59                         <lines>"""+tools.ustr(float(_pageSize[0].replace('cm','')) - float(1.00))+'cm'+""" """+tools.ustr(float(_pageSize[1].replace('cm','')) - float(1.00))+'cm'+""" """+tools.ustr(float(_pageSize[0].replace('cm','')) - float(1.00))+'cm'+""" 1.00cm</lines>
60                         <lines>1.0cm 1.00cm """+tools.ustr(float(_pageSize[0].replace('cm','')) - float(1.00))+'cm'+""" 1.00cm</lines>"""
61         if datas.has_key('form') and datas['form']['page_number']:
62             rml +="""
63                     <fill color="gray"/>
64                     <setFont name="Helvetica" size="10"/>
65                     <drawRightString x='"""+tools.ustr(float(_pageSize[0].replace('cm','')) - float(1.00))+'cm'+"""' y="0.6cm">Page : <pageNumber/> </drawRightString>"""
66         rml +="""</pageGraphics>
67                 </pageTemplate>
68             </template>
69             <stylesheet>
70             <blockTableStyle id="ans_tbl">
71               <blockAlignment value="LEFT"/>
72               <blockValign value="TOP"/>
73               <lineStyle kind="LINEBELOW" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
74             </blockTableStyle>
75             <blockTableStyle id="ans_tbl_white">
76               <blockAlignment value="LEFT"/>
77               <blockValign value="TOP"/>
78               <lineStyle kind="LINEBELOW" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
79             </blockTableStyle>
80             <blockTableStyle id="ans_tbl_gainsboro">
81               <blockAlignment value="LEFT"/>
82               <blockValign value="TOP"/>
83               <lineStyle kind="LINEBELOW" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
84               <blockBackground colorName="gainsboro" start="0,0" stop="-1,-1"/>
85             </blockTableStyle>
86             <blockTableStyle id="page_tbl">
87               <blockAlignment value="LEFT"/>
88               <blockValign value="TOP"/>
89               <lineStyle kind="LINEBELOW" colorName="#000000" start="0,-1" stop="1,-1"/>
90               <blockBackground colorName="gray" start="0,0" stop="-1,-1"/>
91               <blockTextColor colorName="white" start="0,0" stop="0,0"/>
92             </blockTableStyle>
93             <blockTableStyle id="title_tbl">
94               <blockAlignment value="LEFT"/>
95               <blockValign value="TOP"/>
96               <lineStyle kind="LINEBELOW" colorName="#000000" start="0,-1" stop="1,-1"/>
97               <blockBackground colorName="black" start="0,0" stop="-1,-1"/>
98               <blockTextColor colorName="white" start="0,0" stop="0,0"/>
99             </blockTableStyle>
100             <blockTableStyle id="question_tbl">
101               <blockAlignment value="LEFT"/>
102               <blockValign value="TOP"/>
103               <lineStyle kind="LINEBELOW" colorName="#8f8f8f" start="0,-1" stop="1,-1"/>
104             </blockTableStyle>
105             <blockTableStyle id="Table4">
106               <blockAlignment value="LEFT"/>
107               <blockValign value="TOP"/>
108               <lineStyle kind="LINEBELOW" colorName="#000000" start="0,0" stop="-1,-1"/>
109               <lineStyle kind="LINEBEFORE" colorName="#777777" start="0,0" stop="-1,-1"/>
110               <lineStyle kind="LINEAFTER" colorName="#777777" start="0,0" stop="-1,-1"/>
111             </blockTableStyle>
112             <blockTableStyle id="Table5">
113               <blockAlignment value="LEFT"/>
114               <blockValign value="TOP"/>
115               <lineStyle kind="LINEBELOW" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
116               <lineStyle kind="LINEBEFORE" colorName="#777777" start="0,0" stop="-1,-1"/>
117               <lineStyle kind="LINEAFTER" colorName="#777777" start="0,0" stop="-1,-1"/>
118             </blockTableStyle>
119             <initialize>
120               <paraStyle name="all" alignment="justify"/>
121             </initialize>
122             <paraStyle name="response" fontName="Helvetica-oblique" fontSize="9.5"/>
123             <paraStyle name="page" fontName="helvetica-bold" fontSize="15.0" leftIndent="0.0" textColor="white"/>
124             <paraStyle name="title" fontName="helvetica-bold" fontSize="18.0" leftIndent="0.0" textColor="white"/>
125             <paraStyle name="question" fontName="helvetica-boldoblique" fontSize="10.0" leftIndent="3.0"/>
126             <paraStyle name="answer" fontName="helvetica" fontSize="09.0" leftIndent="0.0"/>
127             <paraStyle name="descriptive_text" fontName="helvetica" fontSize="10.0" leftIndent="0.0"/>
128             <paraStyle name="answer_left" alignment="LEFT" fontName="helvetica-bold" fontSize="8.0" leftIndent="0.0"/>
129             <paraStyle name="P2" fontName="Helvetica" fontSize="14.0" leading="15" spaceBefore="6.0" spaceAfter="6.0"/>
130             <paraStyle name="comment" fontName="Helvetica" fontSize="14.0" leading="50" spaceBefore="0.0" spaceAfter="0.0"/>
131             <paraStyle name="P1" fontName="Helvetica" fontSize="9.0" leading="12" spaceBefore="0.0" spaceAfter="1.0"/>
132             <paraStyle name="terp_tblheader_Details" fontName="Helvetica-Bold" fontSize="9.0" leading="11" alignment="LEFT" spaceBefore="6.0" spaceAfter="6.0"/>
133             <paraStyle name="terp_default_9" fontName="Helvetica" fontSize="9.0" leading="11" alignment="LEFT" spaceBefore="0.0" spaceAfter="0.0"/>
134         </stylesheet>
135         <story>"""
136         surv_obj = pooler.get_pool(cr.dbname).get('survey')
137         for survey in surv_obj.browse(cr,uid,ids):
138             if datas.has_key('form') and datas['form']['survey_title']:
139                 rml += """
140                 <blockTable colWidths='"""+_tbl_widths+"""' style="title_tbl">
141                     <tr><td><para style="title">""" + to_xml(tools.ustr(survey.title)) + """</para><para style="P2"><font></font></para></td></tr>
142                 </blockTable>"""
143             seq = 0
144             for page in survey.page_ids:
145                 seq+=1
146                 rml += """
147                 <blockTable colWidths='"""+_tbl_widths+"""' style="page_tbl">
148                     <tr><td><para style="page">"""+ tools.ustr(seq) + """. """ + to_xml(tools.ustr(page.title)) + """</para></td></tr>
149                 </blockTable>"""
150                 for que in page.question_ids:
151                     cols_widhts=[]
152                     rml +="""
153                     <para style="P2"><font></font></para>
154                     <blockTable colWidths='"""+_tbl_widths+"""' style="question_tbl">
155                         <tr><td><para style="question">Que : """  + to_xml(tools.ustr(que.question)) + """</para></td></tr>
156                     </blockTable>
157                     <para style="P2"><font></font></para>"""
158                     if que.type in ['descriptive_text']:
159                         cols_widhts.append(float(_tbl_widths.replace('cm','')))
160                         colWidths = "cm,".join(map(tools.ustr, cols_widhts))
161                         colWidths = colWidths+'cm'
162                         rml+="""
163                         <blockTable colWidths=" """ + colWidths + """ " style="ans_tbl">
164                             <tr>
165                                 <td>
166                                 <para style="descriptive_text">""" + to_xml(tools.ustr(que.descriptive_text)) + """</para>
167                                 </td>
168                             </tr>
169                         </blockTable>"""
170                     elif que.type in ['multiple_choice_multiple_ans','multiple_choice_only_one_ans']:
171                         answer=[]
172                         for ans in que.answer_choice_ids:
173                             answer.append(to_xml(tools.ustr((ans.answer))))
174
175                         def divide_list(lst, n):
176                             return [lst[i::n] for i in range(n)]
177
178                         divide_list = divide_list(answer,_display_ans_in_rows)
179                         for lst in divide_list:
180                             if que.type == 'multiple_choice_multiple_ans':
181                                 if len(lst)<>0 and len(lst)<>int(round(float(len(answer))/_display_ans_in_rows,0)):
182                                    lst.append('')
183                             if not lst:
184                                del divide_list[divide_list.index(lst):]
185                         for divide in divide_list:
186                             a = _divide_columns_for_matrix*len(divide)
187                             b = float(_tbl_widths.replace('cm','')) - float(a)
188                             cols_widhts=[]
189                             for div in range(0,len(divide)):
190                                 cols_widhts.append(float(a/len(divide)))
191                                 cols_widhts.append(float(b/len(divide)))
192                             colWidths = "cm,".join(map(tools.ustr, cols_widhts))
193                             colWidths = colWidths+'cm'
194                             rml+="""<blockTable colWidths=" """ + colWidths + """ " style="ans_tbl">
195                                         <tr>"""
196                             for div in range(0,len(divide)):
197                                if divide[div]!='':
198                                    if que.type == 'multiple_choice_multiple_ans':
199                                            rml+="""
200                                            <td>
201                                                <illustration>
202                                                    <rect x="0.1cm" y="-0.4cm" width="0.5 cm" height="0.5cm" fill="no" stroke="yes"/>
203                                                 </illustration>
204                                            </td>
205                                            <td><para style="answer">""" + divide[div] + """</para></td>"""
206                                    else:
207                                        rml+="""
208                                        <td>
209                                            <illustration>
210                                                <circle x="0.3cm" y="-0.18cm" radius="0.23 cm" fill="no" stroke="yes"/>
211                                             </illustration>
212                                        </td>
213                                        <td><para style="answer">""" + divide[div] + """</para></td>"""
214                                else:
215                                    rml+="""
216                                    <td></td>
217                                    <td></td>"""
218                             rml+="""
219                             </tr></blockTable>"""
220                     elif que.type in ['matrix_of_choices_only_one_ans','rating_scale','matrix_of_choices_only_multi_ans','matrix_of_drop_down_menus']:
221                         if len(que.column_heading_ids):
222                             cols_widhts.append(float(_tbl_widths.replace('cm',''))/float(2.0))
223                             for col in que.column_heading_ids:
224                                 cols_widhts.append(float((float(_tbl_widths.replace('cm',''))/float(2.0))/len(que.column_heading_ids)))
225                         else:
226                             cols_widhts.append(float(_tbl_widths.replace('cm','')))
227                         tmp=0.0
228                         sum = 0.0
229                         i = 0
230                         if que.comment_column:
231                             for col in cols_widhts:
232                                 if i==0:
233                                     cols_widhts[i] = cols_widhts[i]/2.0
234                                     tmp = cols_widhts[i]
235                                 sum += col
236                                 i+=1
237                             cols_widhts.append(round(tmp,2))
238                         colWidths = "cm,".join(map(tools.ustr, cols_widhts))
239                         colWidths = colWidths+'cm'
240                         matrix_ans = ['',]
241                         for col in que.column_heading_ids:
242                             if col.title not in matrix_ans:
243                                 matrix_ans.append(col.title)
244                         if que.comment_column:
245                             matrix_ans.append(to_xml(tools.ustr(que.column_name)))
246                         rml+="""<blockTable colWidths=" """ + colWidths + """ " style="ans_tbl">
247                             <tr>"""
248                         for mat_col in matrix_ans:
249                             rml+="""<td><para style="response">""" + to_xml(tools.ustr(mat_col)) + """</para></td>"""
250                         rml+="""</tr></blockTable>"""
251                         i=0
252                         for ans in que.answer_choice_ids:
253                             if i%2!=0:
254                                 style='ans_tbl_white'
255                             else:
256                                 style='ans_tbl_gainsboro'
257                             i+=1
258                             rml+="""
259                             <blockTable colWidths=" """ + colWidths + """ " style='"""+style+"""'>
260                             <tr><td><para style="answer">""" + to_xml(tools.ustr(ans.answer)) + """</para></td>"""
261                             rec_width = float((sum-tmp)*10+100)
262                             value = ""
263                             if que.type in ['matrix_of_drop_down_menus']:
264                                 value = """ <fill color="white"/>
265                                     <rect x="-0.1cm" y="-0.45cm" width='""" + tools.ustr(cols_widhts[-1] - 0.5) +"cm" + """' height="0.5cm" fill="yes" stroke="yes" round="0.1cm"/>"""
266                             elif que.type in ['matrix_of_choices_only_one_ans','rating_scale']:
267                                 value = """ <fill color="white"/>
268                                     <!--rect x="0.25cm" y="-0.5cm" width="0.8 cm" height="0.5cm" fill="yes" stroke="yes" round="0.1cm"/-->
269                                     <circle x="0.35cm" y="-0.18cm" radius="0.25 cm" fill="yes" stroke="yes"/>"""
270                             else:
271                                 value = """ <fill color="white"/>
272                                     <rect x="0.1cm" y="-0.4cm" width="0.5 cm" height="0.5cm" fill="yes" stroke="yes" round="0.1cm"/>"""
273                             for mat_col in range(1,len(matrix_ans)):
274                                 if matrix_ans[mat_col]==que.column_name:
275                                     if mat_col==1:
276                                         rml+="""
277                                             <td><para style="answer_left">""" + to_xml(tools.ustr(que.column_name)) + """</para></td>"""
278                                     else:
279                                       rml+="""<td></td>"""
280                                 else:
281                                     rml+="""<td><illustration>""" + value + """</illustration></td>"""
282                             rml+= """</tr></blockTable>"""
283                     elif que.type in ['multiple_textboxes','numerical_textboxes', 'date_and_time','date']:
284                         cols_widhts.append(float(_tbl_widths.replace('cm',''))/2)
285                         cols_widhts.append(float(_tbl_widths.replace('cm',''))/2)
286                         colWidths = "cm,".join(map(tools.ustr, cols_widhts))
287                         colWidths = tools.ustr(colWidths) + 'cm'
288                         rect_len = ""
289                         if que.type in ['multiple_textboxes']:
290                             rect_len =  tools.ustr(cols_widhts[0] - 0.3) + "cm"
291                         else:
292                             rect_len = "6cm"
293                         for ans in que.answer_choice_ids:
294                             rml +="""<para style="P1"></para>
295                             <blockTable colWidths=" """+ colWidths + """ " style="ans_tbl">
296                                 <tr>
297                                 <td><para style="answer">""" + to_xml(tools.ustr(ans.answer)) + """</para></td>
298                                     <td>
299                                     <illustration>
300                                         <rect x="0.0cm" y="-0.5cm" width='""" + tools.ustr(rect_len) + """' height="0.6cm" fill="no" stroke="yes"/>
301                                     </illustration>
302                                     </td>
303                                 </tr>
304                             </blockTable>"""
305                     elif que.type in ['comment']:
306                         cols_widhts.append(float(_tbl_widths.replace('cm','')))
307                         colWidths = "cm,".join(map(tools.ustr, cols_widhts))
308                         colWidths = colWidths+'cm'
309                         rect_len = ""
310                         rect_len =  tools.ustr(cols_widhts[0] - 0.4) + "cm"
311                         rml+="""<blockTable colWidths=" """ + colWidths + """ " style="ans_tbl">
312                             <tr>
313                                 <td><para style="comment"><font color="white"> </font></para>
314                                     <illustration>
315                                         <rect x="0.0cm" y="0.3cm" width='""" + tools.ustr(rect_len) + """' height="1.5cm" fill="no" stroke="yes"/>
316                                     </illustration>
317                                 </td>
318                             </tr>
319                         </blockTable>"""
320                     elif que.type in ['single_textbox']:
321                         cols_widhts.append(float(_tbl_widths.replace('cm','')))
322                         colWidths = "cm,".join(map(tools.ustr, cols_widhts))
323                         colWidths = colWidths+'cm'
324                         rml+="""<para style="P2"><font color="white"> </font></para>
325                         <blockTable colWidths=" """ + colWidths + """ " style="ans_tbl">
326                             <tr>
327                                 <td>
328                                     <illustration>
329                                         <rect x="0.2cm" y="0.3cm" width="15.0 cm" height="0.6cm" fill="no" stroke="yes"/>
330                                     </illustration>
331                                 </td>
332                             </tr>
333                         </blockTable>"""
334                     elif que.type in ['table']:
335                         tbl_width = float(_tbl_widths.replace('cm',''))
336                         for i in range(0,len(que.column_heading_ids)):
337                             cols_widhts.append(tbl_width/float(len(que.column_heading_ids)))
338                         colWidths = "cm,".join(map(tools.ustr, cols_widhts))
339                         colWidths = colWidths+'cm'
340                         rml+="""<blockTable colWidths=" """ + colWidths + """ " style="Table4"><tr>"""
341                         for col in que.column_heading_ids:
342                             rml+="""<td><para style="terp_tblheader_Details">""" + to_xml(tools.ustr(col.title)) + """</para></td>"""
343                         rml+="""</tr></blockTable>
344                         <blockTable colWidths=" """ + colWidths + """ " style="Table5">"""
345                         for r in range(0,que.no_of_rows):
346                             rml+="""<tr>"""
347                             for c in que.column_heading_ids:
348                                rml+="""
349                                 <td><para style="terp_default_9"><font color="white"> </font></para></td>"""
350                             rml+="""</tr>"""
351                         rml+="""</blockTable>"""
352                 if datas.has_key('form') and not datas['form']['without_pagebreak']:
353                     rml+="""<pageBreak/>"""
354                 elif not datas.has_key('form'):
355                     rml+="""<pageBreak/>"""
356                 else:
357                     rml+="""<para style="P2"><font></font></para>"""
358         rml+="""</story></document>"""
359         report_type = datas.get('report_type', 'pdf')
360         create_doc = self.generators[report_type]
361         pdf = create_doc(rml, title=self.title)
362         return (pdf, report_type)
363 survey_form('report.survey.form', 'survey','','')
364
365 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: