not getting a course_completed event?

not getting a course_completed event?

by tim st.clair -
Number of replies: 2
Picture of Plugin developers
 

I have a local plugin called 'myevent' which handles the course_completed event. It's event.php is effectively like this:

$observers = array (
array(
'eventname' => '\core\event\course_completed',
'callback' => 'local_myevent_observer::course_completed_handler',
);

in classes/observer.php I'm effectively just throwing all the event data out to an external logger just for the purposes of seeing this work. But I'll want to do other stuff with it, if I can get the event working consistently.

public static function course_completed_handler(\core\event\course_completed $event) {
global $DB, $CFG;
$eventdata = $event->get_record_snapshot('course_completions', $event->objectid);
// go to http://requestb.in/ and press "create a requestbin"
$url = "http://requestb.in/uj7tr0uj";
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "data=".serialize($eventdata));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
}

In order to test completion I've done the following:

  1. created a course with completions enabled.
  2. created a lesson in the course and made a simple quiz
  3. put completion of the activity based on the outcome of the quiz
  4. NOT set a completion rule on the course (made sure none are set).
  5. enrolled a new student and tested that activity completion works.
  6. run the cron job by hand.
  7. Checked completion report to see that the user hasn't completed the course
  8. Checked the mdl_course_completions for this course to see a record created for the student; timestarted = 0, timecompleted = null.
  9. Gone to course completion settings and enabled "completion by ANY", then selected the lesson activity as the condition.
  10. waited a while, then run the cron job by hand again.
  11. checked mdl_course_completions for the course again and found that timestarted is set, and timecompleted is still null.
  12. waited a while, then run the cron job by hand once more
  13. checked mdl_course_completions for the course, and found that the timecompleted is now set.
  14. checked the external provider to see if the curl call has been done - no data has been posted.
  15. created a new event handler which observes core\event\course_updated and pointed it at a similar logging function. edited / saved the course record, and checked requestbin for the result. sure enough, it comes through for that event.

I even through in an @error_log(...) statement just to throw something into the php error log if the event was being run, but don't get anything appearing in the log, even as the timecompleted field is written.

It doesn't seem to matter if course completion rules are set before or after a user starts the course. I went back and (after resetting the course) re-tried the user again, this time with the completion rule already set before the student started then completed the course, but the event still didn't seem to work.

So the event / observer stuff is working, but not when completion runs on the cron. I don't particularly want to do it on an update trigger on the mdl_course_completions table, since I want to run a moodle-connected php process. Where to from here?

Average of ratings: Useful (1)
In reply to tim st.clair

Re: not getting a course_completed event?

by Michael Milette -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Hi Tim,

Did you ever figure out what the problem was with getting the course_completed event working? I am currently having the same issue.

Best regards,

Michael

Average of ratings: Useful (1)
In reply to Michael Milette

Re: not getting a course_completed event?

by Arindam Das -

I faced the same problem. To test I raised the event like this

$data = new stdClass;
$data->id = 165;
$data->userid = 104;
$data->relateduserid = 104;
$data->course = 16;
\core\event\course_completed::create_from_completion($data)->trigger();

and in that case my handler would fire.

But from inside the mark_complete function of completion_completion class my handler would not be executed.