removed bogus code + spelling fix
[moodle.git] / lib / messagelib.php
CommitLineData
ce02a9bf 1<?php
3b120e46 2
ce02a9bf 3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
3b120e46 17
18/**
120b3758 19 * messagelib.php - Contains generic messaging functions for the message system
3b120e46 20 *
78bfb562
PS
21 * @package core
22 * @subpackage message
23 * @copyright Luis Rodrigues and Martin Dougiamas
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3b120e46 25 */
26
78bfb562
PS
27defined('MOODLE_INTERNAL') || die();
28
3b120e46 29/**
7c7d3afa 30 * Called when a message provider wants to send a message.
3b120e46 31 * This functions checks the user's processor configuration to send the given type of message,
32 * then tries to send it.
7c7d3afa
PS
33 *
34 * Required parameter $eventdata structure:
35 * modulename -
36 * userfrom
37 * userto
38 * subject
39 * fullmessage - the full message in a given format
40 * fullmessageformat - the format if the full message (FORMAT_MOODLE, FORMAT_HTML, ..)
41 * fullmessagehtml - the full version (the message processor will choose with one to use)
42 * smallmessage - the small version of the message
43 *
44 * @param object $eventdata information about the message (modulename, userfrom, userto, ...)
3b120e46 45 * @return boolean success
46 */
7c7d3afa 47function message_send($eventdata) {
3b120e46 48 global $CFG, $DB;
49
7c7d3afa
PS
50 //TODO: this function is very slow and inefficient, it would be a major bottleneck in cron processing, this has to be improved in 2.0
51 // probably we could add two parameters with user messaging preferences and we could somehow preload/cache them in cron
e484041b
PS
52
53 //TODO: we need to solve problems with database transactions here somehow, for now we just prevent transactions - sorry
54 $DB->transactions_forbidden();
7c7d3afa 55
1560760f 56 //after how long inactive should the user be considered logged off?
3b120e46 57 if (isset($CFG->block_online_users_timetosee)) {
58 $timetoshowusers = $CFG->block_online_users_timetosee * 60;
59 } else {
1560760f 60 $timetoshowusers = 300;//5 minutes
3b120e46 61 }
62
1560760f
AD
63 // Work out if the user is logged in or not
64 if ((time() - $timetoshowusers) < $eventdata->userto->lastaccess) {
3b120e46 65 $userstate = 'loggedin';
1560760f
AD
66 } else {
67 $userstate = 'loggedoff';
3b120e46 68 }
117bd748 69
1560760f 70 // Create the message object
3b120e46 71 $savemessage = new object();
72 $savemessage->useridfrom = $eventdata->userfrom->id;
73 $savemessage->useridto = $eventdata->userto->id;
74 $savemessage->subject = $eventdata->subject;
75 $savemessage->fullmessage = $eventdata->fullmessage;
76 $savemessage->fullmessageformat = $eventdata->fullmessageformat;
77 $savemessage->fullmessagehtml = $eventdata->fullmessagehtml;
78 $savemessage->smallmessage = $eventdata->smallmessage;
79 $savemessage->timecreated = time();
80
1560760f
AD
81 // Find out what processors are defined currently
82 // When a user doesn't have settings none gets return, if he doesn't want contact "" gets returned
83 $preferencename = 'message_provider_'.$eventdata->component.'_'.$eventdata->name.'_'.$userstate;
84 $processor = get_user_preferences($preferencename, NULL, $eventdata->userto->id);
117bd748 85
7c7d3afa
PS
86 if ($processor == NULL) { //this user never had a preference, save default
87 if (!message_set_default_message_preferences($eventdata->userto)) {
818dc096 88 print_error('cannotsavemessageprefs', 'message');
2044a2b2 89 }
7c7d3afa
PS
90 if ($userstate == 'loggedin') {
91 $processor = 'popup';
2044a2b2 92 }
7c7d3afa
PS
93 if ($userstate == 'loggedoff') {
94 $processor = 'email';
2044a2b2 95 }
96 }
3b120e46 97
d3674ef7 98 // if we are supposed to do something with this message
2044a2b2 99 // No processor for this message, mark it as read
100 if ($processor == "") { //this user cleared all the preferences
117bd748 101 $savemessage->timeread = time();
3b120e46 102 $DB->insert_record('message_read', $savemessage);
103
104 } else { // Process the message
3b120e46 105 /// Store unread message just in case we can not send it
117bd748
PS
106 $savemessage->id = $DB->insert_record('message', $savemessage);
107
3b120e46 108 /// Try to deliver the message to each processor
109 $processorlist = explode(',', $processor);
110 foreach ($processorlist as $procname) {
111 $processorfile = $CFG->dirroot. '/message/output/'.$procname.'/message_output_'.$procname.'.php';
112
117bd748 113 if (is_readable($processorfile)) {
7c7d3afa 114 include_once($processorfile); // defines $module with version etc
3b120e46 115 $processclass = 'message_output_' . $procname;
117bd748
PS
116
117 if (class_exists($processclass)) {
3b120e46 118 $pclass = new $processclass();
119
7c7d3afa 120 if (!$pclass->send_message($savemessage)) {
3b120e46 121 debugging('Error calling message processor '.$procname);
122 return false;
117bd748 123 }
3b120e46 124 }
125 } else {
126 debugging('Error calling message processor '.$procname);
127 return false;
128 }
129 }
1560760f
AD
130
131 $savemessage->timeread = time();
132 $messageid = $savemessage->id;
133 unset($savemessage->id);
134
135 //if there is no more processors that want to process this we can move message to message_read
136 if ( $DB->count_records('message_working', array('unreadmessageid' => $messageid)) == 0){
9d97f08e
PS
137 $DB->insert_record('message_read', $savemessage);
138 $DB->delete_records('message', array('id' => $messageid));
1560760f 139 }
3b120e46 140 }
141
142 return true;
143}
144
120b3758 145
146/**
147 * This code updates the message_providers table with the current set of providers
17da2e6f 148 * @param $component - examples: 'moodle', 'mod_forum', 'block_quiz_results'
120b3758 149 * @return boolean
150 */
151function message_update_providers($component='moodle') {
152 global $DB;
153
154 // load message providers from files
155 $fileproviders = message_get_providers_from_file($component);
156
157 // load message providers from the database
158 $dbproviders = message_get_providers_from_db($component);
159
160 foreach ($fileproviders as $messagename => $fileprovider) {
161
162 if (!empty($dbproviders[$messagename])) { // Already exists in the database
117bd748 163
120b3758 164 if ($dbproviders[$messagename]->capability == $fileprovider['capability']) { // Same, so ignore
165 // exact same message provider already present in db, ignore this entry
166 unset($dbproviders[$messagename]);
167 continue;
168
169 } else { // Update existing one
170 $provider = new object();
7c7d3afa
PS
171 $provider->id = $dbproviders[$messagename]->id;
172 $provider->capability = $fileprovider['capability'];
120b3758 173 $DB->update_record('message_providers', $provider);
174 unset($dbproviders[$messagename]);
175 continue;
176 }
177
178 } else { // New message provider, add it
179
180 $provider = new object();
181 $provider->name = $messagename;
182 $provider->component = $component;
183 $provider->capability = $fileprovider['capability'];
184
185 $DB->insert_record('message_providers', $provider);
186 }
187 }
188
189 foreach ($dbproviders as $dbprovider) { // Delete old ones
190 $DB->delete_records('message_providers', array('id' => $dbprovider->id));
191 }
192
193 return true;
194}
195
196/**
197 * Returns the active providers for the current user, based on capability
198 * @return array of message providers
199 */
200function message_get_my_providers() {
201 global $DB;
202
203 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
204
205 $providers = $DB->get_records('message_providers');
206
207 // Remove all the providers we aren't allowed to see now
208 foreach ($providers as $providerid => $provider) {
209 if (!empty($provider->capability)) {
210 if (!has_capability($provider->capability, $systemcontext)) {
211 unset($providers[$providerid]); // Not allowed to see this
212 }
213 }
214 }
215
216 return $providers;
217}
218
219/**
220 * Gets the message providers that are in the database for this component.
221 * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results'
222 * @return array of message providers
223 *
224 * INTERNAL - to be used from messagelib only
225 */
226function message_get_providers_from_db($component) {
227 global $DB;
228
7c7d3afa 229 return $DB->get_records('message_providers', array('component'=>$component), '', 'name, id, component, capability'); // Name is unique per component
120b3758 230}
231
232/**
233 * Loads the messages definitions for the component (from file). If no
234 * messages are defined for the component, we simply return an empty array.
17da2e6f 235 * @param $component - examples: 'moodle', 'mod_forum', 'block_quiz_results'
120b3758 236 * @return array of message providerss or empty array if not exists
237 *
238 * INTERNAL - to be used from messagelib only
239 */
240function message_get_providers_from_file($component) {
17da2e6f 241 $defpath = get_component_directory($component).'/db/messages.php';
120b3758 242
243 $messageproviders = array();
244
245 if (file_exists($defpath)) {
246 require($defpath);
247 }
248
249 foreach ($messageproviders as $name => $messageprovider) { // Fix up missing values if required
250 if (empty($messageprovider['capability'])) {
251 $messageproviders[$name]['capability'] = NULL;
252 }
253 }
254
255 return $messageproviders;
256}
257
258/**
117bd748 259 * Remove all message providers
120b3758 260 * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results'
261 */
262function message_uninstall($component) {
03dd0575 263 global $DB;
120b3758 264 return $DB->delete_records('message_providers', array('component' => $component));
265}
266
2044a2b2 267/**
268 * Set default message preferences.
269 * @param $user - User to set message preferences
270 */
7c7d3afa 271function message_set_default_message_preferences($user) {
2044a2b2 272 global $DB;
117bd748 273
2044a2b2 274 $providers = $DB->get_records('message_providers');
275 $preferences = array();
7c7d3afa
PS
276 foreach ($providers as $providerid => $provider) {
277 $preferences['message_provider_'.$provider->component.'_'.$provider->name.'_loggedin'] = 'popup';
278 $preferences['message_provider_'.$provider->component.'_'.$provider->name.'_loggedoff'] = 'email';
117bd748 279 }
7c7d3afa 280 return set_user_preferences($preferences, $user->id);
2044a2b2 281}