'Custom Method to build a Summary of a series'

@staticmethod
def seriesSummary(r, **attr):

    s3db = current.s3db
    response = current.response
    s3 = response.s3
    posn_offset = 11
    rheader = attr.get('rheader', None)
    if rheader:
        rheader = rheader(r)
        output = dict(rheader=rheader)
    else:
        output = {}
    if ((r.env.request_method == 'POST') or ('mode' in r.vars)):
        crud_strings = s3.crud_strings['survey_complete']
        question_ids = []
        rvars = r.vars
        if ('mode' in rvars):
            mode = rvars['mode']
            series_id = r.id
            if ('selected' in rvars):
                selected = rvars['selected'].split(',')
            else:
                selected = []
            q_ltable = s3db.survey_question_list
            sertable = s3db.survey_series
            query = ((sertable.id == series_id) & (sertable.template_id == q_ltable.template_id))
            questions = current.db(query).select(q_ltable.posn, q_ltable.question_id, orderby=q_ltable.posn)
            for question in questions:
                qstn_posn = (question.posn + posn_offset)
                if (mode == 'Inclusive'):
                    if (str(qstn_posn) in selected):
                        question_ids.append(str(question.question_id))
                elif (mode == 'Exclusive'):
                    if (str(qstn_posn) not in selected):
                        question_ids.append(str(question.question_id))
            items = buildCompletedList(series_id, question_ids)
            if (r.representation == 'xls'):
                from ..s3.s3codecs.xls import S3XLS
                exporter = S3XLS()
                return exporter.encode(items, title=crud_strings.title_selected, use_colour=False)
            if (r.representation == 'html'):
                table = buildTableFromCompletedList(items)
            output['items'] = table
            output['sortby'] = [[0, 'asc']]
            url_pdf = URL(c='survey', f='series', args=[series_id, 'summary.pdf'], vars={'mode': mode, 'selected': rvars['selected']})
            url_xls = URL(c='survey', f='series', args=[series_id, 'summary.xls'], vars={'mode': mode, 'selected': rvars['selected']})
            s3.formats['pdf'] = url_pdf
            s3.formats['xls'] = url_xls
        else:
            output['items'] = None
        output['title'] = crud_strings.title_selected
        output['subtitle'] = crud_strings.subtitle_selected
        output['help'] = ''
    else:
        crud_strings = s3.crud_strings['survey_series']
        viewing = r.get_vars.get('viewing', None)
        if viewing:
            (dummy, series_id) = viewing.split('.')
        else:
            series_id = r.get_vars.get('series', None)
            if (not series_id):
                series_id = r.id
        form = buildSeriesSummary(series_id, posn_offset)
        output['items'] = form
        output['sortby'] = [[0, 'asc']]
        output['title'] = crud_strings.title_analysis_summary
        output['subtitle'] = crud_strings.subtitle_analysis_summary
        output['help'] = crud_strings.help_analysis_summary
        s3.dataTableBulkActionPosn = 'top'
        s3.actions = None
    response.view = 'survey/series_summary.html'
    return output
