Letter Grade Report

Letter Grade Report

by Scott Rogers -
Number of replies: 3

Hi, just wondering if anyone can help. I don't have SQL knowledge, however, need to create a report for all courses that groups by course and lost all enrolled student LETTER grades. The only reports I have found to date show numerical grades. 

Can anyone helo with the SQL code for this?

Cheers Scott

Average of ratings: Useful (1)
In reply to Scott Rogers

Re: Letter Grade Report

by Ben Haensel -
The query below will return a result for a specific course and user. I would replace that with a department filter or some other kind of filter so you're not getting all grades for all active students. I hope this helps! -Ben

---Letter Grade query for a specific course/user---

select
c.id courseid,
c.fullname course,
u.id userid,
concat(u.lastname, ', ', u.firstname) user,

#percent rounded to 2 decimal places
round(g.finalgrade,2) percent,

#letter grade, uses any custom letters for the course
(SELECT l.letter FROM mdl_context x
JOIN mdl_grade_letters l ON l.contextid = x.id
WHERE x.contextlevel = 50
and x.instanceid in (c.id, 0) and l.lowerboundary <= g.finalgrade
ORDER BY x.id desc, lowerboundary desc limit 1) grade,

#this is the grade feedback from the gradebook, you could see this in SingleView
g.feedback comments

from mdl_user AS u
join mdl_grade_grades g on g.userid = u.id
join mdl_grade_items gi on gi.id = g.itemid
JOIN mdl_course c on c.id = gi.courseid

#Join relationship for student role in the course
JOIN mdl_enrol AS en ON en.courseid = c.id
JOIN mdl_user_enrolments AS ue ON ue.enrolid = en.id and ue.userid = u.id
JOIN mdl_context AS ctx ON c.id = ctx.instanceid
JOIN mdl_role_assignments AS ra ON ra.contextid = ctx.id and ra.userid = u.id
JOIN mdl_role r on ra.roleid = r.id

where gi.itemtype = 'course'

#filter required for student role
AND r.shortname = 'student'

#specific userid
and u.id = 86

#specific courseid
and gi.courseid = 815
Average of ratings: Useful (2)
In reply to Ben Haensel

Odp: Re: Letter Grade Report

by My Awesome Moodle -

Hi Ben,

I am trying to adapt your code to my report. Your SQL works great except when default grade letters are overridden for course. In this situation, the maximum grade letter is printed out for all grades. It would be great if you had the time and willingness to extend your code to support this situation. Thanks!

In reply to My Awesome Moodle

Re: Odp: Re: Letter Grade Report

by Ben Haensel -
This version has a column for what the grade letter would be using the site default grade scale. You can tell the site default is used when the overridden column is null. -Ben
------
select
c.id courseid,
c.fullname course,
u.id userid,
concat(u.lastname, ', ', u.firstname) user,

#percent rounded to 2 decimal places
round(g.finalgrade,2) percent,

#letter grade, uses any custom letters for the course
(SELECT l.letter FROM mdl_grade_letters l
join mdl_context x on l.contextid = x.id
WHERE x.contextlevel = 50
and x.instanceid in (c.id, 0) and l.lowerboundary <= g.finalgrade
ORDER BY x.id desc, lowerboundary desc limit 1) overridden_grade_scale,

(SELECT l.letter FROM mdl_grade_letters l
WHERE l.contextid = 1
and l.lowerboundary <= g.finalgrade
ORDER BY l.lowerboundary desc limit 1) default_grade_scale,

#this is the grade feedback from the gradebook, you could see this in SingleView
g.feedback comments

from mdl_user AS u
join mdl_grade_grades g on g.userid = u.id
join mdl_grade_items gi on gi.id = g.itemid
JOIN mdl_course c on c.id = gi.courseid

#Join relationship for student role in the course
JOIN mdl_enrol AS en ON en.courseid = c.id
JOIN mdl_user_enrolments AS ue ON ue.enrolid = en.id and ue.userid = u.id
JOIN mdl_context AS ctx ON c.id = ctx.instanceid
JOIN mdl_role_assignments AS ra ON ra.contextid = ctx.id and ra.userid = u.id
JOIN mdl_role r on ra.roleid = r.id

where gi.itemtype = 'course'

#filter required for student role
AND r.shortname = 'student'

#specific userid
and u.id = 86

#specific courseid
and gi.courseid = 815
Average of ratings: Useful (2)