Collections functions
General functions
Render functions
Theme permission functions
Resource functions

send_user_notification()

Description

Send system notifications to specified users, checking the user preferences first if specified


This will contain two arrays:-
"emails" array of emails sent, with the following elements:-
"email" => Email address
"subject" => Email subject
"body" => Body text

"messages" Array of system messages sent with the following elements :-
"user" => User ID
"message" => message text
"url" => url

Parameters

ColumnTypeDefaultDescription
$users array [] Array of user IDs or array of user details from get_users()
$notifymessage object An instance of a ResourceSpaceUserNotification object holding message properties
$forcemail bool false Force system to send email instead of notification?

Return

array Array containing resulting messages - can be used for testing when emails are not being sent

Location

include/message_functions.php lines 891 to 1046

Definition

 
function send_user_notification($users=[],$notifymessage$forcemail=false)
    {
    global 
$userref$lang$plugins$header_colour_style_override;

    
// Need to global $applicationname as it is used inside the lang files
    
global $applicationname;
    
$userlanguages = []; // This stores the users in their relevant language key element

    // Set up $results array
    
$results = [];
    
$results["messages"] = [];
    
$results["emails"] = [];
    foreach(
$users as $notify_user)
        {
        
$userdetails $notify_user;
        if(!
is_array($userdetails))
            {
            
$userdetails get_user((int)$userdetails);
            }
        elseif(!isset(
$userdetails["lang"]))
            {
            
// Need full user info
            
$userdetails get_user($userdetails["ref"]);
            }        
        if(
$userdetails == false)
            {
            continue;
            }
        
        
$send_message=true;
        
// Check if preferences should prevent the notification from being sent
        
if(isset($notifymessage->user_preference) && is_array($notifymessage->user_preference))
            {
            foreach(
$notifymessage->user_preference as $preference=>$vals)
                {
                if(
$preference != "")
                    {
                    
$requiredvalue  = (bool)$vals["requiredvalue"];
                    
$default        = (bool)$vals["default"];
                    
get_config_option($userdetails['ref'],$preference$check_pref,$default);
                    
debug(" - Required preference: " $preference " = " . ($requiredvalue "TRUE" "FALSE"));
                    
debug(" - User preference value: " $preference " = " . ($check_pref "TRUE" "FALSE"));
                    if(
$check_pref != $requiredvalue)
                        {
                        
debug("Skipping notification to user #" $userdetails['ref']);
                        
$send_message=false;                        
                        }
                    }
                }
            }
        if(
$send_message==false)
            {
            continue;
            }
        
debug("Sending notification to user #" $userdetails["ref"]);
        
get_config_option($userdetails['ref'],'email_user_notifications'$send_email);
        if(!isset(
$userlanguages[$userdetails['lang']]))
            {
            
$userlanguages[$userdetails['lang']] = [];
            
$userlanguages[$userdetails['lang']]["emails"] = [];
            
$userlanguages[$userdetails['lang']]["message_users"] = [];
            }
        if((
$send_email && filter_var($userdetails["email"], FILTER_VALIDATE_EMAIL)) || $forcemail)
            {
            
debug("Sending email to user #" $userdetails["ref"]);
            
$userlanguages[$userdetails['lang']]["emails"][] = $userdetails["email"];
            }
        else
            {
            
debug("Sending system message to user #" $userdetails["ref"]);
            
$userlanguages[$userdetails['lang']]["message_users"][]=$userdetails["ref"];
            }
        }
    
$url $notifymessage->url ?? NULL;

    
$headerimghtml "";
    if(!isset(
$notifymessage->template))
        {
        
// Add header image to email if not using template
        
$img_url get_header_image(true);
        
$img_div_style "max-height:50px;padding: 5px;";
        
$img_div_style .= "background: " . ((isset($header_colour_style_override) && $header_colour_style_override != '') ? $header_colour_style_override "rgba(0, 0, 0, 0.6)") . ";";
        
$headerimghtml '<div style="' $img_div_style '"><img src="' $img_url '" style="max-height:50px;"  /><br/><br/>';
        }

    foreach(
$userlanguages as $userlanguage=>$notifications)
        {
        
debug("Processing notifications for language: '" $userlanguage "'");
        
// Save the current lang array
        
$saved_lang $lang;
        if (
$userlanguage!="en")
            {
            if (
substr($userlanguage21)=='-' && substr($userlanguage02)!='en')
                {
                
$langpath dirname(__FILE__)."/../languages/" safe_file_name(substr($userlanguage02)) . ".php";
                if(
file_exists($langpath))
                    {
                    include 
$langpath;
                    }
                }
            
$langpath dirname(__FILE__)."/../languages/" safe_file_name($userlanguage) . ".php";
            if(
file_exists($langpath))
                {
                include 
$langpath;
                }
            }

        
# Register plugin languages in reverse order
        
for ($n=count($plugins)-1;$n>=0;$n--)
            {
            if (!isset(
$plugins[$n]))
                {
                continue;
                }
            
register_plugin_language($plugins[$n]);
            }

        
// Load in the correct language strings
        
lang_load_site_text($lang,"",$userlanguage);

        
$subject $notifymessage->get_subject();
        
$messagetext $notifymessage->get_text();
        if (
count($notifications["message_users"])>0)
            {
            
$activitytype $notifymessage->eventdata["type"] ?? NULL;
            
$relatedactivity $notifymessage->eventdata["ref"] ?? NULL;
            foreach(
$notifications["message_users"] as $notifyuser)
                {
                
$results["messages"][] = ["user"=>$notifyuser,"message"=>$messagetext,"url"=>$url];
                }
            
message_add($notifications["message_users"],$messagetext,$url,$userref,MESSAGE_ENUM_NOTIFICATION_TYPE_SCREEN,MESSAGE_DEFAULT_TTL_SECONDS,$activitytype,$relatedactivity);
            }
        if (
count($notifications["emails"])>0)
            {
            if(
strpos($messagetext,$url) === false)
                {
                
// Add the URL to the message if not already present
                
$messagetext $messagetext "<br/><br/><a href='" $url "'>" $url "</a>";
                }
            
send_mail(implode(",",$notifications["emails"]),$subject,$headerimghtml $messagetext,"","",$notifymessage->template,$notifymessage->templatevars);

            foreach(
$notifications["emails"] as $emailsent)
                {
                
$results["emails"][] = ["email"=>$emailsent,"subject"=>$subject,"body"=>$headerimghtml $messagetext];
                }

            foreach(
$notifications["message_users"] as $notifyuser)
                {
                
$results[$notifyuser] = ["type"=>"messsage","body"=>$messagetext];
                }
            }
        
// Restore the saved $lang array
        
$lang $saved_lang;
        }
    return 
$results;
    }

This article was last updated 2nd October 2022 00:05 Europe/London time based on the source file dated 13th September 2022 10:20 Europe/London time.