def get_course_assignments(course_id):

    canvas = Auth.init_canvas(session["oauth_token"])

    data = Assignments.get_course_assignments(canvas, course_id)

    return jsonify({"success": data})
def section():
    """ Show single section. """
    data = request.json

    canvas = Auth.init_canvas(session["oauth_token"])

    # Look only in the current course
    assignments = Assignment.query.filter_by(course_id=data["course_id"]).all()

    if not assignments:
        assignments = []
        scores = []
    else:
        try:
            scores = Assignments.get_all_assignment_scores(
                canvas, data["course_id"], section_id=data["section_id"]
            )
        except Exception as e:
            return (
                jsonify(message=f"{e}"),
                500,
            )

    # Sort the scores array by student last name before returning
    if scores is not None:
        scores = sorted(scores, key=lambda x: x["user_name"].split(" "))
    else:
        return (
            jsonify(message="Please import assignments in the 'Alignments' tab."),
            500,
        )

    return jsonify(scores)
def sync_outcome_scores():
    """ Get the Outcomes for the students. """
    # Instantiate a Canvas object
    canvas = Auth.init_canvas(session["oauth_token"])

    json = request.json

    # Send the outcome_list in prep for querying a smaller set
    data = Outcomes.update_student_scores(
        canvas, json["course_id"], json["student_id_list"], json["outcome_id_list"]
    )

    return jsonify({"success": data})
def save_outcomes():
    """ Save Outcomes from the course into the database
    :methods: POST

    :rtype:
    """
    # Get the data from the form submission
    data = request.values

    # Instantiate a new Canvas object
    canvas = Auth.init_canvas(session["oauth_token"])

    # Store the course Outcomes
    Outcomes.save_outcome_data(canvas, data["id"])
    Assignments.save_assignment_data(canvas, data["id"], data["assignment_groups"])

    # Reload the page
    return redirect(url_for("course", course_id=data["id"]))
def course(course_id):
    """ Single course view
    :param course_id: Canvas course ID
    :type course_id: Int

    :methods: GET

    :rtype:
    """
    # Instantiate a new Canvas object
    canvas = Auth.init_canvas(session["oauth_token"])

    # Get the assignment groups from Canvas
    course = canvas.get_course(course_id)
    sections = course.get_sections()

    query = course.get_assignment_groups()
    assignment_groups = [(str(a.id), a.name) for a in query]

    # Populate assignment_group_ids into the Outcomes fetch form dynamically
    form = StoreOutcomesForm(request.values, id=course_id)
    form.assignment_groups.choices = assignment_groups

    # Look only in the current course
    assignments = Assignment.query.filter_by(course_id=course_id)

    if not assignments:
        assignments = []

    # Look up any existing Outcomes by course ID
    outcomes = Outcome.query.filter(Outcome.course_id == course_id)

    if not outcomes:
        outcomes = None

    return render_template(
        "course.html",
        title="Canvas course",
        outcomes=outcomes,
        assignments=assignments,
        sections=sections,
        form=form,
    )
def dashboard():
    """ Display the logged-in user's courses. """
    canvas = Auth.init_canvas(session["oauth_token"])
    user = canvas.get_current_user()

    all_courses = user.get_courses(
        state=["available"],
        enrollment_state=["active"],
        enrollment_type="teacher",
        include="total_students",
    )

    courses = []
    for c in all_courses:
        courses.append(Course.process_course(c))

    courses = sorted(courses, key=lambda course: course["term"], reverse=True)

    return render_template("dashboard.html", title="Dashboard", courses=courses), 200
def get_assignment_rubric(course_id, assignment_id):
    canvas = Auth.init_canvas(session["oauth_token"])

    data = Assignments.build_assignment_rubric_results(canvas, course_id, assignment_id)

    return jsonify({"success": data})