diff --git a/lang/en/message.php b/lang/en/message.php index b1ea6dafd12..e9d7e2b681a 100644 --- a/lang/en/message.php +++ b/lang/en/message.php @@ -139,6 +139,7 @@ $string['notificationimage'] = 'Notification image'; $string['notifications'] = 'Notifications'; $string['notincontactsheading'] = '{$a} is not in your contacts'; $string['notincontacts'] = 'You need to add {$a} to your contacts to be able to send them messages.'; +$string['notsupported'] = 'Not supported'; $string['numparticipants'] = '{$a} participants'; $string['off'] = 'Off'; $string['offline'] = 'Offline'; diff --git a/message/classes/helper.php b/message/classes/helper.php index 4db082da2aa..77b70dfc16d 100644 --- a/message/classes/helper.php +++ b/message/classes/helper.php @@ -24,6 +24,7 @@ namespace core_message; use DOMDocument; +use stdclass; defined('MOODLE_INTERNAL') || die(); @@ -137,7 +138,7 @@ class helper { foreach ($messages as $message) { // Store the message information. - $msg = new \stdClass(); + $msg = new stdClass(); $msg->id = $message->id; $msg->useridfrom = $message->useridfrom; $msg->text = message_format_message_text($message); @@ -172,16 +173,16 @@ class helper { /** * Helper function for creating a contact object. * - * @param \stdClass $contact + * @param stdClass $contact * @param string $prefix - * @return \stdClass + * @return stdClass */ public static function create_contact($contact, $prefix = '') { global $PAGE; // Create the data we are going to pass to the renderable. $userfields = \user_picture::unalias($contact, array('lastaccess'), $prefix . 'id', $prefix); - $data = new \stdClass(); + $data = new stdClass(); $data->userid = $userfields->id; $data->useridfrom = null; $data->fullname = fullname($userfields); @@ -222,7 +223,7 @@ class helper { /** * Helper function for checking if we should show the user's online status. * - * @param \stdClass $user + * @param stdClass $user * @return boolean */ public static function show_online_status($user) { @@ -263,10 +264,10 @@ class helper { * * @param array $providers * @param int $userid - * @return \stdClass + * @return stdClass */ public static function get_providers_preferences($providers, $userid) { - $preferences = new \stdClass(); + $preferences = new stdClass(); // Get providers preferences. foreach ($providers as $provider) { @@ -453,7 +454,7 @@ class helper { $members = []; foreach ($otherusers as $member) { // Set basic data. - $data = new \stdClass(); + $data = new stdClass(); $data->id = $member->id; $data->fullname = fullname($member); @@ -668,12 +669,12 @@ class helper { * * deleted * * all potential fullname fields * - * @param \stdClass $user + * @param stdClass $user * @param array $userfields An array of userfields to be returned, the values must be a * subset of user_get_default_fields (optional) * @return array the array of userdetails, if visible, or an empty array otherwise. */ - public static function search_get_user_details(\stdClass $user, array $userfields = []): array { + public static function search_get_user_details(stdClass $user, array $userfields = []): array { global $CFG, $USER; require_once($CFG->dirroot . '/user/lib.php'); @@ -721,4 +722,31 @@ class helper { return $html; } + + /** + * Check the support for SMS in different components. + * + * At the moment, SMS is not supported for all the components. + * Components that supports SMS, can implement the callback to let the notification API support them. + * + * @param stdclass $messagedata The message data + * @return bool + */ + public static function supports_sms_notifications(stdclass $messagedata): bool { + // Notification API assigns system ones as component 'moodle'. + // We need a special mechanism to handle that as component callback will throw errors. + if ($messagedata->component === 'moodle') { + return false; + } + + $smssupport = component_callback( + $messagedata->component, + 'supports_sms_notifications' + ); + + if (empty($smssupport)) { + return false; + } + return true; + } } diff --git a/message/classes/output/preferences/notification_list_processor.php b/message/classes/output/preferences/notification_list_processor.php index 5ee10e550c5..6454e126b5a 100644 --- a/message/classes/output/preferences/notification_list_processor.php +++ b/message/classes/output/preferences/notification_list_processor.php @@ -150,6 +150,12 @@ class notification_list_processor implements templatable, renderable { } } + $supportsprocessor = true; + if ($processor->name === 'sms') { + $supportsprocessor = \core_message\helper::supports_sms_notifications($this->provider); + } + $context['supportsprocessor'] = $supportsprocessor; + return $context; } } diff --git a/message/renderer.php b/message/renderer.php index b7ad12cb264..4e8c9fd4b33 100644 --- a/message/renderer.php +++ b/message/renderer.php @@ -144,6 +144,12 @@ class core_message_renderer extends plugin_renderer_base { foreach ($processors as $processor) { $setting = new StdClass(); + $supportsprocessor = true; + if ($processor->name === 'sms') { + $supportsprocessor = core_message\helper::supports_sms_notifications($provider); + } + $setting->supportsprocessor = $supportsprocessor; + $setting->lockedsetting = $providersettingprefix.'locked['.$processor->name.']'; $preference = $processor->name.'_provider_'.$providersettingprefix.'locked'; diff --git a/message/templates/default_notification_preferences.mustache b/message/templates/default_notification_preferences.mustache index 16587059805..3d2c47ede0c 100644 --- a/message/templates/default_notification_preferences.mustache +++ b/message/templates/default_notification_preferences.mustache @@ -59,7 +59,8 @@ "lockedlabel": "Sending Assignment via Web locked status", "enabledsetting": "mod_assign_assign_notification_enabled[popup]", "enabled": 1, - "enabledlabel": "Sending Assignment via Web enabled status" + "enabledlabel": "Sending Assignment via Web enabled status", + "supportsprocessor": true } ] } @@ -106,34 +107,41 @@ {{#settings}}