MDL-65191 core: convert legacy cron to scheduled task
[moodle.git] / badges / cron.php
CommitLineData
27806552
YB
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.
13//
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/>.
16
17/**
18 * Cron job for reviewing and aggregating badge award criteria
19 *
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>
25 */
26
27defined('MOODLE_INTERNAL') || die();
28require_once($CFG->libdir . '/badgeslib.php');
29
30function badge_cron() {
31 global $CFG;
32
60d72efb 33 if (!empty($CFG->enablebadges)) {
27806552
YB
34 badge_review_cron();
35 badge_message_cron();
36 }
37}
38
39/**
40 * Reviews criteria and awards badges
41 *
42 * First find all badges that can be earned, then reviews each badge.
43 * (Not sure how efficient this is timewise).
44 */
45function badge_review_cron() {
46 global $DB, $CFG;
47 $total = 0;
48
49 $courseparams = array();
50 if (empty($CFG->badges_allowcoursebadges)) {
51 $coursesql = '';
52 } else {
53 $coursesql = ' OR EXISTS (SELECT id FROM {course} WHERE visible = :visible AND startdate < :current) ';
54 $courseparams = array('visible' => true, 'current' => time());
55 }
56
57 $sql = 'SELECT id
58 FROM {badge}
59 WHERE (status = :active OR status = :activelocked)
60 AND (type = :site ' . $coursesql . ')';
61 $badgeparams = array(
62 'active' => BADGE_STATUS_ACTIVE,
63 'activelocked' => BADGE_STATUS_ACTIVE_LOCKED,
64 'site' => BADGE_TYPE_SITE
65 );
66 $params = array_merge($badgeparams, $courseparams);
67 $badges = $DB->get_fieldset_sql($sql, $params);
68
69 mtrace('Started reviewing available badges.');
70 foreach ($badges as $bid) {
71 $badge = new badge($bid);
72
73 if ($badge->has_criteria()) {
74 if (debugging()) {
75 mtrace('Processing badge "' . $badge->name . '"...');
76 }
77
78 $issued = $badge->review_all_criteria();
79
80 if (debugging()) {
81 mtrace('...badge was issued to ' . $issued . ' users.');
82 }
83 $total += $issued;
84 }
85 }
86
87 mtrace('Badges were issued ' . $total . ' time(s).');
88}
89
90/**
91 * Sends out scheduled messages to badge creators
92 *
93 */
94function badge_message_cron() {
95 global $DB;
96
97 mtrace('Sending scheduled badge notifications.');
98
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');
102
103 foreach ($scheduled as $sch) {
104 // Send messages.
105 badge_assemble_notification($sch);
106
107 // Update next cron value.
108 $nextcron = badges_calculate_message_schedule($sch->notification);
109 $DB->set_field('badge', 'nextcron', $nextcron, array('id' => $sch->id));
110 }
111}
112
113/**
114 * Creates single message for all notification and sends it out
115 *
116 * @param object $badge A badge which is notified about.
117 */
118function badge_assemble_notification(stdClass $badge) {
f5d17c14
YB
119 global $DB;
120
121 $userfrom = core_user::get_noreply_user();
27806552
YB
122 $userfrom->maildisplay = true;
123
124 if ($msgs = $DB->get_records_select('badge_issued', 'issuernotified IS NULL AND badgeid = ?', array($badge->id))) {
125 // Get badge creator.
126 $creator = $DB->get_record('user', array('id' => $badge->creator), '*', MUST_EXIST);
127 $creatorsubject = get_string('creatorsubject', 'badges', $badge->name);
128 $creatormessage = '';
129
130 // Put all messages in one digest.
131 foreach ($msgs as $msg) {
132 $issuedlink = html_writer::link(new moodle_url('/badges/badge.php', array('hash' => $msg->uniquehash)), $badge->name);
133 $recipient = $DB->get_record('user', array('id' => $msg->userid), '*', MUST_EXIST);
134
135 $a = new stdClass();
136 $a->user = fullname($recipient);
137 $a->link = $issuedlink;
138 $creatormessage .= get_string('creatorbody', 'badges', $a);
139 $DB->set_field('badge_issued', 'issuernotified', time(), array('badgeid' => $msg->badgeid, 'userid' => $msg->userid));
140 }
141
142 // Create a message object.
cc350fd9 143 $eventdata = new \core\message\message();
880fc15b 144 $eventdata->courseid = SITEID;
27806552 145 $eventdata->component = 'moodle';
f5d17c14 146 $eventdata->name = 'badgecreatornotice';
27806552
YB
147 $eventdata->userfrom = $userfrom;
148 $eventdata->userto = $creator;
149 $eventdata->notification = 1;
150 $eventdata->subject = $creatorsubject;
f5d17c14 151 $eventdata->fullmessage = format_text_email($creatormessage, FORMAT_HTML);
27806552 152 $eventdata->fullmessageformat = FORMAT_PLAIN;
f5d17c14
YB
153 $eventdata->fullmessagehtml = $creatormessage;
154 $eventdata->smallmessage = $creatorsubject;
27806552
YB
155
156 message_send($eventdata);
157 }
60d72efb 158}