diff --git a/.upgradenotes/MDL-83080-2024121715070347.yml b/.upgradenotes/MDL-83080-2024121715070347.yml new file mode 100644 index 0000000000000..8af85296d18ef --- /dev/null +++ b/.upgradenotes/MDL-83080-2024121715070347.yml @@ -0,0 +1,7 @@ +issueNumber: MDL-83080 +notes: + core_message: + - message: >- + The `contexturl` property to `\core\message\message` instances can now + contain `\core\url` values in addition to plain strings + type: improved diff --git a/lib/classes/message/message.php b/lib/classes/message/message.php index 30301c5dd970a..8d56719151839 100644 --- a/lib/classes/message/message.php +++ b/lib/classes/message/message.php @@ -14,18 +14,9 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * New messaging class. - * - * @package core_message - * @since Moodle 2.9 - * @copyright 2015 onwards Ankit Agarwal - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - namespace core\message; -defined('MOODLE_INTERNAL') || die(); +use core\url; /** * New messaging class. @@ -46,7 +37,7 @@ * * Optional parameters of the $eventdata object: * notification bool Should the message be considered as a notification rather than a personal message - * contexturl string If this is a notification then you can specify a url to view the event. + * contexturl string|url If this is a notification then you can specify a url to view the event. * For example the forum post the user is being notified of. * contexturlname string The display text for contexturl. * replyto string An email address which can be used to send an reply. @@ -102,7 +93,7 @@ class message { /** @var int Is it a notification? */ private $notification; - /** @var string context url. */ + /** @var string|url context url. */ private $contexturl; /** @var string context name. */ diff --git a/lib/messagelib.php b/lib/messagelib.php index a79b78c0c9579..3eca554fd20d8 100644 --- a/lib/messagelib.php +++ b/lib/messagelib.php @@ -22,6 +22,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +use core\url; + defined('MOODLE_INTERNAL') || die(); require_once(__DIR__ . '/../message/lib.php'); @@ -44,7 +46,8 @@ * * Optional parameters of the $eventdata object: * notification bool should the message be considered as a notification rather than a personal message - * contexturl string if this is a notification then you can specify a url to view the event. For example the forum post the user is being notified of. + * contexturl string|url if this is a notification then you can specify a url to view the event. + * For example the forum post the user is being notified of. * contexturlname string the display text for contexturl * * Note: processor failure will not reported as false return value in all scenarios, @@ -83,6 +86,14 @@ function message_send(\core\message\message $eventdata) { return false; } + // Cast context URL and name. + if (!empty($eventdata->contexturl)) { + $eventdata->contexturl = (string) $eventdata->contexturl; + } + if (!empty($eventdata->contexturlname)) { + $eventdata->contexturlname = (string) $eventdata->contexturlname; + } + // Legacy messages (FROM a single user TO a single user) must be converted into conversation messages. // Then, these will be passed through the conversation messages code below. if (!$eventdata->notification && !$eventdata->convid) { @@ -256,17 +267,8 @@ function message_send(\core\message\message $eventdata) { $tabledata->component = $eventdata->component; $tabledata->timecreated = time(); $tabledata->customdata = $eventdata->customdata; - if (!empty($eventdata->contexturl)) { - $tabledata->contexturl = (string)$eventdata->contexturl; - } else { - $tabledata->contexturl = null; - } - - if (!empty($eventdata->contexturlname)) { - $tabledata->contexturlname = (string)$eventdata->contexturlname; - } else { - $tabledata->contexturlname = null; - } + $tabledata->contexturl = $eventdata->contexturl ?? null; + $tabledata->contexturlname = $eventdata->contexturlname ?? null; if ($messageid = message_handle_phpunit_redirection($eventdata, $table, $tabledata)) { return $messageid;