异常 - 在NULL上调用成员函数get_record()

I'm trying to use the moodle Data Manipulation API to get the grades from students to analyse. But when I use the function get_record() inside of another function that I've created I get the message of the tittle. I don't know why the fuction works in the main and don't work inside a function.Any idea? I'm new in php and moodle manipulation, so take easy on me.

<?php
function get_all_quiz ($courseid) {

    $quizesid = [];
    $quiz = $DB->get_record('moodle.quiz', array('id'=>$courseid));
    $quizesid = $quiz.id;

    return $quizesid;
}

global $DB;
define('CLI_SCRIPT', true);
require '../../var/www/moodle/config.php';

$coursetest = 3;
$studentgrades = [];

$quizes = get_all_quiz($coursetest);
?>

There are a few things you need to fix in this function:

  • You need to add global $DB; inside the function
  • The quiz database table is called 'quiz', not 'moodle.quiz' (Moodle config handles connecting to the correct database)
  • PHP uses '->' operator to access object properties, not '.' (which is used for concatenation), so it is $quiz->id not, $quiz.id
  • If you are wanting to return all quizzes, you should call $DB->get_records(), not $DB->get_record() (which returns just 1 record and outputs debugging warnings if more than one is found).
  • If you want the quizzes for a particular course, then you should match the 'course' field in the quiz record, not the 'id' field (which is the ID of the quiz, not the course).

So the function should probably look like this:

function get_all_quiz($courseid) {
    global $DB;
    return $DB->get_records('quiz', array('course' => $courseid));
}