From aa2fb1e3d2abd425651a2dc6b6c3edc3300f7ed9 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Fri, 14 Mar 2025 14:48:05 +0000 Subject: [PATCH] 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. --- .upgradenotes/MDL-84733-2025031414225824.yml | 11 ++- mod/assign/locallib.php | 93 +++---------------- .../messages/notification_html.mustache | 63 +++++++++++++ .../messages/notification_text.mustache | 60 ++++++++++++ mod/assign/tests/locallib_test.php | 18 ++-- 5 files changed, 158 insertions(+), 87 deletions(-) create mode 100644 mod/assign/templates/messages/notification_html.mustache create mode 100644 mod/assign/templates/messages/notification_text.mustache diff --git a/.upgradenotes/MDL-84733-2025031414225824.yml b/.upgradenotes/MDL-84733-2025031414225824.yml index d2f598869ef..e1139e1076e 100644 --- a/.upgradenotes/MDL-84733-2025031414225824.yml +++ b/.upgradenotes/MDL-84733-2025031414225824.yml @@ -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 diff --git a/mod/assign/locallib.php b/mod/assign/locallib.php index c7061fd0a6a..704b7df7311 100644 --- a/mod/assign/locallib.php +++ b/mod/assign/locallib.php @@ -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 = '

' . - '' . - format_string($course->shortname, true, $formatparams) . - ' ->' . - '' . - $modulename . - ' ->' . - '' . - format_string($assignmentname, true, $formatparams) . - '

'; - $posthtml .= '
'; - $posthtml .= '

' . get_string($messagetype . 'html', 'assign', $info) . '

'; - $posthtml .= '

'; - 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'; diff --git a/mod/assign/templates/messages/notification_html.mustache b/mod/assign/templates/messages/notification_html.mustache new file mode 100644 index 00000000000..07c1b87b701 --- /dev/null +++ b/mod/assign/templates/messages/notification_html.mustache @@ -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 . +}} +{{! + @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": "

Your assignment for Assignment 1 has been submitted. You can check its status on the assignment page.

", + "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}}} +

+
+

{{{messagehtml}}}

+
diff --git a/mod/assign/templates/messages/notification_text.mustache b/mod/assign/templates/messages/notification_text.mustache new file mode 100644 index 00000000000..7538f0fb10d --- /dev/null +++ b/mod/assign/templates/messages/notification_text.mustache @@ -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 . +}} +{{! + @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}}} + +--------------------------------------------------------------------- diff --git a/mod/assign/tests/locallib_test.php b/mod/assign/tests/locallib_test.php index f6b4c5deae9..111c89cc43a 100644 --- a/mod/assign/tests/locallib_test.php +++ b/mod/assign/tests/locallib_test.php @@ -1791,13 +1791,17 @@ You can see it appended to your assignment submission: $message->fullmessage, ); $this->assertEquals( - '

' . $course->shortname . ' ' . - '->Assignment ' . - '->' . $assignname . '


' . - '

' . $teachername . ' has posted some feedback on your ' . - 'assignment submission for \'' . $assignname . '\'

' . "\n" . - 'You can see it appended to your assignment submission.


', + '

+ ' . $course->shortname . ' -> + Assignment -> + ' . $assignname . ' +

+
+

' . $teachername . ' has posted some feedback on your ' . + 'assignment submission for \'' . $assignname . '\'

+You can see it appended to your assignment submission.

+
', $message->fullmessagehtml ); }