Creating a label programatically

Creating a label programatically

by Hector Pasarin -
Number of replies: 2

Someone knows how to create a Label by code? , that is, not using the user interface.

I found an application (MOOSH) that works correctly,  and you can use it to create activities and labels,

But...there is some way to do it without using MOOSH? I only want to create a simple label, only text in a specific course and section.

Thank you all

Average of ratings: -
In reply to Hector Pasarin

Re: Creating a label programatically

by Conn Warwicker -
Picture of Core developers Picture of Plugin developers
```
require_once($CFG->dirroot . '/course/modlib.php');

$courseID = 2;
$sectionID = 1;
$content = 'This is my label';

$course = get_course($courseID);
$module = $DB->get_record('modules', ['name' => 'label']);

$moduleinfo = new stdClass();
$moduleinfo->introeditor =
array(
'text' => $content,
'format' => '1',
'itemid' => NULL,
);
$moduleinfo->visible = '1';
$moduleinfo->visibleoncoursepage = 1;
$moduleinfo->course = $course->id;
$moduleinfo->coursemodule = 0;
$moduleinfo->section = $sectionID;
$moduleinfo->module = $module->id;
$moduleinfo->modulename = 'label';
$moduleinfo->instance = 0;
$moduleinfo->add = 'label';

$newcm = add_moduleinfo($moduleinfo, $course);
```
Average of ratings: Useful (3)
In reply to Conn Warwicker

Re: Creating a label programatically

by Hector Pasarin -
Thank you Conn,

Your code does exactly what I was looking for.

Greetings