Commit bc1a662b authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Delay generation of email content for forum post notifications (bug #716812)


Signed-off-by: default avatarRichard Mansfield <richard.mansfield@catalyst.net.nz>
parent 46f9ea0e
......@@ -645,6 +645,7 @@ class InteractionForumInstance extends InteractionInstance {
class ActivityTypeInteractionForumNewPost extends ActivityTypePlugin {
protected $postid;
protected $temp;
public function __construct($data) {
parent::__construct($data);
......@@ -694,55 +695,59 @@ class ActivityTypeInteractionForumNewPost extends ActivityTypePlugin {
$this->customheaders[] = 'References: <forumpost' . $post->parent . '@' . $hostname . '>';
}
$posttime = strftime(get_string('strftimedaydatetime'), $post->ctime);
$htmlbody = $post->body;
$this->message = strip_tags(str_shorten_html($htmlbody, 200, true)); // For internal notifications.
$post->posttime = strftime(get_string('strftimedaydatetime'), $post->ctime);
$this->message = strip_tags(str_shorten_html($post->body, 200, true)); // For internal notifications.
$textbody = trim(html2text($post->body));
$postlink = get_config('wwwroot') . 'interaction/forum/topic.php?id=' . $post->topicid . '#post' . $this->postid;
$post->textbody = trim(html2text($post->body));
$this->url = get_config('wwwroot') . 'interaction/forum/topic.php?id=' . $post->topicid . '#post' . $this->postid;
$this->url = $postlink;
$this->add_urltext(array(
'key' => 'Topic',
'section' => 'interaction.forum'
));
foreach ($this->users as &$user) {
$lang = (empty($user->lang) || $user->lang == 'default') ? get_config('lang') : $user->lang;
if ($post->parent) {
$user->subject = get_string_from_language($lang, 'replyforumpostnotificationsubject', 'interaction.forum', $post->groupname, $post->forumtitle, $post->topicsubject);
}
else {
$user->subject = get_string_from_language($lang, 'newforumpostnotificationsubject', 'interaction.forum', $post->groupname, $post->forumtitle, $post->subject);
}
$this->strings->subject = (object) array(
'key' => $post->parent ? 'replyforumpostnotificationsubject' : 'newforumpostnotificationsubject',
'section' => 'interaction.forum',
'args' => array($post->groupname, $post->forumtitle, $post->parent ? $post->topicsubject : $post->subject),
);
$type = $subscribers[$user->id]->type;
$unsubscribeid = $post->{$type . 'id'};
$unsubscribelink = get_config('wwwroot') . 'interaction/forum/unsubscribe.php?' . $type . '=' . $unsubscribeid . '&key=' . $subscribers[$user->id]->key;
$user->emailmessage = get_string_from_language($lang, 'forumposttemplate', 'interaction.forum',
$post->subject ? $post->subject : get_string_from_language($lang, 're', 'interaction.forum', $post->topicsubject),
display_name($post->poster, $user),
$posttime,
$textbody,
$postlink,
$type,
$unsubscribelink
);
$user->htmlmessage = get_string_from_language($lang, 'forumposthtmltemplate', 'interaction.forum',
$post->subject ? $post->subject : get_string_from_language($lang, 're', 'interaction.forum', $post->topicsubject),
display_name($post->poster, $user),
$posttime,
$htmlbody,
$postlink,
$unsubscribelink,
$type
);
foreach ($this->users as &$user) {
$user->subscribetype = $subscribers[$user->id]->type;
$user->unsubscribekey = $subscribers[$user->id]->key;
}
$this->temp = (object) array('post' => $post);
}
public function get_emailmessage($user) {
$post = $this->temp->post;
$unsubscribeid = $post->{$user->subscribetype . 'id'};
$unsubscribelink = get_config('wwwroot') . 'interaction/forum/unsubscribe.php?' . $user->subscribetype . '=' . $unsubscribeid . '&key=' . $user->unsubscribekey;
return get_string_from_language($user->lang, 'forumposttemplate', 'interaction.forum',
$post->subject ? $post->subject : get_string_from_language($user->lang, 're', 'interaction.forum', $post->topicsubject),
display_name($post->poster, $user),
$post->posttime,
$post->textbody,
$this->url,
$user->subscribetype,
$unsubscribelink
);
}
public function get_subject($user) {
return $user->subject;
public function get_htmlmessage($user) {
$post = $this->temp->post;
$unsubscribeid = $post->{$user->subscribetype . 'id'};
$unsubscribelink = get_config('wwwroot') . 'interaction/forum/unsubscribe.php?' . $user->subscribetype . '=' . $unsubscribeid . '&key=' . $user->unsubscribekey;
return get_string_from_language($user->lang, 'forumposthtmltemplate', 'interaction.forum',
$post->subject ? $post->subject : get_string_from_language($user->lang, 're', 'interaction.forum', $post->topicsubject),
display_name($post->poster, $user),
$post->posttime,
$post->body,
$this->url,
$unsubscribelink,
$user->subscribetype
);
}
public function get_plugintype(){
......
......@@ -421,18 +421,20 @@ abstract class ActivityType {
if (!empty($user->url)) {
$userdata->url = $user->url;
}
$userdata->urltext = $this->get_urltext($user);
if (empty($user->lang) || $user->lang == 'default') {
$user->lang = get_config('lang');
}
$userdata->message = $this->get_message($user);
$userdata->subject = $this->get_subject($user);
if (empty($user->method)) {
$user->method = call_static_method(get_class($this), 'default_notification_method');
}
// always do internal
$userdata->internalid = call_static_method('PluginNotificationInternal', 'notify_user', $user, $userdata);
foreach (PluginNotificationInternal::$userdata as &$p) {
$function = 'get_' . $p;
$userdata->$p = $this->$function($user);
}
$userdata->internalid = PluginNotificationInternal::notify_user($user, $userdata);
if ($this->update_url($userdata->internalid)) {
$changes->url = $userdata->url = $this->url;
}
......@@ -446,8 +448,18 @@ abstract class ActivityType {
if ($user->method != 'internal') {
$method = $user->method;
safe_require('notification', $method);
$notificationclass = generate_class_name('notification', $method);
$classvars = get_class_vars($notificationclass);
if (!empty($classvars['userdata'])) {
foreach ($classvars['userdata'] as &$p) {
$function = 'get_' . $p;
if (!isset($userdata->$p) && method_exists($this, $function)) {
$userdata->$p = $this->$function($user);
}
}
}
try {
call_static_method(generate_class_name('notification', $method), 'notify_user', $user, $userdata);
call_static_method($notificationclass, 'notify_user', $user, $userdata);
}
catch (MaharaException $e) {
// We don't mind other notification methods failing, as it'll
......
......@@ -31,19 +31,27 @@ require_once(get_config('docroot') . 'notification/lib.php');
class PluginNotificationEmail extends PluginNotification {
static $userdata = array('htmlmessage', 'emailmessage');
public static function notify_user($user, $data) {
$messagehtml = null;
if (!empty($data->overridemessagecontents)) {
$subject = $data->subject;
if (!empty($user->emailmessage)) {
if (!empty($data->emailmessage)) {
$messagebody = $data->emailmessage;
}
else if (!empty($user->emailmessage)) {
$messagebody = $user->emailmessage;
}
else {
$messagebody = $data->message;
}
if (!empty($user->htmlmessage)) {
if (!empty($data->htmlmessage)) {
$messagehtml = $data->htmlmessage;
}
else if (!empty($user->htmlmessage)) {
$messagehtml = $user->htmlmessage;
}
}
......
......@@ -29,6 +29,8 @@ defined('INTERNAL') || die();
class PluginNotificationInternal extends PluginNotification {
static $userdata = array('urltext', 'subject', 'message');
public static function notify_user($user, $data) {
$toinsert = new StdClass;
$toinsert->type = $data->type;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment