Skip to content

Commit 107b017

Browse files
Send mail to poster upon rejection of their post
1 parent 2e5ff48 commit 107b017

8 files changed

+291
-13
lines changed

classes/output/email/renderer.php

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public function moodleoverflow_email_template() {
5555
* @return string
5656
*/
5757
public function format_message_text($cm, $post) {
58+
global $CFG;
59+
include_once($CFG->libdir . '/filelib.php');
5860

5961
// Convert the message.
6062
$message = file_rewrite_pluginfile_urls(

classes/output/email/renderer_textemail.php

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public function moodleoverflow_email_template() {
5151
* @return string
5252
*/
5353
public function format_message_text($cm, $post) {
54+
global $CFG;
55+
include_once($CFG->libdir . '/filelib.php');
5456

5557
// Format the text.
5658
$message = file_rewrite_pluginfile_urls($post->message, 'pluginfile.php',

externallib.php

+45-10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2323
*/
2424

25+
use mod_moodleoverflow\output\moodleoverflow_email;
2526
use mod_moodleoverflow\review;
2627

2728
defined('MOODLE_INTERNAL') || die;
@@ -235,21 +236,20 @@ public static function review_reject_post_returns() {
235236
* @return string|null Url of next post to review.
236237
*/
237238
public static function review_reject_post($postid, $reason = null) {
238-
global $DB;
239+
global $DB, $PAGE, $OUTPUT;
239240

240241
$params = self::validate_parameters(self::review_reject_post_parameters(), ['postid' => $postid, 'reason' => $reason]);
241242
$postid = $params['postid'];
242243

243244
$post = $DB->get_record('moodleoverflow_posts', ['id' => $postid], '*', MUST_EXIST);
244-
$moodleoverflow = $DB->get_record_sql(
245-
'SELECT m.* FROM {moodleoverflow} m ' .
246-
'JOIN {moodleoverflow_discussions} d ON d.moodleoverflow = m.id ' .
247-
'WHERE d.id = :discussionid',
248-
['discussionid' => $post->discussion], MUST_EXIST
249-
);
245+
$discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $post->discussion], '*', MUST_EXIST);
246+
$moodleoverflow = $DB->get_record('moodleoverflow', ['id' => $discussion->moodleoverflow], '*', MUST_EXIST);
250247
$cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id);
248+
$course = get_course($cm->course);
251249
$context = context_module::instance($cm->id);
252250

251+
$PAGE->set_context($context);
252+
253253
require_capability('mod/moodleoverflow:reviewpost', $context);
254254

255255
if ($post->reviewed) {
@@ -261,11 +261,46 @@ public static function review_reject_post($postid, $reason = null) {
261261
}
262262

263263
// Has to be done before deleting the post.
264-
$url = review::get_first_review_post($post->id);
264+
$rendererhtml = $PAGE->get_renderer('mod_moodleoverflow', 'email', 'htmlemail');
265+
$renderertext = $PAGE->get_renderer('mod_moodleoverflow', 'email', 'textemail');
266+
267+
$userto = core_user::get_user($post->userid);
268+
269+
$maildata = new moodleoverflow_email(
270+
$course,
271+
$cm,
272+
$moodleoverflow,
273+
$discussion,
274+
$post,
275+
$userto,
276+
$userto,
277+
false
278+
);
265279

266-
moodleoverflow_delete_post($post, true, $cm, $moodleoverflow);
280+
$textcontext = $maildata->export_for_template($renderertext, true);
281+
$htmlcontext = $maildata->export_for_template($rendererhtml, false);
267282

268-
// email_to_user(core_user::get_user($post->userid), core_user::get_noreply_user(), )
283+
if ($reason) {
284+
$htmlcontext['reason'] = format_text_email($reason, FORMAT_PLAIN);
285+
$textcontext['reason'] = $htmlcontext['reason'];
286+
}
287+
288+
email_to_user(
289+
$userto,
290+
\core_user::get_noreply_user(),
291+
get_string('email_rejected_subject', 'moodleoverflow', $textcontext),
292+
$OUTPUT->render_from_template('mod_moodleoverflow/email_rejected_text', $textcontext),
293+
$OUTPUT->render_from_template('mod_moodleoverflow/email_rejected_html', $htmlcontext)
294+
);
295+
296+
$url = review::get_first_review_post($post->id);
297+
298+
if (!$post->parent) {
299+
// Delete discussion, if this is the question.
300+
moodleoverflow_delete_discussion($discussion, $course, $cm, $moodleoverflow);
301+
} else {
302+
moodleoverflow_delete_post($post, true, $cm, $moodleoverflow);
303+
}
269304

270305
return $url;
271306
}

lang/en/moodleoverflow.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -443,5 +443,7 @@
443443
$string['review_needed'] = 'Review needed!';
444444

445445
$string['email_review_needed_subject'] = 'Review needed in {$a->coursename}: {$a->subject}';
446-
447446
$string['email_rejected_subject'] = '{$a->coursename}: One of your posts has been rejected.';
447+
$string['your_post_was_rejected'] = 'Your post was rejected.';
448+
$string['your_post_was_rejected_with_reason'] = 'Your post was rejected with the following reason:';
449+
$string['original_post'] = 'Original post';
+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
{{!
2+
This file is part of Moodle - http://moodle.org/
3+
4+
Moodle is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
Moodle is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
}}
17+
{{!
18+
@template mod_moodleoverflow/email_rejected_html
19+
20+
Template which defines a moodleoverflow post for sending in a single-post HTML email.
21+
22+
Classes required for JS:
23+
* none
24+
25+
Data attributes required for JS:
26+
* none
27+
28+
Context variables required for this template:
29+
* courselink
30+
* coursename
31+
* moodleoverflowindexlink
32+
* moodleoverflowviewlink
33+
* moodleoverflowname
34+
* discussionlink
35+
* discussionname
36+
* showdiscussionname
37+
* firstpost
38+
* subject
39+
* authorlink
40+
* authorpicture
41+
* authorfullname
42+
* postdate
43+
* grouppicture
44+
* attachments
45+
* message
46+
* parentpostlink
47+
* canreply
48+
* replylink
49+
* permalink
50+
* unsubscribemoodleoverflowlink
51+
* unsubscribediscussionlink
52+
53+
Example context (json):
54+
{
55+
"courselink": "https://example.com/course/view.php?id=2",
56+
"coursename": "Example course",
57+
"moodleoverflowindexlink": "https://example.com/mod/moodleoverflow/index.php?id=2",
58+
"moodleoverflowviewlink": "https://example.com/mod/moodleoverflow/view.php?f=2",
59+
"moodleoverflowname": "Lorem ipsum dolor",
60+
"discussionlink": "https://example.com/mod/moodleoverflow/discuss.php?d=70",
61+
"discussionname": "Is Lorem ipsum Latin?",
62+
"showdiscussionname": 1,
63+
"firstpost": 1,
64+
"subject": "Is Lorem ipsum Latin?",
65+
"authorlink": "https://example.com/user/view.php?id=2&course=2",
66+
"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>",
67+
"authorfullname": "Lucius Caecilius lucundus",
68+
"postdate": "Sunday, 13 September 2015, 2:22 pm",
69+
"grouppicture": "",
70+
"attachments": "",
71+
"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>",
72+
"parentpostlink": "",
73+
"canreply": 1,
74+
"replylink": "https://example.com/mod/moodleoverflow/post.php?reply=2",
75+
"permalink": "https://example.com/mod/moodleoverflow/discuss.php?d=2#2",
76+
"unsubscribemoodleoverflowlink": "https://example.com/mod/moodleoverflow/subscribe.php?id=2",
77+
"unsubscribediscussionlink": "https://example.com/mod/discussion/subscribe.php?id=2&d=2"
78+
}
79+
}}
80+
<div class="navbar">
81+
<a target="_blank" href="{{{ courselink }}}">{{{ coursename }}}</a>
82+
&raquo;
83+
<a target="_blank" href="{{{ moodleoverflowviewlink }}}">{{{ moodleoverflowname }}}</a>
84+
{{# showdiscussionname }}
85+
&raquo;
86+
<a target="_blank" href="{{{ discussionlink }}}">{{{ discussionname }}}</a>
87+
{{/ showdiscussionname }}
88+
</div>
89+
90+
{{^reason}}
91+
{{#str}} your_post_was_rejected, mod_moodleoverflow {{/str}}
92+
{{/reason}}
93+
{{#reason}}
94+
{{#str}} your_post_was_rejected_with_reason, mod_moodleoverflow {{/str}}<br>
95+
<i>{{reason}}</i>
96+
{{/reason}}
97+
98+
<br>
99+
<br>
100+
{{#str}}original_post, mod_moodleoverflow{{/str}}
101+
<hr/>
102+
<br>
103+
104+
<table border="0" cellpadding="3" cellspacing="0" class="moodleoverflowpost">
105+
<tr class="header">
106+
<td width="35" valign="top" class="picture left">
107+
{{{ authorpicture }}}
108+
</td>
109+
<td class="topic {{# firstpost }}starter{{/ firstpost }}">
110+
<div class="subject">
111+
{{{ subject }}}
112+
</div>
113+
<div class="author">
114+
{{# str }} bynameondatenorating, moodleoverflow, {
115+
"name":
116+
{{# authorlink }}{{# quote }} <a target='_blank' href='{{{ authorlink }}}'>{{ authorfullname }}</a>{{/ quote }}{{/authorlink}}
117+
{{^ authorlink }}{{# quote }} {{ authorfullname }} {{/ quote }}{{/ authorlink }} ,
118+
"date": {{# quote }}{{ postdate }}{{/ quote }}
119+
} {{/ str }}
120+
</div>
121+
</td>
122+
</tr>
123+
<tr>
124+
<td class="left side" valign="top">
125+
{{# grouppicture }}
126+
{{{ grouppicture }}}
127+
{{/ grouppicture }}
128+
{{^ grouppicture }}
129+
&nbsp;
130+
{{/ grouppicture }}
131+
</td>
132+
<td class="content">
133+
{{{ message }}}
134+
</td>
135+
</tr>
136+
</table>
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{{!
2+
This file is part of Moodle - http://moodle.org/
3+
4+
Moodle is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
Moodle is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
}}
17+
{{!
18+
@template mod_moodleoverflow/email_rejected_text
19+
20+
Template which defines a moodleoverflow post for sending in a single-post HTML email.
21+
22+
Classes required for JS:
23+
* none
24+
25+
Data attributes required for JS:
26+
* none
27+
28+
Context variables required for this template:
29+
* courselink
30+
* coursename
31+
* moodleoverflowindexlink
32+
* moodleoverflowviewlink
33+
* moodleoverflowname
34+
* discussionlink
35+
* discussionname
36+
* showdiscussionname
37+
* firstpost
38+
* subject
39+
* authorlink
40+
* authorpicture
41+
* authorfullname
42+
* postdate
43+
* grouppicture
44+
* attachments
45+
* message
46+
* parentpostlink
47+
* canreply
48+
* replylink
49+
* permalink
50+
* unsubscribemoodleoverflowlink
51+
* unsubscribediscussionlink
52+
53+
Example context (json):
54+
{
55+
"courselink": "https://example.com/course/view.php?id=2",
56+
"coursename": "Example course",
57+
"moodleoverflowindexlink": "https://example.com/mod/moodleoverflow/index.php?id=2",
58+
"moodleoverflowviewlink": "https://example.com/mod/moodleoverflow/view.php?f=2",
59+
"moodleoverflowname": "Lorem ipsum dolor",
60+
"discussionlink": "https://example.com/mod/moodleoverflow/discuss.php?d=70",
61+
"discussionname": "Is Lorem ipsum Latin?",
62+
"showdiscussionname": 1,
63+
"firstpost": 1,
64+
"subject": "Is Lorem ipsum Latin?",
65+
"authorlink": "https://example.com/user/view.php?id=2&course=2",
66+
"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>",
67+
"authorfullname": "Lucius Caecilius lucundus",
68+
"postdate": "Sunday, 13 September 2015, 2:22 pm",
69+
"grouppicture": "",
70+
"attachments": "",
71+
"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>",
72+
"parentpostlink": "",
73+
"canreply": 1,
74+
"replylink": "https://example.com/mod/moodleoverflow/post.php?reply=2",
75+
"permalink": "https://example.com/mod/moodleoverflow/discuss.php?d=2#2",
76+
"unsubscribemoodleoverflowlink": "https://example.com/mod/moodleoverflow/subscribe.php?id=2",
77+
"unsubscribediscussionlink": "https://example.com/mod/discussion/subscribe.php?id=2&d=2"
78+
}
79+
}}
80+
{{{ coursename }}} -> {{{ moodleoverflowname }}}{{# showdiscussionname }} -> {{{ discussionname }}} {{/ showdiscussionname }}
81+
82+
{{^reason}}
83+
{{#str}} your_post_was_rejected, mod_moodleoverflow {{/str}}
84+
{{/reason}}
85+
{{#reason}}
86+
{{#str}} your_post_was_rejected_with_reason, mod_moodleoverflow {{/str}}: {{reason}}
87+
{{/reason}}
88+
89+
90+
{{#str}}original_post, mod_moodleoverflow{{/str}}
91+
=====================================================================
92+
93+
94+
{{{ subject }}}
95+
{{# str }} bynameondate, mod_moodleoverflow, {
96+
"name": {{# quote }}{{{ authorfullname }}}{{/ quote }},
97+
"date": {{# quote}}{{ postdate }}{{/ quote }}
98+
} {{/ str }}
99+
---------------------------------------------------------------------
100+
{{{ message }}}
101+
---------------------------------------------------------------------

templates/email_review_needed_html.mustache

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616
}}
1717
{{!
18-
@template mod_moodleoverflow/email_html
18+
@template mod_moodleoverflow/email_review_needed_html
1919
2020
Template which defines a moodleoverflow post for sending in a single-post HTML email.
2121

templates/email_review_needed_text.mustache

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616
}}
1717
{{!
18-
@template mod_moodleoverflow/email_reject_text
18+
@template mod_moodleoverflow/email_review_needed_text
1919
2020
Template which defines a moodleoverflow post for sending in a single-post HTML email.
2121

0 commit comments

Comments
 (0)