weekly release 3.6.8+
[moodle.git] / badges / cron.php
blob198982101bf78c64d346bb84e525aa7a87809685
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Cron job for reviewing and aggregating badge award criteria
20 * @package core
21 * @subpackage badges
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() {
31 global $CFG;
33 if (!empty($CFG->enablebadges)) {
34 badge_review_cron();
35 badge_message_cron();
39 /**
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() {
46 global $DB, $CFG;
47 $total = 0;
49 $courseparams = array();
50 if (empty($CFG->badges_allowcoursebadges)) {
51 $coursesql = '';
52 } else {
53 $coursesql = ' OR EXISTS (SELECT c.id FROM {course} c WHERE c.visible = :visible AND c.startdate < :current'
54 . ' AND c.id = b.courseid)';
55 $courseparams = array('visible' => true, 'current' => time());
58 $sql = 'SELECT b.id
59 FROM {badge} b
60 WHERE (b.status = :active OR b.status = :activelocked)
61 AND (b.type = :site ' . $coursesql . ')';
62 $badgeparams = array(
63 'active' => BADGE_STATUS_ACTIVE,
64 'activelocked' => BADGE_STATUS_ACTIVE_LOCKED,
65 'site' => BADGE_TYPE_SITE
67 $params = array_merge($badgeparams, $courseparams);
68 $badges = $DB->get_fieldset_sql($sql, $params);
70 mtrace('Started reviewing available badges.');
71 foreach ($badges as $bid) {
72 $badge = new badge($bid);
74 if ($badge->has_criteria()) {
75 if (debugging()) {
76 mtrace('Processing badge "' . $badge->name . '"...');
79 $issued = $badge->review_all_criteria();
81 if (debugging()) {
82 mtrace('...badge was issued to ' . $issued . ' users.');
84 $total += $issued;
88 mtrace('Badges were issued ' . $total . ' time(s).');
91 /**
92 * Sends out scheduled messages to badge creators
95 function badge_message_cron() {
96 global $DB;
98 mtrace('Sending scheduled badge notifications.');
100 $scheduled = $DB->get_records_select('badge', 'notification > ? AND (status != ?) AND nextcron < ?',
101 array(BADGE_MESSAGE_ALWAYS, BADGE_STATUS_ARCHIVED, time()),
102 'notification ASC', 'id, name, notification, usercreated as creator, timecreated');
104 foreach ($scheduled as $sch) {
105 // Send messages.
106 badge_assemble_notification($sch);
108 // Update next cron value.
109 $nextcron = badges_calculate_message_schedule($sch->notification);
110 $DB->set_field('badge', 'nextcron', $nextcron, array('id' => $sch->id));
115 * Creates single message for all notification and sends it out
117 * @param object $badge A badge which is notified about.
119 function badge_assemble_notification(stdClass $badge) {
120 global $DB;
122 $userfrom = core_user::get_noreply_user();
123 $userfrom->maildisplay = true;
125 if ($msgs = $DB->get_records_select('badge_issued', 'issuernotified IS NULL AND badgeid = ?', array($badge->id))) {
126 // Get badge creator.
127 $creator = $DB->get_record('user', array('id' => $badge->creator), '*', MUST_EXIST);
128 $creatorsubject = get_string('creatorsubject', 'badges', $badge->name);
129 $creatormessage = '';
131 // Put all messages in one digest.
132 foreach ($msgs as $msg) {
133 $issuedlink = html_writer::link(new moodle_url('/badges/badge.php', array('hash' => $msg->uniquehash)), $badge->name);
134 $recipient = $DB->get_record('user', array('id' => $msg->userid), '*', MUST_EXIST);
136 $a = new stdClass();
137 $a->user = fullname($recipient);
138 $a->link = $issuedlink;
139 $creatormessage .= get_string('creatorbody', 'badges', $a);
140 $DB->set_field('badge_issued', 'issuernotified', time(), array('badgeid' => $msg->badgeid, 'userid' => $msg->userid));
143 // Create a message object.
144 $eventdata = new \core\message\message();
145 $eventdata->courseid = SITEID;
146 $eventdata->component = 'moodle';
147 $eventdata->name = 'badgecreatornotice';
148 $eventdata->userfrom = $userfrom;
149 $eventdata->userto = $creator;
150 $eventdata->notification = 1;
151 $eventdata->subject = $creatorsubject;
152 $eventdata->fullmessage = format_text_email($creatormessage, FORMAT_HTML);
153 $eventdata->fullmessageformat = FORMAT_PLAIN;
154 $eventdata->fullmessagehtml = $creatormessage;
155 $eventdata->smallmessage = $creatorsubject;
157 message_send($eventdata);