I am using Moodle version 3.0.2, and I can use grade_get_course_grade() successfully.
I have a user with record ID of 6, and that user is definitely enrolled in several courses. I execute the following function in my page.
function perform_test()
{
global $DB;
$userId = 6;
$courseId = null;
$result = grade_get_course_grade( $userId, $courseId );
if ( is_array( $result ) )
{
foreach ( $result as $id => $rec )
{
$course = $DB->get_record( 'course', array( 'id' => $id ) );
print html_writer::tag( 'p', sprintf( "%d: course='%s' grade=%f", $id, $course->fullname, $rec->grade ) );
}
}
else
{
print_object( $result );
}
}
The result returned by grade_get_course_grade() is an array of stdClass objects, where each object has properties such as the following example.
stdClass Object
(
[grade] => 80.00000
[locked] =>
[hidden] =>
[overridden] => 0
[feedback] =>
[feedbackformat] => 0
[usermodified] =>
[dategraded] => 1468864667
[item] => stdClass Object
(
[scaleid] => 0
[name] => Course total
[grademin] => .00000
[grademax] => 100.00000
[gradepass] => .00000
[locked] =>
[hidden] =>
)
[str_grade] => 80.00
[str_long_grade] => 80.00 / 100.00
[str_feedback] =>
)
Thus, I can successfully print the grade of each course the user is enrolled in. I hope this example helps.
Comparing my output to yours, it seems that your are getting just the sub-object associated with keyword 'item'. I cannot explain that given your code snippet.
I found this forum post while looking for a way to get the total course grade programmatically. It was surprisingly difficult to find documentation for that. I am lucky I came across this post; it saved me from reverse engineering the grader report.