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