MDL-10107 - First check-in of Messaging 2.0 code from Luis Rodrigues, GSOC student.
[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 */
85 function config_form(&$mform){
86 $mform->addElement('static', 'email_labelemail', get_string('processortag', 'message').'Email ', '');
87 $mform->addElement('text', 'email_email', get_string('email'));
88 $mform->setAdvanced('email_labelemail');
89 $mform->setAdvanced('email_email');
90 }
91
92 /**
93 * Parses the form submited data and saves it into preferences array.
94 * @param object $mform preferences form class
95 * @param array $preferences preferences array
96 */
97 function process_form(&$form, &$preferences){
98 $preferences['message_processor_email_email'] = $form->email_email;
99 }
100
101 /**
102 * Loads the config data from database to put on the form (initial load)
103 * @param array $preferences preferences array
104 * @param int $userid the user id
105 */
106 function load_data(&$preferences, $userid){
107 $preferences->email_email = get_user_preferences( 'message_processor_email_email', '', $userid);
108 }
109}