def grade(lti=lti): """ post grade :param lti: the `lti` object from `pylti` :return: grade rendered by grade.html template """ form = AddForm() correct1 = ((form.p11.data + form.p12.data) == form.result1.data) correct2 = ((form.p21.data + form.p22.data) == form.result2.data) correct3 = ((form.p31.data + form.p32.data) == form.result3.data) correct4 = ((form.p41.data + form.p42.data) == form.result4.data) correct5 = ((form.p51.data + form.p52.data) == form.result5.data) form.correct1.data = correct1 form.correct2.data = correct2 form.correct3.data = correct3 form.correct4.data = correct4 form.correct5.data = correct5 score = 0 score += 2 if correct1 else 0 score += 2 if correct2 else 0 score += 2 if correct3 else 0 score += 2 if correct4 else 0 score += 2 if correct5 else 0 lti.post_grade(score / 10.) return render_template('grade.html', form=form, score=score)
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 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)
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 grade(lti=lti): """ post grade :param lti: the `lti` object from `pylti` :return: grade rendered by grade.html template """ form = AddForm() correct = ((form.p1.data + form.p2.data) == form.result.data) form.correct.data = correct lti.post_grade(1 if correct else 0) return render_template('grade.html', form=form)
def submit( grade: float, accuracy: float, user_token: str, assignment_id: str, lti: pylti.flask.LTI, ) -> RequestResultType: """ post grade :return: grade rendered by grade.html template """ with Tracer.main().start_span("grading_submission") as span: span.set_tag("grade_percent", grade) span.log_kv( dict( grade=grade, accuracy=accuracy, user_token=user_token, user_id=lti.user_id, assignment_id=assignment_id, )) from pylti.common import LTI_PROPERTY_LIST, LTI_SESSION_KEY ltisession = { prop: lti.session.get(prop, None) for prop in LTI_PROPERTY_LIST + [LTI_SESSION_KEY] } log.debug(f"Session: {ltisession}") try: log.info( f"Submitting grade {grade} for user {lti.user_id} assignment {assignment_id} to {lti.response_url}" ) lti.post_grade(grade) except Exception as e: log.error(e) raise PostingGradeFailedException( "Failed to submit the grade to the LTI Consumer", user_id=lti.user_id, assignment_id=assignment_id, ) return ( jsonify( dict( grade=grade, accuracy=accuracy, message= f"Successfully graded for {assignment_id}: Received {grade*100}% (acc={accuracy})", )), 200, MIME.json, )
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!"
def post_grade(grade, lti): """ access route with 'session' request :param lti: `lti` object :return: string "grade={}" """ ret = lti.post_grade(grade) return "grade={}".format(ret)
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!"
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)
def RepresentBalances(lti=lti, q=None): user = db.session.query(User).filter_by(lti_user_id=lti.name).first() if q is None: for i in range(len(BalanceQuestionData)): statement = select([question_scores, Question.__table__]).where( and_(question_scores.c.user_id == user.id, question_scores.c.question_id == Question.__table__.c.id, Question.__table__.c.number == i + 1, question_scores.c.score == 1)) results = db.session.execute(statement).first() if not results: q = i + 1 break if not user: form = UserInfoForm() return render_template('GetUserInfo.html', lti=lti, form=form) if q == 'submit': lti.post_grade(1) return render_template('grade.html', form=form) q = int(q) @after_this_request def add_header(response): response.headers['Content-Type'] = 'text/html; charset=utf-8' return response from sympy import simplify, symbols from sympy.parsing.sympy_parser import parse_expr from sympy.parsing.sympy_parser import standard_transformations, implicit_multiplication_application transformations = (standard_transformations + (implicit_multiplication_application, )) assignment = 'RepresentBalances' a, b = symbols("a b") # markdown_include = MarkdownInclude( # configs={'base_path':app.config['MARKDOWN_INCLUDE_PATH']} # ) # md = markdown.Markdown(extensions=['mdx_math','attr_list','markdown.extensions.extra','markdown.extensions.meta',markdown_include]) # with open(os.path.join(app.config['RESOURCES_DIR'],'RepresentBalances', 'Question{:d}.md'.format(q)), 'rb') as f: # source = f.read() # result = md.convert(source.decode('utf-8')) # try: # title = md.Meta['title'][0] # except: # title = 'untitled' form = EquationForm() n = len(BalanceQuestionData[q - 1]['Variables']) for i in range(n): form.variables.append_entry() # Check answers # Answers array try: lhs_input = parse_expr(form.lhs.data, transformations=transformations) rhs_input = parse_expr(form.rhs.data, transformations=transformations) lhs = BalanceQuestionData[q - 1]['LHS'] rhs = BalanceQuestionData[q - 1]['RHS'] for i, variable in enumerate(BalanceQuestionData[q - 1]['Variables']): lhs = lhs.replace(variable, form.variables[i].data) rhs = rhs.replace(variable, form.variables[i].data) lhs = parse_expr(lhs, transformations=transformations) rhs = parse_expr(rhs, transformations=transformations) correct = simplify(lhs - lhs_input) == 0 and simplify(rhs - rhs_input) == 0 except: lhs = form.lhs.data rhs = form.rhs.data correct = False if request.method == 'POST': question = get_or_create(db.session, Question, assignment=assignment, number=q) db.session.commit() statement = question_scores.insert().values(user_id=user.id, question_id=question.id, score=bool(correct)) db.session.execute(statement) db.session.commit() if len(BalanceQuestionData) > q + 1: NextQuestion = q + 1 else: NextQuestion = None return dict(title='Representing balance scales', content='', form=form, q=q, NextQuestion=NextQuestion, lhs=lhs, rhs=rhs, correct=correct, QuestionData=BalanceQuestionData[q - 1])
def grade(lti=lti): form = AddFrom() correct = ((form.p1.data + form.p2.data) == form.result.data) form.correct.data = correct lti.post_grade(1 if correct else 0) return render_template('grade.html', form=form)