mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 20:42:22 +02:00
Merge branch 'MDL-79091' of https://github.com/jay-oswald/moodle
This commit is contained in:
commit
fd8c759323
@ -1398,7 +1398,8 @@ $string['taskadmintitle'] = 'Tasks';
|
||||
$string['taskanalyticscleanup'] = 'Analytics cleanup';
|
||||
$string['taskautomatedbackup'] = 'Automated backups';
|
||||
$string['taskbackupcleanup'] = 'Clean backup tables, logs and files';
|
||||
$string['taskbadgescron'] = 'Award badges';
|
||||
$string['taskbadgesadhoc'] = 'Award badge';
|
||||
$string['taskbadgescron'] = 'Add award badges adhoc tasks';
|
||||
$string['taskbadgesmessagecron'] = 'Background processing for sending badges notifications';
|
||||
$string['taskblogcron'] = 'Sync external blogs';
|
||||
$string['taskcachecleanup'] = 'Remove expired cache entries';
|
||||
|
70
lib/classes/task/badges_adhoc_task.php
Normal file
70
lib/classes/task/badges_adhoc_task.php
Normal file
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace core\task;
|
||||
/**
|
||||
* Class badges_adhoc_task
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2023 Jay Oswald <jayoswald@catalyst-au.net>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
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);
|
||||
$traceprefix = "Badge $data->badgeid: $badge->name: ";
|
||||
|
||||
try {
|
||||
if (!$badge->has_criteria()) {
|
||||
mtrace("$traceprefix Badge has no criteria to be processed");
|
||||
return;
|
||||
}
|
||||
|
||||
$issued = $badge->review_all_criteria();
|
||||
mtrace("$traceprefix badge was issued to $issued users.");
|
||||
|
||||
} catch (\moodle_exception $e) {
|
||||
$badgeeditlink =
|
||||
new \moodle_url('/badges/edit.php', ['id' => $data->badgeid, 'action' => 'badge']);
|
||||
|
||||
switch($e->errorcode){
|
||||
case 'invalidcoursemoduleid':
|
||||
$badge->set_status(BADGE_STATUS_INACTIVE);
|
||||
mtrace("$traceprefix has invalid course modules, it has been made inactive $badgeeditlink");
|
||||
break;
|
||||
default:
|
||||
mtrace("$traceprefix Error: {$e->getMessage()} $badgeeditlink");
|
||||
throw($e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -45,50 +45,43 @@ class badges_cron_task extends scheduled_task {
|
||||
*/
|
||||
public function execute() {
|
||||
global $DB, $CFG;
|
||||
if (!empty($CFG->enablebadges)) {
|
||||
require_once($CFG->libdir . '/badgeslib.php');
|
||||
$total = 0;
|
||||
|
||||
$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());
|
||||
}
|
||||
|
||||
$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) {
|
||||
$badge = new \badge($bid);
|
||||
|
||||
if ($badge->has_criteria()) {
|
||||
if (debugging()) {
|
||||
mtrace('Processing badge "' . $badge->name . '"...');
|
||||
}
|
||||
|
||||
$issued = $badge->review_all_criteria();
|
||||
|
||||
if (debugging()) {
|
||||
mtrace('...badge was issued to ' . $issued . ' users.');
|
||||
}
|
||||
$total += $issued;
|
||||
}
|
||||
}
|
||||
|
||||
mtrace('Badges were issued ' . $total . ' time(s).');
|
||||
if (empty($CFG->enablebadges)) {
|
||||
return;
|
||||
}
|
||||
require_once($CFG->libdir . '/badgeslib.php');
|
||||
|
||||
$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);
|
||||
|
||||
foreach ($badges as $bid) {
|
||||
$task = new badges_adhoc_task();
|
||||
$task->set_custom_data(['badgeid' => $bid]);
|
||||
manager::queue_adhoc_task($task, true);
|
||||
}
|
||||
|
||||
mtrace(count($badges) . " adhoc badge tasks were added");
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user