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