MDL-15892 Added some phpdocs to explain the module parameter better
[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/**
27 * messagelib.php - Contains the events handlers for the message system
28 *
29 * @author Luis Rodrigues
30 * @version $Id$
31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
32 * @package
33 */
34
35
36define('TIMETOSHOWUSERS', 300);
37
38/**
39 * Is trigged by an events_trigger in the MODULE_install function when
40 * a module wants to be a message provider provider.
41 * @param object $eventdata the information about the message provider (name and file)
42 * @return boolean success
43 */
44function message_provider_register_handler($eventdata) {
45 global $DB;
46 $return = true;
47
48 $provider = new object();
49 $provider->modulename = $eventdata->modulename;
50 $provider->modulefile = $eventdata->modulefile;
51 if (!$DB->insert_record('message_providers', $provider)) {
52 $return = false;
53 }
54
55 // everything ok :-)
56 return $return;
57}
58
59/**
60 * To be used to ungegister a message provider (curently not used)
61 * @param object $eventdata the information about the message provider (name and file)
62 * @return boolean success
63 */
64function message_provider_unregister_handler($eventdata) {
65 // everything ok :-)
66 return true;
67}
68
69/**
70 * Triggered when a message provider wants to send a message.
71 * This functions checks the user's processor configuration to send the given type of message,
72 * then tries to send it.
73 * @param object $eventdata information about he message (origin, destination, type, content)
74 * @return boolean success
75 */
76function message_send_handler($eventdata){
77 global $CFG, $DB;
78
79 if (isset($CFG->block_online_users_timetosee)) {
80 $timetoshowusers = $CFG->block_online_users_timetosee * 60;
81 } else {
82 $timetoshowusers = TIMETOSHOWUSERS;
83 }
84
85/// Work out if the user is logged in or not
86 if ((time() - $eventdata->userto->lastaccess) > $timetoshowusers) {
87 $userstate = 'loggedoff';
88 } else {
89 $userstate = 'loggedin';
90 }
91
92/// Create the message object
93 $savemessage = new object();
94 $savemessage->useridfrom = $eventdata->userfrom->id;
95 $savemessage->useridto = $eventdata->userto->id;
96 $savemessage->subject = $eventdata->subject;
97 $savemessage->fullmessage = $eventdata->fullmessage;
98 $savemessage->fullmessageformat = $eventdata->fullmessageformat;
99 $savemessage->fullmessagehtml = $eventdata->fullmessagehtml;
100 $savemessage->smallmessage = $eventdata->smallmessage;
101 $savemessage->timecreated = time();
102
103/// Find out what processors are defined currently
104
105 // XXX TODO
106 // Note this currently defaults to email all the time. We need a better solution
107 // to be able to distinguish between a user who has no settings and one who doesn't want contact
108 // ... perhaps a "none" setting
109
110 $processor = get_user_preferences('message_provider_'.$eventdata->modulename.'_'.$userstate, 'email', $eventdata->userto->id);
111
112/// Now process the message
113
114 if (empty($processor)) { // There is no processor so just mark it as read
115 $savemessage->timeread = time();
116 $messageid = $message->id;
117 unset($message->id);
118 $DB->insert_record('message_read', $savemessage);
119
120 } else { // Process the message
121
122 /// Store unread message just in case we can not send it
123 $savemessage->id = $DB->insert_record('message', $savemessage);
124
125
126 /// Try to deliver the message to each processor
127 $processorlist = explode(',', $processor);
128 foreach ($processorlist as $procname) {
129 $processorfile = $CFG->dirroot. '/message/output/'.$procname.'/message_output_'.$procname.'.php';
130
131 if (is_readable($processorfile)) {
132 include_once( $processorfile ); // defines $module with version etc
133 $processclass = 'message_output_' . $procname;
134
135 if (class_exists($processclass)) {
136 $pclass = new $processclass();
137
138 if (! $pclass->send_message($savemessage)) {
139 debugging('Error calling message processor '.$procname);
140 return false;
141 }
142 }
143 } else {
144 debugging('Error calling message processor '.$procname);
145 return false;
146 }
147 }
148 }
149
150 return true;
151}
152
153?>