Skip to content
This repository was archived by the owner on Jun 30, 2024. It is now read-only.

Commit 3cb36dd

Browse files
committed
Refactor: put _try_to_send_lti_grade in rs_grading.
1 parent 6c1366f commit 3cb36dd

File tree

2 files changed

+59
-40
lines changed

2 files changed

+59
-40
lines changed

controllers/assignments.py

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
# Third-party imports
1919
# -------------------
2020
from psycopg2 import IntegrityError
21-
from rs_grading import do_autograde, do_calculate_totals, do_check_answer, send_lti_grade
22-
from db_dashboard import DashboardDataAnalyzer
2321
import six
2422
import bleach
2523

24+
# Local application imports
25+
# -------------------------
26+
from rs_grading import do_autograde, do_calculate_totals, do_check_answer, send_lti_grade, _get_lti_record, _try_to_send_lti_grade
27+
from db_dashboard import DashboardDataAnalyzer
28+
2629
logger = logging.getLogger(settings.logger)
2730
logger.setLevel(settings.log_level)
2831

@@ -362,39 +365,6 @@ def _autograde(sid=None, student_rownum=None, question_name=None, enforce_deadli
362365
return {'success': False, 'message': "Select an assignment before trying to autograde."}
363366

364367

365-
def _get_assignment(assignment_id):
366-
return db(db.assignments.id == assignment_id).select().first()
367-
368-
def _try_to_send_lti_grade(student_row_num, assignment_id):
369-
# try to send lti grades
370-
assignment = _get_assignment(assignment_id)
371-
if not assignment:
372-
session.flash = "Failed to find assignment object for assignment {}".format(assignment_id)
373-
return False
374-
else:
375-
grade = db(
376-
(db.grades.auth_user == student_row_num) &
377-
(db.grades.assignment == assignment_id)).select().first()
378-
if not grade:
379-
session.flash = "Failed to find grade object for user {} and assignment {}".format(auth.user.id,
380-
assignment_id)
381-
return False
382-
else:
383-
lti_record = _get_lti_record(session.oauth_consumer_key)
384-
if (not lti_record) or (not grade.lis_result_sourcedid) or (not grade.lis_outcome_url):
385-
session.flash = "Failed to send grade back to LMS (Coursera, Canvas, Blackboard...), probably because the student accessed this assignment directly rather than using a link from the LMS, or because there is an error in the assignment link in the LMS. Please report this error."
386-
return False
387-
else:
388-
# really sending
389-
# print("send_lti_grade({}, {}, {}, {}, {}, {}".format(assignment.points, grade.score, lti_record.consumer, lti_record.secret, grade.lis_outcome_url, grade.lis_result_sourcedid))
390-
send_lti_grade(assignment.points,
391-
score=grade.score,
392-
consumer=lti_record.consumer,
393-
secret=lti_record.secret,
394-
outcome_url=grade.lis_outcome_url,
395-
result_sourcedid=grade.lis_result_sourcedid)
396-
return True
397-
398368
@auth.requires_login()
399369
def student_autograde():
400370
"""
@@ -728,9 +698,6 @@ def chooseAssignment():
728698

729699
# The rest of the file is about the the spaced practice:
730700

731-
def _get_lti_record(oauth_consumer_key):
732-
return db(db.lti_keys.consumer == oauth_consumer_key).select().first()
733-
734701
def _get_course_practice_record(course_name):
735702
return db(db.course_practice.course_name == course_name).select().first()
736703

@@ -893,7 +860,7 @@ def practice():
893860
flashcard.sub_chapter_label)
894861
# If the student has any flashcards to practice and has not practiced enough to get their points for today or they
895862
# have intrinsic motivation to practice beyond what they are expected to do.
896-
if (available_flashcards_num > 0 and
863+
if (available_flashcards_num > 0 and
897864
len(questions) > 0 and
898865
(practiced_today_count != questions_to_complete_day or
899866
request.vars.willing_to_continue or

modules/rs_grading.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1+
# Imports
2+
# =======
3+
# These are listed in the order prescribed by `PEP 8
4+
# <http://www.python.org/dev/peps/pep-0008/#imports>`_.
5+
#
6+
# Standard library
7+
# ----------------
18
import datetime
29
import logging
310
from math import ceil
4-
from psycopg2 import IntegrityError
511
from decimal import Decimal, ROUND_HALF_UP
612

13+
# Third-party imports
14+
# -------------------
15+
from psycopg2 import IntegrityError
716
from gluon import current
17+
18+
# Local imports
19+
# -------------
820
from outcome_request import OutcomeRequest
921

1022
logger = logging.getLogger(current.settings.logger)
@@ -461,6 +473,46 @@ def _get_students(course_id=None, sid = None, student_rownum=None, db=None):
461473

462474
return student_rows
463475

476+
477+
def _get_assignment(assignment_id):
478+
return current.db(current.db.assignments.id == assignment_id).select().first()
479+
480+
481+
def _get_lti_record(oauth_consumer_key):
482+
return current.db(current.db.lti_keys.consumer == oauth_consumer_key).select().first()
483+
484+
485+
def _try_to_send_lti_grade(student_row_num, assignment_id):
486+
# try to send lti grades
487+
assignment = _get_assignment(assignment_id)
488+
if not assignment:
489+
current.session.flash = "Failed to find assignment object for assignment {}".format(assignment_id)
490+
return False
491+
else:
492+
grade = current.db(
493+
(current.db.grades.auth_user == student_row_num) &
494+
(current.db.grades.assignment == assignment_id)).select().first()
495+
if not grade:
496+
current.session.flash = "Failed to find grade object for user {} and assignment {}".format(auth.user.id,
497+
assignment_id)
498+
return False
499+
else:
500+
lti_record = _get_lti_record(current.session.oauth_consumer_key)
501+
if (not lti_record) or (not grade.lis_result_sourcedid) or (not grade.lis_outcome_url):
502+
current.session.flash = "Failed to send grade back to LMS (Coursera, Canvas, Blackboard...), probably because the student accessed this assignment directly rather than using a link from the LMS, or because there is an error in the assignment link in the LMS. Please report this error."
503+
return False
504+
else:
505+
# really sending
506+
# print("send_lti_grade({}, {}, {}, {}, {}, {}".format(assignment.points, grade.score, lti_record.consumer, lti_record.secret, grade.lis_outcome_url, grade.lis_result_sourcedid))
507+
send_lti_grade(assignment.points,
508+
score=grade.score,
509+
consumer=lti_record.consumer,
510+
secret=lti_record.secret,
511+
outcome_url=grade.lis_outcome_url,
512+
result_sourcedid=grade.lis_result_sourcedid)
513+
return True
514+
515+
464516
def send_lti_grade(assignment_points, score, consumer, secret, outcome_url, result_sourcedid):
465517

466518
pct = score / float(assignment_points) if score and assignment_points else 0.0

0 commit comments

Comments
 (0)