first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,51 @@
<?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 an observer class containing methods for handling events.
*
* @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;
defined('MOODLE_INTERNAL') || die();
/**
* Observer class containing methods for handling events.
*
* @package message_email
* @copyright 2019 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class event_observers {
/**
* Message viewed event handler.
*
* @param \core\event\message_viewed $event The message viewed event.
*/
public static function message_viewed(\core\event\message_viewed $event) {
global $DB;
$userid = $event->userid;
$messageid = $event->other['messageid'];
$DB->delete_records('message_email_messages', ['useridto' => $userid, 'messageid' => $messageid]);
}
}
@@ -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';
}
}
@@ -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';
}
}
@@ -0,0 +1,159 @@
<?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) {
global $PAGE;
// 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]);
$group = new \stdClass();
$group->id = $conversation->groupid;
$group->picture = $conversation->picture;
$group->courseid = $conversation->courseid;
$grouppictureurl = $renderer->image_url('g/g1')->out(false); // Default image.
if ($url = get_group_picture_url($group, $group->courseid, false, true)) {
$grouppictureurl = $url->out(false);
}
$coursecontext = \context_course::instance($conversation->courseid);
$conversationformatted = new \stdClass();
$conversationformatted->groupname = format_string($conversation->name, true, ['context' => $coursecontext]);
$conversationformatted->grouppictureurl = $grouppictureurl;
$conversationformatted->coursename = format_string($conversation->coursename, true, ['context' => $coursecontext]);
$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->picture = $message->picture;
$user->imagealt = $message->imagealt;
$user->email = $message->email;
$user->id = $message->useridfrom;
$userpicture = new \user_picture($user);
$userpicture->includetoken = true;
$userpictureurl = $userpicture->get_url($PAGE)->out(false);
$messageformatted = new \stdClass();
$messageformatted->userpictureurl = $userpictureurl;
$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;
}
}
@@ -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,0 +1,147 @@
<?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/>.
/**
* Privacy class for requesting user data.
*
* @package message_email
* @copyright 2018 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace message_email\privacy;
defined('MOODLE_INTERNAL') || die();
use \core_privacy\local\metadata\collection;
use \core_privacy\local\request\contextlist;
use \core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\userlist;
use core_privacy\local\request\writer;
use \core_privacy\local\request\approved_userlist;
/**
* Privacy class for requesting user data.
*
* @package message_email
* @copyright 2018 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\core_userlist_provider,
\core_privacy\local\request\user_preference_provider,
\core_privacy\local\request\plugin\provider {
/**
* Returns meta data about this system.
*
* @param collection $collection The initialised collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $collection): collection {
$messageemailmessages = [
'useridto' => 'privacy:metadata:message_email_messages:useridto',
'conversationid' => 'privacy:metadata:message_email_messages:conversationid',
'messageid' => 'privacy:metadata:message_email_messages:messageid',
];
// Note - this data gets deleted once the scheduled task runs.
$collection->add_database_table('message_email_messages',
$messageemailmessages, 'privacy:metadata:message_email_messages');
$collection->link_external_location('smtp', [
'recipient' => 'privacy:metadata:recipient',
'userfrom' => 'privacy:metadata:userfrom',
'subject' => 'privacy:metadata:subject',
'fullmessage' => 'privacy:metadata:fullmessage',
'fullmessagehtml' => 'privacy:metadata:fullmessagehtml',
'attachment' => 'privacy:metadata:attachment',
'attachname' => 'privacy:metadata:attachname',
'replyto' => 'privacy:metadata:replyto',
'replytoname' => 'privacy:metadata:replytoname'
], 'privacy:metadata:externalpurpose');
return $collection;
}
/**
* Get the list of contexts that contain user information for the specified user.
*
* @param int $userid The user to search.
* @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
*/
public static function get_contexts_for_userid(int $userid): contextlist {
$contextlist = new contextlist();
return $contextlist;
}
/**
* Get the list of users who have data within a context.
*
* @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
*/
public static function get_users_in_context(userlist $userlist) {
}
/**
* Export all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts to export information for.
*/
public static function export_user_data(approved_contextlist $contextlist) {
}
/**
* Delete all use data which matches the specified deletion_criteria.
*
* @param context $context A user context.
*/
public static function delete_data_for_all_users_in_context(\context $context) {
}
/**
* Delete multiple users within a single context.
*
* @param approved_userlist $userlist The approved context and user information to delete information for.
*/
public static function delete_data_for_users(approved_userlist $userlist) {
}
/**
* Delete all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
}
/**
* Export all user preferences for the plugin
*
* @param int $userid
*/
public static function export_user_preferences(int $userid) {
$preference = get_user_preferences('message_processor_email_email', null, $userid);
if (!empty($preference)) {
writer::export_user_preference(
'message_email',
'email',
$preference,
get_string('privacy:preference:email', 'message_email')
);
}
}
}
@@ -0,0 +1,178 @@
<?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 the class responsible for sending emails as a digest.
*
* @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\task;
use core\task\scheduled_task;
use moodle_recordset;
defined('MOODLE_INTERNAL') || die();
/**
* Class responsible for sending emails as a digest.
*
* @package message_email
* @copyright 2019 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class send_email_task extends scheduled_task {
/**
* @var int $maxid This is the maximum id of the message in 'message_email_messages'.
* We use this so we know what records to process, as more records may be added
* while this task runs.
*/
private $maxid;
/**
* Get a descriptive name for this task (shown to admins).
*
* @return string
*/
public function get_name() {
return get_string('tasksendemail', 'message_email');
}
/**
* Send out emails.
*/
public function execute() {
global $DB, $PAGE, $SITE;
// Get the maximum id we are going to use.
// We use this as records may be added to the table while this task runs.
$this->maxid = $DB->get_field_sql("SELECT MAX(id) FROM {message_email_messages}");
// We are going to send these emails from 'noreplyaddress'.
$noreplyuser = \core_user::get_noreply_user();
// The renderers used for sending emails.
$htmlrenderer = $PAGE->get_renderer('message_email', 'email', 'htmlemail');
$textrenderer = $PAGE->get_renderer('message_email', 'email', 'textemail');
// Keep track of which emails failed to send.
$users = $this->get_unique_users();
foreach ($users as $user) {
\core\cron::setup_user($user);
$hascontent = false;
$renderable = new \message_email\output\email_digest($user);
$conversations = $this->get_conversations_for_user($user->id);
foreach ($conversations as $conversation) {
$renderable->add_conversation($conversation);
$messages = $this->get_users_messages_for_conversation($conversation->id, $user->id);
if ($messages->valid()) {
$hascontent = true;
foreach ($messages as $message) {
$renderable->add_message($message);
}
}
$messages->close();
}
$conversations->close();
if ($hascontent) {
$subject = get_string('messagedigestemailsubject', 'message_email', format_string($SITE->fullname));
$message = $textrenderer->render($renderable);
$messagehtml = $htmlrenderer->render($renderable);
if (email_to_user($user, $noreplyuser, $subject, $message, $messagehtml)) {
$DB->delete_records_select('message_email_messages', 'useridto = ? AND id <= ?', [$user->id, $this->maxid]);
}
}
}
\core\cron::setup_user();
$users->close();
}
/**
* Returns an array of users in the given conversation.
*
* @return moodle_recordset A moodle_recordset instance.
*/
private function get_unique_users(): moodle_recordset {
global $DB;
$subsql = 'SELECT DISTINCT(useridto) as id
FROM {message_email_messages}
WHERE id <= ?';
$sql = "SELECT *
FROM {user} u
WHERE id IN ($subsql)";
return $DB->get_recordset_sql($sql, [$this->maxid]);
}
/**
* Returns an array of unique conversations that require processing.
*
* @param int $userid The ID of the user we are sending a digest to.
* @return moodle_recordset A moodle_recordset instance.
*/
private function get_conversations_for_user(int $userid): moodle_recordset {
global $DB;
// We shouldn't be joining directly on the group table as group
// conversations may (in the future) be something created that
// isn't related to an actual group in a course. However, for
// now this will have to do before 3.7 code freeze.
// See related MDL-63814.
$sql = "SELECT DISTINCT mc.id, mc.name, c.id as courseid, c.fullname as coursename, g.id as groupid,
g.picture
FROM {message_conversations} mc
JOIN {groups} g
ON mc.itemid = g.id
JOIN {course} c
ON g.courseid = c.id
JOIN {message_email_messages} mem
ON mem.conversationid = mc.id
WHERE mem.useridto = ?
AND mem.id <= ?";
return $DB->get_recordset_sql($sql, [$userid, $this->maxid]);
}
/**
* Returns the messages to send to a user for a given conversation
*
* @param int $conversationid
* @param int $userid
* @return moodle_recordset A moodle_recordset instance.
*/
protected function get_users_messages_for_conversation(int $conversationid, int $userid): moodle_recordset {
global $DB;
$userfieldsapi = \core_user\fields::for_userpic();
$usernamefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$sql = "SELECT $usernamefields, m.*
FROM {messages} m
JOIN {user} u
ON u.id = m.useridfrom
JOIN {message_email_messages} mem
ON mem.messageid = m.id
WHERE mem.useridto = ?
AND mem.conversationid = ?
AND mem.id <= ?";
return $DB->get_recordset_sql($sql, [$userid, $conversationid, $this->maxid]);
}
}
+32
View File
@@ -0,0 +1,32 @@
<?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/>.
/**
* This file defines what events we wish to observe and the method responsible for handling the event.
*
* @package message_email
* @copyright 2019 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$observers = [
[
'eventname' => '\core\event\message_viewed',
'callback' => '\message_email\event_observers::message_viewed',
],
];
+36
View File
@@ -0,0 +1,36 @@
<?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/>.
/**
* Installation code for the email message processor
*
* @package message_email
* @copyright 2009 Moodle Pty Ltd (http://moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Install the email message processor
*/
function xmldb_message_email_install() {
global $DB;
$result = true;
$provider = new stdClass();
$provider->name = 'email';
$DB->insert_record('message_processors', $provider);
return $result;
}
+22
View File
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="message/output/email/db" VERSION="20190325" COMMENT="XMLDB file for Moodle message/output/email"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="message_email_messages" COMMENT="Keeps track of what emails to send in an email digest">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="useridto" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="conversationid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="messageid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="useridto" TYPE="foreign" FIELDS="useridto" REFTABLE="user" REFFIELDS="id"/>
<KEY NAME="conversationid" TYPE="foreign" FIELDS="conversationid" REFTABLE="message_conversations" REFFIELDS="id"/>
<KEY NAME="messageid" TYPE="foreign" FIELDS="messageid" REFTABLE="messages" REFFIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
+38
View File
@@ -0,0 +1,38 @@
<?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/>.
/**
* This file defines tasks performed by the plugin.
*
* @package message_email
* @copyright 2019 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
// List of tasks.
$tasks = array(
array(
'classname' => 'message_email\task\send_email_task',
'blocking' => 0,
'minute' => 0,
'hour' => 22,
'day' => '*',
'dayofweek' => '*',
'month' => '*'
)
);
+44
View File
@@ -0,0 +1,44 @@
<?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/>.
/**
* Upgrade code for email message processor
*
* @package message_email
* @copyright 2008 Luis Rodrigues
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
/**
* Upgrade code for the email message processor
*
* @param int $oldversion The version that we are upgrading from
*/
function xmldb_message_email_upgrade($oldversion) {
// Automatically generated Moodle v4.1.0 release upgrade line.
// Put any upgrade step following this.
// Automatically generated Moodle v4.2.0 release upgrade line.
// Put any upgrade step following this.
// Automatically generated Moodle v4.3.0 release upgrade line.
// Put any upgrade step following this.
// Automatically generated Moodle v4.4.0 release upgrade line.
// Put any upgrade step following this.
return true;
}
@@ -0,0 +1,47 @@
<?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/>.
/**
* Strings for component 'message_email', language 'en'
*
* @package message_email
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['email'] = 'Send email notifications to';
$string['emaildigestunreadmessages'] = 'Unread messages';
$string['emaildigestviewallmessages'] = 'View all messages';
$string['emailonlyfromnoreplyaddress'] = 'Always send email from the no-reply address?';
$string['ifemailleftempty'] = 'Leave empty to send notifications to {$a}';
$string['messagedigestemailsubject'] = '{$a}: Messages digest';
$string['pluginname'] = 'Email';
$string['privacy:metadata:attachment'] = 'A file on the filesystem.';
$string['privacy:metadata:attachname'] = 'The name of the attached file (extension indicates MIME).';
$string['privacy:metadata:externalpurpose'] = 'This information is sent to an external SMTP server to be ultimately delivered as an email to the recipient.';
$string['privacy:metadata:fullmessage'] = 'The full message in a given format.';
$string['privacy:metadata:fullmessagehtml'] = 'The full version of the message.';
$string['privacy:metadata:message_email_messages'] = 'The list of users enrolled via an LTI provider';
$string['privacy:metadata:message_email_messages:conversationid'] = 'The ID of the conversation being sent to';
$string['privacy:metadata:message_email_messages:messageid'] = 'The ID of the message being sent';
$string['privacy:metadata:message_email_messages:useridto'] = 'The ID of the user receiving the message';
$string['privacy:metadata:recipient'] = 'The recipient of the message.';
$string['privacy:metadata:replyto'] = 'The email address to reply to.';
$string['privacy:metadata:replytoname'] = 'Name of reply to recipient.';
$string['privacy:metadata:subject'] = 'The subject line of the message.';
$string['privacy:metadata:userfrom'] = 'The user sending the message.';
$string['privacy:preference:email'] = 'Preferred email notification address';
$string['tasksendemail'] = 'Messages digest mailings';
@@ -0,0 +1,228 @@
<?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 the definiton of the email message processors (sends messages to users via email)
*
* @package message_email
* @copyright 2008 Luis Rodrigues and Martin Dougiamas
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot.'/message/output/lib.php');
/**
* The email message processor
*
* @package message_email
* @copyright 2008 Luis Rodrigues and Martin Dougiamas
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class message_output_email extends message_output {
/**
* Processes the message (sends by email).
* @param object $eventdata the event data submitted by the message sender plus $eventdata->savedmessageid
*/
function send_message($eventdata) {
global $CFG, $DB;
// skip any messaging suspended and deleted users
if ($eventdata->userto->auth === 'nologin' or $eventdata->userto->suspended or $eventdata->userto->deleted) {
return true;
}
//the user the email is going to
$recipient = null;
//check if the recipient has a different email address specified in their messaging preferences Vs their user profile
$emailmessagingpreference = get_user_preferences('message_processor_email_email', null, $eventdata->userto);
$emailmessagingpreference = clean_param($emailmessagingpreference, PARAM_EMAIL);
// If the recipient has set an email address in their preferences use that instead of the one in their profile
// but only if overriding the notification email address is allowed
if (!empty($emailmessagingpreference) && !empty($CFG->messagingallowemailoverride)) {
//clone to avoid altering the actual user object
$recipient = clone($eventdata->userto);
$recipient->email = $emailmessagingpreference;
} else {
$recipient = $eventdata->userto;
}
// Check if we have attachments to send.
$attachment = '';
$attachname = '';
if (!empty($CFG->allowattachments) && !empty($eventdata->attachment)) {
if (empty($eventdata->attachname)) {
// Attachment needs a file name.
debugging('Attachments should have a file name. No attachments have been sent.', DEBUG_DEVELOPER);
} else if (!($eventdata->attachment instanceof stored_file)) {
// Attachment should be of a type stored_file.
debugging('Attachments should be of type stored_file. No attachments have been sent.', DEBUG_DEVELOPER);
} else {
// Copy attachment file to a temporary directory and get the file path.
$attachment = $eventdata->attachment->copy_content_to_temp();
// Get attachment file name.
$attachname = clean_filename($eventdata->attachname);
}
}
// Configure mail replies - this is used for incoming mail replies.
$replyto = '';
$replytoname = '';
if (isset($eventdata->replyto)) {
$replyto = $eventdata->replyto;
if (isset($eventdata->replytoname)) {
$replytoname = $eventdata->replytoname;
}
}
// We email messages from private conversations straight away, but for group we add them to a table to be sent later.
$emailuser = true;
if (!$eventdata->notification) {
if ($eventdata->conversationtype == \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP) {
$emailuser = false;
}
}
if ($emailuser) {
$result = email_to_user($recipient, $eventdata->userfrom, $eventdata->subject, $eventdata->fullmessage,
$eventdata->fullmessagehtml, $attachment, $attachname, true, $replyto, $replytoname);
} else {
$messagetosend = new stdClass();
$messagetosend->useridfrom = $eventdata->userfrom->id;
$messagetosend->useridto = $recipient->id;
$messagetosend->conversationid = $eventdata->convid;
$messagetosend->messageid = $eventdata->savedmessageid;
$result = $DB->insert_record('message_email_messages', $messagetosend, false);
}
// Remove an attachment file if any.
if (!empty($attachment) && file_exists($attachment)) {
unlink($attachment);
}
return $result;
}
/**
* Creates necessary fields in the messaging config form.
*
* @param array $preferences An array of user preferences
*/
function config_form($preferences){
global $USER, $OUTPUT, $CFG;
$string = '';
$choices = array();
$choices['0'] = get_string('textformat');
$choices['1'] = get_string('htmlformat');
$current = $preferences->mailformat;
$string .= $OUTPUT->container(html_writer::label(get_string('emailformat'), 'mailformat'));
$string .= $OUTPUT->container(html_writer::select($choices, 'mailformat', $current, false, array('id' => 'mailformat')));
$string .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'userid', 'value' => $USER->id));
if (!empty($CFG->allowusermailcharset)) {
$choices = array();
$charsets = get_list_of_charsets();
if (!empty($CFG->sitemailcharset)) {
$choices['0'] = get_string('site').' ('.$CFG->sitemailcharset.')';
} else {
$choices['0'] = get_string('site').' (UTF-8)';
}
$choices = array_merge($choices, $charsets);
$current = $preferences->mailcharset;
$string .= $OUTPUT->container(html_writer::label(get_string('emailcharset'), 'mailcharset'));
$string .= $OUTPUT->container(
html_writer::select($choices, 'preference_mailcharset', $current, false, array('id' => 'mailcharset'))
);
}
if (!empty($CFG->messagingallowemailoverride)) {
$inputattributes = array('size' => '30', 'name' => 'email_email', 'value' => $preferences->email_email,
'id' => 'email_email');
$string .= html_writer::label(get_string('email', 'message_email'), 'email_email');
$string .= $OUTPUT->container(html_writer::empty_tag('input', $inputattributes));
if (empty($preferences->email_email) && !empty($preferences->userdefaultemail)) {
$string .= $OUTPUT->container(get_string('ifemailleftempty', 'message_email', $preferences->userdefaultemail));
}
if (!empty($preferences->email_email) && !validate_email($preferences->email_email)) {
$string .= $OUTPUT->container(get_string('invalidemail'), 'error');
}
$string .= '<br/>';
}
return $string;
}
/**
* Parses the submitted form data and saves it into preferences array.
*
* @param stdClass $form preferences form class
* @param array $preferences preferences array
*/
function process_form($form, &$preferences){
global $CFG;
if (isset($form->email_email)) {
$preferences['message_processor_email_email'] = clean_param($form->email_email, PARAM_EMAIL);
}
if (isset($form->preference_mailcharset)) {
$preferences['mailcharset'] = $form->preference_mailcharset;
if (!array_key_exists($preferences['mailcharset'], get_list_of_charsets())) {
$preferences['mailcharset'] = '0';
}
}
if (isset($form->mailformat) && isset($form->userid)) {
require_once($CFG->dirroot.'/user/lib.php');
$user = core_user::get_user($form->userid, '*', MUST_EXIST);
$user->mailformat = clean_param($form->mailformat, PARAM_INT);
user_update_user($user, false, false);
}
}
/**
* Returns the default message output settings for this output
*
* @return int The default settings
*/
public function get_default_messaging_settings() {
return MESSAGE_PERMITTED + MESSAGE_DEFAULT_ENABLED;
}
/**
* Loads the config data from database to put on the form during initial form display
*
* @param array $preferences preferences array
* @param int $userid the user id
*/
function load_data(&$preferences, $userid){
$preferences->email_email = get_user_preferences( 'message_processor_email_email', '', $userid);
}
/**
* Returns true as message can be sent to internal support user.
*
* @return bool
*/
public function can_send_to_any_users() {
return true;
}
}
@@ -0,0 +1,156 @@
{{!
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/>.
}}
{{!
@template message_email/email_digest_html
Template which defines a forum post for sending in a single-post HTML email.
Classes required for JS:
* none
Data attributes required for JS:
* none
Example context (json):
{
"conversations": [
{
"groupname": "Blue Students",
"grouppictureurl": "http://example.com/image.jpg",
"coursename": "Math 101",
"numberofunreadmessages": "2",
"messages": [
{
"userfullname": "Chris Cross",
"userpictureurl": "http://example.com/image.jpg",
"message": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla neque nunc, bibendum ac vestibulum sit amet, scelerisque luctus sem. Maecenas ultricies hendrerit augue, ac venenatis odio volutpat nec",
"timesent": "10:12"
},
{
"userfullname": "Irene Ipsum",
"userpictureurl": "http://example.com/image.jpg",
"message": "Etiam a tristique risus. Pellentesque id tellus eget elit dictum varius id sed sapien",
"timesent": "10:14"
}
],
"viewallmessageslink": "http://example.com"
}
]
}
}}
<head>
<style>
.table {
color: #373a3c;
border: 1px solid #dee2e6;
border-collapse: collapse;
font-size: 14px;
margin-bottom: 20px;
width: 100%;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
.table th {
font-weight: normal;
background-color: #F2F2F2;
border-bottom: 1px solid #dee2e6;
padding: 10px;
vertical-align: top;
}
.text-left {
text-align: left;
}
.text-right {
text-align: right;
}
.float-right {
float: right;
}
.table td {
padding: 10px;
vertical-align: top;
}
.badge {
background-color: #1177d1;
color: #ffffff;
padding: 2px;
border-radius: 50%;
width: 15px;
height: 15px;
display: inline-block;
text-align: center;
font-size: 13px;
line-height: 15px;
}
.linkcolor a {
color: #1177d1;
}
.round {
border-radius: 50%;
}
.message p {
margin-top: 0;
padding-right: 40px;
}
.nowrap {
white-space: nowrap;
}
.gray-light {
color: #868e96;
}
.text-small {
font-size: 13px;
}
</style>
</head>
{{#conversations}}
<table class="table">
<thead>
<tr>
<th width="40px">
<img src="{{{ grouppictureurl }}}" class="round" width="40px" height="40px"/>
</th>
<th class="text-left">
<strong>{{ groupname }}</strong><br>
{{ coursename }}
</th>
<th class="nowrap text-right">
<span class="badge">{{ numberofunreadmessages }}</span> <span class="gray-light">{{#str}} emaildigestunreadmessages, message_email {{/str}}</span>
</th>
</tr>
</thead>
<tbody>
{{#messages}}
<tr>
<td width="40px" style="text-align: center;">
<img src="{{{ userpictureurl }}}" class="round" width="30px" height="30px">
</td>
<td colspan="2" class="message">
<strong>{{{ userfullname }}}</strong>
<span class="float-right gray-light text-small">{{ timesent }}</span>
{{{ message }}}
</td>
</tr>
{{/messages}}
<tr>
<td colspan="3" class="linkcolor">
{{{viewallmessageslink}}}
</td>
</tr>
</tbody>
</table>
{{/conversations}}
@@ -0,0 +1,76 @@
{{!
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/>.
}}
{{!
@template message_email/email_digest_text
Template which defines a forum post for sending in a single-post HTML email.
Classes required for JS:
* none
Data attributes required for JS:
* none
Example context (json):
{
"conversations": [
{
"groupname": "Blue Students",
"grouppictureurl": "http://example.com/image.jpg",
"coursename": "Math 101",
"numberofunreadmessages": "2",
"messages": [
{
"userfullname": "Chris Cross",
"userpictureurl": "http://example.com/image.jpg",
"message": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla neque nunc, bibendum ac vestibulum sit amet, scelerisque luctus sem. Maecenas ultricies hendrerit augue, ac venenatis odio volutpat nec",
"timesent": "10:12"
},
{
"userfullname": "Irene Ipsum",
"userpictureurl": "http://example.com/image.jpg",
"message": "Etiam a tristique risus. Pellentesque id tellus eget elit dictum varius id sed sapien",
"timesent": "10:14"
}
],
"viewallmessageslink": "http://example.com"
}
]
}
}}
{{#conversations}}
{{{grouppictureurl}}}
{{groupname}}
{{coursename}}
{{numberofunreadmessages}} {{#str}}emaildigestunreadmessages, message_email{{/str}}
{{#messages}}
{{{ userpictureurl }}}
{{userfullname}}
{{message}}
{{timesent}}
{{viewallmessageslink}}
{{/messages}}
{{/conversations}}
@@ -0,0 +1,95 @@
<?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/>.
namespace message_email;
/**
* Class for testing the event observers.
*
* @package message_email
* @category test
* @copyright 2019 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class event_observers_test extends \advanced_testcase {
/**
* Test the message viewed event observer.
*/
public function test_message_viewed_observer(): void {
global $DB;
$this->preventResetByRollback(); // Messaging is not compatible with transactions.
$this->resetAfterTest();
// Create the test data.
$course = $this->getDataGenerator()->create_course();
$user1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
$user2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
groups_add_member($group1->id, $user1->id);
groups_add_member($group1->id, $user2->id);
$conversation = \core_message\api::create_conversation(
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
[$user1->id, $user2->id],
'Group 1', \core_message\api::MESSAGE_CONVERSATION_ENABLED,
'core_group',
'groups',
$group1->id,
\context_course::instance($course->id)->id
);
$message = new \core\message\message();
$message->courseid = 1;
$message->component = 'moodle';
$message->name = 'instantmessage';
$message->userfrom = $user1;
$message->convid = $conversation->id;
$message->subject = 'message subject';
$message->fullmessage = 'message body';
$message->fullmessageformat = FORMAT_MARKDOWN;
$message->fullmessagehtml = '<p>message body</p>';
$message->smallmessage = 'small message';
$message->notification = '0';
// Send the message twice.
$messageid1 = message_send($message);
$messageid2 = message_send($message);
// Check there are now 2 messages pending to be sent in the digest.
$this->assertEquals(2, $DB->count_records('message_email_messages'));
// Mark one of the messages as read.
$message1 = $DB->get_record('messages', ['id' => $messageid1]);
\core_message\api::mark_message_as_read($user2->id, $message1);
$emailmessage = $DB->get_records('message_email_messages');
// Check there is now only 1 message pending to be sent in the digest and it is the correct message.
$this->assertEquals(1, count($emailmessage));
$emailmessage = reset($emailmessage);
$this->assertEquals($user2->id, $emailmessage->useridto);
$this->assertEquals($conversation->id, $emailmessage->conversationid);
$this->assertEquals($messageid2, $emailmessage->messageid);
}
}
@@ -0,0 +1,92 @@
<?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/>.
namespace message_email\privacy;
use context_system;
use core_message_external;
use core_privacy\local\request\writer;
use core_privacy\tests\provider_testcase;
/**
* Unit tests for message\output\email\classes\privacy\provider.php
*
* @package message_email
* @covers \message_email\privacy\provider
* @copyright 2018 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider_test extends provider_testcase {
/**
* Basic setup for these tests.
*/
public function setUp(): void {
$this->resetAfterTest(true);
}
/**
* Test returning metadata.
*/
public function test_get_metadata(): void {
$collection = new \core_privacy\local\metadata\collection('message_email');
$collection = \message_email\privacy\provider::get_metadata($collection);
$this->assertNotEmpty($collection);
}
/**
* Test getting the context for the user ID related to this plugin.
*/
public function test_get_contexts_for_userid(): void {
$user = $this->getDataGenerator()->create_user();
$contextlist = \message_email\privacy\provider::get_contexts_for_userid($user->id);
$this->assertEmpty($contextlist);
}
/**
* Test exporting user preferences
*/
public function test_export_user_preferences(): void {
global $CFG;
require_once("{$CFG->dirroot}/message/externallib.php");
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
// Submit configuration form, which adds the preferences..
core_message_external::message_processor_config_form($user->id, 'email', [
[
'name' => 'email_email',
'value' => 'alternate@example.com',
],
]);
// Switch to admin user (so we can validate preferences of the correct user are being exported).
$this->setAdminUser();
provider::export_user_preferences($user->id);
$writer = writer::with_context(context_system::instance());
$this->assertTrue($writer->has_any_data());
$preferences = $writer->get_user_preferences('message_email');
$this->assertNotEmpty($preferences->email);
$this->assertEquals('alternate@example.com', $preferences->email->value);
$this->assertEquals(get_string('privacy:preference:email', 'message_email'), $preferences->email->description);
}
}
@@ -0,0 +1,137 @@
<?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/>.
namespace message_email\task;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
/**
* Class for testing the send email task.
*
* @package message_email
* @category test
* @copyright 2019 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class send_email_test extends \advanced_testcase {
/**
* Test sending email task.
*/
public function test_sending_email_task(): void {
global $DB, $SITE;
$this->preventResetByRollback(); // Messaging is not compatible with transactions.
$this->resetAfterTest();
// Create a course.
$course = $this->getDataGenerator()->create_course();
$user1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
$user2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Create two groups in the course.
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
groups_add_member($group1->id, $user1->id);
groups_add_member($group2->id, $user1->id);
groups_add_member($group1->id, $user2->id);
groups_add_member($group2->id, $user2->id);
$conversation1 = \core_message\api::create_conversation(
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
[$user1->id, $user2->id],
'Group 1', \core_message\api::MESSAGE_CONVERSATION_ENABLED,
'core_group',
'groups',
$group1->id,
\context_course::instance($course->id)->id
);
$conversation2 = \core_message\api::create_conversation(
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
[$user1->id, $user2->id],
'Group 2',
\core_message\api::MESSAGE_CONVERSATION_ENABLED,
'core_group',
'groups',
$group2->id,
\context_course::instance($course->id)->id
);
// Go through each conversation.
if ($conversations = $DB->get_records('message_conversations')) {
foreach ($conversations as $conversation) {
$conversationid = $conversation->id;
// Let's send 5 messages.
for ($i = 1; $i <= 5; $i++) {
$message = new \core\message\message();
$message->courseid = 1;
$message->component = 'moodle';
$message->name = 'instantmessage';
$message->userfrom = $user1;
$message->convid = $conversationid;
$message->subject = 'message subject';
$message->fullmessage = 'message body';
$message->fullmessageformat = FORMAT_MARKDOWN;
$message->fullmessagehtml = '<p>message body</p>';
$message->smallmessage = 'small message';
$message->notification = '0';
message_send($message);
}
}
}
$this->assertEquals(10, $DB->count_records('message_email_messages'));
// Only 1 email is sent as the messages are included in it at a digest.
$sink = $this->redirectEmails();
$task = new send_email_task();
$task->execute();
$this->assertEquals(1, $sink->count());
// Confirm it contains the correct data.
$emails = $sink->get_messages();
$email = reset($emails);
$sitename = format_string($SITE->fullname);
$this->assertSame(get_string('messagedigestemailsubject', 'message_email', $sitename), $email->subject);
$this->assertSame($user2->email, $email->to);
$this->assertNotEmpty($email->header);
$emailbody = quoted_printable_decode($email->body);
$this->assertStringContainsString('Group 1', $emailbody);
$this->assertStringContainsString('Group 2', $emailbody);
// 5 unread messages per conversation, this will be listed twice.
$this->assertMatchesRegularExpression("/<span\b[^>]*>5<\/span> <span\b[^>]*>Unread message\w+/", $emailbody);
// Confirm table was emptied after task was run.
$this->assertEquals(0, $DB->count_records('message_email_messages'));
// Confirm running it again does not send another.
$sink = $this->redirectEmails();
$task = new send_email_task();
$task->execute();
$this->assertEquals(0, $sink->count());
}
}
+29
View File
@@ -0,0 +1,29 @@
<?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 processor version information
*
* @package message_email
* @copyright 2008 Luis Rodrigues
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2024042200; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2024041600; // Requires this Moodle version.
$plugin->component = 'message_email'; // Full name of the plugin (used for diagnostics)