php - What is Course Module instance / `$cm->instance`?
Get the solution ↓↓↓What does the$cm->instance
refer to?
For example I know that$cm->course
means the course id.
Thank you all.
$cm = get_coursemodule_from_id('simplecertificate', $id);
if (!$cm) {
print_error('Course Module ID was incorrect');
}
$course = $DB->get_record('course', array('id' => $cm->course));
if (!$course) {
print_error('course is misconfigured');
}
$certificate = $DB->get_record('simplecertificate', array('id' => $cm->instance));
if (!$certificate) {
print_error('course module is incorrect');
}
Answer
Solution:
The code you have pasted in already shows exactly what $cm->instance is - it is the id of the record in the 'mdl_simplecertificate' database table that holds the details for this activity.
In general, $cm->instance is the id of the database table that stores the details for the type of activity the $cm refers to.
To find the type of activity in question, you need to get the value of $cm->module and look for an entry with that 'id' in the database table 'mdl_modules', the 'name' will give you the name of the activity type and, hence, the name of the database table that contains the instance data for that activity type.
So, if $cm->module points at the 'forum' entry in mdl_modules, then $cm->instance refers to the record with that id in table mdl_forum.
If $cm->module points at the 'assign' entry in mdl_modules, then $cm->instance refers to the record with that id in the table mdl_assign.
In this case, $cm->module will point at the 'simplecertificate' entry in mdl_modules, so $cm->instance refers to the record with that id in table mdl_simplecertificate.
Some more details are in the Moodle docs at: https://docs.moodle.org/dev/Course_module
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: mysqli::real_connect(): (hy000/2002): connection refused
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.