MDL-79091 core_badges: refactor bades cron task

MDL-79091 core_badges: refactor bades cron task
This commit is contained in:
Jay Oswald 2023-11-30 15:34:12 +11:00
parent 5b5e0aab8c
commit 248f7e91f2
2 changed files with 40 additions and 24 deletions

View File

@ -24,10 +24,20 @@ namespace core\task;
*/
class badges_adhoc_task extends adhoc_task {
/**
* Sets the name of the badges adhoc task
*
* @return void
*/
public function get_name() {
return get_string('taskbadgesadhoc', 'admin');
}
/**
* Badge adhoc task to assign a single badge
*
* @return void
*/
public function execute() {
$data = $this->get_custom_data();
$badge = new \core_badges\badge($data->badgeid);
@ -43,7 +53,7 @@ class badges_adhoc_task extends adhoc_task {
mtrace("$traceprefix badge was issued to $issued users.");
} catch (\moodle_exception $e) {
$badgeeditlink =
$badgeeditlink =
new \moodle_url('/badges/edit.php', ['id' => $data->badgeid, 'action' => 'badge']);
switch($e->errorcode){

View File

@ -45,32 +45,38 @@ class badges_cron_task extends scheduled_task {
*/
public function execute() {
global $DB, $CFG;
if (!empty($CFG->enablebadges)) {
require_once($CFG->libdir . '/badgeslib.php');
if (empty($CFG->enablebadges)) {
return;
}
require_once($CFG->libdir . '/badgeslib.php');
$courseparams = array();
if (empty($CFG->badges_allowcoursebadges)) {
$coursesql = '';
} else {
$coursesql = ' OR EXISTS (SELECT c.id FROM {course} c WHERE c.visible = :visible AND c.startdate < :current'
. ' AND c.id = b.courseid) ';
$courseparams = array('visible' => true, 'current' => time());
}
$courseparams = [];
if (empty($CFG->badges_allowcoursebadges)) {
$coursesql = '';
} else {
$coursesql = "OR EXISTS (
SELECT c.id
FROM {course} c
WHERE c.visible = :visible
AND c.startdate < :current
AND c.id = b.courseid
) ";
$courseparams = ['visible' => 1, 'current' => time()];
}
$sql = 'SELECT b.id
FROM {badge} b
WHERE (b.status = :active OR b.status = :activelocked)
AND (b.type = :site ' . $coursesql . ')';
$badgeparams = [
'active' => BADGE_STATUS_ACTIVE,
'activelocked' => BADGE_STATUS_ACTIVE_LOCKED,
'site' => BADGE_TYPE_SITE
];
$params = array_merge($badgeparams, $courseparams);
$badges = $DB->get_fieldset_sql($sql, $params);
$sql = "SELECT b.id
FROM {badge} b
WHERE (b.status = :active OR b.status = :activelocked)
AND (b.type = :site $coursesql )";
$badgeparams = [
'active' => BADGE_STATUS_ACTIVE,
'activelocked' => BADGE_STATUS_ACTIVE_LOCKED,
'site' => BADGE_TYPE_SITE,
];
$params = array_merge($badgeparams, $courseparams);
$badges = $DB->get_fieldset_sql($sql, $params);
mtrace('Started reviewing available badges.');
foreach ($badges as $bid) {
foreach ($badges as $bid) {
$task = new badges_adhoc_task();
$task->set_custom_data(['badgeid' => $bid]);
manager::queue_adhoc_task($task, true);