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]);
}
}