Dear Moodle developers,
I'm searching to write a local plugin whose aim is to update an external database (with user, course and date of completion) after a user completes a course.
I followed instructions from https://moodledev.io/docs/apis/plugintypes/local and other sources. After having checked if other discussion on the topic are available I'm asking you where I'm doing a mistake in writing the code.
Here are the steps. Fyi I see my local plugin as a listener of the course completed event. But when I complete a course nothing is fired and no error is displayed.
Perhaps I missed one step or what else. If you can have a look to the code it will be an help.
Thank you.
- Definition of the event observer
In the file /plugin_folder/db/events.php is defined the hook to the event.
$observers = array (
array (
'eventname' => '\core\event\course_completed',
'callback' => 'local_coursecompletion\local_coursecompletion_observer::external_table_update',
));
- Definition of the callback function called after event triggering
The callback function is defined in the file /plugin_folder/db/observer.php
namespace local_coursecompletion;
include("/plugin_folder/coursecompletion/lib.php");
class local_coursecompletion_observer {
public static function external_table_update(\core\event\course_completed $eventdata) {
global $CFG;
global $DB;
$courseid=$eventdata->courseid;
$userid=$eventdata->userid;
$date=$eventdata→datecompletion;
// external db update
$sync_ext_db=local_coursecompletion_sync_external_table($userid, $courseid, $date);
// event triggering external db updates to log system
$event= \local_coursecompletion\event\external_db_updated::create(array(
'userid' => $userid,
'courseid' => $courseid
));
$event->add_record_snapshot('user', $userid);
$event->add_record_snapshot('course', $courseid);
$event->trigger();
}
- Definition of the function updating external database
In the file /plugin_folder/lib.php
function local_coursecompletion_sync_external_table($userid, $courseid, $datecompletion) : int {
error_reporting( -1);
ini_set('display_errors', 1 );
$conn = oci_connect('user', 'pwd', 'db');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
return -1;
}
$sql = "UPDATE prova_geco_moodle SET campo3 = '12-JAN-23' WHERE codice_corso = 'a1' AND utente = 'vallinoto_ld' ";
$stid = oci_parse($conn, $sql);
if (!$stid) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
return -2;
}
oci_free_statement($stid);
oci_close($conn);
return 0;
}
- Definition of the event \local_coursecompletion\event\external_db_updated
In the file plugin_folder/classes/event/external_db_updated.php
namespace local_coursecompletion\event;
class external_db_updated extends \core\event\base {
protected function init() {
$this->data['crud'] = 'u'; // c(reate), r(ead), u(pdate), d(elete)
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'GECO_MOODLE';
$this->data['description'] = 'Descrizione evento';
}
public static function get_name() {
return get_string('external_db_updated', 'local_coursecompletion');
}
public function get_description() {
return "The user with id {$this->userid} updated the external table GECO_MOODLE with id {$this->objectid}.";
}
}