Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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   * Email digest renderable.
  19   *
  20   * @package    message_email
  21   * @copyright  2019 Mark Nelson <markn@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace message_email\output;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Email digest renderable.
  31   *
  32   * @copyright  2019 Mark Nelson <markn@moodle.com>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class email_digest implements \renderable, \templatable {
  36  
  37      /**
  38       * @var array The conversations
  39       */
  40      protected $conversations = array();
  41  
  42      /**
  43       * @var array The messages
  44       */
  45      protected $messages = array();
  46  
  47      /**
  48       * @var \stdClass The user we want to send the digest email to
  49       */
  50      protected $userto;
  51  
  52      /**
  53       * The email_digest constructor.
  54       *
  55       * @param \stdClass $userto
  56       */
  57      public function __construct(\stdClass $userto) {
  58          $this->userto = $userto;
  59      }
  60  
  61      /**
  62       * Adds another conversation to this digest.
  63       *
  64       * @param \stdClass $conversation The conversation from the 'message_conversations' table.
  65       */
  66      public function add_conversation(\stdClass $conversation) {
  67          $this->conversations[$conversation->id] = $conversation;
  68      }
  69  
  70      /**
  71       * Adds another message to this digest, using the conversation id it belongs to as a key.
  72       *
  73       * @param \stdClass $message The message from the 'messages' table.
  74       */
  75      public function add_message(\stdClass $message) {
  76          $this->messages[$message->conversationid][] = $message;
  77      }
  78  
  79      /**
  80       * Export this data so it can be used as the context for a mustache template.
  81       *
  82       * @param \renderer_base $renderer The render to be used for formatting the email
  83       * @return \stdClass The data ready for use in a mustache template
  84       */
  85      public function export_for_template(\renderer_base $renderer) {
  86          global $PAGE;
  87  
  88          // Prepare the data we are going to send to the template.
  89          $data = new \stdClass();
  90          $data->conversations = [];
  91  
  92          // Don't do anything if there are no messages.
  93          foreach ($this->conversations as $conversation) {
  94              $messages = $this->messages[$conversation->id] ?? [];
  95  
  96              if (empty($messages)) {
  97                  continue;
  98              }
  99  
 100              $viewallmessageslink = new \moodle_url('/message/index.php', ['convid' => $conversation->id]);
 101  
 102              $group = new \stdClass();
 103              $group->id = $conversation->groupid;
 104              $group->picture = $conversation->picture;
 105              $group->courseid = $conversation->courseid;
 106              $grouppictureurl = $renderer->image_url('g/g1')->out(false); // Default image.
 107              if ($url = get_group_picture_url($group, $group->courseid, false, true)) {
 108                  $grouppictureurl = $url->out(false);
 109              }
 110  
 111              $coursecontext = \context_course::instance($conversation->courseid);
 112  
 113              $conversationformatted = new \stdClass();
 114              $conversationformatted->groupname = format_string($conversation->name, true, ['context' => $coursecontext]);
 115              $conversationformatted->grouppictureurl = $grouppictureurl;
 116              $conversationformatted->coursename = format_string($conversation->coursename, true, ['context' => $coursecontext]);
 117              $conversationformatted->numberofunreadmessages = count($messages);
 118              $conversationformatted->messages = [];
 119              $conversationformatted->viewallmessageslink = \html_writer::link($viewallmessageslink,
 120                  get_string('emaildigestviewallmessages', 'message_email'));
 121  
 122              // We only display the last 3 messages.
 123              $messages = array_slice($messages, -3, 3, true);
 124              foreach ($messages as $message) {
 125                  $user = new \stdClass();
 126                  username_load_fields_from_object($user, $message);
 127                  $user->picture = $message->picture;
 128                  $user->imagealt = $message->imagealt;
 129                  $user->email = $message->email;
 130                  $user->id = $message->useridfrom;
 131  
 132                  $userpicture = new \user_picture($user);
 133                  $userpicture->includetoken = true;
 134                  $userpictureurl = $userpicture->get_url($PAGE)->out(false);
 135  
 136                  $messageformatted = new \stdClass();
 137                  $messageformatted->userpictureurl = $userpictureurl;
 138                  $messageformatted->userfullname = fullname($user);
 139                  $messageformatted->message = message_format_message_text($message);
 140  
 141                  // Check if the message was sent today.
 142                  $istoday = userdate($message->timecreated, 'Y-m-d') == userdate(time(), 'Y-m-d');
 143                  if ($istoday) {
 144                      $timesent = userdate($message->timecreated, get_string('strftimetime24', 'langconfig'));
 145                  } else {
 146                      $timesent = userdate($message->timecreated, get_string('strftimedatefullshort', 'langconfig'));
 147                  }
 148  
 149                  $messageformatted->timesent = $timesent;
 150  
 151                  $conversationformatted->messages[] = $messageformatted;
 152              }
 153  
 154              $data->conversations[] = $conversationformatted;
 155          }
 156  
 157          return $data;
 158      }
 159  }