2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Cron job for reviewing and aggregating badge award criteria
22 * @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
27 defined('MOODLE_INTERNAL') ||
die();
28 require_once($CFG->libdir
. '/badgeslib.php');
30 function badge_cron() {
33 if (!empty($CFG->enablebadges
)) {
40 * Reviews criteria and awards badges
42 * First find all badges that can be earned, then reviews each badge.
43 * (Not sure how efficient this is timewise).
45 function badge_review_cron() {
49 $courseparams = array();
50 if (empty($CFG->badges_allowcoursebadges
)) {
53 $coursesql = ' OR EXISTS (SELECT id FROM {course} WHERE visible = :visible AND startdate < :current) ';
54 $courseparams = array('visible' => true, 'current' => time());
59 WHERE (status = :active OR status = :activelocked)
60 AND (type = :site ' . $coursesql . ')';
62 'active' => BADGE_STATUS_ACTIVE
,
63 'activelocked' => BADGE_STATUS_ACTIVE_LOCKED
,
64 'site' => BADGE_TYPE_SITE
66 $params = array_merge($badgeparams, $courseparams);
67 $badges = $DB->get_fieldset_sql($sql, $params);
69 mtrace('Started reviewing available badges.');
70 foreach ($badges as $bid) {
71 $badge = new badge($bid);
73 if ($badge->has_criteria()) {
75 mtrace('Processing badge "' . $badge->name
. '"...');
78 $issued = $badge->review_all_criteria();
81 mtrace('...badge was issued to ' . $issued . ' users.');
87 mtrace('Badges were issued ' . $total . ' time(s).');
91 * Sends out scheduled messages to badge creators
94 function badge_message_cron() {
97 mtrace('Sending scheduled badge notifications.');
99 $scheduled = $DB->get_records_select('badge', 'notification > ? AND (status != ?) AND nextcron < ?',
100 array(BADGE_MESSAGE_ALWAYS
, BADGE_STATUS_ARCHIVED
, time()),
101 'notification ASC', 'id, name, notification, usercreated as creator, timecreated');
103 foreach ($scheduled as $sch) {
105 badge_assemble_notification($sch);
107 // Update next cron value.
108 $nextcron = badges_calculate_message_schedule($sch->notification
);
109 $DB->set_field('badge', 'nextcron', $nextcron, array('id' => $sch->id
));
114 * Creates single message for all notification and sends it out
116 * @param object $badge A badge which is notified about.
118 function badge_assemble_notification(stdClass
$badge) {
121 $admin = get_admin();
122 $userfrom = new stdClass();
123 $userfrom->id
= $admin->id
;
124 $userfrom->email
= !empty($CFG->badges_defaultissuercontact
) ?
$CFG->badges_defaultissuercontact
: $admin->email
;
125 $userfrom->firstname
= !empty($CFG->badges_defaultissuername
) ?
$CFG->badges_defaultissuername
: $admin->firstname
;
126 $userfrom->lastname
= !empty($CFG->badges_defaultissuername
) ?
'' : $admin->lastname
;
127 $userfrom->maildisplay
= true;
129 if ($msgs = $DB->get_records_select('badge_issued', 'issuernotified IS NULL AND badgeid = ?', array($badge->id
))) {
130 // Get badge creator.
131 $creator = $DB->get_record('user', array('id' => $badge->creator
), '*', MUST_EXIST
);
132 $creatorsubject = get_string('creatorsubject', 'badges', $badge->name
);
133 $creatormessage = '';
135 // Put all messages in one digest.
136 foreach ($msgs as $msg) {
137 $issuedlink = html_writer
::link(new moodle_url('/badges/badge.php', array('hash' => $msg->uniquehash
)), $badge->name
);
138 $recipient = $DB->get_record('user', array('id' => $msg->userid
), '*', MUST_EXIST
);
141 $a->user
= fullname($recipient);
142 $a->link
= $issuedlink;
143 $creatormessage .= get_string('creatorbody', 'badges', $a);
144 $DB->set_field('badge_issued', 'issuernotified', time(), array('badgeid' => $msg->badgeid
, 'userid' => $msg->userid
));
147 // Create a message object.
148 $eventdata = new stdClass();
149 $eventdata->component
= 'moodle';
150 $eventdata->name
= 'instantmessage';
151 $eventdata->userfrom
= $userfrom;
152 $eventdata->userto
= $creator;
153 $eventdata->notification
= 1;
154 $eventdata->subject
= $creatorsubject;
155 $eventdata->fullmessage
= $creatormessage;
156 $eventdata->fullmessageformat
= FORMAT_PLAIN
;
157 $eventdata->fullmessagehtml
= format_text($creatormessage, FORMAT_HTML
);
158 $eventdata->smallmessage
= '';
160 message_send($eventdata);