[IMP]:survey,hr_evaluation.(Ref-YSA,APA).
[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="note_table">
55                       <blockAlignment value="LEFT"/>
56                       <blockValign value="TOP"/>
57                     </blockTableStyle>
58                     <blockTableStyle id="Table2">
59                       <blockAlignment value="LEFT"/>
60                       <blockValign value="TOP"/>
61                     </blockTableStyle>
62                     <blockTableStyle id="Table3">
63                       <blockAlignment value="LEFT"/>
64                       <lineStyle kind="LINEBELOW" colorName="#e6e6e6" start="1,0" stop="2,-1"/>
65                       <blockValign value="TOP"/>
66                     </blockTableStyle>
67                     <blockTableStyle id="Table4">
68                       <blockAlignment value="LEFT"/>
69                       <blockValign value="TOP"/>
70                       <lineStyle kind="LINEBELOW" colorName="#000000" start="0,-1" stop="1,-1"/>
71                     </blockTableStyle>
72                     <blockTableStyle id="Table5">
73                       <blockAlignment value="LEFT"/>
74                       <blockValign value="TOP"/>
75                       <lineStyle kind="LINEBELOW" colorName="#8f8f8f" start="0,-1" stop="1,-1"/>
76                     </blockTableStyle>
77                     <blockTableStyle id="Table41">
78                       <blockAlignment value="LEFT"/>
79                       <blockValign value="TOP"/>
80                       <lineStyle kind="LINEBELOW" colorName="#000000" start="0,0" stop="-1,-1"/>
81                       <lineStyle kind="LINEBEFORE" colorName="#777777" start="0,0" stop="-1,-1"/>
82                       <lineStyle kind="LINEAFTER" colorName="#777777" start="0,0" stop="-1,-1"/>
83                     </blockTableStyle>
84                     <blockTableStyle id="Table51">
85                       <blockAlignment value="LEFT"/>
86                       <blockValign value="TOP"/>
87                       <lineStyle kind="LINEBELOW" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
88                       <lineStyle kind="LINEBEFORE" colorName="#777777" start="0,0" stop="-1,-1"/>
89                       <lineStyle kind="LINEAFTER" colorName="#777777" start="0,0" stop="-1,-1"/>
90                     </blockTableStyle>
91                     <blockTableStyle id="Table_heading">
92                       <blockAlignment value="LEFT"/>
93                       <blockValign value="TOP"/>
94                       <lineStyle kind="LINEBEFORE" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
95                       <lineStyle kind="LINEAFTER" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
96                       <lineStyle kind="LINEBELOW" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
97                       <lineStyle kind="LINEABOVE" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
98                     </blockTableStyle>
99                     <blockTableStyle id="Table_head_2">
100                       <blockAlignment value="LEFT"/>
101                       <blockValign value="TOP"/>
102                       <lineStyle kind="LINEBEFORE" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
103                       <lineStyle kind="LINEAFTER" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
104                       <lineStyle kind="LINEBELOW" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
105                       <lineStyle kind="LINEABOVE" colorName="#e6e6e6" start="0,0" stop="-1,-1"/>
106                     </blockTableStyle>
107                     <initialize>
108                       <paraStyle name="all" alignment="justify"/>
109                     </initialize>
110                     <paraStyle name="answer_right" alignment="RIGHT" fontName="helvetica" fontSize="09.0" leftIndent="2.0"/>
111                     <paraStyle name="Standard1" fontName="helvetica-bold" alignment="RIGHT" fontSize="09.0"/>
112                     <paraStyle name="Standard" alignment="LEFT" fontName="Helvetica-Bold" fontSize="11.0"/>
113                     <paraStyle name="header1" fontName="Helvetica" fontSize="11.0"/>
114                     <paraStyle name="response" fontName="Helvetica-oblique" fontSize="9.5"/>
115                     <paraStyle name="page" fontName="helvetica" fontSize="11.0" leftIndent="0.0"/>
116                     <paraStyle name="question" fontName="helvetica-boldoblique" fontSize="10.0" leftIndent="3.0"/>
117                     <paraStyle name="answer_bold" fontName="Helvetica-Bold" fontSize="09.0" leftIndent="2.0"/>
118                     <paraStyle name="answer" fontName="helvetica" fontSize="09.0" leftIndent="2.0"/>
119                     <paraStyle name="answer1" fontName="helvetica" fontSize="09.0" leftIndent="2.0"/>
120                     <paraStyle name="Title" fontName="helvetica" fontSize="20.0" leading="15" spaceBefore="6.0" spaceAfter="6.0" alignment="CENTER"/>
121                     <paraStyle name="P2" fontName="Helvetica" fontSize="14.0" leading="15" spaceBefore="6.0" spaceAfter="6.0"/>
122                     <paraStyle name="comment" fontName="Helvetica" fontSize="14.0" leading="50" spaceBefore="0.0" spaceAfter="0.0"/>
123                     <paraStyle name="P1" fontName="Helvetica" fontSize="9.0" leading="12" spaceBefore="0.0" spaceAfter="1.0"/>
124                     <paraStyle name="terp_tblheader_Details" fontName="Helvetica-Bold" fontSize="9.0" leading="11" alignment="LEFT" spaceBefore="6.0" spaceAfter="6.0"/>
125                     <paraStyle name="terp_default_9" fontName="Helvetica" fontSize="9.0" leading="11" alignment="LEFT" spaceBefore="0.0" spaceAfter="0.0"/>
126                     <paraStyle name="terp_tblheader_General_Centre" fontName="Helvetica-Bold" fontSize="9.0" leading="10" alignment="CENTER" spaceBefore="6.0" spaceAfter="6.0"/>
127                     <paraStyle name="terp_default_Centre_8" fontName="Helvetica" fontSize="9.0" leading="10" alignment="CENTER" spaceBefore="0.0" spaceAfter="0.0"/>
128                   </stylesheet>
129                   <images/>
130                   <story>
131                     <para style="Title"><u>Browse Responses </u></para>
132                     <para style="Standard"><font></font></para>
133                     <para style="P2">
134                       <font color="white"> </font>
135                     </para>
136                     """
137         surv_resp_obj = pooler.get_pool(cr.dbname).get('survey.response')
138         if datas.has_key('form') and datas['form'].has_key('response_ids'):
139             response_id = datas['form']['response_ids'][0][2]
140         elif context.has_key('response_id'):
141             response_id = [int(context['response_id'])]
142         else:
143             response_id = surv_resp_obj.search(cr, uid, [('survey_id','in',ids)])
144         surv_resp_line_obj = pooler.get_pool(cr.dbname).get('survey.response.line')
145         surv_obj = pooler.get_pool(cr.dbname).get('survey')
146         for response in surv_resp_obj.browse(cr,uid, response_id):
147             for survey in surv_obj.browse(cr, uid, [response.survey_id.id]):
148                 status = "Not Finished"
149                 if response.state == "done":
150                     status = "Finished"
151                 rml += """<blockTable colWidths="230.0,120.0,100.0,50" style="Table_heading">
152                           <tr>
153                             <td>
154                               <para style="terp_tblheader_General_Centre">Survey Title </para>
155                             </td>
156                             <td>
157                               <para style="terp_tblheader_General_Centre">Response Create Date </para>
158                             </td>
159                             <td>
160                               <para style="terp_tblheader_General_Centre">Respose By </para>
161                             </td>
162                             <td>
163                               <para style="terp_tblheader_General_Centre">Status</para>
164                             </td>
165                           </tr>
166                           </blockTable>
167                           <blockTable colWidths="230.0,120.0,100.0,50" style="Table_head_2">
168                           <tr>
169                             <td>
170                               <para style="terp_default_Centre_8">""" + to_xml(tools.ustr(survey.title)) + """</para>
171                             </td>
172                             <td>
173                               <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>
174                             </td>
175                             <td>
176                               <para style="terp_default_Centre_8">""" + to_xml(response.user_id.name) + """</para>
177                             </td>
178                             <td>
179                               <para style="terp_default_Centre_8">""" + status + """</para>
180                             </td>
181                           </tr>
182                         </blockTable>"""
183                 for page in survey.page_ids:
184                     rml += """<para style="P2"></para>
185                              <blockTable colWidths="500" style="Table4">
186                                   <tr>
187                                     <td><para style="page">Page :- """ + to_xml(tools.ustr(page.title)) + """</para></td>
188                                   </tr>
189                                </blockTable>"""
190                     if page.note:
191                         rml += """<para style="P2"></para>
192                              <blockTable colWidths="500" style="note_table">
193                                   <tr>
194                                     <td><para style="response">""" + to_xml(tools.ustr(page.note)) + """</para></td>
195                                   </tr>
196                                </blockTable>"""
197                     for que in page.question_ids:
198                         rml += """<para style="P2"></para>
199                                 <blockTable colWidths="500" style="Table5">
200                                   <tr>
201                                     <td><para style="question">Que : """ + to_xml(to_xml(que.question)) + """</para></td>
202                                   </tr>
203                                  </blockTable>"""
204                         answer = surv_resp_line_obj.browse(cr,uid, surv_resp_line_obj.search(cr, uid, [('question_id','=',que.id),('response_id','=',response.id)]))
205                         if que.type in ['descriptive_text']:
206                             rml +="""<blockTable colWidths="500" style="Table1">
207                              <tr>  <td> <para style="response">""" + to_xml(tools.ustr(que.descriptive_text)) + """</para></td> </tr>
208                             </blockTable>"""
209                         elif que.type in ['table']:
210                             if len(answer) and answer[0].state == "done":
211                                 col_heading = pooler.get_pool(cr.dbname).get('survey.tbl.column.heading')
212                                 cols_widhts = []
213                                 for col in range(0, len(que.column_heading_ids)):
214                                     cols_widhts.append(float(500 / (len(que.column_heading_ids))))
215                                 colWidths = ",".join(map(tools.ustr, cols_widhts))
216                                 matrix_ans = []
217                                 rml +="""<para style="P2"></para><blockTable colWidths=" """ + str(colWidths) + """ " style="Table41"><tr>"""
218                                 for col in que.column_heading_ids:
219                                     if col.title not in matrix_ans:
220                                         matrix_ans.append(col.title)
221                                         rml +="""<td> <para style="terp_tblheader_Details">""" + to_xml(tools.ustr(col.title)) +"""</para></td>"""
222                                 rml += """</tr></blockTable>"""
223                                 for row in range(0, que.no_of_rows):
224                                     rml +="""<blockTable colWidths=" """ + str(colWidths) + """ " style="Table51"><tr>"""
225                                     table_data = col_heading.browse(cr, uid, col_heading.search(cr, uid, [('response_table_id', '=', answer[0].id),('name','=',row)]))
226                                     for column in matrix_ans:
227                                         value = False
228                                         for col in table_data:
229                                             if column == col.column_id.title:
230                                                 value = col.value
231                                         if value:
232                                             rml += """<td> <para style="terp_default_9">""" + to_xml(tools.ustr(value)) +"""</para></td>"""
233                                         else:
234                                             rml += """<td><para style="terp_default_9"><font color ="white"> </font></para></td>"""
235                                     rml += """</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_choice_only_one_ans','multiple_choice_multiple_ans']:
241                             if len(answer) and answer[0].state == "done":
242                                 for ans in answer[0].response_answer_ids:
243                                     rml +="""<blockTable colWidths="500" style="Table1">
244                                          <tr> <td> <para style="response">""" + to_xml(tools.ustr(ans.answer_id.answer)) + """</para></td></tr>
245                                         </blockTable>"""
246                                 if que.comment_field_type:
247                                     if answer[0].comment:
248                                         rml+="""<blockTable colWidths="500" style="Table1"><tr>
249                                                 <td><para style="answer">""" + to_xml(tools.ustr(answer[0].comment)) + """</para></td></tr></blockTable>"""
250                             else:
251                                 rml +="""<blockTable colWidths="500" style="Table1">
252                                  <tr>  <td> <para style="response">No Response</para></td> </tr>
253                                 </blockTable>"""
254                         elif que.type in ['multiple_textboxes','date','date_and_time','numerical_textboxes']:
255                             if len(answer) and answer[0].state == "done":
256                                 for ans in answer[0].response_answer_ids:
257                                     rml +="""<blockTable colWidths="200,300" style="Table1">
258                                          <tr> <td> <para style="response">""" + to_xml(tools.ustr(ans.answer_id.answer)) + """</para></td>
259                                          <td> <para style="response">""" + to_xml(tools.ustr(ans.answer)) + """</para></td></tr>
260                                         </blockTable>"""
261                             else:
262                                 rml +="""<blockTable colWidths="500" style="Table1">
263                                  <tr>  <td> <para style="response">No Response</para></td> </tr>
264                                 </blockTable>"""
265                         elif que.type in ['single_textbox']:
266                             if len(answer) and answer[0].state == "done":
267                                 rml +="""<blockTable colWidths="500" style="Table1">
268                                      <tr> <td> <para style="response">""" + to_xml(tools.ustr(answer[0].single_text)) + """</para></td></tr>
269                                     </blockTable>"""
270                             else:
271                                 rml +="""<blockTable colWidths="500" style="Table1">
272                                  <tr>  <td> <para style="response">No Response</para></td> </tr>
273                                 </blockTable>"""
274                         elif que.type in ['comment']:
275                             if len(answer) and answer[0].state == "done":
276                                 rml +="""<blockTable colWidths="500" style="Table1">
277                                      <tr> <td> <para style="response">""" + to_xml(tools.ustr(answer[0].comment)) + """</para></td></tr>
278                                     </blockTable>"""
279                             else:
280                                 rml +="""<blockTable colWidths="500" style="Table1">
281                                  <tr>  <td> <para style="response">No Response</para></td> </tr>
282                                 </blockTable>"""
283                         elif que.type in ['matrix_of_choices_only_one_ans','matrix_of_choices_only_multi_ans','rating_scale','matrix_of_drop_down_menus']:
284                             if len(answer) and answer[0].state == "done":
285                                 if que.type  in ['matrix_of_choices_only_one_ans','rating_scale'] and que.comment_column:
286                                     pass
287                                 cols_widhts = []
288                                 cols_widhts.append(200)
289                                 len_col_heading = len(que.column_heading_ids)
290                                 for col in range(0, len_col_heading):
291                                     cols_widhts.append(float(300 / len_col_heading))
292                                 tmp=0.0
293                                 sum = 0.0
294                                 i = 0
295                                 if que.type in ['matrix_of_choices_only_one_ans','rating_scale'] and que.comment_column:
296                                     for col in cols_widhts:
297                                         if i==0:
298                                             cols_widhts[i] = cols_widhts[i]/2.0
299                                             tmp = cols_widhts[i]
300                                         sum += col
301                                         i+=1
302                                     cols_widhts.append(round(tmp,2))
303                                 colWidths = ",".join(map(tools.ustr, cols_widhts))
304                                 matrix_ans = ['',]
305                                 for col in que.column_heading_ids:
306                                     if col.title not in matrix_ans:
307                                         matrix_ans.append(col.title)
308                                 len_matrix = len(matrix_ans)
309                                 if que.type in ['matrix_of_choices_only_one_ans','rating_scale'] and que.comment_column:
310                                     matrix_ans.append(que.column_name)
311                                 rml+="""<blockTable colWidths=" """ + colWidths + """ " style="Table1"><tr>"""
312                                 for mat_col in matrix_ans:
313                                     rml+="""<td><para style="response">""" + to_xml(tools.ustr(mat_col)) + """</para></td>"""
314                                 rml +="""</tr>"""
315                                 rml+="""</blockTable>"""
316                                 i=0
317                                 for ans in que.answer_choice_ids:
318                                     if i%2!=0:
319                                         style='ans_tbl_white'
320                                     else:
321                                         style='ans_tbl_gainsboro'
322                                     i+=1
323                                     rml+="""<blockTable colWidths=" """ + colWidths + """ " style='"""+style+"""'>
324                                     <tr><td><para style="response">""" + to_xml(tools.ustr(ans.answer)) + """</para></td>"""
325                                     comment_value = ""
326                                     for mat_col in range(1, len_matrix):
327                                         value = """"""
328                                         for res_ans in answer[0].response_answer_ids:
329                                             if res_ans.answer_id.id == ans.id and res_ans.answer == matrix_ans[mat_col]:
330                                                 comment_value =  to_xml(tools.ustr(res_ans.comment_field))
331                                                 if que.type in ['matrix_of_drop_down_menus']:
332                                                     value = """<para style="response">""" + to_xml(tools.ustr(res_ans.value_choice)) + """</para>"""
333                                                 elif que.type in ['matrix_of_choices_only_one_ans','rating_scale']:
334                                                     value = """<illustration><fill color="white"/>
335                                                             <circle x="0.3cm" y="-0.18cm" radius="0.22 cm" fill="yes" stroke="yes"/>
336                                                             <fill color="gray"/>
337                                                             <circle x="0.3cm" y="-0.18cm" radius="0.10 cm" fill="yes" stroke="no"/>
338                                                         </illustration>"""
339                                                 elif que.type in ['matrix_of_choices_only_multi_ans']:
340                                                     value = """<illustration>
341                                                         <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                                                         <fill color="gray"/>
344                                                         <rect x="0.2cm" y="-0.35cm" width="0.3 cm" height="0.3cm" fill="yes" stroke="no"  round="0.1cm"/>
345                                                         </illustration>"""
346                                                 break
347                                             else:
348                                                 if que.type in ['matrix_of_drop_down_menus']:
349                                                     value = """"""
350                                                 elif que.type in ['matrix_of_choices_only_one_ans','rating_scale']:
351                                                     value = """<illustration><fill color="white"/>
352                                                             <circle x="0.3cm" y="-0.18cm" radius="0.22 cm" fill="yes" stroke="yes"  round="0.1cm"/>
353                                                         </illustration>"""
354                                                 elif que.type in ['matrix_of_choices_only_multi_ans']:
355                                                     value = """<illustration><fill color="white"/>
356                                                         <rect x="0.1cm" y="-0.45cm" width="0.5 cm" height="0.5cm" fill="yes" stroke="yes"  round="0.1cm"/>
357                                                         </illustration>"""
358                                         rml+= """<td>""" + value + """</td>"""
359                                     if que.type in ['matrix_of_choices_only_one_ans','rating_scale'] and que.comment_column:
360                                         if comment_value=='False':
361                                             comment_value = ''
362                                         rml+= """<td><para style="response">"""+ to_xml(tools.ustr(comment_value)) + """</para></td>"""
363                                     rml+="""  </tr></blockTable>"""
364                                 if que.is_comment_require:
365                                     rml+="""<blockTable colWidths="500" style="Table1"><tr>
366                                             <td><para style="answer">""" + to_xml(tools.ustr(answer[0].comment or '')) + """</para></td></tr></blockTable>"""
367                             else:
368                                 rml +="""<blockTable colWidths="500" style="Table1">
369                                  <tr>  <td> <para style="response">No Response</para></td> </tr>
370                                 </blockTable>"""
371
372                     rml += """<pageBreak/>"""
373         rml += """</story></document>"""
374         report_type = datas.get('report_type', 'pdf')
375         create_doc = self.generators[report_type]
376         pdf = create_doc(rml, title=self.title)
377         return (pdf, report_type)
378
379 survey_browse_response('report.survey.browse.response', 'survey','','')
380
381 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
382