message MDL-24771 a big commit that fixes a whole bunch of small issues with messaging
[moodle.git] / message / output / email / message_output_email.php
CommitLineData
3b120e46 1<?php
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 * Email message processor - send a given message by email
28 *
29 * @author Luis Rodrigues
3b120e46 30 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
1d422980 31 * @package
3b120e46 32 */
33require_once($CFG->dirroot.'/message/output/lib.php');
34
35class message_output_email extends message_output {
36 /**
37 * Processes the message (sends by email).
38 * @param object $message the message to be sent
39 */
40 function send_message($message) {
fe561379 41 global $DB, $SITE;
3b120e46 42
6d2e6936
AD
43 $userto = $DB->get_record('user', array('id' => $message->useridto));
44 $userfrom = $DB->get_record('user', array('id' => $message->useridfrom));
45
46 //check user preference for where user wants email sent
47 $usertoemailaddress = get_user_preferences('message_processor_email_email', '', $message->useridto);
3b120e46 48
6d2e6936
AD
49 if ( !empty($usertoemailaddress)) {
50 $userto->email = $usertoemailaddress;
3b120e46 51 }
6d2e6936
AD
52
53 $result = email_to_user($userto, $userfrom,
6ee2611c 54 $message->subject, $message->fullmessage, $message->fullmessagehtml);
3b120e46 55
fe561379
AD
56 return $result===true; //email_to_user() can return true, false or "emailstop"
57 //return true;//do we want to report an error if email sending fails?
3b120e46 58 }
59
1d422980 60 /**
3b120e46 61 * Creates necessary fields in the messaging config form.
62 * @param object $mform preferences form class
63 */
1d422980 64 function config_form($preferences){
366e4ec4 65 global $USER;
a813a748 66 $string = get_string('email','message_email').': <input size="30" name="email_email" value="'.$preferences->email_email.'" />';
305a014f 67 if (empty($preferences->email_email)) {
68 $string .= ' ('.get_string('default').': '.$USER->email.')';
366e4ec4 69 }
305a014f 70 return $string;
3b120e46 71 }
72
73 /**
71666cf3 74 * Parses the form submitted data and saves it into preferences array.
3b120e46 75 * @param object $mform preferences form class
1d422980 76 * @param array $preferences preferences array
3b120e46 77 */
d18b1bbd 78 function process_form($form, &$preferences){
3b120e46 79 $preferences['message_processor_email_email'] = $form->email_email;
80 }
81
82 /**
1d422980
PS
83 * Loads the config data from database to put on the form (initial load)
84 * @param array $preferences preferences array
3b120e46 85 * @param int $userid the user id
86 */
87 function load_data(&$preferences, $userid){
88 $preferences->email_email = get_user_preferences( 'message_processor_email_email', '', $userid);
89 }
90}