MDL-3714 mod_forum: Add an inbound message handler

This issue is a part of the MDL-47194 Task.
This issue is a part of the MDL-39707 Epic.
This commit is contained in:
Andrew Nicols
2014-10-07 09:58:36 +08:00
parent 77c0a68db9
commit 91223df61b
6 changed files with 372 additions and 8 deletions
@@ -0,0 +1,292 @@
<?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/>.
/**
* A Handler to process replies to forum posts.
*
* @package mod_forum
* @subpackage core_message
* @copyright 2014 Andrew Nicols
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_forum\message\inbound;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/forum/lib.php');
require_once($CFG->dirroot . '/repository/lib.php');
/**
* A Handler to process replies to forum posts.
*
* @copyright 2014 Andrew Nicols
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class reply_handler extends \core\message\inbound\handler {
/**
* Return a description for the current handler.
*
* @return string
*/
public function get_description() {
return get_string('reply_handler', 'mod_forum');
}
/**
* Return a short name for the current handler.
* This appears in the admin pages as a human-readable name.
*
* @return string
*/
public function get_name() {
return get_string('reply_handler_name', 'mod_forum');
}
/**
* Process a message received and validated by the Inbound Message processor.
*
* @param $messagedata The Inbound Message record
* @param $messagedata The message data packet
* @return bool Whether the message was successfully processed.
*/
public function process_message(\stdClass $record, \stdClass $messagedata) {
global $DB, $CFG, $USER;
// Load the post being replied to.
$post = $DB->get_record('forum_posts', array('id' => $record->datavalue));
if (!$post) {
mtrace("--> Unable to find a post matching with id {$record->datavalue}");
return false;
}
// Load the discussion that this post is in.
$discussion = $DB->get_record('forum_discussions', array('id' => $post->discussion));
if (!$post) {
mtrace("--> Unable to find the discussion for post {$record->datavalue}");
return false;
}
// Load the other required data.
$forum = $DB->get_record('forum', array('id' => $discussion->forum));
$course = $DB->get_record('course', array('id' => $forum->course));
$coursecontext = \context_course::instance($course->id);
$cm = get_fast_modinfo($course->id)->instances['forum'][$forum->id];
$modcontext = \context_module::instance($cm->id);
$usercontext = \context_user::instance($USER->id);
// Make sure user can post in this discussion.
$canpost = true;
if (!forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext)) {
$canpost = false;
}
if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
$groupmode = $cm->groupmode;
} else {
$groupmode = $course->groupmode;
}
if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $modcontext)) {
if ($discussion->groupid == -1) {
$canpost = false;
} else {
if (!groups_is_member($discussion->groupid)) {
$canpost = false;
}
}
}
if (!$canpost) {
$data = new \stdClass();
$data->forum = $forum;
throw new \message\inbound\processing_failed_exception('messageinboundnopostforum', 'mod_forum', $data);
}
// And check the availability.
if (!\core_availability\info_module::is_user_visible($cm, $USER, true)) {
$data = new \stdClass();
$data->forum = $forum;
throw new \message\inbound\processing_failed_exception('messageinboundforumhidden', 'mod_forum', $data);
}
// Before we add this we must check that the user will not exceed the blocking threshold.
// This should result in an appropriate reply.
$thresholdwarning = forum_check_throttling($forum, $cm);
if (!empty($thresholdwarning) && !$thresholdwarning->canpost) {
$data = new \stdClass();
$data->forum = $forum;
$data->message = get_string($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->additional);
throw new \message\inbound\processing_failed_exception('messageinboundthresholdhit', 'mod_forum', $data);
}
$addpost = new \stdClass();
$addpost->course = $course->id;
$addpost->forum = $forum->id;
$addpost->discussion = $discussion->id;
$addpost->modified = $messagedata->timestamp;
$addpost->subject = clean_param($messagedata->envelope->subject, PARAM_TEXT);
$addpost->parent = $post->id;
$addpost->itemid = file_get_unused_draft_itemid();
if (!empty($messagedata->html)) {
$addpost->message = $messagedata->html;
$addpost->messageformat = FORMAT_HTML;
} else {
$addpost->message = $messagedata->plain;
$addpost->messageformat = FORMAT_PLAIN;
}
// We don't trust text coming from e-mail.
$addpost->messagetrust = false;
// Add attachments to the post.
if (!empty($messagedata->attachments['attachment']) && count($messagedata->attachments['attachment'])) {
$attachmentcount = count($messagedata->attachments['attachment']);
if (empty($forum->maxattachments) || $forum->maxbytes == 1 ||
!has_capability('mod/forum:createattachment', $modcontext)) {
// Attachments are not allowed.
mtrace("--> User does not have permission to attach files in this forum. Rejecting e-mail.");
$data = new \stdClass();
$data->forum = $forum;
$data->attachmentcount = $attachmentcount;
throw new \message\inbound\processing_failed_exception('messageinboundattachmentdisallowed', 'mod_forum', $data);
}
if ($forum->maxattachments < $attachmentcount) {
// Too many attachments.
mtrace("--> User attached {$attachmentcount} files when only {$forum->maxattachments} where allowed. "
. " Rejecting e-mail.");
$data = new \stdClass();
$data->forum = $forum;
$data->attachmentcount = $attachmentcount;
throw new \message\inbound\processing_failed_exception('messageinboundfilecountexceeded', 'mod_forum', $data);
}
$filesize = 0;
$addpost->attachments = file_get_unused_draft_itemid();
foreach ($messagedata->attachments['attachment'] as $attachment) {
mtrace("--> Processing {$attachment->filename} as an attachment.");
$this->process_attachment('*', $usercontext, $addpost->attachments, $attachment);
$filesize += $attachment->filesize;
}
if ($forum->maxbytes < $filesize) {
// Too many attachments.
mtrace("--> User attached {$filesize} bytes of files when only {$forum->maxbytes} where allowed. "
. "Rejecting e-mail.");
$data = new \stdClass();
$data->forum = $forum;
$data->maxbytes = display_size($forum->maxbytes);
$data->filesize = display_size($filesize);
throw new \message\inbound\processing_failed_exception('messageinboundfilesizeexceeded', 'mod_forum', $data);
}
}
// Process any files in the message itself.
if (!empty($messagedata->attachments['inline'])) {
foreach ($messagedata->attachments['inline'] as $attachment) {
mtrace("--> Processing {$attachment->filename} as an inline attachment.");
$this->process_attachment('*', $usercontext, $addpost->itemid, $attachment);
// Convert the contentid link in the message.
$draftfile = \moodle_url::make_draftfile_url($addpost->itemid, '/', $attachment->filename);
$addpost->message = preg_replace('/cid:' . $attachment->contentid . '/', $draftfile, $addpost->message);
}
}
// Insert the message content now.
$addpost->id = forum_add_new_post($addpost, true);
// Log the new post creation.
$params = array(
'context' => $modcontext,
'objectid' => $addpost->id,
'other' => array(
'discussionid' => $discussion->id,
'forumid' => $forum->id,
'forumtype' => $forum->type,
)
);
$event = \mod_forum\event\post_created::create($params);
$event->add_record_snapshot('forum_posts', $addpost);
$event->add_record_snapshot('forum_discussions', $discussion);
$event->trigger();
mtrace("--> Created a post {$addpost->id} in {$discussion->id}.");
return $addpost;
}
/**
* Process attachments included in a message.
*
* @param $acceptedtypes String The mimetypes of the acceptable attachment types.
* @param $context context_user The context of the user creating this attachment.
* @param $itemid int The itemid to store this attachment under.
* @param $attachment stdClass The Attachment data to store.
*/
protected function process_attachment($acceptedtypes, \context_user $context, $itemid, \stdClass $attachment) {
global $DB, $USER, $CFG;
// Create the file record.
$record = new \stdClass();
$record->filearea = 'draft';
$record->component = 'user';
$record->itemid = $itemid;
$record->license = $CFG->sitedefaultlicense;
$record->author = fullname($USER);
$record->contextid = $context->id;
$record->userid = $USER->id;
// All files sent by e-mail should have a flat structure.
$record->filepath = '/';
$record->filename = $attachment->filename;
mtrace("--> Attaching {$record->filename} to " .
"/{$record->contextid}/{$record->component}/{$record->filearea}/" .
"{$record->itemid}{$record->filepath}{$record->filename}");
$fs = get_file_storage();
return $fs->create_file_from_string($record, $attachment->content);
}
/**
* Return the content of any success notification to be sent.
* Both an HTML and Plain Text variant must be provided.
*
* @param \stdClass $messagedata The message data.
* @param \stdClass $handlerresult The record for the newly created post.
* @return \stdClass with keys `html` and `plain`.
*/
public function get_success_message(\stdClass $messagedata, $handlerresult) {
$a = new \stdClass();
$a->subject = $handlerresult->subject;
$discussionurl = new \moodle_url('/mod/forum/discuss.php', array('d' => $handlerresult->discussion));
$a->discussionurl = $discussionurl->out();
$message = new \stdClass();
$message->plain = get_string('postbymailsuccess', 'mod_forum', $a);
$message->html = get_string('postbymailsuccess_html', 'mod_forum', $a);
return $message;
}
}
+31
View File
@@ -0,0 +1,31 @@
<?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/>.
/**
* Message Inbound Handlers for mod_forum.
*
* @package mod_forum
* @copyright 2014 Andrew NIcols
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$handlers = array(
array(
'classname' => '\mod_forum\message\inbound\reply_handler',
),
);
+12
View File
@@ -397,6 +397,8 @@ $string['repliesmany'] = '{$a} replies so far';
$string['repliesone'] = '{$a} reply so far';
$string['reply'] = 'Reply';
$string['replyforum'] = 'Reply to forum';
$string['replytoforumpost'] = 'Reply via email to {$a}';
$string['replytoforumpost_html'] = '<a href="mailto:{$a}">Reply via email</a>';
$string['replytouser'] = 'Use email address in reply';
$string['resetforums'] = 'Delete posts from';
$string['resetforumsall'] = 'Delete all posts';
@@ -490,3 +492,13 @@ $string['warnformorepost'] = 'Warning! There is more than one discussion in this
$string['yournewquestion'] = 'Your new question';
$string['yournewtopic'] = 'Your new discussion topic';
$string['yourreply'] = 'Your reply';
$string['reply_handler'] = 'Reply to forum posts via email';
$string['reply_handler_name'] = 'Reply to forum posts';
$string['messageinboundattachmentdisallowed'] = 'Unable to post your reply, since it includes an attachment and the forum doesn\'t allow attachments.';
$string['messageinboundfilecountexceeded'] = 'Unable to post your reply, since it includes more than the maximum number of attachments allowed for the forum ({$a->forum->maxattachments}).';
$string['messageinboundfilesizeexceeded'] = 'Unable to post your reply, since the total attachment size ({$a->filesize}) is greater than the maximum size allowed for the forum ({$a->maxbytes}).';
$string['messageinboundforumhidden'] = 'Unable to post your reply, since the forum is currently unavailable.';
$string['messageinboundnopostforum'] = 'Unable to post your reply, since you do not have permission to post in the {$a->forum->name} forum.';
$string['messageinboundthresholdhit'] = 'Unable to post your reply. You have exceeded the posting threshold set for this forum';
$string['postbymailsuccess'] = 'Congratulations, your forum post with subject "{$a->subject}" was successfully added. You can view it at {$a->discussionurl}.';
$string['postbymailsuccess_html'] = 'Congratulations, your <a href="{$a->discussionurl}">forum post</a> with subject "{$a->subject}" was successfully posted.';
+36 -6
View File
@@ -463,6 +463,7 @@ function forum_cron() {
$courses = array();
$coursemodules = array();
$subscribedusers = array();
$messageinboundhandlers = array();
// Posts older than 2 days will not be mailed. This is to avoid the problem where
// cron has not been running for a long time, and then suddenly people are flooded
@@ -482,6 +483,10 @@ function forum_cron() {
}
$digestsset->close();
// Create the generic messageinboundgenerator.
$messageinboundgenerator = new \core\message\inbound\address_manager();
$messageinboundgenerator->set_handler('\mod_forum\message\inbound\reply_handler');
if ($posts = forum_get_unmailed_posts($starttime, $endtime, $timenow)) {
// Mark them all now as being mailed. It's unlikely but possible there
// might be an error later so that a post is NOT actually mailed out,
@@ -543,6 +548,11 @@ function forum_cron() {
if (!isset($subscribedusers[$forumid])) {
$modcontext = context_module::instance($coursemodules[$forumid]->id);
if ($subusers = \mod_forum\subscriptions::fetch_subscribed_users($forums[$forumid], 0, $modcontext, 'u.*', true)) {
// Save the Inbound Message datakey here to reduce DB queries later.
$messageinboundgenerator->set_data($pid);
$messageinboundhandlers[$pid] = $messageinboundgenerator->fetch_data_key();
foreach ($subusers as $postuser) {
// this user is subscribed to this forum
$subscribedusers[$forumid][$postuser->id] = $postuser->id;
@@ -734,13 +744,22 @@ function forum_cron() {
$shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
// Generate a reply-to address from using the Inbound Message handler.
$replyaddress = null;
if ($userto->canpost[$discussion->id] && array_key_exists($post->id, $messageinboundhandlers)) {
$messageinboundgenerator->set_data($post->id, $messageinboundhandlers[$post->id]);
$replyaddress = $messageinboundgenerator->generate($userto->id);
}
$a = new stdClass();
$a->courseshortname = $shortname;
$a->forumname = $cleanforumname;
$a->subject = format_string($post->subject, true);
$postsubject = html_to_text(get_string('postmailsubject', 'forum', $a));
$posttext = forum_make_mail_text($course, $cm, $forum, $discussion, $post, $userfrom, $userto);
$posthtml = forum_make_mail_html($course, $cm, $forum, $discussion, $post, $userfrom, $userto);
$posttext = forum_make_mail_text($course, $cm, $forum, $discussion, $post, $userfrom, $userto, false,
$replyaddress);
$posthtml = forum_make_mail_html($course, $cm, $forum, $discussion, $post, $userfrom, $userto,
$replyaddress);
// Send the post now!
mtrace('Sending ', '');
@@ -755,6 +774,7 @@ function forum_cron() {
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = $posthtml;
$eventdata->notification = 1;
$eventdata->replyto = $replyaddress;
// If forum_replytouser is not set then send mail using the noreplyaddress.
if (empty($CFG->forum_replytouser)) {
@@ -1127,9 +1147,10 @@ function forum_cron() {
* @param object $userfrom
* @param object $userto
* @param boolean $bare
* @param string $replyaddress The inbound address that a user can reply to the generated e-mail with.
* @return string The email body in plain text format.
*/
function forum_make_mail_text($course, $cm, $forum, $discussion, $post, $userfrom, $userto, $bare = false) {
function forum_make_mail_text($course, $cm, $forum, $discussion, $post, $userfrom, $userto, $bare = false, $replyaddress = null) {
global $CFG, $USER;
$modcontext = context_module::instance($cm->id);
@@ -1198,6 +1219,10 @@ function forum_make_mail_text($course, $cm, $forum, $discussion, $post, $userfro
$posttext .= get_string("digestmailpost", "forum");
$posttext .= ": {$CFG->wwwroot}/mod/forum/index.php?id={$forum->course}\n";
if ($replyaddress) {
$posttext .= "\n\n" . get_string('replytoforumpost', 'mod_forum', $replyaddress);
}
return $posttext;
}
@@ -1212,9 +1237,10 @@ function forum_make_mail_text($course, $cm, $forum, $discussion, $post, $userfro
* @param object $post
* @param object $userfrom
* @param object $userto
* @param string $replyaddress The inbound address that a user can reply to the generated e-mail with.
* @return string The email text in HTML format
*/
function forum_make_mail_html($course, $cm, $forum, $discussion, $post, $userfrom, $userto) {
function forum_make_mail_html($course, $cm, $forum, $discussion, $post, $userfrom, $userto, $replyaddress = null) {
global $CFG;
if ($userto->mailformat != 1) { // Needs to be HTML
@@ -1251,6 +1277,10 @@ function forum_make_mail_html($course, $cm, $forum, $discussion, $post, $userfro
}
$posthtml .= forum_make_mail_post($course, $cm, $forum, $discussion, $post, $userfrom, $userto, false, $canreply, true, false);
if ($replyaddress) {
$posthtml .= "<p>" . get_string('replytoforumpost_html', 'mod_forum', $replyaddress) . "</p>";
}
$footerlinks = array();
if ($canunsubscribe) {
$footerlinks[] = '<a href="' . $CFG->wwwroot . '/mod/forum/subscribe.php?id=' . $forum->id . '">' . get_string('unsubscribe', 'forum') . '</a>';
@@ -4247,7 +4277,7 @@ function forum_add_attachment($post, $forum, $cm, $mform=null, $unused=null) {
* @param string $message
* @return int
*/
function forum_add_new_post($post, $mform, &$message) {
function forum_add_new_post($post, $mform, $unused = null) {
global $USER, $CFG, $DB;
$discussion = $DB->get_record('forum_discussions', array('id' => $post->discussion));
@@ -4270,7 +4300,7 @@ function forum_add_new_post($post, $mform, &$message) {
$post->message = file_save_draft_area_files($post->itemid, $context->id, 'mod_forum', 'post', $post->id,
mod_forum_post_form::editor_options($context, null), $post->message);
$DB->set_field('forum_posts', 'message', $post->message, array('id'=>$post->id));
forum_add_attachment($post, $forum, $cm, $mform, $message);
forum_add_attachment($post, $forum, $cm, $mform);
// Update discussion modified date
$DB->set_field("forum_discussions", "timemodified", $post->modified, array("id" => $post->discussion));
-1
View File
@@ -770,7 +770,6 @@ if ($fromform = $mform_post->get_data()) {
$addpost = $fromform;
$addpost->forum=$forum->id;
if ($fromform->id = forum_add_new_post($addpost, $mform_post, $message)) {
$timemessage = 2;
if (!empty($message)) { // if we're printing stuff about the file upload
$timemessage = 4;
+1 -1
View File
@@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2014082100; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2014082101; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2014050800; // Requires this Moodle version
$plugin->component = 'mod_forum'; // Full name of the plugin (used for diagnostics)