MDL-84733 assign: use a template to render message bodies

The overall structure of these messages, which adds the course
and assignment name to whatever comes from the particular
message language string, was hard-coded in the assignment code.

By changing this to use a template, now themes can override this.
This commit is contained in:
Tim Hunt
2025-03-21 14:57:29 +00:00
parent b9bd43ad78
commit aa2fb1e3d2
5 changed files with 158 additions and 87 deletions
+10 -1
View File
@@ -5,5 +5,14 @@ notes:
When the assignment activity sends notifications, it now sets more
information in the $a object passed to the languages strings for the
message subject and body. This is to give more flexibility to people
using Language Customisation.
using Language Customisation. The avaiable information is the same as
the template context for the two templates in the next paragraph, but
without the messagetext/html.
Also, the overall structure of these messages is now rendered using
templates mod_assign/messages/notification_html and
mod_assign/messages/notification_html, so they can be overridden by themes.
As a result of this, the methods format_notification_message_text and
format_notification_message_html (which should have been private to
mod_assign and not used anywhere else) have been removed.
type: improved
+14 -79
View File
@@ -6433,70 +6433,6 @@ class assign {
return $notifiableusers;
}
/**
* Format a notification for plain text.
*
* @param string $messagetype
* @param stdClass $info
* @param stdClass $course
* @param stdClass $context
* @param string $modulename
* @param string $assignmentname
*/
protected static function format_notification_message_text($messagetype,
$info,
$course,
$context,
$modulename,
$assignmentname) {
$formatparams = array('context' => $context->get_course_context());
$posttext = format_string($course->shortname, true, $formatparams) .
' -> ' .
$modulename .
' -> ' .
format_string($assignmentname, true, $formatparams) . "\n";
$posttext .= '---------------------------------------------------------------------' . "\n";
$posttext .= get_string($messagetype . 'text', 'assign', $info)."\n";
$posttext .= "\n---------------------------------------------------------------------\n";
return $posttext;
}
/**
* Format a notification for HTML.
*
* @param string $messagetype
* @param stdClass $info
* @param stdClass $course
* @param stdClass $context
* @param string $modulename
* @param stdClass $coursemodule
* @param string $assignmentname
*/
protected static function format_notification_message_html($messagetype,
$info,
$course,
$context,
$modulename,
$coursemodule,
$assignmentname) {
global $CFG;
$formatparams = array('context' => $context->get_course_context());
$posthtml = '<p><font face="sans-serif">' .
'<a href="' . $CFG->wwwroot . '/course/view.php?id=' . $course->id . '">' .
format_string($course->shortname, true, $formatparams) .
'</a> ->' .
'<a href="' . $CFG->wwwroot . '/mod/assign/index.php?id=' . $course->id . '">' .
$modulename .
'</a> ->' .
'<a href="' . $CFG->wwwroot . '/mod/assign/view.php?id=' . $coursemodule->id . '">' .
format_string($assignmentname, true, $formatparams) .
'</a></font></p>';
$posthtml .= '<hr /><font face="sans-serif">';
$posthtml .= '<p>' . get_string($messagetype . 'html', 'assign', $info) . '</p>';
$posthtml .= '</font><hr />';
return $posthtml;
}
/**
* Message someone about something (static so it can be called from cron).
*
@@ -6508,7 +6444,7 @@ class assign {
* @param stdClass $coursemodule
* @param context $context
* @param stdClass $course
* @param string $modulename
* @param string $modulename - no longer used.
* @param string $assignmentname
* @param bool $blindmarking
* @param int $uniqueidforuser
@@ -6567,26 +6503,25 @@ class assign {
// Other data passed in.
$info = (object) array_merge((array) $info, $extrainfo);
// Prepare the message subject and body.
// Prepare the message subject and bodies.
$postsubject = get_string($messagetype . 'small', 'assign', $info);
$posttext = self::format_notification_message_text($messagetype,
$info,
$course,
$context,
$modulename,
$assignmentname);
$renderer = $PAGE->get_renderer('mod_assign');
$context = clone $info;
$context->messagetext = get_string($messagetype . 'text', 'assign', $info);
// Mustache strips off all training whitespace, but we want a newline at the end.
$posttext = $renderer->render_from_template(
'mod_assign/messages/notification_text', $context) . "\n";
$posthtml = '';
if ($userto->mailformat == 1) {
$posthtml = self::format_notification_message_html($messagetype,
$info,
$course,
$context,
$modulename,
$coursemodule,
$assignmentname);
$context = clone $info;
$context->messagehtml = get_string($messagetype . 'html', 'assign', $info);
$posthtml = $renderer->render_from_template(
'mod_assign/messages/notification_html', $context);
}
// Build the message object.
$eventdata = new \core\message\message();
$eventdata->courseid = $course->id;
$eventdata->modulename = 'assign';
@@ -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_assign/messages/notification_html
This template is used for the HTML version of messages sent from the assignment module.
Note: much more is passed to this template than might be needed, but this is to give
flexibility to themes.
Classes required for JS:
* none
Context variables required for this template:
* messagehtml - the main content of the message. Normally comes from a lang string.
* username - full name of the user who performed the action leading to the message, but will be changed to 'Participant 123' if blink marking is in use.
* recipentname - full name of the user the email is being sent to. (Never anonymised.)
* assignment - The name of this assignment.
* url - The URL of this assignment activity.
* assigncmid - Course-module id of this assignment.
* courseshortname - short name of the course this assignment is in.
* coursefullname - full name of the course this assignment is in.
* courseurl - URL of the course page.
* courseassignsurl - URL of the paeg listing all assignments in the course.
* timeupdated - Time the action that triggered this message was performed, formatted for display.
Example context (json):
{
"messagehtml": "<p>Your assignment for <strong>Assignment 1</strong> has been submitted. You can check its status on the <a href='https://qa.moodledemo.net/mod/assign/view.php?id=5'>assignment page</a>.</p>",
"username": "Trusty Teacher",
"recipientname": "Smart Student",
"assignment": "Assignment 1",
"url": "https://qa.moodledemo.net/mod/assign/view.php?id=5",
"assigncmid": 5,
"courseshortname": "A100",
"coursefullname": "Assessment for beginners",
"courseurl": "https://qa.moodledemo.net/course/view.php?id=2",
"courseassignsurl": "https://qa.moodledemo.net/mod/assign/index.php?id=2",
"timeupdated": "Fri, 14 Mar 2025, 11:08 PM"
}
}}
<p><font face="sans-serif">
<a href="{{courseurl}}">{{{courseshortname}}}</a> ->
<a href="{{courseassignsurl}}">{{#str}}modulename, mod_assign{{/str}}</a> ->
<a href="{{url}}">{{{assignment}}}</a>
</font></p>
<hr>
<font face="sans-serif"><p>{{{messagehtml}}}</p></font>
<hr>
@@ -0,0 +1,60 @@
{{!
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_assign/messages/notification_text
This template is used for the plain text version of messages sent from the assignment module.
Note: much more is passed to this template than might be needed, but this is to give
flexibility to themes.
Classes required for JS:
* none
Context variables required for this template:
* messagetext - the main content of the message. Normally comes from a lang string.
* username - full name of the user who performed the action leading to the message, but will be changed to 'Participant 123' if blink marking is in use.
* recipentname - full name of the user the email is being sent to. (Never anonymised.)
* assignment - The name of this assignment.
* url - The URL of this assignment activity.
* assigncmid - Course-module id of this assignment.
* courseshortname - short name of the course this assignment is in.
* coursefullname - full name of the course this assignment is in.
* courseurl - URL of the course page.
* courseassignsurl - URL of the paeg listing all assignments in the course.
* timeupdated - Time the action that triggered this message was performed, formatted for display.
Example context (json):
{
"messagehtml": "Your assignment for Assignment 1 has been submitted. You can check its status at https://qa.moodledemo.net/mod/assign/view.php?id=5.",
"username": "Trusty Teacher",
"recipientname": "Smart Student",
"assignment": "Assignment 1",
"url": "https://qa.moodledemo.net/mod/assign/view.php?id=5",
"assigncmid": 5,
"courseshortname": "A100",
"coursefullname": "Assessment for beginners",
"courseurl": "https://qa.moodledemo.net/course/view.php?id=2",
"courseassignsurl": "https://qa.moodledemo.net/mod/assign/index.php?id=2",
"timeupdated": "Fri, 14 Mar 2025, 11:08 PM"
}
}}
{{{courseshortname}}} -> {{#str}}modulename, mod_assign{{/str}} -> {{{assignment}}}
---------------------------------------------------------------------
{{{messagetext}}}
---------------------------------------------------------------------
+11 -7
View File
@@ -1791,13 +1791,17 @@ You can see it appended to your assignment submission:
$message->fullmessage,
);
$this->assertEquals(
'<p><font face="sans-serif"><a href="' . $courseurl . '">' . $course->shortname . '</a> ' .
'-><a href="' . $assignsurl . '">Assignment</a> ' .
'-><a href="' . $assignurl . '">' . $assignname . '</a></font></p><hr />' .
'<font face="sans-serif"><p>' . $teachername . ' has posted some feedback on your ' .
'assignment submission for \'<i>' . $assignname . '</i>\'<br /><br />' . "\n" .
'You can see it appended to your <a href="' . $assignurl .
'">assignment submission</a>.</p></font><hr />',
'<p><font face="sans-serif">
<a href="' . $courseurl . '">' . $course->shortname . '</a> ->
<a href="' . $assignsurl . '">Assignment</a> ->
<a href="' . $assignurl . '">' . $assignname . '</a>
</font></p>
<hr>
<font face="sans-serif"><p>' . $teachername . ' has posted some feedback on your ' .
'assignment submission for \'<i>' . $assignname . '</i>\'<br /><br />
You can see it appended to your <a href="' . $assignurl .
'">assignment submission</a>.</p></font>
<hr>',
$message->fullmessagehtml
);
}