files MDL-20601 Conversion of many of the uses of print_textarea, and the mforms...
[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 $message the message to be sent
39      */
40     function send_message($message) {
41         global $DB;
43         //send an email
44         //if fails saved as read message
46         //first try to get preference
47         $usertoemail = get_user_preferences( 'message_processor_email_email', '', $message->useridto);
48         //if fails use user profile default
49         if ( $usertoemail == NULL){
50             $userto   = $DB->get_record('user', array('id' => $message->useridto));
51             $usertoemail = $userto->email;
52         }
53         $userfrom = $DB->get_record('user', array('id' => $message->useridfrom));
54         if ( email_to_user($usertoemail, $userfrom->email,
55             $message->subject, $message->fullmessage,
56             $message->fullmessagehtml)
57         ){
58             /// Move the entry to the other table
59             $message->timeread = time();
60             $messageid = $message->id;
61             unset($message->id);
63             //if there is no more processor that want to process this can move message
64             if ( $DB->count_records('message_working', array('unreadmessageid' => $messageid)) == 0){
65                 if ($DB->insert_record('message_read', $message)) {
66                     $DB->delete_records('message', array('id' => $messageid));
67                 }
68             }
69         }else{
70             //delete what we've processed and check if can move message
71             $messageid = $message->id;
72             unset($message->id);
73             if ( $DB->count_records('message_working', array('unreadmessageid' => $messageid)) == 0){
74                 if ($DB->insert_record('message_read', $message)) {
75                     $DB->delete_records('message', array('id' => $messageid));
76                 }
77             }
78         }
79         return true;
80     }
82     /**
83      * Creates necessary fields in the messaging config form.
84      * @param object $mform preferences form class
85      */
86     function config_form($preferences){
87         global $USER;
88         $string = get_string('email').': <input size="30" name="email_email" value="'.$preferences->email_email.'" />';
89         if (empty($preferences->email_email)) {
90             $string .= ' ('.get_string('default').': '.$USER->email.')';
91         }
92         return $string;
93     }
95     /**
96      * Parses the form submited data and saves it into preferences array.
97      * @param object $mform preferences form class
98      * @param array $preferences preferences array
99      */
100     function process_form($form, &$preferences){
101         $preferences['message_processor_email_email'] = $form->email_email;
102     }
104     /**
105      * Loads the config data from database to put on the form (initial load)
106      * @param array $preferences preferences array
107      * @param int $userid the user id
108      */
109     function load_data(&$preferences, $userid){
110         $preferences->email_email = get_user_preferences( 'message_processor_email_email', '', $userid);
111     }