<commit_before># -*- coding: utf-8 -*-

"""
    survey - Assessment Data Analysis Tool

    For more details see the blueprint at:
    http://eden.sahanafoundation.org/wiki/BluePrint/SurveyTool/ADAT

    @todo: open template from the dataTables into the section tab not update
    @todo: in the pages that add a link to a template make the combobox display the label not the numbers
    @todo: restrict the deletion of a template to only those with status Pending
"""

module = request.controller
resourcename = request.function

if not settings.has_module(module):
    raise HTTP(404, body="Module disabled: %s" % module)

try:
    from cStringIO import StringIO    # Faster, where available
except:
    from StringIO import StringIO

from gluon.contenttype import contenttype

from s3survey import S3AnalysisPriority, \
                     survey_question_type, \
                     survey_analysis_type, \
                     getMatrix, \
                     DEBUG, \
                     LayoutBlocks, \
                     DataMatrix, MatrixElement, \
                     S3QuestionTypeOptionWidget, \
                     survey_T

# -----------------------------------------------------------------------------
def index():
    """ Module's Home Page """

    module_name = settings.modules[module].name_nice
    response.title = module_name
    return dict(module_name=module_name)

# -----------------------------------------------------------------------------
def template():
    """ RESTful CRUD controller """

    # Load Model
    table = s3db.survey_template

    def prep(r):
        if r.component and r.component_name == "translate":
            table = s3db.survey_translate
            if r.component_id == None:
                # list existing translations and allow the addition of a new translation
                table.file.readable = False
                table.file.writable = False
            else:
                # edit the selected translation
                table.language.writable = False
                table.code.writable = False
            # remove CRUD generated buttons in the tabs
            s3db.configure(table,
                            deletable=False)
        else:
            s3_action_buttons(r)
            query = (r.table.status == 1) # Status of Pending
            rows = db(query).select(r.table.id)
            try:
                s3.actions[1]["restrict"].extend(str(row.id) for row in rows)
            except KeyError: # the restrict key doesn't exist
                s3.actions[1]["restrict"] = [str(row.id) for row in rows]
            except IndexError: # the delete buttons doesn't exist
                pass
            # Add some highlighting to the rows
            query = (r.table.status == 3) # Status of closed
            rows = db(query).select(r.table.id)
            s3.dataTableStyleDisabled = [str(row.id) for row in rows]
            s3.dataTableStyleWarning = [str(row.id) for row in rows]
            query = (r.table.status == 1) # Status of Pending
            rows = db(query).select(r.table.id)
            s3.dataTableStyleAlert = [str(row.id) for row in rows]
            query = (r.table.status == 4) # Status of Master
            rows = db(query).select(r.table.id)
            s3.dataTableStyleWarning.extend(str(row.id) for row in rows)
            s3db.configure(r.tablename,
                            orderby = "%s.status" % r.tablename,
                            create_next = URL(c="survey", f="template"),
                            update_next = URL(c="survey", f="template"),
                            )
        return True
    s3.prep = prep

    # Post-processor
    def postp(r, output):
        if r.component:
            template_id = request.args[0]
            if r.component_name == "section":
                # Add the section select widget to the form
                # Where is this defined?
                sectionSelect = s3.survey_section_select_widget(template_id)
                output.update(form = sectionSelect)
                return output
            elif r.component_name == "translate":
                s3_action_buttons(r)
                s3.actions.append(
                                   dict(label=str(T("Download")),
                                        _class="action-btn",
                                        url=URL(c=module,
                                                f="templateTranslateDownload",
                                                args=["[id]"])
                                       ),
                                  )
                s3.actions.append(
                           dict(label=str(T("Upload")),
                                _class="action-btn",
                                url=URL(c=module,
                                        f="template",
                                        args=[template_id,"translate","[id]"])
                               ),
                          )
                return output


        # Add a button to show what the questionnaire looks like
        #s3_action_buttons(r)
        #s3.actions = s3.actions + [
        #                       dict(label=str(T("Display")),
        #                            _class="action-btn",
        #                            url=URL(c=module,
        #                                    f="templateRead",
        #                                    args=["[id]"])
        #                           ),
        #                      ]

        return output
    s3.postp = postp

    if request.ajax:
        post = request.post_vars
        action = post.get("action")
        template_id = post.get("parent_id")
        section_id = post.get("section_id")
        section_text = post.get("section_text")
        if action == "section" and template_id != None:
            id = db.survey_section.insert(name=section_text,
                                          template_id=template_id,
                                          cloned_section_id=section_id)
            if id == None:
                print "Failed to insert record"
            return

    # remove CRUD generated buttons in the tabs
    s3db.configure("survey_template",
                    listadd=False,
                    deletable=False,
                   )
    output = s3_rest_controller(rheader=s3db.survey_template_rheader)

    return output

# -----------------------------------------------------------------------------
def templateRead():
    """
    """

    if "vars" in request and len(request.vars) > 0:
        dummy, template_id = request.vars.viewing.split(".")
    else:
        template_id = request.args[0]

    def postp(r, output):
        if r.interactive:
            template_id = r.id
            form = s3db.survey_buildQuestionnaireFromTemplate(template_id)
            output["items"] = None
            output["form"] = None
            output["item"] = form
            output["title"] = s3.crud_strings["survey_template"].title_question_details
            return output
    s3.postp = postp

    # remove CRUD generated buttons in the tabs
    s3db.configure("survey_template",
                    listadd=False,
                    editable=False,
                    deletable=False,
                   )

    r = s3_request("survey", "template", args=[template_id])
    output  = r(method = "read", rheader=s3db.survey_template_rheader)
    return output

# -----------------------------------------------------------------------------
def templateSummary():
    """
    """

    # Load Model
    resourcename = "template"
    tablename = "%s_%s" % (module, resourcename)
    s3db.table(tablename)
    s3db.table("survey_complete")
    crud_strings = s3.crud_strings[tablename]

    def postp(r, output):
        if r.interactive:
            if "vars" in request and len(request.vars) > 0:
                dummy, template_id = request.vars.viewing.split(".")
            else:
                template_id = r.id
            form = s3.survey_build_template_summary(template_id)
            output["items"] = form
            output["sortby"] = [[0, "asc"]]
            output["title"] = crud_strings.title_analysis_summary
            output["subtitle"] = crud_strings.subtitle_analysis_summary
            return output

    # remove CRUD generated buttons in the tabs
    s3db.configure(tablename,
                    listadd=False,
                    deletable=False,
                   )

    s3.postp = postp
    output = s3_rest_controller(module,
                                resourcename,
                                method = "list",
                                rheader=s3.survey_template_rheader
                               )
    s3.actions = None
    return output

# -----------------------------------------------------------------------------
def templateTranslateDownload():
    """
    """

    # Load Model
    resourcename = "translate"
    tablename = "%s_%s" % (module, resourcename)
    s3db.table("survey_template")
    s3db.table("survey_translate")
    s3db.table("survey_complete")

    try:
        import xlwt
    except ImportError:
        redirect(URL(c="survey",
                     f="templateTranslation",
                     args=[],
                     vars = {}))
    record = s3.survey_getTranslation(request.args[0])
    if record == None:
        redirect(URL(c="survey",
                     f="templateTranslation",
                     args=[],
                     vars = {}))
    code = record.code
    language = record.language
    lang_fileName = "applications/%s/languages/%s.py" % \
                                    (appname, code)
    try:
        from gluon.languages import read_dict
        strings = read_dict(lang_fileName)
    except:
        strings = dict()
    template_id = record.template_id
    template = s3.survey_getTemplate(template_id)
    book = xlwt.Workbook(encoding="utf-8")
    sheet = book.add_sheet(language)
    output = StringIO()
    qstnList = s3.survey_getAllQuestionsForTemplate(template_id)
    original = {}
    original[template["name"]] = True
    if template["description"] != "":
        original[template["description"]] = True
    for qstn in qstnList:
        original[qstn["name"]] = True
        widgetObj = survey_question_type[qstn["type"]](question_id = qstn["qstn_id"])
        if isinstance(widgetObj, S3QuestionTypeOptionWidget):
            optionList = widgetObj.getList()
            for option in optionList:
                original[option] = True
    sections = s3.survey_getAllSectionsForTemplate(template_id)
    for section in sections:
        original[section["name"]]=True
        section_id = section["section_id"]
        layoutRules = s3.survey_getQstnLayoutRules(template_id, section_id)
        layoutStr = str(layoutRules)
        posn = layoutStr.find("heading")
        while posn != -1:
            start = posn + 11
            end = layoutStr.find("}", start)
            original[layoutStr[start:end]] = True
            posn = layoutStr.find("heading", end)

    row = 0
    sheet.write(row,
                0,
                unicode("Original")
               )
    sheet.write(row,
                1,
                unicode("Translation")
               )
    originalList = original.keys()
    originalList.sort()
    for text in originalList:
        row += 1
        original = unicode(text)
        sheet.write(row,
                    0,
                    original
                   )
        if (original in strings):
            sheet.write(row,
                        1,
                        strings[original]
                       )

    book.save(output)
    output.seek(0)
    response.headers["Content-Type"] = contenttype(".xls")
    filename = "%s.xls" % code
    response.headers["Content-disposition"] = "attachment; filename=\"%s\"" % filename
    return output.read()

# -----------------------------------------------------------------------------
def series():
    """ RESTful CRUD controller """

    # Load Model
    tablename = "%s_%s" % (module, resourcename)
    table = s3db[tablename]
    s3.survey_answerlist_dataTable_pre()

    def prep(r):
        if r.interactive:
            if r.method == "create":
                allTemplates = s3.survey_getAllTemplates()
                if len(allTemplates) == 0:
                    session.warning = T("You need to create a template before you can create a series")
                    redirect(URL(c="survey",
                             f="template",
                             args=[],
                             vars = {}))
            if r.id and (r.method == "update"):
                table.template_id.writable = False
        return True

    def postp(r, output):
        if request.ajax == True and r.method == "read":
            return output["item"]
        if r.component_name == None:
            s3.survey_serieslist_dataTable_post(r)
        elif r.component_name == "complete":<commit_msg>Ensuring that User does not enter Start Date > End Date<commit_after># -*- coding: utf-8 -*-

"""
    survey - Assessment Data Analysis Tool

    For more details see the blueprint at:
    http://eden.sahanafoundation.org/wiki/BluePrint/SurveyTool/ADAT

    @todo: open template from the dataTables into the section tab not update
    @todo: in the pages that add a link to a template make the combobox display the label not the numbers
    @todo: restrict the deletion of a template to only those with status Pending
"""

module = request.controller
resourcename = request.function

if not settings.has_module(module):
    raise HTTP(404, body="Module disabled: %s" % module)

try:
    from cStringIO import StringIO    # Faster, where available
except:
    from StringIO import StringIO

from gluon.contenttype import contenttype

from s3survey import S3AnalysisPriority, \
                     survey_question_type, \
                     survey_analysis_type, \
                     getMatrix, \
                     DEBUG, \
                     LayoutBlocks, \
                     DataMatrix, MatrixElement, \
                     S3QuestionTypeOptionWidget, \
                     survey_T

# -----------------------------------------------------------------------------
def index():
    """ Module's Home Page """

    module_name = settings.modules[module].name_nice
    response.title = module_name
    return dict(module_name=module_name)

# -----------------------------------------------------------------------------
def template():
    """ RESTful CRUD controller """

    # Load Model
    table = s3db.survey_template

    def prep(r):
        if r.component and r.component_name == "translate":
            table = s3db.survey_translate
            if r.component_id == None:
                # list existing translations and allow the addition of a new translation
                table.file.readable = False
                table.file.writable = False
            else:
                # edit the selected translation
                table.language.writable = False
                table.code.writable = False
            # remove CRUD generated buttons in the tabs
            s3db.configure(table,
                            deletable=False)
        else:
            s3_action_buttons(r)
            query = (r.table.status == 1) # Status of Pending
            rows = db(query).select(r.table.id)
            try:
                s3.actions[1]["restrict"].extend(str(row.id) for row in rows)
            except KeyError: # the restrict key doesn't exist
                s3.actions[1]["restrict"] = [str(row.id) for row in rows]
            except IndexError: # the delete buttons doesn't exist
                pass
            # Add some highlighting to the rows
            query = (r.table.status == 3) # Status of closed
            rows = db(query).select(r.table.id)
            s3.dataTableStyleDisabled = [str(row.id) for row in rows]
            s3.dataTableStyleWarning = [str(row.id) for row in rows]
            query = (r.table.status == 1) # Status of Pending
            rows = db(query).select(r.table.id)
            s3.dataTableStyleAlert = [str(row.id) for row in rows]
            query = (r.table.status == 4) # Status of Master
            rows = db(query).select(r.table.id)
            s3.dataTableStyleWarning.extend(str(row.id) for row in rows)
            s3db.configure(r.tablename,
                            orderby = "%s.status" % r.tablename,
                            create_next = URL(c="survey", f="template"),
                            update_next = URL(c="survey", f="template"),
                            )
        return True
    s3.prep = prep

    # Post-processor
    def postp(r, output):
        if r.component:
            template_id = request.args[0]
            if r.component_name == "section":
                # Add the section select widget to the form
                # Where is this defined?
                sectionSelect = s3.survey_section_select_widget(template_id)
                output.update(form = sectionSelect)
                return output
            elif r.component_name == "translate":
                s3_action_buttons(r)
                s3.actions.append(
                                   dict(label=str(T("Download")),
                                        _class="action-btn",
                                        url=URL(c=module,
                                                f="templateTranslateDownload",
                                                args=["[id]"])
                                       ),
                                  )
                s3.actions.append(
                           dict(label=str(T("Upload")),
                                _class="action-btn",
                                url=URL(c=module,
                                        f="template",
                                        args=[template_id,"translate","[id]"])
                               ),
                          )
                return output


        # Add a button to show what the questionnaire looks like
        #s3_action_buttons(r)
        #s3.actions = s3.actions + [
        #                       dict(label=str(T("Display")),
        #                            _class="action-btn",
        #                            url=URL(c=module,
        #                                    f="templateRead",
        #                                    args=["[id]"])
        #                           ),
        #                      ]

        return output
    s3.postp = postp

    if request.ajax:
        post = request.post_vars
        action = post.get("action")
        template_id = post.get("parent_id")
        section_id = post.get("section_id")
        section_text = post.get("section_text")
        if action == "section" and template_id != None:
            id = db.survey_section.insert(name=section_text,
                                          template_id=template_id,
                                          cloned_section_id=section_id)
            if id == None:
                print "Failed to insert record"
            return

    # remove CRUD generated buttons in the tabs
    s3db.configure("survey_template",
                    listadd=False,
                    deletable=False,
                   )
    output = s3_rest_controller(rheader=s3db.survey_template_rheader)

    return output

# -----------------------------------------------------------------------------
def templateRead():
    """
    """

    if "vars" in request and len(request.vars) > 0:
        dummy, template_id = request.vars.viewing.split(".")
    else:
        template_id = request.args[0]

    def postp(r, output):
        if r.interactive:
            template_id = r.id
            form = s3db.survey_buildQuestionnaireFromTemplate(template_id)
            output["items"] = None
            output["form"] = None
            output["item"] = form
            output["title"] = s3.crud_strings["survey_template"].title_question_details
            return output
    s3.postp = postp

    # remove CRUD generated buttons in the tabs
    s3db.configure("survey_template",
                    listadd=False,
                    editable=False,
                    deletable=False,
                   )

    r = s3_request("survey", "template", args=[template_id])
    output  = r(method = "read", rheader=s3db.survey_template_rheader)
    return output

# -----------------------------------------------------------------------------
def templateSummary():
    """
    """

    # Load Model
    resourcename = "template"
    tablename = "%s_%s" % (module, resourcename)
    s3db.table(tablename)
    s3db.table("survey_complete")
    crud_strings = s3.crud_strings[tablename]

    def postp(r, output):
        if r.interactive:
            if "vars" in request and len(request.vars) > 0:
                dummy, template_id = request.vars.viewing.split(".")
            else:
                template_id = r.id
            form = s3.survey_build_template_summary(template_id)
            output["items"] = form
            output["sortby"] = [[0, "asc"]]
            output["title"] = crud_strings.title_analysis_summary
            output["subtitle"] = crud_strings.subtitle_analysis_summary
            return output

    # remove CRUD generated buttons in the tabs
    s3db.configure(tablename,
                    listadd=False,
                    deletable=False,
                   )

    s3.postp = postp
    output = s3_rest_controller(module,
                                resourcename,
                                method = "list",
                                rheader=s3.survey_template_rheader
                               )
    s3.actions = None
    return output

# -----------------------------------------------------------------------------
def templateTranslateDownload():
    """
    """

    # Load Model
    resourcename = "translate"
    tablename = "%s_%s" % (module, resourcename)
    s3db.table("survey_template")
    s3db.table("survey_translate")
    s3db.table("survey_complete")

    try:
        import xlwt
    except ImportError:
        redirect(URL(c="survey",
                     f="templateTranslation",
                     args=[],
                     vars = {}))
    record = s3.survey_getTranslation(request.args[0])
    if record == None:
        redirect(URL(c="survey",
                     f="templateTranslation",
                     args=[],
                     vars = {}))
    code = record.code
    language = record.language
    lang_fileName = "applications/%s/languages/%s.py" % \
                                    (appname, code)
    try:
        from gluon.languages import read_dict
        strings = read_dict(lang_fileName)
    except:
        strings = dict()
    template_id = record.template_id
    template = s3.survey_getTemplate(template_id)
    book = xlwt.Workbook(encoding="utf-8")
    sheet = book.add_sheet(language)
    output = StringIO()
    qstnList = s3.survey_getAllQuestionsForTemplate(template_id)
    original = {}
    original[template["name"]] = True
    if template["description"] != "":
        original[template["description"]] = True
    for qstn in qstnList:
        original[qstn["name"]] = True
        widgetObj = survey_question_type[qstn["type"]](question_id = qstn["qstn_id"])
        if isinstance(widgetObj, S3QuestionTypeOptionWidget):
            optionList = widgetObj.getList()
            for option in optionList:
                original[option] = True
    sections = s3.survey_getAllSectionsForTemplate(template_id)
    for section in sections:
        original[section["name"]]=True
        section_id = section["section_id"]
        layoutRules = s3.survey_getQstnLayoutRules(template_id, section_id)
        layoutStr = str(layoutRules)
        posn = layoutStr.find("heading")
        while posn != -1:
            start = posn + 11
            end = layoutStr.find("}", start)
            original[layoutStr[start:end]] = True
            posn = layoutStr.find("heading", end)

    row = 0
    sheet.write(row,
                0,
                unicode("Original")
               )
    sheet.write(row,
                1,
                unicode("Translation")
               )
    originalList = original.keys()
    originalList.sort()
    for text in originalList:
        row += 1
        original = unicode(text)
        sheet.write(row,
                    0,
                    original
                   )
        if (original in strings):
            sheet.write(row,
                        1,
                        strings[original]
                       )

    book.save(output)
    output.seek(0)
    response.headers["Content-Type"] = contenttype(".xls")
    filename = "%s.xls" % code
    response.headers["Content-disposition"] = "attachment; filename=\"%s\"" % filename
    return output.read()

# -----------------------------------------------------------------------------
def series():
    """ RESTful CRUD controller """

    # Load Model
    tablename = "%s_%s" % (module, resourcename)
    table = s3db[tablename]
    s3.survey_answerlist_dataTable_pre()

    def prep(r):
        if r.interactive:
            if r.method == "create":
                allTemplates = s3.survey_getAllTemplates()
                if len(allTemplates) == 0:
                    session.warning = T("You need to create a template before you can create a series")
                    redirect(URL(c="survey",
                             f="template",
                             args=[],
                             vars = {}))
            if r.id and (r.method == "update"):
                table.template_id.writable = False
        return True

    def postp(r, output):
        if request.ajax == True and r.method == "read":
            return output["item"]
        if r.component_name == None:
            
	    # Set the minimum end_date to the same as the start_date
            s3.jquery_ready.append(
'''S3.start_end_date('survey_series_start_date','survey_series_end_date')''')

            s3.survey_serieslist_dataTable_post(r)
        elif r.component_name == "complete":
{'fraction_non_alphanumeric': 0.12913026965438662, 'fraction_numerical': 0.006304595518420053, 'mean_word_length': 1.3954694323144106, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 50, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7018830', 'n_tokens_mistral': 6958, 'n_tokens_neox': 6693, 'n_words': 1502}