MDL-10107 Fixed the definition of message providers ... it now works through files...
[moodle.git] / lib / messagelib.php
CommitLineData
3b120e46 1<?php // $Id$
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
10// Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
25
26/**
120b3758 27 * messagelib.php - Contains generic messaging functions for the message system
3b120e46 28 *
120b3758 29 * @author Luis Rodrigues and Martin Dougiamas
3b120e46 30 * @version $Id$
31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
32 * @package
33 */
34
35
36define('TIMETOSHOWUSERS', 300);
37
3b120e46 38/**
39 * Triggered when a message provider wants to send a message.
40 * This functions checks the user's processor configuration to send the given type of message,
41 * then tries to send it.
120b3758 42 * @param object $eventdata information about the message (origin, destination, type, content)
3b120e46 43 * @return boolean success
44 */
45function message_send_handler($eventdata){
46 global $CFG, $DB;
47
48 if (isset($CFG->block_online_users_timetosee)) {
49 $timetoshowusers = $CFG->block_online_users_timetosee * 60;
50 } else {
51 $timetoshowusers = TIMETOSHOWUSERS;
52 }
53
54/// Work out if the user is logged in or not
55 if ((time() - $eventdata->userto->lastaccess) > $timetoshowusers) {
56 $userstate = 'loggedoff';
57 } else {
58 $userstate = 'loggedin';
59 }
60
61/// Create the message object
62 $savemessage = new object();
63 $savemessage->useridfrom = $eventdata->userfrom->id;
64 $savemessage->useridto = $eventdata->userto->id;
65 $savemessage->subject = $eventdata->subject;
66 $savemessage->fullmessage = $eventdata->fullmessage;
67 $savemessage->fullmessageformat = $eventdata->fullmessageformat;
68 $savemessage->fullmessagehtml = $eventdata->fullmessagehtml;
69 $savemessage->smallmessage = $eventdata->smallmessage;
70 $savemessage->timecreated = time();
71
72/// Find out what processors are defined currently
73
74 // XXX TODO
75 // Note this currently defaults to email all the time. We need a better solution
76 // to be able to distinguish between a user who has no settings and one who doesn't want contact
77 // ... perhaps a "none" setting
78
120b3758 79 $processor = get_user_preferences('message_provider_'.$eventdata->component.$eventdata->name.'_'.$userstate, 'email', $eventdata->userto->id);
3b120e46 80
81/// Now process the message
82
83 if (empty($processor)) { // There is no processor so just mark it as read
84 $savemessage->timeread = time();
85 $messageid = $message->id;
86 unset($message->id);
87 $DB->insert_record('message_read', $savemessage);
88
89 } else { // Process the message
90
91 /// Store unread message just in case we can not send it
92 $savemessage->id = $DB->insert_record('message', $savemessage);
93
94
95 /// Try to deliver the message to each processor
96 $processorlist = explode(',', $processor);
97 foreach ($processorlist as $procname) {
98 $processorfile = $CFG->dirroot. '/message/output/'.$procname.'/message_output_'.$procname.'.php';
99
100 if (is_readable($processorfile)) {
101 include_once( $processorfile ); // defines $module with version etc
102 $processclass = 'message_output_' . $procname;
103
104 if (class_exists($processclass)) {
105 $pclass = new $processclass();
106
107 if (! $pclass->send_message($savemessage)) {
108 debugging('Error calling message processor '.$procname);
109 return false;
110 }
111 }
112 } else {
113 debugging('Error calling message processor '.$procname);
114 return false;
115 }
116 }
117 }
118
119 return true;
120}
121
120b3758 122
123/**
124 * This code updates the message_providers table with the current set of providers
125 * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results'
126 * @return boolean
127 */
128function message_update_providers($component='moodle') {
129 global $DB;
130
131 // load message providers from files
132 $fileproviders = message_get_providers_from_file($component);
133
134 // load message providers from the database
135 $dbproviders = message_get_providers_from_db($component);
136
137 foreach ($fileproviders as $messagename => $fileprovider) {
138
139 if (!empty($dbproviders[$messagename])) { // Already exists in the database
140
141 if ($dbproviders[$messagename]->capability == $fileprovider['capability']) { // Same, so ignore
142 // exact same message provider already present in db, ignore this entry
143 unset($dbproviders[$messagename]);
144 continue;
145
146 } else { // Update existing one
147 $provider = new object();
148 $provider->id = $dbproviders[$messagename]->id;
149 $provider->capability = $fileprovider['capability'];
150 $DB->update_record('message_providers', $provider);
151 unset($dbproviders[$messagename]);
152 continue;
153 }
154
155 } else { // New message provider, add it
156
157 $provider = new object();
158 $provider->name = $messagename;
159 $provider->component = $component;
160 $provider->capability = $fileprovider['capability'];
161
162 $DB->insert_record('message_providers', $provider);
163 }
164 }
165
166 foreach ($dbproviders as $dbprovider) { // Delete old ones
167 $DB->delete_records('message_providers', array('id' => $dbprovider->id));
168 }
169
170 return true;
171}
172
173/**
174 * Returns the active providers for the current user, based on capability
175 * @return array of message providers
176 */
177function message_get_my_providers() {
178 global $DB;
179
180 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
181
182 $providers = $DB->get_records('message_providers');
183
184 // Remove all the providers we aren't allowed to see now
185 foreach ($providers as $providerid => $provider) {
186 if (!empty($provider->capability)) {
187 if (!has_capability($provider->capability, $systemcontext)) {
188 unset($providers[$providerid]); // Not allowed to see this
189 }
190 }
191 }
192
193 return $providers;
194}
195
196/**
197 * Gets the message providers that are in the database for this component.
198 * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results'
199 * @return array of message providers
200 *
201 * INTERNAL - to be used from messagelib only
202 */
203function message_get_providers_from_db($component) {
204 global $DB;
205
206 if ($dbproviders = $DB->get_records('message_providers', array('component'=>$component), '',
207 'name, id, component, capability')) { // Name is unique per component
208 return $dbproviders;
209 }
210
211 return array();
212}
213
214/**
215 * Loads the messages definitions for the component (from file). If no
216 * messages are defined for the component, we simply return an empty array.
217 * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results'
218 * @return array of message providerss or empty array if not exists
219 *
220 * INTERNAL - to be used from messagelib only
221 */
222function message_get_providers_from_file($component) {
223 global $CFG;
224
225 if ($component == 'moodle') {
226 $defpath = $CFG->libdir.'/db/messages.php';
227
228 } else if ($component == 'unittest') {
229 $defpath = $CFG->libdir.'/simpletest/fixtures/messages.php';
230
231 } else {
232 $compparts = explode('/', $component);
233
234 if ($compparts[0] == 'block') {
235 // Blocks are an exception. Blocks directory is 'blocks', and not
236 // 'block'. So we need to jump through hoops.
237 $defpath = $CFG->dirroot.'/blocks/'.$compparts[1].'/db/messages.php';
238
239 } else if ($compparts[0] == 'format') {
240 // Similar to the above, course formats are 'format' while they
241 // are stored in 'course/format'.
242 $defpath = $CFG->dirroot.'/course/format/'.$compparts[1].'/db/messages.php';
243
244 } else if ($compparts[0] == 'gradeimport') {
245 $defpath = $CFG->dirroot.'/grade/import/'.$compparts[1].'/db/messages.php';
246
247 } else if ($compparts[0] == 'gradeexport') {
248 $defpath = $CFG->dirroot.'/grade/export/'.$compparts[1].'/db/messages.php';
249
250 } else if ($compparts[0] == 'gradereport') {
251 $defpath = $CFG->dirroot.'/grade/report/'.$compparts[1].'/db/messages.php';
252
253 } else {
254 $defpath = $CFG->dirroot.'/'.$component.'/db/messages.php';
255 }
256 }
257
258 $messageproviders = array();
259
260 if (file_exists($defpath)) {
261 require($defpath);
262 }
263
264 foreach ($messageproviders as $name => $messageprovider) { // Fix up missing values if required
265 if (empty($messageprovider['capability'])) {
266 $messageproviders[$name]['capability'] = NULL;
267 }
268 }
269
270 return $messageproviders;
271}
272
273/**
274 * Remove all message providers
275 * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results'
276 */
277function message_uninstall($component) {
278 return $DB->delete_records('message_providers', array('component' => $component));
279}
280
281
3b120e46 282?>