Skip to content

Commit

Permalink
MDL-64017 message_email: task to send messages as a digest
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Apr 18, 2019
1 parent 2902ca9 commit 168d782
Show file tree
Hide file tree
Showing 9 changed files with 666 additions and 9 deletions.
53 changes: 44 additions & 9 deletions lib/tests/messagelib_test.php
Expand Up @@ -836,13 +836,30 @@ public function test_message_send_to_conversation_group() {
$this->preventResetByRollback();
$this->resetAfterTest();

$course = $this->getDataGenerator()->create_course();

// Create some users and a conversation between them.
$user1 = $this->getDataGenerator()->create_user(array('maildisplay' => 1));
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
set_config('allowedemaildomains', 'example.com');
$conversation = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
[$user1->id, $user2->id, $user3->id], 'Group project discussion');

// Create a group in the course.
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
groups_add_member($group1->id, $user1->id);
groups_add_member($group1->id, $user2->id);
groups_add_member($group1->id, $user3->id);

$conversation = \core_message\api::create_conversation(
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
[$user1->id, $user2->id, $user3->id],
'Group project discussion',
\core_message\api::MESSAGE_CONVERSATION_ENABLED,
'core_group',
'groups',
$group1->id,
context_course::instance($course->id)->id
);

// Generate the message.
$message = new \core\message\message();
Expand All @@ -868,8 +885,11 @@ public function test_message_send_to_conversation_group() {
set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user3);

// Now, send a message and verify the email processor are hit.
$sink = $this->redirectEmails();
$messageid = message_send($message);

$sink = $this->redirectEmails();
$task = new \message_email\task\send_email_task();
$task->execute();
$emails = $sink->get_messages();
$this->assertCount(2, $emails);

Expand Down Expand Up @@ -902,14 +922,29 @@ public function test_send_message_to_conversation_group_with_buffering() {
$this->preventResetByRollback();
$this->resetAfterTest();

$course = $this->getDataGenerator()->create_course();

$user1 = $this->getDataGenerator()->create_user(array('maildisplay' => 1));
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
set_config('allowedemaildomains', 'example.com');

// Create a conversation.
$conversation = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
[$user1->id, $user2->id, $user3->id], 'Group project discussion');
// Create a group in the course.
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
groups_add_member($group1->id, $user1->id);
groups_add_member($group1->id, $user2->id);
groups_add_member($group1->id, $user3->id);

$conversation = \core_message\api::create_conversation(
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
[$user1->id, $user2->id, $user3->id],
'Group project discussion',
\core_message\api::MESSAGE_CONVERSATION_ENABLED,
'core_group',
'groups',
$group1->id,
context_course::instance($course->id)->id
);

// Test basic email redirection.
$this->assertFileExists("$CFG->dirroot/message/output/email/version.php");
Expand Down Expand Up @@ -939,18 +974,18 @@ public function test_send_message_to_conversation_group_with_buffering() {

$transaction = $DB->start_delegated_transaction();
$sink = $this->redirectEmails();
$messageid = message_send($message);
message_send($message);
$emails = $sink->get_messages();
$this->assertCount(0, $emails);
$savedmessage = $DB->get_record('messages', array('id' => $messageid), '*', MUST_EXIST);
$sink->clear();
$this->assertFalse($DB->record_exists('message_user_actions', array()));
$DB->delete_records('messages', array());
$events = $eventsink->get_events();
$this->assertCount(0, $events);
$eventsink->clear();
$transaction->allow_commit();
$events = $eventsink->get_events();
$task = new \message_email\task\send_email_task();
$task->execute();
$emails = $sink->get_messages();
$this->assertCount(2, $emails);
$this->assertCount(1, $events);
Expand Down
46 changes: 46 additions & 0 deletions message/output/email/classes/output/email/renderer.php
@@ -0,0 +1,46 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Email digest as html renderer.
*
* @package message_email
* @copyright 2019 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace message_email\output\email;

defined('MOODLE_INTERNAL') || die();

/**
* Email digest as html renderer.
*
* @package message_email
* @copyright 2019 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class renderer extends \message_email\output\renderer {

/**
* The template name for this renderer.
*
* @return string
*/
public function get_template_name() {
return 'email_digest_html';
}
}
46 changes: 46 additions & 0 deletions message/output/email/classes/output/email/renderer_textemail.php
@@ -0,0 +1,46 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Email digest as text renderer.
*
* @package message_email
* @copyright 2019 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace message_email\output\email;

defined('MOODLE_INTERNAL') || die();

/**
* Email digest as text renderer.
*
* @package message_email
* @copyright 2019 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class renderer_textemail extends \message_email\output\renderer {

/**
* The template name for this renderer.
*
* @return string
*/
public function get_template_name() {
return 'email_digest_text';
}
}
136 changes: 136 additions & 0 deletions message/output/email/classes/output/email_digest.php
@@ -0,0 +1,136 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Email digest renderable.
*
* @package message_email
* @copyright 2019 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace message_email\output;

defined('MOODLE_INTERNAL') || die();

/**
* Email digest renderable.
*
* @copyright 2019 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class email_digest implements \renderable, \templatable {

/**
* @var array The conversations
*/
protected $conversations = array();

/**
* @var array The messages
*/
protected $messages = array();

/**
* @var \stdClass The user we want to send the digest email to
*/
protected $userto;

/**
* The email_digest constructor.
*
* @param \stdClass $userto
*/
public function __construct(\stdClass $userto) {
$this->userto = $userto;
}

/**
* Adds another conversation to this digest.
*
* @param \stdClass $conversation The conversation from the 'message_conversations' table.
*/
public function add_conversation(\stdClass $conversation) {
$this->conversations[$conversation->id] = $conversation;
}

/**
* Adds another message to this digest, using the conversation id it belongs to as a key.
*
* @param \stdClass $message The message from the 'messages' table.
*/
public function add_message(\stdClass $message) {
$this->messages[$message->conversationid][] = $message;
}

/**
* Export this data so it can be used as the context for a mustache template.
*
* @param \renderer_base $renderer The render to be used for formatting the email
* @return \stdClass The data ready for use in a mustache template
*/
public function export_for_template(\renderer_base $renderer) {
// Prepare the data we are going to send to the template.
$data = new \stdClass();
$data->conversations = [];

// Don't do anything if there are no messages.
foreach ($this->conversations as $conversation) {
$messages = $this->messages[$conversation->id] ?? [];

if (empty($messages)) {
continue;
}

$viewallmessageslink = new \moodle_url('/message/index.php', ['convid' => $conversation->id]);

$conversationformatted = new \stdClass();
$conversationformatted->groupname = $conversation->name;
$conversationformatted->coursename = $conversation->coursename;
$conversationformatted->numberofunreadmessages = count($messages);
$conversationformatted->messages = [];
$conversationformatted->viewallmessageslink = \html_writer::link($viewallmessageslink,
get_string('emaildigestviewallmessages', 'message_email'));

// We only display the last 3 messages.
$messages = array_slice($messages, -3, 3, true);
foreach ($messages as $message) {
$user = new \stdClass();
username_load_fields_from_object($user, $message);
$user->id = $message->useridfrom;
$messageformatted = new \stdClass();
$messageformatted->userfullname = fullname($user);
$messageformatted->message = message_format_message_text($message);

// Check if the message was sent today.
$istoday = userdate($message->timecreated, 'Y-m-d') == userdate(time(), 'Y-m-d');
if ($istoday) {
$timesent = userdate($message->timecreated, get_string('strftimetime24', 'langconfig'));
} else {
$timesent = userdate($message->timecreated, get_string('strftimedatefullshort', 'langconfig'));
}

$messageformatted->timesent = $timesent;

$conversationformatted->messages[] = $messageformatted;
}

$data->conversations[] = $conversationformatted;
}

return $data;
}
}
50 changes: 50 additions & 0 deletions message/output/email/classes/output/renderer.php
@@ -0,0 +1,50 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Contains renderer class.
*
* @package message_email
* @copyright 2019 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace message_email\output;

defined('MOODLE_INTERNAL') || die();

use plugin_renderer_base;

/**
* Renderer class.
*
* @package message_email
* @copyright 2019 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class renderer extends plugin_renderer_base {

/**
* Formats the email used to send the certificate by the email_certificate_task.
*
* @param email_digest $emaildigest The certificate to email
* @return string
*/
public function render_email_digest(email_digest $emaildigest) {
$data = $emaildigest->export_for_template($this);
return $this->render_from_template('message_email/' . $this->get_template_name(), $data);
}
}

0 comments on commit 168d782

Please sign in to comment.