Annotation of loncom/interface/lonnotify.pm, revision 1.17
1.1 raeburn 1: #
2: # Copyright Michigan State University Board of Trustees
3: #
4: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
5: #
6: # LON-CAPA is free software; you can redistribute it and/or modify
7: # it under the terms of the GNU General Public License as published by
8: # the Free Software Foundation; either version 2 of the License, or
9: # (at your option) any later version.
10: #
11: # LON-CAPA is distributed in the hope that it will be useful,
12: # but WITHOUT ANY WARRANTY; without even the implied warranty of
13: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: # GNU General Public License for more details.
15: #
16: # You should have received a copy of the GNU General Public License
17: # along with LON-CAPA; if not, write to the Free Software
18: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: #
20: # /home/httpd/html/adm/gpl.txt
21: #
22: # http://www.lon-capa.org/
23: #
24:
25: package Apache::lonnotify;
26:
27: use strict;
28: use Apache::lonnet;
29: use Apache::loncommon;
30: use Apache::lonsupportreq;
31: use LONCAPA::Enrollment;
32: use Apache::Constants qw(:common :http);
33: use Apache::lonlocal;
1.2 raeburn 34: use Mail::Send;
35: use HTML::TokeParser;
36: use HTML::Entities;
1.1 raeburn 37:
38: sub handler {
39: my ($r) = @_;
40: &Apache::loncommon::content_type($r,'text/html');
41: $r->send_http_header;
42:
43: if ($r->header_only) {
44: return OK;
45: }
1.2 raeburn 46: my $cdom = $env{'request.role.domain'};
47: unless (&Apache::lonnet::allowed('psa',$cdom)) {
1.1 raeburn 48: # Not allowed to broadcast e-mail system-wide
49: $env{'user.error.msg'}="/adm/notify:psa:0:0:Cannot broadcast e-mail systemwide";
50: return HTTP_NOT_ACCEPTABLE;
51: }
52:
1.2 raeburn 53: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
54: ['command']);
1.1 raeburn 55: my $command = $env{'form.command'};
1.8 raeburn 56: my $origin = $env{'form.origin'};
57:
1.1 raeburn 58: &Apache::lonhtmlcommon::clear_breadcrumbs();
1.12 albertel 59:
1.2 raeburn 60: my $function = &Apache::loncommon::get_users_function();
61: my $tablecolor = &Apache::loncommon::designparm($function.'.tabbg');
1.14 albertel 62:
1.1 raeburn 63: &Apache::lonhtmlcommon::add_breadcrumb
64: ({href=>'/adm/notify',
1.2 raeburn 65: text=>"Broadcast E-mail"});
1.1 raeburn 66: if ($command eq 'process') {
1.14 albertel 67: &print_request_receipt($r,$command,$cdom,$tablecolor);
1.1 raeburn 68: } elsif ($command eq 'compose') {
1.14 albertel 69: &print_composition_form($r,$command,$cdom,$tablecolor);
1.2 raeburn 70: } elsif ($command eq 'pick_target') {
1.14 albertel 71: &print_selection_form($r,$command,$cdom,$tablecolor);
1.2 raeburn 72: } elsif ($command eq 'pick_display') {
1.14 albertel 73: &print_display_option_form($r,$command,$cdom,$tablecolor);
1.2 raeburn 74: } elsif ($command eq 'display') {
1.14 albertel 75: &print_display($r,$command,$cdom,$tablecolor);
1.1 raeburn 76: } else {
1.14 albertel 77: &print_front_page($r,'front',$cdom,$tablecolor);
1.1 raeburn 78: }
79: return OK;
80: }
81:
1.12 albertel 82: sub add_script {
83: my ($js) = @_;
84: return '<script type="text/javascript">'."\n".$js."\n".'</script>';
85: }
86:
87: sub start_page {
1.14 albertel 88: my ($jscript,$bread_title,$formname) = @_;
1.12 albertel 89:
1.14 albertel 90: my $loadcode;
91: if ((defined($env{'form.origin'}))
92: && ($env{'form.command'} eq 'compose'
93: || $env{'form.command'} eq 'pick_target'
94: || $env{'form.command'} eq 'pick_display')) {
95: if ($env{'form.origin'} ne '') {
96: $loadcode = 'javascript:setFormElements(document.'.$env{'form.command'}.')';
97: if (($env{'form.command'} eq 'pick_target')
98: && (($env{'form.origin'} eq 'compose')
99: || ($env{'form.origin'} eq 'process'))) {
100: if ($env{'form.coursepick'} eq 'category') {
101: $loadcode .= ';javascript:setCourseCat(document.'.$env{'form.command'}.')';
102: }
103: }
104: }
105: }
106:
1.15 albertel 107: my $start_page =
108: &Apache::loncommon::start_page('Broadcast e-mail to users', $jscript,
1.16 albertel 109: {'add_entries' =>
110: {'onload' => $loadcode,},});
1.12 albertel 111: my $breadcrumbs =
112: &Apache::lonhtmlcommon::breadcrumbs(undef,$bread_title,
113: 'Broadcast_system_email');
114: my $output = <<"ENDONE";
1.15 albertel 115: $start_page
1.12 albertel 116: $breadcrumbs
117: <br />
118: <form name="$formname" method="POST">
119: ENDONE
120:
121: return $output;
122: }
123:
124: sub end_page {
125: return '</form>'.&Apache::loncommon::end_page();
126: }
127:
1.2 raeburn 128: sub print_front_page {
1.14 albertel 129: my ($r,$formname,$cdom,$tablecolor) = @_;
1.12 albertel 130:
1.2 raeburn 131: my $jscript = qq|
132: function next_page(caller) {
133: if (caller == 'view') {
134: document.front.command.value="pick_display"
135: }
136: else {
137: document.front.command.value="pick_target"
138: }
139: document.front.submit()
140: }
141: |;
1.12 albertel 142:
1.14 albertel 143: my $output = &start_page(&add_script($jscript),
1.12 albertel 144: 'Broadcast e-mail to Domain', $formname);
145:
146: $output .= '<input type="hidden" name="command" />';
1.2 raeburn 147: $output .= &Apache::lonhtmlcommon::start_pick_box();
148: $output .= '<table cellspacing="8" cellpadding="8">'.
149: '<tr><td><a href="javascript:next_page('."'new'".')">'.
150: 'Send a new e-mail message to selected users from this domain</a></td></tr><tr>'.
151: '<td><a href="javascript:next_page('."'view'".')">'.
152: 'Display e-mail sent by Domain Coordinators in this domain'.
153: '</a></td></tr></table>';
154: $output .= &Apache::lonhtmlcommon::end_pick_box();
1.12 albertel 155: $output .= &end_page();
156:
1.2 raeburn 157: $r->print($output);
158: return;
159: }
160:
161: sub print_display_option_form {
1.14 albertel 162: my ($r,$formname,$cdom,$tablecolor) = @_;
1.12 albertel 163: &Apache::lonhtmlcommon::add_breadcrumb({text=>"Display options"});
164:
1.2 raeburn 165: my $table_width = '';
166: my $col_width = '200';
167: my $cmd = 'display';
168: my $submit_text = 'Display e-mail';
169: my @roles = ('dc');
170: my $now = time;
1.12 albertel 171:
1.2 raeburn 172: my $startdateform = &Apache::lonhtmlcommon::date_setter($formname,
173: 'startdate',
174: $now);
175: my $enddateform = &Apache::lonhtmlcommon::date_setter($formname,
176: 'enddate',
177: $now);
1.8 raeburn 178: my %elements = (
179: startdate_month => 'selectbox',
180: startdate_hour => 'selectbox',
181: enddate_month => 'selectbox',
182: enddate_hour => 'selectbox',
183: startdate_day => 'text',
184: startdate_year => 'text',
185: startdate_minute => 'text',
186: startdate_second => 'text',
187: enddate_day => 'text',
188: enddate_year => 'text',
189: enddate_minute => 'text',
190: enddate_second => 'text',
191: sender => 'checkbox',
192: );
193: my $jscript = &Apache::lonhtmlcommon::set_form_elements(\%elements);
1.12 albertel 194:
1.14 albertel 195: my $output = &start_page(&add_script($jscript),
1.12 albertel 196: 'Broadcast e-mail display options', $formname);
197:
1.2 raeburn 198: $output .= &Apache::lonhtmlcommon::start_pick_box($table_width);
199: $output .= &Apache::lonhtmlcommon::row_title($col_width,$tablecolor,&mt('Date range'));
200: $output .= '<td><table><tr><td>Earliest to display: </td><td>'.
201: $startdateform.'</td></tr>';
202: $output .= '<tr><td>Latest to display: </td><td>'.$enddateform.
203: '</td></tr></table></td>';
204: $output .= &Apache::lonhtmlcommon::row_closure();
205: $output .= &Apache::lonhtmlcommon::row_title($col_width,$tablecolor,&mt('Choose sender(s)'));
1.4 raeburn 206: my %personnel = &Apache::lonnet::get_domain_roles($cdom,\@roles);
1.2 raeburn 207: $output .= '<td>';
1.4 raeburn 208: my @domcc = ();
209: foreach my $server (keys %personnel) {
210: foreach my $user (sort(keys %{$personnel{$server}})) {
211: my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
212: unless (grep/^$uname:$udom$/,@domcc) {
213: my %userinfo = &Apache::lonnet::get('environment',['lastname','firstname'],$udom,$uname);
214: $output .= '<input type="checkbox" name="sender" value="'.$uname.':'.$udom.'" /> '.$userinfo{firstname}.' '.$userinfo{lastname}.' ('.$uname.':'.$udom.')';
215: push (@domcc,$uname.':'.$udom);
216: }
1.2 raeburn 217: }
218: }
219: $output .= '</td>';
220: $output .= &Apache::lonhtmlcommon::row_closure();
221: $output .= &Apache::lonhtmlcommon::submit_row($col_width,$tablecolor,&mt('Submit'),$cmd,$submit_text);
222: $output .= &Apache::lonhtmlcommon::end_pick_box();
1.13 albertel 223: $output .= qq(<input type="hidden" name="sortby" value="date" />\n).
224: &end_page();
1.2 raeburn 225: $r->print($output);
226: return;
227: }
228:
229: sub print_display {
1.14 albertel 230: my ($r,$formname,$cdom,$tablecolor) = @_;
1.2 raeburn 231: &Apache::lonhtmlcommon::add_breadcrumb
1.8 raeburn 232: ({href=>"javascript:goBack('pick_display')",
1.2 raeburn 233: text=>"Display options"},
234: {text=>"E-mail display"});
1.12 albertel 235:
1.2 raeburn 236: my $table_width = '';
237: my $col_width = '200';
238: my $rowColor1 = "#ffffff";
239: my $rowColor2 = "#eeeeee";
240: my $rowColor;
241: my $msgcount = 0;
242: my $start = &Apache::lonhtmlcommon::get_date_from_form('startdate');
243: my $end = &Apache::lonhtmlcommon::get_date_from_form('enddate');
244: my @senders = &Apache::loncommon::get_env_multiple('form.sender');
1.6 albertel 245: my %sentmail = &Apache::lonnet::dcmaildump($cdom,$start,$end,\@senders);
1.2 raeburn 246: my %dcmail = ();
247: my %Sortby = ();
248: my $jscript = <<"ENDSCRIPT";
249: function changeSort(caller) {
1.8 raeburn 250: document.$formname.command.value = '$formname';
1.2 raeburn 251: document.$formname.sortby.value = caller;
1.8 raeburn 252: document.$formname.submit();
253: }
254: function goBack(target) {
255: document.$formname.command.value = target;
256: document.$formname.submit();
1.2 raeburn 257: }
1.8 raeburn 258:
1.2 raeburn 259: ENDSCRIPT
1.12 albertel 260:
1.14 albertel 261: my $output = &start_page(&add_script($jscript),
1.12 albertel 262: 'Display Broadcast e-mail', $formname);
1.2 raeburn 263:
1.9 raeburn 264: foreach my $msgid (keys(%sentmail)) {
1.10 raeburn 265: my %content = &Apache::lonmsg::unpackagemsg($sentmail{$msgid});
1.9 raeburn 266: $msgcount ++;
267: %{$dcmail{$msgid}} = ();
268: foreach my $item (keys(%content)) {
1.10 raeburn 269: if ($item eq 'recipient') {
270: foreach my $user (keys(%{$content{recipient}})) {
271: $dcmail{$msgid}{recipient}{$user} = $content{recipient}{$user};
1.2 raeburn 272: }
273: } else {
1.9 raeburn 274: $dcmail{$msgid}{$item} = $content{$item};
1.2 raeburn 275: }
276: }
277: }
278: $output .= &Apache::lonhtmlcommon::start_pick_box();
279: if ($msgcount > 0) {
280: my $rowNum = 0;
281: $output .= '<tr><td><table cellpadding="4" cellspacing="2" width="100%">
282: <tr bgcolor="'.$tablecolor.'" align="center">
283: <td><b><a href="javascript:changeSort('."'date'".')">Date</a></b></td>
284: <td><b><a href="javascript:changeSort('."'subject'".')">Subject</a></b></td>
285: <td><b><a href="javascript:changeSort('."'sender'".')">Sender</a></b></td>
286: <td><b><a href="javascript:changeSort('."'message'".')">Message</a></b></td>
287: <td><b><a href="javascript:changeSort('."'recipients'".')">Recipients</a></b></td>
288: </tr>';
289: if (($env{'form.sortby'} eq 'date') || ($env{'form.sortby'} eq '') || (!defined($env{'form.sortby'})) || (($env{'form.sortby'} eq 'sender') && (@senders <= 1))) {
290: foreach my $msgid (sort(keys(%dcmail))) {
291: if ($rowNum %2 == 1) {
292: $rowColor = $rowColor1;
293: } else {
294: $rowColor = $rowColor2;
295: }
296: my $recipients = '';
1.17 ! albertel 297: my ($date,undef,$sname,$sdom) =
1.11 raeburn 298: &Apache::lonmsg::unpackmsgid($msgid,undef,1);
1.2 raeburn 299: $date = &Apache::lonlocal::locallocaltime($date);
1.10 raeburn 300: foreach my $user (sort(keys(%{$dcmail{$msgid}{recipient}}))) {
301: $recipients .= $dcmail{$msgid}{recipient}{$user}.', ';
1.2 raeburn 302: }
303: $recipients =~ s/,\s$//;
1.5 raeburn 304: $output .= '<tr bgcolor="'.$rowColor.'"><td><small>'.$date.'</small></td><td><small>'.&cr_to_br($dcmail{$msgid}{subject}).'</small></td><td><small>'.$sname.':'.$sdom.'</small></td><td><small>'.&cr_to_br($dcmail{$msgid}{message}).'</small></td><td><small>'.$recipients.'</small></td></tr>'."\n";
1.2 raeburn 305: $rowNum ++;
306: }
307: } else {
308: foreach my $msgid (sort(keys(%dcmail))) {
1.17 ! albertel 309: my ($date,undef,$sname,$sdom) =
1.11 raeburn 310: &Apache::lonmsg::unpackmsgid($msgid,undef,1);
1.2 raeburn 311: if ($env{'form.sortby'} eq 'subject') {
312: push @{$Sortby{$dcmail{$msgid}{subject}}},$msgid;
313: } elsif ($env{'form.sortby'} eq 'message') {
314: push @{$Sortby{$dcmail{$msgid}{message}}},$msgid;
315: } elsif ($env{'form.sortby'} eq 'recipients') {
316: my $recipients ='';
1.10 raeburn 317: foreach my $user (sort(keys(%{$dcmail{$msgid}{recipient}}))) {
318: $recipients .= $dcmail{$msgid}{recipient}{$user}.', ';
1.2 raeburn 319: }
320: $recipients =~ s/,\s$//;
321: push @{$Sortby{$recipients}},$msgid;
322: } elsif ($env{'form.sortby'} eq 'sender') {
323: if (@senders > 1) {
324: push @{$Sortby{$sname.':'.$sdom}},$msgid;
325: }
326: }
327: }
328: foreach my $key (sort(keys(%Sortby))) {
329: foreach my $msgid (@{$Sortby{$key}}) {
330: if ($rowNum %2 == 1) {
331: $rowColor = $rowColor1;
332: } else {
333: $rowColor = $rowColor2;
334: }
335: my $recipients = '';
336: if ($env{'form.sortby'} eq 'recipients') {
337: $recipients = $key;
338: } else {
1.10 raeburn 339: foreach my $user (sort(keys(%{$dcmail{$msgid}{recipient}}))) {
340: $recipients .= $dcmail{$msgid}{recipient}{$user}.', ';
1.2 raeburn 341: }
342: $recipients =~ s/,\s$//;
343: }
1.17 ! albertel 344: my ($date,undef,$sname,$sdom) =
1.11 raeburn 345: &Apache::lonmsg::unpackmsgid($msgid,undef,1);
1.2 raeburn 346: $date = &Apache::lonlocal::locallocaltime($date);
1.5 raeburn 347: $output .= '<tr bgcolor="'.$rowColor.'"><td><small>'.$date.'</small></td><td><small>'.&cr_to_br($dcmail{$msgid}{subject}).'</small></td><td><small>'.$sname.':'.$sdom.'</small></td><td><small>'.&cr_to_br($dcmail{$msgid}{message}).'</small></td><td><small>'.$recipients.'</small></td></tr>'."\n";
1.2 raeburn 348: $rowNum ++;
349: }
350: }
351: }
352: $output .= '</table></td></tr>';
353: } else {
354: $output .= '<tr bgcolor="#ffffff"><td> </td><td><br><center><i><b><small> No mail sent matching supplied criteria </small><br><br></b></i></td><td> </td></tr>';
355: }
356: $output .= &Apache::lonhtmlcommon::end_pick_box();
1.8 raeburn 357: $output .= &Apache::lonhtmlcommon::echo_form_input(['sortby','command','origin']);
358: my $curr_sortby;
359: if (defined($env{'form.sortby'})) {
360: $curr_sortby = $env{'form.sortby'};
361: } else {
362: $curr_sortby = 'date';
363: }
364: $output .= qq(<input type="hidden" name="origin" value="$formname" />\n);
365: $output .= qq(<input type="hidden" name="command" />\n);
366: $output .= qq(<input type="hidden" name="sortby" value="$curr_sortby" />\n);
1.13 albertel 367: $output .= &end_page();
1.2 raeburn 368: $r->print($output);
369: return;
370: }
371:
1.1 raeburn 372: sub print_selection_form {
1.14 albertel 373: my ($r,$formname,$cdom,$tablecolor) = @_;
1.1 raeburn 374: my %coursecodes = ();
375: my %codes = ();
376: my @codetitles = ();
377: my %cat_titles = ();
378: my %cat_order = ();
379: my %idlist = ();
380: my %idnums = ();
381: my %idlist_titles = ();
382: my $caller = 'global';
383: my $totcodes = 0;
384: my $format_reply;
385: my $jscript = '';
1.2 raeburn 386: my $table_width = '100%';
387: my $col_width = '200';
388: my %lt=&Apache::lonlocal::texthash(
389: 'buil' => 'Building valid e-mail address from username, if missing from preferences:',
390: 'kerb' => 'Kerberos: enter default for each realm used in the domain, with comma separation of entries',
391: 'infs' => 'Internal, Filesystem and Local authentication: enter single default.',
392: 'comp' => 'Compose Message'
393: );
394: &Apache::lonhtmlcommon::add_breadcrumb
395: ({text=>"Select Audience"});
396:
1.1 raeburn 397: $totcodes = &Apache::lonsupportreq::retrieve_instcodes(\%coursecodes,$cdom,$totcodes);
398: if ($totcodes > 0) {
399: $format_reply = &Apache::lonnet::auto_instcode_format($caller,$cdom,\%coursecodes,\%codes,\@codetitles,\%cat_titles,\%cat_order);
400: if ($format_reply eq 'ok') {
401: my $numtypes = @codetitles;
402: &Apache::lonsupportreq::build_code_selections(\%codes,\@codetitles,\%cat_titles,\%cat_order,\%idlist,\%idnums,\%idlist_titles);
1.2 raeburn 403: &Apache::lonsupportreq::javascript_code_selections($formname,$numtypes,\%cat_titles,\$jscript,\%idlist,\%idnums,\%idlist_titles,\@codetitles);
1.1 raeburn 404: }
405: }
406:
1.3 albertel 407: my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($cdom);
1.8 raeburn 408:
409: my %elements = (
410: roles => 'selectbox',
411: types => 'selectbox',
412: Year => 'selectbox',
413: coursepick => 'radio',
414: coursetotal => 'text',
415: courselist => 'text',
416: internal => 'text',
417: krb4 => 'text',
418: krb5 => 'text',
419: local => 'text',
420: unix => 'text',
421: );
422: $jscript .= &Apache::lonhtmlcommon::set_form_elements(\%elements);
423: if ($env{'form.coursepick'} eq 'category') {
424: $jscript .= qq|
425: function setCourseCat(formname) {
426: if (formname.Year.options[formname.Year.selectedIndex].value == -1) {
427: return;
428: }
429: courseSet('Year');
430: for (var j=0; j<formname.Semester.length; j++) {
431: if (formname.Semester.options[j].value == "$env{'form.Semester'}") {
432: formname.Semester.options[j].selected = true;
433: }
434: }
435: if (formname.Semester.options[formname.Semester.selectedIndex].value == -1) {
436: return;
437: }
438: courseSet('Semester');
439: for (var j=0; j<formname.Department.length; j++) {
440: if (formname.Department.options[j].value == "$env{'form.Department'}") {
441: formname.Department.options[j].selected = true;
442: }
443: }
444: if (formname.Department.options[formname.Department.selectedIndex].value == -1) {
445: return;
446: }
447: courseSet('Department');
448: for (var j=0; j<formname.Number.length; j++) {
449: if (formname.Number.options[j].value == "$env{'form.Number'}") {
450: formname.Number.options[j].selected = true;
451: }
452: }
453: }
454: |;
455: }
1.12 albertel 456:
457:
458: my $output = &start_page(&add_script($jscript).$cb_jscript,
1.14 albertel 459: 'Choose e-mail audience', $formname);
1.12 albertel 460:
1.13 albertel 461: $output .= &Apache::lonhtmlcommon::start_pick_box($table_width);
462: my @roles = ('ow','cc','in','ta','ep','st','cr');
463: my %longtypes = ();
464: my %authtypes = ();
465: &form_elements(\%longtypes,\%authtypes);
466: my $descrip = $lt{'buil'}.'
1.2 raeburn 467: <ul>
468: <li>'.$lt{'kerb'}.'<br />(e.g., MSU.EDU=msu.edu, MSUE.EDU=msue.msu.edu).</li>
469: <li>'.$lt{'infs'}.'</li>
470: </ul>'."\n";
1.13 albertel 471: my $submit_text = $lt{'comp'};
472: my $cmd = 'compose';
473: $output .= &Apache::lonhtmlcommon::role_select_row(\@roles,$col_width,$tablecolor,'Roles');
474: $output .= &Apache::lonhtmlcommon::course_select_row($col_width,$tablecolor,'Courses',$formname,$totcodes,\@codetitles,\%idlist,\%idlist_titles);
475: $output .= &Apache::lonhtmlcommon::status_select_row(\%longtypes,$col_width,$tablecolor,&mt('Access status'));
476: $output .= &Apache::lonhtmlcommon::email_default_row(\%authtypes,$col_width,$tablecolor,&mt('Username -> Email conversion'),$descrip);
477: $output .= &Apache::lonhtmlcommon::submit_row($col_width,$tablecolor,&mt('Submit'),$cmd,$submit_text);
478: $output .= &Apache::lonhtmlcommon::end_pick_box();
479: $output .= &end_page();
1.2 raeburn 480: $r->print($output);
1.1 raeburn 481: return;
482: }
483:
484: sub print_composition_form {
1.14 albertel 485: my ($r,$formname,$cdom,$tablecolor) = @_;
1.1 raeburn 486: &Apache::lonhtmlcommon::add_breadcrumb
1.8 raeburn 487: ({href=>"javascript:goBack('pick_target')",
1.2 raeburn 488: text=>"Select Audience"},
489: {text=>"Compose Message"});
490: my $jscript = &Apache::loncommon::check_uncheck_jscript();
1.8 raeburn 491: $jscript .= qq|
492: function goBack(target) {
493: document.$formname.command.value = target;
494: document.$formname.submit();
495: }
496: |;
1.1 raeburn 497:
1.2 raeburn 498: my %lt=&Apache::lonlocal::texthash(
499: 'nore' => 'No recipients identified',
500: 'emad' => 'e-mail address',
501: );
1.8 raeburn 502: my %elements = (
503: subject => 'text',
504: message => 'text',
505: sender => 'text',
506: recipient => 'checkbox',
507: );
508: $jscript .= &Apache::lonhtmlcommon::set_form_elements(\%elements);
509:
1.14 albertel 510: $r->print(&start_page(&add_script($jscript),
1.12 albertel 511: 'Broadcast e-mail to users', $formname));
512:
1.2 raeburn 513: my $coursefilter = $env{'form.coursepick'};
514: my %courses = ();
515: if ($coursefilter eq 'all') {
516: %courses = &Apache::lonnet::courseiddump($cdom,'.','.','.','.','.');
517: } elsif ($coursefilter eq 'category') {
518: my $instcode = '';
519: my @cats = ('Semester','Year','Department','Number');
520: foreach my $category (@cats) {
521: if (defined($env{'form.'.$category})) {
522: unless ($env{'form.'.$category} eq '-1') {
523: $instcode .= $env{'form.'.$category};
524: }
1.1 raeburn 525: }
526: }
1.2 raeburn 527: if ($instcode eq '') {
528: $instcode = '.';
529: }
530: %courses = &Apache::lonnet::courseiddump($cdom,'.','.',$instcode,'.','.');
531: } elsif ($coursefilter eq 'specific') {
532: if ($env{'form.coursetotal'} > 1) {
533: my @course_ids = split(/&&/,$env{'form.courselist'});
534: foreach (@course_ids) {
535: $courses{$_} = '';
536: }
537: } else {
538: $courses{$env{'form.courselist'}} = '';
539: }
1.1 raeburn 540: }
1.2 raeburn 541:
542: my @types = &Apache::loncommon::get_env_multiple('form.types');
543: my @roles = &Apache::loncommon::get_env_multiple('form.roles');
544:
545: my %longtypes = ();
546: my %authtypes = ();
547: my %email_defaults = ();
548: my $table_width = '100%';
549: my $col_width = '200';
550:
551: &form_elements(\%longtypes,\%authtypes);
1.3 albertel 552: foreach my $auth (keys(%authtypes)) {
1.2 raeburn 553: if (exists($env{'form.'.$auth})) {
554: my $default = $env{'form.'.$auth};
555: $default =~ s/^,+//;
556: $default =~ s/,+$//;
557: if ($auth =~ /^krb/) {
558: %{$email_defaults{$auth}} = ();
559: if ($default =~ /,/) {
560: my @items = split(/,/,$default);
561: foreach my $item (@items) {
562: my ($realm,$value) = split(/=/,$item);
563: $email_defaults{$auth}{$realm} = $value;
564: }
565: } else {
566: my ($realm,$value) = split(/=/,$default);
567: $email_defaults{$auth}{$realm} = $value;
568: }
569: } else {
570: $email_defaults{$auth} = $default;
571: }
572: }
1.1 raeburn 573: }
1.2 raeburn 574:
575: my $sender = &get_user_info($env{'user.name'},%email_defaults);
576:
1.1 raeburn 577: my %recipients = ();
1.2 raeburn 578: my %users = ();
579: my %access = ();
1.8 raeburn 580: my @sections = ();
1.2 raeburn 581: my $totalrecip = 0;
582: my @unmatched = ();
583: foreach my $role (@roles) {
584: %{$users{$role}} = ();
585: }
586: foreach my $type (@types) {
587: $access{$type} = $type;
588: }
589: foreach my $course_id (keys(%courses)) {
590: my ($cdom,$cnum) = split(/_/,$course_id);
1.8 raeburn 591: &Apache::loncommon::get_course_users($cdom,$cnum,\%access,\@roles,\@sections,\%users);
1.2 raeburn 592: }
593: foreach my $role (keys(%users)) {
594: foreach my $user (keys(%{$users{$role}})) {
595: unless (defined($recipients{$user})) {
596: $recipients{$user} = &get_user_info($user,%email_defaults);
597: if ($recipients{$user} eq '') {
598: push @unmatched, $user;
599: } else {
600: $totalrecip ++;
601: }
602: }
603: }
1.1 raeburn 604: }
1.12 albertel 605: my $output;
1.2 raeburn 606:
607: if ($totalrecip > 0) {
608: $output .= &Apache::lonhtmlcommon::start_pick_box($table_width);
609: $output .= &Apache::lonhtmlcommon::row_title($col_width,$tablecolor,&mt('Subject'));
610: $output .= ' <td><input type="text" name="subject" size="30" /></td>';
611: $output .= &Apache::lonhtmlcommon::row_closure();
612: $output .= &Apache::lonhtmlcommon::row_title($col_width,$tablecolor,&mt('Message'));
613: $output .= ' <td><textarea name="message" id="message"
614: cols="60" rows="10" wrap="hard"></textarea></td>';
615: $output .= &Apache::lonhtmlcommon::row_closure();
616: $output .= &Apache::lonhtmlcommon::row_title($col_width,$tablecolor,&mt('Recipients'));
617: $output .= '<td><input type="button" value="check all"
618: onclick="javascript:checkAll(document.compose.recipient)" />
619: <input type="button" value="uncheck all"
620: onclick="javascript:uncheckAll(document.compose.recipient)" />
621: <br /><table border="0">';
622: if (keys(%recipients) > 0) {
623: $output .= '<tr><td> </td><td><small><b>username:domain</b></small></td><td> </td><td><small><b>'.$lt{'emad'}.'</b></small></td></tr>';
624: }
625: foreach my $username (sort(keys(%recipients))) {
1.1 raeburn 626: if ($recipients{$username} =~ /\@/) {
627: my $value=&Apache::lonnet::escape($username).':'.&Apache::lonnet::escape($recipients{$username});
1.8 raeburn 628: $output .= '<tr><td><input type="checkbox" name="recipient" value="'.$value.'" /></td><td>'.$username.'</td><td> </td><td>'.$recipients{$username}.'</td></tr>';
1.1 raeburn 629: }
630: }
1.5 raeburn 631: $output .= '</table>';
632: if (@unmatched) {
633: $output .= '<br /><br />'.&mt('Could not determine e-mail addresses for the following users:').'<ul>';
634: foreach my $username (sort @unmatched) {
635: $output .= '<li>'.$username.'</li>';
636: }
637: $output .= '</ul>';
638: }
639: $output .= '</td>';
1.2 raeburn 640: $output .= &Apache::lonhtmlcommon::row_closure();
641: $output .= &Apache::lonhtmlcommon::row_title($col_width,$tablecolor,&mt('Sender e-mail address'));
642: $output .= '<td><input type="text" name="sender" value="'.$sender.'" /></td>';
643: $output .= &Apache::lonhtmlcommon::row_closure();
644: $output .= &Apache::lonhtmlcommon::submit_row($col_width,$tablecolor,&mt('Submit'),'process',&mt('Send Message'));
645: $output .= &Apache::lonhtmlcommon::end_pick_box();
1.1 raeburn 646: } else {
1.8 raeburn 647: $output .= $lt{'nore'}."\n".
648: '<input type="hidden" name="command" value="" />'."\n";
1.1 raeburn 649: }
1.8 raeburn 650: $output .= '<input type="hidden" name="origin" value="'.$formname.'" />'."\n";
651: $output .= &Apache::lonhtmlcommon::echo_form_input(['command','origin','subject','message','recipient','sender'],);
1.13 albertel 652: $output .= &end_page();
1.2 raeburn 653: $r->print($output);
1.1 raeburn 654: return;
655: }
656:
657:
658: sub print_request_receipt {
1.14 albertel 659: my ($r,$formname,$dom,$tablecolor) =@_;
1.2 raeburn 660: my @recipients = &Apache::loncommon::get_env_multiple('form.recipient');
1.1 raeburn 661: my $subject = $env{'form.subject'};
662: my $message = $env{'form.message'};
1.2 raeburn 663: my $from = $env{'form.sender'};
664: my $jscript = <<ENDSCRIPT;
1.8 raeburn 665: function goBack(target) {
666: document.$formname.command.value = target;
667: document.$formname.submit();
1.2 raeburn 668: }
669: ENDSCRIPT
1.12 albertel 670:
1.2 raeburn 671: &Apache::lonhtmlcommon::add_breadcrumb
1.8 raeburn 672: ({href=>"javascript:goBack('pick_target')",
1.2 raeburn 673: text=>"Select audience"});
674: &Apache::lonhtmlcommon::add_breadcrumb
1.8 raeburn 675: ({href=>"javascript:goBack('compose')",
1.2 raeburn 676: text=>"Compose Message"});
677: &Apache::lonhtmlcommon::add_breadcrumb
1.8 raeburn 678: ({href=>"/adm/notify?command=process",
1.2 raeburn 679: text=>"Outcome"});
1.12 albertel 680:
681:
1.14 albertel 682: my $output = &start_page(&add_script($jscript), 'E-mail Delivery',
683: $formname);
1.12 albertel 684:
1.2 raeburn 685: $output .= &Apache::lonhtmlcommon::start_pick_box();
686: my @deliveries = ();
687: &broadcast_email(\@recipients,$subject,$from,$message,\@deliveries);
688: if (@deliveries > 0) {
1.5 raeburn 689: &store_mail($subject,$message,$dom,\@deliveries);
1.2 raeburn 690: $output .= '<tr>
691: <td>
692: <table cellpadding="4" cellspacing="2" width="100%">
693: <tr bgcolor="'.$tablecolor.'" align="center">
694: <td><b>Status</b></td>
695: <td><b>Subject</b></td>
696: <td><b>Message</b></td>
697: <td><b>Recipients</b></td>
698: </tr>
699: <tr bgcolor="#eeeeee">
700: <td valign="middle">Sent</td>
1.5 raeburn 701: <td valign="middle">'.&cr_to_br($subject).'</td>
702: <td valign="middle">'.&cr_to_br($message).'</td>
1.2 raeburn 703: <td>';
704: foreach my $person (@deliveries) {
705: my ($username,$email) = split(/:/,$person);
706: $output .= &Apache::lonnet::unescape($email).' ('.&Apache::lonnet::unescape($username).')<br />'."\n";
707: }
708: $output .= '</td>
709: </tr>
710: </table>
711: </td>
712: </tr>';
713: } else {
714: $output .= 'No mail sent - no recipients identified';
1.1 raeburn 715: }
1.2 raeburn 716: $output .= &Apache::lonhtmlcommon::end_pick_box();
717: $output .= '<br /><a href="/adm/notify">Send another message?</a>'."\n";
1.8 raeburn 718: $output .= '<input type="hidden" name="command" />'."\n".
719: '<input type="hidden" name="origin" value="'.$formname.'" />'."\n";
720: $output .= &Apache::lonhtmlcommon::echo_form_input(['command','origin']);
1.13 albertel 721: $output .= &end_page();
1.2 raeburn 722: $r->print($output);
1.1 raeburn 723: return;
724: }
725:
1.2 raeburn 726: sub broadcast_email {
1.12 albertel 727: my ($recipients,$subject,$from,$message,$deliveries)=@_;
1.8 raeburn 728: # Should implement staggered delivery for large numbers of recipients?.
1.2 raeburn 729: foreach my $user (@{$recipients}) {
730: my $msg = new Mail::Send;
731: my ($username,$to) = split(/:/,$user);
732: $username = &Apache::lonnet::unescape($username);
733: $to = &Apache::lonnet::unescape($to);
734: $msg->to($to);
735: $msg->subject($subject);
736: $msg->add('From',"$from");
737: if (my $fh = $msg->open()) {
738: print $fh $message;
739: $fh->close;
740: push(@{$deliveries},$user);
741: }
742: }
743: }
744:
745: sub get_user_info {
1.5 raeburn 746: my ($user,%email_defaults) = @_;
1.2 raeburn 747: my ($uname,$udom) = split(/:/,$user);
748: my @emailtypes = ('permanentemail','critnotification','notification');
1.3 albertel 749: my %userinfo = &Apache::lonnet::get('environment',\@emailtypes,$udom,$uname);
1.2 raeburn 750: my $email = '';
751: foreach my $type (@emailtypes) {
752: $email = $userinfo{$type};
753: if ($email =~ /\@/) {
754: last;
755: }
756: }
757: if ($email eq '') {
758: my $authinfo = &Apache::lonnet::queryauthenticate($uname,$udom);
759: my ($authtype,$autharg) = split(/:/,$authinfo);
760: if ($authtype =~ /^krb/) {
761: if (defined($email_defaults{$authtype}{$autharg})) {
762: $email = $uname.'@'.$email_defaults{$authtype}{$autharg};
763: }
764: } else {
1.5 raeburn 765: if ((defined($email_defaults{$authtype})) && ($email_defaults{$authtype} ne '')) {
1.2 raeburn 766: $email = $uname.'@'.$email_defaults{$authtype};
767: }
768: }
769: }
770: return $email;
771: }
772:
773: sub form_elements {
1.12 albertel 774: my ($longtypes,$authtypes) = @_;
1.2 raeburn 775: %{$longtypes} = (
776: active => 'Currently has access',
777: previous => 'Previously had access',
778: future => 'Will have future access',
779: );
780: %{$authtypes} = (
781: krb4 => 'Kerberos 4',
782: krb5 => 'Kerberos 5',
1.5 raeburn 783: internal => 'Internal (LON-CAPA)',
1.2 raeburn 784: unix => 'Filesystem (UNIX)',
785: local => 'Local/Customized',
786: );
787: return;
788: }
789:
790: sub store_mail {
1.12 albertel 791: my ($subject,$message,$domain,$recipients,$attachmenturl) = @_;
1.9 raeburn 792: my $msgid;
1.10 raeburn 793: ($msgid,$message) = &Apache::lonmsg::packagemsg($subject,$message,undef,undef,
794: $attachmenturl,$recipients,undef,undef,'dcmail');
795:
1.9 raeburn 796: # Store in dc email db files on primary library server for domain.
797: my $server = $Apache::lonnet::domain_primary{$domain};
798: if (defined($server)) {
799: unless (&Apache::lonnet::dcmailput($domain,$msgid,$message,$server)
800: eq 'ok') {
801: &Apache::lonnet::logthis('Storage of dc mail failed for domain'.
802: $domain.' for server: '. $server.'. Message ID was '.$msgid);
1.4 raeburn 803: }
1.9 raeburn 804: } else {
805: &Apache::lonnet::logthis('Storage of dc mail failed for domain'.
806: $domain.' as no primary server identified. Message ID was '.$msgid);
1.2 raeburn 807: }
808: }
809:
1.5 raeburn 810: sub cr_to_br {
811: my $incoming = shift;
812: $incoming =~ s/\n/\<br \/\>/g;
813: return $incoming;
814: }
815:
1.1 raeburn 816: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>