add_moduleinfo throwing error

add_moduleinfo throwing error

από Marios Theo -
Αριθμός απαντήσεων: 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



Μέσος όρος βαθμολογίας: -
Σε απάντηση σε Marios Theo

Re: add_moduleinfo throwing error

από 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.
Σε απάντηση σε Marios Theo

Re: add_moduleinfo throwing error

από Andreas Grabs -
Φωτογραφία Core developers Φωτογραφία Particularly helpful Moodlers Φωτογραφία Peer reviewers Φωτογραφία Plugin developers Φωτογραφία 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
Σε απάντηση σε Andreas Grabs

Re: add_moduleinfo throwing error

από Michael Hughes -
Φωτογραφία Core developers Φωτογραφία Particularly helpful Moodlers Φωτογραφία 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
Σε απάντηση σε Marios Theo

Re: add_moduleinfo throwing error

από Michael Hughes -
Φωτογραφία Core developers Φωτογραφία Particularly helpful Moodlers Φωτογραφία 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.
Σε απάντηση σε Michael Hughes

Re: add_moduleinfo throwing error

από 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!