Example #1
0
def submissions_filter(course_id):
    ''' List all the users in the course '''
    is_grader = g.user.is_grader(int(course_id))
    if not is_grader:
        return "You are not an instructor or TA in this course!"
    course_id = int(course_id)
    course = Course.by_id(course_id)
    students = natsorted(course.get_students(), key=lambda r: r.name())
    assignments = natsorted([a for a, m in course.get_submitted_assignments()],
                            key=lambda r: r.name)
    criteria = request.values.get("criteria", "none")
    search_key = int(request.values.get("search_key", "-1"))
    submissions = []
    if criteria == "student":
        all_subs = Submission.by_student(search_key, course_id)
        all_subs = {s[0].assignment_id: s for s in all_subs}
        submissions = [all_subs.get(assignment.id, (None, None, assignment))
                       for assignment in assignments]
    elif criteria == "assignment":
        all_subs = Submission.by_assignment(search_key, course_id)
        all_subs = {s[0].user_id: s for s in all_subs}
        submissions = [all_subs.get(student.id, (None, student, None))
                       for student in students]
    return render_template('courses/submissions_filter.html',
                           course_id=course_id,
                           assignments=assignments,
                           students=students,
                           submissions= submissions,
                           criteria = criteria,
                           search_key = search_key,
                           is_instructor=is_grader)
Example #2
0
def submit_explain(lti=lti):
    assignment_id = request.form.get('assignment_id', None)
    course_id = request.values.get('course_id', g.course.id if 'course' in g else None)
    if None in (assignment_id, course_id):
        return jsonify(success=False, message="No Assignment ID or Course ID given!")
    assignment = Assignment.by_id(int(assignment_id))
    submission = Submission.save_correct(g.user.id, int(assignment_id), int(course_id))
    
    lis_result_sourcedid = request.form.get('lis_result_sourcedid', submission.url) or None
    code, elements = submission.load_explanation(4)
    if lis_result_sourcedid is None or lis_result_sourcedid == "NOT IN SESSION":
        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)
Example #3
0
def watch():
    assignment_list = request.values.get('assignments', '')
    assignments = [int(aid) for aid in assignment_list.split(',') if len(aid) > 0]
    course_id = request.values.get('course_id',  g.course.id if 'course' in g else None)
    if course_id == None or course_id == "":
        return jsonify(success=False, message="No Course ID given!")
    if g.user is None or not g.user.is_instructor(int(course_id)):
        return jsonify(success=False, message="You are not an instructor in this assignments' course.")
    update = request.values.get('update', 'false') == "true"
    if update:
        data = []
        for aid in assignments:
            submissions = Submission.by_assignment(aid, int(course_id))
            completions = sum([int(sua[0].correct) for sua in submissions])
            workings = len(Submission.get_latest(aid, int(course_id)))
            histories = [process_history([h['time'] for h in sua[0].get_history()])
                         for sua in submissions]
            touches = [int(sua[0].version) for sua in submissions]
            feedbacks = [l[0] for l in Log.calculate_feedbacks(aid, course_id)]
            data.append({'id': aid,
                         'Completions': completions,
                         'Workings': workings,
                         'Time': histories,
                         'Touches': touches,
                         'Feedbacks': feedbacks})
        return jsonify(success=True, data=data)
    else:
        assignments = [Assignment.by_id(aid) for aid in assignments]
        return render_template('blockpy/watch.html', course_id=course_id, assignments=assignments,
                               assignment_list=assignment_list)
Example #4
0
def submit_explain(lti=lti):
    assignment_id = request.form.get('assignment_id', None)
    course_id = request.values.get('course_id',
                                   g.course.id if 'course' in g else None)
    if None in (assignment_id, course_id):
        return jsonify(success=False,
                       message="No Assignment ID or Course ID given!")
    assignment = Assignment.by_id(int(assignment_id))
    submission = Submission.save_correct(g.user.id, int(assignment_id),
                                         int(course_id))

    lis_result_sourcedid = request.form.get('lis_result_sourcedid',
                                            submission.url) or None
    code, elements = submission.load_explanation(4)
    if lis_result_sourcedid is None or lis_result_sourcedid == "NOT IN SESSION":
        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)
Example #5
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)
Example #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)
Example #7
0
def save_explain(lti=lti):
    assignment_id = request.form.get('assignment_id', None)
    course_id = request.values.get('course_id', g.course.id if 'course' in g else None)
    if None in (assignment_id, course_id):
        return jsonify(success=False, message="No Assignment ID or Course ID given!")
    assignment_version = int(request.form.get('version', -1))
    answer = request.form.get('answer', '')
    name = request.form.get('name', '')
    Submission.save_explanation_answer(g.user.id, int(assignment_id), int(course_id), name, answer)
    return jsonify(success=True)
Example #8
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)
Example #9
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)
Example #10
0
def save_explain(lti=lti):
    assignment_id = request.form.get('assignment_id', None)
    course_id = request.values.get('course_id',
                                   g.course.id if 'course' in g else None)
    if None in (assignment_id, course_id):
        return jsonify(success=False,
                       message="No Assignment ID or Course ID given!")
    assignment_version = int(request.form.get('version', -1))
    answer = request.form.get('answer', '')
    name = request.form.get('name', '')
    Submission.save_explanation_answer(g.user.id, int(assignment_id),
                                       int(course_id), name, answer)
    return jsonify(success=True)
Example #11
0
def submit_answers():
    assignment_id = request.form.get('assignment_id', None)
    course_id = request.values.get('course_id',
                                   g.course.id if 'course' in g else None)
    if None in (assignment_id, course_id):
        return jsonify(success=False,
                       message="No Assignment ID or Course ID given!")
    assignment_version = int(request.form.get('version', -1))
    answer = request.form.get('submissions', '')
    Submission.save_code(g.user.id, int(assignment_id), int(course_id), answer,
                         assignment_version)

    answer = float(request.form.get('score', '0'))

    return jsonify(success=True)
Example #12
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)
Example #13
0
def browse_submissions():
    assignment_id = request.values.get('assignment_id', None)
    if assignment_id is None:
        return failure("No Assignment ID given!")
    assignment_id = int(assignment_id)
    course_id = request.values.get('course_id',
                                   g.course.id if 'course' in g else None)
    if course_id == None or course_id == "":
        return failure("No Course ID given!")
    if g.user is None or not g.user.is_instructor(int(course_id)):
        return failure(
            "You are not an instructor in this assignments' course.")
    submissions = Submission.by_assignment(assignment_id, int(course_id))
    formatter = HtmlFormatter(linenos=True, noclasses=True)
    python_lexer = PythonLexer()
    for submission, user, assignment in submissions:
        submission.highlighted_code = highlight(submission.code, python_lexer,
                                                formatter)
        submission.history = process_history(
            [h['time'] for h in submission.get_history()])
    return render_template('blockpy/browse_submissions.html',
                           course_id=course_id,
                           assignment_id=assignment_id,
                           submissions=submissions,
                           ip=request.remote_addr)
Example #14
0
def save_code(lti=lti):
    assignment_id = request.values.get('assignment_id', None)
    assignment_version = int(request.values.get('version', -1))
    course_id = request.values.get('course_id',
                                   g.course.id if 'course' in g else None)
    if None in (assignment_id, course_id) or course_id == "":
        return failure("No Assignment ID or Course ID given!")
    code = request.values.get('code', '')
    timestamp = request.values.get('timestamp', '')
    filename = request.values.get('filename', '__main__')
    is_version_correct = True
    if filename == "__main__":
        submission, is_version_correct = Submission.save_code(
            g.user.id,
            assignment_id,
            int(course_id),
            code,
            assignment_version,
            timestamp=timestamp)
    elif g.user.is_instructor(int(course_id)):
        if filename == "give_feedback":
            Assignment.edit(assignment_id=assignment_id, give_feedback=code)
        elif filename == "on_change":
            Assignment.edit(assignment_id=assignment_id, on_step=code)
        elif filename == "starting_code":
            Assignment.edit(assignment_id=assignment_id, starting_code=code)
        log = Log.new('instructor',
                      filename,
                      assignment_id,
                      g.user.id,
                      body=code,
                      timestamp=timestamp)
    return jsonify(success=True,
                   is_version_correct=is_version_correct,
                   ip=request.remote_addr)
Example #15
0
def upload(lti=lti):
    assignment_id = request.form.get('assignment_id', None)
    course_id = request.values.get('course_id',
                                   g.course.id if 'course' in g else None)
    if None in (assignment_id, course_id):
        return jsonify(success=False,
                       message="No Assignment ID or Course ID given!")
    max_questions = int(request.values.get('max_questions', '4'))
    submission = Submission.load(g.user.id, int(assignment_id), int(course_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().strip()
    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)
Example #16
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)
Example #17
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)
Example #18
0
def get_history(lti=lti):
    assignment_id = request.values.get('assignment_id', None)
    assignment_version = int(request.values.get('version', -1))
    course_id = request.values.get('course_id',  g.course.id if 'course' in g else None)
    if None in (assignment_id, course_id) or course_id == "":
        return jsonify(success=False, message="No Assignment ID or Course ID given!")
    submission = Submission.load(g.user.id, assignment_id, int(course_id))
    return jsonify(success=True, data=submission.get_history())
Example #19
0
def save_correct(lti, lti_exception=None):
    assignment_id = request.values.get('assignment_id', None)
    assignment_group_id = request.values.get('group_id', None)
    status = float(request.values.get('status', "0.0"))
    image = request.values.get('image', "")
    hide_correctness = request.values.get('hide_correctness',
                                          "false") == "true"
    force_update = request.values.get('force_update', "false") == "true"
    course_id = request.values.get('course_id',
                                   g.course.id if 'course' in g else None)
    if course_id == -1 or lti is None:
        return failure("No Assignment ID or Course ID given!")
    if None in (assignment_id, course_id):
        return failure("No Assignment ID or Course ID given!")
    assignment = Assignment.by_id(assignment_id)
    if status >= 1:
        submission = Submission.save_correct(g.user.id,
                                             assignment_id,
                                             course_id=int(course_id))
    else:
        submission = assignment.get_submission(g.user.id, course_id=course_id)
    was_changed = submission.set_status(int(100 * status))
    if not force_update and not was_changed:
        return jsonify(success=True,
                       submitted=False,
                       message="No grade change",
                       ip=request.remote_addr)
    lis_result_sourcedid = request.values.get('lis_result_sourcedid',
                                              submission.url) or None
    if lis_result_sourcedid is None:
        return jsonify(success=True,
                       submitted=False,
                       message="Not in a grading context.",
                       ip=request.remote_addr)
    session['lis_result_sourcedid'] = lis_result_sourcedid
    image_url = submission.save_block_image(image)

    if assignment_group_id != None and assignment_group_id != '' and assignment_group_id != 'None':
        score = get_group_score(int(assignment_group_id), g.user.id,
                                int(course_id), hide_correctness)
        url = url_for('blockpy.view_current_group',
                      group_id=int(assignment_group_id),
                      course_id=int(course_id),
                      user_id=g.user.id,
                      _external=True)
        report = "<a href='{url}'>View Group</a>".format(url=url)
    else:
        score = float(submission.correct) or status
        url = url_for('blockpy.view_current_assignment',
                      assignment_id=int(assignment_id),
                      course_id=int(course_id),
                      user_id=g.user.id,
                      _external=True)
        report = "<a href='{url}'>View Assignment</a>".format(url=url)
    lti.post_grade(score, report, endpoint=lis_result_sourcedid)
    return jsonify(success=True, submitted=True, ip=request.remote_addr)
Example #20
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)
Example #21
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)
Example #22
0
def save_correct(lti, lti_exception=None):
    assignment_id = request.form.get('assignment_id', None)
    status = float(request.form.get('status', "0.0"))
    image = request.form.get('image', "")
    course_id = request.form.get('course_id', g.course.id)
    if None in (assignment_id, course_id):
        return jsonify(success=False, message="No Assignment ID or Course ID given!")
    assignment = Assignment.by_id(assignment_id)
    if status == 1:
        submission = Submission.save_correct(g.user.id, assignment_id, course_id=int(course_id))
    else:
        submission = assignment.get_submission(g.user.id, course_id=course_id)
    if submission.correct:
        message = "Success!"
    else:
        message = "Incomplete"
    sub_blocks_folder = os.path.join(app.config['UPLOADS_DIR'], 'submission_blocks')
    image_path = os.path.join(sub_blocks_folder, str(submission.id)+'.png')
    if image != "":
        converted_image = image[22:].decode('base64')
        with open(image_path, 'wb') as image_file:
            image_file.write(converted_image);
        image_url = url_for('blockpy.get_submission_image', submission_id=submission.id, _external=True)
    elif os.path.exists(image_path):
        try:
            os.remove(image_path)
        except Exception:
            app.logger.info("Could not delete")
    lis_result_sourcedid = request.form.get('lis_result_sourcedid', submission.url) or None
    url = url_for('blockpy.get_submission_code', submission_id=submission.id, _external=True)
    print url
    if lis_result_sourcedid is None:
        return jsonify(success=False, message="Not in a grading context.")
    else:
        session['lis_result_sourcedid'] = lis_result_sourcedid
    if assignment.mode == 'maze':
        lti.post_grade(float(submission.correct), "<h1>{0}</h1>".format(message), endpoint=lis_result_sourcedid);
    else:
        code = highlight(submission.code, PythonLexer(), HtmlFormatter())
        potential_image = "Submitted Blocks:<br><img src='{0}'>".format(image_url) if image else ""
        body = '''
        <h1>{message}</h1>
        <div>Latest work in progress: <a href='{url}' target='_blank'>View</a></div>
        <div>Touches: {touches}</div>
        {potential_image}
        <br>
        Submitted code:<br>
        {code}
        '''.format(message=message, url=url, touches=submission.version, code=code, potential_image=potential_image)
        lti.post_grade(float(submission.correct), body, endpoint=lis_result_sourcedid)
    return jsonify(success=True)
Example #23
0
def save_correct(lti, lti_exception=None):
    assignment_id = request.values.get('assignment_id', None)
    status = float(request.values.get('status', "0.0"))
    image = request.values.get('image', "")
    course_id = request.values.get('course_id', g.course.id if 'course' in g else None)
    if None in (assignment_id, course_id):
        return jsonify(success=False, message="No Assignment ID or Course ID given!")
    assignment = Assignment.by_id(assignment_id)
    if status == 1:
        submission = Submission.save_correct(g.user.id, assignment_id, course_id=int(course_id))
    else:
        submission = assignment.get_submission(g.user.id, course_id=course_id)
    if submission.correct:
        message = "Success!"
    else:
        message = "Incomplete"
    sub_blocks_folder = os.path.join(app.config['UPLOADS_DIR'], 'submission_blocks')
    image_path = os.path.join(sub_blocks_folder, str(submission.id)+'.png')
    if image != "":
        converted_image = image[22:].decode('base64')
        with open(image_path, 'wb') as image_file:
            image_file.write(converted_image);
        image_url = url_for('blockpy.get_submission_image', submission_id=submission.id, _external=True)
    elif os.path.exists(image_path):
        try:
            os.remove(image_path)
        except Exception:
            app.logger.info("Could not delete")
    lis_result_sourcedid = request.values.get('lis_result_sourcedid', submission.url) or None
    url = url_for('blockpy.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.")
    else:
        session['lis_result_sourcedid'] = lis_result_sourcedid
    if assignment.mode == 'maze':
        lti.post_grade(float(submission.correct), "<h1>{0}</h1>".format(message), endpoint=lis_result_sourcedid);
    else:
        formatter = HtmlFormatter(linenos=True, noclasses=True)
        code = highlight(submission.code, PythonLexer(), formatter)
        potential_image = "Submitted Blocks:<br><img src='{0}'>".format(image_url) if image else ""
        body = '''
        <h1>{message}</h1>
        <div>Latest work in progress: <a href='{url}' target='_blank'>View</a></div>
        <div>Touches: {touches}</div>
        {potential_image}
        <br>
        Submitted code:<br>
        {code}
        '''.format(message=message, url=url, touches=submission.version, code=code, potential_image=potential_image)
        lti.post_grade(float(submission.correct), body, endpoint=lis_result_sourcedid)
    return jsonify(success=True)
Example #24
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!"
Example #25
0
def submissions_specific(submission_id):
    ''' List all the users in the course '''
    submission, user, assignment = Submission.full_by_id(int(submission_id))
    if submission is None:
        return "Submission not found"
    course_id = submission.course_id
    if course_id is not None:
        is_instructor = g.user.is_instructor(int(course_id))
    else:
        is_instructor = False
    is_owner = g.user.id == submission.user_id
    if not is_instructor and not is_owner:
        return "You are not an instructor or the owner of the assignment!"
    return render_template('courses/submissions_specific.html',
                           submission=submission,
                           assignment=assignment,
                           user=user,
                           course_id=course_id)
Example #26
0
def save_code(lti=lti):
    assignment_id = request.form.get('assignment_id', None)
    assignment_version = int(request.form.get('version', -1))
    course_id = request.form.get('course_id', g.course.id)
    if None in (assignment_id, course_id):
        return jsonify(success=False, message="No Assignment ID or Course ID given!")
    code = request.form.get('code', '')
    filename = request.form.get('filename', '__main__')
    is_version_correct = True
    if filename == "__main__":
        submission, is_version_correct = Submission.save_code(g.user.id, assignment_id, int(course_id), code, assignment_version)
    elif g.user.is_instructor(int(course_id)):
        if filename == "give_feedback":
            Assignment.edit(assignment_id=assignment_id, give_feedback=code)
        elif filename == "on_change":
            Assignment.edit(assignment_id=assignment_id, on_step=code)
        elif filename == "starting_code":
            Assignment.edit(assignment_id=assignment_id, starting_code=code)
    return jsonify(success=True, is_version_correct=is_version_correct)
Example #27
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!"
Example #28
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)
    
Example #29
0
def upload(lti=lti):
    assignment_id = request.form.get('assignment_id', None)
    course_id = request.values.get('course_id', g.course.id if 'course' in g else None)
    if None in (assignment_id, course_id):
        return jsonify(success=False, message="No Assignment ID or Course ID given!")
    max_questions = int(request.values.get('max_questions', '4'))
    submission = Submission.load(g.user.id, int(assignment_id), int(course_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().strip()
    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)
Example #30
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)
Example #31
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)
Example #32
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)