Exemple #1
0
def submit_explain(lti=lti):
    assignment_id = request.form.get('question_id', None)
    lis_result_sourcedid = request.form.get('lis_result_sourcedid', None)
    if assignment_id is None:
        return jsonify(success=False, message="No Assignment ID given!")
    user = User.from_lti("canvas", session["pylti_user_id"],
                         session.get("user_email", ""),
                         session.get("lis_person_name_given", ""),
                         session.get("lis_person_name_family", ""))
    assignment = Assignment.by_id(assignment_id)
    submission = Submission.save_correct(user.id, assignment_id)
    code, elements = submission.load_explanation(5)
    if lis_result_sourcedid is None:
        return jsonify(success=False, message="Not in a grading context.")
    hl_lines = [e['line'][0] for e in elements]
    message = """<h1>Code Annotation</h1>
    <div>Thank you for submitting. This activity will be graded manually, so please be patient!</div>
    <div><ul><li>{explanations}</li></ul></div>
    <div>{code}</div>
    """.format(code=highlight(
        code, PythonLexer(),
        HtmlFormatter(linenos=True, hl_lines=hl_lines, noclasses=True)),
               explanations='</li><li>'.join([
                   '<b>{line} ({type}):</b> {answer}'.format(
                       line=e['line'][0],
                       answer=e['answer'],
                       type=Submission.abbreviate_element_type(e['name']))
                   for e in sorted(elements, key=lambda e: e['line'][0])
               ]))
    lti.post_grade(0, message, endpoint=lis_result_sourcedid)
    return jsonify(success=True)
 def lti_wrapper(*args, **kwargs):
     """
     Pass LTI reference to function or return error.
     """
     try:
         the_lti = LTI(lti_args, lti_kwargs)
         the_lti.verify()
         kwargs['lti'] = the_lti
         old_user = g.user
         g.user = User.from_lti("canvas", 
                                session["pylti_user_id"], 
                                session.get("lis_person_contact_email_primary", ""),
                                session.get("lis_person_name_given", "Canvas"),
                                session.get("lis_person_name_family", "User"))
         g.roles = session["roles"].split(",") if "roles" in session else []
         g.course = Course.from_lti("canvas", 
                                session["context_id"], 
                                session.get("context_title", ""), 
                                g.user.id)
         session['lti_course'] = g.course.id
         g.user.update_roles(g.roles, g.course.id)
         if old_user != g.user:
             flask_security.utils.logout_user()
             flask_security.utils.login_user(g.user, remember = True)
         if not old_user:
             flask_security.utils.login_user(g.user, remember = True)
     except LTIException as lti_exception:
         kwargs['lti'] = None
         kwargs['lti_exception'] = dict()
         kwargs['lti_exception']['exception'] = lti_exception
         kwargs['lti_exception']['kwargs'] = kwargs
         kwargs['lti_exception']['args'] = args
         flash("LTI Error: "+str(lti_exception)+".\nTry reloading!")
     return function(*args, **kwargs)
Exemple #3
0
 def lti_wrapper(*args, **kwargs):
     """
     Pass LTI reference to function or return error.
     """
     try:
         the_lti = LTI(lti_args, lti_kwargs)
         the_lti.verify()
         kwargs['lti'] = the_lti
         old_user = g.user
         g.user = User.from_lti("canvas", 
                                session["pylti_user_id"], 
                                session.get("lis_person_contact_email_primary", ""),
                                session.get("lis_person_name_given", "Canvas"),
                                session.get("lis_person_name_family", "User"))
         g.roles = session["roles"].split(",") if "roles" in session else []
         g.course = Course.from_lti("canvas", 
                                session["context_id"], 
                                session.get("context_title", ""), 
                                g.user.id)
         session['lti_course'] = g.course.id
         g.user.update_roles(g.roles, g.course.id)
         if old_user != g.user:
             flask_security.utils.logout_user()
             flask_security.utils.login_user(g.user, remember = True)
         if not old_user:
             flask_security.utils.login_user(g.user, remember = True)
     except LTIException as lti_exception:
         kwargs['lti'] = None
         kwargs['lti_exception'] = dict()
         kwargs['lti_exception']['exception'] = lti_exception
         kwargs['lti_exception']['kwargs'] = kwargs
         kwargs['lti_exception']['args'] = args
         flash("LTI Error: "+str(lti_exception)+".\nTry reloading!")
     return function(*args, **kwargs)
Exemple #4
0
def upload(lti=lti):
    assignment_id = request.values.get('assignment_id', None)
    max_questions = int(request.values.get('max_questions', '5'))
    if assignment_id is None:
        return jsonify(success=False, invalid=False, message="No Assignment ID given!")
    user = User.from_lti("canvas", session["pylti_user_id"], 
                         session.get("user_email", ""),
                         session.get("lis_person_name_given", ""),
                         session.get("lis_person_name_family", ""))
    submission = Submission.load(user.id, assignment_id)
    
    # Get the uploaded information
    data_file = request.files.get('files')
    if not data_file:
        return jsonify(success=False, invalid=True, message="No data file!")
    code_submission = data_file.read()
    try:
        elements = find_elements(code_submission)
    except SyntaxError:
        return jsonify(success=True, invalid=True, message="Your python file has errors in it.")
    submission_destructured = submission.save_explanation_code(code_submission, elements)
    
    code, elements = submission.load_explanation(max_questions)
    
    return jsonify(success=True, invalid=False, code=code, elements=elements)
Exemple #5
0
def save_correct(lti=lti):
    assignment_id = request.form.get('question_id', None)
    status = float(request.form.get('status', "0.0"))
    lis_result_sourcedid = request.form.get('lis_result_sourcedid', None)
    if assignment_id is None:
        return jsonify(success=False, message="No Assignment ID given!")
    user = User.from_lti("canvas", session["pylti_user_id"], 
                         session.get("user_email", ""),
                         session.get("lis_person_name_given", ""),
                         session.get("lis_person_name_family", ""))
    assignment = Assignment.by_id(assignment_id)
    if status == 1:
        submission = Submission.save_correct(user.id, assignment_id)
    else:
        submission = assignment.get_submission(user.id)
    if submission.correct:
        message = "Success!"
    else:
        message = "Incomplete"
    url = url_for('lti_assignments.get_submission_code', submission_id=submission.id, _external=True)
    if lis_result_sourcedid is None:
        return jsonify(success=False, message="Not in a grading context.")
    if assignment.mode == 'maze':
        lti.post_grade(float(submission.correct), "<h1>{0}</h1>".format(message), endpoint=lis_result_sourcedid);
    else:
        lti.post_grade(float(submission.correct), "<h1>{0}</h1>".format(message)+"<div>Latest work in progress: <a href='{0}' target='_blank'>View</a></div>".format(url)+"<div>Touches: {0}</div>".format(submission.version)+"Last ran code:<br>"+highlight(submission.code, PythonLexer(), HtmlFormatter()), endpoint=lis_result_sourcedid)
    return jsonify(success=True)
Exemple #6
0
def submit_explain(lti=lti):
    assignment_id = request.form.get('question_id', None)
    lis_result_sourcedid = request.form.get('lis_result_sourcedid', None)
    if assignment_id is None:
        return jsonify(success=False, message="No Assignment ID given!")
    user = User.from_lti("canvas", session["pylti_user_id"], 
                         session.get("user_email", ""),
                         session.get("lis_person_name_given", ""),
                         session.get("lis_person_name_family", ""))
    assignment = Assignment.by_id(assignment_id)
    submission = Submission.save_correct(user.id, assignment_id)
    code, elements = submission.load_explanation(5)
    if lis_result_sourcedid is None:
        return jsonify(success=False, message="Not in a grading context.")
    hl_lines  = [e['line'][0] for e in elements]
    message = """<h1>Code Annotation</h1>
    <div>Thank you for submitting. This activity will be graded manually, so please be patient!</div>
    <div><ul><li>{explanations}</li></ul></div>
    <div>{code}</div>
    """.format(
        code = highlight(code, PythonLexer(), HtmlFormatter(linenos=True, hl_lines=hl_lines, noclasses=True)),
        explanations = '</li><li>'.join(
            ['<b>{line} ({type}):</b> {answer}'.format(line=e['line'][0], 
                                                      answer=e['answer'], 
                                                      type=Submission.abbreviate_element_type(e['name']))
             for e in sorted(elements, key=lambda e: e['line'][0])])
        )
    lti.post_grade(0, message, endpoint=lis_result_sourcedid)
    return jsonify(success=True)
Exemple #7
0
def upload(lti=lti):
    assignment_id = request.values.get('assignment_id', None)
    max_questions = int(request.values.get('max_questions', '5'))
    if assignment_id is None:
        return jsonify(success=False,
                       invalid=False,
                       message="No Assignment ID given!")
    user = User.from_lti("canvas", session["pylti_user_id"],
                         session.get("user_email", ""),
                         session.get("lis_person_name_given", ""),
                         session.get("lis_person_name_family", ""))
    submission = Submission.load(user.id, assignment_id)

    # Get the uploaded information
    data_file = request.files.get('files')
    if not data_file:
        return jsonify(success=False, invalid=True, message="No data file!")
    code_submission = data_file.read()
    try:
        elements = find_elements(code_submission)
    except SyntaxError:
        return jsonify(success=True,
                       invalid=True,
                       message="Your python file has errors in it.")
    submission_destructured = submission.save_explanation_code(
        code_submission, elements)

    code, elements = submission.load_explanation(max_questions)

    return jsonify(success=True, invalid=False, code=code, elements=elements)
Exemple #8
0
def download():
    assignment_id = request.values.get('assignment_id', None)
    if assignment_id is None:
        return jsonify(success=False, message="No Assignment ID given!")
    user = User.from_lti("canvas", session["pylti_user_id"],
                         session.get("user_email", ""),
                         session.get("lis_person_name_given", ""),
                         session.get("lis_person_name_family", ""))
    submission = Submission.load(user.id, assignment_id)
    submission_destructured = submission.load_explanation()
    return jsonify(success=True, **submission_destructured)
Exemple #9
0
def download():
    assignment_id = request.values.get('assignment_id', None)
    if assignment_id is None:
        return jsonify(success=False, message="No Assignment ID given!")
    user = User.from_lti("canvas", session["pylti_user_id"], 
                         session.get("user_email", ""),
                         session.get("lis_person_name_given", ""),
                         session.get("lis_person_name_family", ""))
    submission = Submission.load(user.id, assignment_id)
    submission_destructured = submission.load_explanation()
    return jsonify(success=True, **submission_destructured)
Exemple #10
0
def save_events(lti=lti):
    assignment_id = request.form.get('question_id', None)
    event = request.form.get('event', "blank")
    action = request.form.get('action', "missing")
    if assignment_id is None:
        return jsonify(success=False, message="No Assignment ID given!")
    user = User.from_lti("canvas", session["pylti_user_id"], 
                         session.get("user_email", ""),
                         session.get("lis_person_name_given", ""),
                         session.get("lis_person_name_family", ""))
    log = Log.new(event, action, assignment_id, user.id)
    return jsonify(success=True)
Exemple #11
0
def save_events(lti=lti):
    assignment_id = request.form.get('question_id', None)
    event = request.form.get('event', "blank")
    action = request.form.get('action', "missing")
    if assignment_id is None:
        return jsonify(success=False, message="No Assignment ID given!")
    user = User.from_lti("canvas", session["pylti_user_id"],
                         session.get("user_email", ""),
                         session.get("lis_person_name_given", ""),
                         session.get("lis_person_name_family", ""))
    log = Log.new(event, action, assignment_id, user.id)
    return jsonify(success=True)
Exemple #12
0
def save_explain(lti=lti):
    assignment_id = request.form.get('question_id', None)
    assignment_version = int(request.form.get('version', -1))
    if assignment_id is None:
        return jsonify(success=False, message="No Assignment ID given!")
    answer = request.form.get('answer', '')
    name = request.form.get('name', '')
    user = User.from_lti("canvas", session["pylti_user_id"], 
                         session.get("user_email", ""),
                         session.get("lis_person_name_given", ""),
                         session.get("lis_person_name_family", ""))
    Submission.save_explanation_answer(user.id, assignment_id, name, answer)
    return jsonify(success=True)
Exemple #13
0
def save_explain(lti=lti):
    assignment_id = request.form.get('question_id', None)
    assignment_version = int(request.form.get('version', -1))
    if assignment_id is None:
        return jsonify(success=False, message="No Assignment ID given!")
    answer = request.form.get('answer', '')
    name = request.form.get('name', '')
    user = User.from_lti("canvas", session["pylti_user_id"],
                         session.get("user_email", ""),
                         session.get("lis_person_name_given", ""),
                         session.get("lis_person_name_family", ""))
    Submission.save_explanation_answer(user.id, assignment_id, name, answer)
    return jsonify(success=True)
Exemple #14
0
def ensure_canvas_arguments():
    '''
    Translates the current session data into a valid user
    '''
    user = User.from_lti("canvas", session["pylti_user_id"],
                         session.get("lis_person_contact_email_primary", ""),
                         session.get("lis_person_name_given", "Canvas"),
                         session.get("lis_person_name_family", "User"))
    if "roles" in session:
        roles = session["roles"].split(",")
    else:
        roles = []
    course = Course.from_lti("canvas", session["context_id"],
                             session.get("context_title", ""), user.id)
    return user, roles, course
Exemple #15
0
def grade(lti=lti):
    """ post grade

    :param lti: the `lti` object from `pylti`
    :return: grade rendered by grade.html template
    """
    assignment_id = request.form.get('question_id', None)
    if assignment_id is None:
        return jsonify(success=False, message="No Assignment ID given!")
    user = User.from_lti("canvas", session["pylti_user_id"], 
                         session.get("user_email", ""))
    submission = Submission.save_correct(user.id, assignment_id)
    if 'lis_result_sourcedid' not in session:
        return "Failure"
    #session[''] = session['lis_outcome_service_url']
    lti.post_grade(1, "<h1>Success</h1>"+highlight(submission.code, PythonLexer(), HtmlFormatter()))
    return "Successful!"
Exemple #16
0
def ensure_canvas_arguments():
    '''
    Translates the current session data into a valid user
    '''
    user = User.from_lti("canvas", 
                         session["pylti_user_id"], 
                         session.get("lis_person_contact_email_primary", ""),
                         session.get("lis_person_name_given", "Canvas"),
                         session.get("lis_person_name_family", "User"))
    if "roles" in session:
        roles = session["roles"].split(",")
    else:
        roles = []
    course = Course.from_lti("canvas", 
                             session["context_id"], 
                             session.get("context_title", ""), 
                             user.id)
    return user, roles, course
Exemple #17
0
def grade(lti=lti):
    """ post grade

    :param lti: the `lti` object from `pylti`
    :return: grade rendered by grade.html template
    """
    assignment_id = request.form.get('question_id', None)
    if assignment_id is None:
        return jsonify(success=False, message="No Assignment ID given!")
    user = User.from_lti("canvas", session["pylti_user_id"],
                         session.get("user_email", ""))
    submission = Submission.save_correct(user.id, assignment_id)
    if 'lis_result_sourcedid' not in session:
        return "Failure"
    #session[''] = session['lis_outcome_service_url']
    lti.post_grade(
        1, "<h1>Success</h1>" +
        highlight(submission.code, PythonLexer(), HtmlFormatter()))
    return "Successful!"
Exemple #18
0
def upload():
    assignment_id = request.values.get('assignment_id', None)
    if assignment_id is None:
        return jsonify(success=False, message="No Assignment ID given!")
    user = User.from_lti("canvas", session["pylti_user_id"], 
                         session.get("user_email", ""),
                         session.get("lis_person_name_given", ""),
                         session.get("lis_person_name_family", ""))
    submission = Submission.load(user.id, assignment_id)
    
    # Get the uploaded information
    data_file = request.files.get('files')
    if not data_file:
        return jsonify(success=False, message="No data file!")
    code_submission = data_file.read()
    
    submission_destructured = submission.save_explanation_code(code_submission)
    
    return jsonify(success=True, **submission_destructured)
    
Exemple #19
0
def save_code(lti=lti):
    assignment_id = request.form.get('question_id', None)
    assignment_version = int(request.form.get('version', -1))
    if assignment_id is None:
        return jsonify(success=False, message="No Assignment ID given!")
    code = request.form.get('code', '')
    filename = request.form.get('filename', '__main__')
    user = User.from_lti("canvas", session["pylti_user_id"], 
                         session.get("user_email", ""),
                         session.get("lis_person_name_given", ""),
                         session.get("lis_person_name_family", ""))
    is_version_correct = True
    if filename == "__main__":
        submission, is_version_correct = Submission.save_code(user.id, assignment_id, code, assignment_version)
    elif User.is_lti_instructor(session["roles"]):
        if filename == "on_run":
            Assignment.edit(assignment_id=assignment_id, on_run=code)
        elif filename == "on_change":
            Assignment.edit(assignment_id=assignment_id, on_step=code)
        elif filename == "starting_code":
            Assignment.edit(assignment_id=assignment_id, on_start=code)
    return jsonify(success=True, is_version_correct=is_version_correct)
Exemple #20
0
def save_correct(lti=lti):
    assignment_id = request.form.get('question_id', None)
    status = float(request.form.get('status', "0.0"))
    lis_result_sourcedid = request.form.get('lis_result_sourcedid', None)
    if assignment_id is None:
        return jsonify(success=False, message="No Assignment ID given!")
    user = User.from_lti("canvas", session["pylti_user_id"],
                         session.get("user_email", ""),
                         session.get("lis_person_name_given", ""),
                         session.get("lis_person_name_family", ""))
    assignment = Assignment.by_id(assignment_id)
    if status == 1:
        submission = Submission.save_correct(user.id, assignment_id)
    else:
        submission = assignment.get_submission(user.id)
    if submission.correct:
        message = "Success!"
    else:
        message = "Incomplete"
    url = url_for('lti_assignments.get_submission_code',
                  submission_id=submission.id,
                  _external=True)
    if lis_result_sourcedid is None:
        return jsonify(success=False, message="Not in a grading context.")
    if assignment.mode == 'maze':
        lti.post_grade(float(submission.correct),
                       "<h1>{0}</h1>".format(message),
                       endpoint=lis_result_sourcedid)
    else:
        lti.post_grade(
            float(submission.correct),
            "<h1>{0}</h1>".format(message) +
            "<div>Latest work in progress: <a href='{0}' target='_blank'>View</a></div>"
            .format(url) +
            "<div>Touches: {0}</div>".format(submission.version) +
            "Last ran code:<br>" +
            highlight(submission.code, PythonLexer(), HtmlFormatter()),
            endpoint=lis_result_sourcedid)
    return jsonify(success=True)
Exemple #21
0
def save_code(lti=lti):
    assignment_id = request.form.get('question_id', None)
    assignment_version = int(request.form.get('version', -1))
    if assignment_id is None:
        return jsonify(success=False, message="No Assignment ID given!")
    code = request.form.get('code', '')
    filename = request.form.get('filename', '__main__')
    user = User.from_lti("canvas", session["pylti_user_id"],
                         session.get("user_email", ""),
                         session.get("lis_person_name_given", ""),
                         session.get("lis_person_name_family", ""))
    is_version_correct = True
    if filename == "__main__":
        submission, is_version_correct = Submission.save_code(
            user.id, assignment_id, code, assignment_version)
    elif User.is_lti_instructor(session["roles"]):
        if filename == "on_run":
            Assignment.edit(assignment_id=assignment_id, on_run=code)
        elif filename == "on_change":
            Assignment.edit(assignment_id=assignment_id, on_step=code)
        elif filename == "starting_code":
            Assignment.edit(assignment_id=assignment_id, on_start=code)
    return jsonify(success=True, is_version_correct=is_version_correct)