Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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->hidepicture = $conversation->hidepicture;
 106              $group->courseid = $conversation->courseid;
 107              $grouppictureurl = $renderer->image_url('g/g1')->out(false); // Default image.
 108              if ($url = get_group_picture_url($group, $group->courseid, false, true)) {
 109                  $grouppictureurl = $url->out(false);
 110              }
 111  
 112              $coursecontext = \context_course::instance($conversation->courseid);
 113  
 114              $conversationformatted = new \stdClass();
 115              $conversationformatted->groupname = format_string($conversation->name, true, ['context' => $coursecontext]);
 116              $conversationformatted->grouppictureurl = $grouppictureurl;
 117              $conversationformatted->coursename = format_string($conversation->coursename, true, ['context' => $coursecontext]);
 118              $conversationformatted->numberofunreadmessages = count($messages);
 119              $conversationformatted->messages = [];
 120              $conversationformatted->viewallmessageslink = \html_writer::link($viewallmessageslink,
 121                  get_string('emaildigestviewallmessages', 'message_email'));
 122  
 123              // We only display the last 3 messages.
 124              $messages = array_slice($messages, -3, 3, true);
 125              foreach ($messages as $message) {
 126                  $user = new \stdClass();
 127                  username_load_fields_from_object($user, $message);
 128                  $user->picture = $message->picture;
 129                  $user->imagealt = $message->imagealt;
 130                  $user->email = $message->email;
 131                  $user->id = $message->useridfrom;
 132  
 133                  $userpicture = new \user_picture($user);
 134                  $userpicture->includetoken = true;
 135                  $userpictureurl = $userpicture->get_url($PAGE)->out(false);
 136  
 137                  $messageformatted = new \stdClass();
 138                  $messageformatted->userpictureurl = $userpictureurl;
 139                  $messageformatted->userfullname = fullname($user);
 140                  $messageformatted->message = message_format_message_text($message);
 141  
 142                  // Check if the message was sent today.
 143                  $istoday = userdate($message->timecreated, 'Y-m-d') == userdate(time(), 'Y-m-d');
 144                  if ($istoday) {
 145                      $timesent = userdate($message->timecreated, get_string('strftimetime24', 'langconfig'));
 146                  } else {
 147                      $timesent = userdate($message->timecreated, get_string('strftimedatefullshort', 'langconfig'));
 148                  }
 149  
 150                  $messageformatted->timesent = $timesent;
 151  
 152                  $conversationformatted->messages[] = $messageformatted;
 153              }
 154  
 155              $data->conversations[] = $conversationformatted;
 156          }
 157  
 158          return $data;
 159      }
 160  }