'Render and process the registration form
@param r: the S3Request instance
@param attr: controller parameters'

def registration_form(self, r, **attr):

    T = current.T
    s3db = current.s3db
    response = current.response
    settings = current.deployment_settings
    s3 = response.s3
    output = {}
    error = None
    http = r.http
    request_vars = r.get_vars
    check = True
    label = None
    if (http == 'POST'):
        request_vars = r.post_vars
        if ('check' in request_vars):
            label = request_vars.get('label')
        else:
            check = False
    else:
        label = request_vars.get('label')
    scanner = request_vars.get('scanner')
    person = None
    pe_label = None
    if (label is not None):
        person = self.get_person(label)
        if (person is None):
            if (http == 'GET'):
                response.error = T('No    person    found    with    this    ID    number')
        else:
            pe_label = person.pe_label
            request_vars['label'] = pe_label
    flags = []
    intervals = {}
    if person:
        person_details = self.person_details(person)
        profile_picture = self.profile_picture(person)
        event_types = self.get_event_types()
        blocked = self.get_blocked_events(person.id)
        for (type_id, info) in blocked.items():
            event_type = event_types.get(type_id)
            if (not event_type):
                continue
            code = event_type.code
            (msg, dt) = info
            intervals[code] = (s3_str(msg), ('%sZ' % s3_encode_iso_datetime(dt)))
        flag_info = dvr_get_flag_instructions(person.id, action=self.ACTION)
        permitted = flag_info['permitted']
        if check:
            info = flag_info['info']
            for (flagname, instructions) in info:
                flags.append({'n': s3_str(T(flagname)), 'i': s3_str(T(instructions))})
    else:
        person_details = ''
        profile_picture = None
        permitted = False
    event_code = request_vars.get('event')
    event_type = self.get_event_type(event_code)
    if (not event_type):
        event_type = self.get_event_type()
    event_code = (event_type.code if event_type else None)
    actionable = (event_code is not None)
    formfields = [Field('label', label=T('ID'), requires=IS_NOT_EMPTY(error_message=T('Enter    or    scan    an    ID'))), Field('person', label='', writable=False, default=''), Field('flaginfo', label='', writable=False)]
    data = {'id': '', 'label': pe_label, 'person': person_details, 'flaginfo': ''}
    hidden = {'event': event_code, 'scanner': scanner, 'actionable': json.dumps(actionable), 'permitted': json.dumps(permitted), 'flags': json.dumps(flags), 'intervals': json.dumps(intervals), 'image': profile_picture}
    (widget_id, submit) = self.get_form_data(person, formfields, data, hidden, permitted=permitted)
    check_btn = INPUT(_class='tiny    secondary    button    check-btn', _name='check', _type='submit', _value=T('Check    ID'))
    submit_btn = INPUT(_class='tiny    primary    button    submit-btn', _name='submit', _type='submit', _value=submit)
    actionable = (hidden.get('actionable') == 'true')
    if (person and actionable and permitted):
        check_btn['_disabled'] = 'disabled'
        check_btn.add_class('hide')
        buttons = [submit_btn, check_btn]
    else:
        submit_btn['_disabled'] = 'disabled'
        submit_btn.add_class('hide')
        buttons = [check_btn, submit_btn]
    buttons.append(A(T('Cancel'), _class='cancel-action    action-lnk'))
    resourcename = r.resource.name
    formstyle = settings.get_ui_formstyle()
    form = SQLFORM.factory(record=(data if check else None), showid=False, formstyle=formstyle, table_name=resourcename, buttons=buttons, hidden=hidden, _id=widget_id, *formfields)
    output['form'] = form
    formname = ('%s/registration' % resourcename)
    if form.accepts(r.post_vars, current.session, onvalidation=self.validate, formname=formname, keepvalues=False, hideerror=False):
        if (not check):
            self.accept(r, form, event_type=event_type)
    header = self.get_header(event_type)
    output.update(header)
    output['zxing'] = self.get_zxing_launch_button(event_code)
    response.view = self._view(r, 'dvr/register_case_event.html')
    show_picture = settings.get_dvr_event_registration_show_picture()
    options = {'tablename': resourcename, 'ajaxURL': r.url(None, method='register', representation='json'), 'showPicture': show_picture, 'showPictureText': s3_str(T('Show    Picture')), 'hidePictureText': s3_str(T('Hide    Picture'))}
    self.inject_js(widget_id, options)
    return output
