[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/message/output/email/ -> message_output_email.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Contains the definiton of the email message processors (sends messages to users via email)
  19   *
  20   * @package   message_email
  21   * @copyright 2008 Luis Rodrigues and Martin Dougiamas
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once($CFG->dirroot.'/message/output/lib.php');
  26  
  27  /**
  28   * The email message processor
  29   *
  30   * @package   message_email
  31   * @copyright 2008 Luis Rodrigues and Martin Dougiamas
  32   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class message_output_email extends message_output {
  35      /**
  36       * Processes the message (sends by email).
  37       * @param object $eventdata the event data submitted by the message sender plus $eventdata->savedmessageid
  38       */
  39      function send_message($eventdata) {
  40          global $CFG;
  41  
  42          // Ignore $CFG->noemailever here because we want to test this code,
  43          // the message sending fails later in email_to_user().
  44  
  45          // skip any messaging suspended and deleted users
  46          if ($eventdata->userto->auth === 'nologin' or $eventdata->userto->suspended or $eventdata->userto->deleted) {
  47              return true;
  48          }
  49  
  50          //the user the email is going to
  51          $recipient = null;
  52  
  53          //check if the recipient has a different email address specified in their messaging preferences Vs their user profile
  54          $emailmessagingpreference = get_user_preferences('message_processor_email_email', null, $eventdata->userto);
  55          $emailmessagingpreference = clean_param($emailmessagingpreference, PARAM_EMAIL);
  56  
  57          // If the recipient has set an email address in their preferences use that instead of the one in their profile
  58          // but only if overriding the notification email address is allowed
  59          if (!empty($emailmessagingpreference) && !empty($CFG->messagingallowemailoverride)) {
  60              //clone to avoid altering the actual user object
  61              $recipient = clone($eventdata->userto);
  62              $recipient->email = $emailmessagingpreference;
  63          } else {
  64              $recipient = $eventdata->userto;
  65          }
  66  
  67          // Check if we have attachments to send.
  68          $attachment = '';
  69          $attachname = '';
  70          if (!empty($CFG->allowattachments) && !empty($eventdata->attachment)) {
  71              if (empty($eventdata->attachname)) {
  72                  // Attachment needs a file name.
  73                  debugging('Attachments should have a file name. No attachments have been sent.', DEBUG_DEVELOPER);
  74              } else if (!($eventdata->attachment instanceof stored_file)) {
  75                  // Attachment should be of a type stored_file.
  76                  debugging('Attachments should be of type stored_file. No attachments have been sent.', DEBUG_DEVELOPER);
  77              } else {
  78                  // Copy attachment file to a temporary directory and get the file path.
  79                  $attachment = $eventdata->attachment->copy_content_to_temp();
  80  
  81                  // Get attachment file name.
  82                  $attachname = clean_filename($eventdata->attachname);
  83              }
  84          }
  85  
  86          // Configure mail replies - this is used for incoming mail replies.
  87          $replyto = '';
  88          $replytoname = '';
  89          if (isset($eventdata->replyto)) {
  90              $replyto = $eventdata->replyto;
  91              if (isset($eventdata->replytoname)) {
  92                  $replytoname = $eventdata->replytoname;
  93              }
  94          }
  95  
  96          $result = email_to_user($recipient, $eventdata->userfrom, $eventdata->subject, $eventdata->fullmessage,
  97                                  $eventdata->fullmessagehtml, $attachment, $attachname, true, $replyto, $replytoname);
  98  
  99          // Remove an attachment file if any.
 100          if (!empty($attachment) && file_exists($attachment)) {
 101              unlink($attachment);
 102          }
 103  
 104          return $result;
 105      }
 106  
 107      /**
 108       * Creates necessary fields in the messaging config form.
 109       *
 110       * @param array $preferences An array of user preferences
 111       */
 112      function config_form($preferences){
 113          global $USER, $OUTPUT, $CFG;
 114  
 115          if (empty($CFG->messagingallowemailoverride)) {
 116              return null;
 117          }
 118  
 119          $inputattributes = array('size'=>'30', 'name'=>'email_email', 'value'=>$preferences->email_email);
 120          $string = get_string('email','message_email') . ': ' . html_writer::empty_tag('input', $inputattributes);
 121  
 122          if (empty($preferences->email_email) && !empty($preferences->userdefaultemail)) {
 123              $string .= get_string('ifemailleftempty', 'message_email', $preferences->userdefaultemail);
 124          }
 125  
 126          if (!empty($preferences->email_email) && !validate_email($preferences->email_email)) {
 127              $string .= $OUTPUT->container(get_string('invalidemail'), 'error');
 128          }
 129  
 130          return $string;
 131      }
 132  
 133      /**
 134       * Parses the submitted form data and saves it into preferences array.
 135       *
 136       * @param stdClass $form preferences form class
 137       * @param array $preferences preferences array
 138       */
 139      function process_form($form, &$preferences){
 140          if (isset($form->email_email)) {
 141              $preferences['message_processor_email_email'] = $form->email_email;
 142          }
 143      }
 144  
 145      /**
 146       * Returns the default message output settings for this output
 147       *
 148       * @return int The default settings
 149       */
 150      public function get_default_messaging_settings() {
 151          return MESSAGE_PERMITTED + MESSAGE_DEFAULT_LOGGEDIN + MESSAGE_DEFAULT_LOGGEDOFF;
 152      }
 153  
 154      /**
 155       * Loads the config data from database to put on the form during initial form display
 156       *
 157       * @param array $preferences preferences array
 158       * @param int $userid the user id
 159       */
 160      function load_data(&$preferences, $userid){
 161          $preferences->email_email = get_user_preferences( 'message_processor_email_email', '', $userid);
 162      }
 163  
 164      /**
 165       * Returns true as message can be sent to internal support user.
 166       *
 167       * @return bool
 168       */
 169      public function can_send_to_any_users() {
 170          return true;
 171      }
 172  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1