From 670a81a64fd89dcfe3ef88e0908dfc36c506c0ad Mon Sep 17 00:00:00 2001 From: Sesostris Vieira Date: Fri, 18 Jul 2014 16:37:52 -0300 Subject: [PATCH] =?UTF-8?q?Rotina=20CRON=20para=20gera=C3=A7=C3=A3o=20auto?= =?UTF-8?q?m=C3=A1tica=20dos=20certificados=20de=20alunos=20aprovados.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.php | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ version.php | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/lib.php b/lib.php index da930b9..fa29788 100644 --- a/lib.php +++ b/lib.php @@ -262,6 +262,54 @@ function certificate_supports($feature) { * TODO:This needs to be done */ 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 and cm.module = ? + where c.visible = 1 + order by c.id"; + + echo "\n\nGenerate certificates for user that has completed the course-------------------------\n"; + + $certmodule = $DB->get_record('modules', array('name' => 'certificate')); + $courses = $DB->get_records_sql($sql, array($certmodule->id)); + + $sql = "select u.* + from {course_completions} cc + inner join {user} u on u.id = cc.userid + where cc.course = ? + and cc.timecompleted is not null + and cc.userid not in (select ci.userid + 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)); + $total = count($students); + + if ($total > 0) { + echo " {$total} students without certificate found, generating certificates for them...\n"; + $certificate = $DB->get_record('certificate', array('id' => $course->certificateid)); + $cm = get_coursemodule_from_id('certificate', $course->cmid); + foreach ($students as $student) { + if (!$certificate->requiredtime or + (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); + echo " Done! Certificate {$certrecord->code} generated!\n"; + } else { + echo " {$student->firstname} {$student->lastname} has not required course time. Skiped!\n"; + } + } + } else { + echo " No students without certificate found! Course skipped.\n"; + } + } + return true; } diff --git a/version.php b/version.php index 2caf2fd..8c98463 100644 --- a/version.php +++ b/version.php @@ -26,7 +26,7 @@ $module->version = 2013102300; // The current module version (Date: YYYYMMDDXX) $module->requires = 2012120300; // Requires this Moodle version -$module->cron = 0; // Period for cron to check this module (secs) +$module->cron = 12 * 60 * 60; // Period for cron to check this module (secs) 12 hours $module->component = 'mod_certificate'; $module->maturity = MATURITY_STABLE;