Get content from course module

Get content from course module

by Tara Meyer -
Number of replies: 3

Hello everyone, 

I want to get the content from my plugin, it's almost the same as the page plugin. 

I tried the following lines, but it didn't work for me.


global $DB;
$content = $DB->get_record('newmodule', ['content'=>$content]);

Does anybody know how I can get the content for further processing?

Thanks in advance

Average of ratings: -
In reply to Tara Meyer

Re: Get content from course module

by Benjamin Ellis -
Hi,

The resultant SQL for your example is something like

SELECT * FROM mdl_newmodule WHERE content = $content;

Is that what you really want to do?  $DB->get_record() expects only to find 1 record, so you would usually get the record by its id unless the other field contains unique values. If you want to get the value of the content field for a particular record, you might use (there are other ways to do this)

$DB->get_field('newmodule', 'content', ['id' => $recordid]);

Regards


In reply to Benjamin Ellis

Re: Get content from course module

by Tara Meyer -
Hi Benjamin,

thank you for your proposal.
I have created an observer and want to catch the content at the course_module_created event and process it further.
For this I need the id of the object that was just created by course_module_created.

I thought of your solution, but this one doesn't work for me:

global $DB;
if (!$newmodule = $DB->get_record('newmodule', array('id'=>$id))) {
return false;
}
$cm = get_coursemodule_from_instance('newmodule', $id);
$contentnew = $DB->get_field('newmodule', 'content', ['id' => $cm->id]);

I want to program it dynamically so that each time the ID and content of the created module is used. 

Thank you very much!
In reply to Tara Meyer

Re: Get content from course module

by Benjamin Ellis -

The observer function will get an 'event' object passed in and for course modules, the course module id is $event->objectid and the module information is in the $event->other array 

'other'    => array(
'modulename' => $cm->modname,
'instanceid' => $cm->instance,
'name'       => $cm->name,
)

so you can use that to get the table record e.g.

$record = $DB->get_record($event->other['modname'], array('id' => $event->other['instanceid']);

Cheers