message MDL-25264 made it possible to edit another user's message preferences
[moodle.git] / message / output / email / message_output_email.php
1 <?php
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 ///////////////////////////////////////////////////////////////////////////
26 /**
27  * Email message processor - send a given message by email
28  *
29  * @author Luis Rodrigues
30  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
31  * @package
32  */
33 require_once($CFG->dirroot.'/message/output/lib.php');
35 class message_output_email extends message_output {
36     /**
37      * Processes the message (sends by email).
38      * @param object $eventdata the event data submitted by the message sender plus $eventdata->savedmessageid
39      */
40     function send_message($eventdata) {
41         global $SITE;
43         //hold onto email preference because /admin/cron.php sends a lot of messages at once
44         static $useremailaddresses = array();
46         //check user preference for where user wants email sent
47         if (!array_key_exists($eventdata->userto->id, $useremailaddresses)) {
48             $useremailaddresses[$eventdata->userto->id] = get_user_preferences('message_processor_email_email', $eventdata->userto->email, $eventdata->userto->id);
49         }
50         $usertoemailaddress = $useremailaddresses[$eventdata->userto->id];
52         if ( !empty($usertoemailaddress)) {
53             $userto->email = $usertoemailaddress;
54         }
56         $result = email_to_user($eventdata->userto, $eventdata->userfrom,
57             $eventdata->subject, $eventdata->fullmessage, $eventdata->fullmessagehtml);
59         return $result;
60     }
62     /**
63      * Creates necessary fields in the messaging config form.
64      * @param object $mform preferences form class
65      */
66     function config_form($preferences){
67         global $USER;
68         $string = get_string('email','message_email').': <input size="30" name="email_email" value="'.$preferences->email_email.'" />';
69         if (empty($preferences->email_email)) {
70             $userdefaultemail = null;
71             if (empty($preferences->userdefaultemail)) {
72                 $userdefaultemail = $USER->email;
73             } else {
74                 $userdefaultemail = $preferences->userdefaultemail;
75             }
76             $string .= ' ('.get_string('default').': '.$userdefaultemail.')';
77         }
78         return $string;
79     }
81     /**
82      * Parses the form submitted data and saves it into preferences array.
83      * @param object $mform preferences form class
84      * @param array $preferences preferences array
85      */
86     function process_form($form, &$preferences){
87         $preferences['message_processor_email_email'] = $form->email_email;
88     }
90     /**
91      * Loads the config data from database to put on the form (initial load)
92      * @param array $preferences preferences array
93      * @param int $userid the user id
94      */
95     function load_data(&$preferences, $userid){
96         $preferences->email_email = get_user_preferences( 'message_processor_email_email', '', $userid);
97     }
98 }