MDL-49682 mod_forum: Build forum email templates
authorAndrew Nicols <andrew@nicols.co.uk>
Fri, 25 Sep 2015 11:57:05 +0000 (19:57 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Mon, 12 Oct 2015 05:26:29 +0000 (13:26 +0800)
18 files changed:
mod/forum/classes/output/email/renderer.php [new file with mode: 0644]
mod/forum/classes/output/email/renderer_textemail.php [new file with mode: 0644]
mod/forum/classes/output/emaildigestbasic/renderer.php [new file with mode: 0644]
mod/forum/classes/output/emaildigestbasic/renderer_textemail.php [new file with mode: 0644]
mod/forum/classes/output/emaildigestfull/renderer.php [new file with mode: 0644]
mod/forum/classes/output/emaildigestfull/renderer_textemail.php [new file with mode: 0644]
mod/forum/classes/output/forum_post.php [new file with mode: 0644]
mod/forum/classes/output/forum_post_email.php [new file with mode: 0644]
mod/forum/deprecatedlib.php
mod/forum/lang/en/forum.php
mod/forum/lib.php
mod/forum/renderer.php
mod/forum/templates/forum_post_email_htmlemail.mustache [new file with mode: 0644]
mod/forum/templates/forum_post_email_textemail.mustache [new file with mode: 0644]
mod/forum/templates/forum_post_emaildigestbasic_htmlemail.mustache [new file with mode: 0644]
mod/forum/templates/forum_post_emaildigestbasic_textemail.mustache [new file with mode: 0644]
mod/forum/templates/forum_post_emaildigestfull_htmlemail.mustache [new file with mode: 0644]
mod/forum/templates/forum_post_emaildigestfull_textemail.mustache [new file with mode: 0644]

diff --git a/mod/forum/classes/output/email/renderer.php b/mod/forum/classes/output/email/renderer.php
new file mode 100644 (file)
index 0000000..a37b721
--- /dev/null
@@ -0,0 +1,71 @@
+<?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/>.
+
+/**
+ * Forum post renderable.
+ *
+ * @package    mod_forum
+ * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_forum\output\email;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Forum post renderable.
+ *
+ * @since      Moodle 3.0
+ * @package    mod_forum
+ * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class renderer extends \mod_forum_renderer {
+
+    /**
+     * The template name for this renderer.
+     *
+     * @return string
+     */
+    public function forum_post_template() {
+        return 'forum_post_email_htmlemail';
+    }
+
+    /**
+     * The HTML version of the e-mail message.
+     *
+     * @param \stdClass $forum
+     * @param \stdClass $post
+     * @return string
+     */
+    public function format_message_text($forum, $post) {
+        $options = new \stdClass();
+        $options->para = true;
+        return format_text($post->message, $post->messageformat, $options);
+    }
+
+    /**
+     * The HTML version of the attachments list.
+     *
+     * @param \stdClass $cm
+     * @param \stdClass $post
+     * @return string
+     */
+    public function format_message_attachments($cm, $post) {
+        return forum_print_attachments($post, $cm, "html");
+    }
+}
diff --git a/mod/forum/classes/output/email/renderer_textemail.php b/mod/forum/classes/output/email/renderer_textemail.php
new file mode 100644 (file)
index 0000000..82e99b8
--- /dev/null
@@ -0,0 +1,72 @@
+<?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/>.
+
+/**
+ * Forum post renderable.
+ *
+ * @package    mod_forum
+ * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_forum\output\email;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Forum post renderable.
+ *
+ * @since      Moodle 3.0
+ * @package    mod_forum
+ * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class renderer_textemail extends renderer {
+
+    /**
+     * The template name for this renderer.
+     *
+     * @return string
+     */
+    public function forum_post_template() {
+        return 'forum_post_email_textemail';
+    }
+
+    /**
+     * The plaintext version of the e-mail message.
+     *
+     * @param \stdClass $forum
+     * @param \stdClass $post
+     * @return string
+     */
+    public function format_message_text($forum, $post) {
+        $message = file_rewrite_pluginfile_urls($post->message, 'pluginfile.php',
+            \context_module::instance($forum->id)->id,
+            'mod_forum', 'post', $post->id);
+        return format_text_email($message, $post->messageformat);
+    }
+
+    /**
+     * The plaintext version of the attachments list.
+     *
+     * @param \stdClass $cm
+     * @param \stdClass $post
+     * @return string
+     */
+    public function format_message_attachments($cm, $post) {
+        return forum_print_attachments($post, $cm, "text");
+    }
+}
diff --git a/mod/forum/classes/output/emaildigestbasic/renderer.php b/mod/forum/classes/output/emaildigestbasic/renderer.php
new file mode 100644 (file)
index 0000000..ee32eff
--- /dev/null
@@ -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/>.
+
+/**
+ * Forum post renderable.
+ *
+ * @package    mod_forum
+ * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_forum\output\emaildigestbasic;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Forum post renderable.
+ *
+ * @since      Moodle 3.0
+ * @package    mod_forum
+ * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class renderer extends \mod_forum\output\email\renderer {
+
+    /**
+     * The template name for this renderer.
+     *
+     * @return string
+     */
+    public function forum_post_template() {
+        return 'forum_post_emaildigestbasic_htmlemail';
+    }
+}
diff --git a/mod/forum/classes/output/emaildigestbasic/renderer_textemail.php b/mod/forum/classes/output/emaildigestbasic/renderer_textemail.php
new file mode 100644 (file)
index 0000000..dc1e0ed
--- /dev/null
@@ -0,0 +1,61 @@
+<?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/>.
+
+/**
+ * Forum post renderable.
+ *
+ * @package    mod_forum
+ * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_forum\output\emaildigestbasic;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Forum post renderable.
+ *
+ * @since      Moodle 3.0
+ * @package    mod_forum
+ * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class renderer_textemail extends \mod_forum\output\email\renderer_textemail {
+
+    /**
+     * The template name for this renderer.
+     *
+     * @return string
+     */
+    public function forum_post_template() {
+        return 'forum_post_emaildigestbasic_textemail';
+    }
+
+    /**
+     * The plaintext version of the e-mail message.
+     *
+     * @param \stdClass $forum
+     * @param \stdClass $post
+     * @return string
+     */
+    public function format_message_text($forum, $post) {
+        $message = file_rewrite_pluginfile_urls($post->message, 'pluginfile.php',
+            \context_module::instance($forum->id)->id,
+            'mod_forum', 'post', $post->id);
+        return format_text_email($message, $post->messageformat);
+    }
+}
diff --git a/mod/forum/classes/output/emaildigestfull/renderer.php b/mod/forum/classes/output/emaildigestfull/renderer.php
new file mode 100644 (file)
index 0000000..6f612d7
--- /dev/null
@@ -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/>.
+
+/**
+ * Forum post renderable.
+ *
+ * @package    mod_forum
+ * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_forum\output\emaildigestfull;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Forum post renderable.
+ *
+ * @since      Moodle 3.0
+ * @package    mod_forum
+ * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class renderer extends \mod_forum\output\email\renderer {
+
+    /**
+     * The template name for this renderer.
+     *
+     * @return string
+     */
+    public function forum_post_template() {
+        return 'forum_post_emaildigestfull_htmlemail';
+    }
+}
diff --git a/mod/forum/classes/output/emaildigestfull/renderer_textemail.php b/mod/forum/classes/output/emaildigestfull/renderer_textemail.php
new file mode 100644 (file)
index 0000000..99f70a8
--- /dev/null
@@ -0,0 +1,61 @@
+<?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/>.
+
+/**
+ * Forum post renderable.
+ *
+ * @package    mod_forum
+ * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_forum\output\emaildigestfull;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Forum post renderable.
+ *
+ * @since      Moodle 3.0
+ * @package    mod_forum
+ * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class renderer_textemail extends \mod_forum\output\email\renderer_textemail {
+
+    /**
+     * The template name for this renderer.
+     *
+     * @return string
+     */
+    public function forum_post_template() {
+        return 'forum_post_emaildigestfull_textemail';
+    }
+
+    /**
+     * The plaintext version of the e-mail message.
+     *
+     * @param \stdClass $forum
+     * @param \stdClass $post
+     * @return string
+     */
+    public function format_message_text($forum, $post) {
+        $message = file_rewrite_pluginfile_urls($post->message, 'pluginfile.php',
+            \context_module::instance($forum->id)->id,
+            'mod_forum', 'post', $post->id);
+        return format_text_email($message, $post->messageformat);
+    }
+}
diff --git a/mod/forum/classes/output/forum_post.php b/mod/forum/classes/output/forum_post.php
new file mode 100644 (file)
index 0000000..c68d76c
--- /dev/null
@@ -0,0 +1,481 @@
+<?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/>.
+
+/**
+ * Forum post renderable.
+ *
+ * @package    mod_forum
+ * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_forum\output;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Forum post renderable.
+ *
+ * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ *
+ * @property boolean $viewfullnames Whether to override fullname()
+ */
+class forum_post implements \renderable {
+
+    /**
+     * The course that the forum post is in.
+     *
+     * @var object $course
+     */
+    private $course = null;
+
+    /**
+     * The course module for the forum.
+     *
+     * @var object $cm
+     */
+    private $cm = null;
+
+    /**
+     * The forum that the post is in.
+     *
+     * @var object $forum
+     */
+    private $forum = null;
+
+    /**
+     * The discussion that the forum post is in.
+     *
+     * @var object $discussion
+     */
+    private $discussion = null;
+
+    /**
+     * The forum post being displayed.
+     *
+     * @var object $post
+     */
+    private $post = null;
+
+    /**
+     * Whether the user can reply to this post.
+     *
+     * @var boolean $canreply
+     */
+    private $canreply = false;
+
+    /**
+     * Whether to override forum display when displaying usernames.
+     * @var boolean $viewfullnames
+     */
+    private $viewfullnames = false;
+
+    /**
+     * The user that is reading the post.
+     *
+     * @var object $userto
+     */
+    private $userto = null;
+
+    /**
+     * The user that wrote the post.
+     *
+     * @var object $author
+     */
+    private $author = null;
+
+    /**
+     * An associative array indicating which keys on this object should be writeable.
+     *
+     * @var array $writablekeys
+     */
+    private $writablekeys = array(
+        'viewfullnames'    => true,
+    );
+
+    /**
+     * Builds a renderable forum post
+     *
+     * @param object $course Course of the forum
+     * @param object $cm Course Module of the forum
+     * @param object $forum The forum of the post
+     * @param object $discussion Discussion thread in which the post appears
+     * @param object $post The post
+     * @param object $author Author of the post
+     * @param object $recipient Recipient of the email
+     * @param bool $canreply True if the user can reply to the post
+     */
+    public function __construct($course, $cm, $forum, $discussion, $post, $author, $recipient, $canreply) {
+        $this->course = $course;
+        $this->cm = $cm;
+        $this->forum = $forum;
+        $this->discussion = $discussion;
+        $this->post = $post;
+        $this->author = $author;
+        $this->userto = $recipient;
+        $this->canreply = $canreply;
+    }
+
+    /**
+     * Export this data so it can be used as the context for a mustache template.
+     *
+     * @param \mod_forum_renderer $renderer The render to be used for formatting the message and attachments
+     * @return stdClass Data ready for use in a mustache template
+     */
+    public function export_for_template(\mod_forum_renderer $renderer) {
+        return array(
+            'id'                            => $this->post->id,
+            'coursename'                    => $this->get_coursename(),
+            'courselink'                    => $this->get_courselink(),
+            'forumname'                     => $this->get_forumname(),
+            'showdiscussionname'            => $this->get_showdiscussionname(),
+            'discussionname'                => $this->get_discussionname(),
+            'subject'                       => $this->get_subject(),
+            'authorfullname'                => $this->get_author_fullname(),
+            'postdate'                      => $this->get_postdate(),
+
+            // Format some components according to the renderer.
+            'message'                       => $renderer->format_message_text($this->forum, $this->post),
+            'attachments'                   => $renderer->format_message_attachments($this->cm, $this->post),
+
+            'canreply'                      => $this->canreply,
+            'permalink'                     => $this->get_permalink(),
+            'firstpost'                     => $this->get_is_firstpost(),
+            'replylink'                     => $this->get_replylink(),
+            'unsubscribediscussionlink'     => $this->get_unsubscribediscussionlink(),
+            'unsubscribeforumlink'          => $this->get_unsubscribeforumlink(),
+            'parentpostlink'                => $this->get_parentpostlink(),
+
+            'forumindexlink'                => $this->get_forumindexlink(),
+            'forumviewlink'                 => $this->get_forumviewlink(),
+            'discussionlink'                => $this->get_discussionlink(),
+
+            'authorlink'                    => $this->get_authorlink(),
+            'authorpicture'                 => $this->get_author_picture(),
+
+            'grouppicture'                  => $this->get_group_picture(),
+        );
+    }
+
+    /**
+     * Magically sets a property against this object.
+     *
+     * @param string $key
+     * @param mixed $value
+     */
+    public function __set($key, $value) {
+        // First attempt to use the setter function.
+        $methodname = 'set_' . $key;
+        if (method_exists($this, $methodname)) {
+            return $this->{$methodname}($value);
+        }
+
+        // Fall back to the writable keys list.
+        if (isset($this->writablekeys[$key]) && $this->writablekeys[$key]) {
+            return $this->{$key} = $value;
+        }
+
+        // Throw an error rather than fail silently.
+        throw new \coding_exception('Tried to set unknown property "' . $key . '"');
+    }
+
+    /**
+     * Whether this is the first post.
+     *
+     * @return boolean
+     */
+    public function get_is_firstpost() {
+        return empty($this->post->parent);
+    }
+
+    /**
+     * Get the link to the course.
+     *
+     * @return string
+     */
+    public function get_courselink() {
+        $link = new \moodle_url(
+            // Posts are viewed on the topic.
+            '/course/view.php', array(
+                'id'    => $this->course->id,
+            )
+        );
+
+        return $link->out(false);
+    }
+
+    /**
+     * Get the link to the forum index for this course.
+     *
+     * @return string
+     */
+    public function get_forumindexlink() {
+        $link = new \moodle_url(
+            // Posts are viewed on the topic.
+            '/mod/forum/index.php', array(
+                'id'    => $this->course->id,
+            )
+        );
+
+        return $link->out(false);
+    }
+
+    /**
+     * Get the link to the view page for this forum.
+     *
+     * @return string
+     */
+    public function get_forumviewlink() {
+        $link = new \moodle_url(
+            // Posts are viewed on the topic.
+            '/mod/forum/view.php', array(
+                'f' => $this->forum->id,
+            )
+        );
+
+        return $link->out(false);
+    }
+
+    /**
+     * Get the link to the current discussion.
+     *
+     * @return string
+     */
+    protected function _get_discussionlink() {
+        return new \moodle_url(
+            // Posts are viewed on the topic.
+            '/mod/forum/discuss.php', array(
+                // Within a discussion.
+                'd' => $this->discussion->id,
+            )
+        );
+    }
+
+    /**
+     * Get the link to the current discussion.
+     *
+     * @return string
+     */
+    public function get_discussionlink() {
+        $link = $this->_get_discussionlink();
+
+        return $link->out(false);
+    }
+
+    /**
+     * Get the link to the current post, including post anchor.
+     *
+     * @return string
+     */
+    public function get_permalink() {
+        $link = $this->_get_discussionlink();
+        $link->set_anchor($this->get_postanchor());
+
+        return $link->out(false);
+    }
+
+    /**
+     * Get the link to the parent post.
+     *
+     * @return string
+     */
+    public function get_parentpostlink() {
+        $link = $this->_get_discussionlink();
+        $link->param('parent', $this->post->parent);
+
+        return $link->out(false);
+    }
+
+    /**
+     * Get the link to the author's profile page.
+     *
+     * @return string
+     */
+    public function get_authorlink() {
+        $link = new \moodle_url(
+            '/user/view.php', array(
+                'id' => $this->post->userid,
+                'course' => $this->course->id,
+            )
+        );
+
+        return $link->out(false);
+    }
+
+    /**
+     * Get the link to unsubscribe from the forum.
+     *
+     * @return string
+     */
+    public function get_unsubscribeforumlink() {
+        $link = new \moodle_url(
+            '/mod/forum/subscribe.php', array(
+                'id' => $this->forum->id,
+            )
+        );
+
+        return $link->out(false);
+    }
+
+    /**
+     * Get the link to unsubscribe from the discussion.
+     *
+     * @return string
+     */
+    public function get_unsubscribediscussionlink() {
+        $link = new \moodle_url(
+            '/mod/discussion/subscribe.php', array(
+                'id'  => $this->forum->id,
+                'd'   => $this->discussion->id,
+            )
+        );
+
+        return $link->out(false);
+    }
+
+    /**
+     * Get the link to reply to the current post.
+     *
+     * @return string
+     */
+    public function get_replylink() {
+        return new \moodle_url(
+            '/mod/forum/post.php', array(
+                'reply' => $this->post->id,
+            )
+        );
+    }
+
+    /**
+     * The formatted subject for the current post.
+     *
+     * @return string
+     */
+    public function get_subject() {
+        return format_string($this->post->subject, true);
+    }
+
+    /**
+     * The plaintext anchor id for the current post.
+     *
+     * @return string
+     */
+    public function get_postanchor() {
+        return 'p' . $this->post->id;
+    }
+
+    /**
+     * The name of the course that the forum is in.
+     *
+     * @return string
+     */
+    public function get_coursename() {
+        return format_string($this->course->shortname, true, array(
+            'context' => \context_course::instance($this->course->id),
+        ));
+    }
+
+    /**
+     * The name of the forum.
+     *
+     * @return string
+     */
+    public function get_forumname() {
+        return format_string($this->forum->name, true);
+    }
+
+    /**
+     * The name of the current discussion.
+     *
+     * @return string
+     */
+    public function get_discussionname() {
+        return format_string($this->discussion->name, true);
+    }
+
+    /**
+     * Whether to show the discussion name.
+     * If the forum name matches the discussion name, the discussion name
+     * is not typically displayed.
+     *
+     * @return boolean
+     */
+    public function get_showdiscussionname() {
+        return ($this->forum->name !== $this->discussion->name);
+    }
+
+    /**
+     * The fullname of the post author.
+     *
+     * @return string
+     */
+    public function get_author_fullname() {
+        return fullname($this->author, $this->viewfullnames);
+    }
+
+    /**
+     * The recipient of the post.
+     *
+     * @return string
+     */
+    protected function get_postto() {
+        global $USER;
+        if (null === $this->userto) {
+            return $USER;
+        }
+
+        return $this->userto;
+    }
+
+    /**
+     * The date of the post, formatted according to the postto user's
+     * preferences.
+     *
+     * @return string.
+     */
+    public function get_postdate() {
+        return userdate($this->post->modified, "", \core_date::get_user_timezone($this->get_postto()));
+    }
+
+    /**
+     * The HTML for the author's user picture.
+     *
+     * @return string
+     */
+    public function get_author_picture() {
+        global $OUTPUT;
+
+        return $OUTPUT->user_picture($this->author, array('courseid' => $this->course->id));
+    }
+
+    /**
+     * The HTML for a group picture.
+     *
+     * @return string
+     */
+    public function get_group_picture() {
+        if (isset($this->userfrom->groups)) {
+            $groups = $this->userfrom->groups[$this->forum->id];
+        } else {
+            $groups = groups_get_all_groups($this->course->id, $this->author->id, $this->cm->groupingid);
+        }
+
+        if ($this->get_is_firstpost()) {
+            return print_group_picture($groups, $this->course->id, false, true, true);
+        }
+    }
+}
diff --git a/mod/forum/classes/output/forum_post_email.php b/mod/forum/classes/output/forum_post_email.php
new file mode 100644 (file)
index 0000000..6e60ea1
--- /dev/null
@@ -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/>.
+
+/**
+ * Forum post renderable for e-mail.
+ *
+ * @package    mod_forum
+ * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_forum\output;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Forum post renderable for use in e-mail.
+ *
+ * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class forum_post_email extends forum_post {
+}
index 65c6052..3be31b9 100644 (file)
@@ -708,3 +708,117 @@ function forum_get_potential_subscribers($forumcontext, $groupid, $fields, $sort
 
     \mod_forum\subscriptions::get_potential_subscribers($forumcontext, $groupid, $fields, $sort);
 }
+
+/**
+ * Builds and returns the body of the email notification in plain text.
+ *
+ * @uses CONTEXT_MODULE
+ * @param object $course
+ * @param object $cm
+ * @param object $forum
+ * @param object $discussion
+ * @param object $post
+ * @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. [Since 2.8].
+ * @return string The email body in plain text format.
+ * @deprecated since Moodle 3.0 use \mod_forum\output\forum_post_email instead
+ */
+function forum_make_mail_text($course, $cm, $forum, $discussion, $post, $userfrom, $userto, $bare = false, $replyaddress = null) {
+    global $PAGE;
+    $renderable = new \mod_forum\output\forum_post_email(
+        $course,
+        $cm,
+        $forum,
+        $discussion,
+        $post,
+        $userfrom,
+        $userto,
+        forum_user_can_post($forum, $discussion, $userto, $cm, $course)
+        );
+
+    $modcontext = context_module::instance($cm->id);
+    $renderable->viewfullnames = has_capability('moodle/site:viewfullnames', $modcontext, $userto->id);
+
+    if ($bare) {
+        $renderer = $PAGE->get_renderer('mod_forum', 'emaildigestfull', 'textemail');
+    } else {
+        $renderer = $PAGE->get_renderer('mod_forum', 'email', 'textemail');
+    }
+
+    debugging("forum_make_mail_text() has been deprecated, please use the \mod_forum\output\forum_post_email renderable instead.",
+            DEBUG_DEVELOPER);
+
+    return $renderer->render($renderable);
+}
+
+/**
+ * Builds and returns the body of the email notification in html format.
+ *
+ * @param object $course
+ * @param object $cm
+ * @param object $forum
+ * @param object $discussion
+ * @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. [Since 2.8].
+ * @return string The email text in HTML format
+ * @deprecated since Moodle 3.0 use \mod_forum\output\forum_post_email instead
+ */
+function forum_make_mail_html($course, $cm, $forum, $discussion, $post, $userfrom, $userto, $replyaddress = null) {
+    return forum_make_mail_post($course,
+        $cm,
+        $forum,
+        $discussion,
+        $post,
+        $userfrom,
+        $userto,
+        forum_user_can_post($forum, $discussion, $userto, $cm, $course)
+    );
+}
+
+/**
+ * Given the data about a posting, builds up the HTML to display it and
+ * returns the HTML in a string.  This is designed for sending via HTML email.
+ *
+ * @param object $course
+ * @param object $cm
+ * @param object $forum
+ * @param object $discussion
+ * @param object $post
+ * @param object $userfrom
+ * @param object $userto
+ * @param bool $ownpost
+ * @param bool $reply
+ * @param bool $link
+ * @param bool $rate
+ * @param string $footer
+ * @return string
+ * @deprecated since Moodle 3.0 use \mod_forum\output\forum_post_email instead
+ */
+function forum_make_mail_post($course, $cm, $forum, $discussion, $post, $userfrom, $userto,
+                              $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
+    global $PAGE;
+    $renderable = new \mod_forum\output\forum_post_email(
+        $course,
+        $cm,
+        $forum,
+        $discussion,
+        $post,
+        $userfrom,
+        $userto,
+        $reply);
+
+    $modcontext = context_module::instance($cm->id);
+    $renderable->viewfullnames = has_capability('moodle/site:viewfullnames', $modcontext, $userto->id);
+
+    // Assume that this is being used as a standard forum email.
+    $renderer = $PAGE->get_renderer('mod_forum', 'email', 'htmlemail');
+
+    debugging("forum_make_mail_post() has been deprecated, please use the \mod_forum\output\forum_post_email renderable instead.",
+            DEBUG_DEVELOPER);
+
+    return $renderer->render($renderable);
+}
index e7e7da0..db074ed 100644 (file)
@@ -123,6 +123,7 @@ $string['deletesure'] = 'Are you sure you want to delete this post?';
 $string['deletesureplural'] = 'Are you sure you want to delete this post and all replies? ({$a} posts)';
 $string['digestmailheader'] = 'This is your daily digest of new posts from the {$a->sitename} forums. To change your default forum email preferences, go to {$a->userprefs}.';
 $string['digestmailpost'] = 'Change your forum digest preferences';
+$string['digestmailpostlink'] = 'Change your forum digest preferences: {$a}';
 $string['digestmailprefs'] = 'your user profile';
 $string['digestmailsubject'] = '{$a}: forum digest';
 $string['digestmailtime'] = 'Hour to send digest emails';
@@ -392,6 +393,9 @@ $string['postincontext'] = 'See this post in context';
 $string['postmailinfo'] = 'This is a copy of a message posted on the {$a} website.
 
 To reply click on this link:';
+$string['postmailinfolink'] = 'This is a copy of a message posted on the {$a->coursename} website.
+
+To reply click on this link: {$a->replylink}';
 $string['postmailnow'] = '<p>This post will be mailed out immediately to all forum subscribers.</p>';
 $string['postmailsubject'] = '{$a->courseshortname}: {$a->subject}';
 $string['postrating1'] = 'Mostly separate knowing';
@@ -503,7 +507,9 @@ $string['unreadposts'] = 'Unread posts';
 $string['unreadpostsnumber'] = '{$a} unread posts';
 $string['unreadpostsone'] = '1 unread post';
 $string['unsubscribe'] = 'Unsubscribe from this forum';
+$string['unsubscribelink'] = 'Unsubscribe from this forum: {$a}';
 $string['unsubscribediscussion'] = 'Unsubscribe from this discussion';
+$string['unsubscribediscussionlink'] = 'Unsubscribe from this discussion: {$a}';
 $string['unsubscribeall'] = 'Unsubscribe from all forums';
 $string['unsubscribeallconfirm'] = 'You are currently subscribed to {$a->forums} forums, and {$a->discussions} discussions. Do you really want to unsubscribe from all forums and discussions, and disable discussion auto-subscription?';
 $string['unsubscribeallconfirmforums'] = 'You are currently subscribed to {$a->forums} forums. Do you really want to unsubscribe from all forums and disable discussion auto-subscription?';
index 842310a..4a3d2d5 100644 (file)
@@ -443,10 +443,18 @@ function forum_cron_minimise_user_record(stdClass $user) {
  * @todo MDL-44734 The function will be split up into seperate tasks.
  */
 function forum_cron() {
-    global $CFG, $USER, $DB;
+    global $CFG, $USER, $DB, $PAGE;
 
     $site = get_site();
 
+    // The main renderers.
+    $htmlout = $PAGE->get_renderer('mod_forum', 'email', 'htmlemail');
+    $textout = $PAGE->get_renderer('mod_forum', 'email', 'textemail');
+    $htmldigestfullout = $PAGE->get_renderer('mod_forum', 'emaildigestfull', 'htmlemail');
+    $textdigestfullout = $PAGE->get_renderer('mod_forum', 'emaildigestfull', 'textemail');
+    $htmldigestbasicout = $PAGE->get_renderer('mod_forum', 'emaildigestbasic', 'htmlemail');
+    $textdigestbasicout = $PAGE->get_renderer('mod_forum', 'emaildigestbasic', 'textemail');
+
     // All users that are subscribed to any post that needs sending,
     // please increase $CFG->extramemorylimit on large sites that
     // send notifications to a large number of users.
@@ -762,15 +770,34 @@ function forum_cron() {
                     $replyaddress = $messageinboundgenerator->generate($userto->id);
                 }
 
+                if (!isset($userto->canpost[$discussion->id])) {
+                    $canreply = forum_user_can_post($forum, $discussion, $userto, $cm, $course, $modcontext);
+                } else {
+                    $canreply = $userto->canpost[$discussion->id];
+                }
+
+                $data = new \mod_forum\output\forum_post_email(
+                        $course,
+                        $cm,
+                        $forum,
+                        $discussion,
+                        $post,
+                        $userfrom,
+                        $userto,
+                        $canreply
+                    );
+
+                if (!isset($userto->viewfullnames[$forum->id])) {
+                    $data->viewfullnames = has_capability('moodle/site:viewfullnames', $modcontext, $userto->id);
+                } else {
+                    $data->viewfullnames = $userto->viewfullnames[$forum->id];
+                }
+
                 $a = new stdClass();
-                $a->courseshortname = $shortname;
+                $a->courseshortname = $data->get_coursename();
                 $a->forumname = $cleanforumname;
-                $a->subject = format_string($post->subject, true);
+                $a->subject = $data->get_subject();
                 $postsubject = html_to_text(get_string('postmailsubject', 'forum', $a), 0);
-                $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 ', '');
@@ -781,9 +808,9 @@ function forum_cron() {
                 $eventdata->userfrom            = $userfrom;
                 $eventdata->userto              = $userto;
                 $eventdata->subject             = $postsubject;
-                $eventdata->fullmessage         = $posttext;
+                $eventdata->fullmessage         = $textout->render($data);
                 $eventdata->fullmessageformat   = FORMAT_PLAIN;
-                $eventdata->fullmessagehtml     = $posthtml;
+                $eventdata->fullmessagehtml     = $htmlout->render($data);
                 $eventdata->notification        = 1;
                 $eventdata->replyto             = $replyaddress;
                 if (!empty($replyaddress)) {
@@ -1072,25 +1099,39 @@ function forum_cron() {
                             );
 
                         $maildigest = forum_get_user_maildigest_bulk($digests, $userto, $forum->id);
-                        if ($maildigest == 2) {
-                            // Subjects and link only
-                            $posttext .= "\n";
-                            $posttext .= $CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->id;
-                            $by = new stdClass();
-                            $by->name = fullname($userfrom);
-                            $by->date = userdate($post->modified);
-                            $posttext .= "\n".format_string($post->subject,true).' '.get_string("bynameondate", "forum", $by);
-                            $posttext .= "\n---------------------------------------------------------------------";
-
-                            $by->name = "<a target=\"_blank\" href=\"$CFG->wwwroot/user/view.php?id=$userfrom->id&amp;course=$course->id\">$by->name</a>";
-                            $posthtml .= '<div><a target="_blank" href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->id.'#p'.$post->id.'">'.format_string($post->subject,true).'</a> '.get_string("bynameondate", "forum", $by).'</div>';
+                        if (!isset($userto->canpost[$discussion->id])) {
+                            $canreply = forum_user_can_post($forum, $discussion, $userto, $cm, $course, $modcontext);
+                        } else {
+                            $canreply = $userto->canpost[$discussion->id];
+                        }
+
+                        $data = new \mod_forum\output\forum_post_email(
+                                $course,
+                                $cm,
+                                $forum,
+                                $discussion,
+                                $post,
+                                $userfrom,
+                                $userto,
+                                $canreply
+                            );
+
+                        if (!isset($userto->viewfullnames[$forum->id])) {
+                            $data->viewfullnames = has_capability('moodle/site:viewfullnames', $modcontext, $userto->id);
+                        } else {
+                            $data->viewfullnames = $userto->viewfullnames[$forum->id];
+                        }
 
+                        if ($maildigest == 2) {
+                            // Subjects and link only.
+                            $posttext .= $textdigestbasicout->render($data);
+                            $posthtml .= $htmldigestbasicout->render($data);
                         } else {
-                            // The full treatment
-                            $posttext .= forum_make_mail_text($course, $cm, $forum, $discussion, $post, $userfrom, $userto, true);
-                            $posthtml .= forum_make_mail_post($course, $cm, $forum, $discussion, $post, $userfrom, $userto, false, $canreply, true, false);
+                            // The full treatment.
+                            $posttext .= $textdigestfullout->render($data);
+                            $posthtml .= $htmldigestfullout->render($data);
 
-                        // Create an array of postid's for this user to mark as read.
+                            // Create an array of postid's for this user to mark as read.
                             if (!$CFG->forum_usermarksread) {
                                 $userto->markposts[$post->id] = $post->id;
                             }
@@ -1154,178 +1195,6 @@ function forum_cron() {
     return true;
 }
 
-/**
- * Builds and returns the body of the email notification in plain text.
- *
- * @global object
- * @global object
- * @uses CONTEXT_MODULE
- * @param object $course
- * @param object $cm
- * @param object $forum
- * @param object $discussion
- * @param object $post
- * @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. [Since 2.8].
- * @return string The email body in plain text format.
- */
-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);
-
-    if (!isset($userto->viewfullnames[$forum->id])) {
-        $viewfullnames = has_capability('moodle/site:viewfullnames', $modcontext, $userto->id);
-    } else {
-        $viewfullnames = $userto->viewfullnames[$forum->id];
-    }
-
-    if (!isset($userto->canpost[$discussion->id])) {
-        $canreply = forum_user_can_post($forum, $discussion, $userto, $cm, $course, $modcontext);
-    } else {
-        $canreply = $userto->canpost[$discussion->id];
-    }
-
-    $by = New stdClass;
-    $by->name = fullname($userfrom, $viewfullnames);
-    $by->date = userdate($post->modified, "", core_date::get_user_timezone($userto));
-
-    $strbynameondate = get_string('bynameondate', 'forum', $by);
-
-    $strforums = get_string('forums', 'forum');
-
-    $canunsubscribe = !\mod_forum\subscriptions::is_forcesubscribed($forum);
-
-    $posttext = '';
-
-    if (!$bare) {
-        $shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
-        $posttext  .= "$shortname -> $strforums -> ".format_string($forum->name,true);
-
-        if ($discussion->name != $forum->name) {
-            $posttext  .= " -> ".format_string($discussion->name,true);
-        }
-    }
-
-    // add absolute file links
-    $post->message = file_rewrite_pluginfile_urls($post->message, 'pluginfile.php', $modcontext->id, 'mod_forum', 'post', $post->id);
-
-    $posttext .= "\n";
-    $posttext .= $CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->id;
-    $posttext .= "\n";
-    $posttext .= format_string($post->subject,true);
-    if ($bare) {
-        $posttext .= " ($CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id#p$post->id)";
-    }
-    $posttext .= "\n".$strbynameondate."\n";
-    $posttext .= "---------------------------------------------------------------------\n";
-    $posttext .= format_text_email($post->message, $post->messageformat);
-    $posttext .= "\n\n";
-    $posttext .= forum_print_attachments($post, $cm, "text");
-    $posttext .= "\n---------------------------------------------------------------------\n";
-
-    if (!$bare) {
-        if ($canreply) {
-            $posttext .= get_string("postmailinfo", "forum", $shortname)."\n";
-            $posttext .= "$CFG->wwwroot/mod/forum/post.php?reply=$post->id\n";
-        }
-
-        if ($canunsubscribe) {
-            if (\mod_forum\subscriptions::is_subscribed($userto->id, $forum, null, $cm)) {
-                // If subscribed to this forum, offer the unsubscribe link.
-                $posttext .= get_string("unsubscribe", "forum");
-                $posttext .= ": $CFG->wwwroot/mod/forum/subscribe.php?id=$forum->id\n";
-            }
-            // Always offer the unsubscribe from discussion link.
-            $posttext .= get_string("unsubscribediscussion", "forum");
-            $posttext .= ": $CFG->wwwroot/mod/forum/subscribe.php?id=$forum->id&d=$discussion->id\n";
-        }
-    }
-
-    $posttext .= get_string("digestmailpost", "forum");
-    $posttext .= ": {$CFG->wwwroot}/mod/forum/index.php?id={$forum->course}\n";
-
-    return $posttext;
-}
-
-/**
- * Builds and returns the body of the email notification in html format.
- *
- * @global object
- * @param object $course
- * @param object $cm
- * @param object $forum
- * @param object $discussion
- * @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. [Since 2.8].
- * @return string The email text in HTML format
- */
-function forum_make_mail_html($course, $cm, $forum, $discussion, $post, $userfrom, $userto, $replyaddress = null) {
-    global $CFG;
-
-    if ($userto->mailformat != 1) {  // Needs to be HTML
-        return '';
-    }
-
-    if (!isset($userto->canpost[$discussion->id])) {
-        $canreply = forum_user_can_post($forum, $discussion, $userto, $cm, $course);
-    } else {
-        $canreply = $userto->canpost[$discussion->id];
-    }
-
-    $strforums = get_string('forums', 'forum');
-    $canunsubscribe = ! \mod_forum\subscriptions::is_forcesubscribed($forum);
-    $shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
-
-    $posthtml = '<head>';
-/*    foreach ($CFG->stylesheets as $stylesheet) {
-        //TODO: MDL-21120
-        $posthtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n";
-    }*/
-    $posthtml .= '</head>';
-    $posthtml .= "\n<body id=\"email\">\n\n";
-
-    $posthtml .= '<div class="navbar">'.
-    '<a target="_blank" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.$shortname.'</a> &raquo; '.
-    '<a target="_blank" href="'.$CFG->wwwroot.'/mod/forum/index.php?id='.$course->id.'">'.$strforums.'</a> &raquo; '.
-    '<a target="_blank" href="'.$CFG->wwwroot.'/mod/forum/view.php?f='.$forum->id.'">'.format_string($forum->name,true).'</a>';
-    if ($discussion->name == $forum->name) {
-        $posthtml .= '</div>';
-    } else {
-        $posthtml .= ' &raquo; <a target="_blank" href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->id.'">'.
-                     format_string($discussion->name,true).'</a></div>';
-    }
-    $posthtml .= forum_make_mail_post($course, $cm, $forum, $discussion, $post, $userfrom, $userto, false, $canreply, true, false);
-
-    $footerlinks = array();
-    if ($canunsubscribe) {
-        if (\mod_forum\subscriptions::is_subscribed($userto->id, $forum, null, $cm)) {
-            // If subscribed to this forum, offer the unsubscribe link.
-            $unsublink = new moodle_url('/mod/forum/subscribe.php', array('id' => $forum->id));
-            $footerlinks[] = html_writer::link($unsublink, get_string('unsubscribe', 'mod_forum'));
-        }
-        // Always offer the unsubscribe from discussion link.
-        $unsublink = new moodle_url('/mod/forum/subscribe.php', array(
-                'id' => $forum->id,
-                'd' => $discussion->id,
-            ));
-        $footerlinks[] = html_writer::link($unsublink, get_string('unsubscribediscussion', 'mod_forum'));
-
-        $footerlinks[] = '<a href="' . $CFG->wwwroot . '/mod/forum/unsubscribeall.php">' . get_string('unsubscribeall', 'forum') . '</a>';
-    }
-    $footerlinks[] = "<a href='{$CFG->wwwroot}/mod/forum/index.php?id={$forum->course}'>" . get_string('digestmailpost', 'forum') . '</a>';
-    $posthtml .= '<hr /><div class="mdl-align unsubscribelink">' . implode('&nbsp;', $footerlinks) . '</div>';
-
-    $posthtml .= '</body>';
-
-    return $posthtml;
-}
-
-
 /**
  *
  * @param object $course
@@ -3041,126 +2910,6 @@ function forum_get_course_forum($courseid, $type) {
     return $DB->get_record("forum", array("id" => "$forum->id"));
 }
 
-
-/**
- * Given the data about a posting, builds up the HTML to display it and
- * returns the HTML in a string.  This is designed for sending via HTML email.
- *
- * @global object
- * @param object $course
- * @param object $cm
- * @param object $forum
- * @param object $discussion
- * @param object $post
- * @param object $userform
- * @param object $userto
- * @param bool $ownpost
- * @param bool $reply
- * @param bool $link
- * @param bool $rate
- * @param string $footer
- * @return string
- */
-function forum_make_mail_post($course, $cm, $forum, $discussion, $post, $userfrom, $userto,
-                              $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
-
-    global $CFG, $OUTPUT;
-
-    $modcontext = context_module::instance($cm->id);
-
-    if (!isset($userto->viewfullnames[$forum->id])) {
-        $viewfullnames = has_capability('moodle/site:viewfullnames', $modcontext, $userto->id);
-    } else {
-        $viewfullnames = $userto->viewfullnames[$forum->id];
-    }
-
-    // add absolute file links
-    $post->message = file_rewrite_pluginfile_urls($post->message, 'pluginfile.php', $modcontext->id, 'mod_forum', 'post', $post->id);
-
-    // format the post body
-    $options = new stdClass();
-    $options->para = true;
-    $formattedtext = format_text($post->message, $post->messageformat, $options, $course->id);
-
-    $output = '<table border="0" cellpadding="3" cellspacing="0" class="forumpost">';
-
-    $output .= '<tr class="header"><td width="35" valign="top" class="picture left">';
-    $output .= $OUTPUT->user_picture($userfrom, array('courseid'=>$course->id));
-    $output .= '</td>';
-
-    if ($post->parent) {
-        $output .= '<td class="topic">';
-    } else {
-        $output .= '<td class="topic starter">';
-    }
-    $output .= '<div class="subject">'.format_string($post->subject).'</div>';
-
-    $fullname = fullname($userfrom, $viewfullnames);
-    $by = new stdClass();
-    $by->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$userfrom->id.'&amp;course='.$course->id.'">'.$fullname.'</a>';
-    $by->date = userdate($post->modified, '', core_date::get_user_timezone($userto));
-    $output .= '<div class="author">'.get_string('bynameondate', 'forum', $by).'</div>';
-
-    $output .= '</td></tr>';
-
-    $output .= '<tr><td class="left side" valign="top">';
-
-    if (isset($userfrom->groups)) {
-        $groups = $userfrom->groups[$forum->id];
-    } else {
-        $groups = groups_get_all_groups($course->id, $userfrom->id, $cm->groupingid);
-    }
-
-    if ($groups) {
-        $output .= print_group_picture($groups, $course->id, false, true, true);
-    } else {
-        $output .= '&nbsp;';
-    }
-
-    $output .= '</td><td class="content">';
-
-    $attachments = forum_print_attachments($post, $cm, 'html');
-    if ($attachments !== '') {
-        $output .= '<div class="attachments">';
-        $output .= $attachments;
-        $output .= '</div>';
-    }
-
-    $output .= $formattedtext;
-
-// Commands
-    $commands = array();
-
-    if ($post->parent) {
-        $commands[] = '<a target="_blank" href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.
-                      $post->discussion.'&amp;parent='.$post->parent.'">'.get_string('parent', 'forum').'</a>';
-    }
-
-    if ($reply) {
-        $commands[] = '<a target="_blank" href="'.$CFG->wwwroot.'/mod/forum/post.php?reply='.$post->id.'">'.
-                      get_string('reply', 'forum').'</a>';
-    }
-
-    $output .= '<div class="commands">';
-    $output .= implode(' | ', $commands);
-    $output .= '</div>';
-
-// Context link to post if required
-    if ($link) {
-        $output .= '<div class="link">';
-        $output .= '<a target="_blank" href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id.'">'.
-                     get_string('postincontext', 'forum').'</a>';
-        $output .= '</div>';
-    }
-
-    if ($footer) {
-        $output .= '<div class="footer">'.$footer.'</div>';
-    }
-    $output .= '</td></tr></table>'."\n\n";
-
-    return $output;
-}
-
 /**
  * Print a forum post
  *
index c38ebc9..ee2d4c5 100644 (file)
@@ -183,4 +183,24 @@ class mod_forum_renderer extends plugin_renderer_base {
         $tooltip = implode("\n", $dates);
         return $this->pix_icon('i/calendar', $tooltip, 'moodle', array('class' => 'smallicon timedpost'));
     }
+
+    /**
+     * Display a forum post in the relevant context.
+     *
+     * @param \mod_forum\output\forum_post $post The post to display.
+     * @return string
+     */
+    public function render_forum_post_email(\mod_forum\output\forum_post_email $post) {
+        $data = $post->export_for_template($this);
+        return $this->render_from_template('mod_forum/' . $this->forum_post_template(), $data);
+    }
+
+    /**
+     * The template name for this renderer.
+     *
+     * @return string
+     */
+    public function forum_post_template() {
+        return 'forum_post';
+    }
 }
diff --git a/mod/forum/templates/forum_post_email_htmlemail.mustache b/mod/forum/templates/forum_post_email_htmlemail.mustache
new file mode 100644 (file)
index 0000000..bb32eac
--- /dev/null
@@ -0,0 +1,161 @@
+{{!
+    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 mod_forum/forum_post_email_htmlemail
+
+    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
+
+    Context variables required for this template:
+    * courselink
+    * coursename
+    * forumindexlink
+    * forumviewlink
+    * forumname
+    * discussionlink
+    * discussionname
+    * showdiscussionname
+    * firstpost
+    * subject
+    * authorlink
+    * authorpicture
+    * authorfullname
+    * postdate
+    * grouppicture
+    * attachments
+    * message
+    * parentpostlink
+    * canreply
+    * replylink
+    * permalink
+    * unsubscribeforumlink
+    * unsubscribediscussionlink
+
+    Example context (json):
+    {
+        "courselink": "https://example.com/course/view.php?id=2",
+        "coursename": "Example course",
+        "forumindexlink": "https://example.com/mod/forum/index.php?id=2",
+        "forumviewlink": "https://example.com/mod/forum/view.php?f=2",
+        "forumname": "Lorem ipsum dolor",
+        "discussionlink": "https://example.com/mod/forum/discuss.php?d=70",
+        "discussionname": "Is Lorem ipsum Latin?",
+        "showdiscussionname": 1,
+        "firstpost": 1,
+        "subject": "Is Lorem ipsum Latin?",
+        "authorlink": "https://example.com/user/view.php?id=2&course=2",
+        "authorpicture": "<a href=\"https://example.com/user/view.php?id=2&amp;course=6\"><img src=\"https://example.com/theme/image.php?theme=clean&amp;component=core&amp;image=u%2Ff2&amp;svg=0\" alt=\"Picture of Admin User\" title=\"Picture of Admin User\" class=\"userpicture defaultuserpic\" width=\"35\" height=\"35\" /></a>",
+        "authorfullname": "Lucius Caecilius lucundus",
+        "postdate": "Sunday, 13 September 2015, 2:22 pm",
+        "grouppicture": "",
+        "attachments": "",
+        "message": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum et auctor libero. Quisque porta egestas neque, et bibendum libero dignissim at. Nulla facilisi. Morbi eget accumsan felis. Nunc et vulputate odio, vel venenatis nisl. Nunc maximus ipsum sed tincidunt mollis. Integer nunc erat, luctus sit amet arcu tincidunt, volutpat dignissim mi. Sed ut magna quam.  Mauris accumsan porta turpis sed aliquam. Etiam at justo tristique, imperdiet augue quis, consectetur sapien. Ut nec erat malesuada sem suscipit lobortis. Vivamus posuere nibh eu ipsum porta fringilla.  Sed vitae dapibus ipsum, ac condimentum enim. Sed dignissim ante at elit mollis, ac tempor lacus iaculis. Etiam nec lectus vitae nibh vulputate volutpat. Nulla quis tellus aliquam, commodo nisi et, dictum est.</p><p><br /></p>",
+        "parentpostlink": "",
+        "canreply": 1,
+        "replylink": "https://example.com/mod/forum/post.php?reply=2",
+        "permalink": "https://example.com/mod/forum/discuss.php?d=2#2",
+        "unsubscribeforumlink": "https://example.com/mod/forum/subscribe.php?id=2",
+        "unsubscribediscussionlink": "https://example.com/mod/discussion/subscribe.php?id=2&d=2"
+    }
+}}
+<head>
+</head>
+<body id="email">
+<div class="navbar">
+    <a target="_blank" href="{{{ courselink }}}">{{ coursename }}</a>
+    &raquo;
+    <a target="_blank" href="{{{ forumindexlink }}}">{{# str }} forums, forum {{/ str }}</a>
+    &raquo;
+    <a target="_blank" href="{{{ forumviewlink }}}">{{ forumname }}</a>
+{{# showdiscussionname }}
+    &raquo;
+    <a target="_blank" href="{{{ discussionlink }}}">{{ discussionname }}</a>
+{{/ showdiscussionname }}
+</div>
+
+<table border="0" cellpadding="3" cellspacing="0" class="forumpost">
+    <tr class="header">
+        <td width="35" valign="top" class="picture left">
+            {{{ authorpicture }}}
+        </td>
+        <td class="topic {{# firstpost }}starter{{/ firstpost }}">
+            <div class="subject">
+                {{ subject }}
+            </div>
+            <div class="author">
+                {{# str }} bynameondate, forum, { "name": "<a target='_blank' href='{{{ authorlink }}}'>{{ authorfullname }}</a>", "date": "{{ postdate }}" } {{/ str }}
+            </div>
+        </td>
+    </tr>
+    <tr>
+        <td class="left side" valign="top">
+            {{# grouppicture }}
+                {{{ grouppicture }}}
+            {{/ grouppicture }}
+            {{^ grouppicture }}
+                &nbsp;
+            {{/ grouppicture }}
+        </td>
+        <td class="content">
+            {{# attachments }}
+                <div class="attachments">
+                    {{ attachments }}
+                </div>
+            {{/ attachments }}
+            {{{ message }}}
+
+            <div class="commands">
+                {{^ firstpost }}
+                    <a target="_blank" href="{{{ parentpostlink }}}">
+                        {{# str }} parent, forum {{/ str }}
+                    </a>
+                    {{# canreply }}
+                        |
+                    {{/ canreply }}
+                {{/ firstpost }}
+                {{# canreply }}
+                    <a target="_blank" href="{{{ replylink }}}">
+                        {{# str }} reply, forum {{/ str }}
+                    </a>
+                {{/ canreply }}
+            </div>
+
+            <div class="link">
+                <a target="_blank" href="{{{ permalink }}}">
+                    {{# str }} postincontext, forum {{/ str }}
+                </a>
+            </div>
+        </td>
+    </tr>
+</table>
+
+<hr />
+<div class="mdl-align unsubscribelink">
+{{# unsubscribeforumlink }}
+    <a href="{{{ unsubscribeforumlink }}}">{{# str }} unsubscribe, forum {{/ str }}</a>&nbsp;
+{{/ unsubscribeforumlink }}
+{{# unsubscribediscussionlink }}
+    <a href="{{{ unsubscribediscussionlink }}}">{{# str }} unsubscribediscussion, forum {{/ str }}</a>&nbsp;
+{{/ unsubscribediscussionlink }}
+    <a href="{{{ forumindexlink }}}">{{# str }} digestmailpost, forum {{/ str }}</a>
+</div>
+</body>
diff --git a/mod/forum/templates/forum_post_email_textemail.mustache b/mod/forum/templates/forum_post_email_textemail.mustache
new file mode 100644 (file)
index 0000000..6c3fed9
--- /dev/null
@@ -0,0 +1,63 @@
+{{!
+    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 mod_forum/forum_post_email_textemail
+
+    Template which defines a forum post for sending in a single-post text email.
+
+    Classes required for JS:
+    * none
+
+    Data attributes required for JS:
+    * none
+
+    Context variables required for this template:
+    * coursename
+    * forumname
+    * showdiscussionname
+    * discussionname
+    * discussionlink
+    * subject
+    * authorfullname
+    * postdate
+    * message
+    * attachments
+    * canreply
+    * replylink
+    * unsubscribeforumlink
+    * unsubscribediscussionlink
+    * forumindexlink
+}}
+{{ coursename }} -> {{# str }} forums, forum {{/ str }} -> {{ forumname }}{{# showdiscussionname }} -> {{ discussionname }} {{/ showdiscussionname }}
+{{ discussionlink }}
+{{ subject }}
+{{# str }} bynameondate, forum, { "name": "{{ authorfullname }}", "date": "{{ postdate }}" } {{/ str }}
+---------------------------------------------------------------------
+{{ message }}
+
+{{ attachments }}
+---------------------------------------------------------------------
+{{# canreply }}
+{{# str }} postmailinfolink, forum, { "coursename": "{{ coursename }}", "replylink": "{{ replylink }}" } {{/ str }}
+{{/ canreply }}
+{{# unsubscribeforumlink }}
+{{# str }} unsubscribelink, forum, {{{ unsubscribeforumlink }}} {{/ str }}
+{{/ unsubscribeforumlink }}
+{{# unsubscribediscussionlink }}
+{{# str }} unsubscribediscussionlink, forum, {{{ unsubscribediscussionlink }}} {{/ str }}
+{{/ unsubscribediscussionlink }}
+{{# str }} digestmailpostlink, forum, {{{ forumindexlink }}} {{/ str }}
diff --git a/mod/forum/templates/forum_post_emaildigestbasic_htmlemail.mustache b/mod/forum/templates/forum_post_emaildigestbasic_htmlemail.mustache
new file mode 100644 (file)
index 0000000..444d4c7
--- /dev/null
@@ -0,0 +1,50 @@
+{{!
+    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 mod_forum/forum_post_emaildigestbasic_htmlemail
+
+    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
+
+    Context variables required for this template:
+    * permalink
+    * subject
+    * authorlink
+    * authorfullname
+    * postdate
+
+    Example context (json):
+    {
+        "permalink": "https://example.com/mod/forum/discuss.php?d=2#2",
+        "subject": "Is Lorem ipsum Latin?",
+        "authorlink": "https://example.com/user/view.php?id=2&course=2",
+        "authorfullname": "Lucius Caecilius lucundus",
+        "postdate": "Sunday, 13 September 2015, 2:22 pm"
+    }
+}}
+<div>
+  <a target="_blank" href="{{{ permalink }}}">{{ subject }}</a>
+  {{# str }} bynameondate, forum, {
+      "name": "<a target=\"_blank\" href=\"{{{ authorlink }}}\">{{ authorfullname }}</a>",
+      "date": "{{ postdate }}"
+  } {{/ str }}
+</div>';
diff --git a/mod/forum/templates/forum_post_emaildigestbasic_textemail.mustache b/mod/forum/templates/forum_post_emaildigestbasic_textemail.mustache
new file mode 100644 (file)
index 0000000..4d4beb2
--- /dev/null
@@ -0,0 +1,37 @@
+{{!
+    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 mod_forum/forum_post_emaildigestbasic_textemail
+
+    Template which defines a forum post for sending in a single-post text email.
+
+    Classes required for JS:
+    * none
+
+    Data attributes required for JS:
+    * none
+
+    Context variables required for this template:
+    * discussionlink
+    * subject
+    * authorfullname
+    * postdate
+}}
+
+{{ discussionlink }}
+{{ subject }} {{# str }} bynameondate, forum, { "name": "{{ authorfullname }}", "date": "{{ postdate }}" } {{/ str }}
+---------------------------------------------------------------------
diff --git a/mod/forum/templates/forum_post_emaildigestfull_htmlemail.mustache b/mod/forum/templates/forum_post_emaildigestfull_htmlemail.mustache
new file mode 100644 (file)
index 0000000..5b2240f
--- /dev/null
@@ -0,0 +1,80 @@
+{{!
+    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 mod_forum/forum_post_emaildigestfull_htmlemail
+
+    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
+
+    Context variables required for this template:
+    * courselink
+    * coursename
+    * forumindexlink
+    * forumviewlink
+    * forumname
+    * discussionlink
+    * discussionname
+    * showdiscussionname
+    * firstpost
+    * subject
+    * authorlink
+    * authorpicture
+    * authorfullname
+    * postdate
+    * grouppicture
+    * attachments
+    * message
+    * parentpostlink
+    * canreply
+    * replylink
+    * permalink
+    * unsubscribeforumlink
+    * unsubscribediscussionlink
+
+    Example context (json):
+    {
+        "courselink": "https://example.com/course/view.php?id=2",
+        "coursename": "Example course",
+        "forumindexlink": "https://example.com/mod/forum/index.php?id=2",
+        "forumviewlink": "https://example.com/mod/forum/view.php?f=2",
+        "forumname": "Lorem ipsum dolor",
+        "discussionlink": "https://example.com/mod/forum/discuss.php?d=70",
+        "discussionname": "Is Lorem ipsum Latin?",
+        "showdiscussionname": 1,
+        "firstpost": 1,
+        "subject": "Is Lorem ipsum Latin?",
+        "authorlink": "https://example.com/user/view.php?id=2&course=2",
+        "authorpicture": "<a href=\"https://example.com/user/view.php?id=2&amp;course=6\"><img src=\"https://example.com/theme/image.php?theme=clean&amp;component=core&amp;image=u%2Ff2&amp;svg=0\" alt=\"Picture of Admin User\" title=\"Picture of Admin User\" class=\"userpicture defaultuserpic\" width=\"35\" height=\"35\" /></a>",
+        "authorfullname": "Lucius Caecilius lucundus",
+        "postdate": "Sunday, 13 September 2015, 2:22 pm",
+        "grouppicture": "",
+        "attachments": "",
+        "message": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum et auctor libero. Quisque porta egestas neque, et bibendum libero dignissim at. Nulla facilisi. Morbi eget accumsan felis. Nunc et vulputate odio, vel venenatis nisl. Nunc maximus ipsum sed tincidunt mollis. Integer nunc erat, luctus sit amet arcu tincidunt, volutpat dignissim mi. Sed ut magna quam.  Mauris accumsan porta turpis sed aliquam. Etiam at justo tristique, imperdiet augue quis, consectetur sapien. Ut nec erat malesuada sem suscipit lobortis. Vivamus posuere nibh eu ipsum porta fringilla.  Sed vitae dapibus ipsum, ac condimentum enim. Sed dignissim ante at elit mollis, ac tempor lacus iaculis. Etiam nec lectus vitae nibh vulputate volutpat. Nulla quis tellus aliquam, commodo nisi et, dictum est.</p><p><br /></p>",
+        "parentpostlink": "",
+        "canreply": 1,
+        "replylink": "https://example.com/mod/forum/post.php?reply=2",
+        "permalink": "https://example.com/mod/forum/discuss.php?d=2#2",
+        "unsubscribeforumlink": "https://example.com/mod/forum/subscribe.php?id=2",
+        "unsubscribediscussionlink": "https://example.com/mod/discussion/subscribe.php?id=2&d=2"
+    }
+}}
+{{> mod_forum/forum_post_email_htmlemail }}
diff --git a/mod/forum/templates/forum_post_emaildigestfull_textemail.mustache b/mod/forum/templates/forum_post_emaildigestfull_textemail.mustache
new file mode 100644 (file)
index 0000000..7ef386d
--- /dev/null
@@ -0,0 +1,47 @@
+{{!
+    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 mod_forum/forum_post_emaildigestfull_textemail
+
+    Template which defines a forum post for sending in a single-post text email.
+
+    Classes required for JS:
+    * none
+
+    Data attributes required for JS:
+    * none
+
+    Context variables required for this template:
+    * discussionlink
+    * subject
+    * permalink
+    * authorfullname
+    * postdate
+    * message
+    * attachments
+    * forumindexlink
+}}
+
+{{ discussionlink }}
+{{ subject }} ({{{ permalink }}})
+{{# str }} bynameondate, forum, { "name": "{{ authorfullname }}", "date": "{{ postdate }}" } {{/ str }}
+---------------------------------------------------------------------
+{{ message }}
+
+{{ attachments }}
+---------------------------------------------------------------------
+{{# str }} digestmailpostlink, forum, {{{ forumindexlink }}} {{/ str }}