Set activity completion using PHP

Set activity completion using PHP

by Anton van der Merwe -
Number of replies: 9

Hi there,

(it seems that this is the more appropriate forum to post this)

So hoping someone can help me with what shoud be fairly easy (but I'm battling to find an answer in all my searches)!

I've developed a few activities using Flash. All I want to do is set the activity to 'completed' by calling a Moodle PHP function / running a script.  (maybe even giving the student a 100% grade)

I don't want to package it as a SCORM object. (been there, done that, working, but don't want the SCORM 'interface').

Your guidance would be highly appreciated!

(using ver 2.4)

Average of ratings: -
In reply to Anton van der Merwe

Re: Set activity completion using PHP

by Ankit Agarwal -

Have you looked at the completetion class in /completion?

In reply to Ankit Agarwal

Re: Set activity completion using PHP

by Anton van der Merwe -

Hi Ankit, I'm new to the moodle.  I'm battling to find where I can look into the Completion class.  Can you point me into the right direction?

Thx

In reply to Anton van der Merwe

Re: Set activity completion using PHP

by Darko Miletić -

Something like this would be in order

// $course is full course object
$completion_info = new completion_info($course);
// $cm is course module object - activity
$completion = $completion_info->update_state($cm, COMPLETION_COMPLETE);
Average of ratings: Useful (1)
In reply to Darko Miletić

Re: Set activity completion using PHP

by Anton van der Merwe -

Thank you so much for your reply.

In my php script, would I have to "require" config.php to be able to access to these functions?

In reply to Anton van der Merwe

Re: Set activity completion using PHP

by Darko Miletić -

Yes for using any Moodle API config.php is a must. Than add any additional libraries you intend to use.

In this case you would need these:

require_once(<relative path to config.php>);
require_once($CFG->libdir.'/completionlib.php');
// $course is full couurse object
$completion_info = new completion_info($course);
// $cm is course module object
$completion = $completion_info->update_state($cm, COMPLETION_COMPLETE));
Average of ratings: Useful (2)
In reply to Darko Miletić

Re: Set activity completion using PHP

by Anton van der Merwe -

Thank you Darko!

In reply to Anton van der Merwe

Re: Set activity completion using PHP

by Gonzalo Santos -

hi, very util script

how can i set the objetc cm from an ID?

thakz

In reply to Gonzalo Santos

Re: Set activity completion using PHP

by Gonzalo Santos -

this code not work for me, any i forget to include?

 

// Registrar actividad como completada en Moodle
// obtengo coursemodule por ID
$cm = get_coursemodule_from_id('quiz', 13);

// completo curso por ID

$courseid = $cm->course;
$course_object = $DB->get_record('course', array('id'=>$courseid));
//
$completion_info = new completion_info($course_object);
//print_r($completion_info);
$completion = $completion_info->update_state($cm, COMPLETION_COMPLETE);
echo $completion;