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