MDL-20700 coding style cleanup - cvs keywords removed, closign php tag removed, trail...
[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
30 * @version $Id$
31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
32 * @package
33 */
34require_once($CFG->dirroot.'/message/output/lib.php');
35
36class message_output_email extends message_output {
37 /**
38 * Processes the message (sends by email).
39 * @param object $message the message to be sent
40 */
41 function send_message($message) {
42 global $DB;
43
44 //send an email
45 //if fails saved as read message
46
47 //first try to get preference
48 $usertoemail = get_user_preferences( 'message_processor_email_email', '', $message->useridto);
49 //if fails use user profile default
50 if ( $usertoemail == NULL){
51 $userto = $DB->get_record('user', array('id' => $message->useridto));
52 $usertoemail = $userto->email;
53 }
54 $userfrom = $DB->get_record('user', array('id' => $message->useridfrom));
55 if ( email_to_user($usertoemail, $userfrom->email,
56 $message->subject, $message->fullmessage,
57 $message->fullmessagehtml)
58 ){
59 /// Move the entry to the other table
60 $message->timeread = time();
61 $messageid = $message->id;
62 unset($message->id);
63
64 //if there is no more processor that want to process this can move message
65 if ( $DB->count_records('message_working', array('unreadmessageid' => $messageid)) == 0){
66 if ($DB->insert_record('message_read', $message)) {
67 $DB->delete_records('message', array('id' => $messageid));
68 }
69 }
70 }else{
71 //delete what we've processed and check if can move message
72 if ( $DB->count_records('message_working', array('unreadmessageid' => $messageid)) == 0){
73 if ($DB->insert_record('message_read', $message)) {
74 $DB->delete_records('message', array('id' => $messageid));
75 }
76 }
77 }
78 return true;
79 }
80
81 /**
82 * Creates necessary fields in the messaging config form.
83 * @param object $mform preferences form class
84 */
366e4ec4 85 function config_form($preferences){
86 global $USER;
305a014f 87 $string = get_string('email').': <input size="30" name="email_email" value="'.$preferences->email_email.'" />';
88 if (empty($preferences->email_email)) {
89 $string .= ' ('.get_string('default').': '.$USER->email.')';
366e4ec4 90 }
305a014f 91 return $string;
3b120e46 92 }
93
94 /**
95 * Parses the form submited data and saves it into preferences array.
96 * @param object $mform preferences form class
97 * @param array $preferences preferences array
98 */
d18b1bbd 99 function process_form($form, &$preferences){
3b120e46 100 $preferences['message_processor_email_email'] = $form->email_email;
101 }
102
103 /**
104 * Loads the config data from database to put on the form (initial load)
105 * @param array $preferences preferences array
106 * @param int $userid the user id
107 */
108 function load_data(&$preferences, $userid){
109 $preferences->email_email = get_user_preferences( 'message_processor_email_email', '', $userid);
110 }
111}