Source code for uw.local.teaching.webui.admin_exams_edit

"""Assessment creation form implementation.

This is the form to create a new assessment.
"""

from ll.xist.ns import html

from uw.web.wsgi import status
from uw.web.wsgi.delegate import delegate_get_post
from uw.web.wsgi.form import use_form_param
from uw.web.wsgi.function import return_html

from uw.web.html.form import render_select
from uw.web.html.format import format_return

from ...util.identity import use_remote_identity

from .exam_edit import write_edit_form, write_create_controls, handle_exam_create_form

[docs]def render_exam_create_form (cursor): """Render the assessment creation form. :param cursor: DB connection cursor :return: HTML form to select a type of assessment to create :rtype: HTML form <form> """ exam_series = cursor.execute_tuples ("select series_code, series_description from exam_exam_series where series_code not in ('F', 'LF', 'L') order by series_description") exam_series.insert(0, ("", "Choose assessment")) return html.form ( html.p ( render_select ("series", exam_series, ""), ' ', html.input (type="submit", value="Add…") ), method="get", action="exam-create" )
@use_remote_identity @return_html def exam_create_get_handler (cursor, term, admin, remote_identity, roles, form): """Assessment creation form GET URL handler. :param cursor: DB connection cursor :param term: the relevant term :param admin: the relevant admin unit :param roles: the active permissions roles :param form: CGI form results, expects an assessment series to be chosen in the form results :return: tuple of (description, HTML elements for form to fill in the most important details about the assessment) :rtype: (str, list) """ if not 'ISC' in roles: raise status.HTTPForbidden () result = [format_return ('Main Menu', None, None, dot='Offering')] series_code = form.optional_field_value ('series') sequence = cursor.execute_optional_tuple ("select * from exam_exam_series natural left join (select series_code, max(series_sequence) as series_sequence from exam_exam where (term_id, admin_id) = (%(term_id)s, %(admin_id)s) group by series_code) as t where series_code=%(series_code)s", term_id=term.code (), admin_id=admin.admin_id, series_code=series_code) if sequence is None: # Invalid series result.append (html.p ('Please select an assessment type and try again. ', html.a ('Return to offering', href="."), '.')) new_description = "" else: new_description = sequence.series_description if sequence.series_sequence is None: sequence_num=1 else: sequence_num = sequence.series_sequence + 1 new_description += ' %d' % sequence_num result.append ( write_edit_form (write_create_controls (cursor, term, admin, remote_identity=remote_identity), html.p ( 'Please fill in as much of the following as possible:', html.input (type="hidden", name="seq", value=sequence_num), html.input (type="hidden", name="series", value=series_code), ), html.input (type="submit", name="!create", value="Create!") ) ) return "%s (%s): Create %s Assessment" % (admin.admin_description, term.description (), new_description), result @return_html def exam_create_post_handler (cursor, term, admin, roles, form): """Assessment creation form POST URL handler. :param cursor: DB connection cursor :param term: the relevant term :param admin: the relevant admin unit :param roles: the active permissions roles :param form: CGI form results Creates a new assessment and sets its characteristics based on the form results. """ if not 'ISC' in roles: raise status.HTTPForbidden () series = form.required_field_value ('series') sequence = int (form.required_field_value ('seq')) exam_scanning = form.optional_field_value ("scanning") if not exam_scanning: return 'Error: No scanning integration option chosen', html.p ('Please select whether or not you want Crowdmark or Markbox and try again. ', html.a ('Go back.', href="javascript:history.go(-1)")) exam_crowdmark = exam_scanning == 'C' exam = cursor.execute_required_tuple ("insert into exam_exam (term_id, admin_id, series_code, series_sequence) values (%(term_id)s, %(admin_id)s, %(series)s, %(sequence)s) returning *", term_id=term.code (), admin_id=admin.admin_id, series=series, sequence=sequence) result = handle_exam_create_form (cursor, form, exam) if result is None: cursor.callproc_none ("exam_exam_crowdmark_set", exam.exam_id, exam_crowdmark) cursor.callproc_none ("exam_exam_markbox_set", exam.exam_id, exam_scanning == 'M') scanning_url = 'edit-scanning' if exam_crowdmark else '' raise status.HTTPFound ("exam/%d/%s" % (exam.exam_id, scanning_url)) else: cursor.connection.rollback () return result exam_create_handler = use_form_param ( delegate_get_post (exam_create_get_handler, exam_create_post_handler) )