diff --git a/classes/event/certificate_created.php b/classes/event/certificate_created.php index adb94b3..acc58df 100644 --- a/classes/event/certificate_created.php +++ b/classes/event/certificate_created.php @@ -29,4 +29,14 @@ namespace mod_certificate\event; defined('MOODLE_INTERNAL') || die(); class certificate_created extends \core\event\base { + /** + * Init method. + * + * @return void + */ + protected function init() { + $this->data['crud'] = 'r'; + $this->data['edulevel'] = self::LEVEL_OTHER; + $this->data['objecttable'] = 'certificate'; + } } diff --git a/lib.php b/lib.php index 743142c..4ff6a57 100644 --- a/lib.php +++ b/lib.php @@ -349,6 +349,13 @@ function certificate_get_post_actions() { function certificate_cron () { global $DB; + // $sql = "select distinct cm.id as cmid, c.*, ce.id as certificateid, ce.name as certificatename + // from {certificate} ce + // inner join {course} c on c.id = ce.course + // inner join {course_modules} cm on cm.course = c.id and cm.instance = ce.id + // where c.visible = 1 + // order by c.id"; + $sql = "select distinct cm.id as cmid, c.*, ce.id as certificateid, ce.name as certificatename from {certificate} ce inner join {course} c on c.id = ce.course @@ -367,6 +374,7 @@ function certificate_cron () { from {certificate_issues} ci where ci.certificateid = ?) order by u.firstname"; + foreach ($courses as $course) { echo " Processing course {$course->fullname}, certificate: {$course->certificatename}...\n"; $students = $DB->get_records_sql($sql, array($course->id, $course->certificateid)); @@ -380,17 +388,12 @@ function certificate_cron () { (certificate_get_course_time($course->id) >= ($certificate->requiredtime * 60))) { echo " generating issue certificate for {$student->firstname} {$student->lastname}..."; $certrecord = certificate_get_issue($course, $student, $certificate, $cm); - - $context = context_module::instance($cm->id); - + $context = context_course::instance($course->id); $event = \mod_certificate\event\certificate_created::create(array( 'objectid' => $certrecord->id, 'context' => $context, 'other' => $certrecord) ); - //$event->add_record_snapshot('course_modules', $this->cm); - //$event->add_record_snapshot('attendance_sessions', $session); - //$event->add_record_snapshot('attendance_log', $record); $event->trigger(); echo " Done! Certificate {$certrecord->code} generated!\n"; diff --git a/locallib.php b/locallib.php index e9bf1d4..c69fe94 100644 --- a/locallib.php +++ b/locallib.php @@ -366,7 +366,10 @@ function certificate_get_issue($course, $user, $certificate, $cm) { // Create new certificate issue record $certissue = new stdClass(); $certissue->certificateid = $certificate->id; + $certissue->user = $user->username; + $certissue->course = $certificate->course; $certissue->userid = $user->id; + $certissue->gradefmt = $certificate->gradefmt; $certissue->code = certificate_generate_code(); $certissue->timecreated = time(); $certissue->id = $DB->insert_record('certificate_issues', $certissue);