Force course completion using API

Force course completion using API

by Manolo Mohedano -
Number of replies: 2

Hello all... We have developed a little local plugin in our moodle (v 3.5) platform. After doing some operations, we need to set a course as completed for a particular user.

I've seen the Activity Completion API (at https://docs.moodle.org/dev/Activity_completion_API) and the code write in lib/completionlib.php and course/completion.php and I've found the way to set an activity as viewed, but I'm not able to find the way to set a course as completed for a student. In this kind of courses we don't have any other activity or resource we can use in course completion criteria, students only see some text in section 0 description.

We have our courses configured to be 'manually' marked as completed by teachers, so I suppose we can do an INSERT query in 'course_completions' table directly in our code, but we prefer to do it better (using Completion API or similar).

What would be the best way to do this?

Thanks in advance for your time...

Average of ratings: -
In reply to Manolo Mohedano

Re: Force course completion using API

by Renaat Debleu -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

One should use the Moodle API: it will work in the future - log items will be created - permissions will be checked.

I would use something like:


$completioninfo = new completion_info($course);
$criteria = $completioninfo->get_criteria(COMPLETION_CRITERIA_TYPE_ROLE);
foreach ($criteria as $criterion) {
$completions = $completioninfo->get_completions($userid, COMPLETION_CRITERIA_TYPE_ROLE);
foreach ($completions as $completion) {
if ($completion->is_complete()) {
continue;
}
if ($completion->criteriaid === $criterion->id) {
$criterion->complete($completion);
}
}
}
Average of ratings: Useful (1)
In reply to Renaat Debleu

Re: Force course completion using API

by Manolo Mohedano -

Thanks a lot, Renaat. I'm trying your solution and looks great!

Thanks for your time. Best regards...