use grade_get_course_grade();

use grade_get_course_grade();

by tufit tufit -
Number of replies: 1

How to use grade_get_course_grade(); function? I use this function: 

$resultkrb = grade_get_course_grades(79, null); 

$grd = $resultkrb->grades[2]; 

print_r ($resultkrb);


but I see:


stdClass Object ( [scaleid] => 0 [name] => W całym kursie [grademin] => 0.00000 [grademax] => 100.00000 [gradepass] => 0.00000 [locked] => [hidden] => [grades] => Array egg )




grade_get_course_grade( $userid,
 $courseid_or_ids = null 
)

Returns the aggregated or calculated course grade for a single user for one or more courses

Parameters
int$useridThe ID of the single user
int | array$courseid_or_idsOptional ID of course or array of IDs, empty means all of the user's courses
Returns
mixed grade info or grades array including item info, false if error

Definition at line 136 of file querylib.php.

Average of ratings: Useful (1)
In reply to tufit tufit

Re: use grade_get_course_grade();

by Mike Finch -

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.