Exception - Call to undefined function grade_get_grades()

Exception - Call to undefined function grade_get_grades()

by Lina Ahmed Kamal -
Number of replies: 2

Hi,

I'm trying to develop a plugin and I need to get the student's grades for the activities.

I implemented the following code and it works as expected, but the problem happens when I logout then login again and try accessing the course containing the block plugin. I get this error " Exception - Call to undefined function grade_get_grades()"   

            $gradinginfo = grade_get_grades($courseid, 'mod', $activity['modulename'], $activity['instance'], $userid);
            if (isset($gradinginfo->items[0]->grades[$userid]) &&
                    !$gradinginfo->items[0]->grades[$userid]->hidden ) {
                $grade = $gradinginfo->items[0]->grades[$userid]->str_grade;
                $cellcontent .=HTML_WRITER::tag('div',$grade, array('class'=> 'hi2'));
                echo $grade ;
            } else {
                $grade = '-';
                $cellcontent .=HTML_WRITER::tag('div',$grade, array('class'=> 'hi2'));
                echo $grade ;

            }

This  is my first time developing with moodle and i don't know what I'm missing here, so any help would be so much appretiated.


Average of ratings: -
In reply to Lina Ahmed Kamal

Re: Exception - Call to undefined function grade_get_grades()

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
At a quick guess, you need to add the following to the top of your function:
global $CFG;
require_once($CFG->libdir.'/gradelib.php');
That should be enough to fix the problem (as an aside, and you seem to be getting away with it, but "HTML_WRITER", should be "html_writer", as the class name is lowercase, not uppercase).

Average of ratings: Useful (1)
In reply to Davo Smith

Re: Exception - Call to undefined function grade_get_grades()

by Lina Ahmed Kamal -
Thank you so much for those tips, I will try this.