Hi @nate_mcclennen ,
Getting the teacher name for a course can be tricky depending on your use of Canvas. The teacher role is not 1:1, so getting a single name for a teacher requires your own logic.
- If every course simply has 1 teacher, then you can probably run a something like,
SELECT DISTINCT course_id, user_id FROM enrollment_dim WHERE type = 'TeacherEnrollment'
and JOIN it down to user_dim. Then use that as a CTE or view to attach the teacher to the course. - If a course can have more than 1 teacher, you may have to do that, and do a window function and order by created_at, assuming that the first teacher enrolled is the one you want.
- My preferred method. I have a separate view called course_sis_teacher_vw. This view comes from our SIS data that I use to populate Canvas. It contains the course sis id and the teachers sis id, and joins in the canvas course id and teacher canvas id, and their names. Since there is only 1 teacher of record in our SIS, I use this when I need that specific teacher from Canvas.
Solving that, you can query assignments, with course, and teacher name. This example uses option 2.
WITH teachers AS (
SELECT
course_id
, course_section_id
, user_id
, canvas_user_id
, teacher_name
FROM
(SELECT
enrollment_dim.course_id
, enrollment_dim.course_section_id
, enrollment_dim.user_id
, user_dim.canvas_id AS canvas_user_id
, user_dim.sortable_name AS teacher_name
, ROW_NUMBER() OVER (PARTITION BY type, user_id ORDER BY enrollment_dim.created_at ASC) first_teacher
FROM enrollment_dim
JOIN user_dim ON enrollment_dim.user_id = user_dim.id
WHERE type = 'TeacherEnrollment'
) x
WHERE first_teacher = 1
)
SELECT
course_dim.id
, course_dim.canvas_id AS canvas_course_id
, course_dim.name AS course_name
, assignment_dim.title AS assignment_title
, teacher_name
FROM assignment_dim
JOIN course_dim ON assignment_dim.course_id = course_dim.id
JOIN teachers ON teachers.course_id = course_dim.id
Now, the want the URL of the assignment, can be tricky. It depends on the assignment and submission type, then joining onto those tables, and quite possibly having to format your own strings to replicate the URL from routes and ids.
Are you also looking for the submissions of these assignments? In that case, you'd basically take that query and SELECT FROM submission_dim, JOIN ON assignment_dim and the rest, with a second JOIN on user_dim AS students, and joining submission_dim.user_id = user_dim.id to get the student name.
SELECT
course_dim.id
, course_dim.canvas_id AS canvas_course_id
, course_dim.name AS course_name
, assignment_dim.title AS assignment_title
, teacher_name
, students.sortable_name
FROM submission_dim
JOIN assignment_dim ON assignment_dim.id = submission_dim.assignment_id
JOIN course_dim ON course_dim.id = assignment_dim.course_id
JOIN teachers ON teachers.course_id = course_dim.id
JOIN user_dim AS students ON students.id = submission_dim.user_id