Automatic installer lang files (20100904)
[moodle.git] / lib / messagelib.php
blob47b06b95c31f53b3f361cf8ea8733fa732451d9d
1 <?php
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.
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/>.
18 /**
19 * messagelib.php - Contains generic messaging functions for the message system
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
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Called when a message provider wants to send a message.
31 * This functions checks the user's processor configuration to send the given type of message,
32 * then tries to send it.
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
44 * @param object $eventdata information about the message (modulename, userfrom, userto, ...)
45 * @return boolean success
47 function message_send($eventdata) {
48 global $CFG, $DB;
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
53 //TODO: we need to solve problems with database transactions here somehow, for now we just prevent transactions - sorry
54 $DB->transactions_forbidden();
56 //after how long inactive should the user be considered logged off?
57 if (isset($CFG->block_online_users_timetosee)) {
58 $timetoshowusers = $CFG->block_online_users_timetosee * 60;
59 } else {
60 $timetoshowusers = 300;//5 minutes
63 // Work out if the user is logged in or not
64 if ((time() - $timetoshowusers) < $eventdata->userto->lastaccess) {
65 $userstate = 'loggedin';
66 } else {
67 $userstate = 'loggedoff';
70 // Create the message object
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();
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);
86 if ($processor == NULL) { //this user never had a preference, save default
87 if (!message_set_default_message_preferences($eventdata->userto)) {
88 print_error('cannotsavemessageprefs', 'message');
90 if ($userstate == 'loggedin') {
91 $processor = 'popup';
93 if ($userstate == 'loggedoff') {
94 $processor = 'email';
98 // if we are suposed to do something with this message
99 // No processor for this message, mark it as read
100 if ($processor == "") { //this user cleared all the preferences
101 $savemessage->timeread = time();
102 $messageid = $message->id;
103 unset($message->id);
104 $DB->insert_record('message_read', $savemessage);
106 } else { // Process the message
107 /// Store unread message just in case we can not send it
108 $savemessage->id = $DB->insert_record('message', $savemessage);
110 /// Try to deliver the message to each processor
111 $processorlist = explode(',', $processor);
112 foreach ($processorlist as $procname) {
113 $processorfile = $CFG->dirroot. '/message/output/'.$procname.'/message_output_'.$procname.'.php';
115 if (is_readable($processorfile)) {
116 include_once($processorfile); // defines $module with version etc
117 $processclass = 'message_output_' . $procname;
119 if (class_exists($processclass)) {
120 $pclass = new $processclass();
122 if (!$pclass->send_message($savemessage)) {
123 debugging('Error calling message processor '.$procname);
124 return false;
127 } else {
128 debugging('Error calling message processor '.$procname);
129 return false;
133 $savemessage->timeread = time();
134 $messageid = $savemessage->id;
135 unset($savemessage->id);
137 //if there is no more processors that want to process this we can move message to message_read
138 if ( $DB->count_records('message_working', array('unreadmessageid' => $messageid)) == 0){
139 $DB->insert_record('message_read', $savemessage);
140 $DB->delete_records('message', array('id' => $messageid));
144 return true;
149 * This code updates the message_providers table with the current set of providers
150 * @param $component - examples: 'moodle', 'mod_forum', 'block_quiz_results'
151 * @return boolean
153 function message_update_providers($component='moodle') {
154 global $DB;
156 // load message providers from files
157 $fileproviders = message_get_providers_from_file($component);
159 // load message providers from the database
160 $dbproviders = message_get_providers_from_db($component);
162 foreach ($fileproviders as $messagename => $fileprovider) {
164 if (!empty($dbproviders[$messagename])) { // Already exists in the database
166 if ($dbproviders[$messagename]->capability == $fileprovider['capability']) { // Same, so ignore
167 // exact same message provider already present in db, ignore this entry
168 unset($dbproviders[$messagename]);
169 continue;
171 } else { // Update existing one
172 $provider = new object();
173 $provider->id = $dbproviders[$messagename]->id;
174 $provider->capability = $fileprovider['capability'];
175 $DB->update_record('message_providers', $provider);
176 unset($dbproviders[$messagename]);
177 continue;
180 } else { // New message provider, add it
182 $provider = new object();
183 $provider->name = $messagename;
184 $provider->component = $component;
185 $provider->capability = $fileprovider['capability'];
187 $DB->insert_record('message_providers', $provider);
191 foreach ($dbproviders as $dbprovider) { // Delete old ones
192 $DB->delete_records('message_providers', array('id' => $dbprovider->id));
195 return true;
199 * Returns the active providers for the current user, based on capability
200 * @return array of message providers
202 function message_get_my_providers() {
203 global $DB;
205 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
207 $providers = $DB->get_records('message_providers');
209 // Remove all the providers we aren't allowed to see now
210 foreach ($providers as $providerid => $provider) {
211 if (!empty($provider->capability)) {
212 if (!has_capability($provider->capability, $systemcontext)) {
213 unset($providers[$providerid]); // Not allowed to see this
218 return $providers;
222 * Gets the message providers that are in the database for this component.
223 * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results'
224 * @return array of message providers
226 * INTERNAL - to be used from messagelib only
228 function message_get_providers_from_db($component) {
229 global $DB;
231 return $DB->get_records('message_providers', array('component'=>$component), '', 'name, id, component, capability'); // Name is unique per component
235 * Loads the messages definitions for the component (from file). If no
236 * messages are defined for the component, we simply return an empty array.
237 * @param $component - examples: 'moodle', 'mod_forum', 'block_quiz_results'
238 * @return array of message providerss or empty array if not exists
240 * INTERNAL - to be used from messagelib only
242 function message_get_providers_from_file($component) {
243 $defpath = get_component_directory($component).'/db/messages.php';
245 $messageproviders = array();
247 if (file_exists($defpath)) {
248 require($defpath);
251 foreach ($messageproviders as $name => $messageprovider) { // Fix up missing values if required
252 if (empty($messageprovider['capability'])) {
253 $messageproviders[$name]['capability'] = NULL;
257 return $messageproviders;
261 * Remove all message providers
262 * @param $component - examples: 'moodle', 'mod/forum', 'block/quiz_results'
264 function message_uninstall($component) {
265 return $DB->delete_records('message_providers', array('component' => $component));
269 * Set default message preferences.
270 * @param $user - User to set message preferences
272 function message_set_default_message_preferences($user) {
273 global $DB;
275 $providers = $DB->get_records('message_providers');
276 $preferences = array();
277 foreach ($providers as $providerid => $provider) {
278 $preferences['message_provider_'.$provider->component.'_'.$provider->name.'_loggedin'] = 'popup';
279 $preferences['message_provider_'.$provider->component.'_'.$provider->name.'_loggedoff'] = 'email';
281 return set_user_preferences($preferences, $user->id);