Search code examples
moodle

Get data from php Object that name have backslash


Hello I have Return data from sql that show me this :

tool_courserating\local\models\rating Object 
( [data:core\persistent:private] => Array 
( [id] => 3 [courseid] => 8 [userid] => 2 [rating] => 4 [review] => [hasreview] => 0 [timecreated] => 1702151698 [timemodified] => 1702151698 ) 
[errors:core\persistent:private] => Array ( ) 
[validated:core\persistent:private] => )

The code that come this result is :

$rating = (object) tool_courserating\local\models\rating::get_record(['courseid' => $courseData->id]);
print_r($rating);

I'm not php expert so Idk how to get rating from this object that I have! and if it's array how I can control it with foreach?(Ik it's Stupid Question)

------------------------Update---------------

The class That I Using it's from here :Github SRC


Solution

  • The tool uses Moodle's persistent database class.

    So you just need to use the get function for the rating

    Also, there could be more than one rating for a course, so you should use get_records rather than get_record

     $ratings = \tool_courserating\local\models\rating::get_records(['courseid' => $courseData->id]]);
    
     foreach ($ratings as $rating) {
         echo $rating->get('rating');
     }