root/broadcast/broadcast_send.php

Revision 287, 4.9 kB (checked in by kevin, 8 months ago)

Changed messaging to set message sender id to -1 - this avoids putting potentially hundreds of messages in the admin's sent folder.

Line 
1 <?php
2
3 // Run includes
4 require_once("../../includes.php");
5 include($CFG->dirroot . "mod/broadcast/broadcast.config.php");
6
7 global $CFG, $template, $PAGE;
8 $template['css'] .= file_get_contents($CFG->dirroot . "mod/broadcast/css");
9
10 define("context", "broadcast");
11
12 $cid = optional_param('cid');
13 $message_subject = optional_param('message_subject');
14 $message_text = optional_param('message_text');
15 $profile_field = optional_param('profile_field','');
16 $profile_value = optional_param('profile_value','');
17
18 $thesql='SELECT * FROM ' . $CFG->prefix . 'users
19         WHERE ident =' . intval($_SESSION['userid']);
20 $from=get_record_sql($thesql);
21
22 templates_page_setup();
23
24 $body = "";
25 $title = "";
26 $output = "";
27 $description = '';
28 $error = false;
29
30 if (isloggedin()) {           
31     if ($cid != '*') {
32         $community=get_record('users','ident',intval($cid)); //get the community from the ident...   
33         if ($community) {       
34             $description = __gettext("Sent emails to").': ' . $community->name;       
35             if (isadmin($_SESSION['userid']) || $broadcast_allow_owners && $community->owner == $_SESSION['userid']) {
36                 // only admins and community owners can do this                 
37                 $recipients = broadcast_get_community_users($community,$profile_field,$profile_value);
38             } else {
39                 $error = true;
40                 $body = __gettext("You are not allowed to contact this community.");
41             }
42         } else {
43             $error = true;
44             $body = __gettext("The specified community does not exist.");
45         }
46             
47     } else {
48         $description = __gettext("Sent emails to users");
49         $recipients = broadcast_get_all_users($profile_field,$profile_value);
50     }
51     
52 } else {
53     $error = true;
54     $body = __gettext("You need to be logged-in to do this action.");
55 }
56
57 if (!$error) {
58     /*if ($profile_value) {
59         $description .= ' '.__gettext("where"). ' "' . $profile_field . '" = "'.$profile_value.'".';
60     } else {
61         $description .= '.';
62     }*/
63     $description .= '.';
64     $description .= ' <br /><br /><a href="'.$CFG->wwwroot.'mod/broadcast/broadcast_start.php">';
65     $description .= __gettext("Return to broadcast form").'</a>';
66     $problems = array();
67     if ($recipients) {
68         foreach ($recipients as $recipient) {
69             $result = broadcast_to_user($recipient, $from, $message_subject, $message_text) ;
70             if ($result != 1) {
71                 $p = new stdClass;
72                 $p->result = $result;
73                 $p->recipient = $recipient;
74                 $problems[] = $p;
75             }
76         }
77     }
78     if ($problems) {
79         $body .= __gettext("There was a problem mailing to the following users:");
80         $body .= "<table width=100%><tr><th>Name</th><th>Status</th></tr>";
81         foreach($problems as $p) {
82             $body .= "<tr><td>" . $p->recipient->name . "</td>";
83             $result_message = '<span style="color:red;">'. $p->result . "</font>";
84             $body .= "</td><td>" . $result_message . "</td></tr>\n";
85         }
86         $body .= "</table>";
87     }
88         
89 }
90
91 // Draw page
92 $title = __gettext("Broadcast");
93
94 $body = templates_draw(array(
95                 'context' => 'contentholder',
96                 'title' => $title,
97                 'body' => '<p>'.$description.'</p>'.$body
98             )
99             );
100
101 echo templates_page_draw( array(
102                 $title, $body
103             )
104             );
105
106 function broadcast_to_user($recipient, $from, $message_subject, $message_text) {
107     
108     global $CFG;
109     
110     //TODO: this should really be in a background cron job
111     // I don't use message_user from userlib because that does not support HTML
112     
113     $extra_text = '<br /><br />----<br /><br />'.sprintf(__gettext('If you prefer not to receive direct email updates from %s, '
114     .'you can log in and set your Email Notification (under Account Preferences) to "No".'),$CFG->sitename)
115     . '<br /><br /><a href="'.$CFG->wwwroot.'">'.$CFG->sitename.'</a>';
116     
117     if ($recipient->email_flag) {
118         $result=email_to_user($recipient, $from, $message_subject, format_text_email($message_text.$extra_text, FORMAT_HTML), '<html><body>' . $message_text . $extra_text. '</body></html>', "","", true,$from->email, $from->name);
119     } else {
120         $result = 1;
121     }
122     
123     $m = new StdClass;
124     $m->title = $message_subject;
125     $m->body = $message_text;
126     //$m->from_id = $from->ident;
127     // avoid putting lots of messages in sent folder
128     $m->from_id = -1;
129     $m->to_id = $recipient->ident;
130     $m->posted = time();
131     $m->status = 'unread';
132     
133     if (!insert_record('messages',$m)) {
134         trigger_error(__FUNCTION__.": Failed to send message from {$from->ident} to {$recipient->ident}. An unknown error occurred.", E_ERROR);
135     } else {
136         plugin_hook("message", "publish", $m);
137     }
138     
139     return $result;
140 }
141         
142 ?>
143
144
145
Note: See TracBrowser for help on using the browser.