Sql for grade percentage

Sql for grade percentage

by Leslie Foster -
Number of replies: 6

Moodle 3.2.4+ (Build 20170714) 


I have a custom php report that produces an Excel spreadsheet.  It extracts data from Moodle, but the final grade shows as a raw score rather than a percentage.  What changes to the code (below) do I need to make to show percentage grades?


SQL

 $sql = "SELECT

            ue.timestart AS enrolmentstartdate,

            ue.userid AS user_id,

            user.*,

            ud_site.data AS user_site,

            en.enrol AS enrol_method,

            en.cost AS enrol_cost,

            course.id AS course_id,

            course.fullname AS course_fullname,

            course.shortname AS course_shortname,

            category.name AS course_category,

            tag.name AS course_tag,

            gg.finalgrade AS course_finalgrade 

        FROM {user_enrolments} AS ue

        JOIN {user} AS user ON ue.userid = user.id

        JOIN {user_info_data} AS ud_site ON ud_site.userid = user.id

        JOIN {user_info_field} AS uf_site ON uf_site.id = ud_site.fieldid AND uf_site.shortname = 'site'

        JOIN {enrol} AS en ON ue.enrolid = en.id

        JOIN {course} AS course ON en.courseid = course.id

        JOIN {course_categories} AS category ON course.category = category.id

        LEFT JOIN {tag_instance} AS ti ON course.id = ti.itemid

        LEFT JOIN {tag} AS tag ON ti.tagid = tag.id

        JOIN {grade_items} AS gi ON en.courseid = gi.courseid AND gi.itemtype = 'course'

        LEFT JOIN {grade_grades} AS gg ON gg.itemid = gi.id AND ue.userid = gg.userid

        WHERE user.id != 0 ";


Output to Spreadsheet

$row[] = round($record->course_finalgrade,0); //Final Result

Average of ratings: -
In reply to Leslie Foster

Re: Sql for grade percentage

by Colin Fraser -
Picture of Documentation writers Picture of Testers

Wouldn't it be a mathematical operation in the spreadsheet? 

I take it there is a call to run the SQL then prepare the data for insertion into the spreadsheet. At the point the spreadsheet is created, could that be an auto-created template with the titles for columns and labels created?  Could the destination cell be setup to auto-calculate the data passed to it? I don't really know, I am no VBA expert, but it would seem that at some point you have to create the spreadsheet, so why not anticipate the data that is passed to it? 


In reply to Colin Fraser

Re: Sql for grade percentage

by Leslie Foster -

It is an existing report and I wanted to make a small change.


The client has access to the report and hence whilst I can make changes to the spreadsheet, it would be better if the client could download the report in spreadsheet format without having to make changes.

In reply to Leslie Foster

Re: Sql for grade percentage

by Olumuyiwa Taiwo -
Picture of Plugin developers

The documentation on the grade_grades table might help: https://docs.moodle.org/dev/Grades#grade_grades

In reply to Olumuyiwa Taiwo

Re: Sql for grade percentage

by Rick Jerz -
Picture of Particularly helpful Moodlers Picture of Testers

Olumuyiwa, I took interest in this link that you provided.  I noticed that the various grade tables, and their columns and documentation.  I got to wondering, how does one get to the various (and many) tables documentation, like this one for the grades tables?  I went back one level to docs.moodle.org/dev, but I didn't see how one gets to Grades from this webpage, or documentation about the other tables.

So might you know where I should go to get "table" documentation?

(For example, I would like to know where "contextid" in the grades_letters table is used?  The contextid is not the courseid, so it must link somewhere else.  I am still a novice at all of this sql stuff, but learning.)

In reply to Rick Jerz

Re: Sql for grade percentage

by Olumuyiwa Taiwo -
Picture of Plugin developers

Rick, the database schema documentation is at https://docs.moodle.org/dev/Database_Schema. Follow the links to download the schema for the relevant Moodle version.

I don't think the 'contextid' column in the grade_letters table links to any other table, however it's used in various parts of Moodle -- gradebook, backup, etc.

Average of ratings: Useful (1)