Schedule task for each course block

Schedule task for each course block

by CNM Richardson -
Number of replies: 1

I am trying to develop a block plugin for our moodle courses that will email all students every week who have not attended the course that week.

I have been able to create a block with a schedule task via the Task API.

The first problem I am running into, is how do I get the context of each block/course?

I tried using this code in task execute() function:

```global $COURSE;

$context = context_course::instance($COURSE->id);```

But this can't find the class `context_course` when it runs.

Any idea how I could achieve this? 

Thanks.


Average of ratings: -
In reply to CNM Richardson

Re: Schedule task for each course block

by Brian Merritt -
Picture of Particularly helpful Moodlers
\context_course is defined in accesslib.php and if you feed it with a valid id it should give you the context record for that course (assuming you are in a course, then $COURSE->id should work). If you aren't in a course (for example running in a scheduled task) then you should probably first search the DB for all relevant blocks and use that course id to get the id for the context.