add_moduleinfo throwing error

add_moduleinfo throwing error

by Marios Theo -
Number of replies: 6

Hello people,

I am trying to create a label in my moodle course with the following code.

    $module = $DB->get_record('modules', array('name' => 'label'));
    foreach($courses_to_add_actibity as $course){
        $customLabel = new stdClass();

        $customLabel->name = "Instructions";
        $customLabel->introeditor = array("text" => "Test", "format" => "1", "itemid" => 0);
        $customLabel->grade = 100;
        $customLabel->gradecat = 60;
        $customLabel->gradepass = 0;
        $customLabel->visible = "1";
        $customLabel->visibleoncoursepage = "1";
        $customLabel->availabilityconditionsjson = '{"op":"&","c":[],"showc":[]}';
        $customLabel->course = $course->id;
        $customLabel->coursemodule = 0;
        $customLabel->section = 0;
        $customLabel->module = $module->id;
        $customLabel->modulename = "label";
        $customLabel->instance = 0;
        $customLabel->add = "label";
        $customLabel->update = 0;
        $customLabel->return = 0;
        $customLabel->sr = 0;
        $moduleinfo = add_moduleinfo($customLabel, $course);
    }

After i execute above code i get the error:

!!! Invalid user !!!
!! SELECT id FROM {user} WHERE id = ? AND deleted = ?
[array (
  0 => 0,
  1 => 0,
)]
Error code: invaliduser !!
!! Stack trace: * line 1599 of \lib\dml\moodle_database.php: dml_missing_record_exception thrown
* line 1575 of \lib\dml\moodle_database.php: call to moodle_database->get_record_select()
* line 6496 of \lib\accesslib.php: call to moodle_database->get_record()
* line 1051 of \lib\filelib.php: call to context_user::instance()
* line 159 of \course\modlib.php: call to file_save_draft_area_files()
* line 128 of \custompage\createlabel\addActivity.php: call to add_moduleinfo()
 !!

What am i missing?

I am using moodle 3.9.1+

I really appreciate any response.

Regards
Marios



Average of ratings: -
In reply to Marios Theo

Re: add_moduleinfo throwing error

by Marios Theo -
Just an update,
Labels are created when I visit the url addActivity.php where this code is in.
What i am trying to figure out though is if can do it by terminal as i have to create labels in a very long list with courses.

Thanking you for your time.
In reply to Marios Theo

Re: add_moduleinfo throwing error

by Andreas Grabs -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Translators
Hi Marios,

if you run your script by cli you have to setup the admin user.
Just add the following line before your code:
cron_setup_user();

Best regards
Andreas
Average of ratings: Useful (1)
In reply to Andreas Grabs

Re: add_moduleinfo throwing error

by Michael Hughes -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
or pass in a $user object: cron_setup_user($user) if you want to run it as a specific user, it depends on who you want the action to be logged as
Average of ratings: Useful (1)
In reply to Michael Hughes

Re: add_moduleinfo throwing error

by Marios Theo -
Thank you both for your answers,
It worked now. and i also set the user object so i keep logs of who executed what.
Super useful!
In reply to Marios Theo

Re: add_moduleinfo throwing error

by Michael Hughes -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
I suspect your issue here is that you're not running this as a "user" (at least it's not evident the code above).

The introeditor process file_save_draft_area_files() expects the draft files area to be associated with a user (as normally a user would be uploading / adding these to the intro text).

This function expects that the $USER global object has been set sensibly. Have you called "require_login()" at the top of the page to ensure that it is running as a user that is logged in? This should set the $USER global appropriately.

If you don't do this then you've got the potential that this script is runnable by *anyone*, including anonymous users.
Average of ratings: Useful (1)
In reply to Michael Hughes

Re: add_moduleinfo throwing error

by Marios Theo -
No i didn't set the global $USER, i thought that would not be necessary.
I understand that "require_login()" is mandatory as to avoid anonymous users running the script. I am utilizing this in my other scripts that i have in my moodle instance.
Thank you again for your time! I really appreciate it!