General developer forum

"require_login" and cron executions

Picture of Iñaki Arenaza
Re: "require_login" and cron executions
Core developersDocumentation writersParticularly helpful MoodlersPlugin developers

Hi Alan,

duplicating a course means having backup and restore capabilities. Those capabilities are tied to role assignments, which in turn are tied (assigned) to users. So the duplicate_course() function is checking which user is executing the action (via require_login()).

When you run your cli cron endpoint from moodlecron, Moodle is setting up some user (probably admin user, but I'm not really sure about this) before executing your endpoint. When you schedule it independently as it own script, nothing is setting up that user and thus require_login() fails.

You can check for this by having a look at the $USER global variable in your cron endpoint. You could use something similar to the following code in your cron endpoint to make it work in both scenarios:

if (empty($USER) || empty($USER->id)) {
    $USER = $DB->get_record('user', array('username'=>'some-user-with-the-right-roles', 'deleted'=>0));

You need to add this piece of code before calling duplicate_course().



Average of ratings: -